blob: 29f3f252baf61466e3eb9aa0f901a7bee1a268f1 [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;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.Dialog;
30import android.app.SearchManager;
31import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070036import android.content.ClipData;
37import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
41import android.content.DialogInterface;
42import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
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;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040053import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070054import android.os.AsyncTask;
Adam Cohen50370f32011-08-25 18:57:14 -070055import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020057import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080058import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070059import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040060import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080061import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070062import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080063import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
66import android.text.TextUtils;
67import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070068import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080069import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080070import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.view.KeyEvent;
72import android.view.LayoutInflater;
73import android.view.Menu;
74import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070075import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080076import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.view.View;
Winson Chung82f55532011-08-09 14:14:23 -070078import android.view.ViewGroup;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070079import android.view.WindowManager;
Adam Cohencff6af82011-09-13 14:51:53 -070080import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080081import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070082import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080083import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070085import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070087import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070088import android.widget.TextView;
89import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070090
Adam Cohendf2cc412011-04-27 16:56:57 -070091import com.android.common.Search;
92import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070093import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080094
Adam Cohenc0dcf592011-06-01 15:30:43 -070095import java.io.DataInputStream;
96import java.io.DataOutputStream;
97import java.io.FileNotFoundException;
98import java.io.IOException;
99import java.util.ArrayList;
100import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102/**
103 * Default launcher application.
104 */
Michael Jurka946ad472010-07-09 18:05:18 -0700105public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700106 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
107 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800108 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700109 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Joe Onorato9c1289c2009-08-17 11:03:03 -0400111 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400112 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700113
Winson Chung70d72102011-08-12 11:24:35 -0700114 private static final int MENU_GROUP_WALLPAPER = 1;
115 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
116 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700117 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
118 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
120 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700121 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 private static final int REQUEST_PICK_APPLICATION = 6;
123 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700124 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700125 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
127 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
128
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800129 static final int SCREEN_COUNT = 5;
130 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
Joe Onorato7c312c12009-08-13 21:36:53 -0700132 static final int DIALOG_CREATE_SHORTCUT = 1;
133 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Romain Guy98d01652009-06-30 16:21:04 -0700135 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 // Type: int
138 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700139 // Type: int
140 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700142 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
143 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
145 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700146 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700148 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 // Type: boolean
150 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
151 // Type: long
152 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
153
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700154 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
155
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700156 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700157 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700158 private State mState = State.WORKSPACE;
159 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700160
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700162 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
163 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung82f55532011-08-09 14:14:23 -0700164 private static final int DISMISS_CLING_DURATION = 300;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800167 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800169 private final BroadcastReceiver mCloseSystemDialogsReceiver
170 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800171 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private LayoutInflater mInflater;
174
Joe Onorato00acb122009-08-04 16:04:30 -0400175 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700177
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700178 private AppWidgetManager mAppWidgetManager;
179 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700180
Michael Jurkac9d95c52011-08-29 14:03:34 -0700181 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700182 private int[] mTmpAddItemCellCoordinates = new int[2];
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private FolderInfo mFolderInfo;
185
Winson Chung3d503fb2011-07-13 17:25:49 -0700186 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800187 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700188
Winson Chungf0ea4d32011-06-06 14:27:16 -0700189 private SearchDropTargetBar mSearchDeleteBar;
190 private AppsCustomizeTabHost mAppsCustomizeTabHost;
191 private AppsCustomizePagedView mAppsCustomizeContent;
192 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private Bundle mSavedState;
195
196 private SpannableStringBuilder mDefaultKeySsb = null;
197
Joe Onorato9c1289c2009-08-17 11:03:03 -0400198 private boolean mWorkspaceLoading = true;
199
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800200 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private boolean mRestoring;
202 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700203 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
205 private Bundle mSavedInstanceState;
206
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800208 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800209 private boolean mUserPresent = true;
210 private boolean mVisible = false;
211 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700213 private static LocaleConfiguration sLocaleConfiguration = null;
214
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700215 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700216
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700217 private Intent mAppMarketIntent = null;
218
Adam Cohended9f8d2010-11-03 13:25:16 -0700219 // Related to the auto-advancing of widgets
220 private final int ADVANCE_MSG = 1;
221 private final int mAdvanceInterval = 20000;
222 private final int mAdvanceStagger = 250;
223 private long mAutoAdvanceSentTime;
224 private long mAutoAdvanceTimeLeft = -1;
225 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
226 new HashMap<View, AppWidgetProviderInfo>();
227
Winson Chung400438b2011-01-16 17:53:48 -0800228 // Determines how long to wait after a rotation before restoring the screen orientation to
229 // match the sensor state.
230 private final int mRestoreScreenOrientationDelay = 500;
231
Michael Jurka4ef207b2010-11-29 17:05:45 -0800232 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700233 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
234 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
235 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800236
Adam Cohen2801caf2011-05-13 20:57:39 -0700237 private DragLayer mDragLayer;
238
Michael Jurkaddd62e92011-02-16 17:49:14 -0800239 private BubbleTextView mWaitingForResume;
240
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800241 private static ArrayList<PendingAddArguments> sPendingAddList
242 = new ArrayList<PendingAddArguments>();
243
244 private static class PendingAddArguments {
245 int requestCode;
246 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700247 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800248 int screen;
249 int cellX;
250 int cellY;
251 }
252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 @Override
254 protected void onCreate(Bundle savedInstanceState) {
255 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800256 LauncherApplication app = ((LauncherApplication)getApplication());
257 mModel = app.setLauncher(this);
258 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400259 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700261
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700262 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700263 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
264 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200267 android.os.Debug.startMethodTracing(
268 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 }
270
271 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 setContentView(R.layout.launcher);
273 setupViews();
Winson Chung82f55532011-08-09 14:14:23 -0700274 enableClingsIfNecessary();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800276 registerContentObservers();
277
Joe Onorato7c312c12009-08-13 21:36:53 -0700278 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700279
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 mSavedState = savedInstanceState;
281 restoreState(mSavedState);
282
Winson Chunga12a2502010-12-20 14:41:35 -0800283 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700284 if (mAppsCustomizeContent != null) {
285 mAppsCustomizeContent.onPackagesUpdated();
286 }
Winson Chunga12a2502010-12-20 14:41:35 -0800287
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 if (PROFILE_STARTUP) {
289 android.os.Debug.stopMethodTracing();
290 }
291
292 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 }
295
Michael Jurkac57b7a82011-08-09 22:02:20 -0700296 if (!mModel.isAllAppsLoaded()) {
297 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
298 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
299 }
300
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 // For handling default keys
302 mDefaultKeySsb = new SpannableStringBuilder();
303 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800304
305 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
306 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800307
308 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700309 int coi = getCurrentOrientationIndexForGlobalIcons();
310 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
311 sAppMarketIcon[coi] == null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700312 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100313 updateGlobalSearchIcon();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700314 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700315 if (sGlobalSearchIcon[coi] != null) {
316 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700317 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700318 if (sVoiceSearchIcon[coi] != null) {
319 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700320 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700321 if (sAppMarketIcon[coi] != null) {
322 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800323 }
Adam Cohen446e9402011-09-15 18:21:21 -0700324
325 // On large interfaces, we want the screen to auto-rotate based on the current orientation
326 if (LauncherApplication.isScreenLarge()) {
327 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 }
Romain Guycbb89e42009-06-08 15:52:54 -0700330
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700332 if (sLocaleConfiguration == null) {
333 new AsyncTask<Void, Void, LocaleConfiguration>() {
334 @Override
335 protected LocaleConfiguration doInBackground(Void... unused) {
336 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
337 readConfiguration(Launcher.this, localeConfiguration);
338 return localeConfiguration;
339 }
340
341 @Override
342 protected void onPostExecute(LocaleConfiguration result) {
343 sLocaleConfiguration = result;
344 checkForLocaleChange(); // recursive, but now with a locale configuration
345 }
346 }.execute();
347 return;
348 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700349
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 final Configuration configuration = getResources().getConfiguration();
351
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700352 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 final String locale = configuration.locale.toString();
354
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700355 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 final int mcc = configuration.mcc;
357
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700358 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 final int mnc = configuration.mnc;
360
Romain Guy84f296c2009-11-04 15:00:44 -0800361 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362
Romain Guy84f296c2009-11-04 15:00:44 -0800363 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700364 sLocaleConfiguration.locale = locale;
365 sLocaleConfiguration.mcc = mcc;
366 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700367
Joe Onorato0589f0f2010-02-08 13:44:00 -0800368 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700369
370 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
371 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700372 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700373 public void run() {
374 writeConfiguration(Launcher.this, localeConfiguration);
375 }
376 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700377 }
378 }
379
380 private static class LocaleConfiguration {
381 public String locale;
382 public int mcc = -1;
383 public int mnc = -1;
384 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700385
Romain Guy98d01652009-06-30 16:21:04 -0700386 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
387 DataInputStream in = null;
388 try {
389 in = new DataInputStream(context.openFileInput(PREFERENCES));
390 configuration.locale = in.readUTF();
391 configuration.mcc = in.readInt();
392 configuration.mnc = in.readInt();
393 } catch (FileNotFoundException e) {
394 // Ignore
395 } catch (IOException e) {
396 // Ignore
397 } finally {
398 if (in != null) {
399 try {
400 in.close();
401 } catch (IOException e) {
402 // Ignore
403 }
404 }
405 }
406 }
407
408 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
409 DataOutputStream out = null;
410 try {
411 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
412 out.writeUTF(configuration.locale);
413 out.writeInt(configuration.mcc);
414 out.writeInt(configuration.mnc);
415 out.flush();
416 } catch (FileNotFoundException e) {
417 // Ignore
418 } catch (IOException e) {
419 //noinspection ResultOfMethodCallIgnored
420 context.getFileStreamPath(PREFERENCES).delete();
421 } finally {
422 if (out != null) {
423 try {
424 out.close();
425 } catch (IOException e) {
426 // Ignore
427 }
428 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 }
430 }
431
Adam Cohen716b51e2011-06-30 12:09:54 -0700432 public DragLayer getDragLayer() {
433 return mDragLayer;
434 }
435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 static int getScreen() {
437 synchronized (sLock) {
438 return sScreen;
439 }
440 }
441
442 static void setScreen(int screen) {
443 synchronized (sLock) {
444 sScreen = screen;
445 }
446 }
447
Winson Chung557d6ed2011-07-08 15:34:52 -0700448 /**
449 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
450 * a configuration step, this allows the proper animations to run after other transitions.
451 */
452 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700453 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800454 switch (args.requestCode) {
455 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700456 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
457 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800458 break;
459 case REQUEST_PICK_SHORTCUT:
460 processShortcut(args.intent);
461 break;
462 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700463 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
464 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700465 result = true;
466 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800467 case REQUEST_PICK_APPWIDGET:
468 addAppWidgetFromPick(args.intent);
469 break;
470 case REQUEST_CREATE_APPWIDGET:
471 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700472 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700473 result = true;
474 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800475 case REQUEST_PICK_WALLPAPER:
476 // We just wanted the activity result here so we can clear mWaitingForResult
477 break;
478 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700479 // In any situation where we have a multi-step drop, we should reset the add info only after
480 // we complete the drop
481 resetAddInfo();
482 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800483 }
484
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800486 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700487 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700488 mWaitingForResult = false;
489
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 // The pattern used here is that a user PICKs a specific application,
491 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700492
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
494 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700495 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800496 final PendingAddArguments args = new PendingAddArguments();
497 args.requestCode = requestCode;
498 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700499 args.container = mPendingAddInfo.container;
500 args.screen = mPendingAddInfo.screen;
501 args.cellX = mPendingAddInfo.cellX;
502 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800503
504 // If the loader is still running, defer the add until it is done.
505 if (isWorkspaceLocked()) {
506 sPendingAddList.add(args);
507 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700508 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800509 }
Romain Guy18042c82009-11-06 11:44:55 -0800510 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700511 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
512 if (data != null) {
513 // Clean up the appWidgetId if we canceled
514 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
515 if (appWidgetId != -1) {
516 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
517 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 }
519 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700520
521 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700522 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800523 }
524
525 @Override
526 protected void onResume() {
527 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800528 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700529 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400530 mWorkspaceLoading = true;
531 mModel.startLoader(this, true);
532 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700533 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800535 if (mWaitingForResume != null) {
536 mWaitingForResume.setStayPressed(false);
537 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700538 // When we resume Launcher, a different Activity might be responsible for the app
539 // market intent, so refresh the icon
540 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 }
542
543 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700544 protected void onPause() {
545 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700546 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800547 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700548 }
Romain Guycbb89e42009-06-08 15:52:54 -0700549
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700550 @Override
551 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400552 // Flag the loader to stop early before switching
553 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700554 if (mAppsCustomizeContent != null) {
555 mAppsCustomizeContent.surrender();
556 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800557 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700558 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700559
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800560 // We can't hide the IME if it was forced open. So don't bother
561 /*
562 @Override
563 public void onWindowFocusChanged(boolean hasFocus) {
564 super.onWindowFocusChanged(hasFocus);
565
566 if (hasFocus) {
567 final InputMethodManager inputManager = (InputMethodManager)
568 getSystemService(Context.INPUT_METHOD_SERVICE);
569 WindowManager.LayoutParams lp = getWindow().getAttributes();
570 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
571 android.os.Handler()) {
572 protected void onReceiveResult(int resultCode, Bundle resultData) {
573 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
574 }
575 });
576 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
577 }
578 }
579 */
580
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 private boolean acceptFilter() {
582 final InputMethodManager inputManager = (InputMethodManager)
583 getSystemService(Context.INPUT_METHOD_SERVICE);
584 return !inputManager.isFullscreenMode();
585 }
586
587 @Override
588 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700589 final int uniChar = event.getUnicodeChar();
590 final boolean handled = super.onKeyDown(keyCode, event);
591 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
592 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
594 keyCode, event);
595 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700596 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700597 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700598 // showSearchDialog()
599 // If there are multiple keystrokes before the search dialog takes focus,
600 // onSearchRequested() will be called for every keystroke,
601 // but it is idempotent, so it's fine.
602 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 }
604 }
605
Joe Onorato8a9625e2010-01-28 15:55:35 -0800606 // Eat the long press event so the keyboard doesn't come up.
607 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
608 return true;
609 }
610
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 return handled;
612 }
613
Karl Rosaen138a0412009-04-23 19:00:21 -0700614 private String getTypedText() {
615 return mDefaultKeySsb.toString();
616 }
617
618 private void clearTypedText() {
619 mDefaultKeySsb.clear();
620 mDefaultKeySsb.clearSpans();
621 Selection.setSelection(mDefaultKeySsb, 0);
622 }
623
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700625 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
626 * State
627 */
628 private static State intToState(int stateOrdinal) {
629 State state = State.WORKSPACE;
630 final State[] stateValues = State.values();
631 for (int i = 0; i < stateValues.length; i++) {
632 if (stateValues[i].ordinal() == stateOrdinal) {
633 state = stateValues[i];
634 break;
635 }
636 }
637 return state;
638 }
639
640 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 * Restores the previous state, if it exists.
642 *
643 * @param savedState The previous state.
644 */
645 private void restoreState(Bundle savedState) {
646 if (savedState == null) {
647 return;
648 }
649
Michael Jurka883f55b2010-10-21 15:47:14 -0700650 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700651 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800652 showAllApps(false);
653 }
654
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
656 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700657 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 }
659
Winson Chung3d503fb2011-07-13 17:25:49 -0700660 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
661 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662
Winson Chung3d503fb2011-07-13 17:25:49 -0700663 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
664 mPendingAddInfo.container = pendingAddContainer;
665 mPendingAddInfo.screen = pendingAddScreen;
666 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
667 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 mRestoring = true;
669 }
670
671 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
672 if (renameFolder) {
673 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700674 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 mRestoring = true;
676 }
Winson Chunga12a2502010-12-20 14:41:35 -0800677
Winson Chung785d2eb2011-04-14 16:08:02 -0700678
679 // Restore the AppsCustomize tab
680 if (mAppsCustomizeTabHost != null) {
681 String curTab = savedState.getString("apps_customize_currentTab");
682 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700683 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700684 mAppsCustomizeContent.setContentType(
685 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
686 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700687 mAppsCustomizeContent.loadAssociatedPages(
688 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700689 }
690
Winson Chung5afbf7b2011-07-25 11:53:08 -0700691 int currentIndex = savedState.getInt("apps_customize_currentIndex");
692 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 }
695
696 /**
697 * Finds all the views we need and configure them properly.
698 */
699 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700700 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400701
Winson Chung4c98d922011-05-31 16:50:48 -0700702 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
703 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704
Winson Chung4c98d922011-05-31 16:50:48 -0700705 // Setup the drag layer
706 mDragLayer.setup(this, dragController);
707
Winson Chung3d503fb2011-07-13 17:25:49 -0700708 // Setup the hotseat
709 mHotseat = (Hotseat) findViewById(R.id.hotseat);
710 if (mHotseat != null) {
711 mHotseat.setup(this);
712 }
713
Winson Chung4c98d922011-05-31 16:50:48 -0700714 // Setup the workspace
715 mWorkspace.setHapticFeedbackEnabled(false);
716 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700717 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700718 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700719
Winson Chungf0ea4d32011-06-06 14:27:16 -0700720 // Get the search/delete bar
721 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700722
Winson Chungf0ea4d32011-06-06 14:27:16 -0700723 // Setup AppsCustomize
724 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
725 findViewById(R.id.apps_customize_pane);
726 mAppsCustomizeContent = (AppsCustomizePagedView)
727 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
728 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400729
Romain Guy1fbc1c82009-11-09 20:43:08 -0800730
Winson Chung4c98d922011-05-31 16:50:48 -0700731
Winson Chung3d503fb2011-07-13 17:25:49 -0700732 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700733 dragController.setDragScoller(mWorkspace);
734 dragController.setScrollView(mDragLayer);
735 dragController.setMoveTarget(mWorkspace);
736 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700737 if (mSearchDeleteBar != null) {
738 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800739 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 }
741
742 /**
743 * Creates a view representing a shortcut.
744 *
745 * @param info The data structure describing the shortcut.
746 *
747 * @return A View inflated from R.layout.application.
748 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800749 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700751 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753
754 /**
755 * Creates a view representing a shortcut inflated from the specified resource.
756 *
757 * @param layoutResId The id of the XML layout used to create the shortcut.
758 * @param parent The group the shortcut belongs to.
759 * @param info The data structure describing the shortcut.
760 *
761 * @return A View inflated from layoutResId.
762 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800763 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800764 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
765 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 return favorite;
768 }
769
770 /**
771 * Add an application shortcut to the workspace.
772 *
773 * @param data The intent describing the application.
774 * @param cellInfo The position on screen where to create the shortcut.
775 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700776 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700777 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700778 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700779
Adam Cohenfbba09b2011-07-18 21:43:05 -0700780 // First we check if we already know the exact location where we want to add this item.
781 if (cellX >= 0 && cellY >= 0) {
782 cellXY[0] = cellX;
783 cellXY[1] = cellY;
784 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700785 showOutOfSpaceMessage();
786 return;
787 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800789 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800790
Romain Guy73b979d2009-06-09 12:57:21 -0700791 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800792 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800794 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700795 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700796 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800797 } else {
798 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 }
800 }
Romain Guycbb89e42009-06-08 15:52:54 -0700801
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 /**
803 * Add a shortcut to the workspace.
804 *
805 * @param data The intent describing the shortcut.
806 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700808 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
809 int cellY) {
810 int[] cellXY = mTmpAddItemCellCoordinates;
811 int[] touchXY = mPendingAddInfo.dropPos;
812 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700813
Michael Jurkad3ef3062010-11-23 16:23:58 -0800814 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700815
Adam Cohen558baaf2011-08-15 15:22:57 -0700816 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
817 final View view = createShortcut(info);
818
Adam Cohenfbba09b2011-07-18 21:43:05 -0700819 // First we check if we already know the exact location where we want to add this item.
820 if (cellX >= 0 && cellY >= 0) {
821 cellXY[0] = cellX;
822 cellXY[1] = cellY;
823 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700824
825 // If appropriate, either create a folder or add to an existing folder
826 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
827 true, null,null)) {
828 return;
829 }
830 DragObject dragObject = new DragObject();
831 dragObject.dragInfo = info;
832 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
833 return;
834 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700835 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800836 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700837 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800838 foundCellSpan = (result != null);
839 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700840 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800841 }
842
843 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700844 showOutOfSpaceMessage();
845 return;
846 }
847
Adam Cohen558baaf2011-08-15 15:22:57 -0700848 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849
850 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700851 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
852 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 }
854 }
855
Adam Cohen50370f32011-08-25 18:57:14 -0700856 class Padding {
857 int left = 0;
858 int right = 0;
859 int top = 0;
860 int bottom = 0;
861 }
862
Adam Cohenf814aa02011-09-01 13:48:05 -0700863 Padding getPaddingForWidget(ComponentName component) {
Adam Cohen50370f32011-08-25 18:57:14 -0700864 PackageManager packageManager = getPackageManager();
865 Padding p = new Padding();
866 android.content.pm.ApplicationInfo appInfo;
867
868 try {
Adam Cohenf814aa02011-09-01 13:48:05 -0700869 appInfo = packageManager.getApplicationInfo(component.getPackageName(), 0);
Adam Cohen50370f32011-08-25 18:57:14 -0700870 } catch (Exception e) {
871 // if we can't find the package, return 0 padding
872 return p;
873 }
874
875 // TODO: This should be ICE_CREAM_SANDWICH, but since the unbundled apps
876 // may not have updated their targetSdkVersion yet, we've bumped it down for now.
877 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB_MR2) {
878 Resources r = getResources();
879 p.left = r.getDimensionPixelSize(R.dimen.app_widget_padding_left);
880 p.right = r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
881 p.top = r.getDimensionPixelSize(R.dimen.app_widget_padding_top);
882 p.bottom = r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
883 }
884
885 return p;
886 }
887
Adam Cohenf814aa02011-09-01 13:48:05 -0700888 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
889 if (spanXY == null) {
890 spanXY = new int[2];
891 }
892
893 Padding padding = getPaddingForWidget(component);
894 // We want to account for the extra amount of padding that we are adding to the widget
895 // to ensure that it gets the full amount of space that it has requested
896 int requiredWidth = minWidth + padding.left + padding.right;
897 int requiredHeight = minHeight + padding.top + padding.bottom;
898 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
899 }
900
901 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
902 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
903 }
904
Adam Cohencbf47e32011-09-16 17:32:37 -0700905 int[] getMinResizeSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
906 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
907 }
908
Adam Cohenf814aa02011-09-01 13:48:05 -0700909 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
910 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
911 }
912
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700914 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700916 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700917 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700919 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700920 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700921
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700922 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700923 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700924
Adam Cohenf814aa02011-09-01 13:48:05 -0700925 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700926
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700928 // We have saved the position to which the widget was dragged-- this really only matters
929 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700930 int[] cellXY = mTmpAddItemCellCoordinates;
931 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700932 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700933 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
934 cellXY[0] = mPendingAddInfo.cellX;
935 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700936 foundCellSpan = true;
937 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700938 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700939 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700940 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800941 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700942 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700943 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700944 }
945
Michael Jurka0280c3b2010-09-17 15:00:07 -0700946 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800947 if (appWidgetId != -1) {
948 // Deleting an app widget ID is a void call but writes to disk before returning
949 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800950 new Thread("deleteAppWidgetId") {
951 public void run() {
952 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
953 }
954 }.start();
955 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700956 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800957 return;
958 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800959
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700960 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -0700961 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700962 launcherInfo.spanX = spanXY[0];
963 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700964
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700966 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967
968 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700970 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700971
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700972 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700974
Winson Chung3d503fb2011-07-13 17:25:49 -0700975 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400976 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700977
978 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 }
980 }
Romain Guycbb89e42009-06-08 15:52:54 -0700981
Adam Cohended9f8d2010-11-03 13:25:16 -0700982 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
983 @Override
984 public void onReceive(Context context, Intent intent) {
985 final String action = intent.getAction();
986 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
987 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700988 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700989 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700990
Winson Chungc100e8e2011-08-09 16:02:43 -0700991 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -0700992 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -0700993 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
994 mAppsCustomizeTabHost.reset();
995 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700996 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700997 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
998 mUserPresent = true;
999 updateRunning();
1000 }
1001 }
1002 };
1003
1004 @Override
1005 public void onAttachedToWindow() {
1006 super.onAttachedToWindow();
1007
1008 // Listen for broadcasts related to user-presence
1009 final IntentFilter filter = new IntentFilter();
1010 filter.addAction(Intent.ACTION_SCREEN_OFF);
1011 filter.addAction(Intent.ACTION_USER_PRESENT);
1012 registerReceiver(mReceiver, filter);
1013
Adam Cohend113e0c2010-11-11 10:48:05 -08001014 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001015 mVisible = true;
1016 }
1017
1018 @Override
1019 public void onDetachedFromWindow() {
1020 super.onDetachedFromWindow();
1021 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001022 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001023
Adam Cohend113e0c2010-11-11 10:48:05 -08001024 if (mAttached) {
1025 unregisterReceiver(mReceiver);
1026 mAttached = false;
1027 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001028 updateRunning();
1029 }
1030
1031 public void onWindowVisibilityChanged(int visibility) {
1032 mVisible = visibility == View.VISIBLE;
1033 updateRunning();
1034 }
1035
1036 private void sendAdvanceMessage(long delay) {
1037 mHandler.removeMessages(ADVANCE_MSG);
1038 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1039 mHandler.sendMessageDelayed(msg, delay);
1040 mAutoAdvanceSentTime = System.currentTimeMillis();
1041 }
1042
1043 private void updateRunning() {
1044 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1045 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1046 mAutoAdvanceRunning = autoAdvanceRunning;
1047 if (autoAdvanceRunning) {
1048 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1049 sendAdvanceMessage(delay);
1050 } else {
1051 if (!mWidgetsToAdvance.isEmpty()) {
1052 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1053 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1054 }
1055 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001056 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001057 }
1058 }
1059 }
1060
1061 private final Handler mHandler = new Handler() {
1062 @Override
1063 public void handleMessage(Message msg) {
1064 if (msg.what == ADVANCE_MSG) {
1065 int i = 0;
1066 for (View key: mWidgetsToAdvance.keySet()) {
1067 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1068 final int delay = mAdvanceStagger * i;
1069 if (v instanceof Advanceable) {
1070 postDelayed(new Runnable() {
1071 public void run() {
1072 ((Advanceable) v).advance();
1073 }
1074 }, delay);
1075 }
1076 i++;
1077 }
1078 sendAdvanceMessage(mAdvanceInterval);
1079 }
1080 }
1081 };
1082
1083 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001084 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001085 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1086 if (v instanceof Advanceable) {
1087 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001088 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001089 updateRunning();
1090 }
1091 }
1092
1093 void removeWidgetToAutoAdvance(View hostView) {
1094 if (mWidgetsToAdvance.containsKey(hostView)) {
1095 mWidgetsToAdvance.remove(hostView);
1096 updateRunning();
1097 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001098 }
1099
Joe Onorato9c1289c2009-08-17 11:03:03 -04001100 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001101 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001102 launcherInfo.hostView = null;
1103 }
1104
Adam Cohended9f8d2010-11-03 13:25:16 -07001105 void showOutOfSpaceMessage() {
1106 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1107 }
1108
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001109 public LauncherAppWidgetHost getAppWidgetHost() {
1110 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
Romain Guycbb89e42009-06-08 15:52:54 -07001112
Winson Chunga9abd0e2010-10-27 17:18:37 -07001113 public LauncherModel getModel() {
1114 return mModel;
1115 }
1116
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001117 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001118 getWindow().closeAllPanels();
1119
Winson Chung87acb482011-08-23 17:28:05 -07001120 /**
1121 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001122 try {
1123 dismissDialog(DIALOG_CREATE_SHORTCUT);
1124 // Unlock the workspace if the dialog was showing
1125 } catch (Exception e) {
1126 // An exception is thrown if the dialog is not visible, which is fine
1127 }
1128
1129 try {
1130 dismissDialog(DIALOG_RENAME_FOLDER);
1131 // Unlock the workspace if the dialog was showing
1132 } catch (Exception e) {
1133 // An exception is thrown if the dialog is not visible, which is fine
1134 }
Winson Chung87acb482011-08-23 17:28:05 -07001135 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001136
1137 // Whatever we were doing is hereby canceled.
1138 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001139 }
1140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001141 @Override
1142 protected void onNewIntent(Intent intent) {
1143 super.onNewIntent(intent);
1144
1145 // Close the menu
1146 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001147 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001148 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001149
Adam Cohen95bb8002011-07-03 23:40:28 -07001150 closeFolder();
1151
Joe Onorato14f122b2009-11-19 14:06:36 -08001152 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1153 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001154
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001155 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001156 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001157 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001158 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001159 }
Winson Chungde1af762011-07-21 16:44:07 -07001160 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001161 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001162
Joe Onorato3a8820b2009-11-10 15:06:42 -08001163 final View v = getWindow().peekDecorView();
1164 if (v != null && v.getWindowToken() != null) {
1165 InputMethodManager imm = (InputMethodManager)getSystemService(
1166 INPUT_METHOD_SERVICE);
1167 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001169
Michael Jurka35aa14d2011-07-07 17:01:08 -07001170 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001171 if (mAppsCustomizeTabHost != null) {
1172 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 }
1175 }
1176
1177 @Override
1178 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1179 // Do not call super here
1180 mSavedInstanceState = savedInstanceState;
1181 }
1182
1183 @Override
1184 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001185 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001186 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187
Michael Jurka883f55b2010-10-21 15:47:14 -07001188 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001189 // We close any open folder since it will not be re-opened, and we need to make sure
1190 // this state is reflected.
1191 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192
Winson Chung3d503fb2011-07-13 17:25:49 -07001193 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1194 mWaitingForResult) {
1195 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1196 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1197 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1198 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001199 }
1200
1201 if (mFolderInfo != null && mWaitingForResult) {
1202 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1203 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1204 }
Michael Jurka946ad472010-07-09 18:05:18 -07001205
Winson Chung785d2eb2011-04-14 16:08:02 -07001206 // Save the current AppsCustomize tab
1207 if (mAppsCustomizeTabHost != null) {
1208 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1209 if (currentTabTag != null) {
1210 outState.putString("apps_customize_currentTab", currentTabTag);
1211 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001212 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1213 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001214 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 }
1216
1217 @Override
1218 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001220
Winson Chunge7a03942011-08-05 15:05:12 -07001221 // Remove all pending runnables
1222 mHandler.removeMessages(ADVANCE_MSG);
1223 mHandler.removeMessages(0);
1224
Winson Chungcd2b0142011-06-08 16:02:26 -07001225 // Stop callbacks from LauncherModel
1226 LauncherApplication app = ((LauncherApplication) getApplication());
1227 mModel.stopLoader();
1228 app.setLauncher(null);
1229
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001231 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001233 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001235 mAppWidgetHost = null;
1236
1237 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238
1239 TextKeyListener.getInstance().release();
1240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241
Winson Chung3d503fb2011-07-13 17:25:49 -07001242 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001243
1244 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001245 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001246
1247 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1248 mWorkspace.removeAllViews();
1249 mWorkspace = null;
1250 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001251
1252 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 }
1254
Adam Cohena9cf38f2011-05-02 15:36:58 -07001255 public DragController getDragController() {
1256 return mDragController;
1257 }
1258
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 @Override
1260 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001261 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 super.startActivityForResult(intent, requestCode);
1263 }
1264
Winson Chung70d72102011-08-12 11:24:35 -07001265 /**
1266 * Indicates that we want global search for this activity by setting the globalSearch
1267 * argument for {@link #startSearch} to true.
1268 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001270 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001272
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001273 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001274
Karl Rosaen138a0412009-04-23 19:00:21 -07001275 if (initialQuery == null) {
1276 // Use any text typed in the launcher as the initial query
1277 initialQuery = getTypedText();
1278 clearTypedText();
1279 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 if (appSearchData == null) {
1281 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001282 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
Romain Guycbb89e42009-06-08 15:52:54 -07001284
Karl Rosaen138a0412009-04-23 19:00:21 -07001285 final SearchManager searchManager =
1286 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001287 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001288 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 }
1290
Winson Chung70d72102011-08-12 11:24:35 -07001291 @Override
1292 public boolean onCreateOptionsMenu(Menu menu) {
1293 if (isWorkspaceLocked()) {
1294 return false;
1295 }
1296
1297 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001298
1299 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1300 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1301 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001302 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1303 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1304 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001305 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.help_url)));
1306 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1307 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1308
Winson Chung70d72102011-08-12 11:24:35 -07001309 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1310 .setIcon(android.R.drawable.ic_menu_gallery)
1311 .setAlphabeticShortcut('W');
1312 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1313 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001314 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001315 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001316 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1317 .setIcon(android.R.drawable.ic_menu_preferences)
1318 .setIntent(settings)
1319 .setAlphabeticShortcut('P');
Winson Chung70d72102011-08-12 11:24:35 -07001320 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1321 .setIcon(android.R.drawable.ic_menu_help)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001322 .setIntent(help)
Winson Chung70d72102011-08-12 11:24:35 -07001323 .setAlphabeticShortcut('H');
1324 return true;
1325 }
1326
1327 @Override
1328 public boolean onPrepareOptionsMenu(Menu menu) {
1329 super.onPrepareOptionsMenu(menu);
1330
1331 if (mAppsCustomizeTabHost.isTransitioning()) {
1332 return false;
1333 }
1334 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1335 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1336
1337 return true;
1338 }
1339
1340 @Override
1341 public boolean onOptionsItemSelected(MenuItem item) {
1342 switch (item.getItemId()) {
1343 case MENU_WALLPAPER_SETTINGS:
1344 startWallpaper();
1345 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001346 }
1347
1348 return super.onOptionsItemSelected(item);
1349 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001351 @Override
1352 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001353 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001354 // Use a custom animation for launching search
1355 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001356 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001357 }
1358
Joe Onorato9c1289c2009-08-17 11:03:03 -04001359 public boolean isWorkspaceLocked() {
1360 return mWorkspaceLoading || mWaitingForResult;
1361 }
1362
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001364 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001365 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 }
1367
Michael Jurka0280c3b2010-09-17 15:00:07 -07001368 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001369 mPendingAddInfo.container = ItemInfo.NO_ID;
1370 mPendingAddInfo.screen = -1;
1371 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1372 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1373 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001374 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001375
Michael Jurkaaf442092010-06-10 17:01:57 -07001376 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001377 // TODO: catch bad widget exception when sent
1378 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001379 // TODO: Is this log message meaningful?
1380 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001381 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001382 }
1383
Winson Chung55cef262010-10-28 14:14:18 -07001384 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001385 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386
Bjorn Bringert7984c942009-12-09 15:38:25 +00001387 if (appWidget.configure != null) {
1388 // Launch over to configure widget, if needed
1389 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1390 intent.setComponent(appWidget.configure);
1391 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001392 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001393 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1394 intent.putExtra(
1395 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1396 info.mimeType);
1397
1398 final String mimeType = info.mimeType;
1399 final ClipData clipData = (ClipData) info.configurationData;
1400 final ClipDescription clipDesc = clipData.getDescription();
1401 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1402 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001403 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001404 final CharSequence stringData = item.getText();
1405 final Uri uriData = item.getUri();
1406 final Intent intentData = item.getIntent();
1407 final String key =
1408 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1409 if (uriData != null) {
1410 intent.putExtra(key, uriData);
1411 } else if (intentData != null) {
1412 intent.putExtra(key, intentData);
1413 } else if (stringData != null) {
1414 intent.putExtra(key, stringData);
1415 }
1416 break;
1417 }
1418 }
1419 }
Winson Chung55cef262010-10-28 14:14:18 -07001420 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001421
Romain Guy8e633c52010-03-04 12:51:36 -08001422 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001424 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001425 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001426
1427 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001428 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 }
1430 }
Romain Guycbb89e42009-06-08 15:52:54 -07001431
Adam Cohenfbba09b2011-07-18 21:43:05 -07001432 /**
1433 * Process a shortcut drop.
1434 *
1435 * @param componentName The name of the component
1436 * @param screen The screen where it should be added
1437 * @param cell The cell it should be added to, optional
1438 * @param position The location on the screen where it was dropped, optional
1439 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001440 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1441 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001442 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001443 mPendingAddInfo.container = container;
1444 mPendingAddInfo.screen = screen;
1445 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001446
1447 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001448 mPendingAddInfo.cellX = cell[0];
1449 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001450 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001451
1452 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1453 createShortcutIntent.setComponent(componentName);
1454 processShortcut(createShortcutIntent);
1455 }
1456
Adam Cohenfbba09b2011-07-18 21:43:05 -07001457 /**
1458 * Process a widget drop.
1459 *
1460 * @param info The PendingAppWidgetInfo of the widget being added.
1461 * @param screen The screen where it should be added
1462 * @param cell The cell it should be added to, optional
1463 * @param position The location on the screen where it was dropped, optional
1464 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001465 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1466 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001467 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001468 mPendingAddInfo.container = info.container = container;
1469 mPendingAddInfo.screen = info.screen = screen;
1470 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001471 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001472 mPendingAddInfo.cellX = cell[0];
1473 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001474 }
1475
1476 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1477 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1478 addAppWidgetImpl(appWidgetId, info);
1479 }
1480
Joe Onoratodeb98af2010-02-19 14:59:39 -08001481 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001482 // Handle case where user selected "Applications"
1483 String applicationName = getResources().getString(R.string.group_applications);
1484 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001485
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001486 if (applicationName != null && applicationName.equals(shortcutName)) {
1487 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1488 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001489
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001490 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1491 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001492 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001493 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001494 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001495 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001496 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 }
1498
Winson Chung24ab2f12010-09-16 14:10:47 -07001499 void processWallpaper(Intent intent) {
1500 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1501 }
1502
Winson Chung3d503fb2011-07-13 17:25:49 -07001503 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1504 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001505 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 folderInfo.title = getText(R.string.folder_name);
1507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001509 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1510 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001511 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512
1513 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001514 FolderIcon newFolder =
1515 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1516 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1517 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001518 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 }
Romain Guycbb89e42009-06-08 15:52:54 -07001520
Joe Onorato9c1289c2009-08-17 11:03:03 -04001521 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001522 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001523 }
1524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 private void showNotifications() {
1526 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1527 if (statusBar != null) {
1528 statusBar.expand();
1529 }
1530 }
1531
1532 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001533 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001535 Intent chooser = Intent.createChooser(pickWallpaper,
1536 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001537 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1538 // Removed in Eclair MR1
1539// WallpaperManager wm = (WallpaperManager)
1540// getSystemService(Context.WALLPAPER_SERVICE);
1541// WallpaperInfo wi = wm.getWallpaperInfo();
1542// if (wi != null && wi.getSettingsActivity() != null) {
1543// LabeledIntent li = new LabeledIntent(getPackageName(),
1544// R.string.configure_wallpaper, 0);
1545// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1546// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1547// }
Mike Clerona0618e42009-10-22 13:55:21 -07001548 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 }
1550
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001551 /**
1552 * Registers various content observers. The current implementation registers
1553 * only a favorites observer to keep track of the favorites applications.
1554 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001555 private void registerContentObservers() {
1556 ContentResolver resolver = getContentResolver();
1557 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1558 true, mWidgetObserver);
1559 }
1560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 @Override
1562 public boolean dispatchKeyEvent(KeyEvent event) {
1563 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1564 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001566 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001567 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001568 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001569 dumpState();
1570 return true;
1571 }
1572 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001573 }
1574 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1575 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001576 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 return true;
1578 }
1579 }
1580
1581 return super.dispatchKeyEvent(event);
1582 }
1583
Joe Onorato88ec0992009-11-19 13:16:06 -08001584 @Override
1585 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001586 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001587 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001588 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001589 Folder openFolder = mWorkspace.getOpenFolder();
1590 if (openFolder.isEditingName()) {
1591 openFolder.dismissEditingName();
1592 } else {
1593 closeFolder();
1594 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001595 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001596 mWorkspace.exitWidgetResizeMode();
1597
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001598 // Back button is a no-op here, but give at least some feedback for the button press
1599 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001600 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001601 }
1602
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001603 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001604 * Re-listen when widgets are reset.
1605 */
1606 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001607 if (mAppWidgetHost != null) {
1608 mAppWidgetHost.startListening();
1609 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001610 }
1611
1612 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001613 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1614 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001616 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001617 if (mModel != null) {
1618 mModel.unbindWorkspaceItems();
1619 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001621
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 /**
1623 * Launches the intent referred by the clicked shortcut.
1624 *
1625 * @param v The view representing the clicked shortcut.
1626 */
1627 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001628 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1629 // view has detached (it's possible for this to happen if the view is removed mid touch).
1630 if (v.getWindowToken() == null) {
1631 return;
1632 }
1633
1634 if (mWorkspace.isSwitchingState()) {
1635 return;
1636 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001637
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001639 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001641 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001642 int[] pos = new int[2];
1643 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001644 intent.setSourceBounds(new Rect(pos[0], pos[1],
1645 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001646 boolean success = startActivitySafely(intent, tag);
1647
1648 if (success && v instanceof BubbleTextView) {
1649 mWaitingForResume = (BubbleTextView) v;
1650 mWaitingForResume.setStayPressed(true);
1651 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001653 if (v instanceof FolderIcon) {
1654 FolderIcon fi = (FolderIcon) v;
1655 handleFolderClick(fi);
1656 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001657 } else if (v == mAllAppsButton) {
1658 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001659 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001660 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001661 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 }
1664 }
1665
Michael Jurka0e260592010-06-30 17:07:39 -07001666 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001667 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001668 // clicking anywhere on the workspace causes the customization drawer to slide down
1669 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001670 return false;
1671 }
1672
Michael Jurkaaf442092010-06-10 17:01:57 -07001673 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001674 * Event handler for the search button
1675 *
1676 * @param v The view that was clicked.
1677 */
1678 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001679 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001680 }
1681
1682 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001683 * Event handler for the voice button
1684 *
1685 * @param v The view that was clicked.
1686 */
1687 public void onClickVoiceButton(View v) {
1688 startVoiceSearch();
1689 }
1690
1691 private void startVoiceSearch() {
1692 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001693 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001694 startActivity(intent);
1695 }
1696
1697 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001698 * Event handler for the "grid" button that appears on the home screen, which
1699 * enters all apps mode.
1700 *
1701 * @param v The view that was clicked.
1702 */
1703 public void onClickAllAppsButton(View v) {
1704 showAllApps(true);
1705 }
1706
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001707 public void onClickAppMarketButton(View v) {
1708 if (mAppMarketIntent != null) {
1709 startActivitySafely(mAppMarketIntent, "app market");
1710 }
1711 }
1712
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001713 void startApplicationDetailsActivity(ComponentName componentName) {
1714 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001715 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1716 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001717 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001718 startActivity(intent);
1719 }
1720
Patrick Dubroy5539af72010-09-07 15:22:01 -07001721 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1722 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1723 // System applications cannot be installed. For now, show a toast explaining that.
1724 // We may give them the option of disabling apps this way.
1725 int messageId = R.string.uninstall_system_app_text;
1726 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1727 } else {
1728 String packageName = appInfo.componentName.getPackageName();
1729 String className = appInfo.componentName.getClassName();
1730 Intent intent = new Intent(
1731 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001732 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1733 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001734 startActivity(intent);
1735 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001736 }
1737
Michael Jurkaddd62e92011-02-16 17:49:14 -08001738 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1740 try {
1741 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001742 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 } catch (ActivityNotFoundException e) {
1744 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001745 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 } catch (SecurityException e) {
1747 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001748 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001750 "or use the exported attribute for this activity. "
1751 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001753 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001755
Romain Guy8e633c52010-03-04 12:51:36 -08001756 void startActivityForResultSafely(Intent intent, int requestCode) {
1757 try {
1758 startActivityForResult(intent, requestCode);
1759 } catch (ActivityNotFoundException e) {
1760 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1761 } catch (SecurityException e) {
1762 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1763 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1764 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1765 "or use the exported attribute for this activity.", e);
1766 }
1767 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001768
Adam Cohena9cf38f2011-05-02 15:36:58 -07001769 private void handleFolderClick(FolderIcon folderIcon) {
1770 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001771 Folder openFolder = mWorkspace.getFolderForTag(info);
1772
1773 // If the folder info reports that the associated folder is open, then verify that
1774 // it is actually opened. There have been a few instances where this gets out of sync.
1775 if (info.opened && openFolder == null) {
1776 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1777 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1778 info.opened = false;
1779 }
1780
Adam Cohena9cf38f2011-05-02 15:36:58 -07001781 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 // Close any open folder
1783 closeFolder();
1784 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001785 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 } else {
1787 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 int folderScreen;
1789 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001790 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 // .. and close it
1792 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001793 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 // Close any folder open on the current screen
1795 closeFolder();
1796 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001797 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 }
1799 }
1800 }
1801 }
1802
Adam Cohen2801caf2011-05-13 20:57:39 -07001803 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001804 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001805 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1806 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1807 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1808
Adam Cohenc51934b2011-07-26 21:07:43 -07001809 FolderInfo info = (FolderInfo) fi.getTag();
1810 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1811 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001812 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1813 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001814 }
1815
Adam Cohen2801caf2011-05-13 20:57:39 -07001816 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1817 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1818 oa.start();
1819 }
1820
1821 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001822 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001823 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1824 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1825 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1826
Adam Cohenc51934b2011-07-26 21:07:43 -07001827 FolderInfo info = (FolderInfo) fi.getTag();
1828 CellLayout cl = null;
1829 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1830 cl = (CellLayout) fi.getParent().getParent();
1831 }
1832
1833 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001834 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1835 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001836 oa.addListener(new AnimatorListenerAdapter() {
1837 @Override
1838 public void onAnimationEnd(Animator animation) {
1839 if (layout != null) {
1840 layout.clearFolderLeaveBehind();
1841 }
1842 }
1843 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001844 oa.start();
1845 }
1846
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001848 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 * is animated relative to the specified View. If the View is null, no animation
1850 * is played.
1851 *
1852 * @param folderInfo The FolderInfo describing the folder to open.
1853 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001854 public void openFolder(FolderIcon folderIcon) {
1855 Folder folder = folderIcon.mFolder;
1856 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001857
Adam Cohen2801caf2011-05-13 20:57:39 -07001858 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001859 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860
Adam Cohen4554ee12011-08-03 16:13:21 -07001861 // Just verify that the folder hasn't already been added to the DragLayer.
1862 // There was a one-off crash where the folder had a parent already.
1863 if (folder.getParent() == null) {
1864 mDragLayer.addView(folder);
1865 mDragController.addDropTarget((DropTarget) folder);
1866 } else {
1867 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1868 folder.getParent() + ").");
1869 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001870 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001871 }
1872
1873 public void closeFolder() {
1874 Folder folder = mWorkspace.getOpenFolder();
1875 if (folder != null) {
1876 closeFolder(folder);
1877 }
1878 }
1879
1880 void closeFolder(Folder folder) {
1881 folder.getInfo().opened = false;
1882
1883 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1884 if (parent != null) {
1885 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1886 shrinkAndFadeInFolderIcon(fi);
1887 }
1888 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 }
1890
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001892 if (mState != State.WORKSPACE) {
1893 return false;
1894 }
1895
Joe Onorato9c1289c2009-08-17 11:03:03 -04001896 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 return false;
1898 }
1899
1900 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001901 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 }
1903
Michael Jurka0280c3b2010-09-17 15:00:07 -07001904 resetAddInfo();
1905 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001907 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 return true;
1909 }
1910
Winson Chung3d503fb2011-07-13 17:25:49 -07001911 // The hotseat touch handling does not go through Workspace, and we always allow long press
1912 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001913 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001914 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1915 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001916 if (itemUnderLongClick == null) {
1917 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001918 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1919 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001920 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001922 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001924 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 }
1926 }
1927 }
1928 return true;
1929 }
1930
Winson Chung3d503fb2011-07-13 17:25:49 -07001931 boolean isHotseatLayout(View layout) {
1932 return mHotseat != null && layout != null &&
1933 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1934 }
1935 Hotseat getHotseat() {
1936 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001937 }
1938
Winson Chung3d503fb2011-07-13 17:25:49 -07001939 /**
1940 * Returns the CellLayout of the specified container at the specified screen.
1941 */
1942 CellLayout getCellLayout(long container, int screen) {
1943 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1944 if (mHotseat != null) {
1945 return mHotseat.getLayout();
1946 } else {
1947 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001948 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001949 } else {
1950 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001951 }
1952 }
1953
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 Workspace getWorkspace() {
1955 return mWorkspace;
1956 }
1957
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 @Override
1959 protected Dialog onCreateDialog(int id) {
1960 switch (id) {
1961 case DIALOG_CREATE_SHORTCUT:
1962 return new CreateShortcut().createDialog();
1963 case DIALOG_RENAME_FOLDER:
1964 return new RenameFolder().createDialog();
1965 }
1966
1967 return super.onCreateDialog(id);
1968 }
1969
1970 @Override
1971 protected void onPrepareDialog(int id, Dialog dialog) {
1972 switch (id) {
1973 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 break;
1975 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001976 if (mFolderInfo != null) {
1977 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1978 final CharSequence text = mFolderInfo.title;
1979 input.setText(text);
1980 input.setSelection(0, text.length());
1981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 break;
1983 }
1984 }
1985
1986 void showRenameDialog(FolderInfo info) {
1987 mFolderInfo = info;
1988 mWaitingForResult = true;
1989 showDialog(DIALOG_RENAME_FOLDER);
1990 }
1991
Adam Cohenfbba09b2011-07-18 21:43:05 -07001992 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001993 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001994 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1995 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 mWaitingForResult = true;
1997 showDialog(DIALOG_CREATE_SHORTCUT);
1998 }
1999
2000 private class RenameFolder {
2001 private EditText mInput;
2002
2003 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2005 mInput = (EditText) layout.findViewById(R.id.folder_name);
2006
2007 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2008 builder.setIcon(0);
2009 builder.setTitle(getString(R.string.rename_folder_title));
2010 builder.setCancelable(true);
2011 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2012 public void onCancel(DialogInterface dialog) {
2013 cleanup();
2014 }
2015 });
2016 builder.setNegativeButton(getString(R.string.cancel_action),
2017 new Dialog.OnClickListener() {
2018 public void onClick(DialogInterface dialog, int which) {
2019 cleanup();
2020 }
2021 }
2022 );
2023 builder.setPositiveButton(getString(R.string.rename_action),
2024 new Dialog.OnClickListener() {
2025 public void onClick(DialogInterface dialog, int which) {
2026 changeFolderName();
2027 }
2028 }
2029 );
2030 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002031
2032 final AlertDialog dialog = builder.create();
2033 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2034 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002035 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002036 mInput.requestFocus();
2037 InputMethodManager inputManager = (InputMethodManager)
2038 getSystemService(Context.INPUT_METHOD_SERVICE);
2039 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002040 }
2041 });
2042
2043 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 }
2045
2046 private void changeFolderName() {
2047 final String name = mInput.getText().toString();
2048 if (!TextUtils.isEmpty(name)) {
2049 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002050 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 mFolderInfo.title = name;
2052 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2053
Joe Onorato9c1289c2009-08-17 11:03:03 -04002054 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002055 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002056 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 } else {
2058 final FolderIcon folderIcon = (FolderIcon)
2059 mWorkspace.getViewForTag(mFolderInfo);
2060 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002061 // TODO: At some point we'll probably want some version of setting
2062 // the text for a folder icon.
2063 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 getWorkspace().requestLayout();
2065 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002066 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002067 mWorkspaceLoading = true;
2068 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 }
2070 }
2071 }
2072 cleanup();
2073 }
2074
2075 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002076 dismissDialog(DIALOG_RENAME_FOLDER);
2077 mWaitingForResult = false;
2078 mFolderInfo = null;
2079 }
2080 }
2081
Daniel Sandler843e8602010-06-07 14:59:01 -04002082 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2083 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002084 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002085 }
2086
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002087 // AllAppsView.Watcher
2088 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002089 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002090 mWorkspace.setVisibility(View.GONE);
2091 }
2092 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002093
2094 /**
2095 * Helper method for the cameraZoomIn/cameraZoomOut animations
2096 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002097 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002098 * @param scaleFactor The scale factor used for the zoom
2099 */
2100 private void setPivotsForZoom(View view, State state, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002101 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002102 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002103 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002104
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002105 void updateWallpaperVisibility(boolean visible) {
2106 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2107 int curflags = getWindow().getAttributes().flags
2108 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2109 if (wpflags != curflags) {
2110 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2111 }
2112 }
2113
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002114 /**
2115 * Zoom the camera out from the workspace to reveal 'toView'.
2116 * Assumes that the view to show is anchored at either the very top or very bottom
2117 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002118 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002119 */
Winson Chungc07918d2011-07-01 15:35:26 -07002120 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002121 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002122
Winson Chungf0ea4d32011-06-06 14:27:16 -07002123 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2124 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2125 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2126 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002127 final int startDelay =
2128 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002129
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002130 setPivotsForZoom(toView, toState, scale);
2131
Michael Jurkad74c9842011-07-10 12:44:21 -07002132 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002133 mWorkspace.changeState(Workspace.State.SMALL, animated);
Adam Cohencff6af82011-09-13 14:51:53 -07002134 //hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002135
2136 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002137 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002138 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002139 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2140 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002141 ((View) toView.getParent()).invalidate();
2142 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002143 toView.setFastScaleX(a * scale + b * 1f);
2144 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002145 }
2146 });
Adam Cohen61033d32010-11-15 18:29:44 -08002147
Winson Chungf0ea4d32011-06-06 14:27:16 -07002148 toView.setVisibility(View.VISIBLE);
2149 toView.setFastAlpha(0f);
2150 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2151 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2152 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2153 public void onAnimationUpdate(float a, float b) {
2154 // don't need to invalidate because we do so above
2155 toView.setFastAlpha(a * 0f + b * 1f);
2156 }
2157 });
Adam Cohencff6af82011-09-13 14:51:53 -07002158 alphaAnim.setStartDelay(startDelay);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002159 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002160
Michael Jurkaabded662011-03-04 12:06:57 -08002161 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002162 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim, false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002163 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002164 scaleAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002165 boolean animationCancelled = false;
2166
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002167 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002168 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002169 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002170 // Prepare the position
2171 toView.setTranslationX(0.0f);
2172 toView.setTranslationY(0.0f);
2173 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002174 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002175 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002176 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002177 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002178 // If we don't set the final scale values here, if this animation is cancelled
2179 // it will have the wrong scale value and subsequent cameraPan animations will
2180 // not fix that
2181 toView.setScaleX(1.0f);
2182 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002183 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002184 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002185 }
Winson Chung32174c82011-07-19 15:47:55 -07002186
2187 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2188 // Hide the workspace scrollbar
2189 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002190 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002191 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002192 if (!animationCancelled) {
2193 updateWallpaperVisibility(false);
2194 }
2195 }
2196
Adam Cohencff6af82011-09-13 14:51:53 -07002197 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002198 public void onAnimationCancel(Animator animation) {
2199 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002200 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002201 });
2202
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002203 // toView should appear right at the end of the workspace shrink animation
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002204
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002205 if (mStateAnimation != null) mStateAnimation.cancel();
2206 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002207 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002208 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002209 } else {
2210 toView.setTranslationX(0.0f);
2211 toView.setTranslationY(0.0f);
2212 toView.setScaleX(1.0f);
2213 toView.setScaleY(1.0f);
2214 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002215 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002216 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002217 ((LauncherTransitionable) toView).onLauncherTransitionStart(null, false);
2218 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002219
Winson Chungc07918d2011-07-01 15:35:26 -07002220 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2221 // Hide the workspace scrollbar
2222 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002223 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002224 }
Michael Jurkaabded662011-03-04 12:06:57 -08002225 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002226 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002227 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002228 }
2229
2230 /**
2231 * Zoom the camera back into the workspace, hiding 'fromView'.
2232 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002233 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002234 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002235 */
Winson Chungc07918d2011-07-01 15:35:26 -07002236 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002237 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002238
Winson Chungf0ea4d32011-06-06 14:27:16 -07002239 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2240 final float scaleFactor = (float)
2241 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2242 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002243
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002244 setPivotsForZoom(fromView, fromState, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002245 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002246 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002247 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002248 if (mStateAnimation != null) mStateAnimation.cancel();
2249 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002250
Michael Jurka742574b2011-02-02 23:51:01 -08002251 final float oldScaleX = fromView.getScaleX();
2252 final float oldScaleY = fromView.getScaleY();
2253
2254 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2255 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002256 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2257 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002258 ((View)fromView.getParent()).fastInvalidate();
2259 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2260 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2261 }
2262 });
Michael Jurkaabded662011-03-04 12:06:57 -08002263 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002264 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Adam Cohencff6af82011-09-13 14:51:53 -07002265 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002266 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2267 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002268 // don't need to invalidate because we do so above
2269 fromView.setFastAlpha(a * 1f + b * 0f);
2270 }
2271 });
Michael Jurkaabded662011-03-04 12:06:57 -08002272 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002273 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002274 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002275 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002276 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002277 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002278 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002279 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002280 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002281 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim,true);
Michael Jurka2763be32011-02-24 11:19:57 -08002282 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002283 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002284 }
2285 });
2286
Winson Chungf0ea4d32011-06-06 14:27:16 -07002287 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002288 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002289 } else {
2290 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002291 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002292 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null, true);
2293 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002294 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002295 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002296 }
2297
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002298 void showWorkspace(boolean animated) {
Adam Cohencff6af82011-09-13 14:51:53 -07002299 Resources res = getResources();
2300 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2301
2302 mWorkspace.changeState(Workspace.State.NORMAL, animated, stagger);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002303 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002304 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002305 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002306
Adam Cohen7777d962011-08-18 18:58:38 -07002307 mWorkspace.showDockDivider(!animated);
2308 mWorkspace.flashScrollingIndicator();
2309
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002310 // Change the state *after* we've called all the transition code
2311 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002312
Winson Chung5fb63472011-02-02 17:03:37 -08002313 // Resume the auto-advance of widgets
2314 mUserPresent = true;
2315 updateRunning();
2316
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002317 // send an accessibility event to announce the context change
2318 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002319 }
2320
Adam Cohen7777d962011-08-18 18:58:38 -07002321 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002322 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002323 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Winson Chungb26f3d62011-06-02 10:49:29 -07002324 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002325 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002326 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002327 }
Adam Cohen7777d962011-08-18 18:58:38 -07002328
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002329 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chung09bfc452011-09-09 11:30:20 -07002330 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2331
Winson Chunge7a03942011-08-05 15:05:12 -07002332 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002333 @Override
2334 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002335 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002336 // Before we show workspace, hide all apps again because
2337 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2338 // clean up our state transition functions
2339 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohen7777d962011-08-18 18:58:38 -07002340 mSearchDeleteBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002341 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002342 } else {
2343 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002344 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002345 }
2346 }, (extendedDelay ?
2347 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2348 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2349 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002350 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002351 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002352 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2353 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002354 }
2355 // Otherwise, we are not in spring loaded mode, so don't do anything.
2356 }
2357
Adam Cohenfc53cd22011-07-20 15:45:11 -07002358 public boolean isAllAppsCustomizeOpen() {
2359 return mState == State.APPS_CUSTOMIZE;
2360 }
2361
Winson Chungf0ea4d32011-06-06 14:27:16 -07002362 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002363 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002364 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002365 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002366 if (!LauncherApplication.isScreenLarge()) {
2367 if (animated) {
2368 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002369 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002370 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002371 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002372 }
2373 }
2374 }
2375
2376 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002377 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002378 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002379 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002380 if (!LauncherApplication.isScreenLarge()) {
2381 if (animated) {
2382 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002383 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002384 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002385 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002386 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002387 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002388 }
2389
Winson Chung785d2eb2011-04-14 16:08:02 -07002390 void showAllApps(boolean animated) {
2391 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002392
Winson Chungf0ea4d32011-06-06 14:27:16 -07002393 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2394 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002395
Winson Chung3d503fb2011-07-13 17:25:49 -07002396 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002397 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002398
Winson Chungf0ea4d32011-06-06 14:27:16 -07002399 // Change the state *after* we've called all the transition code
2400 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002401
2402 // Pause the auto-advance of widgets until we are out of AllApps
2403 mUserPresent = false;
2404 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002405 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002406
2407 // Send an accessibility event to announce the context change
2408 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2409 }
2410
Joe Onoratob2061212009-11-24 16:13:54 -05002411 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002412 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002413 * - Home from workspace
2414 * - from center screen
2415 * - from other screens
2416 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002417 * - from center screen
2418 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002419 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002420 * - from center screen
2421 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002422 * - Launch app from workspace and quit
2423 * - with back
2424 * - with home
2425 * - Launch app from all apps and quit
2426 * - with back
2427 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002428 * - Go to a screen that's not the default, then all
2429 * apps, and launch and app, and go back
2430 * - with back
2431 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002432 * - On workspace, long press power and go back
2433 * - with back
2434 * - with home
2435 * - On all apps, long press power and go back
2436 * - with back
2437 * - with home
2438 * - On workspace, power off
2439 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002440 * - Launch an app and turn off the screen while in that app
2441 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002442 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002443 * - From all apps
2444 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002445 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2446 * - From all apps
2447 * - From the center workspace
2448 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002449 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002450 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002451 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2452 mWorkspace.setVisibility(View.VISIBLE);
2453 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002454
Winson Chung3d503fb2011-07-13 17:25:49 -07002455 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002456 mSearchDeleteBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002457
Winson Chungf0ea4d32011-06-06 14:27:16 -07002458 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002459 if (mAllAppsButton != null) {
2460 mAllAppsButton.requestFocus();
2461 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002462 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002463 }
2464
Joe Onorato7c312c12009-08-13 21:36:53 -07002465 void lockAllApps() {
2466 // TODO
2467 }
2468
2469 void unlockAllApps() {
2470 // TODO
2471 }
2472
Patrick Dubroy5f445422011-02-18 14:35:21 -08002473 /**
2474 * Add an item from all apps or customize onto the given workspace screen.
2475 * If layout is null, add to the current screen.
2476 */
2477 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002478 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2479 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002480 } else {
2481 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002482 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002483 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002484
Winson Chungdff8ebb2011-09-08 17:25:31 -07002485 /** Maps the current orientation to an index for referencing orientation correct global icons */
2486 private int getCurrentOrientationIndexForGlobalIcons() {
2487 // default - 0, landscape - 1
2488 switch (getResources().getConfiguration().orientation) {
2489 case Configuration.ORIENTATION_LANDSCAPE:
2490 return 1;
2491 default:
2492 return 0;
2493 }
2494 }
2495
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002496 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002497 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002498 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002499 // Look for the toolbar icon specified in the activity meta-data
2500 Bundle metaData = packageManager.getActivityInfo(
2501 activityName, PackageManager.GET_META_DATA).metaData;
2502 if (metaData != null) {
2503 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2504 if (iconResId != 0) {
2505 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002506 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002507 }
2508 }
2509 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002510 // This can happen if the activity defines an invalid drawable
2511 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2512 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002513 } catch (Resources.NotFoundException nfe) {
2514 // This can happen if the activity defines an invalid drawable
2515 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2516 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002517 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002518 return null;
2519 }
2520
2521 // if successful in getting icon, return it; otherwise, set button to use default drawable
2522 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2523 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2524 TextView button = (TextView) findViewById(buttonId);
2525 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002526 Resources r = getResources();
2527 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2528 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002529
2530 // If we were unable to find the icon via the meta-data, use a generic one
2531 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002532 toolbarIcon = r.getDrawable(fallbackDrawableId);
2533 toolbarIcon.setBounds(0, 0, w, h);
2534 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002535 return null;
2536 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002537 toolbarIcon.setBounds(0, 0, w, h);
2538 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002539 return toolbarIcon.getConstantState();
2540 }
2541 }
2542
2543 // if successful in getting icon, return it; otherwise, set button to use default drawable
2544 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2545 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2546 ImageView button = (ImageView) findViewById(buttonId);
2547 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2548
Michael Jurka19e0fc52011-07-22 18:00:21 -07002549 if (button != null) {
2550 // If we were unable to find the icon via the meta-data, use a
2551 // generic one
2552 if (toolbarIcon == null) {
2553 button.setImageResource(fallbackDrawableId);
2554 } else {
2555 button.setImageDrawable(toolbarIcon);
2556 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002557 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002558
2559 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2560
Michael Jurka0423dcf2010-10-05 14:56:18 -07002561 }
2562
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002563 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2564 TextView button = (TextView) findViewById(buttonId);
2565 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2566 }
2567
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002568 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002569 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002570 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002571 }
2572
Michael Jurka0423dcf2010-10-05 14:56:18 -07002573 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002574 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2575 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002576 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002577
Winson Chung1cad91e2011-05-25 17:41:01 -07002578 final SearchManager searchManager =
2579 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2580 ComponentName activityName = searchManager.getGlobalSearchActivity();
2581 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002582 int coi = getCurrentOrientationIndexForGlobalIcons();
2583 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002584 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002585 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002586 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002587 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002588 // We disable both search and voice search when there is no global search provider
Winson Chung1cad91e2011-05-25 17:41:01 -07002589 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002590 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002591 voiceButton.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002592 }
2593 }
2594
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002595 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002596 updateButtonWithDrawable(R.id.search_button, d);
2597 }
2598
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002599 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002600 final View searchDivider = findViewById(R.id.search_divider);
2601 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002602
Winson Chung1cad91e2011-05-25 17:41:01 -07002603 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2604 ComponentName activityName = intent.resolveActivity(getPackageManager());
2605 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002606 int coi = getCurrentOrientationIndexForGlobalIcons();
2607 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002608 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002609 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002610 voiceButton.setVisibility(View.VISIBLE);
2611 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002612 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002613 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002614 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002615 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002616
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002617 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002618 updateButtonWithDrawable(R.id.voice_button, d);
2619 }
2620
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002621 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002622 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002623 */
2624 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002625 final View marketButton = findViewById(R.id.market_button);
2626 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2627 // Find the app market activity by resolving an intent.
2628 // (If multiple app markets are installed, it will return the ResolverActivity.)
2629 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002630 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002631 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002632 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002633 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002634 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002635 marketButton.setVisibility(View.VISIBLE);
2636 } else {
2637 // We should hide and disable the view so that we don't try and restore the visibility
2638 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2639 marketButton.setVisibility(View.GONE);
2640 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002641 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002642 }
2643
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002644 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002645 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002646 }
2647
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002648 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002649 * Displays the shortcut creation dialog and launches, if necessary, the
2650 * appropriate activity.
2651 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002652 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002653 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2654 DialogInterface.OnShowListener {
2655
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002656 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002657
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002658 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002659 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002660
Winson Chung55b65502011-05-26 12:03:43 -07002661 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2662 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002663 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002664
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002665 AlertDialog dialog = builder.create();
2666 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002667 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002668 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002669
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002670 return dialog;
2671 }
2672
2673 public void onCancel(DialogInterface dialog) {
2674 mWaitingForResult = false;
2675 cleanup();
2676 }
2677
Romain Guycbb89e42009-06-08 15:52:54 -07002678 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002679 mWaitingForResult = false;
2680 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002681 }
2682
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002683 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002684 try {
2685 dismissDialog(DIALOG_CREATE_SHORTCUT);
2686 } catch (Exception e) {
2687 // An exception is thrown if the dialog is not visible, which is fine
2688 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002689 }
2690
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002691 /**
2692 * Handle the action clicked in the "Add to home" dialog.
2693 */
2694 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002695 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002696
Winson Chung55b65502011-05-26 12:03:43 -07002697 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2698 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002699 case AddAdapter.ITEM_APPLICATION: {
2700 if (mAppsCustomizeTabHost != null) {
2701 mAppsCustomizeTabHost.selectAppsTab();
2702 }
2703 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002704 break;
2705 }
Winson Chung55b65502011-05-26 12:03:43 -07002706 case AddAdapter.ITEM_APPWIDGET: {
2707 if (mAppsCustomizeTabHost != null) {
2708 mAppsCustomizeTabHost.selectWidgetsTab();
2709 }
2710 showAllApps(true);
2711 break;
2712 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002713 case AddAdapter.ITEM_WALLPAPER: {
2714 startWallpaper();
2715 break;
2716 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002717 }
2718 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002719
2720 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002721 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002722 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002723 }
2724
2725 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002726 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002727 */
2728 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2729 @Override
2730 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002731 closeSystemDialogs();
2732 }
2733 }
2734
2735 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002736 * Receives notifications whenever the appwidgets are reset.
2737 */
2738 private class AppWidgetResetObserver extends ContentObserver {
2739 public AppWidgetResetObserver() {
2740 super(new Handler());
2741 }
2742
2743 @Override
2744 public void onChange(boolean selfChange) {
2745 onAppWidgetReset();
2746 }
2747 }
2748
2749 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002750 * If the activity is currently paused, signal that we need to re-run the loader
2751 * in onResume.
2752 *
2753 * This needs to be called from incoming places where resources might have been loaded
2754 * while we are paused. That is becaues the Configuration might be wrong
2755 * when we're not running, and if it comes back to what it was when we
2756 * were paused, we are not restarted.
2757 *
2758 * Implementation of the method from LauncherModel.Callbacks.
2759 *
2760 * @return true if we are currently paused. The caller might be able to
2761 * skip some work in that case since we will come back again.
2762 */
2763 public boolean setLoadOnResume() {
2764 if (mPaused) {
2765 Log.i(TAG, "setLoadOnResume");
2766 mOnResumeNeedsLoad = true;
2767 return true;
2768 } else {
2769 return false;
2770 }
2771 }
2772
2773 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002774 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002775 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002776 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002777 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002778 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002779 } else {
2780 return SCREEN_COUNT / 2;
2781 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002782 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002783
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002784
Joe Onorato9c1289c2009-08-17 11:03:03 -04002785 /**
2786 * Refreshes the shortcuts shown on the workspace.
2787 *
2788 * Implementation of the method from LauncherModel.Callbacks.
2789 */
2790 public void startBinding() {
2791 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002792
2793 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002794 int count = workspace.getChildCount();
2795 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002796 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002797 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2798 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002799 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002800 if (mHotseat != null) {
2801 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002802 }
2803 }
2804
2805 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002806 * Bind the items start-end from the list.
2807 *
2808 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002809 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002810 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002811 setLoadOnResume();
2812
Joe Onorato9c1289c2009-08-17 11:03:03 -04002813 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002814 for (int i=start; i<end; i++) {
2815 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002816
2817 // Short circuit if we are loading dock items for a configuration which has no dock
2818 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2819 mHotseat == null) {
2820 continue;
2821 }
2822
Joe Onorato9c1289c2009-08-17 11:03:03 -04002823 switch (item.itemType) {
2824 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2825 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002826 View shortcut = createShortcut((ShortcutInfo)item);
2827 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2828 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002829 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002830 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002831 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002832 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002833 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002834 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2835 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002836 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002837 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002838 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002839 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002840 }
2841
Joe Onorato9c1289c2009-08-17 11:03:03 -04002842 /**
2843 * Implementation of the method from LauncherModel.Callbacks.
2844 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002845 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002846 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002847 sFolders.clear();
2848 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002849 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002850
2851 /**
2852 * Add the views for a widget to the workspace.
2853 *
2854 * Implementation of the method from LauncherModel.Callbacks.
2855 */
2856 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002857 setLoadOnResume();
2858
Daniel Sandler843e8602010-06-07 14:59:01 -04002859 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2860 if (DEBUG_WIDGETS) {
2861 Log.d(TAG, "bindAppWidget: " + item);
2862 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002863 final Workspace workspace = mWorkspace;
2864
2865 final int appWidgetId = item.appWidgetId;
2866 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002867 if (DEBUG_WIDGETS) {
2868 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2869 }
2870
Joe Onorato9c1289c2009-08-17 11:03:03 -04002871 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2872
Joe Onorato9c1289c2009-08-17 11:03:03 -04002873 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2874 item.hostView.setTag(item);
2875
Winson Chung3d503fb2011-07-13 17:25:49 -07002876 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002877 item.cellY, item.spanX, item.spanY, false);
2878
Adam Cohended9f8d2010-11-03 13:25:16 -07002879 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2880
Joe Onorato9c1289c2009-08-17 11:03:03 -04002881 workspace.requestLayout();
2882
Daniel Sandler843e8602010-06-07 14:59:01 -04002883 if (DEBUG_WIDGETS) {
2884 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2885 + (SystemClock.uptimeMillis()-start) + "ms");
2886 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002887 }
2888
2889 /**
2890 * Callback saying that there aren't any more items to bind.
2891 *
2892 * Implementation of the method from LauncherModel.Callbacks.
2893 */
2894 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002895 setLoadOnResume();
2896
Joe Onorato9c1289c2009-08-17 11:03:03 -04002897 if (mSavedState != null) {
2898 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002899 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002900 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002901 mSavedState = null;
2902 }
2903
2904 if (mSavedInstanceState != null) {
2905 super.onRestoreInstanceState(mSavedInstanceState);
2906 mSavedInstanceState = null;
2907 }
2908
Joe Onorato9c1289c2009-08-17 11:03:03 -04002909 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002910
2911 // If we received the result of any pending adds while the loader was running (e.g. the
2912 // widget configuration forced an orientation change), process them now.
2913 for (int i = 0; i < sPendingAddList.size(); i++) {
2914 completeAdd(sPendingAddList.get(i));
2915 }
2916 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002917 }
2918
2919 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002920 * Updates the icons on the launcher that are affected by changes to the package list
2921 * on the device.
2922 */
2923 private void updateIconsAffectedByPackageManagerChanges() {
2924 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002925 updateVoiceSearchIcon();
2926 }
2927
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002928 @Override
2929 public void bindSearchablesChanged() {
2930 updateGlobalSearchIcon();
2931 }
2932
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002933 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002934 * Add the icons for all apps.
2935 *
2936 * Implementation of the method from LauncherModel.Callbacks.
2937 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002938 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2939 // Remove the progress bar entirely; we could also make it GONE
2940 // but better to remove it since we know it's not going to be used
2941 View progressBar = mAppsCustomizeTabHost.
2942 findViewById(R.id.apps_customize_progress_bar);
2943 if (progressBar != null) {
2944 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002945 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002946 // We just post the call to setApps so the user sees the progress bar
2947 // disappear-- otherwise, it just looks like the progress bar froze
2948 // which doesn't look great
2949 mAppsCustomizeTabHost.post(new Runnable() {
2950 public void run() {
2951 if (mAppsCustomizeContent != null) {
2952 mAppsCustomizeContent.setApps(apps);
2953 }
2954 }
2955 });
2956
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002957 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002958 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002959 }
2960
2961 /**
2962 * A package was installed.
2963 *
2964 * Implementation of the method from LauncherModel.Callbacks.
2965 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002966 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002967 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002968 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002969
Winson Chung785d2eb2011-04-14 16:08:02 -07002970 if (mAppsCustomizeContent != null) {
2971 mAppsCustomizeContent.addApps(apps);
2972 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002973 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002974 }
2975
2976 /**
2977 * A package was updated.
2978 *
2979 * Implementation of the method from LauncherModel.Callbacks.
2980 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002981 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002982 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002983 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08002984 if (mWorkspace != null) {
2985 mWorkspace.updateShortcuts(apps);
2986 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002987
Winson Chung785d2eb2011-04-14 16:08:02 -07002988 if (mAppsCustomizeContent != null) {
2989 mAppsCustomizeContent.updateApps(apps);
2990 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002991 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002992 }
2993
2994 /**
2995 * A package was uninstalled.
2996 *
2997 * Implementation of the method from LauncherModel.Callbacks.
2998 */
Joe Onorato36115782010-06-17 13:28:48 -04002999 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003000 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003001 if (permanent) {
3002 mWorkspace.removeItems(apps);
3003 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003004
Winson Chung785d2eb2011-04-14 16:08:02 -07003005 if (mAppsCustomizeContent != null) {
3006 mAppsCustomizeContent.removeApps(apps);
3007 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003008 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003009 }
Joe Onoratobe386092009-11-17 17:32:16 -08003010
3011 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003012 * A number of packages were updated.
3013 */
3014 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003015
Winson Chung785d2eb2011-04-14 16:08:02 -07003016 if (mAppsCustomizeContent != null) {
3017 mAppsCustomizeContent.onPackagesUpdated();
3018 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003019 }
3020
Winson Chung400438b2011-01-16 17:53:48 -08003021 private int mapConfigurationOriActivityInfoOri(int configOri) {
3022 final Display d = getWindowManager().getDefaultDisplay();
3023 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3024 switch (d.getRotation()) {
3025 case Surface.ROTATION_0:
3026 case Surface.ROTATION_180:
3027 // We are currently in the same basic orientation as the natural orientation
3028 naturalOri = configOri;
3029 break;
3030 case Surface.ROTATION_90:
3031 case Surface.ROTATION_270:
3032 // We are currently in the other basic orientation to the natural orientation
3033 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3034 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3035 break;
3036 }
3037
3038 int[] oriMap = {
3039 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3040 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3041 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3042 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3043 };
3044 // Since the map starts at portrait, we need to offset if this device's natural orientation
3045 // is landscape.
3046 int indexOffset = 0;
3047 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3048 indexOffset = 1;
3049 }
3050 return oriMap[(d.getRotation() + indexOffset) % 4];
3051 }
Adam Cohen446e9402011-09-15 18:21:21 -07003052
3053 public void lockScreenOrientationOnLargeUI() {
3054 if (LauncherApplication.isScreenLarge()) {
3055 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3056 .getConfiguration().orientation));
3057 }
Winson Chung400438b2011-01-16 17:53:48 -08003058 }
Adam Cohen446e9402011-09-15 18:21:21 -07003059 public void unlockScreenOrientationOnLargeUI() {
3060 if (LauncherApplication.isScreenLarge()) {
3061 mHandler.postDelayed(new Runnable() {
3062 public void run() {
3063 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3064 }
3065 }, mRestoreScreenOrientationDelay);
3066 }
Winson Chung400438b2011-01-16 17:53:48 -08003067 }
3068
Winson Chung82f55532011-08-09 14:14:23 -07003069 /* Cling related */
3070 private static final String WORKSPACE_CLING_DISMISSED_KEY = "cling.workspace.dismissed";
3071 private static final String ALLAPPS_CLING_DISMISSED_KEY = "cling.allapps.dismissed";
3072 private void enableClingsIfNecessary() {
3073 // TEMPORARY: DISABLE CLINGS ON LARGE UI
3074 if (LauncherApplication.isScreenLarge()) return;
3075
Brett Chabot2a9e2282011-08-23 15:03:13 -07003076 // disable clings when running in a test harness
3077 if(ActivityManager.isRunningInTestHarness()) return;
3078
Winson Chung82f55532011-08-09 14:14:23 -07003079 // Enable the clings only if they have not been dismissed before
3080 SharedPreferences prefs =
3081 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3082 if (!prefs.getBoolean(WORKSPACE_CLING_DISMISSED_KEY, false)) {
3083 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3084 cling.init(this);
3085 cling.setVisibility(View.VISIBLE);
3086 }
3087 if (!prefs.getBoolean(ALLAPPS_CLING_DISMISSED_KEY, false)) {
3088 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3089 cling.init(this);
3090 cling.setVisibility(View.VISIBLE);
3091 }
3092 }
3093 private void dismissCling(final Cling cling, final String flag) {
3094 if (cling != null) {
3095 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
3096 anim.setDuration(DISMISS_CLING_DURATION);
3097 anim.addListener(new AnimatorListenerAdapter() {
3098 public void onAnimationEnd(Animator animation) {
3099 cling.setVisibility(View.GONE);
3100 cling.cleanup();
3101 SharedPreferences prefs =
3102 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3103 SharedPreferences.Editor editor = prefs.edit();
3104 editor.putBoolean(flag, true);
3105 editor.commit();
3106 };
3107 });
3108 anim.start();
3109 }
3110 }
3111 public void dismissWorkspaceCling(View v) {
3112 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3113 dismissCling(cling, WORKSPACE_CLING_DISMISSED_KEY);
3114 }
3115 public void dismissAllAppsCling(View v) {
3116 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3117 dismissCling(cling, ALLAPPS_CLING_DISMISSED_KEY);
3118 }
3119
Winson Chung80baf5a2010-08-09 16:03:15 -07003120 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003121 * Prints out out state for debugging.
3122 */
3123 public void dumpState() {
3124 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003125 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003126 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3127 Log.d(TAG, "mRestoring=" + mRestoring);
3128 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3129 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003130 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003131 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003132
Winson Chung785d2eb2011-04-14 16:08:02 -07003133 if (mAppsCustomizeContent != null) {
3134 mAppsCustomizeContent.dumpState();
3135 }
Joe Onoratobe386092009-11-17 17:32:16 -08003136 Log.d(TAG, "END launcher2 dump state");
3137 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003138}
Michael Jurka2763be32011-02-24 11:19:57 -08003139
Michael Jurkaabded662011-03-04 12:06:57 -08003140interface LauncherTransitionable {
Winson Chung5a808352011-06-27 19:08:49 -07003141 void onLauncherTransitionStart(Animator animation, boolean toWorkspace);
3142 void onLauncherTransitionEnd(Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003143}