blob: 28fdb90d3e1604c4aa3b0e3c5f12cbd3a579561a [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 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324 }
Romain Guycbb89e42009-06-08 15:52:54 -0700325
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800326 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700327 if (sLocaleConfiguration == null) {
328 new AsyncTask<Void, Void, LocaleConfiguration>() {
329 @Override
330 protected LocaleConfiguration doInBackground(Void... unused) {
331 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
332 readConfiguration(Launcher.this, localeConfiguration);
333 return localeConfiguration;
334 }
335
336 @Override
337 protected void onPostExecute(LocaleConfiguration result) {
338 sLocaleConfiguration = result;
339 checkForLocaleChange(); // recursive, but now with a locale configuration
340 }
341 }.execute();
342 return;
343 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700344
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800345 final Configuration configuration = getResources().getConfiguration();
346
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700347 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800348 final String locale = configuration.locale.toString();
349
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700350 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 final int mcc = configuration.mcc;
352
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700353 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 final int mnc = configuration.mnc;
355
Romain Guy84f296c2009-11-04 15:00:44 -0800356 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357
Romain Guy84f296c2009-11-04 15:00:44 -0800358 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700359 sLocaleConfiguration.locale = locale;
360 sLocaleConfiguration.mcc = mcc;
361 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700362
Joe Onorato0589f0f2010-02-08 13:44:00 -0800363 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700364
365 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
366 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700367 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700368 public void run() {
369 writeConfiguration(Launcher.this, localeConfiguration);
370 }
371 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700372 }
373 }
374
375 private static class LocaleConfiguration {
376 public String locale;
377 public int mcc = -1;
378 public int mnc = -1;
379 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700380
Romain Guy98d01652009-06-30 16:21:04 -0700381 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
382 DataInputStream in = null;
383 try {
384 in = new DataInputStream(context.openFileInput(PREFERENCES));
385 configuration.locale = in.readUTF();
386 configuration.mcc = in.readInt();
387 configuration.mnc = in.readInt();
388 } catch (FileNotFoundException e) {
389 // Ignore
390 } catch (IOException e) {
391 // Ignore
392 } finally {
393 if (in != null) {
394 try {
395 in.close();
396 } catch (IOException e) {
397 // Ignore
398 }
399 }
400 }
401 }
402
403 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
404 DataOutputStream out = null;
405 try {
406 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
407 out.writeUTF(configuration.locale);
408 out.writeInt(configuration.mcc);
409 out.writeInt(configuration.mnc);
410 out.flush();
411 } catch (FileNotFoundException e) {
412 // Ignore
413 } catch (IOException e) {
414 //noinspection ResultOfMethodCallIgnored
415 context.getFileStreamPath(PREFERENCES).delete();
416 } finally {
417 if (out != null) {
418 try {
419 out.close();
420 } catch (IOException e) {
421 // Ignore
422 }
423 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800424 }
425 }
426
Adam Cohen716b51e2011-06-30 12:09:54 -0700427 public DragLayer getDragLayer() {
428 return mDragLayer;
429 }
430
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 static int getScreen() {
432 synchronized (sLock) {
433 return sScreen;
434 }
435 }
436
437 static void setScreen(int screen) {
438 synchronized (sLock) {
439 sScreen = screen;
440 }
441 }
442
Winson Chung557d6ed2011-07-08 15:34:52 -0700443 /**
444 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
445 * a configuration step, this allows the proper animations to run after other transitions.
446 */
447 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700448 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800449 switch (args.requestCode) {
450 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700451 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
452 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800453 break;
454 case REQUEST_PICK_SHORTCUT:
455 processShortcut(args.intent);
456 break;
457 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700458 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
459 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700460 result = true;
461 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800462 case REQUEST_PICK_APPWIDGET:
463 addAppWidgetFromPick(args.intent);
464 break;
465 case REQUEST_CREATE_APPWIDGET:
466 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700467 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700468 result = true;
469 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800470 case REQUEST_PICK_WALLPAPER:
471 // We just wanted the activity result here so we can clear mWaitingForResult
472 break;
473 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700474 // In any situation where we have a multi-step drop, we should reset the add info only after
475 // we complete the drop
476 resetAddInfo();
477 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800478 }
479
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800481 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700482 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700483 mWaitingForResult = false;
484
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 // The pattern used here is that a user PICKs a specific application,
486 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700487
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
489 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700490 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800491 final PendingAddArguments args = new PendingAddArguments();
492 args.requestCode = requestCode;
493 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700494 args.container = mPendingAddInfo.container;
495 args.screen = mPendingAddInfo.screen;
496 args.cellX = mPendingAddInfo.cellX;
497 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800498
499 // If the loader is still running, defer the add until it is done.
500 if (isWorkspaceLocked()) {
501 sPendingAddList.add(args);
502 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700503 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 }
Romain Guy18042c82009-11-06 11:44:55 -0800505 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700506 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
507 if (data != null) {
508 // Clean up the appWidgetId if we canceled
509 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
510 if (appWidgetId != -1) {
511 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
512 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800513 }
514 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700515
516 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700517 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 }
519
520 @Override
521 protected void onResume() {
522 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800523 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700524 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400525 mWorkspaceLoading = true;
526 mModel.startLoader(this, true);
527 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700528 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800529 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800530 if (mWaitingForResume != null) {
531 mWaitingForResume.setStayPressed(false);
532 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700533 // When we resume Launcher, a different Activity might be responsible for the app
534 // market intent, so refresh the icon
535 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 }
537
538 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700539 protected void onPause() {
540 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700541 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800542 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700543 }
Romain Guycbb89e42009-06-08 15:52:54 -0700544
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700545 @Override
546 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400547 // Flag the loader to stop early before switching
548 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700549 if (mAppsCustomizeContent != null) {
550 mAppsCustomizeContent.surrender();
551 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800552 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700553 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700554
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800555 // We can't hide the IME if it was forced open. So don't bother
556 /*
557 @Override
558 public void onWindowFocusChanged(boolean hasFocus) {
559 super.onWindowFocusChanged(hasFocus);
560
561 if (hasFocus) {
562 final InputMethodManager inputManager = (InputMethodManager)
563 getSystemService(Context.INPUT_METHOD_SERVICE);
564 WindowManager.LayoutParams lp = getWindow().getAttributes();
565 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
566 android.os.Handler()) {
567 protected void onReceiveResult(int resultCode, Bundle resultData) {
568 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
569 }
570 });
571 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
572 }
573 }
574 */
575
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 private boolean acceptFilter() {
577 final InputMethodManager inputManager = (InputMethodManager)
578 getSystemService(Context.INPUT_METHOD_SERVICE);
579 return !inputManager.isFullscreenMode();
580 }
581
582 @Override
583 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700584 final int uniChar = event.getUnicodeChar();
585 final boolean handled = super.onKeyDown(keyCode, event);
586 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
587 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
589 keyCode, event);
590 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700591 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700592 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700593 // showSearchDialog()
594 // If there are multiple keystrokes before the search dialog takes focus,
595 // onSearchRequested() will be called for every keystroke,
596 // but it is idempotent, so it's fine.
597 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
599 }
600
Joe Onorato8a9625e2010-01-28 15:55:35 -0800601 // Eat the long press event so the keyboard doesn't come up.
602 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
603 return true;
604 }
605
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 return handled;
607 }
608
Karl Rosaen138a0412009-04-23 19:00:21 -0700609 private String getTypedText() {
610 return mDefaultKeySsb.toString();
611 }
612
613 private void clearTypedText() {
614 mDefaultKeySsb.clear();
615 mDefaultKeySsb.clearSpans();
616 Selection.setSelection(mDefaultKeySsb, 0);
617 }
618
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700620 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
621 * State
622 */
623 private static State intToState(int stateOrdinal) {
624 State state = State.WORKSPACE;
625 final State[] stateValues = State.values();
626 for (int i = 0; i < stateValues.length; i++) {
627 if (stateValues[i].ordinal() == stateOrdinal) {
628 state = stateValues[i];
629 break;
630 }
631 }
632 return state;
633 }
634
635 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 * Restores the previous state, if it exists.
637 *
638 * @param savedState The previous state.
639 */
640 private void restoreState(Bundle savedState) {
641 if (savedState == null) {
642 return;
643 }
644
Michael Jurka883f55b2010-10-21 15:47:14 -0700645 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700646 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800647 showAllApps(false);
648 }
649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
651 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700652 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 }
654
Winson Chung3d503fb2011-07-13 17:25:49 -0700655 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
656 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700657
Winson Chung3d503fb2011-07-13 17:25:49 -0700658 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
659 mPendingAddInfo.container = pendingAddContainer;
660 mPendingAddInfo.screen = pendingAddScreen;
661 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
662 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 mRestoring = true;
664 }
665
666 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
667 if (renameFolder) {
668 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700669 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 mRestoring = true;
671 }
Winson Chunga12a2502010-12-20 14:41:35 -0800672
Winson Chung785d2eb2011-04-14 16:08:02 -0700673
674 // Restore the AppsCustomize tab
675 if (mAppsCustomizeTabHost != null) {
676 String curTab = savedState.getString("apps_customize_currentTab");
677 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700678 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700679 mAppsCustomizeContent.setContentType(
680 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
681 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700682 mAppsCustomizeContent.loadAssociatedPages(
683 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700684 }
685
Winson Chung5afbf7b2011-07-25 11:53:08 -0700686 int currentIndex = savedState.getInt("apps_customize_currentIndex");
687 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700688 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 }
690
691 /**
692 * Finds all the views we need and configure them properly.
693 */
694 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700695 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400696
Winson Chung4c98d922011-05-31 16:50:48 -0700697 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
698 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699
Winson Chung4c98d922011-05-31 16:50:48 -0700700 // Setup the drag layer
701 mDragLayer.setup(this, dragController);
702
Winson Chung3d503fb2011-07-13 17:25:49 -0700703 // Setup the hotseat
704 mHotseat = (Hotseat) findViewById(R.id.hotseat);
705 if (mHotseat != null) {
706 mHotseat.setup(this);
707 }
708
Winson Chung4c98d922011-05-31 16:50:48 -0700709 // Setup the workspace
710 mWorkspace.setHapticFeedbackEnabled(false);
711 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700712 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700713 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700714
Winson Chungf0ea4d32011-06-06 14:27:16 -0700715 // Get the search/delete bar
716 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700717
Winson Chungf0ea4d32011-06-06 14:27:16 -0700718 // Setup AppsCustomize
719 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
720 findViewById(R.id.apps_customize_pane);
721 mAppsCustomizeContent = (AppsCustomizePagedView)
722 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
723 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400724
Romain Guy1fbc1c82009-11-09 20:43:08 -0800725
Winson Chung4c98d922011-05-31 16:50:48 -0700726
Winson Chung3d503fb2011-07-13 17:25:49 -0700727 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700728 dragController.setDragScoller(mWorkspace);
729 dragController.setScrollView(mDragLayer);
730 dragController.setMoveTarget(mWorkspace);
731 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700732 if (mSearchDeleteBar != null) {
733 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800734 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800735 }
736
737 /**
738 * Creates a view representing a shortcut.
739 *
740 * @param info The data structure describing the shortcut.
741 *
742 * @return A View inflated from R.layout.application.
743 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800744 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800745 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700746 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 }
748
749 /**
750 * Creates a view representing a shortcut inflated from the specified resource.
751 *
752 * @param layoutResId The id of the XML layout used to create the shortcut.
753 * @param parent The group the shortcut belongs to.
754 * @param info The data structure describing the shortcut.
755 *
756 * @return A View inflated from layoutResId.
757 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800758 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800759 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
760 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 return favorite;
763 }
764
765 /**
766 * Add an application shortcut to the workspace.
767 *
768 * @param data The intent describing the application.
769 * @param cellInfo The position on screen where to create the shortcut.
770 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700771 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700772 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700773 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700774
Adam Cohenfbba09b2011-07-18 21:43:05 -0700775 // First we check if we already know the exact location where we want to add this item.
776 if (cellX >= 0 && cellY >= 0) {
777 cellXY[0] = cellX;
778 cellXY[1] = cellY;
779 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700780 showOutOfSpaceMessage();
781 return;
782 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800784 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800785
Romain Guy73b979d2009-06-09 12:57:21 -0700786 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800787 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800789 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700790 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700791 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800792 } else {
793 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 }
795 }
Romain Guycbb89e42009-06-08 15:52:54 -0700796
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 /**
798 * Add a shortcut to the workspace.
799 *
800 * @param data The intent describing the shortcut.
801 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700803 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
804 int cellY) {
805 int[] cellXY = mTmpAddItemCellCoordinates;
806 int[] touchXY = mPendingAddInfo.dropPos;
807 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700808
Michael Jurkad3ef3062010-11-23 16:23:58 -0800809 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700810
Adam Cohen558baaf2011-08-15 15:22:57 -0700811 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
812 final View view = createShortcut(info);
813
Adam Cohenfbba09b2011-07-18 21:43:05 -0700814 // First we check if we already know the exact location where we want to add this item.
815 if (cellX >= 0 && cellY >= 0) {
816 cellXY[0] = cellX;
817 cellXY[1] = cellY;
818 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700819
820 // If appropriate, either create a folder or add to an existing folder
821 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
822 true, null,null)) {
823 return;
824 }
825 DragObject dragObject = new DragObject();
826 dragObject.dragInfo = info;
827 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
828 return;
829 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700830 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800831 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700832 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800833 foundCellSpan = (result != null);
834 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700835 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800836 }
837
838 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700839 showOutOfSpaceMessage();
840 return;
841 }
842
Adam Cohen558baaf2011-08-15 15:22:57 -0700843 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844
845 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700846 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
847 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 }
849 }
850
Adam Cohen50370f32011-08-25 18:57:14 -0700851 class Padding {
852 int left = 0;
853 int right = 0;
854 int top = 0;
855 int bottom = 0;
856 }
857
Adam Cohenf814aa02011-09-01 13:48:05 -0700858 Padding getPaddingForWidget(ComponentName component) {
Adam Cohen50370f32011-08-25 18:57:14 -0700859 PackageManager packageManager = getPackageManager();
860 Padding p = new Padding();
861 android.content.pm.ApplicationInfo appInfo;
862
863 try {
Adam Cohenf814aa02011-09-01 13:48:05 -0700864 appInfo = packageManager.getApplicationInfo(component.getPackageName(), 0);
Adam Cohen50370f32011-08-25 18:57:14 -0700865 } catch (Exception e) {
866 // if we can't find the package, return 0 padding
867 return p;
868 }
869
870 // TODO: This should be ICE_CREAM_SANDWICH, but since the unbundled apps
871 // may not have updated their targetSdkVersion yet, we've bumped it down for now.
872 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB_MR2) {
873 Resources r = getResources();
874 p.left = r.getDimensionPixelSize(R.dimen.app_widget_padding_left);
875 p.right = r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
876 p.top = r.getDimensionPixelSize(R.dimen.app_widget_padding_top);
877 p.bottom = r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
878 }
879
880 return p;
881 }
882
Adam Cohenf814aa02011-09-01 13:48:05 -0700883 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
884 if (spanXY == null) {
885 spanXY = new int[2];
886 }
887
888 Padding padding = getPaddingForWidget(component);
889 // We want to account for the extra amount of padding that we are adding to the widget
890 // to ensure that it gets the full amount of space that it has requested
891 int requiredWidth = minWidth + padding.left + padding.right;
892 int requiredHeight = minHeight + padding.top + padding.bottom;
893 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
894 }
895
896 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
897 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
898 }
899
900 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
901 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
902 }
903
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700905 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800906 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700907 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700908 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700910 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700911 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700912
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700913 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700914 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700915
Adam Cohenf814aa02011-09-01 13:48:05 -0700916 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700917
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700919 // We have saved the position to which the widget was dragged-- this really only matters
920 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700921 int[] cellXY = mTmpAddItemCellCoordinates;
922 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700923 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700924 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
925 cellXY[0] = mPendingAddInfo.cellX;
926 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700927 foundCellSpan = true;
928 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700929 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700930 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700931 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800932 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700933 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700934 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700935 }
936
Michael Jurka0280c3b2010-09-17 15:00:07 -0700937 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800938 if (appWidgetId != -1) {
939 // Deleting an app widget ID is a void call but writes to disk before returning
940 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800941 new Thread("deleteAppWidgetId") {
942 public void run() {
943 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
944 }
945 }.start();
946 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700947 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800948 return;
949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700951 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -0700952 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700953 launcherInfo.spanX = spanXY[0];
954 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700955
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700957 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958
959 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700961 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700962
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700963 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700965
Winson Chung3d503fb2011-07-13 17:25:49 -0700966 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400967 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700968
969 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 }
971 }
Romain Guycbb89e42009-06-08 15:52:54 -0700972
Adam Cohended9f8d2010-11-03 13:25:16 -0700973 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
974 @Override
975 public void onReceive(Context context, Intent intent) {
976 final String action = intent.getAction();
977 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
978 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700979 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700980 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700981
Winson Chungc100e8e2011-08-09 16:02:43 -0700982 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -0700983 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -0700984 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
985 mAppsCustomizeTabHost.reset();
986 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700987 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700988 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
989 mUserPresent = true;
990 updateRunning();
991 }
992 }
993 };
994
995 @Override
996 public void onAttachedToWindow() {
997 super.onAttachedToWindow();
998
999 // Listen for broadcasts related to user-presence
1000 final IntentFilter filter = new IntentFilter();
1001 filter.addAction(Intent.ACTION_SCREEN_OFF);
1002 filter.addAction(Intent.ACTION_USER_PRESENT);
1003 registerReceiver(mReceiver, filter);
1004
Adam Cohend113e0c2010-11-11 10:48:05 -08001005 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001006 mVisible = true;
1007 }
1008
1009 @Override
1010 public void onDetachedFromWindow() {
1011 super.onDetachedFromWindow();
1012 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001013 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001014
Adam Cohend113e0c2010-11-11 10:48:05 -08001015 if (mAttached) {
1016 unregisterReceiver(mReceiver);
1017 mAttached = false;
1018 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001019 updateRunning();
1020 }
1021
1022 public void onWindowVisibilityChanged(int visibility) {
1023 mVisible = visibility == View.VISIBLE;
1024 updateRunning();
1025 }
1026
1027 private void sendAdvanceMessage(long delay) {
1028 mHandler.removeMessages(ADVANCE_MSG);
1029 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1030 mHandler.sendMessageDelayed(msg, delay);
1031 mAutoAdvanceSentTime = System.currentTimeMillis();
1032 }
1033
1034 private void updateRunning() {
1035 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1036 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1037 mAutoAdvanceRunning = autoAdvanceRunning;
1038 if (autoAdvanceRunning) {
1039 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1040 sendAdvanceMessage(delay);
1041 } else {
1042 if (!mWidgetsToAdvance.isEmpty()) {
1043 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1044 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1045 }
1046 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001047 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001048 }
1049 }
1050 }
1051
1052 private final Handler mHandler = new Handler() {
1053 @Override
1054 public void handleMessage(Message msg) {
1055 if (msg.what == ADVANCE_MSG) {
1056 int i = 0;
1057 for (View key: mWidgetsToAdvance.keySet()) {
1058 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1059 final int delay = mAdvanceStagger * i;
1060 if (v instanceof Advanceable) {
1061 postDelayed(new Runnable() {
1062 public void run() {
1063 ((Advanceable) v).advance();
1064 }
1065 }, delay);
1066 }
1067 i++;
1068 }
1069 sendAdvanceMessage(mAdvanceInterval);
1070 }
1071 }
1072 };
1073
1074 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001075 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001076 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1077 if (v instanceof Advanceable) {
1078 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001079 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001080 updateRunning();
1081 }
1082 }
1083
1084 void removeWidgetToAutoAdvance(View hostView) {
1085 if (mWidgetsToAdvance.containsKey(hostView)) {
1086 mWidgetsToAdvance.remove(hostView);
1087 updateRunning();
1088 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001089 }
1090
Joe Onorato9c1289c2009-08-17 11:03:03 -04001091 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001092 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001093 launcherInfo.hostView = null;
1094 }
1095
Adam Cohended9f8d2010-11-03 13:25:16 -07001096 void showOutOfSpaceMessage() {
1097 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1098 }
1099
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001100 public LauncherAppWidgetHost getAppWidgetHost() {
1101 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 }
Romain Guycbb89e42009-06-08 15:52:54 -07001103
Winson Chunga9abd0e2010-10-27 17:18:37 -07001104 public LauncherModel getModel() {
1105 return mModel;
1106 }
1107
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001108 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001109 getWindow().closeAllPanels();
1110
Winson Chung87acb482011-08-23 17:28:05 -07001111 /**
1112 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001113 try {
1114 dismissDialog(DIALOG_CREATE_SHORTCUT);
1115 // Unlock the workspace if the dialog was showing
1116 } catch (Exception e) {
1117 // An exception is thrown if the dialog is not visible, which is fine
1118 }
1119
1120 try {
1121 dismissDialog(DIALOG_RENAME_FOLDER);
1122 // Unlock the workspace if the dialog was showing
1123 } catch (Exception e) {
1124 // An exception is thrown if the dialog is not visible, which is fine
1125 }
Winson Chung87acb482011-08-23 17:28:05 -07001126 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001127
1128 // Whatever we were doing is hereby canceled.
1129 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001130 }
1131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 @Override
1133 protected void onNewIntent(Intent intent) {
1134 super.onNewIntent(intent);
1135
1136 // Close the menu
1137 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001138 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001139 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001140
Adam Cohen95bb8002011-07-03 23:40:28 -07001141 closeFolder();
1142
Joe Onorato14f122b2009-11-19 14:06:36 -08001143 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1144 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001145
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001146 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001147 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001148 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001149 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001150 }
Winson Chungde1af762011-07-21 16:44:07 -07001151 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001152 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001153
Joe Onorato3a8820b2009-11-10 15:06:42 -08001154 final View v = getWindow().peekDecorView();
1155 if (v != null && v.getWindowToken() != null) {
1156 InputMethodManager imm = (InputMethodManager)getSystemService(
1157 INPUT_METHOD_SERVICE);
1158 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001160
Michael Jurka35aa14d2011-07-07 17:01:08 -07001161 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001162 if (mAppsCustomizeTabHost != null) {
1163 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001164 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165 }
1166 }
1167
1168 @Override
1169 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1170 // Do not call super here
1171 mSavedInstanceState = savedInstanceState;
1172 }
1173
1174 @Override
1175 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001176 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001177 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178
Michael Jurka883f55b2010-10-21 15:47:14 -07001179 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001180 // We close any open folder since it will not be re-opened, and we need to make sure
1181 // this state is reflected.
1182 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183
Winson Chung3d503fb2011-07-13 17:25:49 -07001184 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1185 mWaitingForResult) {
1186 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1187 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1188 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1189 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001190 }
1191
1192 if (mFolderInfo != null && mWaitingForResult) {
1193 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1194 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1195 }
Michael Jurka946ad472010-07-09 18:05:18 -07001196
Winson Chung785d2eb2011-04-14 16:08:02 -07001197 // Save the current AppsCustomize tab
1198 if (mAppsCustomizeTabHost != null) {
1199 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1200 if (currentTabTag != null) {
1201 outState.putString("apps_customize_currentTab", currentTabTag);
1202 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001203 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1204 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001205 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 }
1207
1208 @Override
1209 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001211
Winson Chunge7a03942011-08-05 15:05:12 -07001212 // Remove all pending runnables
1213 mHandler.removeMessages(ADVANCE_MSG);
1214 mHandler.removeMessages(0);
1215
Winson Chungcd2b0142011-06-08 16:02:26 -07001216 // Stop callbacks from LauncherModel
1217 LauncherApplication app = ((LauncherApplication) getApplication());
1218 mModel.stopLoader();
1219 app.setLauncher(null);
1220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001222 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001224 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001226 mAppWidgetHost = null;
1227
1228 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229
1230 TextKeyListener.getInstance().release();
1231
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001232
Winson Chung3d503fb2011-07-13 17:25:49 -07001233 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001234
1235 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001236 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001237
1238 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1239 mWorkspace.removeAllViews();
1240 mWorkspace = null;
1241 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001242
1243 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 }
1245
Adam Cohena9cf38f2011-05-02 15:36:58 -07001246 public DragController getDragController() {
1247 return mDragController;
1248 }
1249
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 @Override
1251 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001252 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 super.startActivityForResult(intent, requestCode);
1254 }
1255
Winson Chung70d72102011-08-12 11:24:35 -07001256 /**
1257 * Indicates that we want global search for this activity by setting the globalSearch
1258 * argument for {@link #startSearch} to true.
1259 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001261 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001263
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001264 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001265
Karl Rosaen138a0412009-04-23 19:00:21 -07001266 if (initialQuery == null) {
1267 // Use any text typed in the launcher as the initial query
1268 initialQuery = getTypedText();
1269 clearTypedText();
1270 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 if (appSearchData == null) {
1272 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001273 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 }
Romain Guycbb89e42009-06-08 15:52:54 -07001275
Karl Rosaen138a0412009-04-23 19:00:21 -07001276 final SearchManager searchManager =
1277 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001278 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001279 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 }
1281
Winson Chung70d72102011-08-12 11:24:35 -07001282 @Override
1283 public boolean onCreateOptionsMenu(Menu menu) {
1284 if (isWorkspaceLocked()) {
1285 return false;
1286 }
1287
1288 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001289
1290 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1291 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1292 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001293 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1294 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1295 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001296 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.help_url)));
1297 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1298 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1299
Winson Chung70d72102011-08-12 11:24:35 -07001300 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1301 .setIcon(android.R.drawable.ic_menu_gallery)
1302 .setAlphabeticShortcut('W');
1303 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1304 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001305 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001306 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001307 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1308 .setIcon(android.R.drawable.ic_menu_preferences)
1309 .setIntent(settings)
1310 .setAlphabeticShortcut('P');
Winson Chung70d72102011-08-12 11:24:35 -07001311 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1312 .setIcon(android.R.drawable.ic_menu_help)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001313 .setIntent(help)
Winson Chung70d72102011-08-12 11:24:35 -07001314 .setAlphabeticShortcut('H');
1315 return true;
1316 }
1317
1318 @Override
1319 public boolean onPrepareOptionsMenu(Menu menu) {
1320 super.onPrepareOptionsMenu(menu);
1321
1322 if (mAppsCustomizeTabHost.isTransitioning()) {
1323 return false;
1324 }
1325 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1326 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1327
1328 return true;
1329 }
1330
1331 @Override
1332 public boolean onOptionsItemSelected(MenuItem item) {
1333 switch (item.getItemId()) {
1334 case MENU_WALLPAPER_SETTINGS:
1335 startWallpaper();
1336 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001337 }
1338
1339 return super.onOptionsItemSelected(item);
1340 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001342 @Override
1343 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001344 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001345 // Use a custom animation for launching search
1346 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001347 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001348 }
1349
Joe Onorato9c1289c2009-08-17 11:03:03 -04001350 public boolean isWorkspaceLocked() {
1351 return mWorkspaceLoading || mWaitingForResult;
1352 }
1353
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001355 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001356 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 }
1358
Michael Jurka0280c3b2010-09-17 15:00:07 -07001359 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001360 mPendingAddInfo.container = ItemInfo.NO_ID;
1361 mPendingAddInfo.screen = -1;
1362 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1363 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1364 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001365 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001366
Michael Jurkaaf442092010-06-10 17:01:57 -07001367 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001368 // TODO: catch bad widget exception when sent
1369 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001370 // TODO: Is this log message meaningful?
1371 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001372 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001373 }
1374
Winson Chung55cef262010-10-28 14:14:18 -07001375 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001376 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377
Bjorn Bringert7984c942009-12-09 15:38:25 +00001378 if (appWidget.configure != null) {
1379 // Launch over to configure widget, if needed
1380 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1381 intent.setComponent(appWidget.configure);
1382 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001383 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001384 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1385 intent.putExtra(
1386 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1387 info.mimeType);
1388
1389 final String mimeType = info.mimeType;
1390 final ClipData clipData = (ClipData) info.configurationData;
1391 final ClipDescription clipDesc = clipData.getDescription();
1392 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1393 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001394 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001395 final CharSequence stringData = item.getText();
1396 final Uri uriData = item.getUri();
1397 final Intent intentData = item.getIntent();
1398 final String key =
1399 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1400 if (uriData != null) {
1401 intent.putExtra(key, uriData);
1402 } else if (intentData != null) {
1403 intent.putExtra(key, intentData);
1404 } else if (stringData != null) {
1405 intent.putExtra(key, stringData);
1406 }
1407 break;
1408 }
1409 }
1410 }
Winson Chung55cef262010-10-28 14:14:18 -07001411 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001412
Romain Guy8e633c52010-03-04 12:51:36 -08001413 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001415 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001416 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001417
1418 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001419 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001420 }
1421 }
Romain Guycbb89e42009-06-08 15:52:54 -07001422
Adam Cohenfbba09b2011-07-18 21:43:05 -07001423 /**
1424 * Process a shortcut drop.
1425 *
1426 * @param componentName The name of the component
1427 * @param screen The screen where it should be added
1428 * @param cell The cell it should be added to, optional
1429 * @param position The location on the screen where it was dropped, optional
1430 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001431 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1432 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001433 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001434 mPendingAddInfo.container = container;
1435 mPendingAddInfo.screen = screen;
1436 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001437
1438 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001439 mPendingAddInfo.cellX = cell[0];
1440 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001441 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001442
1443 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1444 createShortcutIntent.setComponent(componentName);
1445 processShortcut(createShortcutIntent);
1446 }
1447
Adam Cohenfbba09b2011-07-18 21:43:05 -07001448 /**
1449 * Process a widget drop.
1450 *
1451 * @param info The PendingAppWidgetInfo of the widget being added.
1452 * @param screen The screen where it should be added
1453 * @param cell The cell it should be added to, optional
1454 * @param position The location on the screen where it was dropped, optional
1455 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001456 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1457 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001458 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001459 mPendingAddInfo.container = info.container = container;
1460 mPendingAddInfo.screen = info.screen = screen;
1461 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001462 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001463 mPendingAddInfo.cellX = cell[0];
1464 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001465 }
1466
1467 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1468 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1469 addAppWidgetImpl(appWidgetId, info);
1470 }
1471
Joe Onoratodeb98af2010-02-19 14:59:39 -08001472 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001473 // Handle case where user selected "Applications"
1474 String applicationName = getResources().getString(R.string.group_applications);
1475 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001476
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001477 if (applicationName != null && applicationName.equals(shortcutName)) {
1478 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1479 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001480
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001481 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1482 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001483 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001484 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001485 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001486 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001487 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
1489
Winson Chung24ab2f12010-09-16 14:10:47 -07001490 void processWallpaper(Intent intent) {
1491 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1492 }
1493
Winson Chung3d503fb2011-07-13 17:25:49 -07001494 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1495 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001496 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 folderInfo.title = getText(R.string.folder_name);
1498
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001500 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1501 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001502 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503
1504 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001505 FolderIcon newFolder =
1506 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1507 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1508 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001509 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 }
Romain Guycbb89e42009-06-08 15:52:54 -07001511
Joe Onorato9c1289c2009-08-17 11:03:03 -04001512 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001513 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001514 }
1515
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 private void showNotifications() {
1517 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1518 if (statusBar != null) {
1519 statusBar.expand();
1520 }
1521 }
1522
1523 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001524 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001526 Intent chooser = Intent.createChooser(pickWallpaper,
1527 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001528 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1529 // Removed in Eclair MR1
1530// WallpaperManager wm = (WallpaperManager)
1531// getSystemService(Context.WALLPAPER_SERVICE);
1532// WallpaperInfo wi = wm.getWallpaperInfo();
1533// if (wi != null && wi.getSettingsActivity() != null) {
1534// LabeledIntent li = new LabeledIntent(getPackageName(),
1535// R.string.configure_wallpaper, 0);
1536// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1537// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1538// }
Mike Clerona0618e42009-10-22 13:55:21 -07001539 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
1541
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001542 /**
1543 * Registers various content observers. The current implementation registers
1544 * only a favorites observer to keep track of the favorites applications.
1545 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001546 private void registerContentObservers() {
1547 ContentResolver resolver = getContentResolver();
1548 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1549 true, mWidgetObserver);
1550 }
1551
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 @Override
1553 public boolean dispatchKeyEvent(KeyEvent event) {
1554 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1555 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001557 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001558 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001559 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001560 dumpState();
1561 return true;
1562 }
1563 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001564 }
1565 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1566 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001567 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001568 return true;
1569 }
1570 }
1571
1572 return super.dispatchKeyEvent(event);
1573 }
1574
Joe Onorato88ec0992009-11-19 13:16:06 -08001575 @Override
1576 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001577 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001578 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001579 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001580 Folder openFolder = mWorkspace.getOpenFolder();
1581 if (openFolder.isEditingName()) {
1582 openFolder.dismissEditingName();
1583 } else {
1584 closeFolder();
1585 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001586 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001587 mWorkspace.exitWidgetResizeMode();
1588
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001589 // Back button is a no-op here, but give at least some feedback for the button press
1590 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001591 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001592 }
1593
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001595 * Re-listen when widgets are reset.
1596 */
1597 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001598 if (mAppWidgetHost != null) {
1599 mAppWidgetHost.startListening();
1600 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001601 }
1602
1603 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001604 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1605 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001607 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001608 if (mModel != null) {
1609 mModel.unbindWorkspaceItems();
1610 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001612
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 /**
1614 * Launches the intent referred by the clicked shortcut.
1615 *
1616 * @param v The view representing the clicked shortcut.
1617 */
1618 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001619 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1620 // view has detached (it's possible for this to happen if the view is removed mid touch).
1621 if (v.getWindowToken() == null) {
1622 return;
1623 }
1624
1625 if (mWorkspace.isSwitchingState()) {
1626 return;
1627 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001628
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001630 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001632 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001633 int[] pos = new int[2];
1634 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001635 intent.setSourceBounds(new Rect(pos[0], pos[1],
1636 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001637 boolean success = startActivitySafely(intent, tag);
1638
1639 if (success && v instanceof BubbleTextView) {
1640 mWaitingForResume = (BubbleTextView) v;
1641 mWaitingForResume.setStayPressed(true);
1642 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001644 if (v instanceof FolderIcon) {
1645 FolderIcon fi = (FolderIcon) v;
1646 handleFolderClick(fi);
1647 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001648 } else if (v == mAllAppsButton) {
1649 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001650 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001651 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001652 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001653 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654 }
1655 }
1656
Michael Jurka0e260592010-06-30 17:07:39 -07001657 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001658 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001659 // clicking anywhere on the workspace causes the customization drawer to slide down
1660 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001661 return false;
1662 }
1663
Michael Jurkaaf442092010-06-10 17:01:57 -07001664 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001665 * Event handler for the search button
1666 *
1667 * @param v The view that was clicked.
1668 */
1669 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001670 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001671 }
1672
1673 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001674 * Event handler for the voice button
1675 *
1676 * @param v The view that was clicked.
1677 */
1678 public void onClickVoiceButton(View v) {
1679 startVoiceSearch();
1680 }
1681
1682 private void startVoiceSearch() {
1683 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001684 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001685 startActivity(intent);
1686 }
1687
1688 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001689 * Event handler for the "grid" button that appears on the home screen, which
1690 * enters all apps mode.
1691 *
1692 * @param v The view that was clicked.
1693 */
1694 public void onClickAllAppsButton(View v) {
1695 showAllApps(true);
1696 }
1697
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001698 public void onClickAppMarketButton(View v) {
1699 if (mAppMarketIntent != null) {
1700 startActivitySafely(mAppMarketIntent, "app market");
1701 }
1702 }
1703
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001704 void startApplicationDetailsActivity(ComponentName componentName) {
1705 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001706 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1707 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001708 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001709 startActivity(intent);
1710 }
1711
Patrick Dubroy5539af72010-09-07 15:22:01 -07001712 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1713 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1714 // System applications cannot be installed. For now, show a toast explaining that.
1715 // We may give them the option of disabling apps this way.
1716 int messageId = R.string.uninstall_system_app_text;
1717 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1718 } else {
1719 String packageName = appInfo.componentName.getPackageName();
1720 String className = appInfo.componentName.getClassName();
1721 Intent intent = new Intent(
1722 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001723 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1724 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001725 startActivity(intent);
1726 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001727 }
1728
Michael Jurkaddd62e92011-02-16 17:49:14 -08001729 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001730 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1731 try {
1732 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001733 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 } catch (ActivityNotFoundException e) {
1735 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001736 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 } catch (SecurityException e) {
1738 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001739 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001741 "or use the exported attribute for this activity. "
1742 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001743 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001744 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001746
Romain Guy8e633c52010-03-04 12:51:36 -08001747 void startActivityForResultSafely(Intent intent, int requestCode) {
1748 try {
1749 startActivityForResult(intent, requestCode);
1750 } catch (ActivityNotFoundException e) {
1751 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1752 } catch (SecurityException e) {
1753 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1754 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1755 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1756 "or use the exported attribute for this activity.", e);
1757 }
1758 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759
Adam Cohena9cf38f2011-05-02 15:36:58 -07001760 private void handleFolderClick(FolderIcon folderIcon) {
1761 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001762 Folder openFolder = mWorkspace.getFolderForTag(info);
1763
1764 // If the folder info reports that the associated folder is open, then verify that
1765 // it is actually opened. There have been a few instances where this gets out of sync.
1766 if (info.opened && openFolder == null) {
1767 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1768 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1769 info.opened = false;
1770 }
1771
Adam Cohena9cf38f2011-05-02 15:36:58 -07001772 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 // Close any open folder
1774 closeFolder();
1775 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001776 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 } else {
1778 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 int folderScreen;
1780 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001781 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 // .. and close it
1783 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001784 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 // Close any folder open on the current screen
1786 closeFolder();
1787 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001788 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 }
1790 }
1791 }
1792 }
1793
Adam Cohen2801caf2011-05-13 20:57:39 -07001794 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001795 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001796 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1797 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1798 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1799
Adam Cohenc51934b2011-07-26 21:07:43 -07001800 FolderInfo info = (FolderInfo) fi.getTag();
1801 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1802 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001803 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1804 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001805 }
1806
Adam Cohen2801caf2011-05-13 20:57:39 -07001807 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1808 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1809 oa.start();
1810 }
1811
1812 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001813 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001814 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1815 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1816 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1817
Adam Cohenc51934b2011-07-26 21:07:43 -07001818 FolderInfo info = (FolderInfo) fi.getTag();
1819 CellLayout cl = null;
1820 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1821 cl = (CellLayout) fi.getParent().getParent();
1822 }
1823
1824 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001825 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1826 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001827 oa.addListener(new AnimatorListenerAdapter() {
1828 @Override
1829 public void onAnimationEnd(Animator animation) {
1830 if (layout != null) {
1831 layout.clearFolderLeaveBehind();
1832 }
1833 }
1834 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001835 oa.start();
1836 }
1837
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001839 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 * is animated relative to the specified View. If the View is null, no animation
1841 * is played.
1842 *
1843 * @param folderInfo The FolderInfo describing the folder to open.
1844 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001845 public void openFolder(FolderIcon folderIcon) {
1846 Folder folder = folderIcon.mFolder;
1847 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848
Adam Cohen2801caf2011-05-13 20:57:39 -07001849 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001850 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001851
Adam Cohen4554ee12011-08-03 16:13:21 -07001852 // Just verify that the folder hasn't already been added to the DragLayer.
1853 // There was a one-off crash where the folder had a parent already.
1854 if (folder.getParent() == null) {
1855 mDragLayer.addView(folder);
1856 mDragController.addDropTarget((DropTarget) folder);
1857 } else {
1858 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1859 folder.getParent() + ").");
1860 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001861 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001862 }
1863
1864 public void closeFolder() {
1865 Folder folder = mWorkspace.getOpenFolder();
1866 if (folder != null) {
1867 closeFolder(folder);
1868 }
1869 }
1870
1871 void closeFolder(Folder folder) {
1872 folder.getInfo().opened = false;
1873
1874 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1875 if (parent != null) {
1876 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1877 shrinkAndFadeInFolderIcon(fi);
1878 }
1879 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 }
1881
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001883 if (mState != State.WORKSPACE) {
1884 return false;
1885 }
1886
Joe Onorato9c1289c2009-08-17 11:03:03 -04001887 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 return false;
1889 }
1890
1891 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001892 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 }
1894
Michael Jurka0280c3b2010-09-17 15:00:07 -07001895 resetAddInfo();
1896 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001898 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 return true;
1900 }
1901
Winson Chung3d503fb2011-07-13 17:25:49 -07001902 // The hotseat touch handling does not go through Workspace, and we always allow long press
1903 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001904 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001905 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1906 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001907 if (itemUnderLongClick == null) {
1908 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001909 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1910 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001911 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001913 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001915 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 }
1917 }
1918 }
1919 return true;
1920 }
1921
Winson Chung3d503fb2011-07-13 17:25:49 -07001922 boolean isHotseatLayout(View layout) {
1923 return mHotseat != null && layout != null &&
1924 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1925 }
1926 Hotseat getHotseat() {
1927 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001928 }
1929
Winson Chung3d503fb2011-07-13 17:25:49 -07001930 /**
1931 * Returns the CellLayout of the specified container at the specified screen.
1932 */
1933 CellLayout getCellLayout(long container, int screen) {
1934 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1935 if (mHotseat != null) {
1936 return mHotseat.getLayout();
1937 } else {
1938 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001939 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001940 } else {
1941 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001942 }
1943 }
1944
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 Workspace getWorkspace() {
1946 return mWorkspace;
1947 }
1948
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 @Override
1950 protected Dialog onCreateDialog(int id) {
1951 switch (id) {
1952 case DIALOG_CREATE_SHORTCUT:
1953 return new CreateShortcut().createDialog();
1954 case DIALOG_RENAME_FOLDER:
1955 return new RenameFolder().createDialog();
1956 }
1957
1958 return super.onCreateDialog(id);
1959 }
1960
1961 @Override
1962 protected void onPrepareDialog(int id, Dialog dialog) {
1963 switch (id) {
1964 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 break;
1966 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001967 if (mFolderInfo != null) {
1968 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1969 final CharSequence text = mFolderInfo.title;
1970 input.setText(text);
1971 input.setSelection(0, text.length());
1972 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 break;
1974 }
1975 }
1976
1977 void showRenameDialog(FolderInfo info) {
1978 mFolderInfo = info;
1979 mWaitingForResult = true;
1980 showDialog(DIALOG_RENAME_FOLDER);
1981 }
1982
Adam Cohenfbba09b2011-07-18 21:43:05 -07001983 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001984 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001985 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1986 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 mWaitingForResult = true;
1988 showDialog(DIALOG_CREATE_SHORTCUT);
1989 }
1990
1991 private class RenameFolder {
1992 private EditText mInput;
1993
1994 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
1996 mInput = (EditText) layout.findViewById(R.id.folder_name);
1997
1998 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
1999 builder.setIcon(0);
2000 builder.setTitle(getString(R.string.rename_folder_title));
2001 builder.setCancelable(true);
2002 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2003 public void onCancel(DialogInterface dialog) {
2004 cleanup();
2005 }
2006 });
2007 builder.setNegativeButton(getString(R.string.cancel_action),
2008 new Dialog.OnClickListener() {
2009 public void onClick(DialogInterface dialog, int which) {
2010 cleanup();
2011 }
2012 }
2013 );
2014 builder.setPositiveButton(getString(R.string.rename_action),
2015 new Dialog.OnClickListener() {
2016 public void onClick(DialogInterface dialog, int which) {
2017 changeFolderName();
2018 }
2019 }
2020 );
2021 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002022
2023 final AlertDialog dialog = builder.create();
2024 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2025 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002026 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002027 mInput.requestFocus();
2028 InputMethodManager inputManager = (InputMethodManager)
2029 getSystemService(Context.INPUT_METHOD_SERVICE);
2030 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002031 }
2032 });
2033
2034 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 }
2036
2037 private void changeFolderName() {
2038 final String name = mInput.getText().toString();
2039 if (!TextUtils.isEmpty(name)) {
2040 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002041 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 mFolderInfo.title = name;
2043 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2044
Joe Onorato9c1289c2009-08-17 11:03:03 -04002045 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002046 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002047 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 } else {
2049 final FolderIcon folderIcon = (FolderIcon)
2050 mWorkspace.getViewForTag(mFolderInfo);
2051 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002052 // TODO: At some point we'll probably want some version of setting
2053 // the text for a folder icon.
2054 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 getWorkspace().requestLayout();
2056 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002057 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002058 mWorkspaceLoading = true;
2059 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 }
2061 }
2062 }
2063 cleanup();
2064 }
2065
2066 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 dismissDialog(DIALOG_RENAME_FOLDER);
2068 mWaitingForResult = false;
2069 mFolderInfo = null;
2070 }
2071 }
2072
Daniel Sandler843e8602010-06-07 14:59:01 -04002073 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2074 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002075 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002076 }
2077
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002078 // AllAppsView.Watcher
2079 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002080 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002081 mWorkspace.setVisibility(View.GONE);
2082 }
2083 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002084
2085 /**
2086 * Helper method for the cameraZoomIn/cameraZoomOut animations
2087 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002088 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002089 * @param scaleFactor The scale factor used for the zoom
2090 */
2091 private void setPivotsForZoom(View view, State state, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002092 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002093 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002094 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002095
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002096 void updateWallpaperVisibility(boolean visible) {
2097 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2098 int curflags = getWindow().getAttributes().flags
2099 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2100 if (wpflags != curflags) {
2101 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2102 }
2103 }
2104
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002105 /**
2106 * Zoom the camera out from the workspace to reveal 'toView'.
2107 * Assumes that the view to show is anchored at either the very top or very bottom
2108 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002109 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002110 */
Winson Chungc07918d2011-07-01 15:35:26 -07002111 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002112 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002113
Winson Chungf0ea4d32011-06-06 14:27:16 -07002114 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2115 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2116 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2117 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002118 final int startDelay =
2119 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002120
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002121 setPivotsForZoom(toView, toState, scale);
2122
Michael Jurkad74c9842011-07-10 12:44:21 -07002123 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002124 mWorkspace.changeState(Workspace.State.SMALL, animated);
Adam Cohencff6af82011-09-13 14:51:53 -07002125 //hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002126
2127 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002128 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002129 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002130 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2131 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002132 ((View) toView.getParent()).invalidate();
2133 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002134 toView.setFastScaleX(a * scale + b * 1f);
2135 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002136 }
2137 });
Adam Cohen61033d32010-11-15 18:29:44 -08002138
Winson Chungf0ea4d32011-06-06 14:27:16 -07002139 toView.setVisibility(View.VISIBLE);
2140 toView.setFastAlpha(0f);
2141 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2142 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2143 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2144 public void onAnimationUpdate(float a, float b) {
2145 // don't need to invalidate because we do so above
2146 toView.setFastAlpha(a * 0f + b * 1f);
2147 }
2148 });
Adam Cohencff6af82011-09-13 14:51:53 -07002149 alphaAnim.setStartDelay(startDelay);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002150 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002151
Michael Jurkaabded662011-03-04 12:06:57 -08002152 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002153 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim, false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002154 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002155 scaleAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002156 boolean animationCancelled = false;
2157
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002158 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002159 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002160 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002161 // Prepare the position
2162 toView.setTranslationX(0.0f);
2163 toView.setTranslationY(0.0f);
2164 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002165 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002166 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002167 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002168 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002169 // If we don't set the final scale values here, if this animation is cancelled
2170 // it will have the wrong scale value and subsequent cameraPan animations will
2171 // not fix that
2172 toView.setScaleX(1.0f);
2173 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002174 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002175 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002176 }
Winson Chung32174c82011-07-19 15:47:55 -07002177
2178 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2179 // Hide the workspace scrollbar
2180 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002181 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002182 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002183 if (!animationCancelled) {
2184 updateWallpaperVisibility(false);
2185 }
2186 }
2187
Adam Cohencff6af82011-09-13 14:51:53 -07002188 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002189 public void onAnimationCancel(Animator animation) {
2190 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002191 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002192 });
2193
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002194 // toView should appear right at the end of the workspace shrink animation
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002195
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002196 if (mStateAnimation != null) mStateAnimation.cancel();
2197 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002198 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002199 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002200 } else {
2201 toView.setTranslationX(0.0f);
2202 toView.setTranslationY(0.0f);
2203 toView.setScaleX(1.0f);
2204 toView.setScaleY(1.0f);
2205 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002206 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002207 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002208 ((LauncherTransitionable) toView).onLauncherTransitionStart(null, false);
2209 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002210
Winson Chungc07918d2011-07-01 15:35:26 -07002211 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2212 // Hide the workspace scrollbar
2213 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002214 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002215 }
Michael Jurkaabded662011-03-04 12:06:57 -08002216 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002217 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002218 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002219 }
2220
2221 /**
2222 * Zoom the camera back into the workspace, hiding 'fromView'.
2223 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002224 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002225 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002226 */
Winson Chungc07918d2011-07-01 15:35:26 -07002227 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002228 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002229
Winson Chungf0ea4d32011-06-06 14:27:16 -07002230 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2231 final float scaleFactor = (float)
2232 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2233 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002234
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002235 setPivotsForZoom(fromView, fromState, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002236 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002237 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002238 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002239 if (mStateAnimation != null) mStateAnimation.cancel();
2240 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002241
Michael Jurka742574b2011-02-02 23:51:01 -08002242 final float oldScaleX = fromView.getScaleX();
2243 final float oldScaleY = fromView.getScaleY();
2244
2245 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2246 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002247 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2248 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002249 ((View)fromView.getParent()).fastInvalidate();
2250 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2251 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2252 }
2253 });
Michael Jurkaabded662011-03-04 12:06:57 -08002254 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002255 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Adam Cohencff6af82011-09-13 14:51:53 -07002256 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002257 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2258 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002259 // don't need to invalidate because we do so above
2260 fromView.setFastAlpha(a * 1f + b * 0f);
2261 }
2262 });
Michael Jurkaabded662011-03-04 12:06:57 -08002263 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002264 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002265 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002266 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002267 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002268 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002269 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002270 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002271 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002272 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim,true);
Michael Jurka2763be32011-02-24 11:19:57 -08002273 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002274 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002275 }
2276 });
2277
Winson Chungf0ea4d32011-06-06 14:27:16 -07002278 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002279 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002280 } else {
2281 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002282 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002283 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null, true);
2284 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002285 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002286 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002287 }
2288
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002289 void showWorkspace(boolean animated) {
Adam Cohencff6af82011-09-13 14:51:53 -07002290 Resources res = getResources();
2291 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2292
2293 mWorkspace.changeState(Workspace.State.NORMAL, animated, stagger);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002294 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002295 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002296 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002297
Adam Cohen7777d962011-08-18 18:58:38 -07002298 mWorkspace.showDockDivider(!animated);
2299 mWorkspace.flashScrollingIndicator();
2300
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002301 // Change the state *after* we've called all the transition code
2302 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002303
Winson Chung5fb63472011-02-02 17:03:37 -08002304 // Resume the auto-advance of widgets
2305 mUserPresent = true;
2306 updateRunning();
2307
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002308 // send an accessibility event to announce the context change
2309 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002310 }
2311
Adam Cohen7777d962011-08-18 18:58:38 -07002312 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002313 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002314 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Winson Chungb26f3d62011-06-02 10:49:29 -07002315 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002316 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002317 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002318 }
Adam Cohen7777d962011-08-18 18:58:38 -07002319
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002320 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chung09bfc452011-09-09 11:30:20 -07002321 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2322
Winson Chunge7a03942011-08-05 15:05:12 -07002323 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002324 @Override
2325 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002326 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002327 // Before we show workspace, hide all apps again because
2328 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2329 // clean up our state transition functions
2330 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohen7777d962011-08-18 18:58:38 -07002331 mSearchDeleteBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002332 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002333 } else {
2334 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002335 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002336 }
2337 }, (extendedDelay ?
2338 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2339 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2340 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002341 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002342 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002343 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2344 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002345 }
2346 // Otherwise, we are not in spring loaded mode, so don't do anything.
2347 }
2348
Adam Cohenfc53cd22011-07-20 15:45:11 -07002349 public boolean isAllAppsCustomizeOpen() {
2350 return mState == State.APPS_CUSTOMIZE;
2351 }
2352
Winson Chungf0ea4d32011-06-06 14:27:16 -07002353 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002354 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002355 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002356 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002357 if (!LauncherApplication.isScreenLarge()) {
2358 if (animated) {
2359 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002360 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002361 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002362 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002363 }
2364 }
2365 }
2366
2367 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002368 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002369 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002370 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002371 if (!LauncherApplication.isScreenLarge()) {
2372 if (animated) {
2373 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002374 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002375 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002376 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002377 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002378 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002379 }
2380
Winson Chung785d2eb2011-04-14 16:08:02 -07002381 void showAllApps(boolean animated) {
2382 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002383
Winson Chungf0ea4d32011-06-06 14:27:16 -07002384 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2385 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002386
Winson Chung3d503fb2011-07-13 17:25:49 -07002387 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002388 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002389
Winson Chungf0ea4d32011-06-06 14:27:16 -07002390 // Change the state *after* we've called all the transition code
2391 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002392
2393 // Pause the auto-advance of widgets until we are out of AllApps
2394 mUserPresent = false;
2395 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002396 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002397
2398 // Send an accessibility event to announce the context change
2399 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2400 }
2401
Joe Onoratob2061212009-11-24 16:13:54 -05002402 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002403 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002404 * - Home from workspace
2405 * - from center screen
2406 * - from other screens
2407 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002408 * - from center screen
2409 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002410 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002411 * - from center screen
2412 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002413 * - Launch app from workspace and quit
2414 * - with back
2415 * - with home
2416 * - Launch app from all apps and quit
2417 * - with back
2418 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002419 * - Go to a screen that's not the default, then all
2420 * apps, and launch and app, and go back
2421 * - with back
2422 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002423 * - On workspace, long press power and go back
2424 * - with back
2425 * - with home
2426 * - On all apps, long press power and go back
2427 * - with back
2428 * - with home
2429 * - On workspace, power off
2430 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002431 * - Launch an app and turn off the screen while in that app
2432 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002433 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002434 * - From all apps
2435 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002436 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2437 * - From all apps
2438 * - From the center workspace
2439 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002440 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002441 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002442 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2443 mWorkspace.setVisibility(View.VISIBLE);
2444 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002445
Winson Chung3d503fb2011-07-13 17:25:49 -07002446 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002447 mSearchDeleteBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002448
Winson Chungf0ea4d32011-06-06 14:27:16 -07002449 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002450 if (mAllAppsButton != null) {
2451 mAllAppsButton.requestFocus();
2452 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002453 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002454 }
2455
Joe Onorato7c312c12009-08-13 21:36:53 -07002456 void lockAllApps() {
2457 // TODO
2458 }
2459
2460 void unlockAllApps() {
2461 // TODO
2462 }
2463
Patrick Dubroy5f445422011-02-18 14:35:21 -08002464 /**
2465 * Add an item from all apps or customize onto the given workspace screen.
2466 * If layout is null, add to the current screen.
2467 */
2468 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002469 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2470 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002471 } else {
2472 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002473 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002474 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002475
Winson Chungdff8ebb2011-09-08 17:25:31 -07002476 /** Maps the current orientation to an index for referencing orientation correct global icons */
2477 private int getCurrentOrientationIndexForGlobalIcons() {
2478 // default - 0, landscape - 1
2479 switch (getResources().getConfiguration().orientation) {
2480 case Configuration.ORIENTATION_LANDSCAPE:
2481 return 1;
2482 default:
2483 return 0;
2484 }
2485 }
2486
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002487 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002488 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002489 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002490 // Look for the toolbar icon specified in the activity meta-data
2491 Bundle metaData = packageManager.getActivityInfo(
2492 activityName, PackageManager.GET_META_DATA).metaData;
2493 if (metaData != null) {
2494 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2495 if (iconResId != 0) {
2496 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002497 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002498 }
2499 }
2500 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002501 // This can happen if the activity defines an invalid drawable
2502 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2503 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002504 } catch (Resources.NotFoundException nfe) {
2505 // This can happen if the activity defines an invalid drawable
2506 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2507 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002508 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002509 return null;
2510 }
2511
2512 // if successful in getting icon, return it; otherwise, set button to use default drawable
2513 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2514 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2515 TextView button = (TextView) findViewById(buttonId);
2516 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002517 Resources r = getResources();
2518 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2519 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002520
2521 // If we were unable to find the icon via the meta-data, use a generic one
2522 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002523 toolbarIcon = r.getDrawable(fallbackDrawableId);
2524 toolbarIcon.setBounds(0, 0, w, h);
2525 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002526 return null;
2527 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002528 toolbarIcon.setBounds(0, 0, w, h);
2529 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002530 return toolbarIcon.getConstantState();
2531 }
2532 }
2533
2534 // if successful in getting icon, return it; otherwise, set button to use default drawable
2535 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2536 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2537 ImageView button = (ImageView) findViewById(buttonId);
2538 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2539
Michael Jurka19e0fc52011-07-22 18:00:21 -07002540 if (button != null) {
2541 // If we were unable to find the icon via the meta-data, use a
2542 // generic one
2543 if (toolbarIcon == null) {
2544 button.setImageResource(fallbackDrawableId);
2545 } else {
2546 button.setImageDrawable(toolbarIcon);
2547 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002548 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002549
2550 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2551
Michael Jurka0423dcf2010-10-05 14:56:18 -07002552 }
2553
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002554 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2555 TextView button = (TextView) findViewById(buttonId);
2556 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2557 }
2558
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002559 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002560 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002561 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002562 }
2563
Michael Jurka0423dcf2010-10-05 14:56:18 -07002564 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002565 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2566 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002567 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002568
Winson Chung1cad91e2011-05-25 17:41:01 -07002569 final SearchManager searchManager =
2570 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2571 ComponentName activityName = searchManager.getGlobalSearchActivity();
2572 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002573 int coi = getCurrentOrientationIndexForGlobalIcons();
2574 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002575 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002576 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002577 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002578 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002579 // We disable both search and voice search when there is no global search provider
Winson Chung1cad91e2011-05-25 17:41:01 -07002580 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002581 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002582 voiceButton.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002583 }
2584 }
2585
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002586 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002587 updateButtonWithDrawable(R.id.search_button, d);
2588 }
2589
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002590 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002591 final View searchDivider = findViewById(R.id.search_divider);
2592 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002593
Winson Chung1cad91e2011-05-25 17:41:01 -07002594 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2595 ComponentName activityName = intent.resolveActivity(getPackageManager());
2596 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002597 int coi = getCurrentOrientationIndexForGlobalIcons();
2598 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002599 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2600 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002601 voiceButton.setVisibility(View.VISIBLE);
2602 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002603 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002604 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002605 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002606 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002607
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002608 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002609 updateButtonWithDrawable(R.id.voice_button, d);
2610 }
2611
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002612 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002613 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002614 */
2615 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002616 final View marketButton = findViewById(R.id.market_button);
2617 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2618 // Find the app market activity by resolving an intent.
2619 // (If multiple app markets are installed, it will return the ResolverActivity.)
2620 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002621 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002622 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002623 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002624 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002625 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002626 marketButton.setVisibility(View.VISIBLE);
2627 } else {
2628 // We should hide and disable the view so that we don't try and restore the visibility
2629 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2630 marketButton.setVisibility(View.GONE);
2631 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002632 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002633 }
2634
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002635 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002636 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002637 }
2638
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002639 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002640 * Displays the shortcut creation dialog and launches, if necessary, the
2641 * appropriate activity.
2642 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002643 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002644 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2645 DialogInterface.OnShowListener {
2646
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002647 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002648
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002649 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002650 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002651
Winson Chung55b65502011-05-26 12:03:43 -07002652 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2653 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002654 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002655
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002656 AlertDialog dialog = builder.create();
2657 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002658 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002659 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002661 return dialog;
2662 }
2663
2664 public void onCancel(DialogInterface dialog) {
2665 mWaitingForResult = false;
2666 cleanup();
2667 }
2668
Romain Guycbb89e42009-06-08 15:52:54 -07002669 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002670 mWaitingForResult = false;
2671 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002672 }
2673
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002674 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002675 try {
2676 dismissDialog(DIALOG_CREATE_SHORTCUT);
2677 } catch (Exception e) {
2678 // An exception is thrown if the dialog is not visible, which is fine
2679 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002680 }
2681
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002682 /**
2683 * Handle the action clicked in the "Add to home" dialog.
2684 */
2685 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002686 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002687
Winson Chung55b65502011-05-26 12:03:43 -07002688 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2689 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002690 case AddAdapter.ITEM_APPLICATION: {
2691 if (mAppsCustomizeTabHost != null) {
2692 mAppsCustomizeTabHost.selectAppsTab();
2693 }
2694 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002695 break;
2696 }
Winson Chung55b65502011-05-26 12:03:43 -07002697 case AddAdapter.ITEM_APPWIDGET: {
2698 if (mAppsCustomizeTabHost != null) {
2699 mAppsCustomizeTabHost.selectWidgetsTab();
2700 }
2701 showAllApps(true);
2702 break;
2703 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002704 case AddAdapter.ITEM_WALLPAPER: {
2705 startWallpaper();
2706 break;
2707 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002708 }
2709 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002710
2711 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002712 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002714 }
2715
2716 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002717 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002718 */
2719 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2720 @Override
2721 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002722 closeSystemDialogs();
2723 }
2724 }
2725
2726 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002727 * Receives notifications whenever the appwidgets are reset.
2728 */
2729 private class AppWidgetResetObserver extends ContentObserver {
2730 public AppWidgetResetObserver() {
2731 super(new Handler());
2732 }
2733
2734 @Override
2735 public void onChange(boolean selfChange) {
2736 onAppWidgetReset();
2737 }
2738 }
2739
2740 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002741 * If the activity is currently paused, signal that we need to re-run the loader
2742 * in onResume.
2743 *
2744 * This needs to be called from incoming places where resources might have been loaded
2745 * while we are paused. That is becaues the Configuration might be wrong
2746 * when we're not running, and if it comes back to what it was when we
2747 * were paused, we are not restarted.
2748 *
2749 * Implementation of the method from LauncherModel.Callbacks.
2750 *
2751 * @return true if we are currently paused. The caller might be able to
2752 * skip some work in that case since we will come back again.
2753 */
2754 public boolean setLoadOnResume() {
2755 if (mPaused) {
2756 Log.i(TAG, "setLoadOnResume");
2757 mOnResumeNeedsLoad = true;
2758 return true;
2759 } else {
2760 return false;
2761 }
2762 }
2763
2764 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002765 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002766 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002767 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002768 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002769 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002770 } else {
2771 return SCREEN_COUNT / 2;
2772 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002773 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002774
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002775
Joe Onorato9c1289c2009-08-17 11:03:03 -04002776 /**
2777 * Refreshes the shortcuts shown on the workspace.
2778 *
2779 * Implementation of the method from LauncherModel.Callbacks.
2780 */
2781 public void startBinding() {
2782 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002783
2784 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002785 int count = workspace.getChildCount();
2786 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002787 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002788 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2789 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002790 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002791 if (mHotseat != null) {
2792 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002793 }
2794 }
2795
2796 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002797 * Bind the items start-end from the list.
2798 *
2799 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002800 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002801 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002802 setLoadOnResume();
2803
Joe Onorato9c1289c2009-08-17 11:03:03 -04002804 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002805 for (int i=start; i<end; i++) {
2806 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002807
2808 // Short circuit if we are loading dock items for a configuration which has no dock
2809 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2810 mHotseat == null) {
2811 continue;
2812 }
2813
Joe Onorato9c1289c2009-08-17 11:03:03 -04002814 switch (item.itemType) {
2815 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2816 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002817 View shortcut = createShortcut((ShortcutInfo)item);
2818 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2819 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002820 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002821 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002822 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002823 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002824 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002825 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2826 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002827 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002828 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002829 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002830 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002831 }
2832
Joe Onorato9c1289c2009-08-17 11:03:03 -04002833 /**
2834 * Implementation of the method from LauncherModel.Callbacks.
2835 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002836 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002837 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002838 sFolders.clear();
2839 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002840 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002841
2842 /**
2843 * Add the views for a widget to the workspace.
2844 *
2845 * Implementation of the method from LauncherModel.Callbacks.
2846 */
2847 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002848 setLoadOnResume();
2849
Daniel Sandler843e8602010-06-07 14:59:01 -04002850 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2851 if (DEBUG_WIDGETS) {
2852 Log.d(TAG, "bindAppWidget: " + item);
2853 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002854 final Workspace workspace = mWorkspace;
2855
2856 final int appWidgetId = item.appWidgetId;
2857 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002858 if (DEBUG_WIDGETS) {
2859 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2860 }
2861
Joe Onorato9c1289c2009-08-17 11:03:03 -04002862 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2863
Joe Onorato9c1289c2009-08-17 11:03:03 -04002864 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2865 item.hostView.setTag(item);
2866
Winson Chung3d503fb2011-07-13 17:25:49 -07002867 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002868 item.cellY, item.spanX, item.spanY, false);
2869
Adam Cohended9f8d2010-11-03 13:25:16 -07002870 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2871
Joe Onorato9c1289c2009-08-17 11:03:03 -04002872 workspace.requestLayout();
2873
Daniel Sandler843e8602010-06-07 14:59:01 -04002874 if (DEBUG_WIDGETS) {
2875 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2876 + (SystemClock.uptimeMillis()-start) + "ms");
2877 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002878 }
2879
2880 /**
2881 * Callback saying that there aren't any more items to bind.
2882 *
2883 * Implementation of the method from LauncherModel.Callbacks.
2884 */
2885 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002886 setLoadOnResume();
2887
Joe Onorato9c1289c2009-08-17 11:03:03 -04002888 if (mSavedState != null) {
2889 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002890 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002891 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002892 mSavedState = null;
2893 }
2894
2895 if (mSavedInstanceState != null) {
2896 super.onRestoreInstanceState(mSavedInstanceState);
2897 mSavedInstanceState = null;
2898 }
2899
Joe Onorato9c1289c2009-08-17 11:03:03 -04002900 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002901
2902 // If we received the result of any pending adds while the loader was running (e.g. the
2903 // widget configuration forced an orientation change), process them now.
2904 for (int i = 0; i < sPendingAddList.size(); i++) {
2905 completeAdd(sPendingAddList.get(i));
2906 }
2907 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002908 }
2909
2910 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002911 * Updates the icons on the launcher that are affected by changes to the package list
2912 * on the device.
2913 */
2914 private void updateIconsAffectedByPackageManagerChanges() {
2915 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002916 updateVoiceSearchIcon();
2917 }
2918
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002919 @Override
2920 public void bindSearchablesChanged() {
2921 updateGlobalSearchIcon();
2922 }
2923
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002924 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002925 * Add the icons for all apps.
2926 *
2927 * Implementation of the method from LauncherModel.Callbacks.
2928 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002929 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2930 // Remove the progress bar entirely; we could also make it GONE
2931 // but better to remove it since we know it's not going to be used
2932 View progressBar = mAppsCustomizeTabHost.
2933 findViewById(R.id.apps_customize_progress_bar);
2934 if (progressBar != null) {
2935 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002936 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002937 // We just post the call to setApps so the user sees the progress bar
2938 // disappear-- otherwise, it just looks like the progress bar froze
2939 // which doesn't look great
2940 mAppsCustomizeTabHost.post(new Runnable() {
2941 public void run() {
2942 if (mAppsCustomizeContent != null) {
2943 mAppsCustomizeContent.setApps(apps);
2944 }
2945 }
2946 });
2947
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002948 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002949 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002950 }
2951
2952 /**
2953 * A package was installed.
2954 *
2955 * Implementation of the method from LauncherModel.Callbacks.
2956 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002957 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002958 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002959 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002960
Winson Chung785d2eb2011-04-14 16:08:02 -07002961 if (mAppsCustomizeContent != null) {
2962 mAppsCustomizeContent.addApps(apps);
2963 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002964 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002965 }
2966
2967 /**
2968 * A package was updated.
2969 *
2970 * Implementation of the method from LauncherModel.Callbacks.
2971 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002972 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002973 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002974 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08002975 if (mWorkspace != null) {
2976 mWorkspace.updateShortcuts(apps);
2977 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002978
Winson Chung785d2eb2011-04-14 16:08:02 -07002979 if (mAppsCustomizeContent != null) {
2980 mAppsCustomizeContent.updateApps(apps);
2981 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002982 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002983 }
2984
2985 /**
2986 * A package was uninstalled.
2987 *
2988 * Implementation of the method from LauncherModel.Callbacks.
2989 */
Joe Onorato36115782010-06-17 13:28:48 -04002990 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002991 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002992 if (permanent) {
2993 mWorkspace.removeItems(apps);
2994 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002995
Winson Chung785d2eb2011-04-14 16:08:02 -07002996 if (mAppsCustomizeContent != null) {
2997 mAppsCustomizeContent.removeApps(apps);
2998 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002999 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003000 }
Joe Onoratobe386092009-11-17 17:32:16 -08003001
3002 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003003 * A number of packages were updated.
3004 */
3005 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003006
Winson Chung785d2eb2011-04-14 16:08:02 -07003007 if (mAppsCustomizeContent != null) {
3008 mAppsCustomizeContent.onPackagesUpdated();
3009 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003010 }
3011
Winson Chung400438b2011-01-16 17:53:48 -08003012 private int mapConfigurationOriActivityInfoOri(int configOri) {
3013 final Display d = getWindowManager().getDefaultDisplay();
3014 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3015 switch (d.getRotation()) {
3016 case Surface.ROTATION_0:
3017 case Surface.ROTATION_180:
3018 // We are currently in the same basic orientation as the natural orientation
3019 naturalOri = configOri;
3020 break;
3021 case Surface.ROTATION_90:
3022 case Surface.ROTATION_270:
3023 // We are currently in the other basic orientation to the natural orientation
3024 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3025 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3026 break;
3027 }
3028
3029 int[] oriMap = {
3030 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3031 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3032 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3033 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3034 };
3035 // Since the map starts at portrait, we need to offset if this device's natural orientation
3036 // is landscape.
3037 int indexOffset = 0;
3038 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3039 indexOffset = 1;
3040 }
3041 return oriMap[(d.getRotation() + indexOffset) % 4];
3042 }
3043 public void lockScreenOrientation() {
3044 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3045 .getConfiguration().orientation));
3046 }
3047 public void unlockScreenOrientation() {
3048 mHandler.postDelayed(new Runnable() {
3049 public void run() {
3050 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3051 }
3052 }, mRestoreScreenOrientationDelay);
3053 }
3054
Winson Chung82f55532011-08-09 14:14:23 -07003055 /* Cling related */
3056 private static final String WORKSPACE_CLING_DISMISSED_KEY = "cling.workspace.dismissed";
3057 private static final String ALLAPPS_CLING_DISMISSED_KEY = "cling.allapps.dismissed";
3058 private void enableClingsIfNecessary() {
3059 // TEMPORARY: DISABLE CLINGS ON LARGE UI
3060 if (LauncherApplication.isScreenLarge()) return;
3061
Brett Chabot2a9e2282011-08-23 15:03:13 -07003062 // disable clings when running in a test harness
3063 if(ActivityManager.isRunningInTestHarness()) return;
3064
Winson Chung82f55532011-08-09 14:14:23 -07003065 // Enable the clings only if they have not been dismissed before
3066 SharedPreferences prefs =
3067 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3068 if (!prefs.getBoolean(WORKSPACE_CLING_DISMISSED_KEY, false)) {
3069 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3070 cling.init(this);
3071 cling.setVisibility(View.VISIBLE);
3072 }
3073 if (!prefs.getBoolean(ALLAPPS_CLING_DISMISSED_KEY, false)) {
3074 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3075 cling.init(this);
3076 cling.setVisibility(View.VISIBLE);
3077 }
3078 }
3079 private void dismissCling(final Cling cling, final String flag) {
3080 if (cling != null) {
3081 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
3082 anim.setDuration(DISMISS_CLING_DURATION);
3083 anim.addListener(new AnimatorListenerAdapter() {
3084 public void onAnimationEnd(Animator animation) {
3085 cling.setVisibility(View.GONE);
3086 cling.cleanup();
3087 SharedPreferences prefs =
3088 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3089 SharedPreferences.Editor editor = prefs.edit();
3090 editor.putBoolean(flag, true);
3091 editor.commit();
3092 };
3093 });
3094 anim.start();
3095 }
3096 }
3097 public void dismissWorkspaceCling(View v) {
3098 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3099 dismissCling(cling, WORKSPACE_CLING_DISMISSED_KEY);
3100 }
3101 public void dismissAllAppsCling(View v) {
3102 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3103 dismissCling(cling, ALLAPPS_CLING_DISMISSED_KEY);
3104 }
3105
Winson Chung80baf5a2010-08-09 16:03:15 -07003106 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003107 * Prints out out state for debugging.
3108 */
3109 public void dumpState() {
3110 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003111 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003112 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3113 Log.d(TAG, "mRestoring=" + mRestoring);
3114 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3115 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003116 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003117 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003118
Winson Chung785d2eb2011-04-14 16:08:02 -07003119 if (mAppsCustomizeContent != null) {
3120 mAppsCustomizeContent.dumpState();
3121 }
Joe Onoratobe386092009-11-17 17:32:16 -08003122 Log.d(TAG, "END launcher2 dump state");
3123 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003124}
Michael Jurka2763be32011-02-24 11:19:57 -08003125
Michael Jurkaabded662011-03-04 12:06:57 -08003126interface LauncherTransitionable {
Winson Chung5a808352011-06-27 19:08:49 -07003127 void onLauncherTransitionStart(Animator animation, boolean toWorkspace);
3128 void onLauncherTransitionEnd(Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003129}