blob: 02f19ac092d63fc1aae95d253dc0d44db7891297 [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;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070098import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.FileNotFoundException;
100import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700101import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.util.ArrayList;
103import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700104
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105/**
106 * Default launcher application.
107 */
Michael Jurka946ad472010-07-09 18:05:18 -0700108public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700109 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
110 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800111 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700112 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
Joe Onorato9c1289c2009-08-17 11:03:03 -0400114 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400115 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700116
Winson Chung70d72102011-08-12 11:24:35 -0700117 private static final int MENU_GROUP_WALLPAPER = 1;
118 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
119 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700120 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
121 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
123 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700124 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 private static final int REQUEST_PICK_APPLICATION = 6;
126 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700127 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700128 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
130 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
131
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800132 static final int SCREEN_COUNT = 5;
133 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Joe Onorato7c312c12009-08-13 21:36:53 -0700135 static final int DIALOG_CREATE_SHORTCUT = 1;
136 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Romain Guy98d01652009-06-30 16:21:04 -0700138 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
140 // Type: int
141 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700142 // Type: int
143 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700145 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
146 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
148 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700149 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700151 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 // Type: boolean
153 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
154 // Type: long
155 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
156
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700157 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
158
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700159 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700160 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700161 private State mState = State.WORKSPACE;
162 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700163
Joe Onorato9c1289c2009-08-17 11:03:03 -0400164 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700165 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
166 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700167 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700168 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800171 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800173 private final BroadcastReceiver mCloseSystemDialogsReceiver
174 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800175 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private LayoutInflater mInflater;
178
Joe Onorato00acb122009-08-04 16:04:30 -0400179 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700181
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700182 private AppWidgetManager mAppWidgetManager;
183 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700184
Michael Jurkac9d95c52011-08-29 14:03:34 -0700185 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700186 private int[] mTmpAddItemCellCoordinates = new int[2];
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private FolderInfo mFolderInfo;
189
Winson Chung3d503fb2011-07-13 17:25:49 -0700190 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800191 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700192
Winson Chungc51db6a2011-10-05 11:44:49 -0700193 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700194 private AppsCustomizeTabHost mAppsCustomizeTabHost;
195 private AppsCustomizePagedView mAppsCustomizeContent;
196 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private Bundle mSavedState;
199
200 private SpannableStringBuilder mDefaultKeySsb = null;
201
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 private boolean mWorkspaceLoading = true;
203
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800204 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 private boolean mRestoring;
206 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700207 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
209 private Bundle mSavedInstanceState;
210
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800212 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800213 private boolean mUserPresent = true;
214 private boolean mVisible = false;
215 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700217 private static LocaleConfiguration sLocaleConfiguration = null;
218
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700219 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700220
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700221 private Intent mAppMarketIntent = null;
222
Adam Cohended9f8d2010-11-03 13:25:16 -0700223 // Related to the auto-advancing of widgets
224 private final int ADVANCE_MSG = 1;
225 private final int mAdvanceInterval = 20000;
226 private final int mAdvanceStagger = 250;
227 private long mAutoAdvanceSentTime;
228 private long mAutoAdvanceTimeLeft = -1;
229 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
230 new HashMap<View, AppWidgetProviderInfo>();
231
Winson Chung400438b2011-01-16 17:53:48 -0800232 // Determines how long to wait after a rotation before restoring the screen orientation to
233 // match the sensor state.
234 private final int mRestoreScreenOrientationDelay = 500;
235
Michael Jurka4ef207b2010-11-29 17:05:45 -0800236 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700237 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
238 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
239 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800240
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700241 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
242
Adam Cohen2801caf2011-05-13 20:57:39 -0700243 private DragLayer mDragLayer;
244
Michael Jurkaddd62e92011-02-16 17:49:14 -0800245 private BubbleTextView mWaitingForResume;
246
Michael Jurkac1f5d262011-09-30 19:32:27 -0700247 private Runnable mBuildLayersRunnable = new Runnable() {
248 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700249 if (mWorkspace != null) {
250 mWorkspace.buildPageHardwareLayers();
251 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700252 }
253 };
254
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800255 private static ArrayList<PendingAddArguments> sPendingAddList
256 = new ArrayList<PendingAddArguments>();
257
258 private static class PendingAddArguments {
259 int requestCode;
260 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700261 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800262 int screen;
263 int cellX;
264 int cellY;
265 }
266
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800267 @Override
268 protected void onCreate(Bundle savedInstanceState) {
269 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800270 LauncherApplication app = ((LauncherApplication)getApplication());
271 mModel = app.setLauncher(this);
272 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400273 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700275
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700276 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700277 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
278 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700279
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200281 android.os.Debug.startMethodTracing(
282 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 }
284
285 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 setContentView(R.layout.launcher);
287 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700288 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800290 registerContentObservers();
291
Joe Onorato7c312c12009-08-13 21:36:53 -0700292 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 mSavedState = savedInstanceState;
295 restoreState(mSavedState);
296
Winson Chunga12a2502010-12-20 14:41:35 -0800297 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700298 if (mAppsCustomizeContent != null) {
299 mAppsCustomizeContent.onPackagesUpdated();
300 }
Winson Chunga12a2502010-12-20 14:41:35 -0800301
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302 if (PROFILE_STARTUP) {
303 android.os.Debug.stopMethodTracing();
304 }
305
306 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400307 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800308 }
309
Michael Jurkac57b7a82011-08-09 22:02:20 -0700310 if (!mModel.isAllAppsLoaded()) {
311 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
312 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
313 }
314
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 // For handling default keys
316 mDefaultKeySsb = new SpannableStringBuilder();
317 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800318
319 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
320 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800321
Winson Chungc51db6a2011-10-05 11:44:49 -0700322 boolean searchVisible = false;
323 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800324 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700325 int coi = getCurrentOrientationIndexForGlobalIcons();
326 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
327 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700328 updateAppMarketIcon();
329 searchVisible = updateGlobalSearchIcon();
330 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700331 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700332 if (sGlobalSearchIcon[coi] != null) {
333 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700334 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700335 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700336 if (sVoiceSearchIcon[coi] != null) {
337 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700338 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700339 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700340 if (sAppMarketIcon[coi] != null) {
341 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800342 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700343 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700344
345 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohendc16b602011-09-26 15:12:33 -0700346 if (LauncherApplication.isScreenLarge() || Build.TYPE.contentEquals("eng")) {
Adam Cohen446e9402011-09-15 18:21:21 -0700347 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
348 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 }
Romain Guycbb89e42009-06-08 15:52:54 -0700350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700352 if (sLocaleConfiguration == null) {
353 new AsyncTask<Void, Void, LocaleConfiguration>() {
354 @Override
355 protected LocaleConfiguration doInBackground(Void... unused) {
356 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
357 readConfiguration(Launcher.this, localeConfiguration);
358 return localeConfiguration;
359 }
360
361 @Override
362 protected void onPostExecute(LocaleConfiguration result) {
363 sLocaleConfiguration = result;
364 checkForLocaleChange(); // recursive, but now with a locale configuration
365 }
366 }.execute();
367 return;
368 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700369
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 final Configuration configuration = getResources().getConfiguration();
371
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700372 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 final String locale = configuration.locale.toString();
374
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700375 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 final int mcc = configuration.mcc;
377
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700378 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 final int mnc = configuration.mnc;
380
Romain Guy84f296c2009-11-04 15:00:44 -0800381 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382
Romain Guy84f296c2009-11-04 15:00:44 -0800383 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700384 sLocaleConfiguration.locale = locale;
385 sLocaleConfiguration.mcc = mcc;
386 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700387
Joe Onorato0589f0f2010-02-08 13:44:00 -0800388 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700389
390 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
391 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700392 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700393 public void run() {
394 writeConfiguration(Launcher.this, localeConfiguration);
395 }
396 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700397 }
398 }
399
400 private static class LocaleConfiguration {
401 public String locale;
402 public int mcc = -1;
403 public int mnc = -1;
404 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700405
Romain Guy98d01652009-06-30 16:21:04 -0700406 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
407 DataInputStream in = null;
408 try {
409 in = new DataInputStream(context.openFileInput(PREFERENCES));
410 configuration.locale = in.readUTF();
411 configuration.mcc = in.readInt();
412 configuration.mnc = in.readInt();
413 } catch (FileNotFoundException e) {
414 // Ignore
415 } catch (IOException e) {
416 // Ignore
417 } finally {
418 if (in != null) {
419 try {
420 in.close();
421 } catch (IOException e) {
422 // Ignore
423 }
424 }
425 }
426 }
427
428 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
429 DataOutputStream out = null;
430 try {
431 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
432 out.writeUTF(configuration.locale);
433 out.writeInt(configuration.mcc);
434 out.writeInt(configuration.mnc);
435 out.flush();
436 } catch (FileNotFoundException e) {
437 // Ignore
438 } catch (IOException e) {
439 //noinspection ResultOfMethodCallIgnored
440 context.getFileStreamPath(PREFERENCES).delete();
441 } finally {
442 if (out != null) {
443 try {
444 out.close();
445 } catch (IOException e) {
446 // Ignore
447 }
448 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 }
450 }
451
Adam Cohen716b51e2011-06-30 12:09:54 -0700452 public DragLayer getDragLayer() {
453 return mDragLayer;
454 }
455
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 static int getScreen() {
457 synchronized (sLock) {
458 return sScreen;
459 }
460 }
461
462 static void setScreen(int screen) {
463 synchronized (sLock) {
464 sScreen = screen;
465 }
466 }
467
Winson Chung557d6ed2011-07-08 15:34:52 -0700468 /**
469 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
470 * a configuration step, this allows the proper animations to run after other transitions.
471 */
472 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700473 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800474 switch (args.requestCode) {
475 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700476 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
477 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800478 break;
479 case REQUEST_PICK_SHORTCUT:
480 processShortcut(args.intent);
481 break;
482 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700483 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
484 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700485 result = true;
486 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800487 case REQUEST_PICK_APPWIDGET:
488 addAppWidgetFromPick(args.intent);
489 break;
490 case REQUEST_CREATE_APPWIDGET:
491 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700492 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700493 result = true;
494 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800495 case REQUEST_PICK_WALLPAPER:
496 // We just wanted the activity result here so we can clear mWaitingForResult
497 break;
498 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700499 // In any situation where we have a multi-step drop, we should reset the add info only after
500 // we complete the drop
501 resetAddInfo();
502 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800503 }
504
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800506 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700507 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700508 mWaitingForResult = false;
509
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510 // The pattern used here is that a user PICKs a specific application,
511 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700512
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800513 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
514 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700515 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800516 final PendingAddArguments args = new PendingAddArguments();
517 args.requestCode = requestCode;
518 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700519 args.container = mPendingAddInfo.container;
520 args.screen = mPendingAddInfo.screen;
521 args.cellX = mPendingAddInfo.cellX;
522 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800523
524 // If the loader is still running, defer the add until it is done.
525 if (isWorkspaceLocked()) {
526 sPendingAddList.add(args);
527 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700528 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800529 }
Romain Guy18042c82009-11-06 11:44:55 -0800530 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700531 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
532 if (data != null) {
533 // Clean up the appWidgetId if we canceled
534 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
535 if (appWidgetId != -1) {
536 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800538 }
539 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700540
541 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700542 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 }
544
545 @Override
546 protected void onResume() {
547 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800548 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700549 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400550 mWorkspaceLoading = true;
551 mModel.startLoader(this, true);
552 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700553 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800554 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800555 if (mWaitingForResume != null) {
556 mWaitingForResume.setStayPressed(false);
557 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700558 // When we resume Launcher, a different Activity might be responsible for the app
559 // market intent, so refresh the icon
560 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -0700561 if (!mWorkspaceLoading) {
562 mWorkspace.post(mBuildLayersRunnable);
563 }
Mathew Inwood8193f692011-10-12 17:30:58 +0100564 clearTypedText();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800565 }
566
567 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700568 protected void onPause() {
569 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700570 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800571 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700572 }
Romain Guycbb89e42009-06-08 15:52:54 -0700573
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700574 @Override
575 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400576 // Flag the loader to stop early before switching
577 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700578 if (mAppsCustomizeContent != null) {
579 mAppsCustomizeContent.surrender();
580 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800581 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700582 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700583
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800584 // We can't hide the IME if it was forced open. So don't bother
585 /*
586 @Override
587 public void onWindowFocusChanged(boolean hasFocus) {
588 super.onWindowFocusChanged(hasFocus);
589
590 if (hasFocus) {
591 final InputMethodManager inputManager = (InputMethodManager)
592 getSystemService(Context.INPUT_METHOD_SERVICE);
593 WindowManager.LayoutParams lp = getWindow().getAttributes();
594 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
595 android.os.Handler()) {
596 protected void onReceiveResult(int resultCode, Bundle resultData) {
597 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
598 }
599 });
600 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
601 }
602 }
603 */
604
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 private boolean acceptFilter() {
606 final InputMethodManager inputManager = (InputMethodManager)
607 getSystemService(Context.INPUT_METHOD_SERVICE);
608 return !inputManager.isFullscreenMode();
609 }
610
611 @Override
612 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700613 final int uniChar = event.getUnicodeChar();
614 final boolean handled = super.onKeyDown(keyCode, event);
615 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
616 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
618 keyCode, event);
619 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700620 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700621 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700622 // showSearchDialog()
623 // If there are multiple keystrokes before the search dialog takes focus,
624 // onSearchRequested() will be called for every keystroke,
625 // but it is idempotent, so it's fine.
626 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 }
628 }
629
Joe Onorato8a9625e2010-01-28 15:55:35 -0800630 // Eat the long press event so the keyboard doesn't come up.
631 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
632 return true;
633 }
634
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800635 return handled;
636 }
637
Karl Rosaen138a0412009-04-23 19:00:21 -0700638 private String getTypedText() {
639 return mDefaultKeySsb.toString();
640 }
641
642 private void clearTypedText() {
643 mDefaultKeySsb.clear();
644 mDefaultKeySsb.clearSpans();
645 Selection.setSelection(mDefaultKeySsb, 0);
646 }
647
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700649 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
650 * State
651 */
652 private static State intToState(int stateOrdinal) {
653 State state = State.WORKSPACE;
654 final State[] stateValues = State.values();
655 for (int i = 0; i < stateValues.length; i++) {
656 if (stateValues[i].ordinal() == stateOrdinal) {
657 state = stateValues[i];
658 break;
659 }
660 }
661 return state;
662 }
663
664 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800665 * Restores the previous state, if it exists.
666 *
667 * @param savedState The previous state.
668 */
669 private void restoreState(Bundle savedState) {
670 if (savedState == null) {
671 return;
672 }
673
Michael Jurka883f55b2010-10-21 15:47:14 -0700674 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700675 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800676 showAllApps(false);
677 }
678
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
680 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700681 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
683
Winson Chung3d503fb2011-07-13 17:25:49 -0700684 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
685 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700686
Winson Chung3d503fb2011-07-13 17:25:49 -0700687 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
688 mPendingAddInfo.container = pendingAddContainer;
689 mPendingAddInfo.screen = pendingAddScreen;
690 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
691 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 mRestoring = true;
693 }
694
695 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
696 if (renameFolder) {
697 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700698 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 mRestoring = true;
700 }
Winson Chunga12a2502010-12-20 14:41:35 -0800701
Winson Chung785d2eb2011-04-14 16:08:02 -0700702
703 // Restore the AppsCustomize tab
704 if (mAppsCustomizeTabHost != null) {
705 String curTab = savedState.getString("apps_customize_currentTab");
706 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700707 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700708 mAppsCustomizeContent.setContentType(
709 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
710 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700711 mAppsCustomizeContent.loadAssociatedPages(
712 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700713 }
714
Winson Chung5afbf7b2011-07-25 11:53:08 -0700715 int currentIndex = savedState.getInt("apps_customize_currentIndex");
716 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700717 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800718 }
719
720 /**
721 * Finds all the views we need and configure them properly.
722 */
723 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700724 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400725
Winson Chung4c98d922011-05-31 16:50:48 -0700726 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
727 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800728
Winson Chung4c98d922011-05-31 16:50:48 -0700729 // Setup the drag layer
730 mDragLayer.setup(this, dragController);
731
Winson Chung3d503fb2011-07-13 17:25:49 -0700732 // Setup the hotseat
733 mHotseat = (Hotseat) findViewById(R.id.hotseat);
734 if (mHotseat != null) {
735 mHotseat.setup(this);
736 }
737
Winson Chung4c98d922011-05-31 16:50:48 -0700738 // Setup the workspace
739 mWorkspace.setHapticFeedbackEnabled(false);
740 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700741 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700742 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700743
Winson Chungf0ea4d32011-06-06 14:27:16 -0700744 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700745 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700746
Winson Chungf0ea4d32011-06-06 14:27:16 -0700747 // Setup AppsCustomize
748 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
749 findViewById(R.id.apps_customize_pane);
750 mAppsCustomizeContent = (AppsCustomizePagedView)
751 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
752 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400753
Michael Jurka5130e402011-10-13 04:55:35 -0700754 // Get the all apps button
755 mAllAppsButton = findViewById(R.id.all_apps_button);
756 if (mAllAppsButton != null) {
757 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
758 @Override
759 public boolean onTouch(View v, MotionEvent event) {
760 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
761 onTouchDownAllAppsButton(v);
762 }
763 return false;
764 }
765 });
766 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700767 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700768 dragController.setDragScoller(mWorkspace);
769 dragController.setScrollView(mDragLayer);
770 dragController.setMoveTarget(mWorkspace);
771 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700772 if (mSearchDropTargetBar != null) {
773 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800774 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 }
776
777 /**
778 * Creates a view representing a shortcut.
779 *
780 * @param info The data structure describing the shortcut.
781 *
782 * @return A View inflated from R.layout.application.
783 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800784 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700786 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800787 }
788
789 /**
790 * Creates a view representing a shortcut inflated from the specified resource.
791 *
792 * @param layoutResId The id of the XML layout used to create the shortcut.
793 * @param parent The group the shortcut belongs to.
794 * @param info The data structure describing the shortcut.
795 *
796 * @return A View inflated from layoutResId.
797 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800798 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800799 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
800 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 return favorite;
803 }
804
805 /**
806 * Add an application shortcut to the workspace.
807 *
808 * @param data The intent describing the application.
809 * @param cellInfo The position on screen where to create the shortcut.
810 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700811 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700812 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700813 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700814
Adam Cohenfbba09b2011-07-18 21:43:05 -0700815 // First we check if we already know the exact location where we want to add this item.
816 if (cellX >= 0 && cellY >= 0) {
817 cellXY[0] = cellX;
818 cellXY[1] = cellY;
819 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700820 showOutOfSpaceMessage();
821 return;
822 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800823
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800824 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800825
Romain Guy73b979d2009-06-09 12:57:21 -0700826 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800827 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800828 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800829 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700830 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700831 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800832 } else {
833 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 }
835 }
Romain Guycbb89e42009-06-08 15:52:54 -0700836
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800837 /**
838 * Add a shortcut to the workspace.
839 *
840 * @param data The intent describing the shortcut.
841 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700843 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
844 int cellY) {
845 int[] cellXY = mTmpAddItemCellCoordinates;
846 int[] touchXY = mPendingAddInfo.dropPos;
847 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700848
Michael Jurkad3ef3062010-11-23 16:23:58 -0800849 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700850
Adam Cohen558baaf2011-08-15 15:22:57 -0700851 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
852 final View view = createShortcut(info);
853
Adam Cohenfbba09b2011-07-18 21:43:05 -0700854 // First we check if we already know the exact location where we want to add this item.
855 if (cellX >= 0 && cellY >= 0) {
856 cellXY[0] = cellX;
857 cellXY[1] = cellY;
858 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700859
860 // If appropriate, either create a folder or add to an existing folder
861 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
862 true, null,null)) {
863 return;
864 }
865 DragObject dragObject = new DragObject();
866 dragObject.dragInfo = info;
867 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
868 return;
869 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700870 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800871 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700872 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800873 foundCellSpan = (result != null);
874 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700875 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800876 }
877
878 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700879 showOutOfSpaceMessage();
880 return;
881 }
882
Adam Cohen558baaf2011-08-15 15:22:57 -0700883 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884
885 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700886 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
887 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800888 }
889 }
890
Adam Cohen50370f32011-08-25 18:57:14 -0700891 class Padding {
892 int left = 0;
893 int right = 0;
894 int top = 0;
895 int bottom = 0;
896 }
897
Adam Cohenf814aa02011-09-01 13:48:05 -0700898 Padding getPaddingForWidget(ComponentName component) {
Adam Cohen50370f32011-08-25 18:57:14 -0700899 PackageManager packageManager = getPackageManager();
900 Padding p = new Padding();
901 android.content.pm.ApplicationInfo appInfo;
902
903 try {
Adam Cohenf814aa02011-09-01 13:48:05 -0700904 appInfo = packageManager.getApplicationInfo(component.getPackageName(), 0);
Adam Cohen50370f32011-08-25 18:57:14 -0700905 } catch (Exception e) {
906 // if we can't find the package, return 0 padding
907 return p;
908 }
909
Adam Cohenff648b52011-09-19 11:57:30 -0700910 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Adam Cohen50370f32011-08-25 18:57:14 -0700911 Resources r = getResources();
Adam Cohen82ebbd22011-09-30 15:05:40 -0700912 // The default padding values are private API currently, but will be added in
913 // API level 15. The current values are (8, 8, 8, 8).
914 p.left = r.getDimensionPixelSize(com.android.internal.
915 R.dimen.default_app_widget_padding_left);
916 p.right = r.getDimensionPixelSize(com.android.internal.
917 R.dimen.default_app_widget_padding_right);
918 p.top = r.getDimensionPixelSize(com.android.internal.
919 R.dimen.default_app_widget_padding_top);
920 p.bottom = r.getDimensionPixelSize(com.android.internal.
921 R.dimen.default_app_widget_padding_bottom);
Adam Cohen50370f32011-08-25 18:57:14 -0700922 }
923
924 return p;
925 }
926
Adam Cohenf814aa02011-09-01 13:48:05 -0700927 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
928 if (spanXY == null) {
929 spanXY = new int[2];
930 }
931
932 Padding padding = getPaddingForWidget(component);
933 // We want to account for the extra amount of padding that we are adding to the widget
934 // to ensure that it gets the full amount of space that it has requested
935 int requiredWidth = minWidth + padding.left + padding.right;
936 int requiredHeight = minHeight + padding.top + padding.bottom;
937 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
938 }
939
940 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
941 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
942 }
943
Adam Cohencbf47e32011-09-16 17:32:37 -0700944 int[] getMinResizeSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
945 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
946 }
947
Adam Cohenf814aa02011-09-01 13:48:05 -0700948 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
949 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
950 }
951
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700953 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700955 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700956 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800957 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700958 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700959 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700960
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700961 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700962 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700963
Adam Cohenf814aa02011-09-01 13:48:05 -0700964 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700965
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700967 // We have saved the position to which the widget was dragged-- this really only matters
968 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700969 int[] cellXY = mTmpAddItemCellCoordinates;
970 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700971 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700972 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
973 cellXY[0] = mPendingAddInfo.cellX;
974 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700975 foundCellSpan = true;
976 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700977 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700978 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700979 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800980 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700981 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700982 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700983 }
984
Michael Jurka0280c3b2010-09-17 15:00:07 -0700985 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800986 if (appWidgetId != -1) {
987 // Deleting an app widget ID is a void call but writes to disk before returning
988 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800989 new Thread("deleteAppWidgetId") {
990 public void run() {
991 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
992 }
993 }.start();
994 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700995 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800996 return;
997 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800998
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700999 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -07001000 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001001 launcherInfo.spanX = spanXY[0];
1002 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001003
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001005 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006
1007 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001009 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001010
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001011 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001013
Winson Chung3d503fb2011-07-13 17:25:49 -07001014 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001015 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001016
1017 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018 }
1019 }
Romain Guycbb89e42009-06-08 15:52:54 -07001020
Adam Cohended9f8d2010-11-03 13:25:16 -07001021 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1022 @Override
1023 public void onReceive(Context context, Intent intent) {
1024 final String action = intent.getAction();
1025 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1026 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001027 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001028 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001029
Winson Chungc100e8e2011-08-09 16:02:43 -07001030 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001031 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001032 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1033 mAppsCustomizeTabHost.reset();
1034 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001035 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001036 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1037 mUserPresent = true;
1038 updateRunning();
1039 }
1040 }
1041 };
1042
1043 @Override
1044 public void onAttachedToWindow() {
1045 super.onAttachedToWindow();
1046
1047 // Listen for broadcasts related to user-presence
1048 final IntentFilter filter = new IntentFilter();
1049 filter.addAction(Intent.ACTION_SCREEN_OFF);
1050 filter.addAction(Intent.ACTION_USER_PRESENT);
1051 registerReceiver(mReceiver, filter);
1052
Adam Cohend113e0c2010-11-11 10:48:05 -08001053 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001054 mVisible = true;
1055 }
1056
1057 @Override
1058 public void onDetachedFromWindow() {
1059 super.onDetachedFromWindow();
1060 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001061 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001062
Adam Cohend113e0c2010-11-11 10:48:05 -08001063 if (mAttached) {
1064 unregisterReceiver(mReceiver);
1065 mAttached = false;
1066 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001067 updateRunning();
1068 }
1069
1070 public void onWindowVisibilityChanged(int visibility) {
1071 mVisible = visibility == View.VISIBLE;
1072 updateRunning();
1073 }
1074
1075 private void sendAdvanceMessage(long delay) {
1076 mHandler.removeMessages(ADVANCE_MSG);
1077 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1078 mHandler.sendMessageDelayed(msg, delay);
1079 mAutoAdvanceSentTime = System.currentTimeMillis();
1080 }
1081
1082 private void updateRunning() {
1083 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1084 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1085 mAutoAdvanceRunning = autoAdvanceRunning;
1086 if (autoAdvanceRunning) {
1087 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1088 sendAdvanceMessage(delay);
1089 } else {
1090 if (!mWidgetsToAdvance.isEmpty()) {
1091 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1092 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1093 }
1094 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001095 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001096 }
1097 }
1098 }
1099
1100 private final Handler mHandler = new Handler() {
1101 @Override
1102 public void handleMessage(Message msg) {
1103 if (msg.what == ADVANCE_MSG) {
1104 int i = 0;
1105 for (View key: mWidgetsToAdvance.keySet()) {
1106 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1107 final int delay = mAdvanceStagger * i;
1108 if (v instanceof Advanceable) {
1109 postDelayed(new Runnable() {
1110 public void run() {
1111 ((Advanceable) v).advance();
1112 }
1113 }, delay);
1114 }
1115 i++;
1116 }
1117 sendAdvanceMessage(mAdvanceInterval);
1118 }
1119 }
1120 };
1121
1122 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001123 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001124 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1125 if (v instanceof Advanceable) {
1126 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001127 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001128 updateRunning();
1129 }
1130 }
1131
1132 void removeWidgetToAutoAdvance(View hostView) {
1133 if (mWidgetsToAdvance.containsKey(hostView)) {
1134 mWidgetsToAdvance.remove(hostView);
1135 updateRunning();
1136 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001137 }
1138
Joe Onorato9c1289c2009-08-17 11:03:03 -04001139 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001140 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001141 launcherInfo.hostView = null;
1142 }
1143
Adam Cohended9f8d2010-11-03 13:25:16 -07001144 void showOutOfSpaceMessage() {
1145 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1146 }
1147
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001148 public LauncherAppWidgetHost getAppWidgetHost() {
1149 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001150 }
Romain Guycbb89e42009-06-08 15:52:54 -07001151
Winson Chunga9abd0e2010-10-27 17:18:37 -07001152 public LauncherModel getModel() {
1153 return mModel;
1154 }
1155
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001156 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001157 getWindow().closeAllPanels();
1158
Winson Chung87acb482011-08-23 17:28:05 -07001159 /**
1160 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001161 try {
1162 dismissDialog(DIALOG_CREATE_SHORTCUT);
1163 // Unlock the workspace if the dialog was showing
1164 } catch (Exception e) {
1165 // An exception is thrown if the dialog is not visible, which is fine
1166 }
1167
1168 try {
1169 dismissDialog(DIALOG_RENAME_FOLDER);
1170 // Unlock the workspace if the dialog was showing
1171 } catch (Exception e) {
1172 // An exception is thrown if the dialog is not visible, which is fine
1173 }
Winson Chung87acb482011-08-23 17:28:05 -07001174 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001175
1176 // Whatever we were doing is hereby canceled.
1177 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001178 }
1179
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 @Override
1181 protected void onNewIntent(Intent intent) {
1182 super.onNewIntent(intent);
1183
1184 // Close the menu
1185 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001186 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001187 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001188
Joe Onorato14f122b2009-11-19 14:06:36 -08001189 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1190 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001191
Adam Cohenac56cff2011-09-28 20:45:37 -07001192 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001193 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001194 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001195 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1196 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001197 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001198 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001199
1200 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001201 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001202 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001203
Joe Onorato3a8820b2009-11-10 15:06:42 -08001204 final View v = getWindow().peekDecorView();
1205 if (v != null && v.getWindowToken() != null) {
1206 InputMethodManager imm = (InputMethodManager)getSystemService(
1207 INPUT_METHOD_SERVICE);
1208 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001209 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001210
Michael Jurka35aa14d2011-07-07 17:01:08 -07001211 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001212 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001213 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001214 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215 }
1216 }
1217
1218 @Override
1219 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1220 // Do not call super here
1221 mSavedInstanceState = savedInstanceState;
1222 }
1223
1224 @Override
1225 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001226 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001227 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228
Michael Jurka883f55b2010-10-21 15:47:14 -07001229 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001230 // We close any open folder since it will not be re-opened, and we need to make sure
1231 // this state is reflected.
1232 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233
Winson Chung3d503fb2011-07-13 17:25:49 -07001234 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1235 mWaitingForResult) {
1236 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1237 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1238 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1239 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 }
1241
1242 if (mFolderInfo != null && mWaitingForResult) {
1243 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1244 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1245 }
Michael Jurka946ad472010-07-09 18:05:18 -07001246
Winson Chung785d2eb2011-04-14 16:08:02 -07001247 // Save the current AppsCustomize tab
1248 if (mAppsCustomizeTabHost != null) {
1249 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1250 if (currentTabTag != null) {
1251 outState.putString("apps_customize_currentTab", currentTabTag);
1252 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001253 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1254 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001255 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 }
1257
1258 @Override
1259 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001261
Winson Chunge7a03942011-08-05 15:05:12 -07001262 // Remove all pending runnables
1263 mHandler.removeMessages(ADVANCE_MSG);
1264 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001265 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001266
Winson Chungcd2b0142011-06-08 16:02:26 -07001267 // Stop callbacks from LauncherModel
1268 LauncherApplication app = ((LauncherApplication) getApplication());
1269 mModel.stopLoader();
1270 app.setLauncher(null);
1271
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001273 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001274 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001275 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001277 mAppWidgetHost = null;
1278
1279 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280
1281 TextKeyListener.getInstance().release();
1282
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283
Winson Chung3d503fb2011-07-13 17:25:49 -07001284 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001285
1286 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001287 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001288
1289 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1290 mWorkspace.removeAllViews();
1291 mWorkspace = null;
1292 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001293
1294 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 }
1296
Adam Cohena9cf38f2011-05-02 15:36:58 -07001297 public DragController getDragController() {
1298 return mDragController;
1299 }
1300
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 @Override
1302 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001303 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001304 super.startActivityForResult(intent, requestCode);
1305 }
1306
Winson Chung70d72102011-08-12 11:24:35 -07001307 /**
1308 * Indicates that we want global search for this activity by setting the globalSearch
1309 * argument for {@link #startSearch} to true.
1310 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001311 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001312 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001313 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001314
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001315 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001316
Karl Rosaen138a0412009-04-23 19:00:21 -07001317 if (initialQuery == null) {
1318 // Use any text typed in the launcher as the initial query
1319 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001320 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 if (appSearchData == null) {
1322 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001323 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001324 }
Romain Guycbb89e42009-06-08 15:52:54 -07001325
Karl Rosaen138a0412009-04-23 19:00:21 -07001326 final SearchManager searchManager =
1327 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001328 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001329 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 }
1331
Winson Chung70d72102011-08-12 11:24:35 -07001332 @Override
1333 public boolean onCreateOptionsMenu(Menu menu) {
1334 if (isWorkspaceLocked()) {
1335 return false;
1336 }
1337
1338 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001339
1340 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1341 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1342 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001343 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1344 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1345 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001346 String helpUrl = getString(R.string.help_url);
1347 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001348 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1349 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1350
Winson Chung70d72102011-08-12 11:24:35 -07001351 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1352 .setIcon(android.R.drawable.ic_menu_gallery)
1353 .setAlphabeticShortcut('W');
1354 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1355 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001356 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001357 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001358 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1359 .setIcon(android.R.drawable.ic_menu_preferences)
1360 .setIntent(settings)
1361 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001362 if (!helpUrl.isEmpty()) {
1363 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1364 .setIcon(android.R.drawable.ic_menu_help)
1365 .setIntent(help)
1366 .setAlphabeticShortcut('H');
1367 }
Winson Chung70d72102011-08-12 11:24:35 -07001368 return true;
1369 }
1370
1371 @Override
1372 public boolean onPrepareOptionsMenu(Menu menu) {
1373 super.onPrepareOptionsMenu(menu);
1374
1375 if (mAppsCustomizeTabHost.isTransitioning()) {
1376 return false;
1377 }
1378 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1379 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1380
1381 return true;
1382 }
1383
1384 @Override
1385 public boolean onOptionsItemSelected(MenuItem item) {
1386 switch (item.getItemId()) {
1387 case MENU_WALLPAPER_SETTINGS:
1388 startWallpaper();
1389 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001390 }
1391
1392 return super.onOptionsItemSelected(item);
1393 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001395 @Override
1396 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001397 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001398 // Use a custom animation for launching search
1399 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001400 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001401 }
1402
Joe Onorato9c1289c2009-08-17 11:03:03 -04001403 public boolean isWorkspaceLocked() {
1404 return mWorkspaceLoading || mWaitingForResult;
1405 }
1406
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001408 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001409 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001410 }
1411
Michael Jurka0280c3b2010-09-17 15:00:07 -07001412 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001413 mPendingAddInfo.container = ItemInfo.NO_ID;
1414 mPendingAddInfo.screen = -1;
1415 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1416 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1417 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001418 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001419
Michael Jurkaaf442092010-06-10 17:01:57 -07001420 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001421 // TODO: catch bad widget exception when sent
1422 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001423 // TODO: Is this log message meaningful?
1424 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001425 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001426 }
1427
Winson Chung55cef262010-10-28 14:14:18 -07001428 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001429 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430
Bjorn Bringert7984c942009-12-09 15:38:25 +00001431 if (appWidget.configure != null) {
1432 // Launch over to configure widget, if needed
1433 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1434 intent.setComponent(appWidget.configure);
1435 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001436 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001437 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1438 intent.putExtra(
1439 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1440 info.mimeType);
1441
1442 final String mimeType = info.mimeType;
1443 final ClipData clipData = (ClipData) info.configurationData;
1444 final ClipDescription clipDesc = clipData.getDescription();
1445 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1446 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001447 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001448 final CharSequence stringData = item.getText();
1449 final Uri uriData = item.getUri();
1450 final Intent intentData = item.getIntent();
1451 final String key =
1452 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1453 if (uriData != null) {
1454 intent.putExtra(key, uriData);
1455 } else if (intentData != null) {
1456 intent.putExtra(key, intentData);
1457 } else if (stringData != null) {
1458 intent.putExtra(key, stringData);
1459 }
1460 break;
1461 }
1462 }
1463 }
Winson Chung55cef262010-10-28 14:14:18 -07001464 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001465
Romain Guy8e633c52010-03-04 12:51:36 -08001466 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001468 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001469 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001470
1471 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001472 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001473 }
1474 }
Romain Guycbb89e42009-06-08 15:52:54 -07001475
Adam Cohenfbba09b2011-07-18 21:43:05 -07001476 /**
1477 * Process a shortcut drop.
1478 *
1479 * @param componentName The name of the component
1480 * @param screen The screen where it should be added
1481 * @param cell The cell it should be added to, optional
1482 * @param position The location on the screen where it was dropped, optional
1483 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001484 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1485 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001486 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001487 mPendingAddInfo.container = container;
1488 mPendingAddInfo.screen = screen;
1489 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001490
1491 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001492 mPendingAddInfo.cellX = cell[0];
1493 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001494 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001495
1496 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1497 createShortcutIntent.setComponent(componentName);
1498 processShortcut(createShortcutIntent);
1499 }
1500
Adam Cohenfbba09b2011-07-18 21:43:05 -07001501 /**
1502 * Process a widget drop.
1503 *
1504 * @param info The PendingAppWidgetInfo of the widget being added.
1505 * @param screen The screen where it should be added
1506 * @param cell The cell it should be added to, optional
1507 * @param position The location on the screen where it was dropped, optional
1508 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001509 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1510 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001511 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001512 mPendingAddInfo.container = info.container = container;
1513 mPendingAddInfo.screen = info.screen = screen;
1514 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001515 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001516 mPendingAddInfo.cellX = cell[0];
1517 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001518 }
1519
1520 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1521 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1522 addAppWidgetImpl(appWidgetId, info);
1523 }
1524
Joe Onoratodeb98af2010-02-19 14:59:39 -08001525 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001526 // Handle case where user selected "Applications"
1527 String applicationName = getResources().getString(R.string.group_applications);
1528 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001529
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001530 if (applicationName != null && applicationName.equals(shortcutName)) {
1531 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1532 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001533
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001534 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1535 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001536 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001537 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001538 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001539 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001540 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 }
1542
Winson Chung24ab2f12010-09-16 14:10:47 -07001543 void processWallpaper(Intent intent) {
1544 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1545 }
1546
Winson Chung3d503fb2011-07-13 17:25:49 -07001547 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1548 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001549 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 folderInfo.title = getText(R.string.folder_name);
1551
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001553 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1554 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001555 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556
1557 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001558 FolderIcon newFolder =
1559 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1560 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1561 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001562 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 }
Romain Guycbb89e42009-06-08 15:52:54 -07001564
Joe Onorato9c1289c2009-08-17 11:03:03 -04001565 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001566 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001567 }
1568
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 private void showNotifications() {
1570 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1571 if (statusBar != null) {
1572 statusBar.expand();
1573 }
1574 }
1575
1576 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001577 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001579 Intent chooser = Intent.createChooser(pickWallpaper,
1580 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001581 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1582 // Removed in Eclair MR1
1583// WallpaperManager wm = (WallpaperManager)
1584// getSystemService(Context.WALLPAPER_SERVICE);
1585// WallpaperInfo wi = wm.getWallpaperInfo();
1586// if (wi != null && wi.getSettingsActivity() != null) {
1587// LabeledIntent li = new LabeledIntent(getPackageName(),
1588// R.string.configure_wallpaper, 0);
1589// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1590// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1591// }
Mike Clerona0618e42009-10-22 13:55:21 -07001592 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 }
1594
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001595 /**
1596 * Registers various content observers. The current implementation registers
1597 * only a favorites observer to keep track of the favorites applications.
1598 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001599 private void registerContentObservers() {
1600 ContentResolver resolver = getContentResolver();
1601 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1602 true, mWidgetObserver);
1603 }
1604
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001605 @Override
1606 public boolean dispatchKeyEvent(KeyEvent event) {
1607 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1608 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001610 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001611 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001612 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001613 dumpState();
1614 return true;
1615 }
1616 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001617 }
1618 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1619 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001620 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 return true;
1622 }
1623 }
1624
1625 return super.dispatchKeyEvent(event);
1626 }
1627
Joe Onorato88ec0992009-11-19 13:16:06 -08001628 @Override
1629 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001630 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001631 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001632 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001633 Folder openFolder = mWorkspace.getOpenFolder();
1634 if (openFolder.isEditingName()) {
1635 openFolder.dismissEditingName();
1636 } else {
1637 closeFolder();
1638 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001639 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001640 mWorkspace.exitWidgetResizeMode();
1641
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001642 // Back button is a no-op here, but give at least some feedback for the button press
1643 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001644 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001645 }
1646
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001648 * Re-listen when widgets are reset.
1649 */
1650 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001651 if (mAppWidgetHost != null) {
1652 mAppWidgetHost.startListening();
1653 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001654 }
1655
1656 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001657 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1658 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001660 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001661 if (mModel != null) {
1662 mModel.unbindWorkspaceItems();
1663 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 /**
1667 * Launches the intent referred by the clicked shortcut.
1668 *
1669 * @param v The view representing the clicked shortcut.
1670 */
1671 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001672 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1673 // view has detached (it's possible for this to happen if the view is removed mid touch).
1674 if (v.getWindowToken() == null) {
1675 return;
1676 }
1677
1678 if (mWorkspace.isSwitchingState()) {
1679 return;
1680 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001681
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001683 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001685 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001686 int[] pos = new int[2];
1687 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001688 intent.setSourceBounds(new Rect(pos[0], pos[1],
1689 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001690 boolean success = startActivitySafely(intent, tag);
1691
1692 if (success && v instanceof BubbleTextView) {
1693 mWaitingForResume = (BubbleTextView) v;
1694 mWaitingForResume.setStayPressed(true);
1695 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001697 if (v instanceof FolderIcon) {
1698 FolderIcon fi = (FolderIcon) v;
1699 handleFolderClick(fi);
1700 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001701 } else if (v == mAllAppsButton) {
1702 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001703 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001704 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001705 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001706 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 }
1708 }
1709
Michael Jurka0e260592010-06-30 17:07:39 -07001710 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001711 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001712 // clicking anywhere on the workspace causes the customization drawer to slide down
1713 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001714 return false;
1715 }
1716
Michael Jurkaaf442092010-06-10 17:01:57 -07001717 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001718 * Event handler for the search button
1719 *
1720 * @param v The view that was clicked.
1721 */
1722 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001723 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001724 }
1725
1726 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001727 * Event handler for the voice button
1728 *
1729 * @param v The view that was clicked.
1730 */
1731 public void onClickVoiceButton(View v) {
1732 startVoiceSearch();
1733 }
1734
1735 private void startVoiceSearch() {
1736 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001737 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001738 startActivity(intent);
1739 }
1740
1741 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001742 * Event handler for the "grid" button that appears on the home screen, which
1743 * enters all apps mode.
1744 *
1745 * @param v The view that was clicked.
1746 */
1747 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001748 showAllApps(true);
1749 }
1750
1751 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001752 // Provide the same haptic feedback that the system offers for virtual keys.
1753 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001754 }
1755
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001756 public void onClickAppMarketButton(View v) {
1757 if (mAppMarketIntent != null) {
1758 startActivitySafely(mAppMarketIntent, "app market");
1759 }
1760 }
1761
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001762 void startApplicationDetailsActivity(ComponentName componentName) {
1763 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001764 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1765 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001766 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001767 startActivity(intent);
1768 }
1769
Patrick Dubroy5539af72010-09-07 15:22:01 -07001770 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1771 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1772 // System applications cannot be installed. For now, show a toast explaining that.
1773 // We may give them the option of disabling apps this way.
1774 int messageId = R.string.uninstall_system_app_text;
1775 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1776 } else {
1777 String packageName = appInfo.componentName.getPackageName();
1778 String className = appInfo.componentName.getClassName();
1779 Intent intent = new Intent(
1780 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001781 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1782 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001783 startActivity(intent);
1784 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001785 }
1786
Michael Jurkaddd62e92011-02-16 17:49:14 -08001787 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1789 try {
1790 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001791 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 } catch (ActivityNotFoundException e) {
1793 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001794 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 } catch (SecurityException e) {
1796 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001797 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001799 "or use the exported attribute for this activity. "
1800 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001802 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001804
Romain Guy8e633c52010-03-04 12:51:36 -08001805 void startActivityForResultSafely(Intent intent, int requestCode) {
1806 try {
1807 startActivityForResult(intent, requestCode);
1808 } catch (ActivityNotFoundException e) {
1809 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1810 } catch (SecurityException e) {
1811 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1812 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1813 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1814 "or use the exported attribute for this activity.", e);
1815 }
1816 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817
Adam Cohena9cf38f2011-05-02 15:36:58 -07001818 private void handleFolderClick(FolderIcon folderIcon) {
1819 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001820 Folder openFolder = mWorkspace.getFolderForTag(info);
1821
1822 // If the folder info reports that the associated folder is open, then verify that
1823 // it is actually opened. There have been a few instances where this gets out of sync.
1824 if (info.opened && openFolder == null) {
1825 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1826 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1827 info.opened = false;
1828 }
1829
Adam Cohena9cf38f2011-05-02 15:36:58 -07001830 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831 // Close any open folder
1832 closeFolder();
1833 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001834 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001835 } else {
1836 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 int folderScreen;
1838 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001839 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 // .. and close it
1841 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001842 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843 // Close any folder open on the current screen
1844 closeFolder();
1845 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001846 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 }
1848 }
1849 }
1850 }
1851
Adam Cohen2801caf2011-05-13 20:57:39 -07001852 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001853 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001854 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1855 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1856 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1857
Adam Cohenc51934b2011-07-26 21:07:43 -07001858 FolderInfo info = (FolderInfo) fi.getTag();
1859 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1860 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001861 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1862 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001863 }
1864
Adam Cohen2801caf2011-05-13 20:57:39 -07001865 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1866 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1867 oa.start();
1868 }
1869
1870 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001871 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001872 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1873 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1874 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1875
Adam Cohenc51934b2011-07-26 21:07:43 -07001876 FolderInfo info = (FolderInfo) fi.getTag();
1877 CellLayout cl = null;
1878 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1879 cl = (CellLayout) fi.getParent().getParent();
1880 }
1881
1882 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001883 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1884 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001885 oa.addListener(new AnimatorListenerAdapter() {
1886 @Override
1887 public void onAnimationEnd(Animator animation) {
1888 if (layout != null) {
1889 layout.clearFolderLeaveBehind();
1890 }
1891 }
1892 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001893 oa.start();
1894 }
1895
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001896 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001897 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 * is animated relative to the specified View. If the View is null, no animation
1899 * is played.
1900 *
1901 * @param folderInfo The FolderInfo describing the folder to open.
1902 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001903 public void openFolder(FolderIcon folderIcon) {
1904 Folder folder = folderIcon.mFolder;
1905 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906
Adam Cohen2801caf2011-05-13 20:57:39 -07001907 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001908 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909
Adam Cohen4554ee12011-08-03 16:13:21 -07001910 // Just verify that the folder hasn't already been added to the DragLayer.
1911 // There was a one-off crash where the folder had a parent already.
1912 if (folder.getParent() == null) {
1913 mDragLayer.addView(folder);
1914 mDragController.addDropTarget((DropTarget) folder);
1915 } else {
1916 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1917 folder.getParent() + ").");
1918 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001919 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001920 }
1921
1922 public void closeFolder() {
1923 Folder folder = mWorkspace.getOpenFolder();
1924 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07001925 if (folder.isEditingName()) {
1926 folder.dismissEditingName();
1927 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001928 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07001929
1930 // Dismiss the folder cling
1931 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07001932 }
1933 }
1934
1935 void closeFolder(Folder folder) {
1936 folder.getInfo().opened = false;
1937
1938 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1939 if (parent != null) {
1940 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1941 shrinkAndFadeInFolderIcon(fi);
1942 }
1943 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 }
1945
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001947 if (mState != State.WORKSPACE) {
1948 return false;
1949 }
1950
Joe Onorato9c1289c2009-08-17 11:03:03 -04001951 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 return false;
1953 }
1954
1955 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001956 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 }
1958
Michael Jurka0280c3b2010-09-17 15:00:07 -07001959 resetAddInfo();
1960 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001962 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963 return true;
1964 }
1965
Winson Chung3d503fb2011-07-13 17:25:49 -07001966 // The hotseat touch handling does not go through Workspace, and we always allow long press
1967 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001968 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001969 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1970 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001971 if (itemUnderLongClick == null) {
1972 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001973 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1974 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001975 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001977 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001978 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001979 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 }
1981 }
1982 }
1983 return true;
1984 }
1985
Winson Chung3d503fb2011-07-13 17:25:49 -07001986 boolean isHotseatLayout(View layout) {
1987 return mHotseat != null && layout != null &&
1988 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1989 }
1990 Hotseat getHotseat() {
1991 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001992 }
1993
Winson Chung3d503fb2011-07-13 17:25:49 -07001994 /**
1995 * Returns the CellLayout of the specified container at the specified screen.
1996 */
1997 CellLayout getCellLayout(long container, int screen) {
1998 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1999 if (mHotseat != null) {
2000 return mHotseat.getLayout();
2001 } else {
2002 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002003 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002004 } else {
2005 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002006 }
2007 }
2008
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 Workspace getWorkspace() {
2010 return mWorkspace;
2011 }
2012
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 @Override
2014 protected Dialog onCreateDialog(int id) {
2015 switch (id) {
2016 case DIALOG_CREATE_SHORTCUT:
2017 return new CreateShortcut().createDialog();
2018 case DIALOG_RENAME_FOLDER:
2019 return new RenameFolder().createDialog();
2020 }
2021
2022 return super.onCreateDialog(id);
2023 }
2024
2025 @Override
2026 protected void onPrepareDialog(int id, Dialog dialog) {
2027 switch (id) {
2028 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 break;
2030 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002031 if (mFolderInfo != null) {
2032 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2033 final CharSequence text = mFolderInfo.title;
2034 input.setText(text);
2035 input.setSelection(0, text.length());
2036 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 break;
2038 }
2039 }
2040
2041 void showRenameDialog(FolderInfo info) {
2042 mFolderInfo = info;
2043 mWaitingForResult = true;
2044 showDialog(DIALOG_RENAME_FOLDER);
2045 }
2046
Adam Cohenfbba09b2011-07-18 21:43:05 -07002047 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002048 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002049 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2050 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 mWaitingForResult = true;
2052 showDialog(DIALOG_CREATE_SHORTCUT);
2053 }
2054
2055 private class RenameFolder {
2056 private EditText mInput;
2057
2058 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2060 mInput = (EditText) layout.findViewById(R.id.folder_name);
2061
2062 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2063 builder.setIcon(0);
2064 builder.setTitle(getString(R.string.rename_folder_title));
2065 builder.setCancelable(true);
2066 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2067 public void onCancel(DialogInterface dialog) {
2068 cleanup();
2069 }
2070 });
2071 builder.setNegativeButton(getString(R.string.cancel_action),
2072 new Dialog.OnClickListener() {
2073 public void onClick(DialogInterface dialog, int which) {
2074 cleanup();
2075 }
2076 }
2077 );
2078 builder.setPositiveButton(getString(R.string.rename_action),
2079 new Dialog.OnClickListener() {
2080 public void onClick(DialogInterface dialog, int which) {
2081 changeFolderName();
2082 }
2083 }
2084 );
2085 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002086
2087 final AlertDialog dialog = builder.create();
2088 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2089 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002090 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002091 mInput.requestFocus();
2092 InputMethodManager inputManager = (InputMethodManager)
2093 getSystemService(Context.INPUT_METHOD_SERVICE);
2094 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002095 }
2096 });
2097
2098 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 }
2100
2101 private void changeFolderName() {
2102 final String name = mInput.getText().toString();
2103 if (!TextUtils.isEmpty(name)) {
2104 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002105 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 mFolderInfo.title = name;
2107 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2108
Joe Onorato9c1289c2009-08-17 11:03:03 -04002109 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002110 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002111 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 } else {
2113 final FolderIcon folderIcon = (FolderIcon)
2114 mWorkspace.getViewForTag(mFolderInfo);
2115 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002116 // TODO: At some point we'll probably want some version of setting
2117 // the text for a folder icon.
2118 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 getWorkspace().requestLayout();
2120 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002121 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002122 mWorkspaceLoading = true;
2123 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002124 }
2125 }
2126 }
2127 cleanup();
2128 }
2129
2130 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 dismissDialog(DIALOG_RENAME_FOLDER);
2132 mWaitingForResult = false;
2133 mFolderInfo = null;
2134 }
2135 }
2136
Daniel Sandler843e8602010-06-07 14:59:01 -04002137 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2138 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002139 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002140 }
2141
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002142 // AllAppsView.Watcher
2143 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002144 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002145 mWorkspace.setVisibility(View.GONE);
2146 }
2147 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002148
2149 /**
2150 * Helper method for the cameraZoomIn/cameraZoomOut animations
2151 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002152 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002153 * @param scaleFactor The scale factor used for the zoom
2154 */
2155 private void setPivotsForZoom(View view, State state, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002156 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002157 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002158 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002159
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002160 void updateWallpaperVisibility(boolean visible) {
2161 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2162 int curflags = getWindow().getAttributes().flags
2163 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2164 if (wpflags != curflags) {
2165 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2166 }
2167 }
2168
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002169 /**
2170 * Zoom the camera out from the workspace to reveal 'toView'.
2171 * Assumes that the view to show is anchored at either the very top or very bottom
2172 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002173 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002174 */
Winson Chungc07918d2011-07-01 15:35:26 -07002175 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002176 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002177 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002178
Winson Chungf0ea4d32011-06-06 14:27:16 -07002179 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2180 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2181 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2182 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002183 final int startDelay =
2184 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002185
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002186 setPivotsForZoom(toView, toState, scale);
2187
Michael Jurkad74c9842011-07-10 12:44:21 -07002188 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002189 mWorkspace.changeState(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002190
2191 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002192 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002193 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002194 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2195 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002196 ((View) toView.getParent()).invalidate();
2197 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002198 toView.setFastScaleX(a * scale + b * 1f);
2199 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002200 }
2201 });
Adam Cohen61033d32010-11-15 18:29:44 -08002202
Winson Chungf0ea4d32011-06-06 14:27:16 -07002203 toView.setVisibility(View.VISIBLE);
2204 toView.setFastAlpha(0f);
2205 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2206 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2207 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2208 public void onAnimationUpdate(float a, float b) {
2209 // don't need to invalidate because we do so above
2210 toView.setFastAlpha(a * 0f + b * 1f);
2211 }
2212 });
Adam Cohencff6af82011-09-13 14:51:53 -07002213 alphaAnim.setStartDelay(startDelay);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002214 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002215
Michael Jurkaabded662011-03-04 12:06:57 -08002216 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002217 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, scaleAnim,
2218 false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002219 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002220 scaleAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002221 boolean animationCancelled = false;
2222
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002223 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002224 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002225 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002226 // Prepare the position
2227 toView.setTranslationX(0.0f);
2228 toView.setTranslationY(0.0f);
2229 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002230 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002231 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002232 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002233 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002234 // If we don't set the final scale values here, if this animation is cancelled
2235 // it will have the wrong scale value and subsequent cameraPan animations will
2236 // not fix that
2237 toView.setScaleX(1.0f);
2238 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002239 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002240 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance,
2241 scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002242 }
Winson Chung32174c82011-07-19 15:47:55 -07002243
2244 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2245 // Hide the workspace scrollbar
2246 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002247 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002248 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002249 if (!animationCancelled) {
2250 updateWallpaperVisibility(false);
2251 }
2252 }
2253
Adam Cohencff6af82011-09-13 14:51:53 -07002254 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002255 public void onAnimationCancel(Animator animation) {
2256 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002257 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002258 });
2259
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002260 // toView should appear right at the end of the workspace shrink animation
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002261
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002262 if (mStateAnimation != null) mStateAnimation.cancel();
2263 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002264 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002265 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002266 } else {
2267 toView.setTranslationX(0.0f);
2268 toView.setTranslationY(0.0f);
2269 toView.setScaleX(1.0f);
2270 toView.setScaleY(1.0f);
2271 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002272 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002273 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002274 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, null, false);
2275 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance, null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002276
Winson Chungc07918d2011-07-01 15:35:26 -07002277 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2278 // Hide the workspace scrollbar
2279 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002280 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002281 }
Michael Jurkaabded662011-03-04 12:06:57 -08002282 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002283 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002284 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002285 }
2286
2287 /**
2288 * Zoom the camera back into the workspace, hiding 'fromView'.
2289 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002290 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002291 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002292 */
Winson Chungc07918d2011-07-01 15:35:26 -07002293 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002294 Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002295 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002296
Winson Chungf0ea4d32011-06-06 14:27:16 -07002297 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2298 final float scaleFactor = (float)
2299 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2300 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002301
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002302 setPivotsForZoom(fromView, fromState, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002303 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002304 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002305 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002306 if (mStateAnimation != null) mStateAnimation.cancel();
2307 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002308
Michael Jurka742574b2011-02-02 23:51:01 -08002309 final float oldScaleX = fromView.getScaleX();
2310 final float oldScaleY = fromView.getScaleY();
2311
2312 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2313 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002314 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2315 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002316 ((View)fromView.getParent()).fastInvalidate();
2317 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2318 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2319 }
2320 });
Michael Jurkaabded662011-03-04 12:06:57 -08002321 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002322 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Adam Cohencff6af82011-09-13 14:51:53 -07002323 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002324 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2325 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002326 // don't need to invalidate because we do so above
2327 fromView.setFastAlpha(a * 1f + b * 0f);
2328 }
2329 });
Michael Jurkaabded662011-03-04 12:06:57 -08002330 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002331 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, alphaAnim,
2332 true);
Michael Jurka2763be32011-02-24 11:19:57 -08002333 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002334 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002335 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002336 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002337 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002338 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002339 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002340 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance,
2341 alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002342 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002343 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002344 }
2345 });
2346
Winson Chungf0ea4d32011-06-06 14:27:16 -07002347 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002348 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002349 } else {
2350 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002351 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002352 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, null, true);
2353 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance, null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002354 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002355 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002356 }
2357
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002358 void showWorkspace(boolean animated) {
Adam Cohencff6af82011-09-13 14:51:53 -07002359 Resources res = getResources();
2360 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2361
2362 mWorkspace.changeState(Workspace.State.NORMAL, animated, stagger);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002363 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002364 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002365 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002366
Adam Cohen7777d962011-08-18 18:58:38 -07002367 mWorkspace.showDockDivider(!animated);
2368 mWorkspace.flashScrollingIndicator();
2369
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002370 // Change the state *after* we've called all the transition code
2371 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002372
Winson Chung5fb63472011-02-02 17:03:37 -08002373 // Resume the auto-advance of widgets
2374 mUserPresent = true;
2375 updateRunning();
2376
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002377 // send an accessibility event to announce the context change
2378 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002379 }
2380
Adam Cohen7777d962011-08-18 18:58:38 -07002381 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002382 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002383 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Winson Chungb26f3d62011-06-02 10:49:29 -07002384 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002385 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002386 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002387 }
Adam Cohen7777d962011-08-18 18:58:38 -07002388
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002389 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chung09bfc452011-09-09 11:30:20 -07002390 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2391
Winson Chunge7a03942011-08-05 15:05:12 -07002392 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002393 @Override
2394 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002395 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002396 // Before we show workspace, hide all apps again because
2397 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2398 // clean up our state transition functions
2399 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002400 mSearchDropTargetBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002401 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002402 } else {
2403 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002404 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002405 }
2406 }, (extendedDelay ?
2407 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2408 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2409 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002410 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002411 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002412 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2413 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002414 }
2415 // Otherwise, we are not in spring loaded mode, so don't do anything.
2416 }
2417
Adam Cohenfc53cd22011-07-20 15:45:11 -07002418 public boolean isAllAppsCustomizeOpen() {
2419 return mState == State.APPS_CUSTOMIZE;
2420 }
2421
Winson Chungf0ea4d32011-06-06 14:27:16 -07002422 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002423 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002424 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002425 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002426 if (!LauncherApplication.isScreenLarge()) {
2427 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002428 int duration = mSearchDropTargetBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002429 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002430 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002431 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002432 }
2433 }
2434 }
2435
2436 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002437 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002438 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002439 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002440 if (!LauncherApplication.isScreenLarge()) {
2441 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002442 int duration = mSearchDropTargetBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002443 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002444 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002445 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002446 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002447 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002448 }
2449
Winson Chung785d2eb2011-04-14 16:08:02 -07002450 void showAllApps(boolean animated) {
2451 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002452
Winson Chungf0ea4d32011-06-06 14:27:16 -07002453 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2454 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002455
Winson Chung3d503fb2011-07-13 17:25:49 -07002456 // Hide the search bar and hotseat
Winson Chungc51db6a2011-10-05 11:44:49 -07002457 mSearchDropTargetBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002458
Winson Chungf0ea4d32011-06-06 14:27:16 -07002459 // Change the state *after* we've called all the transition code
2460 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002461
2462 // Pause the auto-advance of widgets until we are out of AllApps
2463 mUserPresent = false;
2464 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002465 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002466
2467 // Send an accessibility event to announce the context change
2468 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2469 }
2470
Joe Onoratob2061212009-11-24 16:13:54 -05002471 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002472 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002473 * - Home from workspace
2474 * - from center screen
2475 * - from other screens
2476 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002477 * - from center screen
2478 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002479 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002480 * - from center screen
2481 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002482 * - Launch app from workspace and quit
2483 * - with back
2484 * - with home
2485 * - Launch app from all apps and quit
2486 * - with back
2487 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002488 * - Go to a screen that's not the default, then all
2489 * apps, and launch and app, and go back
2490 * - with back
2491 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002492 * - On workspace, long press power and go back
2493 * - with back
2494 * - with home
2495 * - On all apps, long press power and go back
2496 * - with back
2497 * - with home
2498 * - On workspace, power off
2499 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002500 * - Launch an app and turn off the screen while in that app
2501 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002502 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002503 * - From all apps
2504 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002505 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2506 * - From all apps
2507 * - From the center workspace
2508 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002509 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002510 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002511 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2512 mWorkspace.setVisibility(View.VISIBLE);
2513 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002514
Winson Chung3d503fb2011-07-13 17:25:49 -07002515 // Show the search bar and hotseat
Winson Chungc51db6a2011-10-05 11:44:49 -07002516 mSearchDropTargetBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002517
Winson Chungf0ea4d32011-06-06 14:27:16 -07002518 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002519 if (mAllAppsButton != null) {
2520 mAllAppsButton.requestFocus();
2521 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002522 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002523 }
2524
Joe Onorato7c312c12009-08-13 21:36:53 -07002525 void lockAllApps() {
2526 // TODO
2527 }
2528
2529 void unlockAllApps() {
2530 // TODO
2531 }
2532
Patrick Dubroy5f445422011-02-18 14:35:21 -08002533 /**
2534 * Add an item from all apps or customize onto the given workspace screen.
2535 * If layout is null, add to the current screen.
2536 */
2537 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002538 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2539 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002540 } else {
2541 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002542 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002543 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002544
Winson Chungdff8ebb2011-09-08 17:25:31 -07002545 /** Maps the current orientation to an index for referencing orientation correct global icons */
2546 private int getCurrentOrientationIndexForGlobalIcons() {
2547 // default - 0, landscape - 1
2548 switch (getResources().getConfiguration().orientation) {
2549 case Configuration.ORIENTATION_LANDSCAPE:
2550 return 1;
2551 default:
2552 return 0;
2553 }
2554 }
2555
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002556 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002557 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002558 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002559 // Look for the toolbar icon specified in the activity meta-data
2560 Bundle metaData = packageManager.getActivityInfo(
2561 activityName, PackageManager.GET_META_DATA).metaData;
2562 if (metaData != null) {
2563 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2564 if (iconResId != 0) {
2565 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002566 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002567 }
2568 }
2569 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002570 // This can happen if the activity defines an invalid drawable
2571 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2572 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002573 } catch (Resources.NotFoundException nfe) {
2574 // This can happen if the activity defines an invalid drawable
2575 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2576 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002577 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002578 return null;
2579 }
2580
2581 // if successful in getting icon, return it; otherwise, set button to use default drawable
2582 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2583 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2584 TextView button = (TextView) findViewById(buttonId);
2585 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002586 Resources r = getResources();
2587 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2588 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002589
2590 // If we were unable to find the icon via the meta-data, use a generic one
2591 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002592 toolbarIcon = r.getDrawable(fallbackDrawableId);
2593 toolbarIcon.setBounds(0, 0, w, h);
2594 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002595 return null;
2596 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002597 toolbarIcon.setBounds(0, 0, w, h);
2598 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002599 return toolbarIcon.getConstantState();
2600 }
2601 }
2602
2603 // if successful in getting icon, return it; otherwise, set button to use default drawable
2604 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2605 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2606 ImageView button = (ImageView) findViewById(buttonId);
2607 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2608
Michael Jurka19e0fc52011-07-22 18:00:21 -07002609 if (button != null) {
2610 // If we were unable to find the icon via the meta-data, use a
2611 // generic one
2612 if (toolbarIcon == null) {
2613 button.setImageResource(fallbackDrawableId);
2614 } else {
2615 button.setImageDrawable(toolbarIcon);
2616 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002617 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002618
2619 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2620
Michael Jurka0423dcf2010-10-05 14:56:18 -07002621 }
2622
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002623 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2624 TextView button = (TextView) findViewById(buttonId);
2625 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2626 }
2627
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002628 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002629 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002630 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002631 }
2632
Winson Chungc51db6a2011-10-05 11:44:49 -07002633 private boolean updateGlobalSearchIcon() {
2634 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002635 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2636 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002637 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002638 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002639
Winson Chung1cad91e2011-05-25 17:41:01 -07002640 final SearchManager searchManager =
2641 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2642 ComponentName activityName = searchManager.getGlobalSearchActivity();
2643 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002644 int coi = getCurrentOrientationIndexForGlobalIcons();
2645 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002646 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002647 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002648 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2649 searchButton.setVisibility(View.VISIBLE);
2650 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002651 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002652 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002653 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002654 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2655 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2656 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002657 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002658 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002659 }
2660 }
2661
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002662 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002663 updateButtonWithDrawable(R.id.search_button, d);
2664 }
2665
Winson Chungc51db6a2011-10-05 11:44:49 -07002666 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002667 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002668 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002669 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002670
Winson Chungc51db6a2011-10-05 11:44:49 -07002671 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002672 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2673 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002674 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002675 int coi = getCurrentOrientationIndexForGlobalIcons();
2676 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002677 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002678 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002679 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002680 voiceButton.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002681 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002682 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002683 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002684 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002685 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002686 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002687 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002688 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002689
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002690 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002691 updateButtonWithDrawable(R.id.voice_button, d);
2692 }
2693
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002694 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002695 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002696 */
2697 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002698 final View marketButton = findViewById(R.id.market_button);
2699 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2700 // Find the app market activity by resolving an intent.
2701 // (If multiple app markets are installed, it will return the ResolverActivity.)
2702 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002703 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002704 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002705 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002706 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002707 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002708 marketButton.setVisibility(View.VISIBLE);
2709 } else {
2710 // We should hide and disable the view so that we don't try and restore the visibility
2711 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2712 marketButton.setVisibility(View.GONE);
2713 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002714 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002715 }
2716
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002717 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002718 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002719 }
2720
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002721 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002722 * Displays the shortcut creation dialog and launches, if necessary, the
2723 * appropriate activity.
2724 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002725 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002726 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2727 DialogInterface.OnShowListener {
2728
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002729 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002730
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002731 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002732 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002733
Winson Chung55b65502011-05-26 12:03:43 -07002734 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2735 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002736 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002737
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002738 AlertDialog dialog = builder.create();
2739 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002740 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002741 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002742
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002743 return dialog;
2744 }
2745
2746 public void onCancel(DialogInterface dialog) {
2747 mWaitingForResult = false;
2748 cleanup();
2749 }
2750
Romain Guycbb89e42009-06-08 15:52:54 -07002751 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002752 mWaitingForResult = false;
2753 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002754 }
2755
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002756 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002757 try {
2758 dismissDialog(DIALOG_CREATE_SHORTCUT);
2759 } catch (Exception e) {
2760 // An exception is thrown if the dialog is not visible, which is fine
2761 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002762 }
2763
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002764 /**
2765 * Handle the action clicked in the "Add to home" dialog.
2766 */
2767 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002768 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002769
Winson Chung55b65502011-05-26 12:03:43 -07002770 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2771 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002772 case AddAdapter.ITEM_APPLICATION: {
2773 if (mAppsCustomizeTabHost != null) {
2774 mAppsCustomizeTabHost.selectAppsTab();
2775 }
2776 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002777 break;
2778 }
Winson Chung55b65502011-05-26 12:03:43 -07002779 case AddAdapter.ITEM_APPWIDGET: {
2780 if (mAppsCustomizeTabHost != null) {
2781 mAppsCustomizeTabHost.selectWidgetsTab();
2782 }
2783 showAllApps(true);
2784 break;
2785 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002786 case AddAdapter.ITEM_WALLPAPER: {
2787 startWallpaper();
2788 break;
2789 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002790 }
2791 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002792
2793 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002794 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002795 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002796 }
2797
2798 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002799 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002800 */
2801 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2802 @Override
2803 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002804 closeSystemDialogs();
2805 }
2806 }
2807
2808 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002809 * Receives notifications whenever the appwidgets are reset.
2810 */
2811 private class AppWidgetResetObserver extends ContentObserver {
2812 public AppWidgetResetObserver() {
2813 super(new Handler());
2814 }
2815
2816 @Override
2817 public void onChange(boolean selfChange) {
2818 onAppWidgetReset();
2819 }
2820 }
2821
2822 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002823 * If the activity is currently paused, signal that we need to re-run the loader
2824 * in onResume.
2825 *
2826 * This needs to be called from incoming places where resources might have been loaded
2827 * while we are paused. That is becaues the Configuration might be wrong
2828 * when we're not running, and if it comes back to what it was when we
2829 * were paused, we are not restarted.
2830 *
2831 * Implementation of the method from LauncherModel.Callbacks.
2832 *
2833 * @return true if we are currently paused. The caller might be able to
2834 * skip some work in that case since we will come back again.
2835 */
2836 public boolean setLoadOnResume() {
2837 if (mPaused) {
2838 Log.i(TAG, "setLoadOnResume");
2839 mOnResumeNeedsLoad = true;
2840 return true;
2841 } else {
2842 return false;
2843 }
2844 }
2845
2846 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002847 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002848 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002849 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002850 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002851 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002852 } else {
2853 return SCREEN_COUNT / 2;
2854 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002855 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002856
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002857
Joe Onorato9c1289c2009-08-17 11:03:03 -04002858 /**
2859 * Refreshes the shortcuts shown on the workspace.
2860 *
2861 * Implementation of the method from LauncherModel.Callbacks.
2862 */
2863 public void startBinding() {
2864 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002865
2866 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002867 int count = workspace.getChildCount();
2868 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002869 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002870 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2871 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002872 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002873 if (mHotseat != null) {
2874 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002875 }
2876 }
2877
2878 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002879 * Bind the items start-end from the list.
2880 *
2881 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002882 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002883 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002884 setLoadOnResume();
2885
Joe Onorato9c1289c2009-08-17 11:03:03 -04002886 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002887 for (int i=start; i<end; i++) {
2888 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002889
2890 // Short circuit if we are loading dock items for a configuration which has no dock
2891 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2892 mHotseat == null) {
2893 continue;
2894 }
2895
Joe Onorato9c1289c2009-08-17 11:03:03 -04002896 switch (item.itemType) {
2897 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2898 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002899 View shortcut = createShortcut((ShortcutInfo)item);
2900 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2901 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002902 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002903 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002904 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002905 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002906 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002907 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2908 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002909 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002910 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002911 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002912 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002913 }
2914
Joe Onorato9c1289c2009-08-17 11:03:03 -04002915 /**
2916 * Implementation of the method from LauncherModel.Callbacks.
2917 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002918 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002919 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002920 sFolders.clear();
2921 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002922 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002923
2924 /**
2925 * Add the views for a widget to the workspace.
2926 *
2927 * Implementation of the method from LauncherModel.Callbacks.
2928 */
2929 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002930 setLoadOnResume();
2931
Daniel Sandler843e8602010-06-07 14:59:01 -04002932 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2933 if (DEBUG_WIDGETS) {
2934 Log.d(TAG, "bindAppWidget: " + item);
2935 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002936 final Workspace workspace = mWorkspace;
2937
2938 final int appWidgetId = item.appWidgetId;
2939 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002940 if (DEBUG_WIDGETS) {
2941 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2942 }
2943
Joe Onorato9c1289c2009-08-17 11:03:03 -04002944 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2945
Joe Onorato9c1289c2009-08-17 11:03:03 -04002946 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2947 item.hostView.setTag(item);
2948
Winson Chung3d503fb2011-07-13 17:25:49 -07002949 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002950 item.cellY, item.spanX, item.spanY, false);
2951
Adam Cohended9f8d2010-11-03 13:25:16 -07002952 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2953
Joe Onorato9c1289c2009-08-17 11:03:03 -04002954 workspace.requestLayout();
2955
Daniel Sandler843e8602010-06-07 14:59:01 -04002956 if (DEBUG_WIDGETS) {
2957 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2958 + (SystemClock.uptimeMillis()-start) + "ms");
2959 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002960 }
2961
2962 /**
2963 * Callback saying that there aren't any more items to bind.
2964 *
2965 * Implementation of the method from LauncherModel.Callbacks.
2966 */
2967 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002968 setLoadOnResume();
2969
Joe Onorato9c1289c2009-08-17 11:03:03 -04002970 if (mSavedState != null) {
2971 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002972 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002973 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002974 mSavedState = null;
2975 }
2976
2977 if (mSavedInstanceState != null) {
2978 super.onRestoreInstanceState(mSavedInstanceState);
2979 mSavedInstanceState = null;
2980 }
2981
Joe Onorato9c1289c2009-08-17 11:03:03 -04002982 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002983
2984 // If we received the result of any pending adds while the loader was running (e.g. the
2985 // widget configuration forced an orientation change), process them now.
2986 for (int i = 0; i < sPendingAddList.size(); i++) {
2987 completeAdd(sPendingAddList.get(i));
2988 }
2989 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002990
Winson Chungc51db6a2011-10-05 11:44:49 -07002991 // Update the market app icon as necessary (the other icons will be managed in response to
2992 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002993 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07002994
2995 mWorkspace.post(mBuildLayersRunnable);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002996 }
2997
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002998 @Override
2999 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003000 boolean searchVisible = updateGlobalSearchIcon();
3001 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3002 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003003 }
3004
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003005 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003006 * Add the icons for all apps.
3007 *
3008 * Implementation of the method from LauncherModel.Callbacks.
3009 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003010 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3011 // Remove the progress bar entirely; we could also make it GONE
3012 // but better to remove it since we know it's not going to be used
3013 View progressBar = mAppsCustomizeTabHost.
3014 findViewById(R.id.apps_customize_progress_bar);
3015 if (progressBar != null) {
3016 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003017 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003018 // We just post the call to setApps so the user sees the progress bar
3019 // disappear-- otherwise, it just looks like the progress bar froze
3020 // which doesn't look great
3021 mAppsCustomizeTabHost.post(new Runnable() {
3022 public void run() {
3023 if (mAppsCustomizeContent != null) {
3024 mAppsCustomizeContent.setApps(apps);
3025 }
3026 }
3027 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003028 }
3029
3030 /**
3031 * A package was installed.
3032 *
3033 * Implementation of the method from LauncherModel.Callbacks.
3034 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003035 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003036 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003037 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003038
Winson Chung785d2eb2011-04-14 16:08:02 -07003039 if (mAppsCustomizeContent != null) {
3040 mAppsCustomizeContent.addApps(apps);
3041 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003042 }
3043
3044 /**
3045 * A package was updated.
3046 *
3047 * Implementation of the method from LauncherModel.Callbacks.
3048 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003049 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003050 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003051 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003052 if (mWorkspace != null) {
3053 mWorkspace.updateShortcuts(apps);
3054 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003055
Winson Chung785d2eb2011-04-14 16:08:02 -07003056 if (mAppsCustomizeContent != null) {
3057 mAppsCustomizeContent.updateApps(apps);
3058 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003059 }
3060
3061 /**
3062 * A package was uninstalled.
3063 *
3064 * Implementation of the method from LauncherModel.Callbacks.
3065 */
Joe Onorato36115782010-06-17 13:28:48 -04003066 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003067 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003068 if (permanent) {
3069 mWorkspace.removeItems(apps);
3070 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003071
Winson Chung785d2eb2011-04-14 16:08:02 -07003072 if (mAppsCustomizeContent != null) {
3073 mAppsCustomizeContent.removeApps(apps);
3074 }
Winson Chunga1820962011-10-03 16:31:06 -07003075
3076 // Notify the drag controller
3077 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003078 }
Joe Onoratobe386092009-11-17 17:32:16 -08003079
3080 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003081 * A number of packages were updated.
3082 */
3083 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003084 if (mAppsCustomizeContent != null) {
3085 mAppsCustomizeContent.onPackagesUpdated();
3086 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003087 }
3088
Winson Chung400438b2011-01-16 17:53:48 -08003089 private int mapConfigurationOriActivityInfoOri(int configOri) {
3090 final Display d = getWindowManager().getDefaultDisplay();
3091 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3092 switch (d.getRotation()) {
3093 case Surface.ROTATION_0:
3094 case Surface.ROTATION_180:
3095 // We are currently in the same basic orientation as the natural orientation
3096 naturalOri = configOri;
3097 break;
3098 case Surface.ROTATION_90:
3099 case Surface.ROTATION_270:
3100 // We are currently in the other basic orientation to the natural orientation
3101 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3102 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3103 break;
3104 }
3105
3106 int[] oriMap = {
3107 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3108 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3109 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3110 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3111 };
3112 // Since the map starts at portrait, we need to offset if this device's natural orientation
3113 // is landscape.
3114 int indexOffset = 0;
3115 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3116 indexOffset = 1;
3117 }
3118 return oriMap[(d.getRotation() + indexOffset) % 4];
3119 }
Adam Cohen446e9402011-09-15 18:21:21 -07003120
3121 public void lockScreenOrientationOnLargeUI() {
3122 if (LauncherApplication.isScreenLarge()) {
3123 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3124 .getConfiguration().orientation));
3125 }
Winson Chung400438b2011-01-16 17:53:48 -08003126 }
Adam Cohen446e9402011-09-15 18:21:21 -07003127 public void unlockScreenOrientationOnLargeUI() {
3128 if (LauncherApplication.isScreenLarge()) {
3129 mHandler.postDelayed(new Runnable() {
3130 public void run() {
3131 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3132 }
3133 }, mRestoreScreenOrientationDelay);
3134 }
Winson Chung400438b2011-01-16 17:53:48 -08003135 }
3136
Winson Chung82f55532011-08-09 14:14:23 -07003137 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003138 private static final String PREFS_KEY = "com.android.launcher2.prefs";
3139 private boolean isClingsEnabled() {
Winson Chung82f55532011-08-09 14:14:23 -07003140 // TEMPORARY: DISABLE CLINGS ON LARGE UI
Winson Chung7d7541e2011-09-16 20:14:36 -07003141 if (LauncherApplication.isScreenLarge()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003142 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003143 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003144
Winson Chung7d7541e2011-09-16 20:14:36 -07003145 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003146 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003147 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3148 Cling cling = (Cling) findViewById(clingId);
3149 if (cling != null) {
3150 cling.init(this, positionData);
3151 cling.setVisibility(View.VISIBLE);
3152 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3153 if (animate) {
3154 cling.buildLayer();
3155 cling.setAlpha(0f);
3156 cling.animate()
3157 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003158 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003159 .setDuration(SHOW_CLING_DURATION)
3160 .setStartDelay(delay)
3161 .start();
3162 } else {
3163 cling.setAlpha(1f);
3164 }
3165 }
3166 return cling;
3167 }
3168 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003169 if (cling != null) {
3170 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003171 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003172 anim.addListener(new AnimatorListenerAdapter() {
3173 public void onAnimationEnd(Animator animation) {
3174 cling.setVisibility(View.GONE);
3175 cling.cleanup();
3176 SharedPreferences prefs =
3177 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3178 SharedPreferences.Editor editor = prefs.edit();
3179 editor.putBoolean(flag, true);
3180 editor.commit();
3181 };
3182 });
3183 anim.start();
3184 }
3185 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003186 private void removeCling(int id) {
3187 final View cling = findViewById(id);
3188 if (cling != null) {
3189 final ViewGroup parent = (ViewGroup) cling.getParent();
3190 parent.post(new Runnable() {
3191 @Override
3192 public void run() {
3193 parent.removeView(cling);
3194 }
3195 });
3196 }
3197 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003198 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003199 // Enable the clings only if they have not been dismissed before
3200 SharedPreferences prefs =
3201 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003202 if (isClingsEnabled() && !prefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003203 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003204 } else {
3205 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003206 }
3207 }
3208 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003209 // Enable the clings only if they have not been dismissed before
3210 SharedPreferences prefs =
3211 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003212 if (isClingsEnabled() && !prefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003213 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003214 } else {
3215 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003216 }
3217 }
3218 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003219 // Enable the clings only if they have not been dismissed before
3220 SharedPreferences prefs =
3221 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
3222 Cling cling = null;
Winson Chung9d9d74f2011-09-19 11:49:12 -07003223 if (isClingsEnabled() && !prefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003224 cling = initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003225 } else {
3226 removeCling(R.id.folder_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003227 }
3228 return cling;
3229 }
3230 public boolean isFolderClingVisible() {
3231 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003232 if (cling != null) {
3233 return cling.getVisibility() == View.VISIBLE;
3234 }
3235 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003236 }
Winson Chung82f55532011-08-09 14:14:23 -07003237 public void dismissWorkspaceCling(View v) {
3238 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003239 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003240 }
3241 public void dismissAllAppsCling(View v) {
3242 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003243 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3244 }
3245 public void dismissFolderCling(View v) {
3246 Cling cling = (Cling) findViewById(R.id.folder_cling);
3247 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003248 }
3249
Winson Chung80baf5a2010-08-09 16:03:15 -07003250 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003251 * Prints out out state for debugging.
3252 */
3253 public void dumpState() {
3254 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003255 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003256 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3257 Log.d(TAG, "mRestoring=" + mRestoring);
3258 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3259 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003260 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003261 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003262
Winson Chung785d2eb2011-04-14 16:08:02 -07003263 if (mAppsCustomizeContent != null) {
3264 mAppsCustomizeContent.dumpState();
3265 }
Joe Onoratobe386092009-11-17 17:32:16 -08003266 Log.d(TAG, "END launcher2 dump state");
3267 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003268
3269 @Override
3270 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3271 super.dump(prefix, fd, writer, args);
3272 writer.println(" ");
3273 writer.println("Debug logs: ");
3274 for (int i = 0; i < sDumpLogs.size(); i++) {
3275 writer.println(" " + sDumpLogs.get(i));
3276 }
3277 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003278}
Michael Jurka2763be32011-02-24 11:19:57 -08003279
Michael Jurkaabded662011-03-04 12:06:57 -08003280interface LauncherTransitionable {
Winson Chung7d7541e2011-09-16 20:14:36 -07003281 void onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace);
3282 void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003283}