blob: 1fbf7a5a66ea2c429c1391922b5c17049f63ab47 [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 Cohen82ebbd22011-09-30 15:05:40 -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;
Winson Chung7a74ac92011-09-20 17:43:51 -070083import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080084import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070086import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070088import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070089import android.widget.TextView;
90import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070091
Adam Cohendf2cc412011-04-27 16:56:57 -070092import com.android.common.Search;
93import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070094import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080095
Adam Cohenc0dcf592011-06-01 15:30:43 -070096import java.io.DataInputStream;
97import java.io.DataOutputStream;
98import java.io.FileNotFoundException;
99import java.io.IOException;
100import java.util.ArrayList;
101import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103/**
104 * Default launcher application.
105 */
Michael Jurka946ad472010-07-09 18:05:18 -0700106public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700107 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
108 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800109 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700110 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Joe Onorato9c1289c2009-08-17 11:03:03 -0400112 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400113 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700114
Winson Chung70d72102011-08-12 11:24:35 -0700115 private static final int MENU_GROUP_WALLPAPER = 1;
116 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
117 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700118 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
119 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
121 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700122 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int REQUEST_PICK_APPLICATION = 6;
124 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700125 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700126 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
128 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
129
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800130 static final int SCREEN_COUNT = 5;
131 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
Joe Onorato7c312c12009-08-13 21:36:53 -0700133 static final int DIALOG_CREATE_SHORTCUT = 1;
134 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Romain Guy98d01652009-06-30 16:21:04 -0700136 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
138 // Type: int
139 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700140 // Type: int
141 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700143 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
144 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
146 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700147 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700149 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 // Type: boolean
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
152 // Type: long
153 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
154
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700155 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
156
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700157 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700158 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700159 private State mState = State.WORKSPACE;
160 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700161
Joe Onorato9c1289c2009-08-17 11:03:03 -0400162 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700163 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
164 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700165 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700166 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800169 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800171 private final BroadcastReceiver mCloseSystemDialogsReceiver
172 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800173 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private LayoutInflater mInflater;
176
Joe Onorato00acb122009-08-04 16:04:30 -0400177 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700179
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700180 private AppWidgetManager mAppWidgetManager;
181 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700182
Michael Jurkac9d95c52011-08-29 14:03:34 -0700183 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700184 private int[] mTmpAddItemCellCoordinates = new int[2];
185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private FolderInfo mFolderInfo;
187
Winson Chung3d503fb2011-07-13 17:25:49 -0700188 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800189 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700190
Winson Chungf0ea4d32011-06-06 14:27:16 -0700191 private SearchDropTargetBar mSearchDeleteBar;
192 private AppsCustomizeTabHost mAppsCustomizeTabHost;
193 private AppsCustomizePagedView mAppsCustomizeContent;
194 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 private Bundle mSavedState;
197
198 private SpannableStringBuilder mDefaultKeySsb = null;
199
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 private boolean mWorkspaceLoading = true;
201
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800202 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 private boolean mRestoring;
204 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700205 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
207 private Bundle mSavedInstanceState;
208
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800210 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800211 private boolean mUserPresent = true;
212 private boolean mVisible = false;
213 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700215 private static LocaleConfiguration sLocaleConfiguration = null;
216
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700217 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700218
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700219 private Intent mAppMarketIntent = null;
220
Adam Cohended9f8d2010-11-03 13:25:16 -0700221 // Related to the auto-advancing of widgets
222 private final int ADVANCE_MSG = 1;
223 private final int mAdvanceInterval = 20000;
224 private final int mAdvanceStagger = 250;
225 private long mAutoAdvanceSentTime;
226 private long mAutoAdvanceTimeLeft = -1;
227 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
228 new HashMap<View, AppWidgetProviderInfo>();
229
Winson Chung400438b2011-01-16 17:53:48 -0800230 // Determines how long to wait after a rotation before restoring the screen orientation to
231 // match the sensor state.
232 private final int mRestoreScreenOrientationDelay = 500;
233
Michael Jurka4ef207b2010-11-29 17:05:45 -0800234 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700235 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
236 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
237 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800238
Adam Cohen2801caf2011-05-13 20:57:39 -0700239 private DragLayer mDragLayer;
240
Michael Jurkaddd62e92011-02-16 17:49:14 -0800241 private BubbleTextView mWaitingForResume;
242
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800243 private static ArrayList<PendingAddArguments> sPendingAddList
244 = new ArrayList<PendingAddArguments>();
245
246 private static class PendingAddArguments {
247 int requestCode;
248 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700249 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800250 int screen;
251 int cellX;
252 int cellY;
253 }
254
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 @Override
256 protected void onCreate(Bundle savedInstanceState) {
257 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800258 LauncherApplication app = ((LauncherApplication)getApplication());
259 mModel = app.setLauncher(this);
260 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400261 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700263
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700264 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700265 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
266 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700267
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200269 android.os.Debug.startMethodTracing(
270 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 }
272
273 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 setContentView(R.layout.launcher);
275 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700276 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800278 registerContentObservers();
279
Joe Onorato7c312c12009-08-13 21:36:53 -0700280 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 mSavedState = savedInstanceState;
283 restoreState(mSavedState);
284
Winson Chunga12a2502010-12-20 14:41:35 -0800285 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700286 if (mAppsCustomizeContent != null) {
287 mAppsCustomizeContent.onPackagesUpdated();
288 }
Winson Chunga12a2502010-12-20 14:41:35 -0800289
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 if (PROFILE_STARTUP) {
291 android.os.Debug.stopMethodTracing();
292 }
293
294 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400295 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 }
297
Michael Jurkac57b7a82011-08-09 22:02:20 -0700298 if (!mModel.isAllAppsLoaded()) {
299 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
300 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
301 }
302
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 // For handling default keys
304 mDefaultKeySsb = new SpannableStringBuilder();
305 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800306
307 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
308 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800309
310 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700311 int coi = getCurrentOrientationIndexForGlobalIcons();
312 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
313 sAppMarketIcon[coi] == null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700314 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100315 updateGlobalSearchIcon();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700316 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700317 if (sGlobalSearchIcon[coi] != null) {
318 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700319 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700320 if (sVoiceSearchIcon[coi] != null) {
321 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700322 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700323 if (sAppMarketIcon[coi] != null) {
324 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800325 }
Adam Cohen446e9402011-09-15 18:21:21 -0700326
327 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohendc16b602011-09-26 15:12:33 -0700328 if (LauncherApplication.isScreenLarge() || Build.TYPE.contentEquals("eng")) {
Adam Cohen446e9402011-09-15 18:21:21 -0700329 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 }
Romain Guycbb89e42009-06-08 15:52:54 -0700332
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700334 if (sLocaleConfiguration == null) {
335 new AsyncTask<Void, Void, LocaleConfiguration>() {
336 @Override
337 protected LocaleConfiguration doInBackground(Void... unused) {
338 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
339 readConfiguration(Launcher.this, localeConfiguration);
340 return localeConfiguration;
341 }
342
343 @Override
344 protected void onPostExecute(LocaleConfiguration result) {
345 sLocaleConfiguration = result;
346 checkForLocaleChange(); // recursive, but now with a locale configuration
347 }
348 }.execute();
349 return;
350 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 final Configuration configuration = getResources().getConfiguration();
353
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700354 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 final String locale = configuration.locale.toString();
356
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700357 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 final int mcc = configuration.mcc;
359
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700360 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 final int mnc = configuration.mnc;
362
Romain Guy84f296c2009-11-04 15:00:44 -0800363 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364
Romain Guy84f296c2009-11-04 15:00:44 -0800365 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700366 sLocaleConfiguration.locale = locale;
367 sLocaleConfiguration.mcc = mcc;
368 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700369
Joe Onorato0589f0f2010-02-08 13:44:00 -0800370 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700371
372 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
373 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700374 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700375 public void run() {
376 writeConfiguration(Launcher.this, localeConfiguration);
377 }
378 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700379 }
380 }
381
382 private static class LocaleConfiguration {
383 public String locale;
384 public int mcc = -1;
385 public int mnc = -1;
386 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700387
Romain Guy98d01652009-06-30 16:21:04 -0700388 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
389 DataInputStream in = null;
390 try {
391 in = new DataInputStream(context.openFileInput(PREFERENCES));
392 configuration.locale = in.readUTF();
393 configuration.mcc = in.readInt();
394 configuration.mnc = in.readInt();
395 } catch (FileNotFoundException e) {
396 // Ignore
397 } catch (IOException e) {
398 // Ignore
399 } finally {
400 if (in != null) {
401 try {
402 in.close();
403 } catch (IOException e) {
404 // Ignore
405 }
406 }
407 }
408 }
409
410 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
411 DataOutputStream out = null;
412 try {
413 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
414 out.writeUTF(configuration.locale);
415 out.writeInt(configuration.mcc);
416 out.writeInt(configuration.mnc);
417 out.flush();
418 } catch (FileNotFoundException e) {
419 // Ignore
420 } catch (IOException e) {
421 //noinspection ResultOfMethodCallIgnored
422 context.getFileStreamPath(PREFERENCES).delete();
423 } finally {
424 if (out != null) {
425 try {
426 out.close();
427 } catch (IOException e) {
428 // Ignore
429 }
430 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 }
432 }
433
Adam Cohen716b51e2011-06-30 12:09:54 -0700434 public DragLayer getDragLayer() {
435 return mDragLayer;
436 }
437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 static int getScreen() {
439 synchronized (sLock) {
440 return sScreen;
441 }
442 }
443
444 static void setScreen(int screen) {
445 synchronized (sLock) {
446 sScreen = screen;
447 }
448 }
449
Winson Chung557d6ed2011-07-08 15:34:52 -0700450 /**
451 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
452 * a configuration step, this allows the proper animations to run after other transitions.
453 */
454 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700455 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800456 switch (args.requestCode) {
457 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700458 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
459 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800460 break;
461 case REQUEST_PICK_SHORTCUT:
462 processShortcut(args.intent);
463 break;
464 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700465 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
466 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700467 result = true;
468 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800469 case REQUEST_PICK_APPWIDGET:
470 addAppWidgetFromPick(args.intent);
471 break;
472 case REQUEST_CREATE_APPWIDGET:
473 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700474 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700475 result = true;
476 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800477 case REQUEST_PICK_WALLPAPER:
478 // We just wanted the activity result here so we can clear mWaitingForResult
479 break;
480 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700481 // In any situation where we have a multi-step drop, we should reset the add info only after
482 // we complete the drop
483 resetAddInfo();
484 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800485 }
486
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800487 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800488 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700489 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700490 mWaitingForResult = false;
491
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 // The pattern used here is that a user PICKs a specific application,
493 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700494
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
496 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700497 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800498 final PendingAddArguments args = new PendingAddArguments();
499 args.requestCode = requestCode;
500 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700501 args.container = mPendingAddInfo.container;
502 args.screen = mPendingAddInfo.screen;
503 args.cellX = mPendingAddInfo.cellX;
504 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800505
506 // If the loader is still running, defer the add until it is done.
507 if (isWorkspaceLocked()) {
508 sPendingAddList.add(args);
509 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700510 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800511 }
Romain Guy18042c82009-11-06 11:44:55 -0800512 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700513 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
514 if (data != null) {
515 // Clean up the appWidgetId if we canceled
516 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
517 if (appWidgetId != -1) {
518 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
519 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800520 }
521 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700522
523 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700524 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800525 }
526
527 @Override
528 protected void onResume() {
529 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800530 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700531 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400532 mWorkspaceLoading = true;
533 mModel.startLoader(this, true);
534 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700535 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800537 if (mWaitingForResume != null) {
538 mWaitingForResume.setStayPressed(false);
539 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700540 // When we resume Launcher, a different Activity might be responsible for the app
541 // market intent, so refresh the icon
542 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 }
544
545 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700546 protected void onPause() {
547 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700548 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800549 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700550 }
Romain Guycbb89e42009-06-08 15:52:54 -0700551
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700552 @Override
553 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400554 // Flag the loader to stop early before switching
555 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700556 if (mAppsCustomizeContent != null) {
557 mAppsCustomizeContent.surrender();
558 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800559 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700560 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700561
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800562 // We can't hide the IME if it was forced open. So don't bother
563 /*
564 @Override
565 public void onWindowFocusChanged(boolean hasFocus) {
566 super.onWindowFocusChanged(hasFocus);
567
568 if (hasFocus) {
569 final InputMethodManager inputManager = (InputMethodManager)
570 getSystemService(Context.INPUT_METHOD_SERVICE);
571 WindowManager.LayoutParams lp = getWindow().getAttributes();
572 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
573 android.os.Handler()) {
574 protected void onReceiveResult(int resultCode, Bundle resultData) {
575 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
576 }
577 });
578 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
579 }
580 }
581 */
582
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 private boolean acceptFilter() {
584 final InputMethodManager inputManager = (InputMethodManager)
585 getSystemService(Context.INPUT_METHOD_SERVICE);
586 return !inputManager.isFullscreenMode();
587 }
588
589 @Override
590 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700591 final int uniChar = event.getUnicodeChar();
592 final boolean handled = super.onKeyDown(keyCode, event);
593 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
594 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800595 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
596 keyCode, event);
597 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700598 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700599 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700600 // showSearchDialog()
601 // If there are multiple keystrokes before the search dialog takes focus,
602 // onSearchRequested() will be called for every keystroke,
603 // but it is idempotent, so it's fine.
604 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 }
606 }
607
Joe Onorato8a9625e2010-01-28 15:55:35 -0800608 // Eat the long press event so the keyboard doesn't come up.
609 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
610 return true;
611 }
612
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 return handled;
614 }
615
Karl Rosaen138a0412009-04-23 19:00:21 -0700616 private String getTypedText() {
617 return mDefaultKeySsb.toString();
618 }
619
620 private void clearTypedText() {
621 mDefaultKeySsb.clear();
622 mDefaultKeySsb.clearSpans();
623 Selection.setSelection(mDefaultKeySsb, 0);
624 }
625
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800626 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700627 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
628 * State
629 */
630 private static State intToState(int stateOrdinal) {
631 State state = State.WORKSPACE;
632 final State[] stateValues = State.values();
633 for (int i = 0; i < stateValues.length; i++) {
634 if (stateValues[i].ordinal() == stateOrdinal) {
635 state = stateValues[i];
636 break;
637 }
638 }
639 return state;
640 }
641
642 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 * Restores the previous state, if it exists.
644 *
645 * @param savedState The previous state.
646 */
647 private void restoreState(Bundle savedState) {
648 if (savedState == null) {
649 return;
650 }
651
Michael Jurka883f55b2010-10-21 15:47:14 -0700652 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700653 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800654 showAllApps(false);
655 }
656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
658 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700659 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 }
661
Winson Chung3d503fb2011-07-13 17:25:49 -0700662 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
663 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700664
Winson Chung3d503fb2011-07-13 17:25:49 -0700665 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
666 mPendingAddInfo.container = pendingAddContainer;
667 mPendingAddInfo.screen = pendingAddScreen;
668 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
669 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 mRestoring = true;
671 }
672
673 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
674 if (renameFolder) {
675 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700676 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 mRestoring = true;
678 }
Winson Chunga12a2502010-12-20 14:41:35 -0800679
Winson Chung785d2eb2011-04-14 16:08:02 -0700680
681 // Restore the AppsCustomize tab
682 if (mAppsCustomizeTabHost != null) {
683 String curTab = savedState.getString("apps_customize_currentTab");
684 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700685 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700686 mAppsCustomizeContent.setContentType(
687 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
688 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700689 mAppsCustomizeContent.loadAssociatedPages(
690 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700691 }
692
Winson Chung5afbf7b2011-07-25 11:53:08 -0700693 int currentIndex = savedState.getInt("apps_customize_currentIndex");
694 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700695 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 }
697
698 /**
699 * Finds all the views we need and configure them properly.
700 */
701 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700702 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400703
Winson Chung4c98d922011-05-31 16:50:48 -0700704 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
705 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706
Winson Chung4c98d922011-05-31 16:50:48 -0700707 // Setup the drag layer
708 mDragLayer.setup(this, dragController);
709
Winson Chung3d503fb2011-07-13 17:25:49 -0700710 // Setup the hotseat
711 mHotseat = (Hotseat) findViewById(R.id.hotseat);
712 if (mHotseat != null) {
713 mHotseat.setup(this);
714 }
715
Winson Chung4c98d922011-05-31 16:50:48 -0700716 // Setup the workspace
717 mWorkspace.setHapticFeedbackEnabled(false);
718 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700719 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700720 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700721
Winson Chungf0ea4d32011-06-06 14:27:16 -0700722 // Get the search/delete bar
723 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700724
Winson Chungf0ea4d32011-06-06 14:27:16 -0700725 // Setup AppsCustomize
726 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
727 findViewById(R.id.apps_customize_pane);
728 mAppsCustomizeContent = (AppsCustomizePagedView)
729 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
730 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400731
Romain Guy1fbc1c82009-11-09 20:43:08 -0800732
Winson Chung4c98d922011-05-31 16:50:48 -0700733
Winson Chung3d503fb2011-07-13 17:25:49 -0700734 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700735 dragController.setDragScoller(mWorkspace);
736 dragController.setScrollView(mDragLayer);
737 dragController.setMoveTarget(mWorkspace);
738 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700739 if (mSearchDeleteBar != null) {
740 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800741 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 }
743
744 /**
745 * Creates a view representing a shortcut.
746 *
747 * @param info The data structure describing the shortcut.
748 *
749 * @return A View inflated from R.layout.application.
750 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800751 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700753 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 }
755
756 /**
757 * Creates a view representing a shortcut inflated from the specified resource.
758 *
759 * @param layoutResId The id of the XML layout used to create the shortcut.
760 * @param parent The group the shortcut belongs to.
761 * @param info The data structure describing the shortcut.
762 *
763 * @return A View inflated from layoutResId.
764 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800765 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800766 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
767 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800768 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 return favorite;
770 }
771
772 /**
773 * Add an application shortcut to the workspace.
774 *
775 * @param data The intent describing the application.
776 * @param cellInfo The position on screen where to create the shortcut.
777 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700778 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700779 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700780 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700781
Adam Cohenfbba09b2011-07-18 21:43:05 -0700782 // First we check if we already know the exact location where we want to add this item.
783 if (cellX >= 0 && cellY >= 0) {
784 cellXY[0] = cellX;
785 cellXY[1] = cellY;
786 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700787 showOutOfSpaceMessage();
788 return;
789 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800790
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800791 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800792
Romain Guy73b979d2009-06-09 12:57:21 -0700793 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800794 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800796 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700797 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700798 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800799 } else {
800 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 }
802 }
Romain Guycbb89e42009-06-08 15:52:54 -0700803
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 /**
805 * Add a shortcut to the workspace.
806 *
807 * @param data The intent describing the shortcut.
808 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700810 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
811 int cellY) {
812 int[] cellXY = mTmpAddItemCellCoordinates;
813 int[] touchXY = mPendingAddInfo.dropPos;
814 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700815
Michael Jurkad3ef3062010-11-23 16:23:58 -0800816 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700817
Adam Cohen558baaf2011-08-15 15:22:57 -0700818 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
819 final View view = createShortcut(info);
820
Adam Cohenfbba09b2011-07-18 21:43:05 -0700821 // First we check if we already know the exact location where we want to add this item.
822 if (cellX >= 0 && cellY >= 0) {
823 cellXY[0] = cellX;
824 cellXY[1] = cellY;
825 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700826
827 // If appropriate, either create a folder or add to an existing folder
828 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
829 true, null,null)) {
830 return;
831 }
832 DragObject dragObject = new DragObject();
833 dragObject.dragInfo = info;
834 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
835 return;
836 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700837 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800838 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700839 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800840 foundCellSpan = (result != null);
841 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700842 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800843 }
844
845 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700846 showOutOfSpaceMessage();
847 return;
848 }
849
Adam Cohen558baaf2011-08-15 15:22:57 -0700850 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851
852 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700853 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
854 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 }
856 }
857
Adam Cohen50370f32011-08-25 18:57:14 -0700858 class Padding {
859 int left = 0;
860 int right = 0;
861 int top = 0;
862 int bottom = 0;
863 }
864
Adam Cohenf814aa02011-09-01 13:48:05 -0700865 Padding getPaddingForWidget(ComponentName component) {
Adam Cohen50370f32011-08-25 18:57:14 -0700866 PackageManager packageManager = getPackageManager();
867 Padding p = new Padding();
868 android.content.pm.ApplicationInfo appInfo;
869
870 try {
Adam Cohenf814aa02011-09-01 13:48:05 -0700871 appInfo = packageManager.getApplicationInfo(component.getPackageName(), 0);
Adam Cohen50370f32011-08-25 18:57:14 -0700872 } catch (Exception e) {
873 // if we can't find the package, return 0 padding
874 return p;
875 }
876
Adam Cohenff648b52011-09-19 11:57:30 -0700877 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Adam Cohen50370f32011-08-25 18:57:14 -0700878 Resources r = getResources();
Adam Cohen82ebbd22011-09-30 15:05:40 -0700879 // The default padding values are private API currently, but will be added in
880 // API level 15. The current values are (8, 8, 8, 8).
881 p.left = r.getDimensionPixelSize(com.android.internal.
882 R.dimen.default_app_widget_padding_left);
883 p.right = r.getDimensionPixelSize(com.android.internal.
884 R.dimen.default_app_widget_padding_right);
885 p.top = r.getDimensionPixelSize(com.android.internal.
886 R.dimen.default_app_widget_padding_top);
887 p.bottom = r.getDimensionPixelSize(com.android.internal.
888 R.dimen.default_app_widget_padding_bottom);
Adam Cohen50370f32011-08-25 18:57:14 -0700889 }
890
891 return p;
892 }
893
Adam Cohenf814aa02011-09-01 13:48:05 -0700894 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
895 if (spanXY == null) {
896 spanXY = new int[2];
897 }
898
899 Padding padding = getPaddingForWidget(component);
900 // We want to account for the extra amount of padding that we are adding to the widget
901 // to ensure that it gets the full amount of space that it has requested
902 int requiredWidth = minWidth + padding.left + padding.right;
903 int requiredHeight = minHeight + padding.top + padding.bottom;
904 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
905 }
906
907 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
908 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
909 }
910
Adam Cohencbf47e32011-09-16 17:32:37 -0700911 int[] getMinResizeSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
912 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
913 }
914
Adam Cohenf814aa02011-09-01 13:48:05 -0700915 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
916 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
917 }
918
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700920 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700922 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700923 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700925 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700926 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700927
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700928 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700929 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700930
Adam Cohenf814aa02011-09-01 13:48:05 -0700931 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700932
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700934 // We have saved the position to which the widget was dragged-- this really only matters
935 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700936 int[] cellXY = mTmpAddItemCellCoordinates;
937 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700938 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700939 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
940 cellXY[0] = mPendingAddInfo.cellX;
941 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700942 foundCellSpan = true;
943 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700944 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700945 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700946 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800947 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700948 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700949 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700950 }
951
Michael Jurka0280c3b2010-09-17 15:00:07 -0700952 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800953 if (appWidgetId != -1) {
954 // Deleting an app widget ID is a void call but writes to disk before returning
955 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800956 new Thread("deleteAppWidgetId") {
957 public void run() {
958 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
959 }
960 }.start();
961 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700962 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800963 return;
964 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800965
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700966 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -0700967 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700968 launcherInfo.spanX = spanXY[0];
969 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700970
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700972 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973
974 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700976 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700977
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700978 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700980
Winson Chung3d503fb2011-07-13 17:25:49 -0700981 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400982 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700983
984 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 }
986 }
Romain Guycbb89e42009-06-08 15:52:54 -0700987
Adam Cohended9f8d2010-11-03 13:25:16 -0700988 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
989 @Override
990 public void onReceive(Context context, Intent intent) {
991 final String action = intent.getAction();
992 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
993 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700994 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700995 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700996
Winson Chungc100e8e2011-08-09 16:02:43 -0700997 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -0700998 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -0700999 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1000 mAppsCustomizeTabHost.reset();
1001 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001002 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001003 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1004 mUserPresent = true;
1005 updateRunning();
1006 }
1007 }
1008 };
1009
1010 @Override
1011 public void onAttachedToWindow() {
1012 super.onAttachedToWindow();
1013
1014 // Listen for broadcasts related to user-presence
1015 final IntentFilter filter = new IntentFilter();
1016 filter.addAction(Intent.ACTION_SCREEN_OFF);
1017 filter.addAction(Intent.ACTION_USER_PRESENT);
1018 registerReceiver(mReceiver, filter);
1019
Adam Cohend113e0c2010-11-11 10:48:05 -08001020 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001021 mVisible = true;
1022 }
1023
1024 @Override
1025 public void onDetachedFromWindow() {
1026 super.onDetachedFromWindow();
1027 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001028 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001029
Adam Cohend113e0c2010-11-11 10:48:05 -08001030 if (mAttached) {
1031 unregisterReceiver(mReceiver);
1032 mAttached = false;
1033 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001034 updateRunning();
1035 }
1036
1037 public void onWindowVisibilityChanged(int visibility) {
1038 mVisible = visibility == View.VISIBLE;
1039 updateRunning();
1040 }
1041
1042 private void sendAdvanceMessage(long delay) {
1043 mHandler.removeMessages(ADVANCE_MSG);
1044 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1045 mHandler.sendMessageDelayed(msg, delay);
1046 mAutoAdvanceSentTime = System.currentTimeMillis();
1047 }
1048
1049 private void updateRunning() {
1050 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1051 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1052 mAutoAdvanceRunning = autoAdvanceRunning;
1053 if (autoAdvanceRunning) {
1054 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1055 sendAdvanceMessage(delay);
1056 } else {
1057 if (!mWidgetsToAdvance.isEmpty()) {
1058 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1059 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1060 }
1061 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001062 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001063 }
1064 }
1065 }
1066
1067 private final Handler mHandler = new Handler() {
1068 @Override
1069 public void handleMessage(Message msg) {
1070 if (msg.what == ADVANCE_MSG) {
1071 int i = 0;
1072 for (View key: mWidgetsToAdvance.keySet()) {
1073 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1074 final int delay = mAdvanceStagger * i;
1075 if (v instanceof Advanceable) {
1076 postDelayed(new Runnable() {
1077 public void run() {
1078 ((Advanceable) v).advance();
1079 }
1080 }, delay);
1081 }
1082 i++;
1083 }
1084 sendAdvanceMessage(mAdvanceInterval);
1085 }
1086 }
1087 };
1088
1089 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001090 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001091 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1092 if (v instanceof Advanceable) {
1093 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001094 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001095 updateRunning();
1096 }
1097 }
1098
1099 void removeWidgetToAutoAdvance(View hostView) {
1100 if (mWidgetsToAdvance.containsKey(hostView)) {
1101 mWidgetsToAdvance.remove(hostView);
1102 updateRunning();
1103 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001104 }
1105
Joe Onorato9c1289c2009-08-17 11:03:03 -04001106 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001107 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001108 launcherInfo.hostView = null;
1109 }
1110
Adam Cohended9f8d2010-11-03 13:25:16 -07001111 void showOutOfSpaceMessage() {
1112 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1113 }
1114
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001115 public LauncherAppWidgetHost getAppWidgetHost() {
1116 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 }
Romain Guycbb89e42009-06-08 15:52:54 -07001118
Winson Chunga9abd0e2010-10-27 17:18:37 -07001119 public LauncherModel getModel() {
1120 return mModel;
1121 }
1122
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001123 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001124 getWindow().closeAllPanels();
1125
Winson Chung87acb482011-08-23 17:28:05 -07001126 /**
1127 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001128 try {
1129 dismissDialog(DIALOG_CREATE_SHORTCUT);
1130 // Unlock the workspace if the dialog was showing
1131 } catch (Exception e) {
1132 // An exception is thrown if the dialog is not visible, which is fine
1133 }
1134
1135 try {
1136 dismissDialog(DIALOG_RENAME_FOLDER);
1137 // Unlock the workspace if the dialog was showing
1138 } catch (Exception e) {
1139 // An exception is thrown if the dialog is not visible, which is fine
1140 }
Winson Chung87acb482011-08-23 17:28:05 -07001141 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001142
1143 // Whatever we were doing is hereby canceled.
1144 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001145 }
1146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 @Override
1148 protected void onNewIntent(Intent intent) {
1149 super.onNewIntent(intent);
1150
1151 // Close the menu
1152 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001153 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001154 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001155
Joe Onorato14f122b2009-11-19 14:06:36 -08001156 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1157 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001158
Adam Cohenac56cff2011-09-28 20:45:37 -07001159 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001160 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001161 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001162 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1163 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001164 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001165 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001166
1167 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001168 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001169 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001170
Joe Onorato3a8820b2009-11-10 15:06:42 -08001171 final View v = getWindow().peekDecorView();
1172 if (v != null && v.getWindowToken() != null) {
1173 InputMethodManager imm = (InputMethodManager)getSystemService(
1174 INPUT_METHOD_SERVICE);
1175 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001177
Michael Jurka35aa14d2011-07-07 17:01:08 -07001178 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001179 if (mAppsCustomizeTabHost != null) {
1180 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001181 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 }
1183 }
1184
1185 @Override
1186 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1187 // Do not call super here
1188 mSavedInstanceState = savedInstanceState;
1189 }
1190
1191 @Override
1192 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001193 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001194 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195
Michael Jurka883f55b2010-10-21 15:47:14 -07001196 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001197 // We close any open folder since it will not be re-opened, and we need to make sure
1198 // this state is reflected.
1199 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200
Winson Chung3d503fb2011-07-13 17:25:49 -07001201 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1202 mWaitingForResult) {
1203 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1204 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1205 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1206 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001207 }
1208
1209 if (mFolderInfo != null && mWaitingForResult) {
1210 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1211 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1212 }
Michael Jurka946ad472010-07-09 18:05:18 -07001213
Winson Chung785d2eb2011-04-14 16:08:02 -07001214 // Save the current AppsCustomize tab
1215 if (mAppsCustomizeTabHost != null) {
1216 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1217 if (currentTabTag != null) {
1218 outState.putString("apps_customize_currentTab", currentTabTag);
1219 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001220 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1221 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001222 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001223 }
1224
1225 @Override
1226 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001228
Winson Chunge7a03942011-08-05 15:05:12 -07001229 // Remove all pending runnables
1230 mHandler.removeMessages(ADVANCE_MSG);
1231 mHandler.removeMessages(0);
1232
Winson Chungcd2b0142011-06-08 16:02:26 -07001233 // Stop callbacks from LauncherModel
1234 LauncherApplication app = ((LauncherApplication) getApplication());
1235 mModel.stopLoader();
1236 app.setLauncher(null);
1237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001239 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001241 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001242 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001243 mAppWidgetHost = null;
1244
1245 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246
1247 TextKeyListener.getInstance().release();
1248
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001249
Winson Chung3d503fb2011-07-13 17:25:49 -07001250 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001251
1252 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001253 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001254
1255 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1256 mWorkspace.removeAllViews();
1257 mWorkspace = null;
1258 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001259
1260 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 }
1262
Adam Cohena9cf38f2011-05-02 15:36:58 -07001263 public DragController getDragController() {
1264 return mDragController;
1265 }
1266
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 @Override
1268 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001269 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 super.startActivityForResult(intent, requestCode);
1271 }
1272
Winson Chung70d72102011-08-12 11:24:35 -07001273 /**
1274 * Indicates that we want global search for this activity by setting the globalSearch
1275 * argument for {@link #startSearch} to true.
1276 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001278 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001280
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001281 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001282
Karl Rosaen138a0412009-04-23 19:00:21 -07001283 if (initialQuery == null) {
1284 // Use any text typed in the launcher as the initial query
1285 initialQuery = getTypedText();
1286 clearTypedText();
1287 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 if (appSearchData == null) {
1289 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001290 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
Romain Guycbb89e42009-06-08 15:52:54 -07001292
Karl Rosaen138a0412009-04-23 19:00:21 -07001293 final SearchManager searchManager =
1294 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001295 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001296 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 }
1298
Winson Chung70d72102011-08-12 11:24:35 -07001299 @Override
1300 public boolean onCreateOptionsMenu(Menu menu) {
1301 if (isWorkspaceLocked()) {
1302 return false;
1303 }
1304
1305 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001306
1307 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1308 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1309 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001310 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1311 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1312 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001313 String helpUrl = getString(R.string.help_url);
1314 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001315 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1316 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1317
Winson Chung70d72102011-08-12 11:24:35 -07001318 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1319 .setIcon(android.R.drawable.ic_menu_gallery)
1320 .setAlphabeticShortcut('W');
1321 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1322 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001323 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001324 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001325 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1326 .setIcon(android.R.drawable.ic_menu_preferences)
1327 .setIntent(settings)
1328 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001329 if (!helpUrl.isEmpty()) {
1330 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1331 .setIcon(android.R.drawable.ic_menu_help)
1332 .setIntent(help)
1333 .setAlphabeticShortcut('H');
1334 }
Winson Chung70d72102011-08-12 11:24:35 -07001335 return true;
1336 }
1337
1338 @Override
1339 public boolean onPrepareOptionsMenu(Menu menu) {
1340 super.onPrepareOptionsMenu(menu);
1341
1342 if (mAppsCustomizeTabHost.isTransitioning()) {
1343 return false;
1344 }
1345 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1346 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1347
1348 return true;
1349 }
1350
1351 @Override
1352 public boolean onOptionsItemSelected(MenuItem item) {
1353 switch (item.getItemId()) {
1354 case MENU_WALLPAPER_SETTINGS:
1355 startWallpaper();
1356 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001357 }
1358
1359 return super.onOptionsItemSelected(item);
1360 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001362 @Override
1363 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001364 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001365 // Use a custom animation for launching search
1366 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001367 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001368 }
1369
Joe Onorato9c1289c2009-08-17 11:03:03 -04001370 public boolean isWorkspaceLocked() {
1371 return mWorkspaceLoading || mWaitingForResult;
1372 }
1373
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001375 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001376 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 }
1378
Michael Jurka0280c3b2010-09-17 15:00:07 -07001379 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001380 mPendingAddInfo.container = ItemInfo.NO_ID;
1381 mPendingAddInfo.screen = -1;
1382 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1383 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1384 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001385 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001386
Michael Jurkaaf442092010-06-10 17:01:57 -07001387 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001388 // TODO: catch bad widget exception when sent
1389 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001390 // TODO: Is this log message meaningful?
1391 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001392 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001393 }
1394
Winson Chung55cef262010-10-28 14:14:18 -07001395 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001396 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397
Bjorn Bringert7984c942009-12-09 15:38:25 +00001398 if (appWidget.configure != null) {
1399 // Launch over to configure widget, if needed
1400 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1401 intent.setComponent(appWidget.configure);
1402 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001403 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001404 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1405 intent.putExtra(
1406 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1407 info.mimeType);
1408
1409 final String mimeType = info.mimeType;
1410 final ClipData clipData = (ClipData) info.configurationData;
1411 final ClipDescription clipDesc = clipData.getDescription();
1412 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1413 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001414 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001415 final CharSequence stringData = item.getText();
1416 final Uri uriData = item.getUri();
1417 final Intent intentData = item.getIntent();
1418 final String key =
1419 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1420 if (uriData != null) {
1421 intent.putExtra(key, uriData);
1422 } else if (intentData != null) {
1423 intent.putExtra(key, intentData);
1424 } else if (stringData != null) {
1425 intent.putExtra(key, stringData);
1426 }
1427 break;
1428 }
1429 }
1430 }
Winson Chung55cef262010-10-28 14:14:18 -07001431 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001432
Romain Guy8e633c52010-03-04 12:51:36 -08001433 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001435 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001436 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001437
1438 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001439 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 }
1441 }
Romain Guycbb89e42009-06-08 15:52:54 -07001442
Adam Cohenfbba09b2011-07-18 21:43:05 -07001443 /**
1444 * Process a shortcut drop.
1445 *
1446 * @param componentName The name of the component
1447 * @param screen The screen where it should be added
1448 * @param cell The cell it should be added to, optional
1449 * @param position The location on the screen where it was dropped, optional
1450 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001451 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1452 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001453 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001454 mPendingAddInfo.container = container;
1455 mPendingAddInfo.screen = screen;
1456 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001457
1458 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001459 mPendingAddInfo.cellX = cell[0];
1460 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001461 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001462
1463 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1464 createShortcutIntent.setComponent(componentName);
1465 processShortcut(createShortcutIntent);
1466 }
1467
Adam Cohenfbba09b2011-07-18 21:43:05 -07001468 /**
1469 * Process a widget drop.
1470 *
1471 * @param info The PendingAppWidgetInfo of the widget being added.
1472 * @param screen The screen where it should be added
1473 * @param cell The cell it should be added to, optional
1474 * @param position The location on the screen where it was dropped, optional
1475 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001476 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1477 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001478 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001479 mPendingAddInfo.container = info.container = container;
1480 mPendingAddInfo.screen = info.screen = screen;
1481 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001482 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001483 mPendingAddInfo.cellX = cell[0];
1484 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001485 }
1486
1487 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1488 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1489 addAppWidgetImpl(appWidgetId, info);
1490 }
1491
Joe Onoratodeb98af2010-02-19 14:59:39 -08001492 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001493 // Handle case where user selected "Applications"
1494 String applicationName = getResources().getString(R.string.group_applications);
1495 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001496
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001497 if (applicationName != null && applicationName.equals(shortcutName)) {
1498 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1499 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001500
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001501 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1502 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001503 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001504 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001505 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001506 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001507 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 }
1509
Winson Chung24ab2f12010-09-16 14:10:47 -07001510 void processWallpaper(Intent intent) {
1511 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1512 }
1513
Winson Chung3d503fb2011-07-13 17:25:49 -07001514 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1515 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001516 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 folderInfo.title = getText(R.string.folder_name);
1518
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001520 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1521 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001522 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523
1524 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001525 FolderIcon newFolder =
1526 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1527 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1528 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001529 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 }
Romain Guycbb89e42009-06-08 15:52:54 -07001531
Joe Onorato9c1289c2009-08-17 11:03:03 -04001532 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001533 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001534 }
1535
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 private void showNotifications() {
1537 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1538 if (statusBar != null) {
1539 statusBar.expand();
1540 }
1541 }
1542
1543 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001544 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001546 Intent chooser = Intent.createChooser(pickWallpaper,
1547 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001548 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1549 // Removed in Eclair MR1
1550// WallpaperManager wm = (WallpaperManager)
1551// getSystemService(Context.WALLPAPER_SERVICE);
1552// WallpaperInfo wi = wm.getWallpaperInfo();
1553// if (wi != null && wi.getSettingsActivity() != null) {
1554// LabeledIntent li = new LabeledIntent(getPackageName(),
1555// R.string.configure_wallpaper, 0);
1556// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1557// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1558// }
Mike Clerona0618e42009-10-22 13:55:21 -07001559 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 }
1561
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001562 /**
1563 * Registers various content observers. The current implementation registers
1564 * only a favorites observer to keep track of the favorites applications.
1565 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001566 private void registerContentObservers() {
1567 ContentResolver resolver = getContentResolver();
1568 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1569 true, mWidgetObserver);
1570 }
1571
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 @Override
1573 public boolean dispatchKeyEvent(KeyEvent event) {
1574 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1575 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001577 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001578 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001579 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001580 dumpState();
1581 return true;
1582 }
1583 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001584 }
1585 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1586 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001587 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 return true;
1589 }
1590 }
1591
1592 return super.dispatchKeyEvent(event);
1593 }
1594
Joe Onorato88ec0992009-11-19 13:16:06 -08001595 @Override
1596 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001597 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001598 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001599 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001600 Folder openFolder = mWorkspace.getOpenFolder();
1601 if (openFolder.isEditingName()) {
1602 openFolder.dismissEditingName();
1603 } else {
1604 closeFolder();
1605 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001606 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001607 mWorkspace.exitWidgetResizeMode();
1608
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001609 // Back button is a no-op here, but give at least some feedback for the button press
1610 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001611 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001612 }
1613
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001615 * Re-listen when widgets are reset.
1616 */
1617 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001618 if (mAppWidgetHost != null) {
1619 mAppWidgetHost.startListening();
1620 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001621 }
1622
1623 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001624 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1625 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001627 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001628 if (mModel != null) {
1629 mModel.unbindWorkspaceItems();
1630 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001632
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 /**
1634 * Launches the intent referred by the clicked shortcut.
1635 *
1636 * @param v The view representing the clicked shortcut.
1637 */
1638 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001639 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1640 // view has detached (it's possible for this to happen if the view is removed mid touch).
1641 if (v.getWindowToken() == null) {
1642 return;
1643 }
1644
1645 if (mWorkspace.isSwitchingState()) {
1646 return;
1647 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001648
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001650 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001652 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001653 int[] pos = new int[2];
1654 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001655 intent.setSourceBounds(new Rect(pos[0], pos[1],
1656 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001657 boolean success = startActivitySafely(intent, tag);
1658
1659 if (success && v instanceof BubbleTextView) {
1660 mWaitingForResume = (BubbleTextView) v;
1661 mWaitingForResume.setStayPressed(true);
1662 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001664 if (v instanceof FolderIcon) {
1665 FolderIcon fi = (FolderIcon) v;
1666 handleFolderClick(fi);
1667 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001668 } else if (v == mAllAppsButton) {
1669 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001670 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001671 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001672 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001673 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 }
1675 }
1676
Michael Jurka0e260592010-06-30 17:07:39 -07001677 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001678 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001679 // clicking anywhere on the workspace causes the customization drawer to slide down
1680 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001681 return false;
1682 }
1683
Michael Jurkaaf442092010-06-10 17:01:57 -07001684 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001685 * Event handler for the search button
1686 *
1687 * @param v The view that was clicked.
1688 */
1689 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001690 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001691 }
1692
1693 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001694 * Event handler for the voice button
1695 *
1696 * @param v The view that was clicked.
1697 */
1698 public void onClickVoiceButton(View v) {
1699 startVoiceSearch();
1700 }
1701
1702 private void startVoiceSearch() {
1703 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001704 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001705 startActivity(intent);
1706 }
1707
1708 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001709 * Event handler for the "grid" button that appears on the home screen, which
1710 * enters all apps mode.
1711 *
1712 * @param v The view that was clicked.
1713 */
1714 public void onClickAllAppsButton(View v) {
1715 showAllApps(true);
1716 }
1717
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001718 public void onClickAppMarketButton(View v) {
1719 if (mAppMarketIntent != null) {
1720 startActivitySafely(mAppMarketIntent, "app market");
1721 }
1722 }
1723
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001724 void startApplicationDetailsActivity(ComponentName componentName) {
1725 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001726 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1727 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001728 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001729 startActivity(intent);
1730 }
1731
Patrick Dubroy5539af72010-09-07 15:22:01 -07001732 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1733 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1734 // System applications cannot be installed. For now, show a toast explaining that.
1735 // We may give them the option of disabling apps this way.
1736 int messageId = R.string.uninstall_system_app_text;
1737 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1738 } else {
1739 String packageName = appInfo.componentName.getPackageName();
1740 String className = appInfo.componentName.getClassName();
1741 Intent intent = new Intent(
1742 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001743 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1744 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001745 startActivity(intent);
1746 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001747 }
1748
Michael Jurkaddd62e92011-02-16 17:49:14 -08001749 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001750 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1751 try {
1752 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001753 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 } catch (ActivityNotFoundException e) {
1755 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001756 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 } catch (SecurityException e) {
1758 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001759 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001761 "or use the exported attribute for this activity. "
1762 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001764 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001766
Romain Guy8e633c52010-03-04 12:51:36 -08001767 void startActivityForResultSafely(Intent intent, int requestCode) {
1768 try {
1769 startActivityForResult(intent, requestCode);
1770 } catch (ActivityNotFoundException e) {
1771 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1772 } catch (SecurityException e) {
1773 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1774 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1775 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1776 "or use the exported attribute for this activity.", e);
1777 }
1778 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779
Adam Cohena9cf38f2011-05-02 15:36:58 -07001780 private void handleFolderClick(FolderIcon folderIcon) {
1781 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001782 Folder openFolder = mWorkspace.getFolderForTag(info);
1783
1784 // If the folder info reports that the associated folder is open, then verify that
1785 // it is actually opened. There have been a few instances where this gets out of sync.
1786 if (info.opened && openFolder == null) {
1787 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1788 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1789 info.opened = false;
1790 }
1791
Adam Cohena9cf38f2011-05-02 15:36:58 -07001792 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001793 // Close any open folder
1794 closeFolder();
1795 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001796 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 } else {
1798 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 int folderScreen;
1800 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001801 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 // .. and close it
1803 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001804 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 // Close any folder open on the current screen
1806 closeFolder();
1807 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001808 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 }
1810 }
1811 }
1812 }
1813
Adam Cohen2801caf2011-05-13 20:57:39 -07001814 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001815 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001816 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1817 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1818 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1819
Adam Cohenc51934b2011-07-26 21:07:43 -07001820 FolderInfo info = (FolderInfo) fi.getTag();
1821 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1822 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001823 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1824 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001825 }
1826
Adam Cohen2801caf2011-05-13 20:57:39 -07001827 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1828 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1829 oa.start();
1830 }
1831
1832 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001833 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001834 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1835 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1836 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1837
Adam Cohenc51934b2011-07-26 21:07:43 -07001838 FolderInfo info = (FolderInfo) fi.getTag();
1839 CellLayout cl = null;
1840 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1841 cl = (CellLayout) fi.getParent().getParent();
1842 }
1843
1844 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001845 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1846 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001847 oa.addListener(new AnimatorListenerAdapter() {
1848 @Override
1849 public void onAnimationEnd(Animator animation) {
1850 if (layout != null) {
1851 layout.clearFolderLeaveBehind();
1852 }
1853 }
1854 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001855 oa.start();
1856 }
1857
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001859 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 * is animated relative to the specified View. If the View is null, no animation
1861 * is played.
1862 *
1863 * @param folderInfo The FolderInfo describing the folder to open.
1864 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001865 public void openFolder(FolderIcon folderIcon) {
1866 Folder folder = folderIcon.mFolder;
1867 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868
Adam Cohen2801caf2011-05-13 20:57:39 -07001869 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001870 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871
Adam Cohen4554ee12011-08-03 16:13:21 -07001872 // Just verify that the folder hasn't already been added to the DragLayer.
1873 // There was a one-off crash where the folder had a parent already.
1874 if (folder.getParent() == null) {
1875 mDragLayer.addView(folder);
1876 mDragController.addDropTarget((DropTarget) folder);
1877 } else {
1878 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1879 folder.getParent() + ").");
1880 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001881 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001882 }
1883
1884 public void closeFolder() {
1885 Folder folder = mWorkspace.getOpenFolder();
1886 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07001887 if (folder.isEditingName()) {
1888 folder.dismissEditingName();
1889 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001890 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07001891
1892 // Dismiss the folder cling
1893 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07001894 }
1895 }
1896
1897 void closeFolder(Folder folder) {
1898 folder.getInfo().opened = false;
1899
1900 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1901 if (parent != null) {
1902 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1903 shrinkAndFadeInFolderIcon(fi);
1904 }
1905 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 }
1907
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001909 if (mState != State.WORKSPACE) {
1910 return false;
1911 }
1912
Joe Onorato9c1289c2009-08-17 11:03:03 -04001913 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 return false;
1915 }
1916
1917 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001918 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001919 }
1920
Michael Jurka0280c3b2010-09-17 15:00:07 -07001921 resetAddInfo();
1922 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001924 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 return true;
1926 }
1927
Winson Chung3d503fb2011-07-13 17:25:49 -07001928 // The hotseat touch handling does not go through Workspace, and we always allow long press
1929 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001930 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001931 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1932 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001933 if (itemUnderLongClick == null) {
1934 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001935 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1936 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001937 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001939 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001941 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 }
1943 }
1944 }
1945 return true;
1946 }
1947
Winson Chung3d503fb2011-07-13 17:25:49 -07001948 boolean isHotseatLayout(View layout) {
1949 return mHotseat != null && layout != null &&
1950 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1951 }
1952 Hotseat getHotseat() {
1953 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001954 }
1955
Winson Chung3d503fb2011-07-13 17:25:49 -07001956 /**
1957 * Returns the CellLayout of the specified container at the specified screen.
1958 */
1959 CellLayout getCellLayout(long container, int screen) {
1960 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1961 if (mHotseat != null) {
1962 return mHotseat.getLayout();
1963 } else {
1964 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001965 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001966 } else {
1967 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001968 }
1969 }
1970
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 Workspace getWorkspace() {
1972 return mWorkspace;
1973 }
1974
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 @Override
1976 protected Dialog onCreateDialog(int id) {
1977 switch (id) {
1978 case DIALOG_CREATE_SHORTCUT:
1979 return new CreateShortcut().createDialog();
1980 case DIALOG_RENAME_FOLDER:
1981 return new RenameFolder().createDialog();
1982 }
1983
1984 return super.onCreateDialog(id);
1985 }
1986
1987 @Override
1988 protected void onPrepareDialog(int id, Dialog dialog) {
1989 switch (id) {
1990 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 break;
1992 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001993 if (mFolderInfo != null) {
1994 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1995 final CharSequence text = mFolderInfo.title;
1996 input.setText(text);
1997 input.setSelection(0, text.length());
1998 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 break;
2000 }
2001 }
2002
2003 void showRenameDialog(FolderInfo info) {
2004 mFolderInfo = info;
2005 mWaitingForResult = true;
2006 showDialog(DIALOG_RENAME_FOLDER);
2007 }
2008
Adam Cohenfbba09b2011-07-18 21:43:05 -07002009 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002010 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002011 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2012 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 mWaitingForResult = true;
2014 showDialog(DIALOG_CREATE_SHORTCUT);
2015 }
2016
2017 private class RenameFolder {
2018 private EditText mInput;
2019
2020 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2022 mInput = (EditText) layout.findViewById(R.id.folder_name);
2023
2024 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2025 builder.setIcon(0);
2026 builder.setTitle(getString(R.string.rename_folder_title));
2027 builder.setCancelable(true);
2028 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2029 public void onCancel(DialogInterface dialog) {
2030 cleanup();
2031 }
2032 });
2033 builder.setNegativeButton(getString(R.string.cancel_action),
2034 new Dialog.OnClickListener() {
2035 public void onClick(DialogInterface dialog, int which) {
2036 cleanup();
2037 }
2038 }
2039 );
2040 builder.setPositiveButton(getString(R.string.rename_action),
2041 new Dialog.OnClickListener() {
2042 public void onClick(DialogInterface dialog, int which) {
2043 changeFolderName();
2044 }
2045 }
2046 );
2047 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002048
2049 final AlertDialog dialog = builder.create();
2050 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2051 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002052 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002053 mInput.requestFocus();
2054 InputMethodManager inputManager = (InputMethodManager)
2055 getSystemService(Context.INPUT_METHOD_SERVICE);
2056 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002057 }
2058 });
2059
2060 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 }
2062
2063 private void changeFolderName() {
2064 final String name = mInput.getText().toString();
2065 if (!TextUtils.isEmpty(name)) {
2066 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002067 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 mFolderInfo.title = name;
2069 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2070
Joe Onorato9c1289c2009-08-17 11:03:03 -04002071 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002072 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002073 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 } else {
2075 final FolderIcon folderIcon = (FolderIcon)
2076 mWorkspace.getViewForTag(mFolderInfo);
2077 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002078 // TODO: At some point we'll probably want some version of setting
2079 // the text for a folder icon.
2080 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 getWorkspace().requestLayout();
2082 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002083 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002084 mWorkspaceLoading = true;
2085 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 }
2087 }
2088 }
2089 cleanup();
2090 }
2091
2092 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 dismissDialog(DIALOG_RENAME_FOLDER);
2094 mWaitingForResult = false;
2095 mFolderInfo = null;
2096 }
2097 }
2098
Daniel Sandler843e8602010-06-07 14:59:01 -04002099 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2100 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002101 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002102 }
2103
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002104 // AllAppsView.Watcher
2105 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002106 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002107 mWorkspace.setVisibility(View.GONE);
2108 }
2109 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002110
2111 /**
2112 * Helper method for the cameraZoomIn/cameraZoomOut animations
2113 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002114 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002115 * @param scaleFactor The scale factor used for the zoom
2116 */
2117 private void setPivotsForZoom(View view, State state, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002118 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002119 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002120 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002121
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002122 void updateWallpaperVisibility(boolean visible) {
2123 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2124 int curflags = getWindow().getAttributes().flags
2125 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2126 if (wpflags != curflags) {
2127 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2128 }
2129 }
2130
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002131 /**
2132 * Zoom the camera out from the workspace to reveal 'toView'.
2133 * Assumes that the view to show is anchored at either the very top or very bottom
2134 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002135 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002136 */
Winson Chungc07918d2011-07-01 15:35:26 -07002137 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002138 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002139 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002140
Winson Chungf0ea4d32011-06-06 14:27:16 -07002141 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2142 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2143 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2144 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002145 final int startDelay =
2146 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002147
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002148 setPivotsForZoom(toView, toState, scale);
2149
Michael Jurkad74c9842011-07-10 12:44:21 -07002150 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002151 mWorkspace.changeState(Workspace.State.SMALL, animated);
Adam Cohencff6af82011-09-13 14:51:53 -07002152 //hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002153
2154 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002155 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002156 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002157 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2158 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002159 ((View) toView.getParent()).invalidate();
2160 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002161 toView.setFastScaleX(a * scale + b * 1f);
2162 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002163 }
2164 });
Adam Cohen61033d32010-11-15 18:29:44 -08002165
Winson Chungf0ea4d32011-06-06 14:27:16 -07002166 toView.setVisibility(View.VISIBLE);
2167 toView.setFastAlpha(0f);
2168 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2169 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2170 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2171 public void onAnimationUpdate(float a, float b) {
2172 // don't need to invalidate because we do so above
2173 toView.setFastAlpha(a * 0f + b * 1f);
2174 }
2175 });
Adam Cohencff6af82011-09-13 14:51:53 -07002176 alphaAnim.setStartDelay(startDelay);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002177 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002178
Michael Jurkaabded662011-03-04 12:06:57 -08002179 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002180 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, scaleAnim,
2181 false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002182 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002183 scaleAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002184 boolean animationCancelled = false;
2185
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002186 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002187 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002188 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002189 // Prepare the position
2190 toView.setTranslationX(0.0f);
2191 toView.setTranslationY(0.0f);
2192 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002193 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002194 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002195 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002196 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002197 // If we don't set the final scale values here, if this animation is cancelled
2198 // it will have the wrong scale value and subsequent cameraPan animations will
2199 // not fix that
2200 toView.setScaleX(1.0f);
2201 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002202 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002203 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance,
2204 scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002205 }
Winson Chung32174c82011-07-19 15:47:55 -07002206
2207 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2208 // Hide the workspace scrollbar
2209 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002210 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002211 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002212 if (!animationCancelled) {
2213 updateWallpaperVisibility(false);
2214 }
2215 }
2216
Adam Cohencff6af82011-09-13 14:51:53 -07002217 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002218 public void onAnimationCancel(Animator animation) {
2219 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002220 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002221 });
2222
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002223 // toView should appear right at the end of the workspace shrink animation
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002224
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002225 if (mStateAnimation != null) mStateAnimation.cancel();
2226 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002227 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002228 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002229 } else {
2230 toView.setTranslationX(0.0f);
2231 toView.setTranslationY(0.0f);
2232 toView.setScaleX(1.0f);
2233 toView.setScaleY(1.0f);
2234 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002235 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002236 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002237 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, null, false);
2238 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance, null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002239
Winson Chungc07918d2011-07-01 15:35:26 -07002240 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2241 // Hide the workspace scrollbar
2242 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002243 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002244 }
Michael Jurkaabded662011-03-04 12:06:57 -08002245 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002246 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002247 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002248 }
2249
2250 /**
2251 * Zoom the camera back into the workspace, hiding 'fromView'.
2252 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002253 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002254 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002255 */
Winson Chungc07918d2011-07-01 15:35:26 -07002256 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002257 Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002258 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002259
Winson Chungf0ea4d32011-06-06 14:27:16 -07002260 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2261 final float scaleFactor = (float)
2262 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2263 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002264
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002265 setPivotsForZoom(fromView, fromState, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002266 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002267 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002268 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002269 if (mStateAnimation != null) mStateAnimation.cancel();
2270 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002271
Michael Jurka742574b2011-02-02 23:51:01 -08002272 final float oldScaleX = fromView.getScaleX();
2273 final float oldScaleY = fromView.getScaleY();
2274
2275 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2276 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002277 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2278 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002279 ((View)fromView.getParent()).fastInvalidate();
2280 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2281 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2282 }
2283 });
Michael Jurkaabded662011-03-04 12:06:57 -08002284 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002285 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Adam Cohencff6af82011-09-13 14:51:53 -07002286 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002287 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2288 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002289 // don't need to invalidate because we do so above
2290 fromView.setFastAlpha(a * 1f + b * 0f);
2291 }
2292 });
Michael Jurkaabded662011-03-04 12:06:57 -08002293 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002294 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, alphaAnim,
2295 true);
Michael Jurka2763be32011-02-24 11:19:57 -08002296 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002297 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002298 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002299 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002300 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002301 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002302 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002303 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance,
2304 alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002305 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002306 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002307 }
2308 });
2309
Winson Chungf0ea4d32011-06-06 14:27:16 -07002310 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002311 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002312 } else {
2313 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002314 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002315 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, null, true);
2316 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance, null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002317 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002318 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002319 }
2320
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002321 void showWorkspace(boolean animated) {
Adam Cohencff6af82011-09-13 14:51:53 -07002322 Resources res = getResources();
2323 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2324
2325 mWorkspace.changeState(Workspace.State.NORMAL, animated, stagger);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002326 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002327 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002328 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002329
Adam Cohen7777d962011-08-18 18:58:38 -07002330 mWorkspace.showDockDivider(!animated);
2331 mWorkspace.flashScrollingIndicator();
2332
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002333 // Change the state *after* we've called all the transition code
2334 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002335
Winson Chung5fb63472011-02-02 17:03:37 -08002336 // Resume the auto-advance of widgets
2337 mUserPresent = true;
2338 updateRunning();
2339
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002340 // send an accessibility event to announce the context change
2341 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002342 }
2343
Adam Cohen7777d962011-08-18 18:58:38 -07002344 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002345 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002346 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Winson Chungb26f3d62011-06-02 10:49:29 -07002347 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002348 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002349 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002350 }
Adam Cohen7777d962011-08-18 18:58:38 -07002351
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002352 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chung09bfc452011-09-09 11:30:20 -07002353 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2354
Winson Chunge7a03942011-08-05 15:05:12 -07002355 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002356 @Override
2357 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002358 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002359 // Before we show workspace, hide all apps again because
2360 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2361 // clean up our state transition functions
2362 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohen7777d962011-08-18 18:58:38 -07002363 mSearchDeleteBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002364 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002365 } else {
2366 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002367 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002368 }
2369 }, (extendedDelay ?
2370 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2371 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2372 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002373 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002374 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002375 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2376 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002377 }
2378 // Otherwise, we are not in spring loaded mode, so don't do anything.
2379 }
2380
Adam Cohenfc53cd22011-07-20 15:45:11 -07002381 public boolean isAllAppsCustomizeOpen() {
2382 return mState == State.APPS_CUSTOMIZE;
2383 }
2384
Winson Chungf0ea4d32011-06-06 14:27:16 -07002385 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002386 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002387 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002388 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002389 if (!LauncherApplication.isScreenLarge()) {
2390 if (animated) {
2391 int duration = mSearchDeleteBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002392 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002393 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002394 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002395 }
2396 }
2397 }
2398
2399 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002400 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002401 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002402 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002403 if (!LauncherApplication.isScreenLarge()) {
2404 if (animated) {
2405 int duration = mSearchDeleteBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002406 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002407 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002408 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002409 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002410 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002411 }
2412
Winson Chung785d2eb2011-04-14 16:08:02 -07002413 void showAllApps(boolean animated) {
2414 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002415
Winson Chungf0ea4d32011-06-06 14:27:16 -07002416 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2417 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002418
Winson Chung3d503fb2011-07-13 17:25:49 -07002419 // Hide the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002420 mSearchDeleteBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002421
Winson Chungf0ea4d32011-06-06 14:27:16 -07002422 // Change the state *after* we've called all the transition code
2423 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002424
2425 // Pause the auto-advance of widgets until we are out of AllApps
2426 mUserPresent = false;
2427 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002428 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002429
2430 // Send an accessibility event to announce the context change
2431 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2432 }
2433
Joe Onoratob2061212009-11-24 16:13:54 -05002434 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002435 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002436 * - Home from workspace
2437 * - from center screen
2438 * - from other screens
2439 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002440 * - from center screen
2441 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002442 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002443 * - from center screen
2444 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002445 * - Launch app from workspace and quit
2446 * - with back
2447 * - with home
2448 * - Launch app from all apps and quit
2449 * - with back
2450 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002451 * - Go to a screen that's not the default, then all
2452 * apps, and launch and app, and go back
2453 * - with back
2454 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002455 * - On workspace, long press power and go back
2456 * - with back
2457 * - with home
2458 * - On all apps, long press power and go back
2459 * - with back
2460 * - with home
2461 * - On workspace, power off
2462 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002463 * - Launch an app and turn off the screen while in that app
2464 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002465 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002466 * - From all apps
2467 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002468 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2469 * - From all apps
2470 * - From the center workspace
2471 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002472 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002473 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002474 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2475 mWorkspace.setVisibility(View.VISIBLE);
2476 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002477
Winson Chung3d503fb2011-07-13 17:25:49 -07002478 // Show the search bar and hotseat
Winson Chungf0ea4d32011-06-06 14:27:16 -07002479 mSearchDeleteBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002480
Winson Chungf0ea4d32011-06-06 14:27:16 -07002481 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002482 if (mAllAppsButton != null) {
2483 mAllAppsButton.requestFocus();
2484 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002485 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002486 }
2487
Joe Onorato7c312c12009-08-13 21:36:53 -07002488 void lockAllApps() {
2489 // TODO
2490 }
2491
2492 void unlockAllApps() {
2493 // TODO
2494 }
2495
Patrick Dubroy5f445422011-02-18 14:35:21 -08002496 /**
2497 * Add an item from all apps or customize onto the given workspace screen.
2498 * If layout is null, add to the current screen.
2499 */
2500 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002501 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2502 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002503 } else {
2504 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002505 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002506 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002507
Winson Chungdff8ebb2011-09-08 17:25:31 -07002508 /** Maps the current orientation to an index for referencing orientation correct global icons */
2509 private int getCurrentOrientationIndexForGlobalIcons() {
2510 // default - 0, landscape - 1
2511 switch (getResources().getConfiguration().orientation) {
2512 case Configuration.ORIENTATION_LANDSCAPE:
2513 return 1;
2514 default:
2515 return 0;
2516 }
2517 }
2518
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002519 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002520 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002521 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002522 // Look for the toolbar icon specified in the activity meta-data
2523 Bundle metaData = packageManager.getActivityInfo(
2524 activityName, PackageManager.GET_META_DATA).metaData;
2525 if (metaData != null) {
2526 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2527 if (iconResId != 0) {
2528 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002529 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002530 }
2531 }
2532 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002533 // This can happen if the activity defines an invalid drawable
2534 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2535 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002536 } catch (Resources.NotFoundException nfe) {
2537 // This can happen if the activity defines an invalid drawable
2538 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2539 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002540 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002541 return null;
2542 }
2543
2544 // if successful in getting icon, return it; otherwise, set button to use default drawable
2545 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2546 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2547 TextView button = (TextView) findViewById(buttonId);
2548 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002549 Resources r = getResources();
2550 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2551 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002552
2553 // If we were unable to find the icon via the meta-data, use a generic one
2554 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002555 toolbarIcon = r.getDrawable(fallbackDrawableId);
2556 toolbarIcon.setBounds(0, 0, w, h);
2557 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002558 return null;
2559 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002560 toolbarIcon.setBounds(0, 0, w, h);
2561 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002562 return toolbarIcon.getConstantState();
2563 }
2564 }
2565
2566 // if successful in getting icon, return it; otherwise, set button to use default drawable
2567 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2568 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2569 ImageView button = (ImageView) findViewById(buttonId);
2570 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2571
Michael Jurka19e0fc52011-07-22 18:00:21 -07002572 if (button != null) {
2573 // If we were unable to find the icon via the meta-data, use a
2574 // generic one
2575 if (toolbarIcon == null) {
2576 button.setImageResource(fallbackDrawableId);
2577 } else {
2578 button.setImageDrawable(toolbarIcon);
2579 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002580 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002581
2582 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2583
Michael Jurka0423dcf2010-10-05 14:56:18 -07002584 }
2585
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002586 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2587 TextView button = (TextView) findViewById(buttonId);
2588 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2589 }
2590
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002591 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002592 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002593 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002594 }
2595
Michael Jurka0423dcf2010-10-05 14:56:18 -07002596 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002597 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2598 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002599 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002600
Winson Chung1cad91e2011-05-25 17:41:01 -07002601 final SearchManager searchManager =
2602 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2603 ComponentName activityName = searchManager.getGlobalSearchActivity();
2604 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002605 int coi = getCurrentOrientationIndexForGlobalIcons();
2606 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002607 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002608 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002609 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002610 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002611 // We disable both search and voice search when there is no global search provider
Winson Chung1cad91e2011-05-25 17:41:01 -07002612 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002613 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002614 voiceButton.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002615 }
2616 }
2617
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002618 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002619 updateButtonWithDrawable(R.id.search_button, d);
2620 }
2621
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002622 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002623 final View searchDivider = findViewById(R.id.search_divider);
2624 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002625
Winson Chung1cad91e2011-05-25 17:41:01 -07002626 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2627 ComponentName activityName = intent.resolveActivity(getPackageManager());
2628 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002629 int coi = getCurrentOrientationIndexForGlobalIcons();
2630 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002631 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002632 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002633 voiceButton.setVisibility(View.VISIBLE);
2634 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002635 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002636 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002637 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002638 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002639
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002640 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002641 updateButtonWithDrawable(R.id.voice_button, d);
2642 }
2643
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002644 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002645 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002646 */
2647 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002648 final View marketButton = findViewById(R.id.market_button);
2649 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2650 // Find the app market activity by resolving an intent.
2651 // (If multiple app markets are installed, it will return the ResolverActivity.)
2652 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002653 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002654 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002655 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002656 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002657 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002658 marketButton.setVisibility(View.VISIBLE);
2659 } else {
2660 // We should hide and disable the view so that we don't try and restore the visibility
2661 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2662 marketButton.setVisibility(View.GONE);
2663 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002664 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002665 }
2666
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002667 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002668 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002669 }
2670
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002671 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002672 * Displays the shortcut creation dialog and launches, if necessary, the
2673 * appropriate activity.
2674 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002675 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002676 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2677 DialogInterface.OnShowListener {
2678
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002679 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002680
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002681 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002682 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002683
Winson Chung55b65502011-05-26 12:03:43 -07002684 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2685 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002686 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002687
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002688 AlertDialog dialog = builder.create();
2689 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002690 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002691 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002692
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002693 return dialog;
2694 }
2695
2696 public void onCancel(DialogInterface dialog) {
2697 mWaitingForResult = false;
2698 cleanup();
2699 }
2700
Romain Guycbb89e42009-06-08 15:52:54 -07002701 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002702 mWaitingForResult = false;
2703 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002704 }
2705
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002706 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002707 try {
2708 dismissDialog(DIALOG_CREATE_SHORTCUT);
2709 } catch (Exception e) {
2710 // An exception is thrown if the dialog is not visible, which is fine
2711 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002712 }
2713
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002714 /**
2715 * Handle the action clicked in the "Add to home" dialog.
2716 */
2717 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002718 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002719
Winson Chung55b65502011-05-26 12:03:43 -07002720 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2721 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002722 case AddAdapter.ITEM_APPLICATION: {
2723 if (mAppsCustomizeTabHost != null) {
2724 mAppsCustomizeTabHost.selectAppsTab();
2725 }
2726 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002727 break;
2728 }
Winson Chung55b65502011-05-26 12:03:43 -07002729 case AddAdapter.ITEM_APPWIDGET: {
2730 if (mAppsCustomizeTabHost != null) {
2731 mAppsCustomizeTabHost.selectWidgetsTab();
2732 }
2733 showAllApps(true);
2734 break;
2735 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002736 case AddAdapter.ITEM_WALLPAPER: {
2737 startWallpaper();
2738 break;
2739 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002740 }
2741 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002742
2743 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002744 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002745 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002746 }
2747
2748 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002749 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002750 */
2751 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2752 @Override
2753 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002754 closeSystemDialogs();
2755 }
2756 }
2757
2758 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002759 * Receives notifications whenever the appwidgets are reset.
2760 */
2761 private class AppWidgetResetObserver extends ContentObserver {
2762 public AppWidgetResetObserver() {
2763 super(new Handler());
2764 }
2765
2766 @Override
2767 public void onChange(boolean selfChange) {
2768 onAppWidgetReset();
2769 }
2770 }
2771
2772 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002773 * If the activity is currently paused, signal that we need to re-run the loader
2774 * in onResume.
2775 *
2776 * This needs to be called from incoming places where resources might have been loaded
2777 * while we are paused. That is becaues the Configuration might be wrong
2778 * when we're not running, and if it comes back to what it was when we
2779 * were paused, we are not restarted.
2780 *
2781 * Implementation of the method from LauncherModel.Callbacks.
2782 *
2783 * @return true if we are currently paused. The caller might be able to
2784 * skip some work in that case since we will come back again.
2785 */
2786 public boolean setLoadOnResume() {
2787 if (mPaused) {
2788 Log.i(TAG, "setLoadOnResume");
2789 mOnResumeNeedsLoad = true;
2790 return true;
2791 } else {
2792 return false;
2793 }
2794 }
2795
2796 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002797 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002798 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002799 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002800 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002801 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002802 } else {
2803 return SCREEN_COUNT / 2;
2804 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002805 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002806
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002807
Joe Onorato9c1289c2009-08-17 11:03:03 -04002808 /**
2809 * Refreshes the shortcuts shown on the workspace.
2810 *
2811 * Implementation of the method from LauncherModel.Callbacks.
2812 */
2813 public void startBinding() {
2814 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002815
2816 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002817 int count = workspace.getChildCount();
2818 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002819 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002820 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2821 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002822 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002823 if (mHotseat != null) {
2824 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002825 }
2826 }
2827
2828 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002829 * Bind the items start-end from the list.
2830 *
2831 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002832 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002833 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002834 setLoadOnResume();
2835
Joe Onorato9c1289c2009-08-17 11:03:03 -04002836 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002837 for (int i=start; i<end; i++) {
2838 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002839
2840 // Short circuit if we are loading dock items for a configuration which has no dock
2841 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2842 mHotseat == null) {
2843 continue;
2844 }
2845
Joe Onorato9c1289c2009-08-17 11:03:03 -04002846 switch (item.itemType) {
2847 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2848 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002849 View shortcut = createShortcut((ShortcutInfo)item);
2850 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2851 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002852 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002853 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002854 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002855 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002856 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002857 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2858 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002859 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002860 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002861 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002862 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002863 }
2864
Joe Onorato9c1289c2009-08-17 11:03:03 -04002865 /**
2866 * Implementation of the method from LauncherModel.Callbacks.
2867 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002868 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002869 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002870 sFolders.clear();
2871 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002872 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002873
2874 /**
2875 * Add the views for a widget to the workspace.
2876 *
2877 * Implementation of the method from LauncherModel.Callbacks.
2878 */
2879 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002880 setLoadOnResume();
2881
Daniel Sandler843e8602010-06-07 14:59:01 -04002882 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2883 if (DEBUG_WIDGETS) {
2884 Log.d(TAG, "bindAppWidget: " + item);
2885 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002886 final Workspace workspace = mWorkspace;
2887
2888 final int appWidgetId = item.appWidgetId;
2889 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002890 if (DEBUG_WIDGETS) {
2891 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2892 }
2893
Joe Onorato9c1289c2009-08-17 11:03:03 -04002894 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2895
Joe Onorato9c1289c2009-08-17 11:03:03 -04002896 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2897 item.hostView.setTag(item);
2898
Winson Chung3d503fb2011-07-13 17:25:49 -07002899 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002900 item.cellY, item.spanX, item.spanY, false);
2901
Adam Cohended9f8d2010-11-03 13:25:16 -07002902 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2903
Joe Onorato9c1289c2009-08-17 11:03:03 -04002904 workspace.requestLayout();
2905
Daniel Sandler843e8602010-06-07 14:59:01 -04002906 if (DEBUG_WIDGETS) {
2907 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2908 + (SystemClock.uptimeMillis()-start) + "ms");
2909 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002910 }
2911
2912 /**
2913 * Callback saying that there aren't any more items to bind.
2914 *
2915 * Implementation of the method from LauncherModel.Callbacks.
2916 */
2917 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002918 setLoadOnResume();
2919
Joe Onorato9c1289c2009-08-17 11:03:03 -04002920 if (mSavedState != null) {
2921 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002922 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002923 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002924 mSavedState = null;
2925 }
2926
2927 if (mSavedInstanceState != null) {
2928 super.onRestoreInstanceState(mSavedInstanceState);
2929 mSavedInstanceState = null;
2930 }
2931
Joe Onorato9c1289c2009-08-17 11:03:03 -04002932 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002933
2934 // If we received the result of any pending adds while the loader was running (e.g. the
2935 // widget configuration forced an orientation change), process them now.
2936 for (int i = 0; i < sPendingAddList.size(); i++) {
2937 completeAdd(sPendingAddList.get(i));
2938 }
2939 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002940 }
2941
2942 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002943 * Updates the icons on the launcher that are affected by changes to the package list
2944 * on the device.
2945 */
2946 private void updateIconsAffectedByPackageManagerChanges() {
2947 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002948 updateVoiceSearchIcon();
2949 }
2950
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002951 @Override
2952 public void bindSearchablesChanged() {
2953 updateGlobalSearchIcon();
Winson Chung1f74ff72011-09-21 19:22:53 -07002954 updateVoiceSearchIcon();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002955 }
2956
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002957 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002958 * Add the icons for all apps.
2959 *
2960 * Implementation of the method from LauncherModel.Callbacks.
2961 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002962 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2963 // Remove the progress bar entirely; we could also make it GONE
2964 // but better to remove it since we know it's not going to be used
2965 View progressBar = mAppsCustomizeTabHost.
2966 findViewById(R.id.apps_customize_progress_bar);
2967 if (progressBar != null) {
2968 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002969 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002970 // We just post the call to setApps so the user sees the progress bar
2971 // disappear-- otherwise, it just looks like the progress bar froze
2972 // which doesn't look great
2973 mAppsCustomizeTabHost.post(new Runnable() {
2974 public void run() {
2975 if (mAppsCustomizeContent != null) {
2976 mAppsCustomizeContent.setApps(apps);
2977 }
2978 }
2979 });
2980
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002981 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002982 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002983 }
2984
2985 /**
2986 * A package was installed.
2987 *
2988 * Implementation of the method from LauncherModel.Callbacks.
2989 */
Joe Onorato64e6be72010-03-05 15:05:52 -05002990 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002991 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002992 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002993
Winson Chung785d2eb2011-04-14 16:08:02 -07002994 if (mAppsCustomizeContent != null) {
2995 mAppsCustomizeContent.addApps(apps);
2996 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002997 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002998 }
2999
3000 /**
3001 * A package was updated.
3002 *
3003 * Implementation of the method from LauncherModel.Callbacks.
3004 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003005 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003006 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003007 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003008 if (mWorkspace != null) {
3009 mWorkspace.updateShortcuts(apps);
3010 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003011
Winson Chung785d2eb2011-04-14 16:08:02 -07003012 if (mAppsCustomizeContent != null) {
3013 mAppsCustomizeContent.updateApps(apps);
3014 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003015 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003016 }
3017
3018 /**
3019 * A package was uninstalled.
3020 *
3021 * Implementation of the method from LauncherModel.Callbacks.
3022 */
Joe Onorato36115782010-06-17 13:28:48 -04003023 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003024 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003025 if (permanent) {
3026 mWorkspace.removeItems(apps);
3027 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003028
Winson Chung785d2eb2011-04-14 16:08:02 -07003029 if (mAppsCustomizeContent != null) {
3030 mAppsCustomizeContent.removeApps(apps);
3031 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003032 updateIconsAffectedByPackageManagerChanges();
Winson Chunga1820962011-10-03 16:31:06 -07003033
3034 // Notify the drag controller
3035 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003036 }
Joe Onoratobe386092009-11-17 17:32:16 -08003037
3038 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003039 * A number of packages were updated.
3040 */
3041 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003042 if (mAppsCustomizeContent != null) {
3043 mAppsCustomizeContent.onPackagesUpdated();
3044 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003045 }
3046
Winson Chung400438b2011-01-16 17:53:48 -08003047 private int mapConfigurationOriActivityInfoOri(int configOri) {
3048 final Display d = getWindowManager().getDefaultDisplay();
3049 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3050 switch (d.getRotation()) {
3051 case Surface.ROTATION_0:
3052 case Surface.ROTATION_180:
3053 // We are currently in the same basic orientation as the natural orientation
3054 naturalOri = configOri;
3055 break;
3056 case Surface.ROTATION_90:
3057 case Surface.ROTATION_270:
3058 // We are currently in the other basic orientation to the natural orientation
3059 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3060 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3061 break;
3062 }
3063
3064 int[] oriMap = {
3065 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3066 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3067 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3068 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3069 };
3070 // Since the map starts at portrait, we need to offset if this device's natural orientation
3071 // is landscape.
3072 int indexOffset = 0;
3073 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3074 indexOffset = 1;
3075 }
3076 return oriMap[(d.getRotation() + indexOffset) % 4];
3077 }
Adam Cohen446e9402011-09-15 18:21:21 -07003078
3079 public void lockScreenOrientationOnLargeUI() {
3080 if (LauncherApplication.isScreenLarge()) {
3081 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3082 .getConfiguration().orientation));
3083 }
Winson Chung400438b2011-01-16 17:53:48 -08003084 }
Adam Cohen446e9402011-09-15 18:21:21 -07003085 public void unlockScreenOrientationOnLargeUI() {
3086 if (LauncherApplication.isScreenLarge()) {
3087 mHandler.postDelayed(new Runnable() {
3088 public void run() {
3089 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3090 }
3091 }, mRestoreScreenOrientationDelay);
3092 }
Winson Chung400438b2011-01-16 17:53:48 -08003093 }
3094
Winson Chung82f55532011-08-09 14:14:23 -07003095 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003096 private static final String PREFS_KEY = "com.android.launcher2.prefs";
3097 private boolean isClingsEnabled() {
Winson Chung82f55532011-08-09 14:14:23 -07003098 // TEMPORARY: DISABLE CLINGS ON LARGE UI
Winson Chung7d7541e2011-09-16 20:14:36 -07003099 if (LauncherApplication.isScreenLarge()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003100 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003101 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003102
Winson Chung7d7541e2011-09-16 20:14:36 -07003103 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003104 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003105 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3106 Cling cling = (Cling) findViewById(clingId);
3107 if (cling != null) {
3108 cling.init(this, positionData);
3109 cling.setVisibility(View.VISIBLE);
3110 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3111 if (animate) {
3112 cling.buildLayer();
3113 cling.setAlpha(0f);
3114 cling.animate()
3115 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003116 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003117 .setDuration(SHOW_CLING_DURATION)
3118 .setStartDelay(delay)
3119 .start();
3120 } else {
3121 cling.setAlpha(1f);
3122 }
3123 }
3124 return cling;
3125 }
3126 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003127 if (cling != null) {
3128 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003129 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003130 anim.addListener(new AnimatorListenerAdapter() {
3131 public void onAnimationEnd(Animator animation) {
3132 cling.setVisibility(View.GONE);
3133 cling.cleanup();
3134 SharedPreferences prefs =
3135 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3136 SharedPreferences.Editor editor = prefs.edit();
3137 editor.putBoolean(flag, true);
3138 editor.commit();
3139 };
3140 });
3141 anim.start();
3142 }
3143 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003144 private void removeCling(int id) {
3145 final View cling = findViewById(id);
3146 if (cling != null) {
3147 final ViewGroup parent = (ViewGroup) cling.getParent();
3148 parent.post(new Runnable() {
3149 @Override
3150 public void run() {
3151 parent.removeView(cling);
3152 }
3153 });
3154 }
3155 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003156 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003157 // Enable the clings only if they have not been dismissed before
3158 SharedPreferences prefs =
3159 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003160 if (isClingsEnabled() && !prefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003161 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003162 } else {
3163 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003164 }
3165 }
3166 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003167 // Enable the clings only if they have not been dismissed before
3168 SharedPreferences prefs =
3169 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003170 if (isClingsEnabled() && !prefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003171 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003172 } else {
3173 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003174 }
3175 }
3176 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003177 // Enable the clings only if they have not been dismissed before
3178 SharedPreferences prefs =
3179 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
3180 Cling cling = null;
Winson Chung9d9d74f2011-09-19 11:49:12 -07003181 if (isClingsEnabled() && !prefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003182 cling = initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003183 } else {
3184 removeCling(R.id.folder_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003185 }
3186 return cling;
3187 }
3188 public boolean isFolderClingVisible() {
3189 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003190 if (cling != null) {
3191 return cling.getVisibility() == View.VISIBLE;
3192 }
3193 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003194 }
Winson Chung82f55532011-08-09 14:14:23 -07003195 public void dismissWorkspaceCling(View v) {
3196 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003197 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003198 }
3199 public void dismissAllAppsCling(View v) {
3200 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003201 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3202 }
3203 public void dismissFolderCling(View v) {
3204 Cling cling = (Cling) findViewById(R.id.folder_cling);
3205 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003206 }
3207
Winson Chung80baf5a2010-08-09 16:03:15 -07003208 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003209 * Prints out out state for debugging.
3210 */
3211 public void dumpState() {
3212 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003213 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003214 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3215 Log.d(TAG, "mRestoring=" + mRestoring);
3216 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3217 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003218 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003219 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003220
Winson Chung785d2eb2011-04-14 16:08:02 -07003221 if (mAppsCustomizeContent != null) {
3222 mAppsCustomizeContent.dumpState();
3223 }
Joe Onoratobe386092009-11-17 17:32:16 -08003224 Log.d(TAG, "END launcher2 dump state");
3225 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003226}
Michael Jurka2763be32011-02-24 11:19:57 -08003227
Michael Jurkaabded662011-03-04 12:06:57 -08003228interface LauncherTransitionable {
Winson Chung7d7541e2011-09-16 20:14:36 -07003229 void onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace);
3230 void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003231}