blob: bb58081ce7067dcba57f70cc65ac7403baa0523b [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka742574b2011-02-02 23:51:01 -080020import com.android.common.Search;
21import com.android.launcher.R;
Michael Jurka742574b2011-02-02 23:51:01 -080022import com.android.launcher2.Workspace.ShrinkState;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070023
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080025import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.AnimatorSet;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Michael Jurka742574b2011-02-02 23:51:01 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.Dialog;
32import android.app.SearchManager;
33import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070038import android.content.ClipData;
39import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040040import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080041import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Context;
43import android.content.DialogInterface;
44import android.content.Intent;
Winson Chung400438b2011-01-16 17:53:48 -080045import android.content.IntentFilter;
Adam Cohen860f4c52011-01-27 20:16:13 -080046import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Winson Chung400438b2011-01-16 17:53:48 -080049import android.content.pm.ResolveInfo;
Adam Cohen860f4c52011-01-27 20:16:13 -080050import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080051import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070052import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040053import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080054import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080056import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.os.Parcelable;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080068import android.os.SystemProperties;
Karl Rosaen138a0412009-04-23 19:00:21 -070069import android.provider.LiveFolders;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070070import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080071import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.Selection;
73import android.text.SpannableStringBuilder;
74import android.text.TextUtils;
75import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070076import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080077import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080078import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.KeyEvent;
80import android.view.LayoutInflater;
81import android.view.Menu;
82import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070083import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080084import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.view.View;
86import android.view.ViewGroup;
Winson Chungaafa03c2010-06-11 17:34:16 -070087import android.view.WindowManager;
Michael Jurkae0f5a612011-02-07 16:45:41 -080088import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080089import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080091import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070093import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070095import android.widget.ImageView;
96import android.widget.LinearLayout;
97import android.widget.PopupWindow;
Michael Jurka0e260592010-06-30 17:07:39 -070098import android.widget.TabHost;
Winson Chung68846fd2010-10-29 11:00:27 -070099import android.widget.TextView;
100import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700101
Michael Jurka742574b2011-02-02 23:51:01 -0800102import java.io.DataInputStream;
103import java.io.DataOutputStream;
104import java.io.FileNotFoundException;
105import java.io.IOException;
106import java.util.ArrayList;
107import java.util.HashMap;
108import java.util.List;
Romain Guyedcce092010-03-04 13:03:17 -0800109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110/**
111 * Default launcher application.
112 */
Michael Jurka946ad472010-07-09 18:05:18 -0700113public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700114 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
115 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800116 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700117 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400120 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400121 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800124 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
125
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700127 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
128 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
130 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700131 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 private static final int REQUEST_CREATE_SHORTCUT = 1;
134 private static final int REQUEST_CREATE_LIVE_FOLDER = 4;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700135 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 private static final int REQUEST_PICK_APPLICATION = 6;
137 private static final int REQUEST_PICK_SHORTCUT = 7;
138 private static final int REQUEST_PICK_LIVE_FOLDER = 8;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
142 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
143
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800144 static final int SCREEN_COUNT = 5;
145 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Joe Onorato7c312c12009-08-13 21:36:53 -0700147 static final int DIALOG_CREATE_SHORTCUT = 1;
148 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Romain Guy98d01652009-06-30 16:21:04 -0700150 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
152 // Type: int
153 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700154 // Type: int
155 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: long
157 private static final String RUNTIME_STATE_USER_FOLDERS = "launcher.user_folder";
158 // Type: int
159 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
160 // Type: int
161 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
162 // Type: int
163 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 // Type: boolean
165 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
166 // Type: long
167 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
168
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700169 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
170
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700171 /** The different states that Launcher can be in. */
Michael Jurkaafca9532011-02-16 14:50:35 -0800172 private enum State { WORKSPACE, ALL_APPS, CUSTOMIZE,
Michael Jurkad3ef3062010-11-23 16:23:58 -0800173 CUSTOMIZE_SPRING_LOADED, ALL_APPS_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700174 private State mState = State.WORKSPACE;
175 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700176
Joe Onorato9c1289c2009-08-17 11:03:03 -0400177 static final int APPWIDGET_HOST_ID = 1024;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800180 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800182 private final BroadcastReceiver mCloseSystemDialogsReceiver
183 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800184 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private LayoutInflater mInflater;
187
Joe Onorato00acb122009-08-04 16:04:30 -0400188 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700190
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700191 private AppWidgetManager mAppWidgetManager;
192 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700193
Michael Jurka0280c3b2010-09-17 15:00:07 -0700194 private int mAddScreen = -1;
195 private int mAddIntersectCellX = -1;
196 private int mAddIntersectCellY = -1;
197 private int[] mAddDropPosition;
198 private int[] mTmpAddItemCellCoordinates = new int[2];
199
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200 private FolderInfo mFolderInfo;
201
Joe Onorato7c312c12009-08-13 21:36:53 -0700202 private DeleteZone mDeleteZone;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700203 private HandleView mHandleView;
Joe Onorato7c312c12009-08-13 21:36:53 -0700204 private AllAppsView mAllAppsGrid;
Michael Jurka7ef959b2011-02-23 11:48:32 -0800205 private CustomizeTrayTabHost mHomeCustomizationDrawer;
Adam Cohended9f8d2010-11-03 13:25:16 -0700206 private boolean mAutoAdvanceRunning = false;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700207
Michael Jurka838a4ca2011-02-07 13:33:06 -0800208 private View mButtonCluster;
209 private View mAllAppsButton;
210 private View mDivider;
211 private View mConfigureButton;
212
Winson Chunga12a2502010-12-20 14:41:35 -0800213 private AllAppsPagedView mAllAppsPagedView = null;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -0700214 private CustomizePagedView mCustomizePagedView = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private Bundle mSavedState;
217
218 private SpannableStringBuilder mDefaultKeySsb = null;
219
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 private boolean mWorkspaceLoading = true;
221
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800222 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private boolean mRestoring;
224 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700225 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800226
227 private Bundle mSavedInstanceState;
228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800230 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800231 private boolean mUserPresent = true;
232 private boolean mVisible = false;
233 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700235 private static LocaleConfiguration sLocaleConfiguration = null;
236
Romain Guy84f296c2009-11-04 15:00:44 -0800237 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700238
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700239 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700240
Patrick Dubroy94f78a52011-02-28 17:39:16 -0800241 // The "signpost" images along the bottom of the screen (only in some layouts)
Romain Guy1fbc1c82009-11-09 20:43:08 -0800242 private ImageView mPreviousView;
243 private ImageView mNextView;
Joe Onorato080d9b62009-11-02 12:01:11 -0500244
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400245 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400246 private String[] mHotseatConfig = null;
247 private Intent[] mHotseats = null;
248 private Drawable[] mHotseatIcons = null;
249 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400250
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700251 private Intent mAppMarketIntent = null;
252
Adam Cohended9f8d2010-11-03 13:25:16 -0700253 // Related to the auto-advancing of widgets
254 private final int ADVANCE_MSG = 1;
255 private final int mAdvanceInterval = 20000;
256 private final int mAdvanceStagger = 250;
257 private long mAutoAdvanceSentTime;
258 private long mAutoAdvanceTimeLeft = -1;
259 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
260 new HashMap<View, AppWidgetProviderInfo>();
261
Winson Chung400438b2011-01-16 17:53:48 -0800262 // Determines how long to wait after a rotation before restoring the screen orientation to
263 // match the sensor state.
264 private final int mRestoreScreenOrientationDelay = 500;
265
Michael Jurka4ef207b2010-11-29 17:05:45 -0800266 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800267 private static Drawable.ConstantState sGlobalSearchIcon;
268 private static Drawable.ConstantState sVoiceSearchIcon;
269 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800270
Michael Jurkaddd62e92011-02-16 17:49:14 -0800271 private BubbleTextView mWaitingForResume;
272
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800273 private static ArrayList<PendingAddArguments> sPendingAddList
274 = new ArrayList<PendingAddArguments>();
275
276 private static class PendingAddArguments {
277 int requestCode;
278 Intent intent;
279 int screen;
280 int cellX;
281 int cellY;
282 }
283
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 @Override
285 protected void onCreate(Bundle savedInstanceState) {
286 super.onCreate(savedInstanceState);
Romain Guyb1b69f52009-08-10 15:10:15 -0700287
Winson Chungaafa03c2010-06-11 17:34:16 -0700288 if (LauncherApplication.isInPlaceRotationEnabled()) {
289 // hide the status bar (temporary until we get the status bar design figured out)
290 getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
291 this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR);
292 }
293
Joe Onorato0589f0f2010-02-08 13:44:00 -0800294 LauncherApplication app = ((LauncherApplication)getApplication());
295 mModel = app.setLauncher(this);
296 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400297 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700299
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700300 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700301 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
302 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200305 android.os.Debug.startMethodTracing(
306 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 }
308
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400309 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 setContentView(R.layout.launcher);
Michael Jurka7ef959b2011-02-23 11:48:32 -0800312 mHomeCustomizationDrawer = (CustomizeTrayTabHost) findViewById(R.id.customization_drawer);
Michael Jurka946ad472010-07-09 18:05:18 -0700313 if (mHomeCustomizationDrawer != null) {
Winson Chung80baf5a2010-08-09 16:03:15 -0700314 // share the same customization workspace across all the tabs
Michael Jurka7ef959b2011-02-23 11:48:32 -0800315 mCustomizePagedView = (CustomizePagedView) findViewById(
316 R.id.customization_drawer_tab_contents);
Michael Jurka946ad472010-07-09 18:05:18 -0700317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 setupViews();
319
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800320 registerContentObservers();
321
Joe Onorato7c312c12009-08-13 21:36:53 -0700322 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700323
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324 mSavedState = savedInstanceState;
325 restoreState(mSavedState);
326
Winson Chunga12a2502010-12-20 14:41:35 -0800327 // Update customization drawer _after_ restoring the states
tedbo2b0857a2010-12-22 18:36:24 -0800328 if (mCustomizePagedView != null) {
329 mCustomizePagedView.update();
330 }
Winson Chunga12a2502010-12-20 14:41:35 -0800331
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 if (PROFILE_STARTUP) {
333 android.os.Debug.stopMethodTracing();
334 }
335
336 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400337 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 }
339
340 // For handling default keys
341 mDefaultKeySsb = new SpannableStringBuilder();
342 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800343
344 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
345 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800346
347 // If we have a saved version of these external icons, we load them up immediately
348 if (LauncherApplication.isScreenXLarge()) {
Winson Chungc6d20002011-03-02 11:57:29 -0800349 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
350 updateIconsAffectedByPackageManagerChanges();
Michael Jurka4ef207b2010-11-29 17:05:45 -0800351 }
Michael Jurka1c3f8ec2011-03-07 16:41:21 -0800352 if (sGlobalSearchIcon != null) {
353 updateGlobalSearchIcon(sGlobalSearchIcon);
354 }
355 if (sVoiceSearchIcon != null) {
356 updateVoiceSearchIcon(sVoiceSearchIcon);
357 }
358 if (sAppMarketIcon != null) {
359 updateAppMarketIcon(sAppMarketIcon);
360 }
Michael Jurka4ef207b2010-11-29 17:05:45 -0800361 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 }
Romain Guycbb89e42009-06-08 15:52:54 -0700363
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -0800364 @Override
365 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
366 super.dispatchPopulateAccessibilityEvent(event);
367
368 // we want to take over text population so it is context dependent
369 event.getText().clear();
370 if (mState == State.ALL_APPS) {
371 event.getText().add(getString(R.string.all_apps_button_label));
372 } else if (mState == State.WORKSPACE) {
373 event.getText().add(getString(R.string.all_apps_home_button_label));
374 }
375
376 return true;
377 }
378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700380 if (sLocaleConfiguration == null) {
381 new AsyncTask<Void, Void, LocaleConfiguration>() {
382 @Override
383 protected LocaleConfiguration doInBackground(Void... unused) {
384 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
385 readConfiguration(Launcher.this, localeConfiguration);
386 return localeConfiguration;
387 }
388
389 @Override
390 protected void onPostExecute(LocaleConfiguration result) {
391 sLocaleConfiguration = result;
392 checkForLocaleChange(); // recursive, but now with a locale configuration
393 }
394 }.execute();
395 return;
396 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 final Configuration configuration = getResources().getConfiguration();
399
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700400 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800401 final String locale = configuration.locale.toString();
402
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700403 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800404 final int mcc = configuration.mcc;
405
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700406 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 final int mnc = configuration.mnc;
408
Romain Guy84f296c2009-11-04 15:00:44 -0800409 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410
Romain Guy84f296c2009-11-04 15:00:44 -0800411 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700412 sLocaleConfiguration.locale = locale;
413 sLocaleConfiguration.mcc = mcc;
414 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700415
Joe Onorato0589f0f2010-02-08 13:44:00 -0800416 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400417 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700418
419 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
420 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700421 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700422 public void run() {
423 writeConfiguration(Launcher.this, localeConfiguration);
424 }
425 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700426 }
427 }
428
429 private static class LocaleConfiguration {
430 public String locale;
431 public int mcc = -1;
432 public int mnc = -1;
433 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700434
Romain Guy98d01652009-06-30 16:21:04 -0700435 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
436 DataInputStream in = null;
437 try {
438 in = new DataInputStream(context.openFileInput(PREFERENCES));
439 configuration.locale = in.readUTF();
440 configuration.mcc = in.readInt();
441 configuration.mnc = in.readInt();
442 } catch (FileNotFoundException e) {
443 // Ignore
444 } catch (IOException e) {
445 // Ignore
446 } finally {
447 if (in != null) {
448 try {
449 in.close();
450 } catch (IOException e) {
451 // Ignore
452 }
453 }
454 }
455 }
456
457 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
458 DataOutputStream out = null;
459 try {
460 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
461 out.writeUTF(configuration.locale);
462 out.writeInt(configuration.mcc);
463 out.writeInt(configuration.mnc);
464 out.flush();
465 } catch (FileNotFoundException e) {
466 // Ignore
467 } catch (IOException e) {
468 //noinspection ResultOfMethodCallIgnored
469 context.getFileStreamPath(PREFERENCES).delete();
470 } finally {
471 if (out != null) {
472 try {
473 out.close();
474 } catch (IOException e) {
475 // Ignore
476 }
477 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 }
479 }
480
481 static int getScreen() {
482 synchronized (sLock) {
483 return sScreen;
484 }
485 }
486
487 static void setScreen(int screen) {
488 synchronized (sLock) {
489 sScreen = screen;
490 }
491 }
492
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400493 // Note: This doesn't do all the client-id magic that BrowserProvider does
494 // in Browser. (http://b/2425179)
495 private Uri getDefaultBrowserUri() {
496 String url = getString(R.string.default_browser_url);
497 if (url.indexOf("{CID}") != -1) {
498 url = url.replace("{CID}", "android-google");
499 }
500 return Uri.parse(url);
501 }
502
503 // Load the Intent templates from arrays.xml to populate the hotseats. For
504 // each Intent, if it resolves to a single app, use that as the launch
505 // intent & use that app's label as the contentDescription. Otherwise,
506 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400507 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400508 if (mHotseatConfig == null) {
509 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
510 if (mHotseatConfig.length > 0) {
511 mHotseats = new Intent[mHotseatConfig.length];
512 mHotseatLabels = new CharSequence[mHotseatConfig.length];
513 mHotseatIcons = new Drawable[mHotseatConfig.length];
514 } else {
515 mHotseats = null;
516 mHotseatIcons = null;
517 mHotseatLabels = null;
518 }
519
520 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
521 for (int i=0; i<mHotseatConfig.length; i++) {
522 // load icon for this slot; currently unrelated to the actual activity
523 try {
524 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
525 } catch (ArrayIndexOutOfBoundsException ex) {
526 Log.w(TAG, "Missing hotseat_icons array item #" + i);
527 mHotseatIcons[i] = null;
528 }
529 }
530 hotseatIconDrawables.recycle();
531 }
532
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400533 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400534 for (int i=0; i<mHotseatConfig.length; i++) {
535 Intent intent = null;
536 if (mHotseatConfig[i].equals("*BROWSER*")) {
537 // magic value meaning "launch user's default web browser"
538 // replace it with a generic web request so we can see if there is indeed a default
539 String defaultUri = getString(R.string.default_browser_url);
540 intent = new Intent(
541 Intent.ACTION_VIEW,
542 ((defaultUri != null)
543 ? Uri.parse(defaultUri)
544 : getDefaultBrowserUri())
545 ).addCategory(Intent.CATEGORY_BROWSABLE);
546 // note: if the user launches this without a default set, she
547 // will always be taken to the default URL above; this is
548 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700549 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400550 // URL is unavoidably sent to the chosen app.
551 } else {
552 try {
553 intent = Intent.parseUri(mHotseatConfig[i], 0);
554 } catch (java.net.URISyntaxException ex) {
555 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
556 // bogus; leave intent=null
557 }
558 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700559
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400560 if (intent == null) {
561 mHotseats[i] = null;
562 mHotseatLabels[i] = getText(R.string.activity_not_found);
563 continue;
564 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400565
566 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700567 Log.d(TAG, "loadHotseats: hotseat " + i
568 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400569 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400570 + "]");
571 }
572
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400573 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
574 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700575 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400576 Log.d(TAG, "Best match for intent: " + bestMatch);
577 Log.d(TAG, "All matches: ");
578 for (ResolveInfo ri : allMatches) {
579 Log.d(TAG, " --> " + ri);
580 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400581 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400582 // did this resolve to a single app, or the resolver?
583 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700584 // can't find any activity to handle this. let's leave the
585 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400586 // to launch.
587 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400588
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400589 // set accessibility text to "Not installed"
590 mHotseatLabels[i] = getText(R.string.activity_not_found);
591 } else {
592 boolean found = false;
593 for (ResolveInfo ri : allMatches) {
594 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
595 && bestMatch.activityInfo.applicationInfo.packageName
596 .equals(ri.activityInfo.applicationInfo.packageName)) {
597 found = true;
598 break;
599 }
600 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700601
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400602 if (!found) {
603 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
604 // the bestMatch is probably the ResolveActivity, meaning the
605 // user has not yet selected a default
606 // so: we'll keep the original intent for now
607 mHotseats[i] = intent;
608
609 // set the accessibility text to "Select shortcut"
610 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
611 } else {
612 // we have an app!
613 // now reconstruct the intent to launch it through the front
614 // door
615 ComponentName com = new ComponentName(
616 bestMatch.activityInfo.applicationInfo.packageName,
617 bestMatch.activityInfo.name);
618 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
619
620 // load the app label for accessibility
621 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
622 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400623 }
624
625 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700626 Log.d(TAG, "loadHotseats: hotseat " + i
627 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400628 + ((mHotseats[i] == null)
629 ? "null"
630 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400631 + "] label=[" + mHotseatLabels[i]
632 + "]"
633 );
634 }
635 }
636 }
637
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800638 private void completeAdd(PendingAddArguments args) {
639 switch (args.requestCode) {
640 case REQUEST_PICK_APPLICATION:
641 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
642 break;
643 case REQUEST_PICK_SHORTCUT:
644 processShortcut(args.intent);
645 break;
646 case REQUEST_CREATE_SHORTCUT:
647 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
648 break;
649 case REQUEST_PICK_LIVE_FOLDER:
650 addLiveFolder(args.intent);
651 break;
652 case REQUEST_CREATE_LIVE_FOLDER:
653 completeAddLiveFolder(args.intent, args.screen, args.cellX, args.cellY);
654 break;
655 case REQUEST_PICK_APPWIDGET:
656 addAppWidgetFromPick(args.intent);
657 break;
658 case REQUEST_CREATE_APPWIDGET:
659 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
660 completeAddAppWidget(appWidgetId, args.screen);
661 break;
662 case REQUEST_PICK_WALLPAPER:
663 // We just wanted the activity result here so we can clear mWaitingForResult
664 break;
665 }
666 }
667
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800669 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Romain Guyaad5ef42009-06-10 02:48:37 -0700670 mWaitingForResult = false;
671
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 // The pattern used here is that a user PICKs a specific application,
673 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700674
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
676 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700677
Michael Jurka0280c3b2010-09-17 15:00:07 -0700678 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800679 final PendingAddArguments args = new PendingAddArguments();
680 args.requestCode = requestCode;
681 args.intent = data;
682 args.screen = mAddScreen;
683 args.cellX = mAddIntersectCellX;
684 args.cellY = mAddIntersectCellY;
685
686 // If the loader is still running, defer the add until it is done.
687 if (isWorkspaceLocked()) {
688 sPendingAddList.add(args);
689 } else {
690 completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 }
Romain Guy18042c82009-11-06 11:44:55 -0800692 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
693 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED &&
694 data != null) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700695 // Clean up the appWidgetId if we canceled
696 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
697 if (appWidgetId != -1) {
698 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 }
700 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 }
702
703 @Override
704 protected void onResume() {
705 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800706 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700707 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400708 mWorkspaceLoading = true;
709 mModel.startLoader(this, true);
710 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700711 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800713 if (mWaitingForResume != null) {
714 mWaitingForResume.setStayPressed(false);
715 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700716 // When we resume Launcher, a different Activity might be responsible for the app
717 // market intent, so refresh the icon
718 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 }
720
721 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700722 protected void onPause() {
723 super.onPause();
Michael Jurkaaf442092010-06-10 17:01:57 -0700724 // Some launcher layouts don't have a previous and next view
725 if (mPreviousView != null) {
726 dismissPreview(mPreviousView);
Patrick Dubroyab962b72010-07-26 12:10:10 -0700727 }
728 if (mNextView != null) {
Michael Jurkaaf442092010-06-10 17:01:57 -0700729 dismissPreview(mNextView);
730 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700731 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800732 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700733 }
Romain Guycbb89e42009-06-08 15:52:54 -0700734
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700735 @Override
736 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 // Flag the loader to stop early before switching
738 mModel.stopLoader();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800739 mAllAppsGrid.surrender();
Romain Guy13c2e7b2010-03-10 19:45:00 -0800740 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700741 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700742
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800743 // We can't hide the IME if it was forced open. So don't bother
744 /*
745 @Override
746 public void onWindowFocusChanged(boolean hasFocus) {
747 super.onWindowFocusChanged(hasFocus);
748
749 if (hasFocus) {
750 final InputMethodManager inputManager = (InputMethodManager)
751 getSystemService(Context.INPUT_METHOD_SERVICE);
752 WindowManager.LayoutParams lp = getWindow().getAttributes();
753 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
754 android.os.Handler()) {
755 protected void onReceiveResult(int resultCode, Bundle resultData) {
756 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
757 }
758 });
759 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
760 }
761 }
762 */
763
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 private boolean acceptFilter() {
765 final InputMethodManager inputManager = (InputMethodManager)
766 getSystemService(Context.INPUT_METHOD_SERVICE);
767 return !inputManager.isFullscreenMode();
768 }
769
770 @Override
771 public boolean onKeyDown(int keyCode, KeyEvent event) {
772 boolean handled = super.onKeyDown(keyCode, event);
773 if (!handled && acceptFilter() && keyCode != KeyEvent.KEYCODE_ENTER) {
774 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
775 keyCode, event);
776 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700777 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700778 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700779 // showSearchDialog()
780 // If there are multiple keystrokes before the search dialog takes focus,
781 // onSearchRequested() will be called for every keystroke,
782 // but it is idempotent, so it's fine.
783 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 }
785 }
786
Joe Onorato8a9625e2010-01-28 15:55:35 -0800787 // Eat the long press event so the keyboard doesn't come up.
788 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
789 return true;
790 }
791
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800792 return handled;
793 }
794
Karl Rosaen138a0412009-04-23 19:00:21 -0700795 private String getTypedText() {
796 return mDefaultKeySsb.toString();
797 }
798
799 private void clearTypedText() {
800 mDefaultKeySsb.clear();
801 mDefaultKeySsb.clearSpans();
802 Selection.setSelection(mDefaultKeySsb, 0);
803 }
804
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700806 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
807 * State
808 */
809 private static State intToState(int stateOrdinal) {
810 State state = State.WORKSPACE;
811 final State[] stateValues = State.values();
812 for (int i = 0; i < stateValues.length; i++) {
813 if (stateValues[i].ordinal() == stateOrdinal) {
814 state = stateValues[i];
815 break;
816 }
817 }
818 return state;
819 }
820
821 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800822 * Restores the previous state, if it exists.
823 *
824 * @param savedState The previous state.
825 */
826 private void restoreState(Bundle savedState) {
827 if (savedState == null) {
828 return;
829 }
830
Michael Jurka883f55b2010-10-21 15:47:14 -0700831 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
832
833 if (state == State.ALL_APPS) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800834 showAllApps(false);
Michael Jurka883f55b2010-10-21 15:47:14 -0700835 } else if (state == State.CUSTOMIZE) {
836 showCustomizationDrawer(false);
Joe Onorato3a8820b2009-11-10 15:06:42 -0800837 }
838
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
840 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700841 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 }
843
844 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700845
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700847 mAddScreen = addScreen;
848 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
849 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 mRestoring = true;
851 }
852
853 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
854 if (renameFolder) {
855 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700856 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 mRestoring = true;
858 }
Winson Chunga12a2502010-12-20 14:41:35 -0800859
860 // Restore the current AllApps drawer tab
861 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
862 String curTab = savedState.getString("allapps_currentTab");
863 if (curTab != null) {
864 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
865 tabhost.setCurrentTabByTag(curTab);
866 }
867 int curPage = savedState.getInt("allapps_currentPage", -1);
868 if (curPage > -1) {
869 mAllAppsPagedView.setRestorePage(curPage);
870 }
871 }
872
873 // Restore the current customization drawer tab
874 if (mHomeCustomizationDrawer != null) {
875 String curTab = savedState.getString("customize_currentTab");
876 if (curTab != null) {
877 // We set this directly so that there is no delay before the tab is set
Michael Jurka7ef959b2011-02-23 11:48:32 -0800878 mCustomizePagedView.setCustomizationFilter(
879 mHomeCustomizationDrawer.getCustomizeFilterForTabTag(curTab));
Winson Chunga12a2502010-12-20 14:41:35 -0800880 mHomeCustomizationDrawer.setCurrentTabByTag(curTab);
881 }
882
883 // Note: currently we do not restore the page for the customization tray because unlike
884 // AllApps, the page content can change drastically
885 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800886 }
887
888 /**
889 * Finds all the views we need and configure them properly.
890 */
891 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700892 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400893
Romain Guyb1b69f52009-08-10 15:10:15 -0700894 DragLayer dragLayer = (DragLayer) findViewById(R.id.drag_layer);
Joe Onorato00acb122009-08-04 16:04:30 -0400895 dragLayer.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896
Joe Onorato7c312c12009-08-13 21:36:53 -0700897 mAllAppsGrid = (AllAppsView)dragLayer.findViewById(R.id.all_apps_view);
Joe Onorato6665c0f2009-09-02 15:27:24 -0700898 mAllAppsGrid.setLauncher(this);
Joe Onorato5162ea92009-09-03 09:39:42 -0700899 mAllAppsGrid.setDragController(dragController);
Romain Guyc16fea72010-03-12 17:17:56 -0800900 ((View) mAllAppsGrid).setWillNotDraw(false); // We don't want a hole punched in our window.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700901 // Manage focusability manually since this thing is always visible (in non-xlarge)
Winson Chungaafa03c2010-06-11 17:34:16 -0700902 ((View) mAllAppsGrid).setFocusable(false);
Joe Onorato7c312c12009-08-13 21:36:53 -0700903
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700904 if (LauncherApplication.isScreenXLarge()) {
905 // They need to be INVISIBLE initially so that they will be measured in the layout.
906 // Otherwise the animations are messed up when we show them for the first time.
907 ((View) mAllAppsGrid).setVisibility(View.INVISIBLE);
908 mHomeCustomizationDrawer.setVisibility(View.INVISIBLE);
909 }
910
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 mWorkspace = (Workspace) dragLayer.findViewById(R.id.workspace);
Adam Lesinski6b879f02010-11-04 16:15:23 -0700912
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 final Workspace workspace = mWorkspace;
Joe Onoratof0dde092010-02-16 18:25:23 -0500914 workspace.setHapticFeedbackEnabled(false);
Michael Jurka946ad472010-07-09 18:05:18 -0700915
Joe Onorato7c312c12009-08-13 21:36:53 -0700916 DeleteZone deleteZone = (DeleteZone) dragLayer.findViewById(R.id.delete_zone);
917 mDeleteZone = deleteZone;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700919 View handleView = findViewById(R.id.all_apps_button);
920 if (handleView != null && handleView instanceof HandleView) {
921 // we don't use handle view in xlarge mode
Michael Jurka7ef4f752010-08-03 16:04:26 -0700922 mHandleView = (HandleView)handleView;
Michael Jurka2c3af5f2010-08-03 13:53:20 -0700923 mHandleView.setLauncher(this);
924 mHandleView.setOnClickListener(this);
925 mHandleView.setOnLongClickListener(this);
926 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800927
Winson Chung80baf5a2010-08-09 16:03:15 -0700928 if (mCustomizePagedView != null) {
929 mCustomizePagedView.setLauncher(this);
930 mCustomizePagedView.setDragController(dragController);
Michael Jurkaaf442092010-06-10 17:01:57 -0700931 } else {
932 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
933 hotseatLeft.setContentDescription(mHotseatLabels[0]);
934 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
935 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
936 hotseatRight.setContentDescription(mHotseatLabels[1]);
937 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400938
Michael Jurkaaf442092010-06-10 17:01:57 -0700939 mPreviousView = (ImageView) dragLayer.findViewById(R.id.previous_screen);
940 mNextView = (ImageView) dragLayer.findViewById(R.id.next_screen);
Romain Guy1fbc1c82009-11-09 20:43:08 -0800941
Michael Jurkaaf442092010-06-10 17:01:57 -0700942 Drawable previous = mPreviousView.getDrawable();
943 Drawable next = mNextView.getDrawable();
944 mWorkspace.setIndicators(previous, next);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945
Michael Jurkaaf442092010-06-10 17:01:57 -0700946 mPreviousView.setHapticFeedbackEnabled(false);
947 mPreviousView.setOnLongClickListener(this);
948 mNextView.setHapticFeedbackEnabled(false);
949 mNextView.setOnLongClickListener(this);
950 }
Romain Guy1fbc1c82009-11-09 20:43:08 -0800951
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 workspace.setOnLongClickListener(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400953 workspace.setDragController(dragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 workspace.setLauncher(this);
Michael Jurkac5b262c2011-01-12 20:24:50 -0800955 workspace.setWallpaperDimension();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956
957 deleteZone.setLauncher(this);
Joe Onorato00acb122009-08-04 16:04:30 -0400958 deleteZone.setDragController(dragController);
Michael Jurkab8e14472010-12-20 16:06:10 -0800959
960 final View allAppsButton = findViewById(R.id.all_apps_button);
961 final View divider = findViewById(R.id.divider);
962 final View configureButton = findViewById(R.id.configure_button);
963
Michael Jurka54f7ac32010-08-02 13:56:46 -0700964 if (LauncherApplication.isScreenXLarge()) {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800965 deleteZone.setOverlappingViews(new View[] { allAppsButton, divider, configureButton });
Michael Jurka54f7ac32010-08-02 13:56:46 -0700966 } else {
Winson Chung26cbf3a2011-01-06 16:25:55 -0800967 deleteZone.setOverlappingView(findViewById(R.id.all_apps_button_cluster));
Michael Jurka54f7ac32010-08-02 13:56:46 -0700968 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -0700969 dragController.addDragListener(deleteZone);
970
Adam Cohencdc30d52010-12-01 15:09:47 -0800971 DeleteZone allAppsDeleteZone = (DeleteZone) findViewById(R.id.all_apps_delete_zone);
972 if (allAppsDeleteZone != null) {
973 allAppsDeleteZone.setLauncher(this);
974 allAppsDeleteZone.setDragController(dragController);
975 allAppsDeleteZone.setDragAndDropEnabled(false);
976 dragController.addDragListener(allAppsDeleteZone);
977 dragController.addDropTarget(allAppsDeleteZone);
978 }
979
980 ApplicationInfoDropTarget allAppsInfoTarget = (ApplicationInfoDropTarget)
981 findViewById(R.id.all_apps_info_target);
982 if (allAppsInfoTarget != null) {
983 allAppsInfoTarget.setLauncher(this);
984 dragController.addDragListener(allAppsInfoTarget);
Adam Cohencdc30d52010-12-01 15:09:47 -0800985 allAppsInfoTarget.setDragAndDropEnabled(false);
986 View marketButton = findViewById(R.id.market_button);
987 if (marketButton != null) {
Michael Jurkab8e14472010-12-20 16:06:10 -0800988 allAppsInfoTarget.setOverlappingView(marketButton);
Winson Chungfbb3d9b2011-03-01 12:43:02 -0800989 marketButton.setOnClickListener(new OnClickListener() {
990 public void onClick(View v) {
991 onClickAppMarketButton(v);
992 }
993 });
Adam Cohencdc30d52010-12-01 15:09:47 -0800994 }
995 }
996
Joe Onorato00acb122009-08-04 16:04:30 -0400997 dragController.setDragScoller(workspace);
Joe Onorato00acb122009-08-04 16:04:30 -0400998 dragController.setScrollView(dragLayer);
Romain Guyea3763c2010-01-11 18:02:04 -0800999 dragController.setMoveTarget(workspace);
Jason Samsfd22dac2009-09-20 17:24:16 -07001000
Joe Onorato00acb122009-08-04 16:04:30 -04001001 // The order here is bottom to top.
1002 dragController.addDropTarget(workspace);
1003 dragController.addDropTarget(deleteZone);
Adam Cohencdc30d52010-12-01 15:09:47 -08001004 if (allAppsInfoTarget != null) {
1005 dragController.addDropTarget(allAppsInfoTarget);
1006 }
1007 if (allAppsDeleteZone != null) {
1008 dragController.addDropTarget(allAppsDeleteZone);
1009 }
Michael Jurka838a4ca2011-02-07 13:33:06 -08001010 mButtonCluster = findViewById(R.id.all_apps_button_cluster);
1011
1012 mAllAppsButton = findViewById(R.id.all_apps_button);
1013 mDivider = findViewById(R.id.divider);
1014 mConfigureButton = findViewById(R.id.configure_button);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001015
1016 // We had previously set these click handlers in XML, but the first time we launched
1017 // Configure or All Apps we had an extra 50ms of delay while the java reflection methods
1018 // found the right handler. Setting the handlers directly here eliminates that cost.
1019 if (mConfigureButton != null) {
1020 mConfigureButton.setOnClickListener(new OnClickListener() {
1021 public void onClick(View v) {
1022 onClickConfigureButton(v);
1023 }
1024 });
1025 }
1026 if (mDivider != null) {
1027 mDivider.setOnClickListener(new OnClickListener() {
1028 public void onClick(View v) {
1029 onClickAllAppsButton(v);
1030 }
1031 });
1032 }
1033 if (mAllAppsButton != null) {
1034 mAllAppsButton.setOnClickListener(new OnClickListener() {
1035 public void onClick(View v) {
1036 onClickAllAppsButton(v);
1037 }
1038 });
1039 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040 }
1041
Romain Guy8a73c512009-11-09 19:19:59 -08001042 @SuppressWarnings({"UnusedDeclaration"})
1043 public void previousScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001044 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001045 mWorkspace.scrollLeft();
1046 }
Romain Guy8a73c512009-11-09 19:19:59 -08001047 }
1048
1049 @SuppressWarnings({"UnusedDeclaration"})
1050 public void nextScreen(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001051 if (mState != State.ALL_APPS) {
Joe Onorato61597bd2009-11-19 12:51:57 -08001052 mWorkspace.scrollRight();
1053 }
Romain Guy8a73c512009-11-09 19:19:59 -08001054 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001055
1056 @SuppressWarnings({"UnusedDeclaration"})
1057 public void launchHotSeat(View v) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001058 if (mState == State.ALL_APPS) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -04001059
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001060 int index = -1;
1061 if (v.getId() == R.id.hotseat_left) {
1062 index = 0;
1063 } else if (v.getId() == R.id.hotseat_right) {
1064 index = 1;
1065 }
1066
Daniel Sandlerab1ebd72010-04-27 16:57:25 -04001067 // reload these every tap; you never know when they might change
1068 loadHotseats();
1069 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001070 startActivitySafely(
1071 mHotseats[index],
1072 "hotseat"
1073 );
1074 }
1075 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001076
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077 /**
1078 * Creates a view representing a shortcut.
1079 *
1080 * @param info The data structure describing the shortcut.
1081 *
1082 * @return A View inflated from R.layout.application.
1083 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001084 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001086 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 }
1088
1089 /**
1090 * Creates a view representing a shortcut inflated from the specified resource.
1091 *
1092 * @param layoutResId The id of the XML layout used to create the shortcut.
1093 * @param parent The group the shortcut belongs to.
1094 * @param info The data structure describing the shortcut.
1095 *
1096 * @return A View inflated from layoutResId.
1097 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001098 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001099 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1100 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001101 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 return favorite;
1103 }
1104
1105 /**
1106 * Add an application shortcut to the workspace.
1107 *
1108 * @param data The intent describing the application.
1109 * @param cellInfo The position on screen where to create the shortcut.
1110 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001111 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001112 int intersectCellX, int intersectCellY) {
1113 final int[] cellXY = mTmpAddItemCellCoordinates;
1114 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1115
1116 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1117 showOutOfSpaceMessage();
1118 return;
1119 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001120
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001121 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001122
Romain Guy73b979d2009-06-09 12:57:21 -07001123 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001124 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001126 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001127 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
1128 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001129 } else {
1130 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001131 }
1132 }
Romain Guycbb89e42009-06-08 15:52:54 -07001133
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001134 /**
1135 * Add a shortcut to the workspace.
1136 *
1137 * @param data The intent describing the shortcut.
1138 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001139 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001140 private void completeAddShortcut(Intent data, int screen,
1141 int intersectCellX, int intersectCellY) {
1142 final int[] cellXY = mTmpAddItemCellCoordinates;
1143 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001144
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001145 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001146 boolean foundCellSpan = false;
1147 if (touchXY != null) {
1148 // when dragging and dropping, just find the closest free spot
1149 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1150 int[] result = screenLayout.findNearestVacantArea(
1151 touchXY[0], touchXY[1], 1, 1, cellXY);
1152 foundCellSpan = (result != null);
1153 } else {
1154 foundCellSpan = layout.findCellForSpanThatIntersects(
1155 cellXY, 1, 1, intersectCellX, intersectCellY);
1156 }
1157
1158 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001159 showOutOfSpaceMessage();
1160 return;
1161 }
1162
1163 final ShortcutInfo info = mModel.addShortcut(
1164 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165
1166 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001168 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001169 }
1170 }
1171
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001172 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001173 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001175 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001176 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 */
Winson Chungf7640c82011-02-28 13:47:29 -08001178 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001179 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001180
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001181 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001182 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1183 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001185 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001186 // We have saved the position to which the widget was dragged-- this really only matters
1187 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001188 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001189
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001190 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001191 boolean foundCellSpan = false;
1192 if (touchXY != null) {
1193 // when dragging and dropping, just find the closest free spot
1194 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1195 int[] result = screenLayout.findNearestVacantArea(
1196 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001197 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001198 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001199 // if we long pressed on an empty cell to bring up a menu,
1200 // make sure we intersect the empty cell
1201 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1202 // findCellForSpanThatIntersects will just ignore them
1203 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1204 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001205 }
1206
Michael Jurka0280c3b2010-09-17 15:00:07 -07001207 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001208 if (appWidgetId != -1) {
1209 // Deleting an app widget ID is a void call but writes to disk before returning
1210 // to the caller...
1211 final LauncherAppWidgetHost appWidgetHost = mAppWidgetHost;
1212 new Thread("deleteAppWidgetId") {
1213 public void run() {
1214 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1215 }
1216 }.start();
1217 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001218 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001219 return;
1220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001222 // Build Launcher-specific widget info and save to database
1223 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001224 launcherInfo.spanX = spanXY[0];
1225 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001226
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 LauncherModel.addItemToDatabase(this, launcherInfo,
1228 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001229 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230
1231 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001232 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001233
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001235 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001236
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001237 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001239
Michael Jurka0280c3b2010-09-17 15:00:07 -07001240 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001241 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001242
1243 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 }
1245 }
Romain Guycbb89e42009-06-08 15:52:54 -07001246
Adam Cohended9f8d2010-11-03 13:25:16 -07001247 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1248 @Override
1249 public void onReceive(Context context, Intent intent) {
1250 final String action = intent.getAction();
1251 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1252 mUserPresent = false;
1253 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001254
1255 // Reset AllApps to it's initial state
1256 if (mAllAppsGrid != null) {
1257 mAllAppsGrid.reset();
1258 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001259 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1260 mUserPresent = true;
1261 updateRunning();
1262 }
1263 }
1264 };
1265
1266 @Override
1267 public void onAttachedToWindow() {
1268 super.onAttachedToWindow();
1269
1270 // Listen for broadcasts related to user-presence
1271 final IntentFilter filter = new IntentFilter();
1272 filter.addAction(Intent.ACTION_SCREEN_OFF);
1273 filter.addAction(Intent.ACTION_USER_PRESENT);
1274 registerReceiver(mReceiver, filter);
1275
Adam Cohend113e0c2010-11-11 10:48:05 -08001276 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001277 mVisible = true;
1278 }
1279
1280 @Override
1281 public void onDetachedFromWindow() {
1282 super.onDetachedFromWindow();
1283 mVisible = false;
1284
Adam Cohend113e0c2010-11-11 10:48:05 -08001285 if (mAttached) {
1286 unregisterReceiver(mReceiver);
1287 mAttached = false;
1288 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001289 updateRunning();
1290 }
1291
1292 public void onWindowVisibilityChanged(int visibility) {
1293 mVisible = visibility == View.VISIBLE;
1294 updateRunning();
1295 }
1296
1297 private void sendAdvanceMessage(long delay) {
1298 mHandler.removeMessages(ADVANCE_MSG);
1299 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1300 mHandler.sendMessageDelayed(msg, delay);
1301 mAutoAdvanceSentTime = System.currentTimeMillis();
1302 }
1303
1304 private void updateRunning() {
1305 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1306 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1307 mAutoAdvanceRunning = autoAdvanceRunning;
1308 if (autoAdvanceRunning) {
1309 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1310 sendAdvanceMessage(delay);
1311 } else {
1312 if (!mWidgetsToAdvance.isEmpty()) {
1313 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1314 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1315 }
1316 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001317 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001318 }
1319 }
1320 }
1321
1322 private final Handler mHandler = new Handler() {
1323 @Override
1324 public void handleMessage(Message msg) {
1325 if (msg.what == ADVANCE_MSG) {
1326 int i = 0;
1327 for (View key: mWidgetsToAdvance.keySet()) {
1328 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1329 final int delay = mAdvanceStagger * i;
1330 if (v instanceof Advanceable) {
1331 postDelayed(new Runnable() {
1332 public void run() {
1333 ((Advanceable) v).advance();
1334 }
1335 }, delay);
1336 }
1337 i++;
1338 }
1339 sendAdvanceMessage(mAdvanceInterval);
1340 }
1341 }
1342 };
1343
1344 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1345 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1346 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1347 if (v instanceof Advanceable) {
1348 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001349 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001350 updateRunning();
1351 }
1352 }
1353
1354 void removeWidgetToAutoAdvance(View hostView) {
1355 if (mWidgetsToAdvance.containsKey(hostView)) {
1356 mWidgetsToAdvance.remove(hostView);
1357 updateRunning();
1358 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001359 }
1360
Joe Onorato9c1289c2009-08-17 11:03:03 -04001361 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1362 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001363 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001364 launcherInfo.hostView = null;
1365 }
1366
Adam Cohended9f8d2010-11-03 13:25:16 -07001367 void showOutOfSpaceMessage() {
1368 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1369 }
1370
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001371 public LauncherAppWidgetHost getAppWidgetHost() {
1372 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 }
Romain Guycbb89e42009-06-08 15:52:54 -07001374
Winson Chunga9abd0e2010-10-27 17:18:37 -07001375 public LauncherModel getModel() {
1376 return mModel;
1377 }
1378
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001379 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001380 getWindow().closeAllPanels();
1381
1382 try {
1383 dismissDialog(DIALOG_CREATE_SHORTCUT);
1384 // Unlock the workspace if the dialog was showing
1385 } catch (Exception e) {
1386 // An exception is thrown if the dialog is not visible, which is fine
1387 }
1388
1389 try {
1390 dismissDialog(DIALOG_RENAME_FOLDER);
1391 // Unlock the workspace if the dialog was showing
1392 } catch (Exception e) {
1393 // An exception is thrown if the dialog is not visible, which is fine
1394 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001395
1396 // Whatever we were doing is hereby canceled.
1397 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001398 }
1399
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 @Override
1401 protected void onNewIntent(Intent intent) {
1402 super.onNewIntent(intent);
1403
1404 // Close the menu
1405 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001406 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001407 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001408
Joe Onorato14f122b2009-11-19 14:06:36 -08001409 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1410 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001411
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001412 // In all these cases, only animate if we're already on home
1413
Michael Jurka4cb37242010-08-09 21:05:32 -07001414 if (LauncherApplication.isScreenXLarge()) {
Michael Jurka99229f62010-09-09 11:49:32 -07001415 mWorkspace.unshrink(alreadyOnHome);
1416 }
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001417
Patrick Dubroy758a9232011-03-03 19:54:56 -08001418 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001419 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001420 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001421 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001422 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001423
Joe Onorato3a8820b2009-11-10 15:06:42 -08001424 final View v = getWindow().peekDecorView();
1425 if (v != null && v.getWindowToken() != null) {
1426 InputMethodManager imm = (InputMethodManager)getSystemService(
1427 INPUT_METHOD_SERVICE);
1428 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001430
1431 // Reset AllApps to it's initial state
1432 if (mAllAppsGrid != null) {
1433 mAllAppsGrid.reset();
1434 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 }
1436 }
1437
1438 @Override
1439 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1440 // Do not call super here
1441 mSavedInstanceState = savedInstanceState;
1442 }
1443
1444 @Override
1445 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001446 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447
1448 final ArrayList<Folder> folders = mWorkspace.getOpenFolders();
1449 if (folders.size() > 0) {
1450 final int count = folders.size();
1451 long[] ids = new long[count];
1452 for (int i = 0; i < count; i++) {
1453 final FolderInfo info = folders.get(i).getInfo();
1454 ids[i] = info.id;
1455 }
1456 outState.putLongArray(RUNTIME_STATE_USER_FOLDERS, ids);
1457 } else {
1458 super.onSaveInstanceState(outState);
1459 }
1460
Michael Jurka883f55b2010-10-21 15:47:14 -07001461 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462
Michael Jurka0280c3b2010-09-17 15:00:07 -07001463 if (mAddScreen > -1 && mWaitingForResult) {
1464 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1465 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1466 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
1468
1469 if (mFolderInfo != null && mWaitingForResult) {
1470 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1471 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1472 }
Michael Jurka946ad472010-07-09 18:05:18 -07001473
Winson Chung43b119d2010-12-06 16:45:05 -08001474 // Save the current AllApps drawer tab
1475 if (mAllAppsGrid != null && mAllAppsGrid instanceof AllAppsTabbed) {
1476 AllAppsTabbed tabhost = (AllAppsTabbed) mAllAppsGrid;
1477 String currentTabTag = tabhost.getCurrentTabTag();
1478 if (currentTabTag != null) {
1479 outState.putString("allapps_currentTab", currentTabTag);
Winson Chunga12a2502010-12-20 14:41:35 -08001480 outState.putInt("allapps_currentPage", mAllAppsPagedView.getCurrentPage());
Winson Chung43b119d2010-12-06 16:45:05 -08001481 }
1482 }
1483
1484 // Save the current customization drawer tab
Michael Jurka946ad472010-07-09 18:05:18 -07001485 if (mHomeCustomizationDrawer != null) {
1486 String currentTabTag = mHomeCustomizationDrawer.getCurrentTabTag();
1487 if (currentTabTag != null) {
Winson Chung43b119d2010-12-06 16:45:05 -08001488 outState.putString("customize_currentTab", currentTabTag);
Michael Jurka946ad472010-07-09 18:05:18 -07001489 }
1490 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 }
1492
1493 @Override
1494 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001496
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001498 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001500 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001502 mAppWidgetHost = null;
1503
1504 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505
1506 TextKeyListener.getInstance().release();
1507
Joe Onorato9c1289c2009-08-17 11:03:03 -04001508 mModel.stopLoader();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509
Joe Onorato9c1289c2009-08-17 11:03:03 -04001510 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001511
1512 getContentResolver().unregisterContentObserver(mWidgetObserver);
Winson Chungaafa03c2010-06-11 17:34:16 -07001513
Patrick Dubroyab962b72010-07-26 12:10:10 -07001514 // Some launcher layouts don't have a previous and next view
1515 if (mPreviousView != null) {
1516 dismissPreview(mPreviousView);
1517 }
1518 if (mNextView != null) {
1519 dismissPreview(mNextView);
1520 }
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001521
1522 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001523
1524 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1525 mWorkspace.removeAllViews();
1526 mWorkspace = null;
1527 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001528
1529 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 }
1531
1532 @Override
1533 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001534 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 super.startActivityForResult(intent, requestCode);
1536 }
1537
1538 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001539 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001541
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001542 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001543
Karl Rosaen138a0412009-04-23 19:00:21 -07001544 if (initialQuery == null) {
1545 // Use any text typed in the launcher as the initial query
1546 initialQuery = getTypedText();
1547 clearTypedText();
1548 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 if (appSearchData == null) {
1550 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001551 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 }
Romain Guycbb89e42009-06-08 15:52:54 -07001553
Karl Rosaen138a0412009-04-23 19:00:21 -07001554 final SearchManager searchManager =
1555 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001556 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001557 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 }
1559
1560 @Override
1561 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001562 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001563 return false;
1564 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565
1566 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001567
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1569 .setIcon(android.R.drawable.ic_menu_add)
1570 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001571 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1572 .setIcon(android.R.drawable.ic_menu_manage)
1573 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001574 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 .setIcon(android.R.drawable.ic_menu_gallery)
1576 .setAlphabeticShortcut('W');
1577 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1578 .setIcon(android.R.drawable.ic_search_category_default)
1579 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1580 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1581 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1582 .setAlphabeticShortcut('N');
1583
1584 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001585 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1586 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587
1588 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1589 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1590 .setIntent(settings);
1591
1592 return true;
1593 }
1594
1595 @Override
1596 public boolean onPrepareOptionsMenu(Menu menu) {
1597 super.onPrepareOptionsMenu(menu);
1598
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001599 // If all apps is animating, don't show the menu, because we don't know
1600 // which one to show.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001601 if (mAllAppsGrid.isAnimating()) {
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001602 return false;
1603 }
1604
1605 // Only show the add and wallpaper options when we're not in all apps.
Patrick Dubroyff5f0402010-07-26 15:23:26 -07001606 boolean visible = !mAllAppsGrid.isVisible();
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001607 menu.setGroupVisible(MENU_GROUP_ADD, visible);
1608 menu.setGroupVisible(MENU_GROUP_WALLPAPER, visible);
1609
1610 // Disable add if the workspace is full.
1611 if (visible) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001612 CellLayout layout = (CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage());
1613 menu.setGroupEnabled(MENU_GROUP_ADD, layout.existsEmptyCell());
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001614 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615
1616 return true;
1617 }
1618
1619 @Override
1620 public boolean onOptionsItemSelected(MenuItem item) {
1621 switch (item.getItemId()) {
1622 case MENU_ADD:
1623 addItems();
1624 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001625 case MENU_MANAGE_APPS:
1626 manageApps();
1627 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 case MENU_WALLPAPER_SETTINGS:
1629 startWallpaper();
1630 return true;
1631 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001632 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 return true;
1634 case MENU_NOTIFICATIONS:
1635 showNotifications();
1636 return true;
1637 }
1638
1639 return super.onOptionsItemSelected(item);
1640 }
Romain Guycbb89e42009-06-08 15:52:54 -07001641
Karl Rosaen138a0412009-04-23 19:00:21 -07001642 /**
1643 * Indicates that we want global search for this activity by setting the globalSearch
1644 * argument for {@link #startSearch} to true.
1645 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001647 @Override
1648 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001649 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001650 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001651 }
1652
Joe Onorato9c1289c2009-08-17 11:03:03 -04001653 public boolean isWorkspaceLocked() {
1654 return mWorkspaceLoading || mWaitingForResult;
1655 }
1656
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001657 // Is the workspace preview (brought up by long-pressing on a signpost icon) visible?
1658 private boolean isPreviewVisible() {
1659 return (mPreviousView != null && mPreviousView.getTag() != null) ||
1660 (mNextView != null && mNextView.getTag() != null);
1661 }
1662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 private void addItems() {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001664 if (LauncherApplication.isScreenXLarge()) {
1665 // Animate the widget chooser up from the bottom of the screen
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001666 if (mState != State.CUSTOMIZE) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07001667 showCustomizationDrawer(true);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001668 }
1669 } else {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001670 showWorkspace(true);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001671 showAddDialog(-1, -1);
Patrick Dubroy558654c2010-07-23 16:48:11 -07001672 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 }
1674
Michael Jurka0280c3b2010-09-17 15:00:07 -07001675 private void resetAddInfo() {
1676 mAddScreen = -1;
1677 mAddIntersectCellX = -1;
1678 mAddIntersectCellY = -1;
1679 mAddDropPosition = null;
1680 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001681
Winson Chung55cef262010-10-28 14:14:18 -07001682 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001683 resetAddInfo();
1684 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001685 mAddDropPosition = position;
1686
Michael Jurkaaf442092010-06-10 17:01:57 -07001687 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001688 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1689 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001690 }
1691
Winson Chung7ad01412010-09-27 11:33:03 -07001692 private void manageApps() {
1693 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1694 }
1695
Michael Jurkaaf442092010-06-10 17:01:57 -07001696 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001697 // TODO: catch bad widget exception when sent
1698 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001699 // TODO: Is this log message meaningful?
1700 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001701 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001702 }
1703
Winson Chung55cef262010-10-28 14:14:18 -07001704 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001705 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706
Bjorn Bringert7984c942009-12-09 15:38:25 +00001707 if (appWidget.configure != null) {
1708 // Launch over to configure widget, if needed
1709 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1710 intent.setComponent(appWidget.configure);
1711 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001712 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001713 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1714 intent.putExtra(
1715 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1716 info.mimeType);
1717
1718 final String mimeType = info.mimeType;
1719 final ClipData clipData = (ClipData) info.configurationData;
1720 final ClipDescription clipDesc = clipData.getDescription();
1721 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1722 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001723 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001724 final CharSequence stringData = item.getText();
1725 final Uri uriData = item.getUri();
1726 final Intent intentData = item.getIntent();
1727 final String key =
1728 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1729 if (uriData != null) {
1730 intent.putExtra(key, uriData);
1731 } else if (intentData != null) {
1732 intent.putExtra(key, intentData);
1733 } else if (stringData != null) {
1734 intent.putExtra(key, stringData);
1735 }
1736 break;
1737 }
1738 }
1739 }
Winson Chung55cef262010-10-28 14:14:18 -07001740 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001741
Romain Guy8e633c52010-03-04 12:51:36 -08001742 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001744 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001745 completeAddAppWidget(appWidgetId, mAddScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 }
1747 }
Romain Guycbb89e42009-06-08 15:52:54 -07001748
Michael Jurka0280c3b2010-09-17 15:00:07 -07001749 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1750 resetAddInfo();
1751 mAddScreen = screen;
1752 mAddDropPosition = position;
1753
1754 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1755 createShortcutIntent.setComponent(componentName);
1756 processShortcut(createShortcutIntent);
1757 }
1758
Joe Onoratodeb98af2010-02-19 14:59:39 -08001759 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001760 // Handle case where user selected "Applications"
1761 String applicationName = getResources().getString(R.string.group_applications);
1762 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001763
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001764 if (applicationName != null && applicationName.equals(shortcutName)) {
1765 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1766 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001767
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001768 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1769 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001770 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001771 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001772 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001773 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001774 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 }
1776
Winson Chung24ab2f12010-09-16 14:10:47 -07001777 void processWallpaper(Intent intent) {
1778 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1779 }
1780
Michael Jurka0280c3b2010-09-17 15:00:07 -07001781 void addLiveFolderFromDrop(ComponentName componentName, int screen, int[] position) {
1782 resetAddInfo();
1783 mAddScreen = screen;
1784 mAddDropPosition = position;
1785
1786 Intent createFolderIntent = new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER);
1787 createFolderIntent.setComponent(componentName);
1788
1789 addLiveFolder(createFolderIntent);
1790 }
1791
1792 void addLiveFolder(Intent intent) { // YYY add screen intersect etc. parameters here
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001793 // Handle case where user selected "Folder"
Jeffrey Sharkeyc4bbd0a2009-03-24 22:47:52 -07001794 String folderName = getResources().getString(R.string.group_folder);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001795 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001796
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001797 if (folderName != null && folderName.equals(shortcutName)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001798 addFolder(mAddScreen, mAddIntersectCellX, mAddIntersectCellY);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001799 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001800 startActivityForResultSafely(intent, REQUEST_CREATE_LIVE_FOLDER);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001801 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 }
1803
Adam Cohendf035382011-04-11 17:22:04 -07001804 FolderIcon addFolder(int screen, int intersectCellX, int intersectCellY) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 UserFolderInfo folderInfo = new UserFolderInfo();
1806 folderInfo.title = getText(R.string.folder_name);
1807
Michael Jurka0280c3b2010-09-17 15:00:07 -07001808 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1809 final int[] cellXY = mTmpAddItemCellCoordinates;
1810 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1811 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001812 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001813 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814
1815 // Update the model
Joe Onorato9c1289c2009-08-17 11:03:03 -04001816 LauncherModel.addItemToDatabase(this, folderInfo,
1817 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001818 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001819 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820
1821 // Create the view
1822 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001823 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1824 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001825 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001826 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 }
Romain Guycbb89e42009-06-08 15:52:54 -07001828
Joe Onorato9c1289c2009-08-17 11:03:03 -04001829 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001830 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001831 }
1832
Michael Jurka28750fb2010-09-24 17:43:49 -07001833 private void completeAddLiveFolder(
1834 Intent data, int screen, int intersectCellX, int intersectCellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001835 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1836 final int[] cellXY = mTmpAddItemCellCoordinates;
1837 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
1838 showOutOfSpaceMessage();
1839 return;
1840 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841
Michael Jurka0280c3b2010-09-17 15:00:07 -07001842 final LiveFolderInfo info = addLiveFolder(this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843
1844 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 final View view = LiveFolderIcon.fromXml(R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07001846 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001847 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 }
1849 }
1850
1851 static LiveFolderInfo addLiveFolder(Context context, Intent data,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001852 int screen, int cellX, int cellY, boolean notify) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853
1854 Intent baseIntent = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_BASE_INTENT);
1855 String name = data.getStringExtra(LiveFolders.EXTRA_LIVE_FOLDER_NAME);
1856
1857 Drawable icon = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 Intent.ShortcutIconResource iconResource = null;
1859
1860 Parcelable extra = data.getParcelableExtra(LiveFolders.EXTRA_LIVE_FOLDER_ICON);
1861 if (extra != null && extra instanceof Intent.ShortcutIconResource) {
1862 try {
1863 iconResource = (Intent.ShortcutIconResource) extra;
1864 final PackageManager packageManager = context.getPackageManager();
1865 Resources resources = packageManager.getResourcesForApplication(
1866 iconResource.packageName);
1867 final int id = resources.getIdentifier(iconResource.resourceName, null, null);
1868 icon = resources.getDrawable(id);
1869 } catch (Exception e) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001870 Log.w(TAG, "Could not load live folder icon: " + extra);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 }
1872 }
1873
1874 if (icon == null) {
1875 icon = context.getResources().getDrawable(R.drawable.ic_launcher_folder);
1876 }
1877
1878 final LiveFolderInfo info = new LiveFolderInfo();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001879 info.icon = Utilities.createIconBitmap(icon, context);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 info.title = name;
1881 info.iconResource = iconResource;
1882 info.uri = data.getData();
1883 info.baseIntent = baseIntent;
1884 info.displayMode = data.getIntExtra(LiveFolders.EXTRA_LIVE_FOLDER_DISPLAY_MODE,
1885 LiveFolders.DISPLAY_MODE_GRID);
1886
1887 LauncherModel.addItemToDatabase(context, info, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001888 screen, cellX, cellY, notify);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001889 sFolders.put(info.id, info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890
1891 return info;
1892 }
1893
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 private void showNotifications() {
1895 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1896 if (statusBar != null) {
1897 statusBar.expand();
1898 }
1899 }
1900
1901 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001902 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001904 Intent chooser = Intent.createChooser(pickWallpaper,
1905 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001906 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1907 // Removed in Eclair MR1
1908// WallpaperManager wm = (WallpaperManager)
1909// getSystemService(Context.WALLPAPER_SERVICE);
1910// WallpaperInfo wi = wm.getWallpaperInfo();
1911// if (wi != null && wi.getSettingsActivity() != null) {
1912// LabeledIntent li = new LabeledIntent(getPackageName(),
1913// R.string.configure_wallpaper, 0);
1914// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1915// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1916// }
Mike Clerona0618e42009-10-22 13:55:21 -07001917 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 }
1919
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001920 /**
1921 * Registers various content observers. The current implementation registers
1922 * only a favorites observer to keep track of the favorites applications.
1923 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001924 private void registerContentObservers() {
1925 ContentResolver resolver = getContentResolver();
1926 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1927 true, mWidgetObserver);
1928 }
1929
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 @Override
1931 public boolean dispatchKeyEvent(KeyEvent event) {
1932 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1933 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001935 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001936 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001937 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001938 dumpState();
1939 return true;
1940 }
1941 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001942 }
1943 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1944 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001945 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 return true;
1947 }
1948 }
1949
1950 return super.dispatchKeyEvent(event);
1951 }
1952
Joe Onorato88ec0992009-11-19 13:16:06 -08001953 @Override
1954 public void onBackPressed() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001955 if (mState == State.ALL_APPS || mState == State.CUSTOMIZE) {
1956 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001957 } else if (mWorkspace.getOpenFolder() != null) {
Joe Onorato88ec0992009-11-19 13:16:06 -08001958 closeFolder();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001959 } else if (isPreviewVisible()) {
Michael Jurkaaf442092010-06-10 17:01:57 -07001960 dismissPreview(mPreviousView);
1961 dismissPreview(mNextView);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001962 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001963 mWorkspace.exitWidgetResizeMode();
1964
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001965 // Back button is a no-op here, but give at least some feedback for the button press
1966 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001967 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001968 }
1969
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 private void closeFolder() {
1971 Folder folder = mWorkspace.getOpenFolder();
1972 if (folder != null) {
1973 closeFolder(folder);
1974 }
1975 }
1976
1977 void closeFolder(Folder folder) {
1978 folder.getInfo().opened = false;
Michael Jurka8c920dd2011-01-20 14:16:56 -08001979 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 if (parent != null) {
Michael Jurkaf3ca3ab2010-10-20 17:08:24 -07001981 CellLayout cl = (CellLayout) parent;
1982 cl.removeViewWithoutMarkingCells(folder);
Joe Onoratob6341e92009-11-02 10:41:48 -05001983 if (folder instanceof DropTarget) {
1984 // Live folders aren't DropTargets.
1985 mDragController.removeDropTarget((DropTarget)folder);
1986 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 }
1988 folder.onClose();
1989 }
1990
1991 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001992 * Re-listen when widgets are reset.
1993 */
1994 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001995 if (mAppWidgetHost != null) {
1996 mAppWidgetHost.startListening();
1997 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001998 }
1999
2000 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002001 * Go through the and disconnect any of the callbacks in the drawables and the views or we
2002 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002004 private void unbindDesktopItems() {
2005 for (ItemInfo item: mDesktopItems) {
2006 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002008 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07002010
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 /**
2012 * Launches the intent referred by the clicked shortcut.
2013 *
2014 * @param v The view representing the clicked shortcut.
2015 */
2016 public void onClick(View v) {
2017 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002018 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08002020 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08002021 int[] pos = new int[2];
2022 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002023 intent.setSourceBounds(new Rect(pos[0], pos[1],
2024 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08002025 boolean success = startActivitySafely(intent, tag);
2026
2027 if (success && v instanceof BubbleTextView) {
2028 mWaitingForResume = (BubbleTextView) v;
2029 mWaitingForResume.setStayPressed(true);
2030 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 } else if (tag instanceof FolderInfo) {
2032 handleFolderClick((FolderInfo) tag);
Joe Onorato7404ee42009-07-31 11:54:44 -07002033 } else if (v == mHandleView) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002034 if (mState == State.ALL_APPS) {
2035 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002036 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08002037 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002038 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 }
2040 }
2041
Michael Jurka0e260592010-06-30 17:07:39 -07002042 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002043 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002044 // clicking anywhere on the workspace causes the customization drawer to slide down
2045 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07002046 return false;
2047 }
2048
Michael Jurkaaf442092010-06-10 17:01:57 -07002049 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002050 * Event handler for the search button
2051 *
2052 * @param v The view that was clicked.
2053 */
2054 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002055 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08002056 // Use a custom animation for launching search
2057 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002058 }
2059
2060 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002061 * Event handler for the voice button
2062 *
2063 * @param v The view that was clicked.
2064 */
2065 public void onClickVoiceButton(View v) {
2066 startVoiceSearch();
2067 }
2068
2069 private void startVoiceSearch() {
2070 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2071 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2072 startActivity(intent);
2073 }
2074
2075 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002076 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07002077 * enters home screen customization mode.
2078 *
2079 * @param v The view that was clicked.
2080 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002081 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07002082 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07002083 }
2084
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002085 /**
2086 * Event handler for the "grid" button that appears on the home screen, which
2087 * enters all apps mode.
2088 *
2089 * @param v The view that was clicked.
2090 */
2091 public void onClickAllAppsButton(View v) {
2092 showAllApps(true);
2093 }
2094
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002095 public void onClickAppMarketButton(View v) {
2096 if (mAppMarketIntent != null) {
2097 startActivitySafely(mAppMarketIntent, "app market");
2098 }
2099 }
2100
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002101 void startApplicationDetailsActivity(ComponentName componentName) {
2102 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002103 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2104 Uri.fromParts("package", packageName, null));
2105 startActivity(intent);
2106 }
2107
Patrick Dubroy5539af72010-09-07 15:22:01 -07002108 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2109 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2110 // System applications cannot be installed. For now, show a toast explaining that.
2111 // We may give them the option of disabling apps this way.
2112 int messageId = R.string.uninstall_system_app_text;
2113 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2114 } else {
2115 String packageName = appInfo.componentName.getPackageName();
2116 String className = appInfo.componentName.getClassName();
2117 Intent intent = new Intent(
2118 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
2119 startActivity(intent);
2120 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002121 }
2122
Michael Jurkaddd62e92011-02-16 17:49:14 -08002123 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2125 try {
2126 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002127 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 } catch (ActivityNotFoundException e) {
2129 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04002130 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 } catch (SecurityException e) {
2132 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002133 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002135 "or use the exported attribute for this activity. "
2136 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002137 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002138 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002139 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002140
Romain Guy8e633c52010-03-04 12:51:36 -08002141 void startActivityForResultSafely(Intent intent, int requestCode) {
2142 try {
2143 startActivityForResult(intent, requestCode);
2144 } catch (ActivityNotFoundException e) {
2145 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2146 } catch (SecurityException e) {
2147 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2148 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2149 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2150 "or use the exported attribute for this activity.", e);
2151 }
2152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153
2154 private void handleFolderClick(FolderInfo folderInfo) {
2155 if (!folderInfo.opened) {
2156 // Close any open folder
2157 closeFolder();
2158 // Open the requested folder
2159 openFolder(folderInfo);
2160 } else {
2161 // Find the open folder...
2162 Folder openFolder = mWorkspace.getFolderForTag(folderInfo);
2163 int folderScreen;
2164 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002165 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 // .. and close it
2167 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002168 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 // Close any folder open on the current screen
2170 closeFolder();
2171 // Pull the folder onto this screen
2172 openFolder(folderInfo);
2173 }
2174 }
2175 }
2176 }
2177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002179 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 * is animated relative to the specified View. If the View is null, no animation
2181 * is played.
2182 *
2183 * @param folderInfo The FolderInfo describing the folder to open.
2184 */
Winson Chungaafa03c2010-06-11 17:34:16 -07002185 public void openFolder(FolderInfo folderInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 Folder openFolder;
2187
2188 if (folderInfo instanceof UserFolderInfo) {
2189 openFolder = UserFolder.fromXml(this);
2190 } else if (folderInfo instanceof LiveFolderInfo) {
Joe Onoratoa5902522009-07-30 13:37:37 -07002191 openFolder = com.android.launcher2.LiveFolder.fromXml(this, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 } else {
2193 return;
2194 }
2195
Joe Onorato00acb122009-08-04 16:04:30 -04002196 openFolder.setDragController(mDragController);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002197 openFolder.setLauncher(this);
2198
2199 openFolder.bind(folderInfo);
2200 folderInfo.opened = true;
2201
Winson Chungaafa03c2010-06-11 17:34:16 -07002202 mWorkspace.addInFullScreen(openFolder, folderInfo.screen);
2203
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002204 openFolder.onOpen();
2205 }
2206
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002207 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08002208 if (mState != State.WORKSPACE) {
2209 return false;
2210 }
2211
Romain Guy1fbc1c82009-11-09 20:43:08 -08002212 switch (v.getId()) {
2213 case R.id.previous_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002214 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002215 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2216 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002217 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002218 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002219 return true;
2220 case R.id.next_screen:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002221 if (mState != State.ALL_APPS) {
Joe Onorato0d44e942009-11-16 18:20:51 -08002222 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2223 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Romain Guyf8e6a802009-12-07 17:48:02 -08002224 showPreviews(v);
2225 }
2226 return true;
2227 case R.id.all_apps_button:
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002228 if (mState != State.ALL_APPS) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002229 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2230 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2231 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002232 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002233 return true;
2234 }
2235
Joe Onorato9c1289c2009-08-17 11:03:03 -04002236 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237 return false;
2238 }
2239
2240 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002241 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 }
2243
Michael Jurka0280c3b2010-09-17 15:00:07 -07002244 resetAddInfo();
2245 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002247 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 return true;
2249 }
2250
Michael Jurka0280c3b2010-09-17 15:00:07 -07002251 final View itemUnderLongClick = longClickCellInfo.cell;
2252
Winson Chung304dcde2011-01-07 11:17:23 -08002253 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002254 if (itemUnderLongClick == null) {
2255 // User long pressed on empty space
2256 mWorkspace.setAllowLongPress(false);
2257 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2258 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Patrick Dubroy8fedddc2011-01-19 14:54:38 -08002259 if (LauncherApplication.isScreenXLarge()) {
2260 addItems();
2261 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002262 showAddDialog(longClickCellInfo.cellX, longClickCellInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002263 }
2264 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002265 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002267 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2268 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002269 mAddIntersectCellX = longClickCellInfo.cellX;
2270 mAddIntersectCellY = longClickCellInfo.cellY;
2271 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272 }
2273 }
2274 }
2275 return true;
2276 }
2277
Romain Guye6b8e2f2009-11-10 11:56:55 -08002278 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002279 private void dismissPreview(final View v) {
2280 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002281 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002282 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2283 public void onDismiss() {
2284 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2285 int count = group.getChildCount();
2286 for (int i = 0; i < count; i++) {
2287 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2288 }
Romain Guy9d31e9f2009-11-11 18:54:28 -08002289 ArrayList<Bitmap> bitmaps = (ArrayList<Bitmap>) v.getTag(R.id.icon);
2290 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2291
2292 v.setTag(R.id.workspace, null);
2293 v.setTag(R.id.icon, null);
2294 window.setOnDismissListener(null);
2295 }
2296 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002297 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002298 }
2299 v.setTag(null);
2300 }
2301
Romain Guyf8e6a802009-12-07 17:48:02 -08002302 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002303 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002304 }
2305
Romain Guya6abce82009-11-10 02:54:41 -08002306 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002307 final Resources resources = getResources();
2308 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002309
Romain Guya6abce82009-11-10 02:54:41 -08002310 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002311
Romain Guy9d31e9f2009-11-11 18:54:28 -08002312 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002313
Romain Guyf8e6a802009-12-07 17:48:02 -08002314 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002315 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002316 int extraW = (int) ((r.left + r.right) * max);
2317 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002318
2319 int aW = cell.getWidth() - extraW;
2320 float w = aW / max;
2321
2322 int width = cell.getWidth();
2323 int height = cell.getHeight();
2324 int x = cell.getLeftPadding();
2325 int y = cell.getTopPadding();
2326 width -= (x + cell.getRightPadding());
2327 height -= (y + cell.getBottomPadding());
2328
2329 float scale = w / width;
2330
2331 int count = end - start;
2332
2333 final float sWidth = width * scale;
2334 float sHeight = height * scale;
2335
Romain Guye6b8e2f2009-11-10 11:56:55 -08002336 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002337
Romain Guye6b8e2f2009-11-10 11:56:55 -08002338 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2339 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002340
2341 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002342 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002343 cell = (CellLayout) workspace.getChildAt(i);
2344
Romain Guyf8e6a802009-12-07 17:48:02 -08002345 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002346 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002347
2348 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002349 c.scale(scale, scale);
2350 c.translate(-cell.getLeftPadding(), -cell.getTopPadding());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002351 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002352
Romain Guy9d31e9f2009-11-11 18:54:28 -08002353 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002354 image.setImageBitmap(bitmap);
2355 image.setTag(i);
2356 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002357 image.setOnFocusChangeListener(handler);
2358 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002359 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002360
2361 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002362 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2363
Winson Chungaafa03c2010-06-11 17:34:16 -07002364 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002365 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002366
2367 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002368 p.setContentView(preview);
2369 p.setWidth((int) (sWidth * count + extraW));
2370 p.setHeight((int) (sHeight + extraH));
2371 p.setAnimationStyle(R.style.AnimationPreview);
2372 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002373 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002374 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002375 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002376
Romain Guye6b8e2f2009-11-10 11:56:55 -08002377 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2378 public void onDismiss() {
2379 dismissPreview(anchor);
2380 }
2381 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002382
2383 anchor.setTag(p);
2384 anchor.setTag(R.id.workspace, preview);
Winson Chungaafa03c2010-06-11 17:34:16 -07002385 anchor.setTag(R.id.icon, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002386 }
2387
Romain Guy9d31e9f2009-11-11 18:54:28 -08002388 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002389 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002390
Romain Guye6b8e2f2009-11-10 11:56:55 -08002391 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002392 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002393 }
2394
2395 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002396 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002397 v.post(this);
2398 }
2399
2400 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002401 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002402 }
2403
2404 public void onFocusChange(View v, boolean hasFocus) {
2405 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002406 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002407 }
Romain Guya6abce82009-11-10 02:54:41 -08002408 }
2409 }
2410
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002411 Workspace getWorkspace() {
2412 return mWorkspace;
2413 }
2414
Michael Jurka3c38d562011-02-18 19:22:12 -08002415 TabHost getCustomizationDrawer() {
2416 return mHomeCustomizationDrawer;
2417 }
2418
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 @Override
2420 protected Dialog onCreateDialog(int id) {
2421 switch (id) {
2422 case DIALOG_CREATE_SHORTCUT:
2423 return new CreateShortcut().createDialog();
2424 case DIALOG_RENAME_FOLDER:
2425 return new RenameFolder().createDialog();
2426 }
2427
2428 return super.onCreateDialog(id);
2429 }
2430
2431 @Override
2432 protected void onPrepareDialog(int id, Dialog dialog) {
2433 switch (id) {
2434 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 break;
2436 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002437 if (mFolderInfo != null) {
2438 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2439 final CharSequence text = mFolderInfo.title;
2440 input.setText(text);
2441 input.setSelection(0, text.length());
2442 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002443 break;
2444 }
2445 }
2446
2447 void showRenameDialog(FolderInfo info) {
2448 mFolderInfo = info;
2449 mWaitingForResult = true;
2450 showDialog(DIALOG_RENAME_FOLDER);
2451 }
2452
Michael Jurka0280c3b2010-09-17 15:00:07 -07002453 private void showAddDialog(int intersectX, int intersectY) {
2454 resetAddInfo();
2455 mAddIntersectCellX = intersectX;
2456 mAddIntersectCellY = intersectY;
2457 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002458 mWaitingForResult = true;
2459 showDialog(DIALOG_CREATE_SHORTCUT);
2460 }
2461
Joe Onoratodeb98af2010-02-19 14:59:39 -08002462 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002463 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002464 Bundle bundle = new Bundle();
2465
2466 ArrayList<String> shortcutNames = new ArrayList<String>();
2467 shortcutNames.add(getString(R.string.group_applications));
2468 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2469
2470 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2471 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2472 R.drawable.ic_launcher_application));
2473 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2474
2475 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2476 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002477 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002478 pickIntent.putExtras(bundle);
2479
Joe Onoratodeb98af2010-02-19 14:59:39 -08002480 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002481 }
2482
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002483 private class RenameFolder {
2484 private EditText mInput;
2485
2486 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002487 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2488 mInput = (EditText) layout.findViewById(R.id.folder_name);
2489
2490 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2491 builder.setIcon(0);
2492 builder.setTitle(getString(R.string.rename_folder_title));
2493 builder.setCancelable(true);
2494 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2495 public void onCancel(DialogInterface dialog) {
2496 cleanup();
2497 }
2498 });
2499 builder.setNegativeButton(getString(R.string.cancel_action),
2500 new Dialog.OnClickListener() {
2501 public void onClick(DialogInterface dialog, int which) {
2502 cleanup();
2503 }
2504 }
2505 );
2506 builder.setPositiveButton(getString(R.string.rename_action),
2507 new Dialog.OnClickListener() {
2508 public void onClick(DialogInterface dialog, int which) {
2509 changeFolderName();
2510 }
2511 }
2512 );
2513 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002514
2515 final AlertDialog dialog = builder.create();
2516 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2517 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002518 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002519 mInput.requestFocus();
2520 InputMethodManager inputManager = (InputMethodManager)
2521 getSystemService(Context.INPUT_METHOD_SERVICE);
2522 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002523 }
2524 });
2525
2526 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002527 }
2528
2529 private void changeFolderName() {
2530 final String name = mInput.getText().toString();
2531 if (!TextUtils.isEmpty(name)) {
2532 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002533 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002534 mFolderInfo.title = name;
2535 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2536
Joe Onorato9c1289c2009-08-17 11:03:03 -04002537 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002538 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002539 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002540 } else {
2541 final FolderIcon folderIcon = (FolderIcon)
2542 mWorkspace.getViewForTag(mFolderInfo);
2543 if (folderIcon != null) {
2544 folderIcon.setText(name);
2545 getWorkspace().requestLayout();
2546 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002547 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002548 mWorkspaceLoading = true;
2549 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002550 }
2551 }
2552 }
2553 cleanup();
2554 }
2555
2556 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002557 dismissDialog(DIALOG_RENAME_FOLDER);
2558 mWaitingForResult = false;
2559 mFolderInfo = null;
2560 }
2561 }
2562
Daniel Sandler843e8602010-06-07 14:59:01 -04002563 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2564 public boolean isAllAppsVisible() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002565 return mState == State.ALL_APPS;
Joe Onoratofb0ca672009-09-14 17:55:46 -04002566 }
2567
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002568 // AllAppsView.Watcher
2569 public void zoomed(float zoom) {
Michael Jurka213d9632010-07-28 11:29:25 -07002570 // In XLarge view, we zoom down the workspace below all apps so it's still visible
2571 if (zoom == 1.0f && !LauncherApplication.isScreenXLarge()) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002572 mWorkspace.setVisibility(View.GONE);
2573 }
2574 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002575
Michael Jurkaafca9532011-02-16 14:50:35 -08002576 private void showAndEnableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002577 button.setVisibility(View.VISIBLE);
2578 button.setFocusable(true);
2579 button.setClickable(true);
2580 }
2581
2582 private void hideToolbarButton(View button) {
2583 button.setAlpha(0.0f);
2584 // We can't set it to GONE, otherwise the RelativeLayout gets screwed up
2585 button.setVisibility(View.INVISIBLE);
Michael Jurkaafca9532011-02-16 14:50:35 -08002586 }
2587
2588 private void disableToolbarButton(View button) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002589 button.setFocusable(false);
2590 button.setClickable(false);
2591 }
2592
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002593 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002594 * Helper function for showing or hiding a toolbar button, possibly animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002595 *
2596 * @param show If true, create an animation to the show the item. Otherwise, hide it.
2597 * @param view The toolbar button to be animated
Chet Haaseb1254a62010-09-07 13:35:00 -07002598 * @param seq A AnimatorSet that will be used to animate the transition. If null, the
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002599 * transition will not be animated.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002600 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002601 private void hideOrShowToolbarButton(boolean show, final View view, AnimatorSet seq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002602 final boolean showing = show;
2603 final boolean hiding = !show;
2604
2605 final int duration = show ?
2606 getResources().getInteger(R.integer.config_toolbarButtonFadeInTime) :
2607 getResources().getInteger(R.integer.config_toolbarButtonFadeOutTime);
2608
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002609 if (seq != null) {
Michael Jurkae0f5a612011-02-07 16:45:41 -08002610 ValueAnimator anim = ValueAnimator.ofFloat(view.getAlpha(), show ? 1.0f : 0.0f);
Chet Haase472b2812010-10-14 07:02:04 -07002611 anim.setDuration(duration);
Michael Jurkae0f5a612011-02-07 16:45:41 -08002612 anim.addUpdateListener(new AnimatorUpdateListener() {
2613 public void onAnimationUpdate(ValueAnimator animation) {
2614 view.setAlpha((Float) animation.getAnimatedValue());
2615 }
2616 });
Michael Jurka8edd75c2010-12-17 20:15:06 -08002617 anim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002618 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002619 public void onAnimationStart(Animator animation) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002620 if (showing) showAndEnableToolbarButton(view);
2621 if (hiding) disableToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002622 }
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002623 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002624 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002625 if (hiding) hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002626 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002627 });
2628 seq.play(anim);
2629 } else {
2630 if (showing) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002631 showAndEnableToolbarButton(view);
Michael Jurkaea573482011-02-03 19:48:18 -08002632 view.setAlpha(1f);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002633 } else {
Michael Jurkaafca9532011-02-16 14:50:35 -08002634 disableToolbarButton(view);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002635 hideToolbarButton(view);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002636 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002637 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002638 }
2639
2640 /**
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002641 * Show/hide the appropriate toolbar buttons for newState.
2642 * If showSeq or hideSeq is null, the transition will be done immediately (not animated).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002643 *
2644 * @param newState The state that is being switched to
Chet Haaseb1254a62010-09-07 13:35:00 -07002645 * @param showSeq AnimatorSet in which to put "show" animations, or null.
2646 * @param hideSeq AnimatorSet in which to put "hide" animations, or null.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002647 */
Chet Haaseb1254a62010-09-07 13:35:00 -07002648 private void hideAndShowToolbarButtons(State newState, AnimatorSet showSeq, AnimatorSet hideSeq) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002649 switch (newState) {
2650 case WORKSPACE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002651 hideOrShowToolbarButton(true, mButtonCluster, showSeq);
2652 mDeleteZone.setOverlappingViews(
2653 new View[] { mAllAppsButton, mDivider, mConfigureButton });
Michael Jurka577d0172010-12-17 20:04:50 -08002654 mDeleteZone.setDragAndDropEnabled(true);
2655 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_workspace));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002656 break;
2657 case ALL_APPS:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002658 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002659 mDeleteZone.setDragAndDropEnabled(false);
2660 mDeleteZone.setText(getResources().getString(R.string.delete_zone_label_all_apps));
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002661 break;
2662 case CUSTOMIZE:
Michael Jurka838a4ca2011-02-07 13:33:06 -08002663 hideOrShowToolbarButton(false, mButtonCluster, hideSeq);
Michael Jurka577d0172010-12-17 20:04:50 -08002664 mDeleteZone.setDragAndDropEnabled(false);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002665 break;
2666 }
2667 }
2668
2669 /**
2670 * Helper method for the cameraZoomIn/cameraZoomOut animations
2671 * @param view The view being animated
2672 * @param state The state that we are moving in or out of -- either ALL_APPS or CUSTOMIZE
2673 * @param scaleFactor The scale factor used for the zoom
2674 */
2675 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2676 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002677
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002678 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002679 // Set pivotY so that at the starting zoom factor, the view is partially
2680 // visible. Modifying initialHeightFactor changes how much of the view is
2681 // initially showing, and hence the perceived angle from which the view enters.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002682 if (state == State.ALL_APPS) {
Michael Jurkaea573482011-02-03 19:48:18 -08002683 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002684 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002685 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002686 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002687 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002688 }
2689 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002690
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002691 /**
2692 * Zoom the camera out from the workspace to reveal 'toView'.
2693 * Assumes that the view to show is anchored at either the very top or very bottom
2694 * of the screen.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002695 * @param toState The state to zoom out to. Must be ALL_APPS or CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002696 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002697 private void cameraZoomOut(State toState, boolean animated) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002698 final Resources res = getResources();
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002699 final boolean toAllApps = (toState == State.ALL_APPS);
Adam Cohenf16e5712011-01-13 13:31:45 -08002700
2701 final int duration = toAllApps ?
2702 res.getInteger(R.integer.config_allAppsZoomInTime) :
2703 res.getInteger(R.integer.config_customizeZoomInTime);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002704 final int fadeDuration = toAllApps ?
2705 res.getInteger(R.integer.config_allAppsFadeInTime) :
2706 res.getInteger(R.integer.config_customizeFadeInTime);
Adam Cohenf16e5712011-01-13 13:31:45 -08002707
2708 final float scale = toAllApps ?
2709 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2710 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2711
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002712 final View toView = toAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002713
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002714 setPivotsForZoom(toView, toState, scale);
2715
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002716 if (toAllApps) {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002717 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002718 } else {
Michael Jurkaaf91de02010-11-23 16:23:58 -08002719 mWorkspace.shrink(ShrinkState.TOP, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002720 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002721
2722 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002723 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002724 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002725 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2726 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002727 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002728 toView.setFastScaleX(a * scale + b * 1f);
2729 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002730 }
2731 });
Adam Cohen61033d32010-11-15 18:29:44 -08002732
Adam Cohenf16e5712011-01-13 13:31:45 -08002733 if (toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002734 toView.setFastAlpha(0f);
Michael Jurka9c6fbed2011-03-02 17:41:34 -08002735 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
Michael Jurkaea573482011-02-03 19:48:18 -08002736 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002737 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2738 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002739 // don't need to invalidate because we do so above
2740 toView.setFastAlpha(a * 0f + b * 1f);
2741 }
2742 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002743 alphaAnim.start();
2744 }
2745
Michael Jurkaabded662011-03-04 12:06:57 -08002746 if (toView instanceof LauncherTransitionable) {
2747 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002748 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002749 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002750 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002751 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002752 // Prepare the position
2753 toView.setTranslationX(0.0f);
2754 toView.setTranslationY(0.0f);
2755 toView.setVisibility(View.VISIBLE);
Adam Cohenf16e5712011-01-13 13:31:45 -08002756 if (!toAllApps) {
Michael Jurka2763be32011-02-24 11:19:57 -08002757 toView.setFastAlpha(1.0f);
Adam Cohenf16e5712011-01-13 13:31:45 -08002758 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002759 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002760 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002761 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002762 // If we don't set the final scale values here, if this animation is cancelled
2763 // it will have the wrong scale value and subsequent cameraPan animations will
2764 // not fix that
2765 toView.setScaleX(1.0f);
2766 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002767 if (toView instanceof LauncherTransitionable) {
2768 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002769 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002770 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002771 });
2772
Chet Haaseb1254a62010-09-07 13:35:00 -07002773 AnimatorSet toolbarHideAnim = new AnimatorSet();
2774 AnimatorSet toolbarShowAnim = new AnimatorSet();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002775 hideAndShowToolbarButtons(toState, toolbarShowAnim, toolbarHideAnim);
2776
2777 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002778 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002779
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002780 if (mStateAnimation != null) mStateAnimation.cancel();
2781 mStateAnimation = new AnimatorSet();
2782 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim);
2783 mStateAnimation.play(scaleAnim).after(startDelay);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002784
2785 // Show the new toolbar buttons just as the main animation is ending
2786 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002787 mStateAnimation.play(toolbarShowAnim).after(duration + startDelay - fadeInTime);
2788 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002789 } else {
2790 toView.setTranslationX(0.0f);
2791 toView.setTranslationY(0.0f);
2792 toView.setScaleX(1.0f);
2793 toView.setScaleY(1.0f);
2794 toView.setVisibility(View.VISIBLE);
Michael Jurkaabded662011-03-04 12:06:57 -08002795 if (toView instanceof LauncherTransitionable) {
2796 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2797 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
2798 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002799 hideAndShowToolbarButtons(toState, null, null);
2800 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002801 }
2802
2803 /**
2804 * Zoom the camera back into the workspace, hiding 'fromView'.
2805 * This is the opposite of cameraZoomOut.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002806 * @param fromState The current state (must be ALL_APPS or CUSTOMIZE).
2807 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002808 */
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002809 private void cameraZoomIn(State fromState, boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08002810 cameraZoomIn(fromState, animated, false);
2811 }
2812
2813 private void cameraZoomIn(State fromState, boolean animated, boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002814 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002815 final boolean fromAllApps = (fromState == State.ALL_APPS);
2816
2817 int duration = fromAllApps ?
2818 res.getInteger(R.integer.config_allAppsZoomOutTime) :
2819 res.getInteger(R.integer.config_customizeZoomOutTime);
2820
Michael Jurka742574b2011-02-02 23:51:01 -08002821 final float scaleFactor = fromAllApps ?
Adam Cohenf16e5712011-01-13 13:31:45 -08002822 (float) res.getInteger(R.integer.config_allAppsZoomScaleFactor) :
2823 (float) res.getInteger(R.integer.config_customizeZoomScaleFactor);
2824
2825 final View fromView = fromAllApps ? (View) mAllAppsGrid : mHomeCustomizationDrawer;
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002826
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002827 mCustomizePagedView.endChoiceMode();
2828 mAllAppsPagedView.endChoiceMode();
2829
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002830 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002831
Michael Jurkad3ef3062010-11-23 16:23:58 -08002832 if (!springLoaded) {
2833 mWorkspace.unshrink(animated);
2834 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002835 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002836 if (mStateAnimation != null) mStateAnimation.cancel();
2837 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002838
Michael Jurka742574b2011-02-02 23:51:01 -08002839 final float oldScaleX = fromView.getScaleX();
2840 final float oldScaleY = fromView.getScaleY();
2841
2842 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2843 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002844 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2845 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002846 ((View)fromView.getParent()).fastInvalidate();
2847 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2848 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2849 }
2850 });
Michael Jurkaabded662011-03-04 12:06:57 -08002851 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Adam Cohen61033d32010-11-15 18:29:44 -08002852 alphaAnim.setDuration(res.getInteger(R.integer.config_allAppsFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002853 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002854 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2855 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002856 // don't need to invalidate because we do so above
2857 fromView.setFastAlpha(a * 1f + b * 0f);
2858 }
2859 });
Michael Jurkaabded662011-03-04 12:06:57 -08002860 if (fromView instanceof LauncherTransitionable) {
2861 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002862 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002863 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002864 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002865 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002866 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002867 if (fromView instanceof LauncherTransitionable) {
2868 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002869 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002870 }
2871 });
2872
Chet Haaseb1254a62010-09-07 13:35:00 -07002873 AnimatorSet toolbarHideAnim = new AnimatorSet();
2874 AnimatorSet toolbarShowAnim = new AnimatorSet();
Michael Jurkad3ef3062010-11-23 16:23:58 -08002875 if (!springLoaded) {
2876 hideAndShowToolbarButtons(State.WORKSPACE, toolbarShowAnim, toolbarHideAnim);
2877 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002878
Adam Cohen61033d32010-11-15 18:29:44 -08002879 mStateAnimation.playTogether(scaleAnim, toolbarHideAnim, alphaAnim);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002880
2881 // Show the new toolbar buttons at the very end of the whole animation
2882 final int fadeInTime = res.getInteger(R.integer.config_toolbarButtonFadeInTime);
2883 final int unshrinkTime = res.getInteger(R.integer.config_workspaceUnshrinkTime);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002884 mStateAnimation.play(toolbarShowAnim).after(unshrinkTime - fadeInTime);
2885 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002886 } else {
2887 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002888 if (fromView instanceof LauncherTransitionable) {
2889 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2890 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
2891 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002892 if (!springLoaded) {
2893 hideAndShowToolbarButtons(State.WORKSPACE, null, null);
2894 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002895 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002896 }
2897
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002898 void showAllApps(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002899 if (mState != State.WORKSPACE) {
Winson Chung80baf5a2010-08-09 16:03:15 -07002900 return;
Michael Jurka883f55b2010-10-21 15:47:14 -07002901 }
Winson Chung80baf5a2010-08-09 16:03:15 -07002902
Michael Jurka79212d82010-07-30 16:36:20 -07002903 if (LauncherApplication.isScreenXLarge()) {
Michael Jurkaafca9532011-02-16 14:50:35 -08002904 cameraZoomOut(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002905 } else {
2906 mAllAppsGrid.zoom(1.0f, animated);
2907 }
Joe Onorato3a8820b2009-11-10 15:06:42 -08002908
Romain Guyc16fea72010-03-12 17:17:56 -08002909 ((View) mAllAppsGrid).setFocusable(true);
2910 ((View) mAllAppsGrid).requestFocus();
Winson Chungaafa03c2010-06-11 17:34:16 -07002911
Joe Onorato7c312c12009-08-13 21:36:53 -07002912 // TODO: fade these two too
2913 mDeleteZone.setVisibility(View.GONE);
Adam Lesinski6b879f02010-11-04 16:15:23 -07002914
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002915 // Change the state *after* we've called all the transition code
2916 mState = State.ALL_APPS;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002917
Winson Chung5fb63472011-02-02 17:03:37 -08002918 // Pause the auto-advance of widgets until we are out of AllApps
2919 mUserPresent = false;
2920 updateRunning();
2921
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002922 // send an accessibility event to announce the context change
2923 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2924 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002925
2926 void showWorkspace(boolean animated) {
2927 showWorkspace(animated, null);
2928 }
2929
2930 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002931 if (layout != null) {
2932 // always animated, but that's ok since we never specify a layout and
2933 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002934 mWorkspace.unshrink(layout);
2935 } else {
2936 mWorkspace.unshrink(animated);
2937 }
2938 if (mState == State.ALL_APPS) {
2939 closeAllApps(animated);
2940 } else if (mState == State.CUSTOMIZE) {
2941 hideCustomizationDrawer(animated);
2942 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002943
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002944 // Change the state *after* we've called all the transition code
2945 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002946
Winson Chung5fb63472011-02-02 17:03:37 -08002947 // Resume the auto-advance of widgets
2948 mUserPresent = true;
2949 updateRunning();
2950
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002951 // send an accessibility event to announce the context change
2952 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002953 }
2954
Michael Jurkad3ef3062010-11-23 16:23:58 -08002955 void enterSpringLoadedDragMode(CellLayout layout) {
2956 mWorkspace.enterSpringLoadedDragMode(layout);
2957 if (mState == State.ALL_APPS) {
2958 cameraZoomIn(State.ALL_APPS, true, true);
2959 mState = State.ALL_APPS_SPRING_LOADED;
2960 } else if (mState == State.CUSTOMIZE) {
2961 cameraZoomIn(State.CUSTOMIZE, true, true);
2962 mState = State.CUSTOMIZE_SPRING_LOADED;
2963 }/* else {
2964 // we're already in spring loaded mode; don't do anything
2965 }*/
2966 }
2967
2968 void exitSpringLoadedDragMode() {
2969 if (mState == State.ALL_APPS_SPRING_LOADED) {
2970 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
2971 cameraZoomOut(State.ALL_APPS, true);
2972 mState = State.ALL_APPS;
2973 } else if (mState == State.CUSTOMIZE_SPRING_LOADED) {
2974 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.TOP);
2975 cameraZoomOut(State.CUSTOMIZE, true);
2976 mState = State.CUSTOMIZE;
2977 }/* else {
2978 // we're not in spring loaded mode; don't do anything
2979 }*/
2980 }
2981
Joe Onoratob2061212009-11-24 16:13:54 -05002982 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002983 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002984 * - Home from workspace
2985 * - from center screen
2986 * - from other screens
2987 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002988 * - from center screen
2989 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002990 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002991 * - from center screen
2992 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002993 * - Launch app from workspace and quit
2994 * - with back
2995 * - with home
2996 * - Launch app from all apps and quit
2997 * - with back
2998 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002999 * - Go to a screen that's not the default, then all
3000 * apps, and launch and app, and go back
3001 * - with back
3002 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05003003 * - On workspace, long press power and go back
3004 * - with back
3005 * - with home
3006 * - On all apps, long press power and go back
3007 * - with back
3008 * - with home
3009 * - On workspace, power off
3010 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08003011 * - Launch an app and turn off the screen while in that app
3012 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003013 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08003014 * - From all apps
3015 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07003016 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3017 * - From all apps
3018 * - From the center workspace
3019 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05003020 */
Joe Onorato7bb17492009-09-24 17:51:01 -07003021 void closeAllApps(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003022 if (mState == State.ALL_APPS || mState == State.ALL_APPS_SPRING_LOADED) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003023 mWorkspace.setVisibility(View.VISIBLE);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003024 if (LauncherApplication.isScreenXLarge()) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003025 cameraZoomIn(State.ALL_APPS, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003026 } else {
3027 mAllAppsGrid.zoom(0.0f, animated);
3028 }
Daniel Sandler388f6792010-03-02 14:08:08 -05003029 ((View)mAllAppsGrid).setFocusable(false);
Winson Chung097eb0a2011-03-18 16:56:08 -07003030 ((CellLayout) mWorkspace.getChildAt(mWorkspace.getCurrentPage())).getChildrenLayout().requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07003031 }
Joe Onorato7404ee42009-07-31 11:54:44 -07003032 }
3033
Joe Onorato7c312c12009-08-13 21:36:53 -07003034 void lockAllApps() {
3035 // TODO
3036 }
3037
3038 void unlockAllApps() {
3039 // TODO
3040 }
3041
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003042 // Show the customization drawer (only exists in x-large configuration)
3043 private void showCustomizationDrawer(boolean animated) {
Michael Jurkaafca9532011-02-16 14:50:35 -08003044 if (mState != State.WORKSPACE) {
3045 return;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003046 }
Michael Jurkaafca9532011-02-16 14:50:35 -08003047
3048 cameraZoomOut(State.CUSTOMIZE, animated);
3049
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003050 // Change the state *after* we've called all the transition code
3051 mState = State.CUSTOMIZE;
Winson Chung5fb63472011-02-02 17:03:37 -08003052
3053 // Pause the auto-advance of widgets until we are out of Customization drawer
3054 mUserPresent = false;
3055 updateRunning();
Patrick Dubroy558654c2010-07-23 16:48:11 -07003056 }
3057
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003058 // Hide the customization drawer (only exists in x-large configuration)
3059 void hideCustomizationDrawer(boolean animated) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08003060 if (mState == State.CUSTOMIZE || mState == State.CUSTOMIZE_SPRING_LOADED) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003061 cameraZoomIn(State.CUSTOMIZE, animated);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003062 }
3063 }
3064
Patrick Dubroy5f445422011-02-18 14:35:21 -08003065 /**
3066 * Add an item from all apps or customize onto the given workspace screen.
3067 * If layout is null, add to the current screen.
3068 */
3069 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003070 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
3071 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08003072 } else {
3073 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07003074 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003075 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003076
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003077 void onWorkspaceClick(CellLayout layout) {
3078 showWorkspace(true, layout);
Michael Jurka213d9632010-07-28 11:29:25 -07003079 }
3080
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003081 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003082 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003083 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003084 // Look for the toolbar icon specified in the activity meta-data
3085 Bundle metaData = packageManager.getActivityInfo(
3086 activityName, PackageManager.GET_META_DATA).metaData;
3087 if (metaData != null) {
3088 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
3089 if (iconResId != 0) {
3090 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003091 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003092 }
3093 }
3094 } catch (NameNotFoundException e) {
3095 // Do nothing
3096 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003097 return null;
3098 }
3099
3100 // if successful in getting icon, return it; otherwise, set button to use default drawable
3101 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
3102 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3103 TextView button = (TextView) findViewById(buttonId);
3104 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3105
3106 // If we were unable to find the icon via the meta-data, use a generic one
3107 if (toolbarIcon == null) {
3108 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
3109 return null;
3110 } else {
3111 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
3112 return toolbarIcon.getConstantState();
3113 }
3114 }
3115
3116 // if successful in getting icon, return it; otherwise, set button to use default drawable
3117 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
3118 int buttonId, ComponentName activityName, int fallbackDrawableId) {
3119 ImageView button = (ImageView) findViewById(buttonId);
3120 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
3121
Michael Jurka0423dcf2010-10-05 14:56:18 -07003122 // If we were unable to find the icon via the meta-data, use a generic one
3123 if (toolbarIcon == null) {
3124 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003125 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07003126 } else {
3127 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003128 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003129 }
3130 }
3131
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003132 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
3133 TextView button = (TextView) findViewById(buttonId);
3134 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
3135 }
3136
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003137 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003138 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003139 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003140 }
3141
Michael Jurka0423dcf2010-10-05 14:56:18 -07003142 private void updateGlobalSearchIcon() {
3143 if (LauncherApplication.isScreenXLarge()) {
3144 final SearchManager searchManager =
3145 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3146 ComponentName activityName = searchManager.getGlobalSearchActivity();
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003147 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003148 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Michael Jurka789744f2011-01-25 18:41:55 -08003149 R.id.search_button, activityName, R.drawable.ic_generic_search);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003150 } else {
3151 findViewById(R.id.search_button).setVisibility(View.GONE);
3152 }
3153 }
3154 }
3155
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003156 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003157 updateButtonWithDrawable(R.id.search_button, d);
3158 }
3159
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003160 private void updateVoiceSearchIcon() {
3161 if (LauncherApplication.isScreenXLarge()) {
3162 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3163 ComponentName activityName = intent.resolveActivity(getPackageManager());
3164 if (activityName != null) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003165 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003166 R.id.voice_button, activityName, R.drawable.ic_voice_search);
3167 } else {
3168 findViewById(R.id.voice_button).setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003169 }
3170 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003171 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003172
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003173 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003174 updateButtonWithDrawable(R.id.voice_button, d);
3175 }
3176
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003177 /**
3178 * Sets the app market icon (shown when all apps is visible on x-large screens)
3179 */
3180 private void updateAppMarketIcon() {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003181 if (LauncherApplication.isScreenXLarge()) {
3182 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3183 // Find the app market activity by resolving an intent.
3184 // (If multiple app markets are installed, it will return the ResolverActivity.)
3185 ComponentName activityName = intent.resolveActivity(getPackageManager());
3186 if (activityName != null) {
3187 mAppMarketIntent = intent;
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003188 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Michael Jurka0423dcf2010-10-05 14:56:18 -07003189 R.id.market_button, activityName, R.drawable.app_market_generic);
3190 }
3191 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003192 }
3193
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003194 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003195 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003196 }
3197
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003198 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003199 * Displays the shortcut creation dialog and launches, if necessary, the
3200 * appropriate activity.
3201 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003202 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07003203 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
3204 DialogInterface.OnShowListener {
3205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07003207
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003208 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003209 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07003210
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003211 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
3212 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003213 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07003214
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003215 builder.setInverseBackgroundForced(true);
3216
3217 AlertDialog dialog = builder.create();
3218 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07003219 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07003220 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003221
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003222 return dialog;
3223 }
3224
3225 public void onCancel(DialogInterface dialog) {
3226 mWaitingForResult = false;
3227 cleanup();
3228 }
3229
Romain Guycbb89e42009-06-08 15:52:54 -07003230 public void onDismiss(DialogInterface dialog) {
Romain Guycbb89e42009-06-08 15:52:54 -07003231 }
3232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003233 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08003234 try {
3235 dismissDialog(DIALOG_CREATE_SHORTCUT);
3236 } catch (Exception e) {
3237 // An exception is thrown if the dialog is not visible, which is fine
3238 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003239 }
3240
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003241 /**
3242 * Handle the action clicked in the "Add to home" dialog.
3243 */
3244 public void onClick(DialogInterface dialog, int which) {
3245 Resources res = getResources();
3246 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07003247
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003248 switch (which) {
3249 case AddAdapter.ITEM_SHORTCUT: {
Joe Onoratodeb98af2010-02-19 14:59:39 -08003250 pickShortcut();
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003251 break;
3252 }
Romain Guycbb89e42009-06-08 15:52:54 -07003253
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003254 case AddAdapter.ITEM_APPWIDGET: {
3255 int appWidgetId = Launcher.this.mAppWidgetHost.allocateAppWidgetId();
Romain Guycbb89e42009-06-08 15:52:54 -07003256
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003257 Intent pickIntent = new Intent(AppWidgetManager.ACTION_APPWIDGET_PICK);
3258 pickIntent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003259 // start the pick activity
3260 startActivityForResult(pickIntent, REQUEST_PICK_APPWIDGET);
3261 break;
3262 }
Romain Guycbb89e42009-06-08 15:52:54 -07003263
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003264 case AddAdapter.ITEM_LIVE_FOLDER: {
3265 // Insert extra item to handle inserting folder
3266 Bundle bundle = new Bundle();
Romain Guycbb89e42009-06-08 15:52:54 -07003267
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003268 ArrayList<String> shortcutNames = new ArrayList<String>();
3269 shortcutNames.add(res.getString(R.string.group_folder));
3270 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
Romain Guycbb89e42009-06-08 15:52:54 -07003271
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003272 ArrayList<ShortcutIconResource> shortcutIcons =
3273 new ArrayList<ShortcutIconResource>();
3274 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
3275 R.drawable.ic_launcher_folder));
3276 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
3277
3278 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
3279 pickIntent.putExtra(Intent.EXTRA_INTENT,
3280 new Intent(LiveFolders.ACTION_CREATE_LIVE_FOLDER));
3281 pickIntent.putExtra(Intent.EXTRA_TITLE,
3282 getText(R.string.title_select_live_folder));
3283 pickIntent.putExtras(bundle);
Romain Guycbb89e42009-06-08 15:52:54 -07003284
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07003285 startActivityForResult(pickIntent, REQUEST_PICK_LIVE_FOLDER);
3286 break;
3287 }
3288
3289 case AddAdapter.ITEM_WALLPAPER: {
3290 startWallpaper();
3291 break;
3292 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003293 }
3294 }
Romain Guy7b4ef332009-07-14 13:58:08 -07003295
3296 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07003297 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07003298 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003299 }
3300
3301 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003302 * Receives notifications when applications are added/removed.
3303 */
3304 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3305 @Override
3306 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003307 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05003308 String reason = intent.getStringExtra("reason");
3309 if (!"homekey".equals(reason)) {
3310 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08003311 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05003312 animate = false;
3313 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003314 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05003315 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003316 }
3317 }
3318
3319 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003320 * Receives notifications whenever the appwidgets are reset.
3321 */
3322 private class AppWidgetResetObserver extends ContentObserver {
3323 public AppWidgetResetObserver() {
3324 super(new Handler());
3325 }
3326
3327 @Override
3328 public void onChange(boolean selfChange) {
3329 onAppWidgetReset();
3330 }
3331 }
3332
3333 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003334 * If the activity is currently paused, signal that we need to re-run the loader
3335 * in onResume.
3336 *
3337 * This needs to be called from incoming places where resources might have been loaded
3338 * while we are paused. That is becaues the Configuration might be wrong
3339 * when we're not running, and if it comes back to what it was when we
3340 * were paused, we are not restarted.
3341 *
3342 * Implementation of the method from LauncherModel.Callbacks.
3343 *
3344 * @return true if we are currently paused. The caller might be able to
3345 * skip some work in that case since we will come back again.
3346 */
3347 public boolean setLoadOnResume() {
3348 if (mPaused) {
3349 Log.i(TAG, "setLoadOnResume");
3350 mOnResumeNeedsLoad = true;
3351 return true;
3352 } else {
3353 return false;
3354 }
3355 }
3356
3357 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003358 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003359 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003360 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003361 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003362 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003363 } else {
3364 return SCREEN_COUNT / 2;
3365 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003366 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003367
Winson Chunga12a2502010-12-20 14:41:35 -08003368 void setAllAppsPagedView(AllAppsPagedView view) {
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003369 mAllAppsPagedView = view;
3370 }
3371
Joe Onorato9c1289c2009-08-17 11:03:03 -04003372 /**
3373 * Refreshes the shortcuts shown on the workspace.
3374 *
3375 * Implementation of the method from LauncherModel.Callbacks.
3376 */
3377 public void startBinding() {
3378 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003379
3380 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003381 int count = workspace.getChildCount();
3382 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003383 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003384 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3385 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003386 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003387
Joe Onorato9c1289c2009-08-17 11:03:03 -04003388 if (DEBUG_USER_INTERFACE) {
3389 android.widget.Button finishButton = new android.widget.Button(this);
3390 finishButton.setText("Finish");
3391 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3392
3393 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3394 public void onClick(View v) {
3395 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003396 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003397 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003398 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003399
3400 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3401 // mDesktopItems -> AppWidgetInfo -> hostView).
3402 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003403 }
3404
3405 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003406 * Bind the items start-end from the list.
3407 *
3408 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003409 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003410 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3411
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003412 setLoadOnResume();
3413
Joe Onorato9c1289c2009-08-17 11:03:03 -04003414 final Workspace workspace = mWorkspace;
3415
3416 for (int i=start; i<end; i++) {
3417 final ItemInfo item = shortcuts.get(i);
3418 mDesktopItems.add(item);
3419 switch (item.itemType) {
3420 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3421 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003422 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003423 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3424 false);
3425 break;
3426 case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
3427 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003428 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Michael Jurkac9a96192010-11-01 11:52:08 -07003429 (UserFolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003430 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3431 false);
3432 break;
3433 case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
3434 final FolderIcon newLiveFolder = LiveFolderIcon.fromXml(
3435 R.layout.live_folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003436 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Joe Onorato9c1289c2009-08-17 11:03:03 -04003437 (LiveFolderInfo) item);
3438 workspace.addInScreen(newLiveFolder, item.screen, item.cellX, item.cellY, 1, 1,
3439 false);
3440 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003441 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003442 }
3443
Joe Onorato9c1289c2009-08-17 11:03:03 -04003444 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003445 }
3446
Joe Onorato9c1289c2009-08-17 11:03:03 -04003447 /**
3448 * Implementation of the method from LauncherModel.Callbacks.
3449 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003450 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003451 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003452 sFolders.clear();
3453 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003454 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003455
3456 /**
3457 * Add the views for a widget to the workspace.
3458 *
3459 * Implementation of the method from LauncherModel.Callbacks.
3460 */
3461 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003462 setLoadOnResume();
3463
Daniel Sandler843e8602010-06-07 14:59:01 -04003464 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3465 if (DEBUG_WIDGETS) {
3466 Log.d(TAG, "bindAppWidget: " + item);
3467 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003468 final Workspace workspace = mWorkspace;
3469
3470 final int appWidgetId = item.appWidgetId;
3471 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003472 if (DEBUG_WIDGETS) {
3473 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3474 }
3475
Joe Onorato9c1289c2009-08-17 11:03:03 -04003476 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3477
Joe Onorato9c1289c2009-08-17 11:03:03 -04003478 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3479 item.hostView.setTag(item);
3480
3481 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3482 item.cellY, item.spanX, item.spanY, false);
3483
Adam Cohended9f8d2010-11-03 13:25:16 -07003484 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3485
Joe Onorato9c1289c2009-08-17 11:03:03 -04003486 workspace.requestLayout();
3487
3488 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003489
3490 if (DEBUG_WIDGETS) {
3491 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3492 + (SystemClock.uptimeMillis()-start) + "ms");
3493 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003494 }
3495
3496 /**
3497 * Callback saying that there aren't any more items to bind.
3498 *
3499 * Implementation of the method from LauncherModel.Callbacks.
3500 */
3501 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003502 setLoadOnResume();
3503
Joe Onorato9c1289c2009-08-17 11:03:03 -04003504 if (mSavedState != null) {
3505 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003506 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003507 }
3508
3509 final long[] userFolders = mSavedState.getLongArray(RUNTIME_STATE_USER_FOLDERS);
3510 if (userFolders != null) {
3511 for (long folderId : userFolders) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003512 final FolderInfo info = sFolders.get(folderId);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003513 if (info != null) {
3514 openFolder(info);
3515 }
3516 }
3517 final Folder openFolder = mWorkspace.getOpenFolder();
3518 if (openFolder != null) {
3519 openFolder.requestFocus();
3520 }
3521 }
3522
Joe Onorato9c1289c2009-08-17 11:03:03 -04003523 mSavedState = null;
3524 }
3525
3526 if (mSavedInstanceState != null) {
3527 super.onRestoreInstanceState(mSavedInstanceState);
3528 mSavedInstanceState = null;
3529 }
3530
Winson Chung32bb5e52011-01-31 14:51:56 -08003531 // Workaround a bug that occurs when rotating the device while the customization mode is
3532 // open, we trigger a new layout on all the CellLayout children.
3533 if (LauncherApplication.isScreenXLarge() && (mState == State.CUSTOMIZE)) {
3534 final int childCount = mWorkspace.getChildCount();
3535 for (int i = 0; i < childCount; ++i) {
3536 mWorkspace.getChildAt(i).requestLayout();
3537 }
3538 }
3539
Joe Onorato9c1289c2009-08-17 11:03:03 -04003540 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003541
3542 // If we received the result of any pending adds while the loader was running (e.g. the
3543 // widget configuration forced an orientation change), process them now.
3544 for (int i = 0; i < sPendingAddList.size(); i++) {
3545 completeAdd(sPendingAddList.get(i));
3546 }
3547 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003548 }
3549
3550 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003551 * Updates the icons on the launcher that are affected by changes to the package list
3552 * on the device.
3553 */
3554 private void updateIconsAffectedByPackageManagerChanges() {
3555 updateAppMarketIcon();
3556 updateGlobalSearchIcon();
3557 updateVoiceSearchIcon();
3558 }
3559
3560 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003561 * Add the icons for all apps.
3562 *
3563 * Implementation of the method from LauncherModel.Callbacks.
3564 */
3565 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Romain Guy84f296c2009-11-04 15:00:44 -08003566 mAllAppsGrid.setApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003567 if (mCustomizePagedView != null) {
3568 mCustomizePagedView.setApps(apps);
3569 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003570 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 }
3572
3573 /**
3574 * A package was installed.
3575 *
3576 * Implementation of the method from LauncherModel.Callbacks.
3577 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003578 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003579 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003580 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onoratoa8138d52009-10-06 19:25:30 -07003581 mAllAppsGrid.addApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003582 if (mCustomizePagedView != null) {
3583 mCustomizePagedView.addApps(apps);
3584 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003585 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003586 }
3587
3588 /**
3589 * A package was updated.
3590 *
3591 * Implementation of the method from LauncherModel.Callbacks.
3592 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003593 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003594 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003595 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003596 if (mWorkspace != null) {
3597 mWorkspace.updateShortcuts(apps);
3598 }
3599 if (mAllAppsGrid != null) {
3600 mAllAppsGrid.updateApps(apps);
3601 }
Andrew Stadler959f6032010-09-27 11:42:53 -07003602 if (mCustomizePagedView != null) {
3603 mCustomizePagedView.updateApps(apps);
3604 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003605 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003606 }
3607
3608 /**
3609 * A package was uninstalled.
3610 *
3611 * Implementation of the method from LauncherModel.Callbacks.
3612 */
Joe Onorato36115782010-06-17 13:28:48 -04003613 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003614 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003615 if (permanent) {
3616 mWorkspace.removeItems(apps);
3617 }
Joe Onoratoa8138d52009-10-06 19:25:30 -07003618 mAllAppsGrid.removeApps(apps);
Andrew Stadler959f6032010-09-27 11:42:53 -07003619 if (mCustomizePagedView != null) {
3620 mCustomizePagedView.removeApps(apps);
3621 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003622 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003623 }
Joe Onoratobe386092009-11-17 17:32:16 -08003624
3625 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003626 * A number of packages were updated.
3627 */
3628 public void bindPackagesUpdated() {
3629 // update the customization drawer contents
Winson Chung5f941722010-09-28 16:36:43 -07003630 if (mCustomizePagedView != null) {
Winson Chungb3347bb2010-08-19 14:51:28 -07003631 mCustomizePagedView.update();
Winson Chung5f941722010-09-28 16:36:43 -07003632 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003633 }
3634
Winson Chung400438b2011-01-16 17:53:48 -08003635 private int mapConfigurationOriActivityInfoOri(int configOri) {
3636 final Display d = getWindowManager().getDefaultDisplay();
3637 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3638 switch (d.getRotation()) {
3639 case Surface.ROTATION_0:
3640 case Surface.ROTATION_180:
3641 // We are currently in the same basic orientation as the natural orientation
3642 naturalOri = configOri;
3643 break;
3644 case Surface.ROTATION_90:
3645 case Surface.ROTATION_270:
3646 // We are currently in the other basic orientation to the natural orientation
3647 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3648 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3649 break;
3650 }
3651
3652 int[] oriMap = {
3653 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3654 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3655 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3656 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3657 };
3658 // Since the map starts at portrait, we need to offset if this device's natural orientation
3659 // is landscape.
3660 int indexOffset = 0;
3661 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3662 indexOffset = 1;
3663 }
3664 return oriMap[(d.getRotation() + indexOffset) % 4];
3665 }
3666 public void lockScreenOrientation() {
3667 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3668 .getConfiguration().orientation));
3669 }
3670 public void unlockScreenOrientation() {
3671 mHandler.postDelayed(new Runnable() {
3672 public void run() {
3673 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3674 }
3675 }, mRestoreScreenOrientationDelay);
3676 }
3677
Winson Chung80baf5a2010-08-09 16:03:15 -07003678 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003679 * Prints out out state for debugging.
3680 */
3681 public void dumpState() {
3682 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003683 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003684 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3685 Log.d(TAG, "mRestoring=" + mRestoring);
3686 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3687 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3688 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003689 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003690 mModel.dumpState();
3691 mAllAppsGrid.dumpState();
3692 Log.d(TAG, "END launcher2 dump state");
3693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003694}
Michael Jurka2763be32011-02-24 11:19:57 -08003695
Michael Jurkaabded662011-03-04 12:06:57 -08003696interface LauncherTransitionable {
3697 void onLauncherTransitionStart(Animator animation);
3698 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003699}