blob: 530f49ca946715ee53528481fc403b49c40f6942 [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
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.Dialog;
29import android.app.SearchManager;
30import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070031import android.appwidget.AppWidgetManager;
32import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070035import android.content.ClipData;
36import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen716b51e2011-06-30 12:09:54 -070043import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Winson Chungf0ea4d32011-06-06 14:27:16 -070046import android.content.pm.ResolveInfo;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040050import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080053import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080055import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040063import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080064import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
69import android.text.TextUtils;
70import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080072import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080073import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.KeyEvent;
75import android.view.LayoutInflater;
76import android.view.Menu;
77import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070078import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080079import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.View;
Adam Cohen716b51e2011-06-30 12:09:54 -070081import android.view.ViewGroup;
Michael Jurkae0f5a612011-02-07 16:45:41 -080082import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080083import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080085import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070087import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070089import android.widget.ImageView;
90import android.widget.LinearLayout;
91import android.widget.PopupWindow;
Winson Chung68846fd2010-10-29 11:00:27 -070092import android.widget.TextView;
93import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070094
Adam Cohendf2cc412011-04-27 16:56:57 -070095import com.android.common.Search;
96import com.android.launcher.R;
97import com.android.launcher2.Workspace.ShrinkState;
Romain Guyedcce092010-03-04 13:03:17 -080098
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.DataInputStream;
100import java.io.DataOutputStream;
101import java.io.FileNotFoundException;
102import java.io.IOException;
103import java.util.ArrayList;
104import java.util.HashMap;
105import java.util.List;
106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107/**
108 * Default launcher application.
109 */
Michael Jurka946ad472010-07-09 18:05:18 -0700110public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700111 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
112 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800113 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700114 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
Joe Onorato9c1289c2009-08-17 11:03:03 -0400116 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400117 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700119
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800121 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700124 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
125 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
127 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700128 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
130 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700131 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_PICK_APPLICATION = 6;
133 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700135 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
138
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800139 static final int SCREEN_COUNT = 5;
140 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Joe Onorato7c312c12009-08-13 21:36:53 -0700142 static final int DIALOG_CREATE_SHORTCUT = 1;
143 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Romain Guy98d01652009-06-30 16:21:04 -0700145 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
147 // Type: int
148 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700149 // Type: int
150 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 // Type: int
152 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
153 // Type: int
154 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cellX";
155 // Type: int
156 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cellY";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: boolean
158 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
159 // Type: long
160 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
161
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700162 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
163
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700164 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700165 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700166 private State mState = State.WORKSPACE;
167 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700168
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700170 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
171 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800174 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800176 private final BroadcastReceiver mCloseSystemDialogsReceiver
177 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800178 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private LayoutInflater mInflater;
181
Joe Onorato00acb122009-08-04 16:04:30 -0400182 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700184
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700185 private AppWidgetManager mAppWidgetManager;
186 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700187
Michael Jurka0280c3b2010-09-17 15:00:07 -0700188 private int mAddScreen = -1;
189 private int mAddIntersectCellX = -1;
190 private int mAddIntersectCellY = -1;
191 private int[] mAddDropPosition;
192 private int[] mTmpAddItemCellCoordinates = new int[2];
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private FolderInfo mFolderInfo;
195
Winson Chung97d85d22011-04-13 11:27:36 -0700196 private ViewGroup mButtonCluster;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800197 private View mAllAppsButton;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700198 private SearchDropTargetBar mSearchDeleteBar;
199 private AppsCustomizeTabHost mAppsCustomizeTabHost;
200 private AppsCustomizePagedView mAppsCustomizeContent;
201 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 private Bundle mSavedState;
204
205 private SpannableStringBuilder mDefaultKeySsb = null;
206
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 private boolean mWorkspaceLoading = true;
208
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800209 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 private boolean mRestoring;
211 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700212 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
214 private Bundle mSavedInstanceState;
215
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800217 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800218 private boolean mUserPresent = true;
219 private boolean mVisible = false;
220 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700222 private static LocaleConfiguration sLocaleConfiguration = null;
223
Romain Guy84f296c2009-11-04 15:00:44 -0800224 private ArrayList<ItemInfo> mDesktopItems = new ArrayList<ItemInfo>();
Adam Cohended9f8d2010-11-03 13:25:16 -0700225
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700226 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700227
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400228 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400229 private String[] mHotseatConfig = null;
230 private Intent[] mHotseats = null;
231 private Drawable[] mHotseatIcons = null;
232 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400233
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700234 private Intent mAppMarketIntent = null;
235
Adam Cohended9f8d2010-11-03 13:25:16 -0700236 // Related to the auto-advancing of widgets
237 private final int ADVANCE_MSG = 1;
238 private final int mAdvanceInterval = 20000;
239 private final int mAdvanceStagger = 250;
240 private long mAutoAdvanceSentTime;
241 private long mAutoAdvanceTimeLeft = -1;
242 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
243 new HashMap<View, AppWidgetProviderInfo>();
244
Winson Chung400438b2011-01-16 17:53:48 -0800245 // Determines how long to wait after a rotation before restoring the screen orientation to
246 // match the sensor state.
247 private final int mRestoreScreenOrientationDelay = 500;
248
Michael Jurka4ef207b2010-11-29 17:05:45 -0800249 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800250 private static Drawable.ConstantState sGlobalSearchIcon;
251 private static Drawable.ConstantState sVoiceSearchIcon;
252 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800253
Adam Cohen2801caf2011-05-13 20:57:39 -0700254 private DragLayer mDragLayer;
255
Michael Jurkaddd62e92011-02-16 17:49:14 -0800256 private BubbleTextView mWaitingForResume;
257
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800258 private static ArrayList<PendingAddArguments> sPendingAddList
259 = new ArrayList<PendingAddArguments>();
260
261 private static class PendingAddArguments {
262 int requestCode;
263 Intent intent;
264 int screen;
265 int cellX;
266 int cellY;
267 }
268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 @Override
270 protected void onCreate(Bundle savedInstanceState) {
271 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800272 LauncherApplication app = ((LauncherApplication)getApplication());
273 mModel = app.setLauncher(this);
274 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400275 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700277
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700278 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700279 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
280 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200283 android.os.Debug.startMethodTracing(
284 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 }
286
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400287 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 setContentView(R.layout.launcher);
290 setupViews();
291
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800292 registerContentObservers();
293
Joe Onorato7c312c12009-08-13 21:36:53 -0700294 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700295
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 mSavedState = savedInstanceState;
297 restoreState(mSavedState);
298
Winson Chunga12a2502010-12-20 14:41:35 -0800299 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700300 if (mAppsCustomizeContent != null) {
301 mAppsCustomizeContent.onPackagesUpdated();
302 }
Winson Chunga12a2502010-12-20 14:41:35 -0800303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 if (PROFILE_STARTUP) {
305 android.os.Debug.stopMethodTracing();
306 }
307
308 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400309 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 }
311
312 // For handling default keys
313 mDefaultKeySsb = new SpannableStringBuilder();
314 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800315
316 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
317 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800318
319 // If we have a saved version of these external icons, we load them up immediately
Winson Chungf0ea4d32011-06-06 14:27:16 -0700320 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
321 updateIconsAffectedByPackageManagerChanges();
322 }
323 if (sGlobalSearchIcon != null) {
324 updateGlobalSearchIcon(sGlobalSearchIcon);
325 }
326 if (sVoiceSearchIcon != null) {
327 updateVoiceSearchIcon(sVoiceSearchIcon);
328 }
329 if (sAppMarketIcon != null) {
330 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800331 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 }
Romain Guycbb89e42009-06-08 15:52:54 -0700333
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800334 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700335 if (sLocaleConfiguration == null) {
336 new AsyncTask<Void, Void, LocaleConfiguration>() {
337 @Override
338 protected LocaleConfiguration doInBackground(Void... unused) {
339 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
340 readConfiguration(Launcher.this, localeConfiguration);
341 return localeConfiguration;
342 }
343
344 @Override
345 protected void onPostExecute(LocaleConfiguration result) {
346 sLocaleConfiguration = result;
347 checkForLocaleChange(); // recursive, but now with a locale configuration
348 }
349 }.execute();
350 return;
351 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 final Configuration configuration = getResources().getConfiguration();
354
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700355 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 final String locale = configuration.locale.toString();
357
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700358 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 final int mcc = configuration.mcc;
360
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700361 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 final int mnc = configuration.mnc;
363
Romain Guy84f296c2009-11-04 15:00:44 -0800364 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365
Romain Guy84f296c2009-11-04 15:00:44 -0800366 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700367 sLocaleConfiguration.locale = locale;
368 sLocaleConfiguration.mcc = mcc;
369 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700370
Joe Onorato0589f0f2010-02-08 13:44:00 -0800371 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400372 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700373
374 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
375 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700376 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700377 public void run() {
378 writeConfiguration(Launcher.this, localeConfiguration);
379 }
380 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700381 }
382 }
383
384 private static class LocaleConfiguration {
385 public String locale;
386 public int mcc = -1;
387 public int mnc = -1;
388 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700389
Romain Guy98d01652009-06-30 16:21:04 -0700390 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
391 DataInputStream in = null;
392 try {
393 in = new DataInputStream(context.openFileInput(PREFERENCES));
394 configuration.locale = in.readUTF();
395 configuration.mcc = in.readInt();
396 configuration.mnc = in.readInt();
397 } catch (FileNotFoundException e) {
398 // Ignore
399 } catch (IOException e) {
400 // Ignore
401 } finally {
402 if (in != null) {
403 try {
404 in.close();
405 } catch (IOException e) {
406 // Ignore
407 }
408 }
409 }
410 }
411
412 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
413 DataOutputStream out = null;
414 try {
415 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
416 out.writeUTF(configuration.locale);
417 out.writeInt(configuration.mcc);
418 out.writeInt(configuration.mnc);
419 out.flush();
420 } catch (FileNotFoundException e) {
421 // Ignore
422 } catch (IOException e) {
423 //noinspection ResultOfMethodCallIgnored
424 context.getFileStreamPath(PREFERENCES).delete();
425 } finally {
426 if (out != null) {
427 try {
428 out.close();
429 } catch (IOException e) {
430 // Ignore
431 }
432 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 }
434 }
435
Adam Cohen716b51e2011-06-30 12:09:54 -0700436 public DragLayer getDragLayer() {
437 return mDragLayer;
438 }
439
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 static int getScreen() {
441 synchronized (sLock) {
442 return sScreen;
443 }
444 }
445
446 static void setScreen(int screen) {
447 synchronized (sLock) {
448 sScreen = screen;
449 }
450 }
451
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400452 // Note: This doesn't do all the client-id magic that BrowserProvider does
453 // in Browser. (http://b/2425179)
454 private Uri getDefaultBrowserUri() {
455 String url = getString(R.string.default_browser_url);
456 if (url.indexOf("{CID}") != -1) {
457 url = url.replace("{CID}", "android-google");
458 }
459 return Uri.parse(url);
460 }
461
462 // Load the Intent templates from arrays.xml to populate the hotseats. For
463 // each Intent, if it resolves to a single app, use that as the launch
464 // intent & use that app's label as the contentDescription. Otherwise,
465 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400466 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400467 if (mHotseatConfig == null) {
468 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
469 if (mHotseatConfig.length > 0) {
470 mHotseats = new Intent[mHotseatConfig.length];
471 mHotseatLabels = new CharSequence[mHotseatConfig.length];
472 mHotseatIcons = new Drawable[mHotseatConfig.length];
473 } else {
474 mHotseats = null;
475 mHotseatIcons = null;
476 mHotseatLabels = null;
477 }
478
479 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
480 for (int i=0; i<mHotseatConfig.length; i++) {
481 // load icon for this slot; currently unrelated to the actual activity
482 try {
483 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
484 } catch (ArrayIndexOutOfBoundsException ex) {
485 Log.w(TAG, "Missing hotseat_icons array item #" + i);
486 mHotseatIcons[i] = null;
487 }
488 }
489 hotseatIconDrawables.recycle();
490 }
491
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400492 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400493 for (int i=0; i<mHotseatConfig.length; i++) {
494 Intent intent = null;
495 if (mHotseatConfig[i].equals("*BROWSER*")) {
496 // magic value meaning "launch user's default web browser"
497 // replace it with a generic web request so we can see if there is indeed a default
498 String defaultUri = getString(R.string.default_browser_url);
499 intent = new Intent(
500 Intent.ACTION_VIEW,
501 ((defaultUri != null)
502 ? Uri.parse(defaultUri)
503 : getDefaultBrowserUri())
504 ).addCategory(Intent.CATEGORY_BROWSABLE);
505 // note: if the user launches this without a default set, she
506 // will always be taken to the default URL above; this is
507 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700508 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400509 // URL is unavoidably sent to the chosen app.
510 } else {
511 try {
512 intent = Intent.parseUri(mHotseatConfig[i], 0);
513 } catch (java.net.URISyntaxException ex) {
514 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
515 // bogus; leave intent=null
516 }
517 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700518
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400519 if (intent == null) {
520 mHotseats[i] = null;
521 mHotseatLabels[i] = getText(R.string.activity_not_found);
522 continue;
523 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400524
525 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700526 Log.d(TAG, "loadHotseats: hotseat " + i
527 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400528 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400529 + "]");
530 }
531
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400532 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
533 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700534 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400535 Log.d(TAG, "Best match for intent: " + bestMatch);
536 Log.d(TAG, "All matches: ");
537 for (ResolveInfo ri : allMatches) {
538 Log.d(TAG, " --> " + ri);
539 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400540 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400541 // did this resolve to a single app, or the resolver?
542 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700543 // can't find any activity to handle this. let's leave the
544 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400545 // to launch.
546 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400547
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400548 // set accessibility text to "Not installed"
549 mHotseatLabels[i] = getText(R.string.activity_not_found);
550 } else {
551 boolean found = false;
552 for (ResolveInfo ri : allMatches) {
553 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
554 && bestMatch.activityInfo.applicationInfo.packageName
555 .equals(ri.activityInfo.applicationInfo.packageName)) {
556 found = true;
557 break;
558 }
559 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700560
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400561 if (!found) {
562 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
563 // the bestMatch is probably the ResolveActivity, meaning the
564 // user has not yet selected a default
565 // so: we'll keep the original intent for now
566 mHotseats[i] = intent;
567
568 // set the accessibility text to "Select shortcut"
569 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
570 } else {
571 // we have an app!
572 // now reconstruct the intent to launch it through the front
573 // door
574 ComponentName com = new ComponentName(
575 bestMatch.activityInfo.applicationInfo.packageName,
576 bestMatch.activityInfo.name);
577 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
578
579 // load the app label for accessibility
580 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
581 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400582 }
583
584 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700585 Log.d(TAG, "loadHotseats: hotseat " + i
586 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400587 + ((mHotseats[i] == null)
588 ? "null"
589 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400590 + "] label=[" + mHotseatLabels[i]
591 + "]"
592 );
593 }
594 }
595 }
596
Winson Chung557d6ed2011-07-08 15:34:52 -0700597 /**
598 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
599 * a configuration step, this allows the proper animations to run after other transitions.
600 */
601 private boolean completeAdd(PendingAddArguments args) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800602 switch (args.requestCode) {
603 case REQUEST_PICK_APPLICATION:
604 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
605 break;
606 case REQUEST_PICK_SHORTCUT:
607 processShortcut(args.intent);
608 break;
609 case REQUEST_CREATE_SHORTCUT:
610 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
Winson Chung557d6ed2011-07-08 15:34:52 -0700611 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800612 case REQUEST_PICK_APPWIDGET:
613 addAppWidgetFromPick(args.intent);
614 break;
615 case REQUEST_CREATE_APPWIDGET:
616 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
617 completeAddAppWidget(appWidgetId, args.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -0700618 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800619 case REQUEST_PICK_WALLPAPER:
620 // We just wanted the activity result here so we can clear mWaitingForResult
621 break;
622 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700623 return false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800624 }
625
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800627 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700628 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700629 mWaitingForResult = false;
630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 // The pattern used here is that a user PICKs a specific application,
632 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
635 // launch over to the Music app to actually CREATE_SHORTCUT.
Romain Guycbb89e42009-06-08 15:52:54 -0700636
Michael Jurka0280c3b2010-09-17 15:00:07 -0700637 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800638 final PendingAddArguments args = new PendingAddArguments();
639 args.requestCode = requestCode;
640 args.intent = data;
641 args.screen = mAddScreen;
642 args.cellX = mAddIntersectCellX;
643 args.cellY = mAddIntersectCellY;
644
645 // If the loader is still running, defer the add until it is done.
646 if (isWorkspaceLocked()) {
647 sPendingAddList.add(args);
648 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700649 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 }
Romain Guy18042c82009-11-06 11:44:55 -0800651 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700652 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
653 if (data != null) {
654 // Clean up the appWidgetId if we canceled
655 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
656 if (appWidgetId != -1) {
657 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800659 }
660 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700661
662 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
663 exitSpringLoadedDragModeDelayed(delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 }
665
666 @Override
667 protected void onResume() {
668 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800669 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700670 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400671 mWorkspaceLoading = true;
672 mModel.startLoader(this, true);
673 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700674 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800676 if (mWaitingForResume != null) {
677 mWaitingForResume.setStayPressed(false);
678 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700679 // When we resume Launcher, a different Activity might be responsible for the app
680 // market intent, so refresh the icon
681 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
683
684 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700685 protected void onPause() {
686 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700687 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800688 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700689 }
Romain Guycbb89e42009-06-08 15:52:54 -0700690
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700691 @Override
692 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400693 // Flag the loader to stop early before switching
694 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700695 if (mAppsCustomizeContent != null) {
696 mAppsCustomizeContent.surrender();
697 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800698 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700699 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700700
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800701 // We can't hide the IME if it was forced open. So don't bother
702 /*
703 @Override
704 public void onWindowFocusChanged(boolean hasFocus) {
705 super.onWindowFocusChanged(hasFocus);
706
707 if (hasFocus) {
708 final InputMethodManager inputManager = (InputMethodManager)
709 getSystemService(Context.INPUT_METHOD_SERVICE);
710 WindowManager.LayoutParams lp = getWindow().getAttributes();
711 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
712 android.os.Handler()) {
713 protected void onReceiveResult(int resultCode, Bundle resultData) {
714 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
715 }
716 });
717 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
718 }
719 }
720 */
721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 private boolean acceptFilter() {
723 final InputMethodManager inputManager = (InputMethodManager)
724 getSystemService(Context.INPUT_METHOD_SERVICE);
725 return !inputManager.isFullscreenMode();
726 }
727
728 @Override
729 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700730 final int uniChar = event.getUnicodeChar();
731 final boolean handled = super.onKeyDown(keyCode, event);
732 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
733 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
735 keyCode, event);
736 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700737 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700738 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700739 // showSearchDialog()
740 // If there are multiple keystrokes before the search dialog takes focus,
741 // onSearchRequested() will be called for every keystroke,
742 // but it is idempotent, so it's fine.
743 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 }
745 }
746
Joe Onorato8a9625e2010-01-28 15:55:35 -0800747 // Eat the long press event so the keyboard doesn't come up.
748 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
749 return true;
750 }
751
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 return handled;
753 }
754
Karl Rosaen138a0412009-04-23 19:00:21 -0700755 private String getTypedText() {
756 return mDefaultKeySsb.toString();
757 }
758
759 private void clearTypedText() {
760 mDefaultKeySsb.clear();
761 mDefaultKeySsb.clearSpans();
762 Selection.setSelection(mDefaultKeySsb, 0);
763 }
764
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800765 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700766 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
767 * State
768 */
769 private static State intToState(int stateOrdinal) {
770 State state = State.WORKSPACE;
771 final State[] stateValues = State.values();
772 for (int i = 0; i < stateValues.length; i++) {
773 if (stateValues[i].ordinal() == stateOrdinal) {
774 state = stateValues[i];
775 break;
776 }
777 }
778 return state;
779 }
780
781 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 * Restores the previous state, if it exists.
783 *
784 * @param savedState The previous state.
785 */
786 private void restoreState(Bundle savedState) {
787 if (savedState == null) {
788 return;
789 }
790
Michael Jurka883f55b2010-10-21 15:47:14 -0700791 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
792
Winson Chungf0ea4d32011-06-06 14:27:16 -0700793 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800794 showAllApps(false);
795 }
796
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
798 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700799 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800 }
801
802 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700803
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700805 mAddScreen = addScreen;
806 mAddIntersectCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
807 mAddIntersectCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 mRestoring = true;
809 }
810
811 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
812 if (renameFolder) {
813 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700814 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 mRestoring = true;
816 }
Winson Chunga12a2502010-12-20 14:41:35 -0800817
Winson Chung785d2eb2011-04-14 16:08:02 -0700818
819 // Restore the AppsCustomize tab
820 if (mAppsCustomizeTabHost != null) {
821 String curTab = savedState.getString("apps_customize_currentTab");
822 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700823 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700824 mAppsCustomizeContent.setContentType(
825 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
826 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
827 }
828
829 // Note: currently we do not restore the page for the AppsCustomize pane because the
830 // change in layout can drastically affect the saved page index
831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
833
834 /**
835 * Finds all the views we need and configure them properly.
836 */
837 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700838 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400839
Winson Chung4c98d922011-05-31 16:50:48 -0700840 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
841 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842
Winson Chung4c98d922011-05-31 16:50:48 -0700843 // Setup the drag layer
844 mDragLayer.setup(this, dragController);
845
846 // Setup the workspace
847 mWorkspace.setHapticFeedbackEnabled(false);
848 mWorkspace.setOnLongClickListener(this);
849 mWorkspace.setup(this, dragController);
850 mWorkspace.setWallpaperDimension();
851
Winson Chungf0ea4d32011-06-06 14:27:16 -0700852 // Get the search/delete bar
853 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700854
Winson Chungf0ea4d32011-06-06 14:27:16 -0700855 // Setup AppsCustomize
856 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
857 findViewById(R.id.apps_customize_pane);
858 mAppsCustomizeContent = (AppsCustomizePagedView)
859 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
860 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400861
Winson Chungf0ea4d32011-06-06 14:27:16 -0700862 // Setup AppsCustomize button
863 mAllAppsButton = mDragLayer.findViewById(R.id.all_apps_button);
864 mAllAppsButton.setOnClickListener(new OnClickListener() {
865 public void onClick(View v) {
866 onClickAllAppsButton(v);
867 }
868 });
Romain Guy1fbc1c82009-11-09 20:43:08 -0800869
Winson Chungf0ea4d32011-06-06 14:27:16 -0700870 if (!LauncherApplication.isScreenLarge()) {
871 // Setup AppsCustomize button on the phone
872 HandleView handleView = (HandleView) mAllAppsButton;
873 handleView.setLauncher(this);
874 handleView.setOnLongClickListener(this);
Winson Chung4c98d922011-05-31 16:50:48 -0700875
876 // Setup Hotseat
877 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
878 hotseatLeft.setContentDescription(mHotseatLabels[0]);
879 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
880 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
881 hotseatRight.setContentDescription(mHotseatLabels[1]);
882 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Adam Cohencdc30d52010-12-01 15:09:47 -0800883 }
884
Winson Chungf0ea4d32011-06-06 14:27:16 -0700885 if (!LauncherApplication.isScreenLarge()) {
886 // Setup keylistener for button cluster
887 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
888 View.OnKeyListener listener = null;
889 if (LauncherApplication.isScreenLarge()) {
890 // For tablets, AllApps lives in the button bar at the top
891 listener = new ButtonBarKeyEventListener();
892 } else {
893 // For phones, AppsCustomize lives in the "dock" at the bottom
894 listener = new DockKeyEventListener();
895 }
896 int buttonCount = mButtonCluster.getChildCount();
897 for (int i = 0; i < buttonCount; ++i) {
898 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
899 }
Winson Chung97d85d22011-04-13 11:27:36 -0700900 }
Michael Jurka838a4ca2011-02-07 13:33:06 -0800901
Winson Chung4c98d922011-05-31 16:50:48 -0700902 // Setup the drag controller (the drop targets have to be added in reverse order)
903 dragController.setDragScoller(mWorkspace);
904 dragController.setScrollView(mDragLayer);
905 dragController.setMoveTarget(mWorkspace);
906 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700907 if (mSearchDeleteBar != null) {
908 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800909 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800910 }
911
Romain Guy8a73c512009-11-09 19:19:59 -0800912 @SuppressWarnings({"UnusedDeclaration"})
913 public void previousScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700914 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800915 mWorkspace.scrollLeft();
916 }
Romain Guy8a73c512009-11-09 19:19:59 -0800917 }
918
919 @SuppressWarnings({"UnusedDeclaration"})
920 public void nextScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700921 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800922 mWorkspace.scrollRight();
923 }
Romain Guy8a73c512009-11-09 19:19:59 -0800924 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400925
926 @SuppressWarnings({"UnusedDeclaration"})
927 public void launchHotSeat(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700928 if (mState == State.APPS_CUSTOMIZE) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400929
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400930 int index = -1;
931 if (v.getId() == R.id.hotseat_left) {
932 index = 0;
933 } else if (v.getId() == R.id.hotseat_right) {
934 index = 1;
935 }
936
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400937 // reload these every tap; you never know when they might change
938 loadHotseats();
939 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400940 startActivitySafely(
941 mHotseats[index],
942 "hotseat"
943 );
944 }
945 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700946
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 /**
948 * Creates a view representing a shortcut.
949 *
950 * @param info The data structure describing the shortcut.
951 *
952 * @return A View inflated from R.layout.application.
953 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800954 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700956 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 }
958
959 /**
960 * Creates a view representing a shortcut inflated from the specified resource.
961 *
962 * @param layoutResId The id of the XML layout used to create the shortcut.
963 * @param parent The group the shortcut belongs to.
964 * @param info The data structure describing the shortcut.
965 *
966 * @return A View inflated from layoutResId.
967 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800968 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800969 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
970 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 return favorite;
973 }
974
975 /**
976 * Add an application shortcut to the workspace.
977 *
978 * @param data The intent describing the application.
979 * @param cellInfo The position on screen where to create the shortcut.
980 */
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800981 void completeAddApplication(Intent data, int screen,
Michael Jurka0280c3b2010-09-17 15:00:07 -0700982 int intersectCellX, int intersectCellY) {
983 final int[] cellXY = mTmpAddItemCellCoordinates;
984 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
985
986 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1, intersectCellX, intersectCellY)) {
987 showOutOfSpaceMessage();
988 return;
989 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800991 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800992
Romain Guy73b979d2009-06-09 12:57:21 -0700993 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800994 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800996 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700997 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
998 isWorkspaceLocked(), mAddIntersectCellX, mAddIntersectCellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800999 } else {
1000 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 }
1002 }
Romain Guycbb89e42009-06-08 15:52:54 -07001003
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 /**
1005 * Add a shortcut to the workspace.
1006 *
1007 * @param data The intent describing the shortcut.
1008 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 */
Michael Jurka0280c3b2010-09-17 15:00:07 -07001010 private void completeAddShortcut(Intent data, int screen,
1011 int intersectCellX, int intersectCellY) {
1012 final int[] cellXY = mTmpAddItemCellCoordinates;
1013 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001014
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001015 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001016 boolean foundCellSpan = false;
1017 if (touchXY != null) {
1018 // when dragging and dropping, just find the closest free spot
1019 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1020 int[] result = screenLayout.findNearestVacantArea(
1021 touchXY[0], touchXY[1], 1, 1, cellXY);
1022 foundCellSpan = (result != null);
1023 } else {
1024 foundCellSpan = layout.findCellForSpanThatIntersects(
1025 cellXY, 1, 1, intersectCellX, intersectCellY);
1026 }
1027
1028 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001029 showOutOfSpaceMessage();
1030 return;
1031 }
1032
1033 final ShortcutInfo info = mModel.addShortcut(
1034 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035
1036 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001038 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039 }
1040 }
1041
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001043 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001045 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001046 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 */
Winson Chungf7640c82011-02-28 13:47:29 -08001048 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001049 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001050
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001051 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001052 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1053 int[] spanXY = layout.rectToCell(appWidgetInfo.minWidth, appWidgetInfo.minHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001054
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001055 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001056 // We have saved the position to which the widget was dragged-- this really only matters
1057 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001058 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001059
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001060 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001061 boolean foundCellSpan = false;
1062 if (touchXY != null) {
1063 // when dragging and dropping, just find the closest free spot
1064 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1065 int[] result = screenLayout.findNearestVacantArea(
1066 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001067 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001068 } else {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08001069 // if we long pressed on an empty cell to bring up a menu,
1070 // make sure we intersect the empty cell
1071 // if mAddIntersectCellX/Y are -1 (e.g. we used menu -> add) then
1072 // findCellForSpanThatIntersects will just ignore them
1073 foundCellSpan = layout.findCellForSpanThatIntersects(cellXY, spanXY[0], spanXY[1],
1074 mAddIntersectCellX, mAddIntersectCellY);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001075 }
1076
Michael Jurka0280c3b2010-09-17 15:00:07 -07001077 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001078 if (appWidgetId != -1) {
1079 // Deleting an app widget ID is a void call but writes to disk before returning
1080 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001081 new Thread("deleteAppWidgetId") {
1082 public void run() {
1083 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1084 }
1085 }.start();
1086 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001087 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001088 return;
1089 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091 // Build Launcher-specific widget info and save to database
1092 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001093 launcherInfo.spanX = spanXY[0];
1094 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 LauncherModel.addItemToDatabase(this, launcherInfo,
1097 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001098 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001099
1100 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001101 mDesktopItems.add(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001104 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001105
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001106 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001108
Michael Jurka0280c3b2010-09-17 15:00:07 -07001109 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001110 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001111
1112 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 }
1114 }
Romain Guycbb89e42009-06-08 15:52:54 -07001115
Adam Cohended9f8d2010-11-03 13:25:16 -07001116 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1117 @Override
1118 public void onReceive(Context context, Intent intent) {
1119 final String action = intent.getAction();
1120 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1121 mUserPresent = false;
1122 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001123
1124 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001125 if (mAppsCustomizeContent != null) {
1126 mAppsCustomizeContent.reset();
1127 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001128 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1129 mUserPresent = true;
1130 updateRunning();
1131 }
1132 }
1133 };
1134
1135 @Override
1136 public void onAttachedToWindow() {
1137 super.onAttachedToWindow();
1138
1139 // Listen for broadcasts related to user-presence
1140 final IntentFilter filter = new IntentFilter();
1141 filter.addAction(Intent.ACTION_SCREEN_OFF);
1142 filter.addAction(Intent.ACTION_USER_PRESENT);
1143 registerReceiver(mReceiver, filter);
1144
Adam Cohend113e0c2010-11-11 10:48:05 -08001145 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001146 mVisible = true;
1147 }
1148
1149 @Override
1150 public void onDetachedFromWindow() {
1151 super.onDetachedFromWindow();
1152 mVisible = false;
1153
Adam Cohend113e0c2010-11-11 10:48:05 -08001154 if (mAttached) {
1155 unregisterReceiver(mReceiver);
1156 mAttached = false;
1157 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001158 updateRunning();
1159 }
1160
1161 public void onWindowVisibilityChanged(int visibility) {
1162 mVisible = visibility == View.VISIBLE;
1163 updateRunning();
1164 }
1165
1166 private void sendAdvanceMessage(long delay) {
1167 mHandler.removeMessages(ADVANCE_MSG);
1168 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1169 mHandler.sendMessageDelayed(msg, delay);
1170 mAutoAdvanceSentTime = System.currentTimeMillis();
1171 }
1172
1173 private void updateRunning() {
1174 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1175 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1176 mAutoAdvanceRunning = autoAdvanceRunning;
1177 if (autoAdvanceRunning) {
1178 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1179 sendAdvanceMessage(delay);
1180 } else {
1181 if (!mWidgetsToAdvance.isEmpty()) {
1182 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1183 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1184 }
1185 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001186 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001187 }
1188 }
1189 }
1190
1191 private final Handler mHandler = new Handler() {
1192 @Override
1193 public void handleMessage(Message msg) {
1194 if (msg.what == ADVANCE_MSG) {
1195 int i = 0;
1196 for (View key: mWidgetsToAdvance.keySet()) {
1197 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1198 final int delay = mAdvanceStagger * i;
1199 if (v instanceof Advanceable) {
1200 postDelayed(new Runnable() {
1201 public void run() {
1202 ((Advanceable) v).advance();
1203 }
1204 }, delay);
1205 }
1206 i++;
1207 }
1208 sendAdvanceMessage(mAdvanceInterval);
1209 }
1210 }
1211 };
1212
1213 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
1214 if (appWidgetInfo.autoAdvanceViewId == -1) return;
1215 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1216 if (v instanceof Advanceable) {
1217 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001218 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001219 updateRunning();
1220 }
1221 }
1222
1223 void removeWidgetToAutoAdvance(View hostView) {
1224 if (mWidgetsToAdvance.containsKey(hostView)) {
1225 mWidgetsToAdvance.remove(hostView);
1226 updateRunning();
1227 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001228 }
1229
Joe Onorato9c1289c2009-08-17 11:03:03 -04001230 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
1231 mDesktopItems.remove(launcherInfo);
Adam Cohended9f8d2010-11-03 13:25:16 -07001232 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001233 launcherInfo.hostView = null;
1234 }
1235
Adam Cohended9f8d2010-11-03 13:25:16 -07001236 void showOutOfSpaceMessage() {
1237 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1238 }
1239
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001240 public LauncherAppWidgetHost getAppWidgetHost() {
1241 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
Romain Guycbb89e42009-06-08 15:52:54 -07001243
Winson Chunga9abd0e2010-10-27 17:18:37 -07001244 public LauncherModel getModel() {
1245 return mModel;
1246 }
1247
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001248 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001249 getWindow().closeAllPanels();
1250
1251 try {
1252 dismissDialog(DIALOG_CREATE_SHORTCUT);
1253 // Unlock the workspace if the dialog was showing
1254 } catch (Exception e) {
1255 // An exception is thrown if the dialog is not visible, which is fine
1256 }
1257
1258 try {
1259 dismissDialog(DIALOG_RENAME_FOLDER);
1260 // Unlock the workspace if the dialog was showing
1261 } catch (Exception e) {
1262 // An exception is thrown if the dialog is not visible, which is fine
1263 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001264
1265 // Whatever we were doing is hereby canceled.
1266 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001267 }
1268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 @Override
1270 protected void onNewIntent(Intent intent) {
1271 super.onNewIntent(intent);
1272
1273 // Close the menu
1274 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001275 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001276 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001277
Adam Cohen95bb8002011-07-03 23:40:28 -07001278 closeFolder();
1279
Joe Onorato14f122b2009-11-19 14:06:36 -08001280 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1281 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001282
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001283 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001284 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001285 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001286 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001287 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001288 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001289 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001290
Joe Onorato3a8820b2009-11-10 15:06:42 -08001291 final View v = getWindow().peekDecorView();
1292 if (v != null && v.getWindowToken() != null) {
1293 InputMethodManager imm = (InputMethodManager)getSystemService(
1294 INPUT_METHOD_SERVICE);
1295 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001297
Michael Jurka35aa14d2011-07-07 17:01:08 -07001298 // Reset AllApps to its initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001299 if (mAppsCustomizeContent != null) {
1300 mAppsCustomizeContent.reset();
1301 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001302 }
1303 }
1304
1305 @Override
1306 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1307 // Do not call super here
1308 mSavedInstanceState = savedInstanceState;
1309 }
1310
1311 @Override
1312 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001313 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001314 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315
Michael Jurka883f55b2010-10-21 15:47:14 -07001316 outState.putInt(RUNTIME_STATE, mState.ordinal());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317
Michael Jurka0280c3b2010-09-17 15:00:07 -07001318 if (mAddScreen > -1 && mWaitingForResult) {
1319 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
1320 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddIntersectCellX);
1321 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddIntersectCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 }
1323
1324 if (mFolderInfo != null && mWaitingForResult) {
1325 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1326 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1327 }
Michael Jurka946ad472010-07-09 18:05:18 -07001328
Winson Chung785d2eb2011-04-14 16:08:02 -07001329 // Save the current AppsCustomize tab
1330 if (mAppsCustomizeTabHost != null) {
1331 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1332 if (currentTabTag != null) {
1333 outState.putString("apps_customize_currentTab", currentTabTag);
1334 }
1335 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 }
1337
1338 @Override
1339 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001341
Winson Chungcd2b0142011-06-08 16:02:26 -07001342 // Stop callbacks from LauncherModel
1343 LauncherApplication app = ((LauncherApplication) getApplication());
1344 mModel.stopLoader();
1345 app.setLauncher(null);
1346
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001348 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001350 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001352 mAppWidgetHost = null;
1353
1354 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355
1356 TextKeyListener.getInstance().release();
1357
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358
Joe Onorato9c1289c2009-08-17 11:03:03 -04001359 unbindDesktopItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001360
1361 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001362 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001363
1364 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1365 mWorkspace.removeAllViews();
1366 mWorkspace = null;
1367 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001368
1369 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 }
1371
Adam Cohena9cf38f2011-05-02 15:36:58 -07001372 public DragController getDragController() {
1373 return mDragController;
1374 }
1375
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 @Override
1377 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001378 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 super.startActivityForResult(intent, requestCode);
1380 }
1381
1382 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001383 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001385
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001386 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001387
Karl Rosaen138a0412009-04-23 19:00:21 -07001388 if (initialQuery == null) {
1389 // Use any text typed in the launcher as the initial query
1390 initialQuery = getTypedText();
1391 clearTypedText();
1392 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001393 if (appSearchData == null) {
1394 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001395 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 }
Romain Guycbb89e42009-06-08 15:52:54 -07001397
Karl Rosaen138a0412009-04-23 19:00:21 -07001398 final SearchManager searchManager =
1399 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001400 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001401 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 }
1403
1404 @Override
1405 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001406 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001407 return false;
1408 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409
1410 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001411
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1413 .setIcon(android.R.drawable.ic_menu_add)
1414 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001415 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1416 .setIcon(android.R.drawable.ic_menu_manage)
1417 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001418 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 .setIcon(android.R.drawable.ic_menu_gallery)
1420 .setAlphabeticShortcut('W');
1421 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1422 .setIcon(android.R.drawable.ic_search_category_default)
1423 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1424 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1425 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1426 .setAlphabeticShortcut('N');
1427
1428 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001429 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1430 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431
1432 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1433 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1434 .setIntent(settings);
1435
1436 return true;
1437 }
1438
1439 @Override
1440 public boolean onPrepareOptionsMenu(Menu menu) {
1441 super.onPrepareOptionsMenu(menu);
1442
Winson Chung785d2eb2011-04-14 16:08:02 -07001443 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1444 // related code?
1445 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446
1447 return true;
1448 }
1449
1450 @Override
1451 public boolean onOptionsItemSelected(MenuItem item) {
1452 switch (item.getItemId()) {
1453 case MENU_ADD:
1454 addItems();
1455 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001456 case MENU_MANAGE_APPS:
1457 manageApps();
1458 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459 case MENU_WALLPAPER_SETTINGS:
1460 startWallpaper();
1461 return true;
1462 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001463 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 return true;
1465 case MENU_NOTIFICATIONS:
1466 showNotifications();
1467 return true;
1468 }
1469
1470 return super.onOptionsItemSelected(item);
1471 }
Romain Guycbb89e42009-06-08 15:52:54 -07001472
Karl Rosaen138a0412009-04-23 19:00:21 -07001473 /**
1474 * Indicates that we want global search for this activity by setting the globalSearch
1475 * argument for {@link #startSearch} to true.
1476 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001478 @Override
1479 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001480 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001481 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001482 }
1483
Joe Onorato9c1289c2009-08-17 11:03:03 -04001484 public boolean isWorkspaceLocked() {
1485 return mWorkspaceLoading || mWaitingForResult;
1486 }
1487
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001489 showWorkspace(true);
1490 showAddDialog(-1, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 }
1492
Michael Jurka0280c3b2010-09-17 15:00:07 -07001493 private void resetAddInfo() {
1494 mAddScreen = -1;
1495 mAddIntersectCellX = -1;
1496 mAddIntersectCellY = -1;
1497 mAddDropPosition = null;
1498 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001499
Winson Chung55cef262010-10-28 14:14:18 -07001500 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] position) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001501 resetAddInfo();
1502 mAddScreen = screen;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001503 mAddDropPosition = position;
1504
Michael Jurkaaf442092010-06-10 17:01:57 -07001505 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Winson Chung55cef262010-10-28 14:14:18 -07001506 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1507 addAppWidgetImpl(appWidgetId, info);
Michael Jurkaaf442092010-06-10 17:01:57 -07001508 }
1509
Winson Chung7ad01412010-09-27 11:33:03 -07001510 private void manageApps() {
1511 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1512 }
1513
Michael Jurkaaf442092010-06-10 17:01:57 -07001514 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001515 // TODO: catch bad widget exception when sent
1516 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001517 // TODO: Is this log message meaningful?
1518 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001519 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001520 }
1521
Winson Chung55cef262010-10-28 14:14:18 -07001522 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001523 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524
Bjorn Bringert7984c942009-12-09 15:38:25 +00001525 if (appWidget.configure != null) {
1526 // Launch over to configure widget, if needed
1527 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1528 intent.setComponent(appWidget.configure);
1529 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001530 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001531 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1532 intent.putExtra(
1533 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1534 info.mimeType);
1535
1536 final String mimeType = info.mimeType;
1537 final ClipData clipData = (ClipData) info.configurationData;
1538 final ClipDescription clipDesc = clipData.getDescription();
1539 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1540 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001541 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001542 final CharSequence stringData = item.getText();
1543 final Uri uriData = item.getUri();
1544 final Intent intentData = item.getIntent();
1545 final String key =
1546 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1547 if (uriData != null) {
1548 intent.putExtra(key, uriData);
1549 } else if (intentData != null) {
1550 intent.putExtra(key, intentData);
1551 } else if (stringData != null) {
1552 intent.putExtra(key, stringData);
1553 }
1554 break;
1555 }
1556 }
1557 }
Winson Chung55cef262010-10-28 14:14:18 -07001558 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001559
Romain Guy8e633c52010-03-04 12:51:36 -08001560 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001562 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001563 completeAddAppWidget(appWidgetId, mAddScreen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001564
1565 // Exit spring loaded mode if necessary after adding the widget
1566 exitSpringLoadedDragModeDelayed(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 }
1568 }
Romain Guycbb89e42009-06-08 15:52:54 -07001569
Michael Jurka0280c3b2010-09-17 15:00:07 -07001570 void processShortcutFromDrop(ComponentName componentName, int screen, int[] position) {
1571 resetAddInfo();
1572 mAddScreen = screen;
1573 mAddDropPosition = position;
1574
1575 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1576 createShortcutIntent.setComponent(componentName);
1577 processShortcut(createShortcutIntent);
1578 }
1579
Joe Onoratodeb98af2010-02-19 14:59:39 -08001580 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001581 // Handle case where user selected "Applications"
1582 String applicationName = getResources().getString(R.string.group_applications);
1583 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001584
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001585 if (applicationName != null && applicationName.equals(shortcutName)) {
1586 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1587 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001588
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001589 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1590 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001591 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001592 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001593 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001594 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001595 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 }
1597
Winson Chung24ab2f12010-09-16 14:10:47 -07001598 void processWallpaper(Intent intent) {
1599 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1600 }
1601
Adam Cohend0445262011-07-04 23:53:22 -07001602 FolderIcon addFolder(final int screen, int intersectCellX, int intersectCellY) {
1603 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 folderInfo.title = getText(R.string.folder_name);
1605
Michael Jurka0280c3b2010-09-17 15:00:07 -07001606 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
1607 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohend0445262011-07-04 23:53:22 -07001608 if (!layout.findCellForSpanThatIntersects(cellXY, 1, 1,
1609 intersectCellX, intersectCellY)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001610 showOutOfSpaceMessage();
Adam Cohendf035382011-04-11 17:22:04 -07001611 return null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001612 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613
1614 // Update the model
Adam Cohend0445262011-07-04 23:53:22 -07001615 LauncherModel.addItemToDatabase(Launcher.this, folderInfo,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001616 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001617 screen, cellXY[0], cellXY[1], false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001618 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619
1620 // Create the view
1621 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001622 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1623 folderInfo, mIconCache);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001624 mWorkspace.addInScreen(newFolder, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001625 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 }
Romain Guycbb89e42009-06-08 15:52:54 -07001627
Joe Onorato9c1289c2009-08-17 11:03:03 -04001628 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001629 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001630 }
1631
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 private void showNotifications() {
1633 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1634 if (statusBar != null) {
1635 statusBar.expand();
1636 }
1637 }
1638
1639 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001640 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001642 Intent chooser = Intent.createChooser(pickWallpaper,
1643 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001644 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1645 // Removed in Eclair MR1
1646// WallpaperManager wm = (WallpaperManager)
1647// getSystemService(Context.WALLPAPER_SERVICE);
1648// WallpaperInfo wi = wm.getWallpaperInfo();
1649// if (wi != null && wi.getSettingsActivity() != null) {
1650// LabeledIntent li = new LabeledIntent(getPackageName(),
1651// R.string.configure_wallpaper, 0);
1652// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1653// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1654// }
Mike Clerona0618e42009-10-22 13:55:21 -07001655 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 }
1657
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001658 /**
1659 * Registers various content observers. The current implementation registers
1660 * only a favorites observer to keep track of the favorites applications.
1661 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001662 private void registerContentObservers() {
1663 ContentResolver resolver = getContentResolver();
1664 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1665 true, mWidgetObserver);
1666 }
1667
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 @Override
1669 public boolean dispatchKeyEvent(KeyEvent event) {
1670 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1671 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001673 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001674 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001675 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001676 dumpState();
1677 return true;
1678 }
1679 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001680 }
1681 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1682 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001683 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 return true;
1685 }
1686 }
1687
1688 return super.dispatchKeyEvent(event);
1689 }
1690
Joe Onorato88ec0992009-11-19 13:16:06 -08001691 @Override
1692 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001693 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001694 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001695 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001696 Folder openFolder = mWorkspace.getOpenFolder();
1697 if (openFolder.isEditingName()) {
1698 openFolder.dismissEditingName();
1699 } else {
1700 closeFolder();
1701 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001702 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001703 mWorkspace.exitWidgetResizeMode();
1704
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001705 // Back button is a no-op here, but give at least some feedback for the button press
1706 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001707 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001708 }
1709
Adam Cohen2801caf2011-05-13 20:57:39 -07001710 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 Folder folder = mWorkspace.getOpenFolder();
1712 if (folder != null) {
1713 closeFolder(folder);
1714 }
1715 }
1716
1717 void closeFolder(Folder folder) {
1718 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001719
Michael Jurka8c920dd2011-01-20 14:16:56 -08001720 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001721 if (parent != null) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001722 CellLayout cl = (CellLayout) mWorkspace.getChildAt(folder.mInfo.screen);
Adam Cohen2801caf2011-05-13 20:57:39 -07001723 FolderIcon fi = (FolderIcon) cl.getChildAt(folder.mInfo.cellX, folder.mInfo.cellY);
1724 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001725 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001726 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001727 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728 }
1729
1730 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001731 * Re-listen when widgets are reset.
1732 */
1733 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001734 if (mAppWidgetHost != null) {
1735 mAppWidgetHost.startListening();
1736 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001737 }
1738
1739 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001740 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1741 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04001743 private void unbindDesktopItems() {
1744 for (ItemInfo item: mDesktopItems) {
1745 item.unbind();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001747 mDesktopItems.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001749
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 /**
1751 * Launches the intent referred by the clicked shortcut.
1752 *
1753 * @param v The view representing the clicked shortcut.
1754 */
1755 public void onClick(View v) {
1756 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001757 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001759 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001760 int[] pos = new int[2];
1761 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001762 intent.setSourceBounds(new Rect(pos[0], pos[1],
1763 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001764 boolean success = startActivitySafely(intent, tag);
1765
1766 if (success && v instanceof BubbleTextView) {
1767 mWaitingForResume = (BubbleTextView) v;
1768 mWaitingForResume.setStayPressed(true);
1769 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001771 if (v instanceof FolderIcon) {
1772 FolderIcon fi = (FolderIcon) v;
1773 handleFolderClick(fi);
1774 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001775 } else if (v == mAllAppsButton) {
1776 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001777 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001778 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001779 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001780 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 }
1782 }
1783
Michael Jurka0e260592010-06-30 17:07:39 -07001784 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001785 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001786 // clicking anywhere on the workspace causes the customization drawer to slide down
1787 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001788 return false;
1789 }
1790
Michael Jurkaaf442092010-06-10 17:01:57 -07001791 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001792 * Event handler for the search button
1793 *
1794 * @param v The view that was clicked.
1795 */
1796 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001797 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001798 // Use a custom animation for launching search
1799 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001800 }
1801
1802 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001803 * Event handler for the voice button
1804 *
1805 * @param v The view that was clicked.
1806 */
1807 public void onClickVoiceButton(View v) {
1808 startVoiceSearch();
1809 }
1810
1811 private void startVoiceSearch() {
1812 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1813 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1814 startActivity(intent);
1815 }
1816
1817 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001818 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001819 * enters home screen customization mode.
1820 *
1821 * @param v The view that was clicked.
1822 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001823 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001824 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001825 }
1826
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001827 /**
1828 * Event handler for the "grid" button that appears on the home screen, which
1829 * enters all apps mode.
1830 *
1831 * @param v The view that was clicked.
1832 */
1833 public void onClickAllAppsButton(View v) {
1834 showAllApps(true);
1835 }
1836
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001837 public void onClickAppMarketButton(View v) {
1838 if (mAppMarketIntent != null) {
1839 startActivitySafely(mAppMarketIntent, "app market");
1840 }
1841 }
1842
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001843 void startApplicationDetailsActivity(ComponentName componentName) {
1844 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001845 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1846 Uri.fromParts("package", packageName, null));
1847 startActivity(intent);
1848 }
1849
Patrick Dubroy5539af72010-09-07 15:22:01 -07001850 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1851 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1852 // System applications cannot be installed. For now, show a toast explaining that.
1853 // We may give them the option of disabling apps this way.
1854 int messageId = R.string.uninstall_system_app_text;
1855 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1856 } else {
1857 String packageName = appInfo.componentName.getPackageName();
1858 String className = appInfo.componentName.getClassName();
1859 Intent intent = new Intent(
1860 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1861 startActivity(intent);
1862 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001863 }
1864
Michael Jurkaddd62e92011-02-16 17:49:14 -08001865 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1867 try {
1868 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001869 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 } catch (ActivityNotFoundException e) {
1871 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001872 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 } catch (SecurityException e) {
1874 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001875 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001877 "or use the exported attribute for this activity. "
1878 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001879 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001880 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001882
Romain Guy8e633c52010-03-04 12:51:36 -08001883 void startActivityForResultSafely(Intent intent, int requestCode) {
1884 try {
1885 startActivityForResult(intent, requestCode);
1886 } catch (ActivityNotFoundException e) {
1887 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1888 } catch (SecurityException e) {
1889 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1890 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1891 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1892 "or use the exported attribute for this activity.", e);
1893 }
1894 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895
Adam Cohena9cf38f2011-05-02 15:36:58 -07001896 private void handleFolderClick(FolderIcon folderIcon) {
1897 final FolderInfo info = folderIcon.mInfo;
1898 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 // Close any open folder
1900 closeFolder();
1901 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001902 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 } else {
1904 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001905 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 int folderScreen;
1907 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001908 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 // .. and close it
1910 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001911 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 // Close any folder open on the current screen
1913 closeFolder();
1914 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001915 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 }
1917 }
1918 }
1919 }
1920
Adam Cohen2801caf2011-05-13 20:57:39 -07001921 private void growAndFadeOutFolderIcon(FolderIcon fi) {
1922 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1923 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1924 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1925
1926 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1927 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1928 oa.start();
1929 }
1930
1931 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
1932 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1933 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1934 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1935
1936 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1937 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1938 oa.start();
1939 }
1940
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001942 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 * is animated relative to the specified View. If the View is null, no animation
1944 * is played.
1945 *
1946 * @param folderInfo The FolderInfo describing the folder to open.
1947 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001948 public void openFolder(FolderIcon folderIcon) {
1949 Folder folder = folderIcon.mFolder;
1950 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001951
Adam Cohen2801caf2011-05-13 20:57:39 -07001952 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001953 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954
Adam Cohen8e776a62011-06-28 18:10:06 -07001955 mDragLayer.addView(folder);
1956 mDragController.addDropTarget((DropTarget) folder);
1957
Adam Cohena9cf38f2011-05-02 15:36:58 -07001958 folder.animateOpen();
1959 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 }
1961
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001963 if (mState != State.WORKSPACE) {
1964 return false;
1965 }
1966
Romain Guy1fbc1c82009-11-09 20:43:08 -08001967 switch (v.getId()) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001968 case R.id.all_apps_button:
Winson Chungf0ea4d32011-06-06 14:27:16 -07001969 if (mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001970 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1971 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
1972 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08001973 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001974 return true;
1975 }
1976
Joe Onorato9c1289c2009-08-17 11:03:03 -04001977 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 return false;
1979 }
1980
1981 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001982 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 }
1984
Michael Jurka0280c3b2010-09-17 15:00:07 -07001985 resetAddInfo();
1986 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07001988 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989 return true;
1990 }
1991
Michael Jurka0280c3b2010-09-17 15:00:07 -07001992 final View itemUnderLongClick = longClickCellInfo.cell;
1993
Winson Chung304dcde2011-01-07 11:17:23 -08001994 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001995 if (itemUnderLongClick == null) {
1996 // User long pressed on empty space
1997 mWorkspace.setAllowLongPress(false);
1998 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1999 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07002000 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002002 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002004 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2005 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002006 mAddIntersectCellX = longClickCellInfo.cellX;
2007 mAddIntersectCellY = longClickCellInfo.cellY;
2008 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
2010 }
2011 }
2012 return true;
2013 }
2014
Romain Guye6b8e2f2009-11-10 11:56:55 -08002015 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002016 private void dismissPreview(final View v) {
2017 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002018 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002019 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2020 public void onDismiss() {
2021 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2022 int count = group.getChildCount();
2023 for (int i = 0; i < count; i++) {
2024 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2025 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002026 ArrayList<Bitmap> bitmaps =
2027 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002028 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2029
2030 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002031 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002032 window.setOnDismissListener(null);
2033 }
2034 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002035 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002036 }
2037 v.setTag(null);
2038 }
2039
Romain Guyf8e6a802009-12-07 17:48:02 -08002040 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002041 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002042 }
2043
Romain Guya6abce82009-11-10 02:54:41 -08002044 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002045 final Resources resources = getResources();
2046 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002047
Romain Guya6abce82009-11-10 02:54:41 -08002048 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002049
Romain Guy9d31e9f2009-11-11 18:54:28 -08002050 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002051
Romain Guyf8e6a802009-12-07 17:48:02 -08002052 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002053 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002054 int extraW = (int) ((r.left + r.right) * max);
2055 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002056
2057 int aW = cell.getWidth() - extraW;
2058 float w = aW / max;
2059
2060 int width = cell.getWidth();
2061 int height = cell.getHeight();
Winson Chung4b825dcd2011-06-19 12:41:22 -07002062 int x = cell.getPaddingLeft();
2063 int y = cell.getPaddingTop();
2064 width -= (x + cell.getPaddingRight());
2065 height -= (y + cell.getPaddingBottom());
Romain Guya6abce82009-11-10 02:54:41 -08002066
2067 float scale = w / width;
2068
2069 int count = end - start;
2070
2071 final float sWidth = width * scale;
2072 float sHeight = height * scale;
2073
Romain Guye6b8e2f2009-11-10 11:56:55 -08002074 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002075
Romain Guye6b8e2f2009-11-10 11:56:55 -08002076 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2077 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002078
2079 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002080 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002081 cell = (CellLayout) workspace.getChildAt(i);
2082
Romain Guyf8e6a802009-12-07 17:48:02 -08002083 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002084 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002085
2086 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002087 c.scale(scale, scale);
Winson Chung4b825dcd2011-06-19 12:41:22 -07002088 c.translate(-cell.getPaddingLeft(), -cell.getPaddingTop());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002089 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002090
Romain Guy9d31e9f2009-11-11 18:54:28 -08002091 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002092 image.setImageBitmap(bitmap);
2093 image.setTag(i);
2094 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002095 image.setOnFocusChangeListener(handler);
2096 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002097 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002098
2099 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002100 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2101
Winson Chungaafa03c2010-06-11 17:34:16 -07002102 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002103 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002104
2105 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002106 p.setContentView(preview);
2107 p.setWidth((int) (sWidth * count + extraW));
2108 p.setHeight((int) (sHeight + extraH));
2109 p.setAnimationStyle(R.style.AnimationPreview);
2110 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002111 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002112 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002113 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002114
Romain Guye6b8e2f2009-11-10 11:56:55 -08002115 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2116 public void onDismiss() {
2117 dismissPreview(anchor);
2118 }
2119 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002120
2121 anchor.setTag(p);
2122 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002123 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002124 }
2125
Romain Guy9d31e9f2009-11-11 18:54:28 -08002126 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002127 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002128
Romain Guye6b8e2f2009-11-10 11:56:55 -08002129 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002130 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002131 }
2132
2133 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002134 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002135 v.post(this);
2136 }
2137
2138 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002139 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002140 }
2141
2142 public void onFocusChange(View v, boolean hasFocus) {
2143 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002144 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002145 }
Romain Guya6abce82009-11-10 02:54:41 -08002146 }
2147 }
2148
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002149 Workspace getWorkspace() {
2150 return mWorkspace;
2151 }
2152
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153 @Override
2154 protected Dialog onCreateDialog(int id) {
2155 switch (id) {
2156 case DIALOG_CREATE_SHORTCUT:
2157 return new CreateShortcut().createDialog();
2158 case DIALOG_RENAME_FOLDER:
2159 return new RenameFolder().createDialog();
2160 }
2161
2162 return super.onCreateDialog(id);
2163 }
2164
2165 @Override
2166 protected void onPrepareDialog(int id, Dialog dialog) {
2167 switch (id) {
2168 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 break;
2170 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002171 if (mFolderInfo != null) {
2172 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2173 final CharSequence text = mFolderInfo.title;
2174 input.setText(text);
2175 input.setSelection(0, text.length());
2176 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 break;
2178 }
2179 }
2180
2181 void showRenameDialog(FolderInfo info) {
2182 mFolderInfo = info;
2183 mWaitingForResult = true;
2184 showDialog(DIALOG_RENAME_FOLDER);
2185 }
2186
Michael Jurka0280c3b2010-09-17 15:00:07 -07002187 private void showAddDialog(int intersectX, int intersectY) {
2188 resetAddInfo();
2189 mAddIntersectCellX = intersectX;
2190 mAddIntersectCellY = intersectY;
2191 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002192 mWaitingForResult = true;
2193 showDialog(DIALOG_CREATE_SHORTCUT);
2194 }
2195
Joe Onoratodeb98af2010-02-19 14:59:39 -08002196 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002197 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002198 Bundle bundle = new Bundle();
2199
2200 ArrayList<String> shortcutNames = new ArrayList<String>();
2201 shortcutNames.add(getString(R.string.group_applications));
2202 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2203
2204 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2205 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2206 R.drawable.ic_launcher_application));
2207 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2208
2209 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2210 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002211 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002212 pickIntent.putExtras(bundle);
2213
Joe Onoratodeb98af2010-02-19 14:59:39 -08002214 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002215 }
2216
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002217 private class RenameFolder {
2218 private EditText mInput;
2219
2220 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2222 mInput = (EditText) layout.findViewById(R.id.folder_name);
2223
2224 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2225 builder.setIcon(0);
2226 builder.setTitle(getString(R.string.rename_folder_title));
2227 builder.setCancelable(true);
2228 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2229 public void onCancel(DialogInterface dialog) {
2230 cleanup();
2231 }
2232 });
2233 builder.setNegativeButton(getString(R.string.cancel_action),
2234 new Dialog.OnClickListener() {
2235 public void onClick(DialogInterface dialog, int which) {
2236 cleanup();
2237 }
2238 }
2239 );
2240 builder.setPositiveButton(getString(R.string.rename_action),
2241 new Dialog.OnClickListener() {
2242 public void onClick(DialogInterface dialog, int which) {
2243 changeFolderName();
2244 }
2245 }
2246 );
2247 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002248
2249 final AlertDialog dialog = builder.create();
2250 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2251 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002252 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002253 mInput.requestFocus();
2254 InputMethodManager inputManager = (InputMethodManager)
2255 getSystemService(Context.INPUT_METHOD_SERVICE);
2256 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002257 }
2258 });
2259
2260 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002261 }
2262
2263 private void changeFolderName() {
2264 final String name = mInput.getText().toString();
2265 if (!TextUtils.isEmpty(name)) {
2266 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002267 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002268 mFolderInfo.title = name;
2269 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2270
Joe Onorato9c1289c2009-08-17 11:03:03 -04002271 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002272 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002273 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002274 } else {
2275 final FolderIcon folderIcon = (FolderIcon)
2276 mWorkspace.getViewForTag(mFolderInfo);
2277 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002278 // TODO: At some point we'll probably want some version of setting
2279 // the text for a folder icon.
2280 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002281 getWorkspace().requestLayout();
2282 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002283 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002284 mWorkspaceLoading = true;
2285 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 }
2287 }
2288 }
2289 cleanup();
2290 }
2291
2292 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002293 dismissDialog(DIALOG_RENAME_FOLDER);
2294 mWaitingForResult = false;
2295 mFolderInfo = null;
2296 }
2297 }
2298
Daniel Sandler843e8602010-06-07 14:59:01 -04002299 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2300 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002301 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002302 }
2303
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002304 // AllAppsView.Watcher
2305 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002306 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002307 mWorkspace.setVisibility(View.GONE);
2308 }
2309 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002310
2311 /**
2312 * Helper method for the cameraZoomIn/cameraZoomOut animations
2313 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002314 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002315 * @param scaleFactor The scale factor used for the zoom
2316 */
2317 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2318 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002319
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002320 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002321 // Set pivotY so that at the starting zoom factor, the view is partially
2322 // visible. Modifying initialHeightFactor changes how much of the view is
2323 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002324 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002325 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002326 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002327 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002328 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002329 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002330 }
2331 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002332
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002333 /**
2334 * Zoom the camera out from the workspace to reveal 'toView'.
2335 * Assumes that the view to show is anchored at either the very top or very bottom
2336 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002337 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002338 */
Winson Chungc07918d2011-07-01 15:35:26 -07002339 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002340 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002341
Winson Chungf0ea4d32011-06-06 14:27:16 -07002342 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2343 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2344 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2345 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002346
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002347 setPivotsForZoom(toView, toState, scale);
2348
Winson Chung20f71112011-04-06 14:22:04 -07002349
Winson Chungf0ea4d32011-06-06 14:27:16 -07002350 if (springLoaded) {
2351 if (toState == State.APPS_CUSTOMIZE) {
2352 // Shrink workspaces away if going back to AppsCustomize from spring loaded mode
2353 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
2354 } else {
2355 // Shrink workspaces to bottom if going back to AllApps from spring loaded mode
2356 mWorkspace.shrink(ShrinkState.BOTTOM_VISIBLE, animated);
Winson Chung20f71112011-04-06 14:22:04 -07002357 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002358 } else {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002359 // Shrink workspaces away if going to AllApps/AppsCustomize from workspace
2360 mWorkspace.shrink(ShrinkState.BOTTOM_HIDDEN, animated);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002361 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002362
2363 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002364 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002365 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002366 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2367 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002368 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002369 toView.setFastScaleX(a * scale + b * 1f);
2370 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002371 }
2372 });
Adam Cohen61033d32010-11-15 18:29:44 -08002373
Winson Chungf0ea4d32011-06-06 14:27:16 -07002374 toView.setVisibility(View.VISIBLE);
2375 toView.setFastAlpha(0f);
2376 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2377 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2378 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2379 public void onAnimationUpdate(float a, float b) {
2380 // don't need to invalidate because we do so above
2381 toView.setFastAlpha(a * 0f + b * 1f);
2382 }
2383 });
2384 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002385
Michael Jurkaabded662011-03-04 12:06:57 -08002386 if (toView instanceof LauncherTransitionable) {
2387 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002388 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002389 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002390 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002391 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002392 // Prepare the position
2393 toView.setTranslationX(0.0f);
2394 toView.setTranslationY(0.0f);
2395 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002396 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002397
Winson Chungc07918d2011-07-01 15:35:26 -07002398 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2399 // Hide the workspace scrollbar
2400 mWorkspace.hideScrollingIndicator(true);
2401 mWorkspace.hideScrollIndicatorTrack();
2402 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002403 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002404 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002405 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002406 // If we don't set the final scale values here, if this animation is cancelled
2407 // it will have the wrong scale value and subsequent cameraPan animations will
2408 // not fix that
2409 toView.setScaleX(1.0f);
2410 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002411 if (toView instanceof LauncherTransitionable) {
2412 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002413 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002414 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002415 });
2416
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002417 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002418 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002419
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002420 if (mStateAnimation != null) mStateAnimation.cancel();
2421 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002422 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002423 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002424 } else {
2425 toView.setTranslationX(0.0f);
2426 toView.setTranslationY(0.0f);
2427 toView.setScaleX(1.0f);
2428 toView.setScaleY(1.0f);
2429 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002430 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002431 if (toView instanceof LauncherTransitionable) {
2432 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2433 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002434
Winson Chungc07918d2011-07-01 15:35:26 -07002435 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2436 // Hide the workspace scrollbar
2437 mWorkspace.hideScrollingIndicator(true);
2438 mWorkspace.hideScrollIndicatorTrack();
2439 }
Michael Jurkaabded662011-03-04 12:06:57 -08002440 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002441 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002442 }
2443
2444 /**
2445 * Zoom the camera back into the workspace, hiding 'fromView'.
2446 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002447 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002448 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002449 */
Winson Chungc07918d2011-07-01 15:35:26 -07002450 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002451 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002452
Winson Chungf0ea4d32011-06-06 14:27:16 -07002453 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2454 final float scaleFactor = (float)
2455 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2456 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002457
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002458 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002459
Michael Jurkad3ef3062010-11-23 16:23:58 -08002460 if (!springLoaded) {
2461 mWorkspace.unshrink(animated);
2462 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002463 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002464 if (mStateAnimation != null) mStateAnimation.cancel();
2465 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002466
Michael Jurka742574b2011-02-02 23:51:01 -08002467 final float oldScaleX = fromView.getScaleX();
2468 final float oldScaleY = fromView.getScaleY();
2469
2470 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2471 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002472 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2473 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002474 ((View)fromView.getParent()).fastInvalidate();
2475 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2476 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2477 }
2478 });
Michael Jurkaabded662011-03-04 12:06:57 -08002479 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002480 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002481 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002482 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2483 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002484 // don't need to invalidate because we do so above
2485 fromView.setFastAlpha(a * 1f + b * 0f);
2486 }
2487 });
Michael Jurkaabded662011-03-04 12:06:57 -08002488 if (fromView instanceof LauncherTransitionable) {
2489 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002490 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002491 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002492 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002493 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002494 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002495 if (fromView instanceof LauncherTransitionable) {
2496 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Winson Chung3ac74c52011-06-30 17:39:37 -07002497
Winson Chungc07918d2011-07-01 15:35:26 -07002498 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2499 // Show the workspace scrollbar
2500 mWorkspace.showScrollIndicatorTrack();
2501 mWorkspace.flashScrollingIndicator();
2502 }
Michael Jurka2763be32011-02-24 11:19:57 -08002503 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002504 }
2505 });
2506
Winson Chungf0ea4d32011-06-06 14:27:16 -07002507 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002508 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002509 } else {
2510 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002511 if (fromView instanceof LauncherTransitionable) {
2512 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2513 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002514
Winson Chungc07918d2011-07-01 15:35:26 -07002515 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2516 // Show the workspace scrollbar
2517 mWorkspace.showScrollIndicatorTrack();
2518 mWorkspace.flashScrollingIndicator();
2519 }
Michael Jurkaabded662011-03-04 12:06:57 -08002520 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002521 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002522 }
2523
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002524 void showWorkspace(boolean animated) {
2525 showWorkspace(animated, null);
2526 }
2527
2528 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002529 if (layout != null) {
2530 // always animated, but that's ok since we never specify a layout and
2531 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002532 mWorkspace.unshrink(layout);
2533 } else {
2534 mWorkspace.unshrink(animated);
2535 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002536 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002537 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002538 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002539
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002540 // Change the state *after* we've called all the transition code
2541 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002542
Winson Chung5fb63472011-02-02 17:03:37 -08002543 // Resume the auto-advance of widgets
2544 mUserPresent = true;
2545 updateRunning();
2546
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002547 // send an accessibility event to announce the context change
2548 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002549 }
2550
Michael Jurkad3ef3062010-11-23 16:23:58 -08002551 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002552 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002553 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002554 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002555 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002556 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002557 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002558 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002559 void exitSpringLoadedDragModeDelayed(boolean extendedDelay) {
2560 mWorkspace.postDelayed(new Runnable() {
2561 @Override
2562 public void run() {
2563 exitSpringLoadedDragMode();
2564 }
2565 }, (extendedDelay ?
2566 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2567 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2568 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002569 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002570 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002571 mWorkspace.exitSpringLoadedDragMode(Workspace.ShrinkState.BOTTOM_VISIBLE);
Winson Chungb26f3d62011-06-02 10:49:29 -07002572 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2573 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002574 }
2575 // Otherwise, we are not in spring loaded mode, so don't do anything.
2576 }
2577
2578 /**
2579 * Shows the dock/hotseat area.
2580 */
2581 void showDock(boolean animated) {
2582 if (!LauncherApplication.isScreenLarge()) {
2583 if (animated) {
2584 int duration = mSearchDeleteBar.getTransitionInDuration();
2585 mButtonCluster.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002586 } else {
2587 mButtonCluster.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002588 }
2589 }
2590 }
2591
2592 /**
2593 * Hides the dock/hotseat area.
2594 */
2595 void hideDock(boolean animated) {
2596 if (!LauncherApplication.isScreenLarge()) {
2597 if (animated) {
2598 int duration = mSearchDeleteBar.getTransitionOutDuration();
2599 mButtonCluster.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002600 } else {
2601 mButtonCluster.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002602 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002603 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002604 }
2605
Winson Chung785d2eb2011-04-14 16:08:02 -07002606 void showAllApps(boolean animated) {
2607 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002608
Winson Chungf0ea4d32011-06-06 14:27:16 -07002609 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2610 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002611
Winson Chungf0ea4d32011-06-06 14:27:16 -07002612 // Hide the search bar and dock
2613 mSearchDeleteBar.hideSearchBar(animated);
2614 hideDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002615
Winson Chungf0ea4d32011-06-06 14:27:16 -07002616 // Change the state *after* we've called all the transition code
2617 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002618
2619 // Pause the auto-advance of widgets until we are out of AllApps
2620 mUserPresent = false;
2621 updateRunning();
2622
2623 // Send an accessibility event to announce the context change
2624 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2625 }
2626
Joe Onoratob2061212009-11-24 16:13:54 -05002627 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002628 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002629 * - Home from workspace
2630 * - from center screen
2631 * - from other screens
2632 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002633 * - from center screen
2634 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002635 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002636 * - from center screen
2637 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002638 * - Launch app from workspace and quit
2639 * - with back
2640 * - with home
2641 * - Launch app from all apps and quit
2642 * - with back
2643 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002644 * - Go to a screen that's not the default, then all
2645 * apps, and launch and app, and go back
2646 * - with back
2647 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002648 * - On workspace, long press power and go back
2649 * - with back
2650 * - with home
2651 * - On all apps, long press power and go back
2652 * - with back
2653 * - with home
2654 * - On workspace, power off
2655 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002656 * - Launch an app and turn off the screen while in that app
2657 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002658 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002659 * - From all apps
2660 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002661 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2662 * - From all apps
2663 * - From the center workspace
2664 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002665 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002666 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002667 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2668 mWorkspace.setVisibility(View.VISIBLE);
2669 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002670
Winson Chungf0ea4d32011-06-06 14:27:16 -07002671 // Show the search bar and dock
2672 mSearchDeleteBar.showSearchBar(animated);
2673 showDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002674
Winson Chungf0ea4d32011-06-06 14:27:16 -07002675 // Set focus to the AppsCustomize button
2676 findViewById(R.id.all_apps_button).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002677 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002678 }
2679
Joe Onorato7c312c12009-08-13 21:36:53 -07002680 void lockAllApps() {
2681 // TODO
2682 }
2683
2684 void unlockAllApps() {
2685 // TODO
2686 }
2687
Patrick Dubroy5f445422011-02-18 14:35:21 -08002688 /**
2689 * Add an item from all apps or customize onto the given workspace screen.
2690 * If layout is null, add to the current screen.
2691 */
2692 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002693 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2694 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002695 } else {
2696 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002697 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002698 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002699
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002700 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002701 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002702 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002703 // Look for the toolbar icon specified in the activity meta-data
2704 Bundle metaData = packageManager.getActivityInfo(
2705 activityName, PackageManager.GET_META_DATA).metaData;
2706 if (metaData != null) {
2707 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2708 if (iconResId != 0) {
2709 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002710 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002711 }
2712 }
2713 } catch (NameNotFoundException e) {
2714 // Do nothing
2715 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002716 return null;
2717 }
2718
2719 // if successful in getting icon, return it; otherwise, set button to use default drawable
2720 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2721 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2722 TextView button = (TextView) findViewById(buttonId);
2723 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2724
2725 // If we were unable to find the icon via the meta-data, use a generic one
2726 if (toolbarIcon == null) {
2727 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2728 return null;
2729 } else {
2730 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2731 return toolbarIcon.getConstantState();
2732 }
2733 }
2734
2735 // if successful in getting icon, return it; otherwise, set button to use default drawable
2736 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2737 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2738 ImageView button = (ImageView) findViewById(buttonId);
2739 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2740
Michael Jurka0423dcf2010-10-05 14:56:18 -07002741 // If we were unable to find the icon via the meta-data, use a generic one
2742 if (toolbarIcon == null) {
2743 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002744 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002745 } else {
2746 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002747 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002748 }
2749 }
2750
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002751 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2752 TextView button = (TextView) findViewById(buttonId);
2753 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2754 }
2755
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002756 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002757 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002758 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002759 }
2760
Michael Jurka0423dcf2010-10-05 14:56:18 -07002761 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002762 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2763 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002764
Winson Chung1cad91e2011-05-25 17:41:01 -07002765 final SearchManager searchManager =
2766 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2767 ComponentName activityName = searchManager.getGlobalSearchActivity();
2768 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002769 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002770 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002771 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002772 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002773 } else {
2774 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002775 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002776 }
2777 }
2778
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002779 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002780 updateButtonWithDrawable(R.id.search_button, d);
2781 }
2782
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002783 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002784 final View searchDivider = findViewById(R.id.search_divider);
2785 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002786
Winson Chung1cad91e2011-05-25 17:41:01 -07002787 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2788 ComponentName activityName = intent.resolveActivity(getPackageManager());
2789 if (activityName != null) {
2790 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002791 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2792 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002793 voiceButton.setVisibility(View.VISIBLE);
2794 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002795 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002796 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002797 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002798 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002799
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002800 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002801 updateButtonWithDrawable(R.id.voice_button, d);
2802 }
2803
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002804 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002805 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002806 */
2807 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002808 final View marketButton = findViewById(R.id.market_button);
2809 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2810 // Find the app market activity by resolving an intent.
2811 // (If multiple app markets are installed, it will return the ResolverActivity.)
2812 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002813 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002814 mAppMarketIntent = intent;
2815 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002816 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002817 marketButton.setVisibility(View.VISIBLE);
Winson Chungeb66b142011-06-16 13:14:22 -07002818
2819 // Remove the shop icon text in the Phone UI
2820 if (!LauncherApplication.isScreenLarge()) {
2821 ((TextView) marketButton).setText("");
2822 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002823 } else {
2824 // We should hide and disable the view so that we don't try and restore the visibility
2825 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2826 marketButton.setVisibility(View.GONE);
2827 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002828 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002829 }
2830
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002831 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002832 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002833 }
2834
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002835 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002836 * Displays the shortcut creation dialog and launches, if necessary, the
2837 * appropriate activity.
2838 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002839 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002840 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2841 DialogInterface.OnShowListener {
2842
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002843 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002844
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002846 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002847
Winson Chung55b65502011-05-26 12:03:43 -07002848 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2849 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002850 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002851 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002852
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002853 AlertDialog dialog = builder.create();
2854 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002855 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002856 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002857
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002858 return dialog;
2859 }
2860
2861 public void onCancel(DialogInterface dialog) {
2862 mWaitingForResult = false;
2863 cleanup();
2864 }
2865
Romain Guycbb89e42009-06-08 15:52:54 -07002866 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002867 mWaitingForResult = false;
2868 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002869 }
2870
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002871 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002872 try {
2873 dismissDialog(DIALOG_CREATE_SHORTCUT);
2874 } catch (Exception e) {
2875 // An exception is thrown if the dialog is not visible, which is fine
2876 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002877 }
2878
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002879 /**
2880 * Handle the action clicked in the "Add to home" dialog.
2881 */
2882 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002883 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002884
Winson Chung55b65502011-05-26 12:03:43 -07002885 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2886 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002887 case AddAdapter.ITEM_SHORTCUT: {
Winson Chungd2945262011-06-24 15:22:14 -07002888 if (mAppsCustomizeTabHost != null) {
2889 mAppsCustomizeTabHost.selectWidgetsTab();
2890 }
2891 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002892 break;
2893 }
Winson Chung55b65502011-05-26 12:03:43 -07002894 case AddAdapter.ITEM_APPLICATION: {
2895 if (mAppsCustomizeTabHost != null) {
2896 mAppsCustomizeTabHost.selectAppsTab();
2897 }
2898 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002899 break;
2900 }
Winson Chung55b65502011-05-26 12:03:43 -07002901 case AddAdapter.ITEM_APPWIDGET: {
2902 if (mAppsCustomizeTabHost != null) {
2903 mAppsCustomizeTabHost.selectWidgetsTab();
2904 }
2905 showAllApps(true);
2906 break;
2907 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002908 case AddAdapter.ITEM_WALLPAPER: {
2909 startWallpaper();
2910 break;
2911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002912 }
2913 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002914
2915 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002916 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002917 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002918 }
2919
2920 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002921 * Receives notifications when applications are added/removed.
2922 */
2923 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2924 @Override
2925 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002926 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002927 String reason = intent.getStringExtra("reason");
2928 if (!"homekey".equals(reason)) {
2929 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002930 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002931 animate = false;
2932 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002933 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002934 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002935 }
2936 }
2937
2938 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002939 * Receives notifications whenever the appwidgets are reset.
2940 */
2941 private class AppWidgetResetObserver extends ContentObserver {
2942 public AppWidgetResetObserver() {
2943 super(new Handler());
2944 }
2945
2946 @Override
2947 public void onChange(boolean selfChange) {
2948 onAppWidgetReset();
2949 }
2950 }
2951
2952 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002953 * If the activity is currently paused, signal that we need to re-run the loader
2954 * in onResume.
2955 *
2956 * This needs to be called from incoming places where resources might have been loaded
2957 * while we are paused. That is becaues the Configuration might be wrong
2958 * when we're not running, and if it comes back to what it was when we
2959 * were paused, we are not restarted.
2960 *
2961 * Implementation of the method from LauncherModel.Callbacks.
2962 *
2963 * @return true if we are currently paused. The caller might be able to
2964 * skip some work in that case since we will come back again.
2965 */
2966 public boolean setLoadOnResume() {
2967 if (mPaused) {
2968 Log.i(TAG, "setLoadOnResume");
2969 mOnResumeNeedsLoad = true;
2970 return true;
2971 } else {
2972 return false;
2973 }
2974 }
2975
2976 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002977 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002978 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002979 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002980 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002981 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002982 } else {
2983 return SCREEN_COUNT / 2;
2984 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002985 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002986
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002987
Joe Onorato9c1289c2009-08-17 11:03:03 -04002988 /**
2989 * Refreshes the shortcuts shown on the workspace.
2990 *
2991 * Implementation of the method from LauncherModel.Callbacks.
2992 */
2993 public void startBinding() {
2994 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002995
2996 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002997 int count = workspace.getChildCount();
2998 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002999 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003000 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3001 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003002 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003003
Joe Onorato9c1289c2009-08-17 11:03:03 -04003004 if (DEBUG_USER_INTERFACE) {
3005 android.widget.Button finishButton = new android.widget.Button(this);
3006 finishButton.setText("Finish");
3007 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3008
3009 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3010 public void onClick(View v) {
3011 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003012 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003013 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003014 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003015
3016 // This wasn't being called before which resulted in a leak of AppWidgetHostViews (through
3017 // mDesktopItems -> AppWidgetInfo -> hostView).
3018 unbindDesktopItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003019 }
3020
3021 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003022 * Bind the items start-end from the list.
3023 *
3024 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003025 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003026 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3027
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003028 setLoadOnResume();
3029
Joe Onorato9c1289c2009-08-17 11:03:03 -04003030 final Workspace workspace = mWorkspace;
3031
3032 for (int i=start; i<end; i++) {
3033 final ItemInfo item = shortcuts.get(i);
3034 mDesktopItems.add(item);
3035 switch (item.itemType) {
3036 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3037 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003038 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003039 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3040 false);
3041 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003042 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003043 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003044 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003045 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003046 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3047 false);
3048 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003049 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003050 }
3051
Joe Onorato9c1289c2009-08-17 11:03:03 -04003052 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003053 }
3054
Joe Onorato9c1289c2009-08-17 11:03:03 -04003055 /**
3056 * Implementation of the method from LauncherModel.Callbacks.
3057 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003058 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003059 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003060 sFolders.clear();
3061 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003062 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003063
3064 /**
3065 * Add the views for a widget to the workspace.
3066 *
3067 * Implementation of the method from LauncherModel.Callbacks.
3068 */
3069 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003070 setLoadOnResume();
3071
Daniel Sandler843e8602010-06-07 14:59:01 -04003072 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3073 if (DEBUG_WIDGETS) {
3074 Log.d(TAG, "bindAppWidget: " + item);
3075 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003076 final Workspace workspace = mWorkspace;
3077
3078 final int appWidgetId = item.appWidgetId;
3079 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003080 if (DEBUG_WIDGETS) {
3081 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3082 }
3083
Joe Onorato9c1289c2009-08-17 11:03:03 -04003084 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3085
Joe Onorato9c1289c2009-08-17 11:03:03 -04003086 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3087 item.hostView.setTag(item);
3088
3089 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3090 item.cellY, item.spanX, item.spanY, false);
3091
Adam Cohended9f8d2010-11-03 13:25:16 -07003092 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3093
Joe Onorato9c1289c2009-08-17 11:03:03 -04003094 workspace.requestLayout();
3095
3096 mDesktopItems.add(item);
Daniel Sandler843e8602010-06-07 14:59:01 -04003097
3098 if (DEBUG_WIDGETS) {
3099 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3100 + (SystemClock.uptimeMillis()-start) + "ms");
3101 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003102 }
3103
3104 /**
3105 * Callback saying that there aren't any more items to bind.
3106 *
3107 * Implementation of the method from LauncherModel.Callbacks.
3108 */
3109 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003110 setLoadOnResume();
3111
Joe Onorato9c1289c2009-08-17 11:03:03 -04003112 if (mSavedState != null) {
3113 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003114 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003115 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003116 mSavedState = null;
3117 }
3118
3119 if (mSavedInstanceState != null) {
3120 super.onRestoreInstanceState(mSavedInstanceState);
3121 mSavedInstanceState = null;
3122 }
3123
Joe Onorato9c1289c2009-08-17 11:03:03 -04003124 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003125
3126 // If we received the result of any pending adds while the loader was running (e.g. the
3127 // widget configuration forced an orientation change), process them now.
3128 for (int i = 0; i < sPendingAddList.size(); i++) {
3129 completeAdd(sPendingAddList.get(i));
3130 }
3131 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003132 }
3133
3134 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003135 * Updates the icons on the launcher that are affected by changes to the package list
3136 * on the device.
3137 */
3138 private void updateIconsAffectedByPackageManagerChanges() {
3139 updateAppMarketIcon();
3140 updateGlobalSearchIcon();
3141 updateVoiceSearchIcon();
3142 }
3143
3144 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003145 * Add the icons for all apps.
3146 *
3147 * Implementation of the method from LauncherModel.Callbacks.
3148 */
3149 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003150 if (mAppsCustomizeContent != null) {
3151 mAppsCustomizeContent.setApps(apps);
3152 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003153 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003154 }
3155
3156 /**
3157 * A package was installed.
3158 *
3159 * Implementation of the method from LauncherModel.Callbacks.
3160 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003161 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003162 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003163 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003164
Winson Chung785d2eb2011-04-14 16:08:02 -07003165 if (mAppsCustomizeContent != null) {
3166 mAppsCustomizeContent.addApps(apps);
3167 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003168 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003169 }
3170
3171 /**
3172 * A package was updated.
3173 *
3174 * Implementation of the method from LauncherModel.Callbacks.
3175 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003176 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003177 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003178 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003179 if (mWorkspace != null) {
3180 mWorkspace.updateShortcuts(apps);
3181 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003182
Winson Chung785d2eb2011-04-14 16:08:02 -07003183 if (mAppsCustomizeContent != null) {
3184 mAppsCustomizeContent.updateApps(apps);
3185 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003186 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003187 }
3188
3189 /**
3190 * A package was uninstalled.
3191 *
3192 * Implementation of the method from LauncherModel.Callbacks.
3193 */
Joe Onorato36115782010-06-17 13:28:48 -04003194 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003195 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003196 if (permanent) {
3197 mWorkspace.removeItems(apps);
3198 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003199
Winson Chung785d2eb2011-04-14 16:08:02 -07003200 if (mAppsCustomizeContent != null) {
3201 mAppsCustomizeContent.removeApps(apps);
3202 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003203 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003204 }
Joe Onoratobe386092009-11-17 17:32:16 -08003205
3206 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003207 * A number of packages were updated.
3208 */
3209 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003210
Winson Chung785d2eb2011-04-14 16:08:02 -07003211 if (mAppsCustomizeContent != null) {
3212 mAppsCustomizeContent.onPackagesUpdated();
3213 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003214 }
3215
Winson Chung400438b2011-01-16 17:53:48 -08003216 private int mapConfigurationOriActivityInfoOri(int configOri) {
3217 final Display d = getWindowManager().getDefaultDisplay();
3218 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3219 switch (d.getRotation()) {
3220 case Surface.ROTATION_0:
3221 case Surface.ROTATION_180:
3222 // We are currently in the same basic orientation as the natural orientation
3223 naturalOri = configOri;
3224 break;
3225 case Surface.ROTATION_90:
3226 case Surface.ROTATION_270:
3227 // We are currently in the other basic orientation to the natural orientation
3228 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3229 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3230 break;
3231 }
3232
3233 int[] oriMap = {
3234 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3235 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3236 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3237 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3238 };
3239 // Since the map starts at portrait, we need to offset if this device's natural orientation
3240 // is landscape.
3241 int indexOffset = 0;
3242 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3243 indexOffset = 1;
3244 }
3245 return oriMap[(d.getRotation() + indexOffset) % 4];
3246 }
3247 public void lockScreenOrientation() {
3248 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3249 .getConfiguration().orientation));
3250 }
3251 public void unlockScreenOrientation() {
3252 mHandler.postDelayed(new Runnable() {
3253 public void run() {
3254 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3255 }
3256 }, mRestoreScreenOrientationDelay);
3257 }
3258
Winson Chung80baf5a2010-08-09 16:03:15 -07003259 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003260 * Prints out out state for debugging.
3261 */
3262 public void dumpState() {
3263 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003264 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003265 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3266 Log.d(TAG, "mRestoring=" + mRestoring);
3267 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3268 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
3269 Log.d(TAG, "mDesktopItems.size=" + mDesktopItems.size());
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003270 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003271 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003272
Winson Chung785d2eb2011-04-14 16:08:02 -07003273 if (mAppsCustomizeContent != null) {
3274 mAppsCustomizeContent.dumpState();
3275 }
Joe Onoratobe386092009-11-17 17:32:16 -08003276 Log.d(TAG, "END launcher2 dump state");
3277 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003278}
Michael Jurka2763be32011-02-24 11:19:57 -08003279
Michael Jurkaabded662011-03-04 12:06:57 -08003280interface LauncherTransitionable {
3281 void onLauncherTransitionStart(Animator animation);
3282 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003283}