blob: c3edb9974bb1b8826a7fcb1d6e39771cb31b2fcc [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.Dialog;
30import android.app.SearchManager;
31import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070036import android.content.ClipData;
37import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
41import android.content.DialogInterface;
42import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040053import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070054import android.os.AsyncTask;
Adam Cohen50370f32011-08-25 18:57:14 -070055import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020057import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080058import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070059import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040060import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080061import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070062import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080063import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
66import android.text.TextUtils;
67import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070068import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080069import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080070import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.view.KeyEvent;
72import android.view.LayoutInflater;
73import android.view.Menu;
74import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070075import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080076import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.view.View;
Winson Chung82f55532011-08-09 14:14:23 -070078import android.view.ViewGroup;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070079import android.view.WindowManager;
Adam Cohencff6af82011-09-13 14:51:53 -070080import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080081import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070082import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080083import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080084import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070085import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070087import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070088import android.widget.TextView;
89import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070090
Adam Cohendf2cc412011-04-27 16:56:57 -070091import com.android.common.Search;
92import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070093import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080094
Adam Cohenc0dcf592011-06-01 15:30:43 -070095import java.io.DataInputStream;
96import java.io.DataOutputStream;
97import java.io.FileNotFoundException;
98import java.io.IOException;
99import java.util.ArrayList;
100import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700101
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800102/**
103 * Default launcher application.
104 */
Michael Jurka946ad472010-07-09 18:05:18 -0700105public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700106 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
107 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800108 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700109 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110
Joe Onorato9c1289c2009-08-17 11:03:03 -0400111 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400112 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700113
Winson Chung70d72102011-08-12 11:24:35 -0700114 private static final int MENU_GROUP_WALLPAPER = 1;
115 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
116 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700117 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
118 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800119
120 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700121 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122 private static final int REQUEST_PICK_APPLICATION = 6;
123 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700124 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700125 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
127 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
128
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800129 static final int SCREEN_COUNT = 5;
130 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
Joe Onorato7c312c12009-08-13 21:36:53 -0700132 static final int DIALOG_CREATE_SHORTCUT = 1;
133 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Romain Guy98d01652009-06-30 16:21:04 -0700135 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 // Type: int
138 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700139 // Type: int
140 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700142 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
143 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
145 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700146 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700148 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 // Type: boolean
150 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
151 // Type: long
152 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
153
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700154 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
155
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700156 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700157 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700158 private State mState = State.WORKSPACE;
159 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700160
Joe Onorato9c1289c2009-08-17 11:03:03 -0400161 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700162 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
163 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung82f55532011-08-09 14:14:23 -0700164 private static final int DISMISS_CLING_DURATION = 300;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800167 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800169 private final BroadcastReceiver mCloseSystemDialogsReceiver
170 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800171 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private LayoutInflater mInflater;
174
Joe Onorato00acb122009-08-04 16:04:30 -0400175 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700177
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700178 private AppWidgetManager mAppWidgetManager;
179 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700180
Michael Jurkac9d95c52011-08-29 14:03:34 -0700181 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700182 private int[] mTmpAddItemCellCoordinates = new int[2];
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private FolderInfo mFolderInfo;
185
Winson Chung3d503fb2011-07-13 17:25:49 -0700186 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800187 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700188
Winson Chungf0ea4d32011-06-06 14:27:16 -0700189 private SearchDropTargetBar mSearchDeleteBar;
190 private AppsCustomizeTabHost mAppsCustomizeTabHost;
191 private AppsCustomizePagedView mAppsCustomizeContent;
192 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private Bundle mSavedState;
195
196 private SpannableStringBuilder mDefaultKeySsb = null;
197
Joe Onorato9c1289c2009-08-17 11:03:03 -0400198 private boolean mWorkspaceLoading = true;
199
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800200 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800201 private boolean mRestoring;
202 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700203 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
205 private Bundle mSavedInstanceState;
206
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800208 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800209 private boolean mUserPresent = true;
210 private boolean mVisible = false;
211 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700213 private static LocaleConfiguration sLocaleConfiguration = null;
214
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700215 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700216
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700217 private Intent mAppMarketIntent = null;
218
Adam Cohended9f8d2010-11-03 13:25:16 -0700219 // Related to the auto-advancing of widgets
220 private final int ADVANCE_MSG = 1;
221 private final int mAdvanceInterval = 20000;
222 private final int mAdvanceStagger = 250;
223 private long mAutoAdvanceSentTime;
224 private long mAutoAdvanceTimeLeft = -1;
225 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
226 new HashMap<View, AppWidgetProviderInfo>();
227
Winson Chung400438b2011-01-16 17:53:48 -0800228 // Determines how long to wait after a rotation before restoring the screen orientation to
229 // match the sensor state.
230 private final int mRestoreScreenOrientationDelay = 500;
231
Michael Jurka4ef207b2010-11-29 17:05:45 -0800232 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700233 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
234 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
235 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800236
Adam Cohen2801caf2011-05-13 20:57:39 -0700237 private DragLayer mDragLayer;
238
Michael Jurkaddd62e92011-02-16 17:49:14 -0800239 private BubbleTextView mWaitingForResume;
240
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800241 private static ArrayList<PendingAddArguments> sPendingAddList
242 = new ArrayList<PendingAddArguments>();
243
244 private static class PendingAddArguments {
245 int requestCode;
246 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700247 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800248 int screen;
249 int cellX;
250 int cellY;
251 }
252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 @Override
254 protected void onCreate(Bundle savedInstanceState) {
255 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800256 LauncherApplication app = ((LauncherApplication)getApplication());
257 mModel = app.setLauncher(this);
258 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400259 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800260 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700261
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700262 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700263 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
264 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200267 android.os.Debug.startMethodTracing(
268 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 }
270
271 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 setContentView(R.layout.launcher);
273 setupViews();
Winson Chung82f55532011-08-09 14:14:23 -0700274 enableClingsIfNecessary();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800276 registerContentObservers();
277
Joe Onorato7c312c12009-08-13 21:36:53 -0700278 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700279
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 mSavedState = savedInstanceState;
281 restoreState(mSavedState);
282
Winson Chunga12a2502010-12-20 14:41:35 -0800283 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700284 if (mAppsCustomizeContent != null) {
285 mAppsCustomizeContent.onPackagesUpdated();
286 }
Winson Chunga12a2502010-12-20 14:41:35 -0800287
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 if (PROFILE_STARTUP) {
289 android.os.Debug.stopMethodTracing();
290 }
291
292 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400293 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 }
295
Michael Jurkac57b7a82011-08-09 22:02:20 -0700296 if (!mModel.isAllAppsLoaded()) {
297 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
298 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
299 }
300
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301 // For handling default keys
302 mDefaultKeySsb = new SpannableStringBuilder();
303 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800304
305 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
306 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800307
308 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700309 int coi = getCurrentOrientationIndexForGlobalIcons();
310 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
311 sAppMarketIcon[coi] == null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700312 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100313 updateGlobalSearchIcon();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700314 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700315 if (sGlobalSearchIcon[coi] != null) {
316 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700317 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700318 if (sVoiceSearchIcon[coi] != null) {
319 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700320 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700321 if (sAppMarketIcon[coi] != null) {
322 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800323 }
Adam Cohen446e9402011-09-15 18:21:21 -0700324
325 // On large interfaces, we want the screen to auto-rotate based on the current orientation
326 if (LauncherApplication.isScreenLarge()) {
327 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
328 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 }
Romain Guycbb89e42009-06-08 15:52:54 -0700330
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700332 if (sLocaleConfiguration == null) {
333 new AsyncTask<Void, Void, LocaleConfiguration>() {
334 @Override
335 protected LocaleConfiguration doInBackground(Void... unused) {
336 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
337 readConfiguration(Launcher.this, localeConfiguration);
338 return localeConfiguration;
339 }
340
341 @Override
342 protected void onPostExecute(LocaleConfiguration result) {
343 sLocaleConfiguration = result;
344 checkForLocaleChange(); // recursive, but now with a locale configuration
345 }
346 }.execute();
347 return;
348 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700349
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 final Configuration configuration = getResources().getConfiguration();
351
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700352 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 final String locale = configuration.locale.toString();
354
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700355 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 final int mcc = configuration.mcc;
357
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700358 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800359 final int mnc = configuration.mnc;
360
Romain Guy84f296c2009-11-04 15:00:44 -0800361 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362
Romain Guy84f296c2009-11-04 15:00:44 -0800363 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700364 sLocaleConfiguration.locale = locale;
365 sLocaleConfiguration.mcc = mcc;
366 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700367
Joe Onorato0589f0f2010-02-08 13:44:00 -0800368 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700369
370 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
371 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700372 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700373 public void run() {
374 writeConfiguration(Launcher.this, localeConfiguration);
375 }
376 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700377 }
378 }
379
380 private static class LocaleConfiguration {
381 public String locale;
382 public int mcc = -1;
383 public int mnc = -1;
384 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700385
Romain Guy98d01652009-06-30 16:21:04 -0700386 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
387 DataInputStream in = null;
388 try {
389 in = new DataInputStream(context.openFileInput(PREFERENCES));
390 configuration.locale = in.readUTF();
391 configuration.mcc = in.readInt();
392 configuration.mnc = in.readInt();
393 } catch (FileNotFoundException e) {
394 // Ignore
395 } catch (IOException e) {
396 // Ignore
397 } finally {
398 if (in != null) {
399 try {
400 in.close();
401 } catch (IOException e) {
402 // Ignore
403 }
404 }
405 }
406 }
407
408 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
409 DataOutputStream out = null;
410 try {
411 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
412 out.writeUTF(configuration.locale);
413 out.writeInt(configuration.mcc);
414 out.writeInt(configuration.mnc);
415 out.flush();
416 } catch (FileNotFoundException e) {
417 // Ignore
418 } catch (IOException e) {
419 //noinspection ResultOfMethodCallIgnored
420 context.getFileStreamPath(PREFERENCES).delete();
421 } finally {
422 if (out != null) {
423 try {
424 out.close();
425 } catch (IOException e) {
426 // Ignore
427 }
428 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 }
430 }
431
Adam Cohen716b51e2011-06-30 12:09:54 -0700432 public DragLayer getDragLayer() {
433 return mDragLayer;
434 }
435
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 static int getScreen() {
437 synchronized (sLock) {
438 return sScreen;
439 }
440 }
441
442 static void setScreen(int screen) {
443 synchronized (sLock) {
444 sScreen = screen;
445 }
446 }
447
Winson Chung557d6ed2011-07-08 15:34:52 -0700448 /**
449 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
450 * a configuration step, this allows the proper animations to run after other transitions.
451 */
452 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700453 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800454 switch (args.requestCode) {
455 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700456 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
457 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800458 break;
459 case REQUEST_PICK_SHORTCUT:
460 processShortcut(args.intent);
461 break;
462 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700463 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
464 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700465 result = true;
466 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800467 case REQUEST_PICK_APPWIDGET:
468 addAppWidgetFromPick(args.intent);
469 break;
470 case REQUEST_CREATE_APPWIDGET:
471 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700472 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700473 result = true;
474 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800475 case REQUEST_PICK_WALLPAPER:
476 // We just wanted the activity result here so we can clear mWaitingForResult
477 break;
478 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700479 // In any situation where we have a multi-step drop, we should reset the add info only after
480 // we complete the drop
481 resetAddInfo();
482 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800483 }
484
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800486 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700487 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700488 mWaitingForResult = false;
489
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 // The pattern used here is that a user PICKs a specific application,
491 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700492
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
494 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700495 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800496 final PendingAddArguments args = new PendingAddArguments();
497 args.requestCode = requestCode;
498 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700499 args.container = mPendingAddInfo.container;
500 args.screen = mPendingAddInfo.screen;
501 args.cellX = mPendingAddInfo.cellX;
502 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800503
504 // If the loader is still running, defer the add until it is done.
505 if (isWorkspaceLocked()) {
506 sPendingAddList.add(args);
507 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700508 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800509 }
Romain Guy18042c82009-11-06 11:44:55 -0800510 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700511 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
512 if (data != null) {
513 // Clean up the appWidgetId if we canceled
514 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
515 if (appWidgetId != -1) {
516 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
517 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 }
519 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700520
521 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700522 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800523 }
524
525 @Override
526 protected void onResume() {
527 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800528 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700529 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400530 mWorkspaceLoading = true;
531 mModel.startLoader(this, true);
532 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700533 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800535 if (mWaitingForResume != null) {
536 mWaitingForResume.setStayPressed(false);
537 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700538 // When we resume Launcher, a different Activity might be responsible for the app
539 // market intent, so refresh the icon
540 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 }
542
543 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700544 protected void onPause() {
545 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700546 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800547 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700548 }
Romain Guycbb89e42009-06-08 15:52:54 -0700549
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700550 @Override
551 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400552 // Flag the loader to stop early before switching
553 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700554 if (mAppsCustomizeContent != null) {
555 mAppsCustomizeContent.surrender();
556 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800557 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700558 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700559
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800560 // We can't hide the IME if it was forced open. So don't bother
561 /*
562 @Override
563 public void onWindowFocusChanged(boolean hasFocus) {
564 super.onWindowFocusChanged(hasFocus);
565
566 if (hasFocus) {
567 final InputMethodManager inputManager = (InputMethodManager)
568 getSystemService(Context.INPUT_METHOD_SERVICE);
569 WindowManager.LayoutParams lp = getWindow().getAttributes();
570 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
571 android.os.Handler()) {
572 protected void onReceiveResult(int resultCode, Bundle resultData) {
573 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
574 }
575 });
576 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
577 }
578 }
579 */
580
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 private boolean acceptFilter() {
582 final InputMethodManager inputManager = (InputMethodManager)
583 getSystemService(Context.INPUT_METHOD_SERVICE);
584 return !inputManager.isFullscreenMode();
585 }
586
587 @Override
588 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700589 final int uniChar = event.getUnicodeChar();
590 final boolean handled = super.onKeyDown(keyCode, event);
591 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
592 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
594 keyCode, event);
595 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700596 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700597 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700598 // showSearchDialog()
599 // If there are multiple keystrokes before the search dialog takes focus,
600 // onSearchRequested() will be called for every keystroke,
601 // but it is idempotent, so it's fine.
602 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 }
604 }
605
Joe Onorato8a9625e2010-01-28 15:55:35 -0800606 // Eat the long press event so the keyboard doesn't come up.
607 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
608 return true;
609 }
610
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 return handled;
612 }
613
Karl Rosaen138a0412009-04-23 19:00:21 -0700614 private String getTypedText() {
615 return mDefaultKeySsb.toString();
616 }
617
618 private void clearTypedText() {
619 mDefaultKeySsb.clear();
620 mDefaultKeySsb.clearSpans();
621 Selection.setSelection(mDefaultKeySsb, 0);
622 }
623
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700625 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
626 * State
627 */
628 private static State intToState(int stateOrdinal) {
629 State state = State.WORKSPACE;
630 final State[] stateValues = State.values();
631 for (int i = 0; i < stateValues.length; i++) {
632 if (stateValues[i].ordinal() == stateOrdinal) {
633 state = stateValues[i];
634 break;
635 }
636 }
637 return state;
638 }
639
640 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800641 * Restores the previous state, if it exists.
642 *
643 * @param savedState The previous state.
644 */
645 private void restoreState(Bundle savedState) {
646 if (savedState == null) {
647 return;
648 }
649
Michael Jurka883f55b2010-10-21 15:47:14 -0700650 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700651 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800652 showAllApps(false);
653 }
654
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800655 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
656 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700657 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 }
659
Winson Chung3d503fb2011-07-13 17:25:49 -0700660 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
661 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700662
Winson Chung3d503fb2011-07-13 17:25:49 -0700663 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
664 mPendingAddInfo.container = pendingAddContainer;
665 mPendingAddInfo.screen = pendingAddScreen;
666 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
667 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 mRestoring = true;
669 }
670
671 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
672 if (renameFolder) {
673 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700674 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800675 mRestoring = true;
676 }
Winson Chunga12a2502010-12-20 14:41:35 -0800677
Winson Chung785d2eb2011-04-14 16:08:02 -0700678
679 // Restore the AppsCustomize tab
680 if (mAppsCustomizeTabHost != null) {
681 String curTab = savedState.getString("apps_customize_currentTab");
682 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700683 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700684 mAppsCustomizeContent.setContentType(
685 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
686 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700687 mAppsCustomizeContent.loadAssociatedPages(
688 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700689 }
690
Winson Chung5afbf7b2011-07-25 11:53:08 -0700691 int currentIndex = savedState.getInt("apps_customize_currentIndex");
692 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 }
695
696 /**
697 * Finds all the views we need and configure them properly.
698 */
699 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700700 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400701
Winson Chung4c98d922011-05-31 16:50:48 -0700702 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
703 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704
Winson Chung4c98d922011-05-31 16:50:48 -0700705 // Setup the drag layer
706 mDragLayer.setup(this, dragController);
707
Winson Chung3d503fb2011-07-13 17:25:49 -0700708 // Setup the hotseat
709 mHotseat = (Hotseat) findViewById(R.id.hotseat);
710 if (mHotseat != null) {
711 mHotseat.setup(this);
712 }
713
Winson Chung4c98d922011-05-31 16:50:48 -0700714 // Setup the workspace
715 mWorkspace.setHapticFeedbackEnabled(false);
716 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700717 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700718 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700719
Winson Chungf0ea4d32011-06-06 14:27:16 -0700720 // Get the search/delete bar
721 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700722
Winson Chungf0ea4d32011-06-06 14:27:16 -0700723 // Setup AppsCustomize
724 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
725 findViewById(R.id.apps_customize_pane);
726 mAppsCustomizeContent = (AppsCustomizePagedView)
727 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
728 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400729
Romain Guy1fbc1c82009-11-09 20:43:08 -0800730
Winson Chung4c98d922011-05-31 16:50:48 -0700731
Winson Chung3d503fb2011-07-13 17:25:49 -0700732 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700733 dragController.setDragScoller(mWorkspace);
734 dragController.setScrollView(mDragLayer);
735 dragController.setMoveTarget(mWorkspace);
736 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700737 if (mSearchDeleteBar != null) {
738 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800739 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 }
741
742 /**
743 * Creates a view representing a shortcut.
744 *
745 * @param info The data structure describing the shortcut.
746 *
747 * @return A View inflated from R.layout.application.
748 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800749 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700751 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753
754 /**
755 * Creates a view representing a shortcut inflated from the specified resource.
756 *
757 * @param layoutResId The id of the XML layout used to create the shortcut.
758 * @param parent The group the shortcut belongs to.
759 * @param info The data structure describing the shortcut.
760 *
761 * @return A View inflated from layoutResId.
762 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800763 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800764 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
765 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800767 return favorite;
768 }
769
770 /**
771 * Add an application shortcut to the workspace.
772 *
773 * @param data The intent describing the application.
774 * @param cellInfo The position on screen where to create the shortcut.
775 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700776 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700777 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700778 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700779
Adam Cohenfbba09b2011-07-18 21:43:05 -0700780 // First we check if we already know the exact location where we want to add this item.
781 if (cellX >= 0 && cellY >= 0) {
782 cellXY[0] = cellX;
783 cellXY[1] = cellY;
784 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700785 showOutOfSpaceMessage();
786 return;
787 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800789 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800790
Romain Guy73b979d2009-06-09 12:57:21 -0700791 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800792 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800794 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700795 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700796 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800797 } else {
798 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 }
800 }
Romain Guycbb89e42009-06-08 15:52:54 -0700801
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 /**
803 * Add a shortcut to the workspace.
804 *
805 * @param data The intent describing the shortcut.
806 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700808 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
809 int cellY) {
810 int[] cellXY = mTmpAddItemCellCoordinates;
811 int[] touchXY = mPendingAddInfo.dropPos;
812 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700813
Michael Jurkad3ef3062010-11-23 16:23:58 -0800814 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700815
Adam Cohen558baaf2011-08-15 15:22:57 -0700816 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
817 final View view = createShortcut(info);
818
Adam Cohenfbba09b2011-07-18 21:43:05 -0700819 // First we check if we already know the exact location where we want to add this item.
820 if (cellX >= 0 && cellY >= 0) {
821 cellXY[0] = cellX;
822 cellXY[1] = cellY;
823 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700824
825 // If appropriate, either create a folder or add to an existing folder
826 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
827 true, null,null)) {
828 return;
829 }
830 DragObject dragObject = new DragObject();
831 dragObject.dragInfo = info;
832 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
833 return;
834 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700835 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800836 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700837 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800838 foundCellSpan = (result != null);
839 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700840 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800841 }
842
843 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700844 showOutOfSpaceMessage();
845 return;
846 }
847
Adam Cohen558baaf2011-08-15 15:22:57 -0700848 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800849
850 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700851 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
852 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 }
854 }
855
Adam Cohen50370f32011-08-25 18:57:14 -0700856 class Padding {
857 int left = 0;
858 int right = 0;
859 int top = 0;
860 int bottom = 0;
861 }
862
Adam Cohenf814aa02011-09-01 13:48:05 -0700863 Padding getPaddingForWidget(ComponentName component) {
Adam Cohen50370f32011-08-25 18:57:14 -0700864 PackageManager packageManager = getPackageManager();
865 Padding p = new Padding();
866 android.content.pm.ApplicationInfo appInfo;
867
868 try {
Adam Cohenf814aa02011-09-01 13:48:05 -0700869 appInfo = packageManager.getApplicationInfo(component.getPackageName(), 0);
Adam Cohen50370f32011-08-25 18:57:14 -0700870 } catch (Exception e) {
871 // if we can't find the package, return 0 padding
872 return p;
873 }
874
875 // TODO: This should be ICE_CREAM_SANDWICH, but since the unbundled apps
876 // may not have updated their targetSdkVersion yet, we've bumped it down for now.
877 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.HONEYCOMB_MR2) {
878 Resources r = getResources();
879 p.left = r.getDimensionPixelSize(R.dimen.app_widget_padding_left);
880 p.right = r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
881 p.top = r.getDimensionPixelSize(R.dimen.app_widget_padding_top);
882 p.bottom = r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
883 }
884
885 return p;
886 }
887
Adam Cohenf814aa02011-09-01 13:48:05 -0700888 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
889 if (spanXY == null) {
890 spanXY = new int[2];
891 }
892
893 Padding padding = getPaddingForWidget(component);
894 // We want to account for the extra amount of padding that we are adding to the widget
895 // to ensure that it gets the full amount of space that it has requested
896 int requiredWidth = minWidth + padding.left + padding.right;
897 int requiredHeight = minHeight + padding.top + padding.bottom;
898 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
899 }
900
901 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
902 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
903 }
904
905 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
906 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
907 }
908
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700910 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800911 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700912 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700913 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700915 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700916 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700917
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700918 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700919 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700920
Adam Cohenf814aa02011-09-01 13:48:05 -0700921 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700922
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700924 // We have saved the position to which the widget was dragged-- this really only matters
925 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700926 int[] cellXY = mTmpAddItemCellCoordinates;
927 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700928 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700929 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
930 cellXY[0] = mPendingAddInfo.cellX;
931 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700932 foundCellSpan = true;
933 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700934 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700935 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700936 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800937 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700938 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700939 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700940 }
941
Michael Jurka0280c3b2010-09-17 15:00:07 -0700942 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800943 if (appWidgetId != -1) {
944 // Deleting an app widget ID is a void call but writes to disk before returning
945 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800946 new Thread("deleteAppWidgetId") {
947 public void run() {
948 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
949 }
950 }.start();
951 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700952 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800953 return;
954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700956 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -0700957 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700958 launcherInfo.spanX = spanXY[0];
959 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700960
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700962 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963
964 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700966 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700967
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700968 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700970
Winson Chung3d503fb2011-07-13 17:25:49 -0700971 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400972 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700973
974 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 }
976 }
Romain Guycbb89e42009-06-08 15:52:54 -0700977
Adam Cohended9f8d2010-11-03 13:25:16 -0700978 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
979 @Override
980 public void onReceive(Context context, Intent intent) {
981 final String action = intent.getAction();
982 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
983 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700984 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700985 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700986
Winson Chungc100e8e2011-08-09 16:02:43 -0700987 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -0700988 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -0700989 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
990 mAppsCustomizeTabHost.reset();
991 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -0700992 }
Adam Cohended9f8d2010-11-03 13:25:16 -0700993 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
994 mUserPresent = true;
995 updateRunning();
996 }
997 }
998 };
999
1000 @Override
1001 public void onAttachedToWindow() {
1002 super.onAttachedToWindow();
1003
1004 // Listen for broadcasts related to user-presence
1005 final IntentFilter filter = new IntentFilter();
1006 filter.addAction(Intent.ACTION_SCREEN_OFF);
1007 filter.addAction(Intent.ACTION_USER_PRESENT);
1008 registerReceiver(mReceiver, filter);
1009
Adam Cohend113e0c2010-11-11 10:48:05 -08001010 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001011 mVisible = true;
1012 }
1013
1014 @Override
1015 public void onDetachedFromWindow() {
1016 super.onDetachedFromWindow();
1017 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001018 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001019
Adam Cohend113e0c2010-11-11 10:48:05 -08001020 if (mAttached) {
1021 unregisterReceiver(mReceiver);
1022 mAttached = false;
1023 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001024 updateRunning();
1025 }
1026
1027 public void onWindowVisibilityChanged(int visibility) {
1028 mVisible = visibility == View.VISIBLE;
1029 updateRunning();
1030 }
1031
1032 private void sendAdvanceMessage(long delay) {
1033 mHandler.removeMessages(ADVANCE_MSG);
1034 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1035 mHandler.sendMessageDelayed(msg, delay);
1036 mAutoAdvanceSentTime = System.currentTimeMillis();
1037 }
1038
1039 private void updateRunning() {
1040 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1041 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1042 mAutoAdvanceRunning = autoAdvanceRunning;
1043 if (autoAdvanceRunning) {
1044 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1045 sendAdvanceMessage(delay);
1046 } else {
1047 if (!mWidgetsToAdvance.isEmpty()) {
1048 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1049 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1050 }
1051 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001052 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001053 }
1054 }
1055 }
1056
1057 private final Handler mHandler = new Handler() {
1058 @Override
1059 public void handleMessage(Message msg) {
1060 if (msg.what == ADVANCE_MSG) {
1061 int i = 0;
1062 for (View key: mWidgetsToAdvance.keySet()) {
1063 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1064 final int delay = mAdvanceStagger * i;
1065 if (v instanceof Advanceable) {
1066 postDelayed(new Runnable() {
1067 public void run() {
1068 ((Advanceable) v).advance();
1069 }
1070 }, delay);
1071 }
1072 i++;
1073 }
1074 sendAdvanceMessage(mAdvanceInterval);
1075 }
1076 }
1077 };
1078
1079 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001080 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001081 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1082 if (v instanceof Advanceable) {
1083 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001084 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001085 updateRunning();
1086 }
1087 }
1088
1089 void removeWidgetToAutoAdvance(View hostView) {
1090 if (mWidgetsToAdvance.containsKey(hostView)) {
1091 mWidgetsToAdvance.remove(hostView);
1092 updateRunning();
1093 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001094 }
1095
Joe Onorato9c1289c2009-08-17 11:03:03 -04001096 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001097 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001098 launcherInfo.hostView = null;
1099 }
1100
Adam Cohended9f8d2010-11-03 13:25:16 -07001101 void showOutOfSpaceMessage() {
1102 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1103 }
1104
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001105 public LauncherAppWidgetHost getAppWidgetHost() {
1106 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001107 }
Romain Guycbb89e42009-06-08 15:52:54 -07001108
Winson Chunga9abd0e2010-10-27 17:18:37 -07001109 public LauncherModel getModel() {
1110 return mModel;
1111 }
1112
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001113 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001114 getWindow().closeAllPanels();
1115
Winson Chung87acb482011-08-23 17:28:05 -07001116 /**
1117 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001118 try {
1119 dismissDialog(DIALOG_CREATE_SHORTCUT);
1120 // Unlock the workspace if the dialog was showing
1121 } catch (Exception e) {
1122 // An exception is thrown if the dialog is not visible, which is fine
1123 }
1124
1125 try {
1126 dismissDialog(DIALOG_RENAME_FOLDER);
1127 // Unlock the workspace if the dialog was showing
1128 } catch (Exception e) {
1129 // An exception is thrown if the dialog is not visible, which is fine
1130 }
Winson Chung87acb482011-08-23 17:28:05 -07001131 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001132
1133 // Whatever we were doing is hereby canceled.
1134 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001135 }
1136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 @Override
1138 protected void onNewIntent(Intent intent) {
1139 super.onNewIntent(intent);
1140
1141 // Close the menu
1142 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001143 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001144 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001145
Adam Cohen95bb8002011-07-03 23:40:28 -07001146 closeFolder();
1147
Joe Onorato14f122b2009-11-19 14:06:36 -08001148 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1149 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001150
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001151 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001152 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001153 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001154 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001155 }
Winson Chungde1af762011-07-21 16:44:07 -07001156 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001157 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001158
Joe Onorato3a8820b2009-11-10 15:06:42 -08001159 final View v = getWindow().peekDecorView();
1160 if (v != null && v.getWindowToken() != null) {
1161 InputMethodManager imm = (InputMethodManager)getSystemService(
1162 INPUT_METHOD_SERVICE);
1163 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001165
Michael Jurka35aa14d2011-07-07 17:01:08 -07001166 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001167 if (mAppsCustomizeTabHost != null) {
1168 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001169 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 }
1171 }
1172
1173 @Override
1174 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1175 // Do not call super here
1176 mSavedInstanceState = savedInstanceState;
1177 }
1178
1179 @Override
1180 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001181 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001182 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183
Michael Jurka883f55b2010-10-21 15:47:14 -07001184 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001185 // We close any open folder since it will not be re-opened, and we need to make sure
1186 // this state is reflected.
1187 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188
Winson Chung3d503fb2011-07-13 17:25:49 -07001189 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1190 mWaitingForResult) {
1191 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1192 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1193 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1194 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 }
1196
1197 if (mFolderInfo != null && mWaitingForResult) {
1198 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1199 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1200 }
Michael Jurka946ad472010-07-09 18:05:18 -07001201
Winson Chung785d2eb2011-04-14 16:08:02 -07001202 // Save the current AppsCustomize tab
1203 if (mAppsCustomizeTabHost != null) {
1204 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1205 if (currentTabTag != null) {
1206 outState.putString("apps_customize_currentTab", currentTabTag);
1207 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001208 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1209 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 }
1212
1213 @Override
1214 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001216
Winson Chunge7a03942011-08-05 15:05:12 -07001217 // Remove all pending runnables
1218 mHandler.removeMessages(ADVANCE_MSG);
1219 mHandler.removeMessages(0);
1220
Winson Chungcd2b0142011-06-08 16:02:26 -07001221 // Stop callbacks from LauncherModel
1222 LauncherApplication app = ((LauncherApplication) getApplication());
1223 mModel.stopLoader();
1224 app.setLauncher(null);
1225
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001226 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001227 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001229 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001231 mAppWidgetHost = null;
1232
1233 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234
1235 TextKeyListener.getInstance().release();
1236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237
Winson Chung3d503fb2011-07-13 17:25:49 -07001238 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001239
1240 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001241 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001242
1243 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1244 mWorkspace.removeAllViews();
1245 mWorkspace = null;
1246 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001247
1248 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249 }
1250
Adam Cohena9cf38f2011-05-02 15:36:58 -07001251 public DragController getDragController() {
1252 return mDragController;
1253 }
1254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 @Override
1256 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001257 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 super.startActivityForResult(intent, requestCode);
1259 }
1260
Winson Chung70d72102011-08-12 11:24:35 -07001261 /**
1262 * Indicates that we want global search for this activity by setting the globalSearch
1263 * argument for {@link #startSearch} to true.
1264 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001266 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001268
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001269 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001270
Karl Rosaen138a0412009-04-23 19:00:21 -07001271 if (initialQuery == null) {
1272 // Use any text typed in the launcher as the initial query
1273 initialQuery = getTypedText();
1274 clearTypedText();
1275 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 if (appSearchData == null) {
1277 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001278 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 }
Romain Guycbb89e42009-06-08 15:52:54 -07001280
Karl Rosaen138a0412009-04-23 19:00:21 -07001281 final SearchManager searchManager =
1282 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001283 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001284 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 }
1286
Winson Chung70d72102011-08-12 11:24:35 -07001287 @Override
1288 public boolean onCreateOptionsMenu(Menu menu) {
1289 if (isWorkspaceLocked()) {
1290 return false;
1291 }
1292
1293 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001294
1295 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1296 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1297 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001298 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1299 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1300 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001301 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(getString(R.string.help_url)));
1302 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1303 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1304
Winson Chung70d72102011-08-12 11:24:35 -07001305 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1306 .setIcon(android.R.drawable.ic_menu_gallery)
1307 .setAlphabeticShortcut('W');
1308 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1309 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001310 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001311 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001312 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1313 .setIcon(android.R.drawable.ic_menu_preferences)
1314 .setIntent(settings)
1315 .setAlphabeticShortcut('P');
Winson Chung70d72102011-08-12 11:24:35 -07001316 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1317 .setIcon(android.R.drawable.ic_menu_help)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001318 .setIntent(help)
Winson Chung70d72102011-08-12 11:24:35 -07001319 .setAlphabeticShortcut('H');
1320 return true;
1321 }
1322
1323 @Override
1324 public boolean onPrepareOptionsMenu(Menu menu) {
1325 super.onPrepareOptionsMenu(menu);
1326
1327 if (mAppsCustomizeTabHost.isTransitioning()) {
1328 return false;
1329 }
1330 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1331 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1332
1333 return true;
1334 }
1335
1336 @Override
1337 public boolean onOptionsItemSelected(MenuItem item) {
1338 switch (item.getItemId()) {
1339 case MENU_WALLPAPER_SETTINGS:
1340 startWallpaper();
1341 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001342 }
1343
1344 return super.onOptionsItemSelected(item);
1345 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001347 @Override
1348 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001349 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001350 // Use a custom animation for launching search
1351 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001352 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001353 }
1354
Joe Onorato9c1289c2009-08-17 11:03:03 -04001355 public boolean isWorkspaceLocked() {
1356 return mWorkspaceLoading || mWaitingForResult;
1357 }
1358
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001360 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001361 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 }
1363
Michael Jurka0280c3b2010-09-17 15:00:07 -07001364 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001365 mPendingAddInfo.container = ItemInfo.NO_ID;
1366 mPendingAddInfo.screen = -1;
1367 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1368 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1369 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001370 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001371
Michael Jurkaaf442092010-06-10 17:01:57 -07001372 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001373 // TODO: catch bad widget exception when sent
1374 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001375 // TODO: Is this log message meaningful?
1376 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001377 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001378 }
1379
Winson Chung55cef262010-10-28 14:14:18 -07001380 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001381 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382
Bjorn Bringert7984c942009-12-09 15:38:25 +00001383 if (appWidget.configure != null) {
1384 // Launch over to configure widget, if needed
1385 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1386 intent.setComponent(appWidget.configure);
1387 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001388 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001389 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1390 intent.putExtra(
1391 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1392 info.mimeType);
1393
1394 final String mimeType = info.mimeType;
1395 final ClipData clipData = (ClipData) info.configurationData;
1396 final ClipDescription clipDesc = clipData.getDescription();
1397 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1398 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001399 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001400 final CharSequence stringData = item.getText();
1401 final Uri uriData = item.getUri();
1402 final Intent intentData = item.getIntent();
1403 final String key =
1404 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1405 if (uriData != null) {
1406 intent.putExtra(key, uriData);
1407 } else if (intentData != null) {
1408 intent.putExtra(key, intentData);
1409 } else if (stringData != null) {
1410 intent.putExtra(key, stringData);
1411 }
1412 break;
1413 }
1414 }
1415 }
Winson Chung55cef262010-10-28 14:14:18 -07001416 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001417
Romain Guy8e633c52010-03-04 12:51:36 -08001418 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001420 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001421 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001422
1423 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001424 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 }
1426 }
Romain Guycbb89e42009-06-08 15:52:54 -07001427
Adam Cohenfbba09b2011-07-18 21:43:05 -07001428 /**
1429 * Process a shortcut drop.
1430 *
1431 * @param componentName The name of the component
1432 * @param screen The screen where it should be added
1433 * @param cell The cell it should be added to, optional
1434 * @param position The location on the screen where it was dropped, optional
1435 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001436 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1437 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001438 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001439 mPendingAddInfo.container = container;
1440 mPendingAddInfo.screen = screen;
1441 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001442
1443 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001444 mPendingAddInfo.cellX = cell[0];
1445 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001446 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001447
1448 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1449 createShortcutIntent.setComponent(componentName);
1450 processShortcut(createShortcutIntent);
1451 }
1452
Adam Cohenfbba09b2011-07-18 21:43:05 -07001453 /**
1454 * Process a widget drop.
1455 *
1456 * @param info The PendingAppWidgetInfo of the widget being added.
1457 * @param screen The screen where it should be added
1458 * @param cell The cell it should be added to, optional
1459 * @param position The location on the screen where it was dropped, optional
1460 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001461 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1462 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001463 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001464 mPendingAddInfo.container = info.container = container;
1465 mPendingAddInfo.screen = info.screen = screen;
1466 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001467 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001468 mPendingAddInfo.cellX = cell[0];
1469 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001470 }
1471
1472 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1473 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1474 addAppWidgetImpl(appWidgetId, info);
1475 }
1476
Joe Onoratodeb98af2010-02-19 14:59:39 -08001477 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001478 // Handle case where user selected "Applications"
1479 String applicationName = getResources().getString(R.string.group_applications);
1480 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001481
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001482 if (applicationName != null && applicationName.equals(shortcutName)) {
1483 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1484 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001485
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001486 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1487 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001488 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001489 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001490 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001491 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001492 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 }
1494
Winson Chung24ab2f12010-09-16 14:10:47 -07001495 void processWallpaper(Intent intent) {
1496 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1497 }
1498
Winson Chung3d503fb2011-07-13 17:25:49 -07001499 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1500 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001501 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001502 folderInfo.title = getText(R.string.folder_name);
1503
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001505 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1506 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001507 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508
1509 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001510 FolderIcon newFolder =
1511 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1512 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1513 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001514 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 }
Romain Guycbb89e42009-06-08 15:52:54 -07001516
Joe Onorato9c1289c2009-08-17 11:03:03 -04001517 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001518 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001519 }
1520
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001521 private void showNotifications() {
1522 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1523 if (statusBar != null) {
1524 statusBar.expand();
1525 }
1526 }
1527
1528 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001529 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001531 Intent chooser = Intent.createChooser(pickWallpaper,
1532 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001533 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1534 // Removed in Eclair MR1
1535// WallpaperManager wm = (WallpaperManager)
1536// getSystemService(Context.WALLPAPER_SERVICE);
1537// WallpaperInfo wi = wm.getWallpaperInfo();
1538// if (wi != null && wi.getSettingsActivity() != null) {
1539// LabeledIntent li = new LabeledIntent(getPackageName(),
1540// R.string.configure_wallpaper, 0);
1541// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1542// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1543// }
Mike Clerona0618e42009-10-22 13:55:21 -07001544 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 }
1546
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001547 /**
1548 * Registers various content observers. The current implementation registers
1549 * only a favorites observer to keep track of the favorites applications.
1550 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001551 private void registerContentObservers() {
1552 ContentResolver resolver = getContentResolver();
1553 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1554 true, mWidgetObserver);
1555 }
1556
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001557 @Override
1558 public boolean dispatchKeyEvent(KeyEvent event) {
1559 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1560 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001562 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001563 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001564 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001565 dumpState();
1566 return true;
1567 }
1568 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001569 }
1570 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1571 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001572 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 return true;
1574 }
1575 }
1576
1577 return super.dispatchKeyEvent(event);
1578 }
1579
Joe Onorato88ec0992009-11-19 13:16:06 -08001580 @Override
1581 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001582 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001583 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001584 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001585 Folder openFolder = mWorkspace.getOpenFolder();
1586 if (openFolder.isEditingName()) {
1587 openFolder.dismissEditingName();
1588 } else {
1589 closeFolder();
1590 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001591 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001592 mWorkspace.exitWidgetResizeMode();
1593
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001594 // Back button is a no-op here, but give at least some feedback for the button press
1595 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001596 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001597 }
1598
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001600 * Re-listen when widgets are reset.
1601 */
1602 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001603 if (mAppWidgetHost != null) {
1604 mAppWidgetHost.startListening();
1605 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001606 }
1607
1608 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001609 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1610 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001612 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001613 if (mModel != null) {
1614 mModel.unbindWorkspaceItems();
1615 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001616 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001617
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 /**
1619 * Launches the intent referred by the clicked shortcut.
1620 *
1621 * @param v The view representing the clicked shortcut.
1622 */
1623 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001624 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1625 // view has detached (it's possible for this to happen if the view is removed mid touch).
1626 if (v.getWindowToken() == null) {
1627 return;
1628 }
1629
1630 if (mWorkspace.isSwitchingState()) {
1631 return;
1632 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001633
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001635 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001637 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001638 int[] pos = new int[2];
1639 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001640 intent.setSourceBounds(new Rect(pos[0], pos[1],
1641 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001642 boolean success = startActivitySafely(intent, tag);
1643
1644 if (success && v instanceof BubbleTextView) {
1645 mWaitingForResume = (BubbleTextView) v;
1646 mWaitingForResume.setStayPressed(true);
1647 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001649 if (v instanceof FolderIcon) {
1650 FolderIcon fi = (FolderIcon) v;
1651 handleFolderClick(fi);
1652 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001653 } else if (v == mAllAppsButton) {
1654 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001655 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001656 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001657 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001658 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 }
1660 }
1661
Michael Jurka0e260592010-06-30 17:07:39 -07001662 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001663 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001664 // clicking anywhere on the workspace causes the customization drawer to slide down
1665 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001666 return false;
1667 }
1668
Michael Jurkaaf442092010-06-10 17:01:57 -07001669 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001670 * Event handler for the search button
1671 *
1672 * @param v The view that was clicked.
1673 */
1674 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001675 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001676 }
1677
1678 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001679 * Event handler for the voice button
1680 *
1681 * @param v The view that was clicked.
1682 */
1683 public void onClickVoiceButton(View v) {
1684 startVoiceSearch();
1685 }
1686
1687 private void startVoiceSearch() {
1688 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001689 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001690 startActivity(intent);
1691 }
1692
1693 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001694 * Event handler for the "grid" button that appears on the home screen, which
1695 * enters all apps mode.
1696 *
1697 * @param v The view that was clicked.
1698 */
1699 public void onClickAllAppsButton(View v) {
1700 showAllApps(true);
1701 }
1702
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001703 public void onClickAppMarketButton(View v) {
1704 if (mAppMarketIntent != null) {
1705 startActivitySafely(mAppMarketIntent, "app market");
1706 }
1707 }
1708
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001709 void startApplicationDetailsActivity(ComponentName componentName) {
1710 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001711 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1712 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001713 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001714 startActivity(intent);
1715 }
1716
Patrick Dubroy5539af72010-09-07 15:22:01 -07001717 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1718 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1719 // System applications cannot be installed. For now, show a toast explaining that.
1720 // We may give them the option of disabling apps this way.
1721 int messageId = R.string.uninstall_system_app_text;
1722 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1723 } else {
1724 String packageName = appInfo.componentName.getPackageName();
1725 String className = appInfo.componentName.getClassName();
1726 Intent intent = new Intent(
1727 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001728 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1729 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001730 startActivity(intent);
1731 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001732 }
1733
Michael Jurkaddd62e92011-02-16 17:49:14 -08001734 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001735 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1736 try {
1737 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001738 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 } catch (ActivityNotFoundException e) {
1740 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001741 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 } catch (SecurityException e) {
1743 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001744 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001746 "or use the exported attribute for this activity. "
1747 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001749 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001751
Romain Guy8e633c52010-03-04 12:51:36 -08001752 void startActivityForResultSafely(Intent intent, int requestCode) {
1753 try {
1754 startActivityForResult(intent, requestCode);
1755 } catch (ActivityNotFoundException e) {
1756 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1757 } catch (SecurityException e) {
1758 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1759 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1760 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1761 "or use the exported attribute for this activity.", e);
1762 }
1763 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764
Adam Cohena9cf38f2011-05-02 15:36:58 -07001765 private void handleFolderClick(FolderIcon folderIcon) {
1766 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001767 Folder openFolder = mWorkspace.getFolderForTag(info);
1768
1769 // If the folder info reports that the associated folder is open, then verify that
1770 // it is actually opened. There have been a few instances where this gets out of sync.
1771 if (info.opened && openFolder == null) {
1772 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1773 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1774 info.opened = false;
1775 }
1776
Adam Cohena9cf38f2011-05-02 15:36:58 -07001777 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 // Close any open folder
1779 closeFolder();
1780 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001781 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 } else {
1783 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 int folderScreen;
1785 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001786 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 // .. and close it
1788 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001789 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 // Close any folder open on the current screen
1791 closeFolder();
1792 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001793 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 }
1795 }
1796 }
1797 }
1798
Adam Cohen2801caf2011-05-13 20:57:39 -07001799 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001800 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001801 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1802 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1803 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1804
Adam Cohenc51934b2011-07-26 21:07:43 -07001805 FolderInfo info = (FolderInfo) fi.getTag();
1806 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1807 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001808 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1809 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001810 }
1811
Adam Cohen2801caf2011-05-13 20:57:39 -07001812 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1813 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1814 oa.start();
1815 }
1816
1817 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001818 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001819 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1820 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1821 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1822
Adam Cohenc51934b2011-07-26 21:07:43 -07001823 FolderInfo info = (FolderInfo) fi.getTag();
1824 CellLayout cl = null;
1825 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1826 cl = (CellLayout) fi.getParent().getParent();
1827 }
1828
1829 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001830 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1831 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001832 oa.addListener(new AnimatorListenerAdapter() {
1833 @Override
1834 public void onAnimationEnd(Animator animation) {
1835 if (layout != null) {
1836 layout.clearFolderLeaveBehind();
1837 }
1838 }
1839 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001840 oa.start();
1841 }
1842
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001844 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 * is animated relative to the specified View. If the View is null, no animation
1846 * is played.
1847 *
1848 * @param folderInfo The FolderInfo describing the folder to open.
1849 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001850 public void openFolder(FolderIcon folderIcon) {
1851 Folder folder = folderIcon.mFolder;
1852 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853
Adam Cohen2801caf2011-05-13 20:57:39 -07001854 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001855 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001856
Adam Cohen4554ee12011-08-03 16:13:21 -07001857 // Just verify that the folder hasn't already been added to the DragLayer.
1858 // There was a one-off crash where the folder had a parent already.
1859 if (folder.getParent() == null) {
1860 mDragLayer.addView(folder);
1861 mDragController.addDropTarget((DropTarget) folder);
1862 } else {
1863 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1864 folder.getParent() + ").");
1865 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001866 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001867 }
1868
1869 public void closeFolder() {
1870 Folder folder = mWorkspace.getOpenFolder();
1871 if (folder != null) {
1872 closeFolder(folder);
1873 }
1874 }
1875
1876 void closeFolder(Folder folder) {
1877 folder.getInfo().opened = false;
1878
1879 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1880 if (parent != null) {
1881 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1882 shrinkAndFadeInFolderIcon(fi);
1883 }
1884 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 }
1886
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001888 if (mState != State.WORKSPACE) {
1889 return false;
1890 }
1891
Joe Onorato9c1289c2009-08-17 11:03:03 -04001892 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 return false;
1894 }
1895
1896 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001897 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 }
1899
Michael Jurka0280c3b2010-09-17 15:00:07 -07001900 resetAddInfo();
1901 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001903 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 return true;
1905 }
1906
Winson Chung3d503fb2011-07-13 17:25:49 -07001907 // The hotseat touch handling does not go through Workspace, and we always allow long press
1908 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001909 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001910 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1911 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001912 if (itemUnderLongClick == null) {
1913 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001914 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1915 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001916 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001918 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001920 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 }
1922 }
1923 }
1924 return true;
1925 }
1926
Winson Chung3d503fb2011-07-13 17:25:49 -07001927 boolean isHotseatLayout(View layout) {
1928 return mHotseat != null && layout != null &&
1929 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1930 }
1931 Hotseat getHotseat() {
1932 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001933 }
1934
Winson Chung3d503fb2011-07-13 17:25:49 -07001935 /**
1936 * Returns the CellLayout of the specified container at the specified screen.
1937 */
1938 CellLayout getCellLayout(long container, int screen) {
1939 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1940 if (mHotseat != null) {
1941 return mHotseat.getLayout();
1942 } else {
1943 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001944 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001945 } else {
1946 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001947 }
1948 }
1949
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 Workspace getWorkspace() {
1951 return mWorkspace;
1952 }
1953
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 @Override
1955 protected Dialog onCreateDialog(int id) {
1956 switch (id) {
1957 case DIALOG_CREATE_SHORTCUT:
1958 return new CreateShortcut().createDialog();
1959 case DIALOG_RENAME_FOLDER:
1960 return new RenameFolder().createDialog();
1961 }
1962
1963 return super.onCreateDialog(id);
1964 }
1965
1966 @Override
1967 protected void onPrepareDialog(int id, Dialog dialog) {
1968 switch (id) {
1969 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001970 break;
1971 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001972 if (mFolderInfo != null) {
1973 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1974 final CharSequence text = mFolderInfo.title;
1975 input.setText(text);
1976 input.setSelection(0, text.length());
1977 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 break;
1979 }
1980 }
1981
1982 void showRenameDialog(FolderInfo info) {
1983 mFolderInfo = info;
1984 mWaitingForResult = true;
1985 showDialog(DIALOG_RENAME_FOLDER);
1986 }
1987
Adam Cohenfbba09b2011-07-18 21:43:05 -07001988 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001989 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001990 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
1991 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992 mWaitingForResult = true;
1993 showDialog(DIALOG_CREATE_SHORTCUT);
1994 }
1995
1996 private class RenameFolder {
1997 private EditText mInput;
1998
1999 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2001 mInput = (EditText) layout.findViewById(R.id.folder_name);
2002
2003 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2004 builder.setIcon(0);
2005 builder.setTitle(getString(R.string.rename_folder_title));
2006 builder.setCancelable(true);
2007 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2008 public void onCancel(DialogInterface dialog) {
2009 cleanup();
2010 }
2011 });
2012 builder.setNegativeButton(getString(R.string.cancel_action),
2013 new Dialog.OnClickListener() {
2014 public void onClick(DialogInterface dialog, int which) {
2015 cleanup();
2016 }
2017 }
2018 );
2019 builder.setPositiveButton(getString(R.string.rename_action),
2020 new Dialog.OnClickListener() {
2021 public void onClick(DialogInterface dialog, int which) {
2022 changeFolderName();
2023 }
2024 }
2025 );
2026 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002027
2028 final AlertDialog dialog = builder.create();
2029 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2030 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002031 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002032 mInput.requestFocus();
2033 InputMethodManager inputManager = (InputMethodManager)
2034 getSystemService(Context.INPUT_METHOD_SERVICE);
2035 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002036 }
2037 });
2038
2039 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 }
2041
2042 private void changeFolderName() {
2043 final String name = mInput.getText().toString();
2044 if (!TextUtils.isEmpty(name)) {
2045 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002046 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 mFolderInfo.title = name;
2048 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2049
Joe Onorato9c1289c2009-08-17 11:03:03 -04002050 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002051 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002052 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 } else {
2054 final FolderIcon folderIcon = (FolderIcon)
2055 mWorkspace.getViewForTag(mFolderInfo);
2056 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002057 // TODO: At some point we'll probably want some version of setting
2058 // the text for a folder icon.
2059 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 getWorkspace().requestLayout();
2061 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002062 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002063 mWorkspaceLoading = true;
2064 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 }
2066 }
2067 }
2068 cleanup();
2069 }
2070
2071 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 dismissDialog(DIALOG_RENAME_FOLDER);
2073 mWaitingForResult = false;
2074 mFolderInfo = null;
2075 }
2076 }
2077
Daniel Sandler843e8602010-06-07 14:59:01 -04002078 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2079 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002080 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002081 }
2082
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002083 // AllAppsView.Watcher
2084 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002085 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002086 mWorkspace.setVisibility(View.GONE);
2087 }
2088 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002089
2090 /**
2091 * Helper method for the cameraZoomIn/cameraZoomOut animations
2092 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002093 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002094 * @param scaleFactor The scale factor used for the zoom
2095 */
2096 private void setPivotsForZoom(View view, State state, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002097 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002098 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002099 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002100
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002101 void updateWallpaperVisibility(boolean visible) {
2102 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2103 int curflags = getWindow().getAttributes().flags
2104 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2105 if (wpflags != curflags) {
2106 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2107 }
2108 }
2109
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002110 /**
2111 * Zoom the camera out from the workspace to reveal 'toView'.
2112 * Assumes that the view to show is anchored at either the very top or very bottom
2113 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002114 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002115 */
Winson Chungc07918d2011-07-01 15:35:26 -07002116 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002117 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002118
Winson Chungf0ea4d32011-06-06 14:27:16 -07002119 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2120 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2121 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2122 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002123 final int startDelay =
2124 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002125
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002126 setPivotsForZoom(toView, toState, scale);
2127
Michael Jurkad74c9842011-07-10 12:44:21 -07002128 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002129 mWorkspace.changeState(Workspace.State.SMALL, animated);
Adam Cohencff6af82011-09-13 14:51:53 -07002130 //hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002131
2132 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002133 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002134 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002135 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2136 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002137 ((View) toView.getParent()).invalidate();
2138 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002139 toView.setFastScaleX(a * scale + b * 1f);
2140 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002141 }
2142 });
Adam Cohen61033d32010-11-15 18:29:44 -08002143
Winson Chungf0ea4d32011-06-06 14:27:16 -07002144 toView.setVisibility(View.VISIBLE);
2145 toView.setFastAlpha(0f);
2146 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2147 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2148 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2149 public void onAnimationUpdate(float a, float b) {
2150 // don't need to invalidate because we do so above
2151 toView.setFastAlpha(a * 0f + b * 1f);
2152 }
2153 });
Adam Cohencff6af82011-09-13 14:51:53 -07002154 alphaAnim.setStartDelay(startDelay);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002155 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002156
Michael Jurkaabded662011-03-04 12:06:57 -08002157 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002158 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim, false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002159 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002160 scaleAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002161 boolean animationCancelled = false;
2162
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002163 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002164 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002165 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002166 // Prepare the position
2167 toView.setTranslationX(0.0f);
2168 toView.setTranslationY(0.0f);
2169 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002170 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002171 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002172 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002173 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002174 // If we don't set the final scale values here, if this animation is cancelled
2175 // it will have the wrong scale value and subsequent cameraPan animations will
2176 // not fix that
2177 toView.setScaleX(1.0f);
2178 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002179 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002180 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002181 }
Winson Chung32174c82011-07-19 15:47:55 -07002182
2183 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2184 // Hide the workspace scrollbar
2185 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002186 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002187 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002188 if (!animationCancelled) {
2189 updateWallpaperVisibility(false);
2190 }
2191 }
2192
Adam Cohencff6af82011-09-13 14:51:53 -07002193 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002194 public void onAnimationCancel(Animator animation) {
2195 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002196 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002197 });
2198
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002199 // toView should appear right at the end of the workspace shrink animation
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002200
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002201 if (mStateAnimation != null) mStateAnimation.cancel();
2202 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002203 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002204 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002205 } else {
2206 toView.setTranslationX(0.0f);
2207 toView.setTranslationY(0.0f);
2208 toView.setScaleX(1.0f);
2209 toView.setScaleY(1.0f);
2210 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002211 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002212 if (toView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002213 ((LauncherTransitionable) toView).onLauncherTransitionStart(null, false);
2214 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002215
Winson Chungc07918d2011-07-01 15:35:26 -07002216 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2217 // Hide the workspace scrollbar
2218 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002219 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002220 }
Michael Jurkaabded662011-03-04 12:06:57 -08002221 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002222 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002223 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002224 }
2225
2226 /**
2227 * Zoom the camera back into the workspace, hiding 'fromView'.
2228 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002229 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002230 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002231 */
Winson Chungc07918d2011-07-01 15:35:26 -07002232 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002233 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002234
Winson Chungf0ea4d32011-06-06 14:27:16 -07002235 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2236 final float scaleFactor = (float)
2237 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2238 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002239
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002240 setPivotsForZoom(fromView, fromState, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002241 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002242 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002243 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002244 if (mStateAnimation != null) mStateAnimation.cancel();
2245 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002246
Michael Jurka742574b2011-02-02 23:51:01 -08002247 final float oldScaleX = fromView.getScaleX();
2248 final float oldScaleY = fromView.getScaleY();
2249
2250 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2251 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002252 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2253 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002254 ((View)fromView.getParent()).fastInvalidate();
2255 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2256 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2257 }
2258 });
Michael Jurkaabded662011-03-04 12:06:57 -08002259 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002260 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Adam Cohencff6af82011-09-13 14:51:53 -07002261 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002262 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2263 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002264 // don't need to invalidate because we do so above
2265 fromView.setFastAlpha(a * 1f + b * 0f);
2266 }
2267 });
Michael Jurkaabded662011-03-04 12:06:57 -08002268 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002269 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002270 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002271 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002272 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002273 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002274 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002275 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002276 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002277 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim,true);
Michael Jurka2763be32011-02-24 11:19:57 -08002278 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002279 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002280 }
2281 });
2282
Winson Chungf0ea4d32011-06-06 14:27:16 -07002283 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002284 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002285 } else {
2286 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002287 if (fromView instanceof LauncherTransitionable) {
Winson Chung5a808352011-06-27 19:08:49 -07002288 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null, true);
2289 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002290 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002291 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002292 }
2293
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002294 void showWorkspace(boolean animated) {
Adam Cohencff6af82011-09-13 14:51:53 -07002295 Resources res = getResources();
2296 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2297
2298 mWorkspace.changeState(Workspace.State.NORMAL, animated, stagger);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002299 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002300 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002301 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002302
Adam Cohen7777d962011-08-18 18:58:38 -07002303 mWorkspace.showDockDivider(!animated);
2304 mWorkspace.flashScrollingIndicator();
2305
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002306 // Change the state *after* we've called all the transition code
2307 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002308
Winson Chung5fb63472011-02-02 17:03:37 -08002309 // Resume the auto-advance of widgets
2310 mUserPresent = true;
2311 updateRunning();
2312
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002313 // send an accessibility event to announce the context change
2314 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002315 }
2316
Adam Cohen7777d962011-08-18 18:58:38 -07002317 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002318 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002319 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Winson Chungb26f3d62011-06-02 10:49:29 -07002320 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002321 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002322 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002323 }
Adam Cohen7777d962011-08-18 18:58:38 -07002324
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002325 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chung09bfc452011-09-09 11:30:20 -07002326 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2327
Winson Chunge7a03942011-08-05 15:05:12 -07002328 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002329 @Override
2330 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002331 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002332 // Before we show workspace, hide all apps again because
2333 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2334 // clean up our state transition functions
2335 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohen7777d962011-08-18 18:58:38 -07002336 mSearchDeleteBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002337 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002338 } else {
2339 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002340 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002341 }
2342 }, (extendedDelay ?
2343 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2344 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2345 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002346 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002347 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002348 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2349 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002350 }
2351 // Otherwise, we are not in spring loaded mode, so don't do anything.
2352 }
2353
Adam Cohenfc53cd22011-07-20 15:45:11 -07002354 public boolean isAllAppsCustomizeOpen() {
2355 return mState == State.APPS_CUSTOMIZE;
2356 }
2357
Winson Chungf0ea4d32011-06-06 14:27:16 -07002358 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002359 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002360 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002361 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002362 if (!LauncherApplication.isScreenLarge()) {
2363 if (animated) {
2364 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002365 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002366 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002367 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002368 }
2369 }
2370 }
2371
2372 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002373 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002374 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002375 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002376 if (!LauncherApplication.isScreenLarge()) {
2377 if (animated) {
2378 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002379 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002380 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002381 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002382 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002383 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002384 }
2385
Winson Chung785d2eb2011-04-14 16:08:02 -07002386 void showAllApps(boolean animated) {
2387 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002388
Winson Chungf0ea4d32011-06-06 14:27:16 -07002389 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2390 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002391
Winson Chung3d503fb2011-07-13 17:25:49 -07002392 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002393 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002394
Winson Chungf0ea4d32011-06-06 14:27:16 -07002395 // Change the state *after* we've called all the transition code
2396 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002397
2398 // Pause the auto-advance of widgets until we are out of AllApps
2399 mUserPresent = false;
2400 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002401 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002402
2403 // Send an accessibility event to announce the context change
2404 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2405 }
2406
Joe Onoratob2061212009-11-24 16:13:54 -05002407 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002408 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002409 * - Home from workspace
2410 * - from center screen
2411 * - from other screens
2412 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002413 * - from center screen
2414 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002415 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002416 * - from center screen
2417 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002418 * - Launch app from workspace and quit
2419 * - with back
2420 * - with home
2421 * - Launch app from all apps and quit
2422 * - with back
2423 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002424 * - Go to a screen that's not the default, then all
2425 * apps, and launch and app, and go back
2426 * - with back
2427 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002428 * - On workspace, long press power and go back
2429 * - with back
2430 * - with home
2431 * - On all apps, long press power and go back
2432 * - with back
2433 * - with home
2434 * - On workspace, power off
2435 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002436 * - Launch an app and turn off the screen while in that app
2437 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002438 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002439 * - From all apps
2440 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002441 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2442 * - From all apps
2443 * - From the center workspace
2444 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002445 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002446 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002447 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2448 mWorkspace.setVisibility(View.VISIBLE);
2449 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002450
Winson Chung3d503fb2011-07-13 17:25:49 -07002451 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002452 mSearchDeleteBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002453
Winson Chungf0ea4d32011-06-06 14:27:16 -07002454 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002455 if (mAllAppsButton != null) {
2456 mAllAppsButton.requestFocus();
2457 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002458 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002459 }
2460
Joe Onorato7c312c12009-08-13 21:36:53 -07002461 void lockAllApps() {
2462 // TODO
2463 }
2464
2465 void unlockAllApps() {
2466 // TODO
2467 }
2468
Patrick Dubroy5f445422011-02-18 14:35:21 -08002469 /**
2470 * Add an item from all apps or customize onto the given workspace screen.
2471 * If layout is null, add to the current screen.
2472 */
2473 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002474 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2475 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002476 } else {
2477 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002478 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002479 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002480
Winson Chungdff8ebb2011-09-08 17:25:31 -07002481 /** Maps the current orientation to an index for referencing orientation correct global icons */
2482 private int getCurrentOrientationIndexForGlobalIcons() {
2483 // default - 0, landscape - 1
2484 switch (getResources().getConfiguration().orientation) {
2485 case Configuration.ORIENTATION_LANDSCAPE:
2486 return 1;
2487 default:
2488 return 0;
2489 }
2490 }
2491
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002492 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002493 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002494 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002495 // Look for the toolbar icon specified in the activity meta-data
2496 Bundle metaData = packageManager.getActivityInfo(
2497 activityName, PackageManager.GET_META_DATA).metaData;
2498 if (metaData != null) {
2499 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2500 if (iconResId != 0) {
2501 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002502 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002503 }
2504 }
2505 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002506 // This can happen if the activity defines an invalid drawable
2507 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2508 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002509 } catch (Resources.NotFoundException nfe) {
2510 // This can happen if the activity defines an invalid drawable
2511 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2512 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002513 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002514 return null;
2515 }
2516
2517 // if successful in getting icon, return it; otherwise, set button to use default drawable
2518 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2519 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2520 TextView button = (TextView) findViewById(buttonId);
2521 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002522 Resources r = getResources();
2523 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2524 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002525
2526 // If we were unable to find the icon via the meta-data, use a generic one
2527 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002528 toolbarIcon = r.getDrawable(fallbackDrawableId);
2529 toolbarIcon.setBounds(0, 0, w, h);
2530 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002531 return null;
2532 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002533 toolbarIcon.setBounds(0, 0, w, h);
2534 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002535 return toolbarIcon.getConstantState();
2536 }
2537 }
2538
2539 // if successful in getting icon, return it; otherwise, set button to use default drawable
2540 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2541 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2542 ImageView button = (ImageView) findViewById(buttonId);
2543 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2544
Michael Jurka19e0fc52011-07-22 18:00:21 -07002545 if (button != null) {
2546 // If we were unable to find the icon via the meta-data, use a
2547 // generic one
2548 if (toolbarIcon == null) {
2549 button.setImageResource(fallbackDrawableId);
2550 } else {
2551 button.setImageDrawable(toolbarIcon);
2552 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002553 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002554
2555 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2556
Michael Jurka0423dcf2010-10-05 14:56:18 -07002557 }
2558
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002559 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2560 TextView button = (TextView) findViewById(buttonId);
2561 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2562 }
2563
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002564 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002565 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002566 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002567 }
2568
Michael Jurka0423dcf2010-10-05 14:56:18 -07002569 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002570 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2571 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002572 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002573
Winson Chung1cad91e2011-05-25 17:41:01 -07002574 final SearchManager searchManager =
2575 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2576 ComponentName activityName = searchManager.getGlobalSearchActivity();
2577 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002578 int coi = getCurrentOrientationIndexForGlobalIcons();
2579 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002580 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002581 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002582 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002583 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002584 // We disable both search and voice search when there is no global search provider
Winson Chung1cad91e2011-05-25 17:41:01 -07002585 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002586 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002587 voiceButton.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002588 }
2589 }
2590
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002591 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002592 updateButtonWithDrawable(R.id.search_button, d);
2593 }
2594
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002595 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002596 final View searchDivider = findViewById(R.id.search_divider);
2597 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002598
Winson Chung1cad91e2011-05-25 17:41:01 -07002599 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2600 ComponentName activityName = intent.resolveActivity(getPackageManager());
2601 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002602 int coi = getCurrentOrientationIndexForGlobalIcons();
2603 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002604 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2605 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002606 voiceButton.setVisibility(View.VISIBLE);
2607 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002608 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002609 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002610 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002611 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002612
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002613 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002614 updateButtonWithDrawable(R.id.voice_button, d);
2615 }
2616
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002617 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002618 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002619 */
2620 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002621 final View marketButton = findViewById(R.id.market_button);
2622 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2623 // Find the app market activity by resolving an intent.
2624 // (If multiple app markets are installed, it will return the ResolverActivity.)
2625 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002626 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002627 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002628 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002629 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002630 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002631 marketButton.setVisibility(View.VISIBLE);
2632 } else {
2633 // We should hide and disable the view so that we don't try and restore the visibility
2634 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2635 marketButton.setVisibility(View.GONE);
2636 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002637 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002638 }
2639
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002640 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002641 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002642 }
2643
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002644 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002645 * Displays the shortcut creation dialog and launches, if necessary, the
2646 * appropriate activity.
2647 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002648 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002649 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2650 DialogInterface.OnShowListener {
2651
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002652 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002653
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002654 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002655 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002656
Winson Chung55b65502011-05-26 12:03:43 -07002657 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2658 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002659 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002660
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002661 AlertDialog dialog = builder.create();
2662 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002663 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002664 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002666 return dialog;
2667 }
2668
2669 public void onCancel(DialogInterface dialog) {
2670 mWaitingForResult = false;
2671 cleanup();
2672 }
2673
Romain Guycbb89e42009-06-08 15:52:54 -07002674 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002675 mWaitingForResult = false;
2676 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002677 }
2678
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002680 try {
2681 dismissDialog(DIALOG_CREATE_SHORTCUT);
2682 } catch (Exception e) {
2683 // An exception is thrown if the dialog is not visible, which is fine
2684 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002685 }
2686
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002687 /**
2688 * Handle the action clicked in the "Add to home" dialog.
2689 */
2690 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002691 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002692
Winson Chung55b65502011-05-26 12:03:43 -07002693 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2694 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002695 case AddAdapter.ITEM_APPLICATION: {
2696 if (mAppsCustomizeTabHost != null) {
2697 mAppsCustomizeTabHost.selectAppsTab();
2698 }
2699 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002700 break;
2701 }
Winson Chung55b65502011-05-26 12:03:43 -07002702 case AddAdapter.ITEM_APPWIDGET: {
2703 if (mAppsCustomizeTabHost != null) {
2704 mAppsCustomizeTabHost.selectWidgetsTab();
2705 }
2706 showAllApps(true);
2707 break;
2708 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002709 case AddAdapter.ITEM_WALLPAPER: {
2710 startWallpaper();
2711 break;
2712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002713 }
2714 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002715
2716 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002717 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002718 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002719 }
2720
2721 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002722 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002723 */
2724 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2725 @Override
2726 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002727 closeSystemDialogs();
2728 }
2729 }
2730
2731 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002732 * Receives notifications whenever the appwidgets are reset.
2733 */
2734 private class AppWidgetResetObserver extends ContentObserver {
2735 public AppWidgetResetObserver() {
2736 super(new Handler());
2737 }
2738
2739 @Override
2740 public void onChange(boolean selfChange) {
2741 onAppWidgetReset();
2742 }
2743 }
2744
2745 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002746 * If the activity is currently paused, signal that we need to re-run the loader
2747 * in onResume.
2748 *
2749 * This needs to be called from incoming places where resources might have been loaded
2750 * while we are paused. That is becaues the Configuration might be wrong
2751 * when we're not running, and if it comes back to what it was when we
2752 * were paused, we are not restarted.
2753 *
2754 * Implementation of the method from LauncherModel.Callbacks.
2755 *
2756 * @return true if we are currently paused. The caller might be able to
2757 * skip some work in that case since we will come back again.
2758 */
2759 public boolean setLoadOnResume() {
2760 if (mPaused) {
2761 Log.i(TAG, "setLoadOnResume");
2762 mOnResumeNeedsLoad = true;
2763 return true;
2764 } else {
2765 return false;
2766 }
2767 }
2768
2769 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002770 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002771 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002772 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002773 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002774 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002775 } else {
2776 return SCREEN_COUNT / 2;
2777 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002778 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002779
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002780
Joe Onorato9c1289c2009-08-17 11:03:03 -04002781 /**
2782 * Refreshes the shortcuts shown on the workspace.
2783 *
2784 * Implementation of the method from LauncherModel.Callbacks.
2785 */
2786 public void startBinding() {
2787 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002788
2789 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002790 int count = workspace.getChildCount();
2791 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002792 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002793 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2794 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002795 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002796 if (mHotseat != null) {
2797 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002798 }
2799 }
2800
2801 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002802 * Bind the items start-end from the list.
2803 *
2804 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002805 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002806 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002807 setLoadOnResume();
2808
Joe Onorato9c1289c2009-08-17 11:03:03 -04002809 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002810 for (int i=start; i<end; i++) {
2811 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002812
2813 // Short circuit if we are loading dock items for a configuration which has no dock
2814 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2815 mHotseat == null) {
2816 continue;
2817 }
2818
Joe Onorato9c1289c2009-08-17 11:03:03 -04002819 switch (item.itemType) {
2820 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2821 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002822 View shortcut = createShortcut((ShortcutInfo)item);
2823 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2824 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002825 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002826 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002827 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002828 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002829 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002830 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2831 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002832 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002834 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002835 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002836 }
2837
Joe Onorato9c1289c2009-08-17 11:03:03 -04002838 /**
2839 * Implementation of the method from LauncherModel.Callbacks.
2840 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002841 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002842 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002843 sFolders.clear();
2844 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002846
2847 /**
2848 * Add the views for a widget to the workspace.
2849 *
2850 * Implementation of the method from LauncherModel.Callbacks.
2851 */
2852 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002853 setLoadOnResume();
2854
Daniel Sandler843e8602010-06-07 14:59:01 -04002855 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2856 if (DEBUG_WIDGETS) {
2857 Log.d(TAG, "bindAppWidget: " + item);
2858 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002859 final Workspace workspace = mWorkspace;
2860
2861 final int appWidgetId = item.appWidgetId;
2862 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002863 if (DEBUG_WIDGETS) {
2864 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2865 }
2866
Joe Onorato9c1289c2009-08-17 11:03:03 -04002867 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2868
Joe Onorato9c1289c2009-08-17 11:03:03 -04002869 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2870 item.hostView.setTag(item);
2871
Winson Chung3d503fb2011-07-13 17:25:49 -07002872 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002873 item.cellY, item.spanX, item.spanY, false);
2874
Adam Cohended9f8d2010-11-03 13:25:16 -07002875 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2876
Joe Onorato9c1289c2009-08-17 11:03:03 -04002877 workspace.requestLayout();
2878
Daniel Sandler843e8602010-06-07 14:59:01 -04002879 if (DEBUG_WIDGETS) {
2880 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2881 + (SystemClock.uptimeMillis()-start) + "ms");
2882 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002883 }
2884
2885 /**
2886 * Callback saying that there aren't any more items to bind.
2887 *
2888 * Implementation of the method from LauncherModel.Callbacks.
2889 */
2890 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002891 setLoadOnResume();
2892
Joe Onorato9c1289c2009-08-17 11:03:03 -04002893 if (mSavedState != null) {
2894 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002895 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002896 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002897 mSavedState = null;
2898 }
2899
2900 if (mSavedInstanceState != null) {
2901 super.onRestoreInstanceState(mSavedInstanceState);
2902 mSavedInstanceState = null;
2903 }
2904
Joe Onorato9c1289c2009-08-17 11:03:03 -04002905 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002906
2907 // If we received the result of any pending adds while the loader was running (e.g. the
2908 // widget configuration forced an orientation change), process them now.
2909 for (int i = 0; i < sPendingAddList.size(); i++) {
2910 completeAdd(sPendingAddList.get(i));
2911 }
2912 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002913 }
2914
2915 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002916 * Updates the icons on the launcher that are affected by changes to the package list
2917 * on the device.
2918 */
2919 private void updateIconsAffectedByPackageManagerChanges() {
2920 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002921 updateVoiceSearchIcon();
2922 }
2923
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002924 @Override
2925 public void bindSearchablesChanged() {
2926 updateGlobalSearchIcon();
2927 }
2928
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002929 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002930 * Add the icons for all apps.
2931 *
2932 * Implementation of the method from LauncherModel.Callbacks.
2933 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002934 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2935 // Remove the progress bar entirely; we could also make it GONE
2936 // but better to remove it since we know it's not going to be used
2937 View progressBar = mAppsCustomizeTabHost.
2938 findViewById(R.id.apps_customize_progress_bar);
2939 if (progressBar != null) {
2940 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002941 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002942 // We just post the call to setApps so the user sees the progress bar
2943 // disappear-- otherwise, it just looks like the progress bar froze
2944 // which doesn't look great
2945 mAppsCustomizeTabHost.post(new Runnable() {
2946 public void run() {
2947 if (mAppsCustomizeContent != null) {
2948 mAppsCustomizeContent.setApps(apps);
2949 }
2950 }
2951 });
2952
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002953 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002954 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002955 }
2956
2957 /**
2958 * A package was installed.
2959 *
2960 * Implementation of the method from LauncherModel.Callbacks.
2961 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002962 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002963 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002964 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002965
Winson Chung785d2eb2011-04-14 16:08:02 -07002966 if (mAppsCustomizeContent != null) {
2967 mAppsCustomizeContent.addApps(apps);
2968 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002969 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002970 }
2971
2972 /**
2973 * A package was updated.
2974 *
2975 * Implementation of the method from LauncherModel.Callbacks.
2976 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002977 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002978 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002979 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08002980 if (mWorkspace != null) {
2981 mWorkspace.updateShortcuts(apps);
2982 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002983
Winson Chung785d2eb2011-04-14 16:08:02 -07002984 if (mAppsCustomizeContent != null) {
2985 mAppsCustomizeContent.updateApps(apps);
2986 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002987 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002988 }
2989
2990 /**
2991 * A package was uninstalled.
2992 *
2993 * Implementation of the method from LauncherModel.Callbacks.
2994 */
Joe Onorato36115782010-06-17 13:28:48 -04002995 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002996 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04002997 if (permanent) {
2998 mWorkspace.removeItems(apps);
2999 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003000
Winson Chung785d2eb2011-04-14 16:08:02 -07003001 if (mAppsCustomizeContent != null) {
3002 mAppsCustomizeContent.removeApps(apps);
3003 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003004 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003005 }
Joe Onoratobe386092009-11-17 17:32:16 -08003006
3007 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003008 * A number of packages were updated.
3009 */
3010 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003011
Winson Chung785d2eb2011-04-14 16:08:02 -07003012 if (mAppsCustomizeContent != null) {
3013 mAppsCustomizeContent.onPackagesUpdated();
3014 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003015 }
3016
Winson Chung400438b2011-01-16 17:53:48 -08003017 private int mapConfigurationOriActivityInfoOri(int configOri) {
3018 final Display d = getWindowManager().getDefaultDisplay();
3019 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3020 switch (d.getRotation()) {
3021 case Surface.ROTATION_0:
3022 case Surface.ROTATION_180:
3023 // We are currently in the same basic orientation as the natural orientation
3024 naturalOri = configOri;
3025 break;
3026 case Surface.ROTATION_90:
3027 case Surface.ROTATION_270:
3028 // We are currently in the other basic orientation to the natural orientation
3029 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3030 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3031 break;
3032 }
3033
3034 int[] oriMap = {
3035 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3036 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3037 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3038 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3039 };
3040 // Since the map starts at portrait, we need to offset if this device's natural orientation
3041 // is landscape.
3042 int indexOffset = 0;
3043 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3044 indexOffset = 1;
3045 }
3046 return oriMap[(d.getRotation() + indexOffset) % 4];
3047 }
Adam Cohen446e9402011-09-15 18:21:21 -07003048
3049 public void lockScreenOrientationOnLargeUI() {
3050 if (LauncherApplication.isScreenLarge()) {
3051 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3052 .getConfiguration().orientation));
3053 }
Winson Chung400438b2011-01-16 17:53:48 -08003054 }
Adam Cohen446e9402011-09-15 18:21:21 -07003055 public void unlockScreenOrientationOnLargeUI() {
3056 if (LauncherApplication.isScreenLarge()) {
3057 mHandler.postDelayed(new Runnable() {
3058 public void run() {
3059 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3060 }
3061 }, mRestoreScreenOrientationDelay);
3062 }
Winson Chung400438b2011-01-16 17:53:48 -08003063 }
3064
Winson Chung82f55532011-08-09 14:14:23 -07003065 /* Cling related */
3066 private static final String WORKSPACE_CLING_DISMISSED_KEY = "cling.workspace.dismissed";
3067 private static final String ALLAPPS_CLING_DISMISSED_KEY = "cling.allapps.dismissed";
3068 private void enableClingsIfNecessary() {
3069 // TEMPORARY: DISABLE CLINGS ON LARGE UI
3070 if (LauncherApplication.isScreenLarge()) return;
3071
Brett Chabot2a9e2282011-08-23 15:03:13 -07003072 // disable clings when running in a test harness
3073 if(ActivityManager.isRunningInTestHarness()) return;
3074
Winson Chung82f55532011-08-09 14:14:23 -07003075 // Enable the clings only if they have not been dismissed before
3076 SharedPreferences prefs =
3077 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3078 if (!prefs.getBoolean(WORKSPACE_CLING_DISMISSED_KEY, false)) {
3079 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3080 cling.init(this);
3081 cling.setVisibility(View.VISIBLE);
3082 }
3083 if (!prefs.getBoolean(ALLAPPS_CLING_DISMISSED_KEY, false)) {
3084 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3085 cling.init(this);
3086 cling.setVisibility(View.VISIBLE);
3087 }
3088 }
3089 private void dismissCling(final Cling cling, final String flag) {
3090 if (cling != null) {
3091 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
3092 anim.setDuration(DISMISS_CLING_DURATION);
3093 anim.addListener(new AnimatorListenerAdapter() {
3094 public void onAnimationEnd(Animator animation) {
3095 cling.setVisibility(View.GONE);
3096 cling.cleanup();
3097 SharedPreferences prefs =
3098 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3099 SharedPreferences.Editor editor = prefs.edit();
3100 editor.putBoolean(flag, true);
3101 editor.commit();
3102 };
3103 });
3104 anim.start();
3105 }
3106 }
3107 public void dismissWorkspaceCling(View v) {
3108 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3109 dismissCling(cling, WORKSPACE_CLING_DISMISSED_KEY);
3110 }
3111 public void dismissAllAppsCling(View v) {
3112 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
3113 dismissCling(cling, ALLAPPS_CLING_DISMISSED_KEY);
3114 }
3115
Winson Chung80baf5a2010-08-09 16:03:15 -07003116 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003117 * Prints out out state for debugging.
3118 */
3119 public void dumpState() {
3120 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003121 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003122 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3123 Log.d(TAG, "mRestoring=" + mRestoring);
3124 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3125 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003126 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003127 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003128
Winson Chung785d2eb2011-04-14 16:08:02 -07003129 if (mAppsCustomizeContent != null) {
3130 mAppsCustomizeContent.dumpState();
3131 }
Joe Onoratobe386092009-11-17 17:32:16 -08003132 Log.d(TAG, "END launcher2 dump state");
3133 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003134}
Michael Jurka2763be32011-02-24 11:19:57 -08003135
Michael Jurkaabded662011-03-04 12:06:57 -08003136interface LauncherTransitionable {
Winson Chung5a808352011-06-27 19:08:49 -07003137 void onLauncherTransitionStart(Animator animation, boolean toWorkspace);
3138 void onLauncherTransitionEnd(Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003139}