blob: 6d0f88ff354beb13ebfb88268d98c4a8a45d53ee [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;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080027import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.Dialog;
29import android.app.SearchManager;
30import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070031import android.appwidget.AppWidgetManager;
32import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070035import android.content.ClipData;
36import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040037import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080038import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080039import android.content.Context;
40import android.content.DialogInterface;
41import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Adam Cohen716b51e2011-06-30 12:09:54 -070043import android.content.Intent.ShortcutIconResource;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Winson Chungf0ea4d32011-06-06 14:27:16 -070046import android.content.pm.ResolveInfo;
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;
Daniel Sandlerab1ebd72010-04-27 16:57:25 -040050import android.content.res.TypedArray;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080052import android.graphics.Bitmap;
Romain Guya6abce82009-11-10 02:54:41 -080053import android.graphics.Canvas;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Romain Guyff0c2e22009-11-10 12:09:59 -080055import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040063import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080064import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
69import android.text.TextUtils;
70import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080072import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080073import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.KeyEvent;
75import android.view.LayoutInflater;
76import android.view.Menu;
77import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070078import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080079import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.View;
Adam Cohen716b51e2011-06-30 12:09:54 -070081import android.view.ViewGroup;
Michael Jurkae0f5a612011-02-07 16:45:41 -080082import android.view.View.OnClickListener;
Adam Cohen860f4c52011-01-27 20:16:13 -080083import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080084import android.view.accessibility.AccessibilityEvent;
Adam Cohenf16e5712011-01-13 13:31:45 -080085import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070087import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080088import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070089import android.widget.ImageView;
90import android.widget.LinearLayout;
91import android.widget.PopupWindow;
Winson Chung68846fd2010-10-29 11:00:27 -070092import android.widget.TextView;
93import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070094
Adam Cohendf2cc412011-04-27 16:56:57 -070095import com.android.common.Search;
96import com.android.launcher.R;
Michael Jurkad74c9842011-07-10 12:44:21 -070097import com.android.launcher2.Workspace.State;
Romain Guyedcce092010-03-04 13:03:17 -080098
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.DataInputStream;
100import java.io.DataOutputStream;
101import java.io.FileNotFoundException;
102import java.io.IOException;
103import java.util.ArrayList;
104import java.util.HashMap;
105import java.util.List;
106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107/**
108 * Default launcher application.
109 */
Michael Jurka946ad472010-07-09 18:05:18 -0700110public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700111 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
112 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800113 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700114 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
Joe Onorato9c1289c2009-08-17 11:03:03 -0400116 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400117 static final boolean DEBUG_WIDGETS = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 static final boolean DEBUG_USER_INTERFACE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700119
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120 private static final int MENU_GROUP_ADD = 1;
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800121 private static final int MENU_GROUP_WALLPAPER = MENU_GROUP_ADD + 1;
122
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int MENU_ADD = Menu.FIRST + 1;
Winson Chung7ad01412010-09-27 11:33:03 -0700124 private static final int MENU_MANAGE_APPS = MENU_ADD + 1;
125 private static final int MENU_WALLPAPER_SETTINGS = MENU_MANAGE_APPS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int MENU_SEARCH = MENU_WALLPAPER_SETTINGS + 1;
127 private static final int MENU_NOTIFICATIONS = MENU_SEARCH + 1;
Romain Guy94dabf12009-07-21 10:55:43 -0700128 private static final int MENU_SETTINGS = MENU_NOTIFICATIONS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
130 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700131 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132 private static final int REQUEST_PICK_APPLICATION = 6;
133 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700135 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
138
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800139 static final int SCREEN_COUNT = 5;
140 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Joe Onorato7c312c12009-08-13 21:36:53 -0700142 static final int DIALOG_CREATE_SHORTCUT = 1;
143 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
Romain Guy98d01652009-06-30 16:21:04 -0700145 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
147 // Type: int
148 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700149 // Type: int
150 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 // Type: int
152 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
153 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700154 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700156 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: boolean
158 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
159 // Type: long
160 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
161
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700162 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
163
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700164 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700165 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700166 private State mState = State.WORKSPACE;
167 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700168
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700170 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
171 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800174 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800176 private final BroadcastReceiver mCloseSystemDialogsReceiver
177 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800178 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private LayoutInflater mInflater;
181
Joe Onorato00acb122009-08-04 16:04:30 -0400182 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700184
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700185 private AppWidgetManager mAppWidgetManager;
186 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700187
Michael Jurka0280c3b2010-09-17 15:00:07 -0700188 private int mAddScreen = -1;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700189 private int mAddCellX = -1;
190 private int mAddCellY = -1;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700191 private int[] mAddDropPosition;
192 private int[] mTmpAddItemCellCoordinates = new int[2];
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private FolderInfo mFolderInfo;
195
Winson Chung97d85d22011-04-13 11:27:36 -0700196 private ViewGroup mButtonCluster;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800197 private View mAllAppsButton;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700198 private SearchDropTargetBar mSearchDeleteBar;
199 private AppsCustomizeTabHost mAppsCustomizeTabHost;
200 private AppsCustomizePagedView mAppsCustomizeContent;
201 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 private Bundle mSavedState;
204
205 private SpannableStringBuilder mDefaultKeySsb = null;
206
Joe Onorato9c1289c2009-08-17 11:03:03 -0400207 private boolean mWorkspaceLoading = true;
208
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800209 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 private boolean mRestoring;
211 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700212 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800213
214 private Bundle mSavedInstanceState;
215
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800217 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800218 private boolean mUserPresent = true;
219 private boolean mVisible = false;
220 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700222 private static LocaleConfiguration sLocaleConfiguration = null;
223
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700224 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700225
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400226 // Hotseats (quick-launch icons next to AllApps)
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400227 private String[] mHotseatConfig = null;
228 private Intent[] mHotseats = null;
229 private Drawable[] mHotseatIcons = null;
230 private CharSequence[] mHotseatLabels = null;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400231
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700232 private Intent mAppMarketIntent = null;
233
Adam Cohended9f8d2010-11-03 13:25:16 -0700234 // Related to the auto-advancing of widgets
235 private final int ADVANCE_MSG = 1;
236 private final int mAdvanceInterval = 20000;
237 private final int mAdvanceStagger = 250;
238 private long mAutoAdvanceSentTime;
239 private long mAutoAdvanceTimeLeft = -1;
240 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
241 new HashMap<View, AppWidgetProviderInfo>();
242
Winson Chung400438b2011-01-16 17:53:48 -0800243 // Determines how long to wait after a rotation before restoring the screen orientation to
244 // match the sensor state.
245 private final int mRestoreScreenOrientationDelay = 500;
246
Michael Jurka4ef207b2010-11-29 17:05:45 -0800247 // External icons saved in case of resource changes, orientation, etc.
Michael Jurkae7bf83b2010-12-14 18:02:21 -0800248 private static Drawable.ConstantState sGlobalSearchIcon;
249 private static Drawable.ConstantState sVoiceSearchIcon;
250 private static Drawable.ConstantState sAppMarketIcon;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800251
Adam Cohen2801caf2011-05-13 20:57:39 -0700252 private DragLayer mDragLayer;
253
Michael Jurkaddd62e92011-02-16 17:49:14 -0800254 private BubbleTextView mWaitingForResume;
255
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800256 private static ArrayList<PendingAddArguments> sPendingAddList
257 = new ArrayList<PendingAddArguments>();
258
259 private static class PendingAddArguments {
260 int requestCode;
261 Intent intent;
262 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
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400285 loadHotseats();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 setContentView(R.layout.launcher);
288 setupViews();
289
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
310 // For handling default keys
311 mDefaultKeySsb = new SpannableStringBuilder();
312 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800313
314 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
315 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800316
317 // If we have a saved version of these external icons, we load them up immediately
Winson Chungf0ea4d32011-06-06 14:27:16 -0700318 if (sGlobalSearchIcon == null || sVoiceSearchIcon == null || sAppMarketIcon == null) {
319 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +0100320 updateGlobalSearchIcon();
Winson Chungf0ea4d32011-06-06 14:27:16 -0700321 }
322 if (sGlobalSearchIcon != null) {
323 updateGlobalSearchIcon(sGlobalSearchIcon);
324 }
325 if (sVoiceSearchIcon != null) {
326 updateVoiceSearchIcon(sVoiceSearchIcon);
327 }
328 if (sAppMarketIcon != null) {
329 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800330 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800331 }
Romain Guycbb89e42009-06-08 15:52:54 -0700332
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800333 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700334 if (sLocaleConfiguration == null) {
335 new AsyncTask<Void, Void, LocaleConfiguration>() {
336 @Override
337 protected LocaleConfiguration doInBackground(Void... unused) {
338 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
339 readConfiguration(Launcher.this, localeConfiguration);
340 return localeConfiguration;
341 }
342
343 @Override
344 protected void onPostExecute(LocaleConfiguration result) {
345 sLocaleConfiguration = result;
346 checkForLocaleChange(); // recursive, but now with a locale configuration
347 }
348 }.execute();
349 return;
350 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 final Configuration configuration = getResources().getConfiguration();
353
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700354 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 final String locale = configuration.locale.toString();
356
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700357 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 final int mcc = configuration.mcc;
359
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700360 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 final int mnc = configuration.mnc;
362
Romain Guy84f296c2009-11-04 15:00:44 -0800363 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364
Romain Guy84f296c2009-11-04 15:00:44 -0800365 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700366 sLocaleConfiguration.locale = locale;
367 sLocaleConfiguration.mcc = mcc;
368 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700369
Joe Onorato0589f0f2010-02-08 13:44:00 -0800370 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400371 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700372
373 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
374 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700375 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700376 public void run() {
377 writeConfiguration(Launcher.this, localeConfiguration);
378 }
379 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700380 }
381 }
382
383 private static class LocaleConfiguration {
384 public String locale;
385 public int mcc = -1;
386 public int mnc = -1;
387 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700388
Romain Guy98d01652009-06-30 16:21:04 -0700389 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
390 DataInputStream in = null;
391 try {
392 in = new DataInputStream(context.openFileInput(PREFERENCES));
393 configuration.locale = in.readUTF();
394 configuration.mcc = in.readInt();
395 configuration.mnc = in.readInt();
396 } catch (FileNotFoundException e) {
397 // Ignore
398 } catch (IOException e) {
399 // Ignore
400 } finally {
401 if (in != null) {
402 try {
403 in.close();
404 } catch (IOException e) {
405 // Ignore
406 }
407 }
408 }
409 }
410
411 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
412 DataOutputStream out = null;
413 try {
414 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
415 out.writeUTF(configuration.locale);
416 out.writeInt(configuration.mcc);
417 out.writeInt(configuration.mnc);
418 out.flush();
419 } catch (FileNotFoundException e) {
420 // Ignore
421 } catch (IOException e) {
422 //noinspection ResultOfMethodCallIgnored
423 context.getFileStreamPath(PREFERENCES).delete();
424 } finally {
425 if (out != null) {
426 try {
427 out.close();
428 } catch (IOException e) {
429 // Ignore
430 }
431 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 }
433 }
434
Adam Cohen716b51e2011-06-30 12:09:54 -0700435 public DragLayer getDragLayer() {
436 return mDragLayer;
437 }
438
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 static int getScreen() {
440 synchronized (sLock) {
441 return sScreen;
442 }
443 }
444
445 static void setScreen(int screen) {
446 synchronized (sLock) {
447 sScreen = screen;
448 }
449 }
450
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400451 // Note: This doesn't do all the client-id magic that BrowserProvider does
452 // in Browser. (http://b/2425179)
453 private Uri getDefaultBrowserUri() {
454 String url = getString(R.string.default_browser_url);
455 if (url.indexOf("{CID}") != -1) {
456 url = url.replace("{CID}", "android-google");
457 }
458 return Uri.parse(url);
459 }
460
461 // Load the Intent templates from arrays.xml to populate the hotseats. For
462 // each Intent, if it resolves to a single app, use that as the launch
463 // intent & use that app's label as the contentDescription. Otherwise,
464 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400465 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400466 if (mHotseatConfig == null) {
467 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
468 if (mHotseatConfig.length > 0) {
469 mHotseats = new Intent[mHotseatConfig.length];
470 mHotseatLabels = new CharSequence[mHotseatConfig.length];
471 mHotseatIcons = new Drawable[mHotseatConfig.length];
472 } else {
473 mHotseats = null;
474 mHotseatIcons = null;
475 mHotseatLabels = null;
476 }
477
478 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
479 for (int i=0; i<mHotseatConfig.length; i++) {
480 // load icon for this slot; currently unrelated to the actual activity
481 try {
482 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
483 } catch (ArrayIndexOutOfBoundsException ex) {
484 Log.w(TAG, "Missing hotseat_icons array item #" + i);
485 mHotseatIcons[i] = null;
486 }
487 }
488 hotseatIconDrawables.recycle();
489 }
490
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400491 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400492 for (int i=0; i<mHotseatConfig.length; i++) {
493 Intent intent = null;
494 if (mHotseatConfig[i].equals("*BROWSER*")) {
495 // magic value meaning "launch user's default web browser"
496 // replace it with a generic web request so we can see if there is indeed a default
497 String defaultUri = getString(R.string.default_browser_url);
498 intent = new Intent(
499 Intent.ACTION_VIEW,
500 ((defaultUri != null)
501 ? Uri.parse(defaultUri)
502 : getDefaultBrowserUri())
503 ).addCategory(Intent.CATEGORY_BROWSABLE);
504 // note: if the user launches this without a default set, she
505 // will always be taken to the default URL above; this is
506 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700507 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400508 // URL is unavoidably sent to the chosen app.
509 } else {
510 try {
511 intent = Intent.parseUri(mHotseatConfig[i], 0);
512 } catch (java.net.URISyntaxException ex) {
513 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
514 // bogus; leave intent=null
515 }
516 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700517
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400518 if (intent == null) {
519 mHotseats[i] = null;
520 mHotseatLabels[i] = getText(R.string.activity_not_found);
521 continue;
522 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400523
524 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700525 Log.d(TAG, "loadHotseats: hotseat " + i
526 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400527 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400528 + "]");
529 }
530
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400531 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
532 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700533 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400534 Log.d(TAG, "Best match for intent: " + bestMatch);
535 Log.d(TAG, "All matches: ");
536 for (ResolveInfo ri : allMatches) {
537 Log.d(TAG, " --> " + ri);
538 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400539 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400540 // did this resolve to a single app, or the resolver?
541 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700542 // can't find any activity to handle this. let's leave the
543 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400544 // to launch.
545 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400546
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400547 // set accessibility text to "Not installed"
548 mHotseatLabels[i] = getText(R.string.activity_not_found);
549 } else {
550 boolean found = false;
551 for (ResolveInfo ri : allMatches) {
552 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
553 && bestMatch.activityInfo.applicationInfo.packageName
554 .equals(ri.activityInfo.applicationInfo.packageName)) {
555 found = true;
556 break;
557 }
558 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700559
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400560 if (!found) {
561 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
562 // the bestMatch is probably the ResolveActivity, meaning the
563 // user has not yet selected a default
564 // so: we'll keep the original intent for now
565 mHotseats[i] = intent;
566
567 // set the accessibility text to "Select shortcut"
568 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
569 } else {
570 // we have an app!
571 // now reconstruct the intent to launch it through the front
572 // door
573 ComponentName com = new ComponentName(
574 bestMatch.activityInfo.applicationInfo.packageName,
575 bestMatch.activityInfo.name);
576 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
577
578 // load the app label for accessibility
579 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
580 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400581 }
582
583 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700584 Log.d(TAG, "loadHotseats: hotseat " + i
585 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400586 + ((mHotseats[i] == null)
587 ? "null"
588 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400589 + "] label=[" + mHotseatLabels[i]
590 + "]"
591 );
592 }
593 }
594 }
595
Winson Chung557d6ed2011-07-08 15:34:52 -0700596 /**
597 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
598 * a configuration step, this allows the proper animations to run after other transitions.
599 */
600 private boolean completeAdd(PendingAddArguments args) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800601 switch (args.requestCode) {
602 case REQUEST_PICK_APPLICATION:
603 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
604 break;
605 case REQUEST_PICK_SHORTCUT:
606 processShortcut(args.intent);
607 break;
608 case REQUEST_CREATE_SHORTCUT:
609 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
Winson Chung557d6ed2011-07-08 15:34:52 -0700610 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800611 case REQUEST_PICK_APPWIDGET:
612 addAppWidgetFromPick(args.intent);
613 break;
614 case REQUEST_CREATE_APPWIDGET:
615 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
616 completeAddAppWidget(appWidgetId, args.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -0700617 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800618 case REQUEST_PICK_WALLPAPER:
619 // We just wanted the activity result here so we can clear mWaitingForResult
620 break;
621 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700622 return false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800623 }
624
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800626 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700627 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700628 mWaitingForResult = false;
629
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 // The pattern used here is that a user PICKs a specific application,
631 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
634 // launch over to the Music app to actually CREATE_SHORTCUT.
Michael Jurka0280c3b2010-09-17 15:00:07 -0700635 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800636 final PendingAddArguments args = new PendingAddArguments();
637 args.requestCode = requestCode;
638 args.intent = data;
639 args.screen = mAddScreen;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700640 args.cellX = mAddCellX;
641 args.cellY = mAddCellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800642
643 // If the loader is still running, defer the add until it is done.
644 if (isWorkspaceLocked()) {
645 sPendingAddList.add(args);
646 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700647 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
Romain Guy18042c82009-11-06 11:44:55 -0800649 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700650 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
651 if (data != null) {
652 // Clean up the appWidgetId if we canceled
653 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
654 if (appWidgetId != -1) {
655 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
656 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 }
658 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700659
660 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
661 exitSpringLoadedDragModeDelayed(delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 }
663
664 @Override
665 protected void onResume() {
666 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800667 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700668 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400669 mWorkspaceLoading = true;
670 mModel.startLoader(this, true);
671 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700672 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800673 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800674 if (mWaitingForResume != null) {
675 mWaitingForResume.setStayPressed(false);
676 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700677 // When we resume Launcher, a different Activity might be responsible for the app
678 // market intent, so refresh the icon
679 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 }
681
682 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700683 protected void onPause() {
684 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700685 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800686 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700687 }
Romain Guycbb89e42009-06-08 15:52:54 -0700688
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700689 @Override
690 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400691 // Flag the loader to stop early before switching
692 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700693 if (mAppsCustomizeContent != null) {
694 mAppsCustomizeContent.surrender();
695 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800696 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700697 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700698
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800699 // We can't hide the IME if it was forced open. So don't bother
700 /*
701 @Override
702 public void onWindowFocusChanged(boolean hasFocus) {
703 super.onWindowFocusChanged(hasFocus);
704
705 if (hasFocus) {
706 final InputMethodManager inputManager = (InputMethodManager)
707 getSystemService(Context.INPUT_METHOD_SERVICE);
708 WindowManager.LayoutParams lp = getWindow().getAttributes();
709 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
710 android.os.Handler()) {
711 protected void onReceiveResult(int resultCode, Bundle resultData) {
712 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
713 }
714 });
715 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
716 }
717 }
718 */
719
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 private boolean acceptFilter() {
721 final InputMethodManager inputManager = (InputMethodManager)
722 getSystemService(Context.INPUT_METHOD_SERVICE);
723 return !inputManager.isFullscreenMode();
724 }
725
726 @Override
727 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700728 final int uniChar = event.getUnicodeChar();
729 final boolean handled = super.onKeyDown(keyCode, event);
730 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
731 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800732 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
733 keyCode, event);
734 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700735 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700736 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700737 // showSearchDialog()
738 // If there are multiple keystrokes before the search dialog takes focus,
739 // onSearchRequested() will be called for every keystroke,
740 // but it is idempotent, so it's fine.
741 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 }
743 }
744
Joe Onorato8a9625e2010-01-28 15:55:35 -0800745 // Eat the long press event so the keyboard doesn't come up.
746 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
747 return true;
748 }
749
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800750 return handled;
751 }
752
Karl Rosaen138a0412009-04-23 19:00:21 -0700753 private String getTypedText() {
754 return mDefaultKeySsb.toString();
755 }
756
757 private void clearTypedText() {
758 mDefaultKeySsb.clear();
759 mDefaultKeySsb.clearSpans();
760 Selection.setSelection(mDefaultKeySsb, 0);
761 }
762
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800763 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700764 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
765 * State
766 */
767 private static State intToState(int stateOrdinal) {
768 State state = State.WORKSPACE;
769 final State[] stateValues = State.values();
770 for (int i = 0; i < stateValues.length; i++) {
771 if (stateValues[i].ordinal() == stateOrdinal) {
772 state = stateValues[i];
773 break;
774 }
775 }
776 return state;
777 }
778
779 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 * Restores the previous state, if it exists.
781 *
782 * @param savedState The previous state.
783 */
784 private void restoreState(Bundle savedState) {
785 if (savedState == null) {
786 return;
787 }
788
Michael Jurka883f55b2010-10-21 15:47:14 -0700789 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
790
Winson Chungf0ea4d32011-06-06 14:27:16 -0700791 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800792 showAllApps(false);
793 }
794
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800795 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
796 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700797 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 }
799
800 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700801
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700803 mAddScreen = addScreen;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700804 mAddCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
805 mAddCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800806 mRestoring = true;
807 }
808
809 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
810 if (renameFolder) {
811 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700812 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800813 mRestoring = true;
814 }
Winson Chunga12a2502010-12-20 14:41:35 -0800815
Winson Chung785d2eb2011-04-14 16:08:02 -0700816
817 // Restore the AppsCustomize tab
818 if (mAppsCustomizeTabHost != null) {
819 String curTab = savedState.getString("apps_customize_currentTab");
820 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700821 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700822 mAppsCustomizeContent.setContentType(
823 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
824 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
825 }
826
827 // Note: currently we do not restore the page for the AppsCustomize pane because the
828 // change in layout can drastically affect the saved page index
829 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
831
832 /**
833 * Finds all the views we need and configure them properly.
834 */
835 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700836 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400837
Winson Chung4c98d922011-05-31 16:50:48 -0700838 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
839 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800840
Winson Chung4c98d922011-05-31 16:50:48 -0700841 // Setup the drag layer
842 mDragLayer.setup(this, dragController);
843
844 // Setup the workspace
845 mWorkspace.setHapticFeedbackEnabled(false);
846 mWorkspace.setOnLongClickListener(this);
847 mWorkspace.setup(this, dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700848 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700849
Winson Chungf0ea4d32011-06-06 14:27:16 -0700850 // Get the search/delete bar
851 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700852
Winson Chungf0ea4d32011-06-06 14:27:16 -0700853 // Setup AppsCustomize
854 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
855 findViewById(R.id.apps_customize_pane);
856 mAppsCustomizeContent = (AppsCustomizePagedView)
857 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
858 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400859
Winson Chungf0ea4d32011-06-06 14:27:16 -0700860 // Setup AppsCustomize button
861 mAllAppsButton = mDragLayer.findViewById(R.id.all_apps_button);
862 mAllAppsButton.setOnClickListener(new OnClickListener() {
863 public void onClick(View v) {
864 onClickAllAppsButton(v);
865 }
866 });
Romain Guy1fbc1c82009-11-09 20:43:08 -0800867
Winson Chungf0ea4d32011-06-06 14:27:16 -0700868 if (!LauncherApplication.isScreenLarge()) {
869 // Setup AppsCustomize button on the phone
870 HandleView handleView = (HandleView) mAllAppsButton;
871 handleView.setLauncher(this);
872 handleView.setOnLongClickListener(this);
Winson Chung4c98d922011-05-31 16:50:48 -0700873
874 // Setup Hotseat
875 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
876 hotseatLeft.setContentDescription(mHotseatLabels[0]);
877 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
878 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
879 hotseatRight.setContentDescription(mHotseatLabels[1]);
880 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Adam Cohencdc30d52010-12-01 15:09:47 -0800881 }
882
Winson Chungf0ea4d32011-06-06 14:27:16 -0700883 if (!LauncherApplication.isScreenLarge()) {
884 // Setup keylistener for button cluster
885 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
886 View.OnKeyListener listener = null;
887 if (LauncherApplication.isScreenLarge()) {
888 // For tablets, AllApps lives in the button bar at the top
889 listener = new ButtonBarKeyEventListener();
890 } else {
891 // For phones, AppsCustomize lives in the "dock" at the bottom
892 listener = new DockKeyEventListener();
893 }
894 int buttonCount = mButtonCluster.getChildCount();
895 for (int i = 0; i < buttonCount; ++i) {
896 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
897 }
Winson Chung97d85d22011-04-13 11:27:36 -0700898 }
Michael Jurka838a4ca2011-02-07 13:33:06 -0800899
Winson Chung4c98d922011-05-31 16:50:48 -0700900 // Setup the drag controller (the drop targets have to be added in reverse order)
901 dragController.setDragScoller(mWorkspace);
902 dragController.setScrollView(mDragLayer);
903 dragController.setMoveTarget(mWorkspace);
904 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700905 if (mSearchDeleteBar != null) {
906 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800907 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 }
909
Romain Guy8a73c512009-11-09 19:19:59 -0800910 @SuppressWarnings({"UnusedDeclaration"})
911 public void previousScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700912 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800913 mWorkspace.scrollLeft();
914 }
Romain Guy8a73c512009-11-09 19:19:59 -0800915 }
916
917 @SuppressWarnings({"UnusedDeclaration"})
918 public void nextScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700919 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800920 mWorkspace.scrollRight();
921 }
Romain Guy8a73c512009-11-09 19:19:59 -0800922 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400923
924 @SuppressWarnings({"UnusedDeclaration"})
925 public void launchHotSeat(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700926 if (mState == State.APPS_CUSTOMIZE) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400927
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400928 int index = -1;
929 if (v.getId() == R.id.hotseat_left) {
930 index = 0;
931 } else if (v.getId() == R.id.hotseat_right) {
932 index = 1;
933 }
934
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400935 // reload these every tap; you never know when they might change
936 loadHotseats();
937 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400938 startActivitySafely(
939 mHotseats[index],
940 "hotseat"
941 );
942 }
943 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700944
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 /**
946 * Creates a view representing a shortcut.
947 *
948 * @param info The data structure describing the shortcut.
949 *
950 * @return A View inflated from R.layout.application.
951 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800952 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700954 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 }
956
957 /**
958 * Creates a view representing a shortcut inflated from the specified resource.
959 *
960 * @param layoutResId The id of the XML layout used to create the shortcut.
961 * @param parent The group the shortcut belongs to.
962 * @param info The data structure describing the shortcut.
963 *
964 * @return A View inflated from layoutResId.
965 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800966 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800967 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
968 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 return favorite;
971 }
972
973 /**
974 * Add an application shortcut to the workspace.
975 *
976 * @param data The intent describing the application.
977 * @param cellInfo The position on screen where to create the shortcut.
978 */
Adam Cohenfbba09b2011-07-18 21:43:05 -0700979 void completeAddApplication(Intent data, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700980 final int[] cellXY = mTmpAddItemCellCoordinates;
981 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
982
Adam Cohenfbba09b2011-07-18 21:43:05 -0700983 // First we check if we already know the exact location where we want to add this item.
984 if (cellX >= 0 && cellY >= 0) {
985 cellXY[0] = cellX;
986 cellXY[1] = cellY;
987 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700988 showOutOfSpaceMessage();
989 return;
990 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800992 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800993
Romain Guy73b979d2009-06-09 12:57:21 -0700994 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800995 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800997 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700998 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700999 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001000 } else {
1001 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 }
1003 }
Romain Guycbb89e42009-06-08 15:52:54 -07001004
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 /**
1006 * Add a shortcut to the workspace.
1007 *
1008 * @param data The intent describing the shortcut.
1009 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 */
Adam Cohenfbba09b2011-07-18 21:43:05 -07001011 private void completeAddShortcut(Intent data, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001012 final int[] cellXY = mTmpAddItemCellCoordinates;
1013 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001014
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001015 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001016 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001017
1018 // First we check if we already know the exact location where we want to add this item.
1019 if (cellX >= 0 && cellY >= 0) {
1020 cellXY[0] = cellX;
1021 cellXY[1] = cellY;
1022 foundCellSpan = true;
1023 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001024 // when dragging and dropping, just find the closest free spot
1025 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1026 int[] result = screenLayout.findNearestVacantArea(
1027 touchXY[0], touchXY[1], 1, 1, cellXY);
1028 foundCellSpan = (result != null);
1029 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001030 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001031 }
1032
1033 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001034 showOutOfSpaceMessage();
1035 return;
1036 }
1037
1038 final ShortcutInfo info = mModel.addShortcut(
1039 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001040
1041 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001042 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001043 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 }
1045 }
1046
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001047 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001048 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001050 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001051 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 */
Winson Chungf7640c82011-02-28 13:47:29 -08001053 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001054 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001055
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001056 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001057 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Adam Cohen09353862011-07-14 16:10:03 -07001058
1059 // We want to account for the extra amount of padding that we are adding to the widget
1060 // to ensure that it gets the full amount of space that it has requested
1061 Resources r = getResources();
1062 int requiredWidth = appWidgetInfo.minWidth +
1063 r.getDimensionPixelSize(R.dimen.app_widget_padding_left) +
1064 r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
1065 int requiredHeight = appWidgetInfo.minHeight +
1066 r.getDimensionPixelSize(R.dimen.app_widget_padding_top) +
1067 r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
1068 int[] spanXY = layout.rectToCell(requiredWidth, requiredHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001069
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001070 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001071 // We have saved the position to which the widget was dragged-- this really only matters
1072 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001073 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001074
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001075 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001076 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001077 if (mAddCellX >= 0 && mAddCellY >= 0) {
1078 cellXY[0] = mAddCellX;
1079 cellXY[1] = mAddCellY;
1080 foundCellSpan = true;
1081 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001082 // when dragging and dropping, just find the closest free spot
1083 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1084 int[] result = screenLayout.findNearestVacantArea(
1085 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001086 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001087 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001088 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001089 }
1090
Michael Jurka0280c3b2010-09-17 15:00:07 -07001091 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001092 if (appWidgetId != -1) {
1093 // Deleting an app widget ID is a void call but writes to disk before returning
1094 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001095 new Thread("deleteAppWidgetId") {
1096 public void run() {
1097 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1098 }
1099 }.start();
1100 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001101 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001102 return;
1103 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001104
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001105 // Build Launcher-specific widget info and save to database
1106 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001107 launcherInfo.spanX = spanXY[0];
1108 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 LauncherModel.addItemToDatabase(this, launcherInfo,
1111 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001112 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113
1114 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001116 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001117
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001118 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001120
Michael Jurka0280c3b2010-09-17 15:00:07 -07001121 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001122 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001123
1124 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001125 }
1126 }
Romain Guycbb89e42009-06-08 15:52:54 -07001127
Adam Cohended9f8d2010-11-03 13:25:16 -07001128 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1129 @Override
1130 public void onReceive(Context context, Intent intent) {
1131 final String action = intent.getAction();
1132 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1133 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001134 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001135 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001136
1137 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001138 if (mAppsCustomizeContent != null) {
1139 mAppsCustomizeContent.reset();
1140 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001141 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1142 mUserPresent = true;
1143 updateRunning();
1144 }
1145 }
1146 };
1147
1148 @Override
1149 public void onAttachedToWindow() {
1150 super.onAttachedToWindow();
1151
1152 // Listen for broadcasts related to user-presence
1153 final IntentFilter filter = new IntentFilter();
1154 filter.addAction(Intent.ACTION_SCREEN_OFF);
1155 filter.addAction(Intent.ACTION_USER_PRESENT);
1156 registerReceiver(mReceiver, filter);
1157
Adam Cohend113e0c2010-11-11 10:48:05 -08001158 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001159 mVisible = true;
1160 }
1161
1162 @Override
1163 public void onDetachedFromWindow() {
1164 super.onDetachedFromWindow();
1165 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001166 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001167
Adam Cohend113e0c2010-11-11 10:48:05 -08001168 if (mAttached) {
1169 unregisterReceiver(mReceiver);
1170 mAttached = false;
1171 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001172 updateRunning();
1173 }
1174
1175 public void onWindowVisibilityChanged(int visibility) {
1176 mVisible = visibility == View.VISIBLE;
1177 updateRunning();
1178 }
1179
1180 private void sendAdvanceMessage(long delay) {
1181 mHandler.removeMessages(ADVANCE_MSG);
1182 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1183 mHandler.sendMessageDelayed(msg, delay);
1184 mAutoAdvanceSentTime = System.currentTimeMillis();
1185 }
1186
1187 private void updateRunning() {
1188 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1189 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1190 mAutoAdvanceRunning = autoAdvanceRunning;
1191 if (autoAdvanceRunning) {
1192 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1193 sendAdvanceMessage(delay);
1194 } else {
1195 if (!mWidgetsToAdvance.isEmpty()) {
1196 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1197 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1198 }
1199 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001200 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001201 }
1202 }
1203 }
1204
1205 private final Handler mHandler = new Handler() {
1206 @Override
1207 public void handleMessage(Message msg) {
1208 if (msg.what == ADVANCE_MSG) {
1209 int i = 0;
1210 for (View key: mWidgetsToAdvance.keySet()) {
1211 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1212 final int delay = mAdvanceStagger * i;
1213 if (v instanceof Advanceable) {
1214 postDelayed(new Runnable() {
1215 public void run() {
1216 ((Advanceable) v).advance();
1217 }
1218 }, delay);
1219 }
1220 i++;
1221 }
1222 sendAdvanceMessage(mAdvanceInterval);
1223 }
1224 }
1225 };
1226
1227 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001228 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001229 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1230 if (v instanceof Advanceable) {
1231 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001232 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001233 updateRunning();
1234 }
1235 }
1236
1237 void removeWidgetToAutoAdvance(View hostView) {
1238 if (mWidgetsToAdvance.containsKey(hostView)) {
1239 mWidgetsToAdvance.remove(hostView);
1240 updateRunning();
1241 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001242 }
1243
Joe Onorato9c1289c2009-08-17 11:03:03 -04001244 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001245 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001246 launcherInfo.hostView = null;
1247 }
1248
Adam Cohended9f8d2010-11-03 13:25:16 -07001249 void showOutOfSpaceMessage() {
1250 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1251 }
1252
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001253 public LauncherAppWidgetHost getAppWidgetHost() {
1254 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255 }
Romain Guycbb89e42009-06-08 15:52:54 -07001256
Winson Chunga9abd0e2010-10-27 17:18:37 -07001257 public LauncherModel getModel() {
1258 return mModel;
1259 }
1260
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001261 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001262 getWindow().closeAllPanels();
1263
1264 try {
1265 dismissDialog(DIALOG_CREATE_SHORTCUT);
1266 // Unlock the workspace if the dialog was showing
1267 } catch (Exception e) {
1268 // An exception is thrown if the dialog is not visible, which is fine
1269 }
1270
1271 try {
1272 dismissDialog(DIALOG_RENAME_FOLDER);
1273 // Unlock the workspace if the dialog was showing
1274 } catch (Exception e) {
1275 // An exception is thrown if the dialog is not visible, which is fine
1276 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001277
1278 // Whatever we were doing is hereby canceled.
1279 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001280 }
1281
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 @Override
1283 protected void onNewIntent(Intent intent) {
1284 super.onNewIntent(intent);
1285
1286 // Close the menu
1287 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001288 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001289 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001290
Adam Cohen95bb8002011-07-03 23:40:28 -07001291 closeFolder();
1292
Joe Onorato14f122b2009-11-19 14:06:36 -08001293 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1294 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001295
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001296 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001297 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001298 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001299 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001300 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001301 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001302 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001303
Joe Onorato3a8820b2009-11-10 15:06:42 -08001304 final View v = getWindow().peekDecorView();
1305 if (v != null && v.getWindowToken() != null) {
1306 InputMethodManager imm = (InputMethodManager)getSystemService(
1307 INPUT_METHOD_SERVICE);
1308 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001310
Michael Jurka35aa14d2011-07-07 17:01:08 -07001311 // Reset AllApps to its initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001312 if (mAppsCustomizeContent != null) {
1313 mAppsCustomizeContent.reset();
1314 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001315 }
1316 }
1317
1318 @Override
1319 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1320 // Do not call super here
1321 mSavedInstanceState = savedInstanceState;
1322 }
1323
1324 @Override
1325 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001326 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001327 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001328
Michael Jurka883f55b2010-10-21 15:47:14 -07001329 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001330 // We close any open folder since it will not be re-opened, and we need to make sure
1331 // this state is reflected.
1332 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333
Michael Jurka0280c3b2010-09-17 15:00:07 -07001334 if (mAddScreen > -1 && mWaitingForResult) {
1335 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001336 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddCellX);
1337 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 }
1339
1340 if (mFolderInfo != null && mWaitingForResult) {
1341 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1342 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1343 }
Michael Jurka946ad472010-07-09 18:05:18 -07001344
Winson Chung785d2eb2011-04-14 16:08:02 -07001345 // Save the current AppsCustomize tab
1346 if (mAppsCustomizeTabHost != null) {
1347 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1348 if (currentTabTag != null) {
1349 outState.putString("apps_customize_currentTab", currentTabTag);
1350 }
1351 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 }
1353
1354 @Override
1355 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001357
Winson Chungcd2b0142011-06-08 16:02:26 -07001358 // Stop callbacks from LauncherModel
1359 LauncherApplication app = ((LauncherApplication) getApplication());
1360 mModel.stopLoader();
1361 app.setLauncher(null);
1362
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001363 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001364 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001366 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001368 mAppWidgetHost = null;
1369
1370 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371
1372 TextKeyListener.getInstance().release();
1373
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001374
Adam Cohen4eac29a2011-07-11 17:53:37 -07001375 unbindWorkspaceItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001376
1377 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001378 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001379
1380 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1381 mWorkspace.removeAllViews();
1382 mWorkspace = null;
1383 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001384
1385 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 }
1387
Adam Cohena9cf38f2011-05-02 15:36:58 -07001388 public DragController getDragController() {
1389 return mDragController;
1390 }
1391
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 @Override
1393 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001394 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 super.startActivityForResult(intent, requestCode);
1396 }
1397
1398 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001399 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001400 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001401
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001402 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001403
Karl Rosaen138a0412009-04-23 19:00:21 -07001404 if (initialQuery == null) {
1405 // Use any text typed in the launcher as the initial query
1406 initialQuery = getTypedText();
1407 clearTypedText();
1408 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 if (appSearchData == null) {
1410 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001411 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 }
Romain Guycbb89e42009-06-08 15:52:54 -07001413
Karl Rosaen138a0412009-04-23 19:00:21 -07001414 final SearchManager searchManager =
1415 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001416 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001417 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
1419
1420 @Override
1421 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001422 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001423 return false;
1424 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425
1426 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001427
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1429 .setIcon(android.R.drawable.ic_menu_add)
1430 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001431 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1432 .setIcon(android.R.drawable.ic_menu_manage)
1433 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001434 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 .setIcon(android.R.drawable.ic_menu_gallery)
1436 .setAlphabeticShortcut('W');
1437 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1438 .setIcon(android.R.drawable.ic_search_category_default)
1439 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1440 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1441 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1442 .setAlphabeticShortcut('N');
1443
1444 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001445 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1446 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001447
1448 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1449 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1450 .setIntent(settings);
1451
1452 return true;
1453 }
1454
1455 @Override
1456 public boolean onPrepareOptionsMenu(Menu menu) {
1457 super.onPrepareOptionsMenu(menu);
1458
Winson Chung785d2eb2011-04-14 16:08:02 -07001459 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1460 // related code?
1461 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462
1463 return true;
1464 }
1465
1466 @Override
1467 public boolean onOptionsItemSelected(MenuItem item) {
1468 switch (item.getItemId()) {
1469 case MENU_ADD:
1470 addItems();
1471 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001472 case MENU_MANAGE_APPS:
1473 manageApps();
1474 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 case MENU_WALLPAPER_SETTINGS:
1476 startWallpaper();
1477 return true;
1478 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001479 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 return true;
1481 case MENU_NOTIFICATIONS:
1482 showNotifications();
1483 return true;
1484 }
1485
1486 return super.onOptionsItemSelected(item);
1487 }
Romain Guycbb89e42009-06-08 15:52:54 -07001488
Karl Rosaen138a0412009-04-23 19:00:21 -07001489 /**
1490 * Indicates that we want global search for this activity by setting the globalSearch
1491 * argument for {@link #startSearch} to true.
1492 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001494 @Override
1495 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001496 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001497 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001498 }
1499
Joe Onorato9c1289c2009-08-17 11:03:03 -04001500 public boolean isWorkspaceLocked() {
1501 return mWorkspaceLoading || mWaitingForResult;
1502 }
1503
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001505 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001506 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
1508
Michael Jurka0280c3b2010-09-17 15:00:07 -07001509 private void resetAddInfo() {
1510 mAddScreen = -1;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001511 mAddCellX = -1;
1512 mAddCellY = -1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001513 mAddDropPosition = null;
1514 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001515
Winson Chung7ad01412010-09-27 11:33:03 -07001516 private void manageApps() {
1517 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1518 }
1519
Michael Jurkaaf442092010-06-10 17:01:57 -07001520 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001521 // TODO: catch bad widget exception when sent
1522 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001523 // TODO: Is this log message meaningful?
1524 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001525 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001526 }
1527
Winson Chung55cef262010-10-28 14:14:18 -07001528 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001529 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530
Bjorn Bringert7984c942009-12-09 15:38:25 +00001531 if (appWidget.configure != null) {
1532 // Launch over to configure widget, if needed
1533 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1534 intent.setComponent(appWidget.configure);
1535 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001536 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001537 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1538 intent.putExtra(
1539 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1540 info.mimeType);
1541
1542 final String mimeType = info.mimeType;
1543 final ClipData clipData = (ClipData) info.configurationData;
1544 final ClipDescription clipDesc = clipData.getDescription();
1545 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1546 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001547 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001548 final CharSequence stringData = item.getText();
1549 final Uri uriData = item.getUri();
1550 final Intent intentData = item.getIntent();
1551 final String key =
1552 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1553 if (uriData != null) {
1554 intent.putExtra(key, uriData);
1555 } else if (intentData != null) {
1556 intent.putExtra(key, intentData);
1557 } else if (stringData != null) {
1558 intent.putExtra(key, stringData);
1559 }
1560 break;
1561 }
1562 }
1563 }
Winson Chung55cef262010-10-28 14:14:18 -07001564 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001565
Romain Guy8e633c52010-03-04 12:51:36 -08001566 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001568 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001569 completeAddAppWidget(appWidgetId, mAddScreen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001570
1571 // Exit spring loaded mode if necessary after adding the widget
1572 exitSpringLoadedDragModeDelayed(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
1574 }
Romain Guycbb89e42009-06-08 15:52:54 -07001575
Adam Cohenfbba09b2011-07-18 21:43:05 -07001576 /**
1577 * Process a shortcut drop.
1578 *
1579 * @param componentName The name of the component
1580 * @param screen The screen where it should be added
1581 * @param cell The cell it should be added to, optional
1582 * @param position The location on the screen where it was dropped, optional
1583 */
1584 void processShortcutFromDrop(ComponentName componentName, int screen, int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001585 resetAddInfo();
1586 mAddScreen = screen;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001587 mAddDropPosition = loc;
1588
1589 if (cell != null) {
1590 mAddCellX = cell[0];
1591 mAddCellY = cell[1];
1592 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001593
1594 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1595 createShortcutIntent.setComponent(componentName);
1596 processShortcut(createShortcutIntent);
1597 }
1598
Adam Cohenfbba09b2011-07-18 21:43:05 -07001599 /**
1600 * Process a widget drop.
1601 *
1602 * @param info The PendingAppWidgetInfo of the widget being added.
1603 * @param screen The screen where it should be added
1604 * @param cell The cell it should be added to, optional
1605 * @param position The location on the screen where it was dropped, optional
1606 */
1607 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] cell, int[] loc) {
1608 resetAddInfo();
1609 mAddScreen = screen;
1610 mAddDropPosition = loc;
1611
1612 if (cell != null) {
1613 mAddCellX = cell[0];
1614 mAddCellY = cell[1];
1615 }
1616
1617 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1618 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1619 addAppWidgetImpl(appWidgetId, info);
1620 }
1621
Joe Onoratodeb98af2010-02-19 14:59:39 -08001622 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001623 // Handle case where user selected "Applications"
1624 String applicationName = getResources().getString(R.string.group_applications);
1625 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001626
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001627 if (applicationName != null && applicationName.equals(shortcutName)) {
1628 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1629 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001630
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001631 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1632 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001633 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001634 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001635 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001636 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 }
1639
Winson Chung24ab2f12010-09-16 14:10:47 -07001640 void processWallpaper(Intent intent) {
1641 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1642 }
1643
Adam Cohenfbba09b2011-07-18 21:43:05 -07001644 FolderIcon addFolder(final int screen, int cellX, int cellY) {
Adam Cohend0445262011-07-04 23:53:22 -07001645 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 folderInfo.title = getText(R.string.folder_name);
1647
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 // Update the model
Adam Cohend0445262011-07-04 23:53:22 -07001649 LauncherModel.addItemToDatabase(Launcher.this, folderInfo,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001650 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Adam Cohenfbba09b2011-07-18 21:43:05 -07001651 screen, cellX, cellY, false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001652 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653
1654 // Create the view
1655 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001656 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1657 folderInfo, mIconCache);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001658 mWorkspace.addInScreen(newFolder, screen, cellX, cellY, 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001659 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 }
Romain Guycbb89e42009-06-08 15:52:54 -07001661
Joe Onorato9c1289c2009-08-17 11:03:03 -04001662 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001663 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001664 }
1665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 private void showNotifications() {
1667 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1668 if (statusBar != null) {
1669 statusBar.expand();
1670 }
1671 }
1672
1673 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001674 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001675 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001676 Intent chooser = Intent.createChooser(pickWallpaper,
1677 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001678 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1679 // Removed in Eclair MR1
1680// WallpaperManager wm = (WallpaperManager)
1681// getSystemService(Context.WALLPAPER_SERVICE);
1682// WallpaperInfo wi = wm.getWallpaperInfo();
1683// if (wi != null && wi.getSettingsActivity() != null) {
1684// LabeledIntent li = new LabeledIntent(getPackageName(),
1685// R.string.configure_wallpaper, 0);
1686// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1687// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1688// }
Mike Clerona0618e42009-10-22 13:55:21 -07001689 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 }
1691
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001692 /**
1693 * Registers various content observers. The current implementation registers
1694 * only a favorites observer to keep track of the favorites applications.
1695 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001696 private void registerContentObservers() {
1697 ContentResolver resolver = getContentResolver();
1698 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1699 true, mWidgetObserver);
1700 }
1701
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 @Override
1703 public boolean dispatchKeyEvent(KeyEvent event) {
1704 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1705 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001706 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001707 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001708 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001709 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001710 dumpState();
1711 return true;
1712 }
1713 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001714 }
1715 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1716 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001717 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 return true;
1719 }
1720 }
1721
1722 return super.dispatchKeyEvent(event);
1723 }
1724
Joe Onorato88ec0992009-11-19 13:16:06 -08001725 @Override
1726 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001727 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001728 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001729 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001730 Folder openFolder = mWorkspace.getOpenFolder();
1731 if (openFolder.isEditingName()) {
1732 openFolder.dismissEditingName();
1733 } else {
1734 closeFolder();
1735 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001736 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001737 mWorkspace.exitWidgetResizeMode();
1738
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001739 // Back button is a no-op here, but give at least some feedback for the button press
1740 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001741 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001742 }
1743
Adam Cohen2801caf2011-05-13 20:57:39 -07001744 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 Folder folder = mWorkspace.getOpenFolder();
1746 if (folder != null) {
1747 closeFolder(folder);
1748 }
1749 }
1750
1751 void closeFolder(Folder folder) {
1752 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001753
Michael Jurka8c920dd2011-01-20 14:16:56 -08001754 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 if (parent != null) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001756 CellLayout cl = (CellLayout) mWorkspace.getChildAt(folder.mInfo.screen);
Adam Cohen2801caf2011-05-13 20:57:39 -07001757 FolderIcon fi = (FolderIcon) cl.getChildAt(folder.mInfo.cellX, folder.mInfo.cellY);
1758 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001759 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001761 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 }
1763
1764 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001765 * Re-listen when widgets are reset.
1766 */
1767 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001768 if (mAppWidgetHost != null) {
1769 mAppWidgetHost.startListening();
1770 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001771 }
1772
1773 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001774 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1775 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 */
Adam Cohen4eac29a2011-07-11 17:53:37 -07001777 private void unbindWorkspaceItems() {
1778 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001780
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 /**
1782 * Launches the intent referred by the clicked shortcut.
1783 *
1784 * @param v The view representing the clicked shortcut.
1785 */
1786 public void onClick(View v) {
1787 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001788 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001790 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001791 int[] pos = new int[2];
1792 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001793 intent.setSourceBounds(new Rect(pos[0], pos[1],
1794 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001795 boolean success = startActivitySafely(intent, tag);
1796
1797 if (success && v instanceof BubbleTextView) {
1798 mWaitingForResume = (BubbleTextView) v;
1799 mWaitingForResume.setStayPressed(true);
1800 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001802 if (v instanceof FolderIcon) {
1803 FolderIcon fi = (FolderIcon) v;
1804 handleFolderClick(fi);
1805 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001806 } else if (v == mAllAppsButton) {
1807 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001808 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001809 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001810 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 }
1813 }
1814
Michael Jurka0e260592010-06-30 17:07:39 -07001815 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001816 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001817 // clicking anywhere on the workspace causes the customization drawer to slide down
1818 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001819 return false;
1820 }
1821
Michael Jurkaaf442092010-06-10 17:01:57 -07001822 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001823 * Event handler for the search button
1824 *
1825 * @param v The view that was clicked.
1826 */
1827 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001828 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001829 // Use a custom animation for launching search
1830 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001831 }
1832
1833 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001834 * Event handler for the voice button
1835 *
1836 * @param v The view that was clicked.
1837 */
1838 public void onClickVoiceButton(View v) {
1839 startVoiceSearch();
1840 }
1841
1842 private void startVoiceSearch() {
1843 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1844 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1845 startActivity(intent);
1846 }
1847
1848 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001849 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001850 * enters home screen customization mode.
1851 *
1852 * @param v The view that was clicked.
1853 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001854 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001855 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001856 }
1857
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001858 /**
1859 * Event handler for the "grid" button that appears on the home screen, which
1860 * enters all apps mode.
1861 *
1862 * @param v The view that was clicked.
1863 */
1864 public void onClickAllAppsButton(View v) {
1865 showAllApps(true);
1866 }
1867
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001868 public void onClickAppMarketButton(View v) {
1869 if (mAppMarketIntent != null) {
1870 startActivitySafely(mAppMarketIntent, "app market");
1871 }
1872 }
1873
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001874 void startApplicationDetailsActivity(ComponentName componentName) {
1875 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001876 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1877 Uri.fromParts("package", packageName, null));
1878 startActivity(intent);
1879 }
1880
Patrick Dubroy5539af72010-09-07 15:22:01 -07001881 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1882 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1883 // System applications cannot be installed. For now, show a toast explaining that.
1884 // We may give them the option of disabling apps this way.
1885 int messageId = R.string.uninstall_system_app_text;
1886 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1887 } else {
1888 String packageName = appInfo.componentName.getPackageName();
1889 String className = appInfo.componentName.getClassName();
1890 Intent intent = new Intent(
1891 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1892 startActivity(intent);
1893 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001894 }
1895
Michael Jurkaddd62e92011-02-16 17:49:14 -08001896 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1898 try {
1899 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001900 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 } catch (ActivityNotFoundException e) {
1902 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001903 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 } catch (SecurityException e) {
1905 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001906 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001908 "or use the exported attribute for this activity. "
1909 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001911 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001913
Romain Guy8e633c52010-03-04 12:51:36 -08001914 void startActivityForResultSafely(Intent intent, int requestCode) {
1915 try {
1916 startActivityForResult(intent, requestCode);
1917 } catch (ActivityNotFoundException e) {
1918 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1919 } catch (SecurityException e) {
1920 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1921 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1922 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1923 "or use the exported attribute for this activity.", e);
1924 }
1925 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926
Adam Cohena9cf38f2011-05-02 15:36:58 -07001927 private void handleFolderClick(FolderIcon folderIcon) {
1928 final FolderInfo info = folderIcon.mInfo;
1929 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 // Close any open folder
1931 closeFolder();
1932 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001933 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 } else {
1935 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001936 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 int folderScreen;
1938 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001939 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 // .. and close it
1941 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001942 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 // Close any folder open on the current screen
1944 closeFolder();
1945 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001946 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 }
1948 }
1949 }
1950 }
1951
Adam Cohen2801caf2011-05-13 20:57:39 -07001952 private void growAndFadeOutFolderIcon(FolderIcon fi) {
1953 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1954 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1955 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1956
1957 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1958 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1959 oa.start();
1960 }
1961
1962 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
1963 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1964 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1965 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1966
1967 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1968 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1969 oa.start();
1970 }
1971
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001973 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 * is animated relative to the specified View. If the View is null, no animation
1975 * is played.
1976 *
1977 * @param folderInfo The FolderInfo describing the folder to open.
1978 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001979 public void openFolder(FolderIcon folderIcon) {
1980 Folder folder = folderIcon.mFolder;
1981 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982
Adam Cohen2801caf2011-05-13 20:57:39 -07001983 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001984 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985
Adam Cohen8e776a62011-06-28 18:10:06 -07001986 mDragLayer.addView(folder);
1987 mDragController.addDropTarget((DropTarget) folder);
1988
Adam Cohena9cf38f2011-05-02 15:36:58 -07001989 folder.animateOpen();
1990 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991 }
1992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001994 if (mState != State.WORKSPACE) {
1995 return false;
1996 }
1997
Romain Guy1fbc1c82009-11-09 20:43:08 -08001998 switch (v.getId()) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001999 case R.id.all_apps_button:
Winson Chungf0ea4d32011-06-06 14:27:16 -07002000 if (mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002001 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2002 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2003 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002004 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002005 return true;
2006 }
2007
Joe Onorato9c1289c2009-08-17 11:03:03 -04002008 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 return false;
2010 }
2011
2012 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002013 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 }
2015
Michael Jurka0280c3b2010-09-17 15:00:07 -07002016 resetAddInfo();
2017 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002018 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002019 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 return true;
2021 }
2022
Michael Jurka0280c3b2010-09-17 15:00:07 -07002023 final View itemUnderLongClick = longClickCellInfo.cell;
2024
Winson Chung304dcde2011-01-07 11:17:23 -08002025 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002026 if (itemUnderLongClick == null) {
2027 // User long pressed on empty space
2028 mWorkspace.setAllowLongPress(false);
2029 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2030 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07002031 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002033 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002035 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2036 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002037 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 }
2039 }
2040 }
2041 return true;
2042 }
2043
Romain Guye6b8e2f2009-11-10 11:56:55 -08002044 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002045 private void dismissPreview(final View v) {
2046 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002047 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002048 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2049 public void onDismiss() {
2050 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2051 int count = group.getChildCount();
2052 for (int i = 0; i < count; i++) {
2053 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2054 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002055 ArrayList<Bitmap> bitmaps =
2056 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002057 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2058
2059 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002060 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002061 window.setOnDismissListener(null);
2062 }
2063 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002064 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002065 }
2066 v.setTag(null);
2067 }
2068
Romain Guyf8e6a802009-12-07 17:48:02 -08002069 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002070 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002071 }
2072
Romain Guya6abce82009-11-10 02:54:41 -08002073 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002074 final Resources resources = getResources();
2075 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002076
Romain Guya6abce82009-11-10 02:54:41 -08002077 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002078
Romain Guy9d31e9f2009-11-11 18:54:28 -08002079 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002080
Romain Guyf8e6a802009-12-07 17:48:02 -08002081 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002082 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002083 int extraW = (int) ((r.left + r.right) * max);
2084 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002085
2086 int aW = cell.getWidth() - extraW;
2087 float w = aW / max;
2088
2089 int width = cell.getWidth();
2090 int height = cell.getHeight();
Winson Chung4b825dcd2011-06-19 12:41:22 -07002091 int x = cell.getPaddingLeft();
2092 int y = cell.getPaddingTop();
2093 width -= (x + cell.getPaddingRight());
2094 height -= (y + cell.getPaddingBottom());
Romain Guya6abce82009-11-10 02:54:41 -08002095
2096 float scale = w / width;
2097
2098 int count = end - start;
2099
2100 final float sWidth = width * scale;
2101 float sHeight = height * scale;
2102
Romain Guye6b8e2f2009-11-10 11:56:55 -08002103 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002104
Romain Guye6b8e2f2009-11-10 11:56:55 -08002105 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2106 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002107
2108 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002109 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002110 cell = (CellLayout) workspace.getChildAt(i);
2111
Romain Guyf8e6a802009-12-07 17:48:02 -08002112 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002113 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002114
2115 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002116 c.scale(scale, scale);
Winson Chung4b825dcd2011-06-19 12:41:22 -07002117 c.translate(-cell.getPaddingLeft(), -cell.getPaddingTop());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002118 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002119
Romain Guy9d31e9f2009-11-11 18:54:28 -08002120 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002121 image.setImageBitmap(bitmap);
2122 image.setTag(i);
2123 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002124 image.setOnFocusChangeListener(handler);
2125 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002126 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002127
2128 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002129 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2130
Winson Chungaafa03c2010-06-11 17:34:16 -07002131 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002132 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002133
2134 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002135 p.setContentView(preview);
2136 p.setWidth((int) (sWidth * count + extraW));
2137 p.setHeight((int) (sHeight + extraH));
2138 p.setAnimationStyle(R.style.AnimationPreview);
2139 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002140 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002141 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002142 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002143
Romain Guye6b8e2f2009-11-10 11:56:55 -08002144 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2145 public void onDismiss() {
2146 dismissPreview(anchor);
2147 }
2148 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002149
2150 anchor.setTag(p);
2151 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002152 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002153 }
2154
Romain Guy9d31e9f2009-11-11 18:54:28 -08002155 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002156 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002157
Romain Guye6b8e2f2009-11-10 11:56:55 -08002158 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002159 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002160 }
2161
2162 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002163 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002164 v.post(this);
2165 }
2166
2167 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002168 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002169 }
2170
2171 public void onFocusChange(View v, boolean hasFocus) {
2172 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002173 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002174 }
Romain Guya6abce82009-11-10 02:54:41 -08002175 }
2176 }
2177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 Workspace getWorkspace() {
2179 return mWorkspace;
2180 }
2181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182 @Override
2183 protected Dialog onCreateDialog(int id) {
2184 switch (id) {
2185 case DIALOG_CREATE_SHORTCUT:
2186 return new CreateShortcut().createDialog();
2187 case DIALOG_RENAME_FOLDER:
2188 return new RenameFolder().createDialog();
2189 }
2190
2191 return super.onCreateDialog(id);
2192 }
2193
2194 @Override
2195 protected void onPrepareDialog(int id, Dialog dialog) {
2196 switch (id) {
2197 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002198 break;
2199 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002200 if (mFolderInfo != null) {
2201 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2202 final CharSequence text = mFolderInfo.title;
2203 input.setText(text);
2204 input.setSelection(0, text.length());
2205 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 break;
2207 }
2208 }
2209
2210 void showRenameDialog(FolderInfo info) {
2211 mFolderInfo = info;
2212 mWaitingForResult = true;
2213 showDialog(DIALOG_RENAME_FOLDER);
2214 }
2215
Adam Cohenfbba09b2011-07-18 21:43:05 -07002216 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002217 resetAddInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -07002218 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002219 mWaitingForResult = true;
2220 showDialog(DIALOG_CREATE_SHORTCUT);
2221 }
2222
Joe Onoratodeb98af2010-02-19 14:59:39 -08002223 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002224 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002225 Bundle bundle = new Bundle();
2226
2227 ArrayList<String> shortcutNames = new ArrayList<String>();
2228 shortcutNames.add(getString(R.string.group_applications));
2229 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2230
2231 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2232 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2233 R.drawable.ic_launcher_application));
2234 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2235
2236 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2237 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002238 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002239 pickIntent.putExtras(bundle);
2240
Joe Onoratodeb98af2010-02-19 14:59:39 -08002241 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002242 }
2243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 private class RenameFolder {
2245 private EditText mInput;
2246
2247 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2249 mInput = (EditText) layout.findViewById(R.id.folder_name);
2250
2251 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2252 builder.setIcon(0);
2253 builder.setTitle(getString(R.string.rename_folder_title));
2254 builder.setCancelable(true);
2255 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2256 public void onCancel(DialogInterface dialog) {
2257 cleanup();
2258 }
2259 });
2260 builder.setNegativeButton(getString(R.string.cancel_action),
2261 new Dialog.OnClickListener() {
2262 public void onClick(DialogInterface dialog, int which) {
2263 cleanup();
2264 }
2265 }
2266 );
2267 builder.setPositiveButton(getString(R.string.rename_action),
2268 new Dialog.OnClickListener() {
2269 public void onClick(DialogInterface dialog, int which) {
2270 changeFolderName();
2271 }
2272 }
2273 );
2274 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002275
2276 final AlertDialog dialog = builder.create();
2277 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2278 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002279 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002280 mInput.requestFocus();
2281 InputMethodManager inputManager = (InputMethodManager)
2282 getSystemService(Context.INPUT_METHOD_SERVICE);
2283 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002284 }
2285 });
2286
2287 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002288 }
2289
2290 private void changeFolderName() {
2291 final String name = mInput.getText().toString();
2292 if (!TextUtils.isEmpty(name)) {
2293 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002294 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002295 mFolderInfo.title = name;
2296 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2297
Joe Onorato9c1289c2009-08-17 11:03:03 -04002298 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002299 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002300 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 } else {
2302 final FolderIcon folderIcon = (FolderIcon)
2303 mWorkspace.getViewForTag(mFolderInfo);
2304 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002305 // TODO: At some point we'll probably want some version of setting
2306 // the text for a folder icon.
2307 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 getWorkspace().requestLayout();
2309 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002310 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002311 mWorkspaceLoading = true;
2312 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002313 }
2314 }
2315 }
2316 cleanup();
2317 }
2318
2319 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002320 dismissDialog(DIALOG_RENAME_FOLDER);
2321 mWaitingForResult = false;
2322 mFolderInfo = null;
2323 }
2324 }
2325
Daniel Sandler843e8602010-06-07 14:59:01 -04002326 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2327 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002328 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002329 }
2330
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002331 // AllAppsView.Watcher
2332 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002333 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002334 mWorkspace.setVisibility(View.GONE);
2335 }
2336 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002337
2338 /**
2339 * Helper method for the cameraZoomIn/cameraZoomOut animations
2340 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002341 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002342 * @param scaleFactor The scale factor used for the zoom
2343 */
2344 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2345 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002346
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002347 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002348 // Set pivotY so that at the starting zoom factor, the view is partially
2349 // visible. Modifying initialHeightFactor changes how much of the view is
2350 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002351 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002352 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002353 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002354 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002355 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002356 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002357 }
2358 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002359
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002360 /**
2361 * Zoom the camera out from the workspace to reveal 'toView'.
2362 * Assumes that the view to show is anchored at either the very top or very bottom
2363 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002364 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002365 */
Winson Chungc07918d2011-07-01 15:35:26 -07002366 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002367 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002368
Winson Chungf0ea4d32011-06-06 14:27:16 -07002369 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2370 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2371 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2372 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002373
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002374 setPivotsForZoom(toView, toState, scale);
2375
Michael Jurkad74c9842011-07-10 12:44:21 -07002376 // Shrink workspaces away if going to AppsCustomize from workspace
2377 mWorkspace.shrink(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002378
2379 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002380 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002381 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002382 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2383 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002384 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002385 toView.setFastScaleX(a * scale + b * 1f);
2386 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002387 }
2388 });
Adam Cohen61033d32010-11-15 18:29:44 -08002389
Winson Chungf0ea4d32011-06-06 14:27:16 -07002390 toView.setVisibility(View.VISIBLE);
2391 toView.setFastAlpha(0f);
2392 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2393 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2394 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2395 public void onAnimationUpdate(float a, float b) {
2396 // don't need to invalidate because we do so above
2397 toView.setFastAlpha(a * 0f + b * 1f);
2398 }
2399 });
2400 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002401
Michael Jurkaabded662011-03-04 12:06:57 -08002402 if (toView instanceof LauncherTransitionable) {
2403 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002404 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002405 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002406 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002407 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002408 // Prepare the position
2409 toView.setTranslationX(0.0f);
2410 toView.setTranslationY(0.0f);
2411 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002412 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002413
Winson Chungc07918d2011-07-01 15:35:26 -07002414 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2415 // Hide the workspace scrollbar
2416 mWorkspace.hideScrollingIndicator(true);
2417 mWorkspace.hideScrollIndicatorTrack();
2418 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002419 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002420 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002421 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002422 // If we don't set the final scale values here, if this animation is cancelled
2423 // it will have the wrong scale value and subsequent cameraPan animations will
2424 // not fix that
2425 toView.setScaleX(1.0f);
2426 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002427 if (toView instanceof LauncherTransitionable) {
2428 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002429 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002430 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002431 });
2432
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002433 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002434 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002435
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002436 if (mStateAnimation != null) mStateAnimation.cancel();
2437 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002438 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002439 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002440 } else {
2441 toView.setTranslationX(0.0f);
2442 toView.setTranslationY(0.0f);
2443 toView.setScaleX(1.0f);
2444 toView.setScaleY(1.0f);
2445 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002446 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002447 if (toView instanceof LauncherTransitionable) {
2448 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2449 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002450
Winson Chungc07918d2011-07-01 15:35:26 -07002451 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2452 // Hide the workspace scrollbar
2453 mWorkspace.hideScrollingIndicator(true);
2454 mWorkspace.hideScrollIndicatorTrack();
2455 }
Michael Jurkaabded662011-03-04 12:06:57 -08002456 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002457 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002458 }
2459
2460 /**
2461 * Zoom the camera back into the workspace, hiding 'fromView'.
2462 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002463 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002464 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002465 */
Winson Chungc07918d2011-07-01 15:35:26 -07002466 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002467 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002468
Winson Chungf0ea4d32011-06-06 14:27:16 -07002469 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2470 final float scaleFactor = (float)
2471 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2472 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002473
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002474 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002475
Michael Jurkad3ef3062010-11-23 16:23:58 -08002476 if (!springLoaded) {
2477 mWorkspace.unshrink(animated);
2478 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002479 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002480 if (mStateAnimation != null) mStateAnimation.cancel();
2481 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002482
Michael Jurka742574b2011-02-02 23:51:01 -08002483 final float oldScaleX = fromView.getScaleX();
2484 final float oldScaleY = fromView.getScaleY();
2485
2486 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2487 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002488 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2489 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002490 ((View)fromView.getParent()).fastInvalidate();
2491 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2492 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2493 }
2494 });
Michael Jurkaabded662011-03-04 12:06:57 -08002495 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002496 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002497 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002498 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2499 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002500 // don't need to invalidate because we do so above
2501 fromView.setFastAlpha(a * 1f + b * 0f);
2502 }
2503 });
Michael Jurkaabded662011-03-04 12:06:57 -08002504 if (fromView instanceof LauncherTransitionable) {
2505 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002506 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002507 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002508 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002509 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002510 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002511 if (fromView instanceof LauncherTransitionable) {
2512 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Winson Chung3ac74c52011-06-30 17:39:37 -07002513
Winson Chungc07918d2011-07-01 15:35:26 -07002514 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2515 // Show the workspace scrollbar
2516 mWorkspace.showScrollIndicatorTrack();
2517 mWorkspace.flashScrollingIndicator();
2518 }
Michael Jurka2763be32011-02-24 11:19:57 -08002519 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002520 }
2521 });
2522
Winson Chungf0ea4d32011-06-06 14:27:16 -07002523 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002524 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002525 } else {
2526 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002527 if (fromView instanceof LauncherTransitionable) {
2528 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2529 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002530
Winson Chungc07918d2011-07-01 15:35:26 -07002531 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2532 // Show the workspace scrollbar
2533 mWorkspace.showScrollIndicatorTrack();
2534 mWorkspace.flashScrollingIndicator();
2535 }
Michael Jurkaabded662011-03-04 12:06:57 -08002536 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002537 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002538 }
2539
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002540 void showWorkspace(boolean animated) {
2541 showWorkspace(animated, null);
2542 }
2543
2544 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002545 if (layout != null) {
2546 // always animated, but that's ok since we never specify a layout and
2547 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002548 mWorkspace.unshrink(layout);
2549 } else {
2550 mWorkspace.unshrink(animated);
2551 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002552 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002553 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002554 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002555
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002556 // Change the state *after* we've called all the transition code
2557 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002558
Winson Chung5fb63472011-02-02 17:03:37 -08002559 // Resume the auto-advance of widgets
2560 mUserPresent = true;
2561 updateRunning();
2562
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002563 // send an accessibility event to announce the context change
2564 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002565 }
2566
Michael Jurkad3ef3062010-11-23 16:23:58 -08002567 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002568 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002569 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002570 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002571 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002572 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002573 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002574 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002575 void exitSpringLoadedDragModeDelayed(boolean extendedDelay) {
2576 mWorkspace.postDelayed(new Runnable() {
2577 @Override
2578 public void run() {
2579 exitSpringLoadedDragMode();
2580 }
2581 }, (extendedDelay ?
2582 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2583 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2584 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002585 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002586 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002587 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002588 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2589 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002590 }
2591 // Otherwise, we are not in spring loaded mode, so don't do anything.
2592 }
2593
2594 /**
2595 * Shows the dock/hotseat area.
2596 */
2597 void showDock(boolean animated) {
2598 if (!LauncherApplication.isScreenLarge()) {
2599 if (animated) {
2600 int duration = mSearchDeleteBar.getTransitionInDuration();
2601 mButtonCluster.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002602 } else {
2603 mButtonCluster.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002604 }
2605 }
2606 }
2607
2608 /**
2609 * Hides the dock/hotseat area.
2610 */
2611 void hideDock(boolean animated) {
2612 if (!LauncherApplication.isScreenLarge()) {
2613 if (animated) {
2614 int duration = mSearchDeleteBar.getTransitionOutDuration();
2615 mButtonCluster.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002616 } else {
2617 mButtonCluster.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002618 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002619 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002620 }
2621
Winson Chung785d2eb2011-04-14 16:08:02 -07002622 void showAllApps(boolean animated) {
2623 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002624
Winson Chungf0ea4d32011-06-06 14:27:16 -07002625 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2626 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002627
Winson Chungf0ea4d32011-06-06 14:27:16 -07002628 // Hide the search bar and dock
2629 mSearchDeleteBar.hideSearchBar(animated);
2630 hideDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002631
Winson Chungf0ea4d32011-06-06 14:27:16 -07002632 // Change the state *after* we've called all the transition code
2633 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002634
2635 // Pause the auto-advance of widgets until we are out of AllApps
2636 mUserPresent = false;
2637 updateRunning();
2638
2639 // Send an accessibility event to announce the context change
2640 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2641 }
2642
Joe Onoratob2061212009-11-24 16:13:54 -05002643 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002644 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002645 * - Home from workspace
2646 * - from center screen
2647 * - from other screens
2648 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002649 * - from center screen
2650 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002651 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002652 * - from center screen
2653 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002654 * - Launch app from workspace and quit
2655 * - with back
2656 * - with home
2657 * - Launch app from all apps and quit
2658 * - with back
2659 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002660 * - Go to a screen that's not the default, then all
2661 * apps, and launch and app, and go back
2662 * - with back
2663 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002664 * - On workspace, long press power and go back
2665 * - with back
2666 * - with home
2667 * - On all apps, long press power and go back
2668 * - with back
2669 * - with home
2670 * - On workspace, power off
2671 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002672 * - Launch an app and turn off the screen while in that app
2673 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002674 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002675 * - From all apps
2676 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002677 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2678 * - From all apps
2679 * - From the center workspace
2680 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002681 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002682 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002683 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2684 mWorkspace.setVisibility(View.VISIBLE);
2685 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002686
Winson Chungf0ea4d32011-06-06 14:27:16 -07002687 // Show the search bar and dock
2688 mSearchDeleteBar.showSearchBar(animated);
2689 showDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002690
Winson Chungf0ea4d32011-06-06 14:27:16 -07002691 // Set focus to the AppsCustomize button
2692 findViewById(R.id.all_apps_button).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002693 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002694 }
2695
Joe Onorato7c312c12009-08-13 21:36:53 -07002696 void lockAllApps() {
2697 // TODO
2698 }
2699
2700 void unlockAllApps() {
2701 // TODO
2702 }
2703
Patrick Dubroy5f445422011-02-18 14:35:21 -08002704 /**
2705 * Add an item from all apps or customize onto the given workspace screen.
2706 * If layout is null, add to the current screen.
2707 */
2708 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002709 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2710 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002711 } else {
2712 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002713 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002714 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002715
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002716 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002717 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002718 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002719 // Look for the toolbar icon specified in the activity meta-data
2720 Bundle metaData = packageManager.getActivityInfo(
2721 activityName, PackageManager.GET_META_DATA).metaData;
2722 if (metaData != null) {
2723 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2724 if (iconResId != 0) {
2725 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002726 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002727 }
2728 }
2729 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002730 // This can happen if the activity defines an invalid drawable
2731 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2732 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002733 } catch (Resources.NotFoundException nfe) {
2734 // This can happen if the activity defines an invalid drawable
2735 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2736 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002737 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002738 return null;
2739 }
2740
2741 // if successful in getting icon, return it; otherwise, set button to use default drawable
2742 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2743 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2744 TextView button = (TextView) findViewById(buttonId);
2745 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2746
2747 // If we were unable to find the icon via the meta-data, use a generic one
2748 if (toolbarIcon == null) {
2749 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2750 return null;
2751 } else {
2752 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2753 return toolbarIcon.getConstantState();
2754 }
2755 }
2756
2757 // if successful in getting icon, return it; otherwise, set button to use default drawable
2758 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2759 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2760 ImageView button = (ImageView) findViewById(buttonId);
2761 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2762
Michael Jurka0423dcf2010-10-05 14:56:18 -07002763 // If we were unable to find the icon via the meta-data, use a generic one
2764 if (toolbarIcon == null) {
2765 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002766 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002767 } else {
2768 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002769 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002770 }
2771 }
2772
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002773 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2774 TextView button = (TextView) findViewById(buttonId);
2775 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2776 }
2777
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002778 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002779 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002780 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002781 }
2782
Michael Jurka0423dcf2010-10-05 14:56:18 -07002783 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002784 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2785 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002786
Winson Chung1cad91e2011-05-25 17:41:01 -07002787 final SearchManager searchManager =
2788 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2789 ComponentName activityName = searchManager.getGlobalSearchActivity();
2790 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002791 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002792 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002793 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002794 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002795 } else {
2796 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002797 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002798 }
2799 }
2800
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002801 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002802 updateButtonWithDrawable(R.id.search_button, d);
2803 }
2804
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002805 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002806 final View searchDivider = findViewById(R.id.search_divider);
2807 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002808
Winson Chung1cad91e2011-05-25 17:41:01 -07002809 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2810 ComponentName activityName = intent.resolveActivity(getPackageManager());
2811 if (activityName != null) {
2812 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002813 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2814 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002815 voiceButton.setVisibility(View.VISIBLE);
2816 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002817 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002818 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002819 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002820 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002821
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002822 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002823 updateButtonWithDrawable(R.id.voice_button, d);
2824 }
2825
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002826 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002827 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002828 */
2829 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002830 final View marketButton = findViewById(R.id.market_button);
2831 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2832 // Find the app market activity by resolving an intent.
2833 // (If multiple app markets are installed, it will return the ResolverActivity.)
2834 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002835 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002836 mAppMarketIntent = intent;
2837 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002838 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002839 marketButton.setVisibility(View.VISIBLE);
Winson Chungeb66b142011-06-16 13:14:22 -07002840
2841 // Remove the shop icon text in the Phone UI
2842 if (!LauncherApplication.isScreenLarge()) {
2843 ((TextView) marketButton).setText("");
2844 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002845 } else {
2846 // We should hide and disable the view so that we don't try and restore the visibility
2847 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2848 marketButton.setVisibility(View.GONE);
2849 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002850 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002851 }
2852
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002853 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002854 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002855 }
2856
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002857 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002858 * Displays the shortcut creation dialog and launches, if necessary, the
2859 * appropriate activity.
2860 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002861 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002862 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2863 DialogInterface.OnShowListener {
2864
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002865 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002866
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002867 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002868 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002869
Winson Chung55b65502011-05-26 12:03:43 -07002870 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2871 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002872 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002873 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002874
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002875 AlertDialog dialog = builder.create();
2876 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002877 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002878 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002879
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002880 return dialog;
2881 }
2882
2883 public void onCancel(DialogInterface dialog) {
2884 mWaitingForResult = false;
2885 cleanup();
2886 }
2887
Romain Guycbb89e42009-06-08 15:52:54 -07002888 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002889 mWaitingForResult = false;
2890 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002891 }
2892
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002893 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002894 try {
2895 dismissDialog(DIALOG_CREATE_SHORTCUT);
2896 } catch (Exception e) {
2897 // An exception is thrown if the dialog is not visible, which is fine
2898 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002899 }
2900
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002901 /**
2902 * Handle the action clicked in the "Add to home" dialog.
2903 */
2904 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002905 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002906
Winson Chung55b65502011-05-26 12:03:43 -07002907 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2908 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002909 case AddAdapter.ITEM_SHORTCUT: {
Winson Chungd2945262011-06-24 15:22:14 -07002910 if (mAppsCustomizeTabHost != null) {
2911 mAppsCustomizeTabHost.selectWidgetsTab();
2912 }
2913 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002914 break;
2915 }
Winson Chung55b65502011-05-26 12:03:43 -07002916 case AddAdapter.ITEM_APPLICATION: {
2917 if (mAppsCustomizeTabHost != null) {
2918 mAppsCustomizeTabHost.selectAppsTab();
2919 }
2920 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002921 break;
2922 }
Winson Chung55b65502011-05-26 12:03:43 -07002923 case AddAdapter.ITEM_APPWIDGET: {
2924 if (mAppsCustomizeTabHost != null) {
2925 mAppsCustomizeTabHost.selectWidgetsTab();
2926 }
2927 showAllApps(true);
2928 break;
2929 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002930 case AddAdapter.ITEM_WALLPAPER: {
2931 startWallpaper();
2932 break;
2933 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002934 }
2935 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002936
2937 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002938 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002940 }
2941
2942 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002943 * Receives notifications when applications are added/removed.
2944 */
2945 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2946 @Override
2947 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002948 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002949 String reason = intent.getStringExtra("reason");
2950 if (!"homekey".equals(reason)) {
2951 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002952 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002953 animate = false;
2954 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002955 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002956 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002957 }
2958 }
2959
2960 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002961 * Receives notifications whenever the appwidgets are reset.
2962 */
2963 private class AppWidgetResetObserver extends ContentObserver {
2964 public AppWidgetResetObserver() {
2965 super(new Handler());
2966 }
2967
2968 @Override
2969 public void onChange(boolean selfChange) {
2970 onAppWidgetReset();
2971 }
2972 }
2973
2974 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002975 * If the activity is currently paused, signal that we need to re-run the loader
2976 * in onResume.
2977 *
2978 * This needs to be called from incoming places where resources might have been loaded
2979 * while we are paused. That is becaues the Configuration might be wrong
2980 * when we're not running, and if it comes back to what it was when we
2981 * were paused, we are not restarted.
2982 *
2983 * Implementation of the method from LauncherModel.Callbacks.
2984 *
2985 * @return true if we are currently paused. The caller might be able to
2986 * skip some work in that case since we will come back again.
2987 */
2988 public boolean setLoadOnResume() {
2989 if (mPaused) {
2990 Log.i(TAG, "setLoadOnResume");
2991 mOnResumeNeedsLoad = true;
2992 return true;
2993 } else {
2994 return false;
2995 }
2996 }
2997
2998 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002999 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003000 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003001 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003002 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003003 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003004 } else {
3005 return SCREEN_COUNT / 2;
3006 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003007 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003008
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003009
Joe Onorato9c1289c2009-08-17 11:03:03 -04003010 /**
3011 * Refreshes the shortcuts shown on the workspace.
3012 *
3013 * Implementation of the method from LauncherModel.Callbacks.
3014 */
3015 public void startBinding() {
3016 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003017
3018 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003019 int count = workspace.getChildCount();
3020 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003021 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003022 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3023 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003024 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003025
Joe Onorato9c1289c2009-08-17 11:03:03 -04003026 if (DEBUG_USER_INTERFACE) {
3027 android.widget.Button finishButton = new android.widget.Button(this);
3028 finishButton.setText("Finish");
3029 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3030
3031 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3032 public void onClick(View v) {
3033 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003034 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003035 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003036 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003037
Adam Cohen4eac29a2011-07-11 17:53:37 -07003038 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
3039 unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003040 }
3041
3042 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003043 * Bind the items start-end from the list.
3044 *
3045 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003046 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003047 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3048
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003049 setLoadOnResume();
3050
Joe Onorato9c1289c2009-08-17 11:03:03 -04003051 final Workspace workspace = mWorkspace;
3052
3053 for (int i=start; i<end; i++) {
3054 final ItemInfo item = shortcuts.get(i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003055 switch (item.itemType) {
3056 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3057 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003058 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003059 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3060 false);
3061 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003062 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003063 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003064 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003065 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003066 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3067 false);
3068 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003069 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003070 }
3071
Joe Onorato9c1289c2009-08-17 11:03:03 -04003072 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003073 }
3074
Joe Onorato9c1289c2009-08-17 11:03:03 -04003075 /**
3076 * Implementation of the method from LauncherModel.Callbacks.
3077 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003078 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003079 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003080 sFolders.clear();
3081 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003082 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003083
3084 /**
3085 * Add the views for a widget to the workspace.
3086 *
3087 * Implementation of the method from LauncherModel.Callbacks.
3088 */
3089 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003090 setLoadOnResume();
3091
Daniel Sandler843e8602010-06-07 14:59:01 -04003092 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3093 if (DEBUG_WIDGETS) {
3094 Log.d(TAG, "bindAppWidget: " + item);
3095 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003096 final Workspace workspace = mWorkspace;
3097
3098 final int appWidgetId = item.appWidgetId;
3099 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003100 if (DEBUG_WIDGETS) {
3101 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3102 }
3103
Joe Onorato9c1289c2009-08-17 11:03:03 -04003104 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3105
Joe Onorato9c1289c2009-08-17 11:03:03 -04003106 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3107 item.hostView.setTag(item);
3108
3109 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3110 item.cellY, item.spanX, item.spanY, false);
3111
Adam Cohended9f8d2010-11-03 13:25:16 -07003112 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3113
Joe Onorato9c1289c2009-08-17 11:03:03 -04003114 workspace.requestLayout();
3115
Daniel Sandler843e8602010-06-07 14:59:01 -04003116 if (DEBUG_WIDGETS) {
3117 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3118 + (SystemClock.uptimeMillis()-start) + "ms");
3119 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003120 }
3121
3122 /**
3123 * Callback saying that there aren't any more items to bind.
3124 *
3125 * Implementation of the method from LauncherModel.Callbacks.
3126 */
3127 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003128 setLoadOnResume();
3129
Joe Onorato9c1289c2009-08-17 11:03:03 -04003130 if (mSavedState != null) {
3131 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003132 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003133 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003134 mSavedState = null;
3135 }
3136
3137 if (mSavedInstanceState != null) {
3138 super.onRestoreInstanceState(mSavedInstanceState);
3139 mSavedInstanceState = null;
3140 }
3141
Joe Onorato9c1289c2009-08-17 11:03:03 -04003142 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003143
3144 // If we received the result of any pending adds while the loader was running (e.g. the
3145 // widget configuration forced an orientation change), process them now.
3146 for (int i = 0; i < sPendingAddList.size(); i++) {
3147 completeAdd(sPendingAddList.get(i));
3148 }
3149 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003150 }
3151
3152 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003153 * Updates the icons on the launcher that are affected by changes to the package list
3154 * on the device.
3155 */
3156 private void updateIconsAffectedByPackageManagerChanges() {
3157 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003158 updateVoiceSearchIcon();
3159 }
3160
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003161 @Override
3162 public void bindSearchablesChanged() {
3163 updateGlobalSearchIcon();
3164 }
3165
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003166 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003167 * Add the icons for all apps.
3168 *
3169 * Implementation of the method from LauncherModel.Callbacks.
3170 */
3171 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003172 if (mAppsCustomizeContent != null) {
3173 mAppsCustomizeContent.setApps(apps);
3174 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003175 updateIconsAffectedByPackageManagerChanges();
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003176 updateGlobalSearchIcon();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003177 }
3178
3179 /**
3180 * A package was installed.
3181 *
3182 * Implementation of the method from LauncherModel.Callbacks.
3183 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003184 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003185 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003186 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003187
Winson Chung785d2eb2011-04-14 16:08:02 -07003188 if (mAppsCustomizeContent != null) {
3189 mAppsCustomizeContent.addApps(apps);
3190 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003191 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003192 }
3193
3194 /**
3195 * A package was updated.
3196 *
3197 * Implementation of the method from LauncherModel.Callbacks.
3198 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003199 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003200 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003201 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003202 if (mWorkspace != null) {
3203 mWorkspace.updateShortcuts(apps);
3204 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003205
Winson Chung785d2eb2011-04-14 16:08:02 -07003206 if (mAppsCustomizeContent != null) {
3207 mAppsCustomizeContent.updateApps(apps);
3208 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003209 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003210 }
3211
3212 /**
3213 * A package was uninstalled.
3214 *
3215 * Implementation of the method from LauncherModel.Callbacks.
3216 */
Joe Onorato36115782010-06-17 13:28:48 -04003217 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003218 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003219 if (permanent) {
3220 mWorkspace.removeItems(apps);
3221 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003222
Winson Chung785d2eb2011-04-14 16:08:02 -07003223 if (mAppsCustomizeContent != null) {
3224 mAppsCustomizeContent.removeApps(apps);
3225 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003226 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003227 }
Joe Onoratobe386092009-11-17 17:32:16 -08003228
3229 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003230 * A number of packages were updated.
3231 */
3232 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003233
Winson Chung785d2eb2011-04-14 16:08:02 -07003234 if (mAppsCustomizeContent != null) {
3235 mAppsCustomizeContent.onPackagesUpdated();
3236 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003237 }
3238
Winson Chung400438b2011-01-16 17:53:48 -08003239 private int mapConfigurationOriActivityInfoOri(int configOri) {
3240 final Display d = getWindowManager().getDefaultDisplay();
3241 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3242 switch (d.getRotation()) {
3243 case Surface.ROTATION_0:
3244 case Surface.ROTATION_180:
3245 // We are currently in the same basic orientation as the natural orientation
3246 naturalOri = configOri;
3247 break;
3248 case Surface.ROTATION_90:
3249 case Surface.ROTATION_270:
3250 // We are currently in the other basic orientation to the natural orientation
3251 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3252 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3253 break;
3254 }
3255
3256 int[] oriMap = {
3257 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3258 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3259 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3260 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3261 };
3262 // Since the map starts at portrait, we need to offset if this device's natural orientation
3263 // is landscape.
3264 int indexOffset = 0;
3265 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3266 indexOffset = 1;
3267 }
3268 return oriMap[(d.getRotation() + indexOffset) % 4];
3269 }
3270 public void lockScreenOrientation() {
3271 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3272 .getConfiguration().orientation));
3273 }
3274 public void unlockScreenOrientation() {
3275 mHandler.postDelayed(new Runnable() {
3276 public void run() {
3277 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3278 }
3279 }, mRestoreScreenOrientationDelay);
3280 }
3281
Winson Chung80baf5a2010-08-09 16:03:15 -07003282 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003283 * Prints out out state for debugging.
3284 */
3285 public void dumpState() {
3286 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003287 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003288 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3289 Log.d(TAG, "mRestoring=" + mRestoring);
3290 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3291 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003292 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003293 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003294
Winson Chung785d2eb2011-04-14 16:08:02 -07003295 if (mAppsCustomizeContent != null) {
3296 mAppsCustomizeContent.dumpState();
3297 }
Joe Onoratobe386092009-11-17 17:32:16 -08003298 Log.d(TAG, "END launcher2 dump state");
3299 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003300}
Michael Jurka2763be32011-02-24 11:19:57 -08003301
Michael Jurkaabded662011-03-04 12:06:57 -08003302interface LauncherTransitionable {
3303 void onLauncherTransitionStart(Animator animation);
3304 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003305}