blob: 452421cd76f5a353f6b5702bce26413e8dc3e584 [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();
320 }
321 if (sGlobalSearchIcon != null) {
322 updateGlobalSearchIcon(sGlobalSearchIcon);
323 }
324 if (sVoiceSearchIcon != null) {
325 updateVoiceSearchIcon(sVoiceSearchIcon);
326 }
327 if (sAppMarketIcon != null) {
328 updateAppMarketIcon(sAppMarketIcon);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800329 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 }
Romain Guycbb89e42009-06-08 15:52:54 -0700331
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800332 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700333 if (sLocaleConfiguration == null) {
334 new AsyncTask<Void, Void, LocaleConfiguration>() {
335 @Override
336 protected LocaleConfiguration doInBackground(Void... unused) {
337 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
338 readConfiguration(Launcher.this, localeConfiguration);
339 return localeConfiguration;
340 }
341
342 @Override
343 protected void onPostExecute(LocaleConfiguration result) {
344 sLocaleConfiguration = result;
345 checkForLocaleChange(); // recursive, but now with a locale configuration
346 }
347 }.execute();
348 return;
349 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 final Configuration configuration = getResources().getConfiguration();
352
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700353 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 final String locale = configuration.locale.toString();
355
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700356 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 final int mcc = configuration.mcc;
358
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700359 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 final int mnc = configuration.mnc;
361
Romain Guy84f296c2009-11-04 15:00:44 -0800362 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363
Romain Guy84f296c2009-11-04 15:00:44 -0800364 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700365 sLocaleConfiguration.locale = locale;
366 sLocaleConfiguration.mcc = mcc;
367 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700368
Joe Onorato0589f0f2010-02-08 13:44:00 -0800369 mIconCache.flush();
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400370 loadHotseats();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700371
372 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
373 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700374 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700375 public void run() {
376 writeConfiguration(Launcher.this, localeConfiguration);
377 }
378 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700379 }
380 }
381
382 private static class LocaleConfiguration {
383 public String locale;
384 public int mcc = -1;
385 public int mnc = -1;
386 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700387
Romain Guy98d01652009-06-30 16:21:04 -0700388 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
389 DataInputStream in = null;
390 try {
391 in = new DataInputStream(context.openFileInput(PREFERENCES));
392 configuration.locale = in.readUTF();
393 configuration.mcc = in.readInt();
394 configuration.mnc = in.readInt();
395 } catch (FileNotFoundException e) {
396 // Ignore
397 } catch (IOException e) {
398 // Ignore
399 } finally {
400 if (in != null) {
401 try {
402 in.close();
403 } catch (IOException e) {
404 // Ignore
405 }
406 }
407 }
408 }
409
410 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
411 DataOutputStream out = null;
412 try {
413 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
414 out.writeUTF(configuration.locale);
415 out.writeInt(configuration.mcc);
416 out.writeInt(configuration.mnc);
417 out.flush();
418 } catch (FileNotFoundException e) {
419 // Ignore
420 } catch (IOException e) {
421 //noinspection ResultOfMethodCallIgnored
422 context.getFileStreamPath(PREFERENCES).delete();
423 } finally {
424 if (out != null) {
425 try {
426 out.close();
427 } catch (IOException e) {
428 // Ignore
429 }
430 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800431 }
432 }
433
Adam Cohen716b51e2011-06-30 12:09:54 -0700434 public DragLayer getDragLayer() {
435 return mDragLayer;
436 }
437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 static int getScreen() {
439 synchronized (sLock) {
440 return sScreen;
441 }
442 }
443
444 static void setScreen(int screen) {
445 synchronized (sLock) {
446 sScreen = screen;
447 }
448 }
449
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400450 // Note: This doesn't do all the client-id magic that BrowserProvider does
451 // in Browser. (http://b/2425179)
452 private Uri getDefaultBrowserUri() {
453 String url = getString(R.string.default_browser_url);
454 if (url.indexOf("{CID}") != -1) {
455 url = url.replace("{CID}", "android-google");
456 }
457 return Uri.parse(url);
458 }
459
460 // Load the Intent templates from arrays.xml to populate the hotseats. For
461 // each Intent, if it resolves to a single app, use that as the launch
462 // intent & use that app's label as the contentDescription. Otherwise,
463 // retain the ResolveActivity so the user can pick an app.
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400464 private void loadHotseats() {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400465 if (mHotseatConfig == null) {
466 mHotseatConfig = getResources().getStringArray(R.array.hotseats);
467 if (mHotseatConfig.length > 0) {
468 mHotseats = new Intent[mHotseatConfig.length];
469 mHotseatLabels = new CharSequence[mHotseatConfig.length];
470 mHotseatIcons = new Drawable[mHotseatConfig.length];
471 } else {
472 mHotseats = null;
473 mHotseatIcons = null;
474 mHotseatLabels = null;
475 }
476
477 TypedArray hotseatIconDrawables = getResources().obtainTypedArray(R.array.hotseat_icons);
478 for (int i=0; i<mHotseatConfig.length; i++) {
479 // load icon for this slot; currently unrelated to the actual activity
480 try {
481 mHotseatIcons[i] = hotseatIconDrawables.getDrawable(i);
482 } catch (ArrayIndexOutOfBoundsException ex) {
483 Log.w(TAG, "Missing hotseat_icons array item #" + i);
484 mHotseatIcons[i] = null;
485 }
486 }
487 hotseatIconDrawables.recycle();
488 }
489
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400490 PackageManager pm = getPackageManager();
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400491 for (int i=0; i<mHotseatConfig.length; i++) {
492 Intent intent = null;
493 if (mHotseatConfig[i].equals("*BROWSER*")) {
494 // magic value meaning "launch user's default web browser"
495 // replace it with a generic web request so we can see if there is indeed a default
496 String defaultUri = getString(R.string.default_browser_url);
497 intent = new Intent(
498 Intent.ACTION_VIEW,
499 ((defaultUri != null)
500 ? Uri.parse(defaultUri)
501 : getDefaultBrowserUri())
502 ).addCategory(Intent.CATEGORY_BROWSABLE);
503 // note: if the user launches this without a default set, she
504 // will always be taken to the default URL above; this is
505 // unavoidable as we must specify a valid URL in order for the
Winson Chungaafa03c2010-06-11 17:34:16 -0700506 // chooser to appear, and once the user selects something, that
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400507 // URL is unavoidably sent to the chosen app.
508 } else {
509 try {
510 intent = Intent.parseUri(mHotseatConfig[i], 0);
511 } catch (java.net.URISyntaxException ex) {
512 Log.w(TAG, "Invalid hotseat intent: " + mHotseatConfig[i]);
513 // bogus; leave intent=null
514 }
515 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700516
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400517 if (intent == null) {
518 mHotseats[i] = null;
519 mHotseatLabels[i] = getText(R.string.activity_not_found);
520 continue;
521 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400522
523 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700524 Log.d(TAG, "loadHotseats: hotseat " + i
525 + " initial intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400526 + intent.toUri(Intent.URI_INTENT_SCHEME)
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400527 + "]");
528 }
529
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400530 ResolveInfo bestMatch = pm.resolveActivity(intent, PackageManager.MATCH_DEFAULT_ONLY);
531 List<ResolveInfo> allMatches = pm.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
Winson Chungaafa03c2010-06-11 17:34:16 -0700532 if (LOGD) {
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400533 Log.d(TAG, "Best match for intent: " + bestMatch);
534 Log.d(TAG, "All matches: ");
535 for (ResolveInfo ri : allMatches) {
536 Log.d(TAG, " --> " + ri);
537 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400538 }
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400539 // did this resolve to a single app, or the resolver?
540 if (allMatches.size() == 0 || bestMatch == null) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700541 // can't find any activity to handle this. let's leave the
542 // intent as-is and let Launcher show a toast when it fails
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400543 // to launch.
544 mHotseats[i] = intent;
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400545
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400546 // set accessibility text to "Not installed"
547 mHotseatLabels[i] = getText(R.string.activity_not_found);
548 } else {
549 boolean found = false;
550 for (ResolveInfo ri : allMatches) {
551 if (bestMatch.activityInfo.name.equals(ri.activityInfo.name)
552 && bestMatch.activityInfo.applicationInfo.packageName
553 .equals(ri.activityInfo.applicationInfo.packageName)) {
554 found = true;
555 break;
556 }
557 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700558
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400559 if (!found) {
560 if (LOGD) Log.d(TAG, "Multiple options, no default yet");
561 // the bestMatch is probably the ResolveActivity, meaning the
562 // user has not yet selected a default
563 // so: we'll keep the original intent for now
564 mHotseats[i] = intent;
565
566 // set the accessibility text to "Select shortcut"
567 mHotseatLabels[i] = getText(R.string.title_select_shortcut);
568 } else {
569 // we have an app!
570 // now reconstruct the intent to launch it through the front
571 // door
572 ComponentName com = new ComponentName(
573 bestMatch.activityInfo.applicationInfo.packageName,
574 bestMatch.activityInfo.name);
575 mHotseats[i] = new Intent(Intent.ACTION_MAIN).setComponent(com);
576
577 // load the app label for accessibility
578 mHotseatLabels[i] = bestMatch.activityInfo.loadLabel(pm);
579 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400580 }
581
582 if (LOGD) {
Winson Chungaafa03c2010-06-11 17:34:16 -0700583 Log.d(TAG, "loadHotseats: hotseat " + i
584 + " final intent=["
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400585 + ((mHotseats[i] == null)
586 ? "null"
587 : mHotseats[i].toUri(Intent.URI_INTENT_SCHEME))
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400588 + "] label=[" + mHotseatLabels[i]
589 + "]"
590 );
591 }
592 }
593 }
594
Winson Chung557d6ed2011-07-08 15:34:52 -0700595 /**
596 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
597 * a configuration step, this allows the proper animations to run after other transitions.
598 */
599 private boolean completeAdd(PendingAddArguments args) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800600 switch (args.requestCode) {
601 case REQUEST_PICK_APPLICATION:
602 completeAddApplication(args.intent, args.screen, args.cellX, args.cellY);
603 break;
604 case REQUEST_PICK_SHORTCUT:
605 processShortcut(args.intent);
606 break;
607 case REQUEST_CREATE_SHORTCUT:
608 completeAddShortcut(args.intent, args.screen, args.cellX, args.cellY);
Winson Chung557d6ed2011-07-08 15:34:52 -0700609 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800610 case REQUEST_PICK_APPWIDGET:
611 addAppWidgetFromPick(args.intent);
612 break;
613 case REQUEST_CREATE_APPWIDGET:
614 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
615 completeAddAppWidget(appWidgetId, args.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -0700616 return true;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800617 case REQUEST_PICK_WALLPAPER:
618 // We just wanted the activity result here so we can clear mWaitingForResult
619 break;
620 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700621 return false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800622 }
623
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800625 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700626 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700627 mWaitingForResult = false;
628
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800629 // The pattern used here is that a user PICKs a specific application,
630 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
633 // launch over to the Music app to actually CREATE_SHORTCUT.
Michael Jurka0280c3b2010-09-17 15:00:07 -0700634 if (resultCode == RESULT_OK && mAddScreen != -1) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800635 final PendingAddArguments args = new PendingAddArguments();
636 args.requestCode = requestCode;
637 args.intent = data;
638 args.screen = mAddScreen;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700639 args.cellX = mAddCellX;
640 args.cellY = mAddCellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800641
642 // If the loader is still running, defer the add until it is done.
643 if (isWorkspaceLocked()) {
644 sPendingAddList.add(args);
645 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700646 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800647 }
Romain Guy18042c82009-11-06 11:44:55 -0800648 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700649 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
650 if (data != null) {
651 // Clean up the appWidgetId if we canceled
652 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
653 if (appWidgetId != -1) {
654 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
655 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 }
657 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700658
659 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
660 exitSpringLoadedDragModeDelayed(delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 }
662
663 @Override
664 protected void onResume() {
665 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800666 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700667 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400668 mWorkspaceLoading = true;
669 mModel.startLoader(this, true);
670 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700671 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800673 if (mWaitingForResume != null) {
674 mWaitingForResume.setStayPressed(false);
675 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700676 // When we resume Launcher, a different Activity might be responsible for the app
677 // market intent, so refresh the icon
678 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
680
681 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700682 protected void onPause() {
683 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700684 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800685 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700686 }
Romain Guycbb89e42009-06-08 15:52:54 -0700687
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700688 @Override
689 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400690 // Flag the loader to stop early before switching
691 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700692 if (mAppsCustomizeContent != null) {
693 mAppsCustomizeContent.surrender();
694 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800695 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700696 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700697
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800698 // We can't hide the IME if it was forced open. So don't bother
699 /*
700 @Override
701 public void onWindowFocusChanged(boolean hasFocus) {
702 super.onWindowFocusChanged(hasFocus);
703
704 if (hasFocus) {
705 final InputMethodManager inputManager = (InputMethodManager)
706 getSystemService(Context.INPUT_METHOD_SERVICE);
707 WindowManager.LayoutParams lp = getWindow().getAttributes();
708 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
709 android.os.Handler()) {
710 protected void onReceiveResult(int resultCode, Bundle resultData) {
711 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
712 }
713 });
714 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
715 }
716 }
717 */
718
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 private boolean acceptFilter() {
720 final InputMethodManager inputManager = (InputMethodManager)
721 getSystemService(Context.INPUT_METHOD_SERVICE);
722 return !inputManager.isFullscreenMode();
723 }
724
725 @Override
726 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700727 final int uniChar = event.getUnicodeChar();
728 final boolean handled = super.onKeyDown(keyCode, event);
729 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
730 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800731 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
732 keyCode, event);
733 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700734 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700735 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700736 // showSearchDialog()
737 // If there are multiple keystrokes before the search dialog takes focus,
738 // onSearchRequested() will be called for every keystroke,
739 // but it is idempotent, so it's fine.
740 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 }
742 }
743
Joe Onorato8a9625e2010-01-28 15:55:35 -0800744 // Eat the long press event so the keyboard doesn't come up.
745 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
746 return true;
747 }
748
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800749 return handled;
750 }
751
Karl Rosaen138a0412009-04-23 19:00:21 -0700752 private String getTypedText() {
753 return mDefaultKeySsb.toString();
754 }
755
756 private void clearTypedText() {
757 mDefaultKeySsb.clear();
758 mDefaultKeySsb.clearSpans();
759 Selection.setSelection(mDefaultKeySsb, 0);
760 }
761
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700763 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
764 * State
765 */
766 private static State intToState(int stateOrdinal) {
767 State state = State.WORKSPACE;
768 final State[] stateValues = State.values();
769 for (int i = 0; i < stateValues.length; i++) {
770 if (stateValues[i].ordinal() == stateOrdinal) {
771 state = stateValues[i];
772 break;
773 }
774 }
775 return state;
776 }
777
778 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 * Restores the previous state, if it exists.
780 *
781 * @param savedState The previous state.
782 */
783 private void restoreState(Bundle savedState) {
784 if (savedState == null) {
785 return;
786 }
787
Michael Jurka883f55b2010-10-21 15:47:14 -0700788 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
789
Winson Chungf0ea4d32011-06-06 14:27:16 -0700790 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800791 showAllApps(false);
792 }
793
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800794 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
795 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700796 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 }
798
799 final int addScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700800
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 if (addScreen > -1) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700802 mAddScreen = addScreen;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700803 mAddCellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
804 mAddCellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 mRestoring = true;
806 }
807
808 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
809 if (renameFolder) {
810 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700811 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800812 mRestoring = true;
813 }
Winson Chunga12a2502010-12-20 14:41:35 -0800814
Winson Chung785d2eb2011-04-14 16:08:02 -0700815
816 // Restore the AppsCustomize tab
817 if (mAppsCustomizeTabHost != null) {
818 String curTab = savedState.getString("apps_customize_currentTab");
819 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700820 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700821 mAppsCustomizeContent.setContentType(
822 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
823 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
824 }
825
826 // Note: currently we do not restore the page for the AppsCustomize pane because the
827 // change in layout can drastically affect the saved page index
828 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 }
830
831 /**
832 * Finds all the views we need and configure them properly.
833 */
834 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700835 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400836
Winson Chung4c98d922011-05-31 16:50:48 -0700837 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
838 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839
Winson Chung4c98d922011-05-31 16:50:48 -0700840 // Setup the drag layer
841 mDragLayer.setup(this, dragController);
842
843 // Setup the workspace
844 mWorkspace.setHapticFeedbackEnabled(false);
845 mWorkspace.setOnLongClickListener(this);
846 mWorkspace.setup(this, dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700847 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700848
Winson Chungf0ea4d32011-06-06 14:27:16 -0700849 // Get the search/delete bar
850 mSearchDeleteBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700851
Winson Chungf0ea4d32011-06-06 14:27:16 -0700852 // Setup AppsCustomize
853 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
854 findViewById(R.id.apps_customize_pane);
855 mAppsCustomizeContent = (AppsCustomizePagedView)
856 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
857 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400858
Winson Chungf0ea4d32011-06-06 14:27:16 -0700859 // Setup AppsCustomize button
860 mAllAppsButton = mDragLayer.findViewById(R.id.all_apps_button);
861 mAllAppsButton.setOnClickListener(new OnClickListener() {
862 public void onClick(View v) {
863 onClickAllAppsButton(v);
864 }
865 });
Romain Guy1fbc1c82009-11-09 20:43:08 -0800866
Winson Chungf0ea4d32011-06-06 14:27:16 -0700867 if (!LauncherApplication.isScreenLarge()) {
868 // Setup AppsCustomize button on the phone
869 HandleView handleView = (HandleView) mAllAppsButton;
870 handleView.setLauncher(this);
871 handleView.setOnLongClickListener(this);
Winson Chung4c98d922011-05-31 16:50:48 -0700872
873 // Setup Hotseat
874 ImageView hotseatLeft = (ImageView) findViewById(R.id.hotseat_left);
875 hotseatLeft.setContentDescription(mHotseatLabels[0]);
876 hotseatLeft.setImageDrawable(mHotseatIcons[0]);
877 ImageView hotseatRight = (ImageView) findViewById(R.id.hotseat_right);
878 hotseatRight.setContentDescription(mHotseatLabels[1]);
879 hotseatRight.setImageDrawable(mHotseatIcons[1]);
Adam Cohencdc30d52010-12-01 15:09:47 -0800880 }
881
Winson Chungf0ea4d32011-06-06 14:27:16 -0700882 if (!LauncherApplication.isScreenLarge()) {
883 // Setup keylistener for button cluster
884 mButtonCluster = (ViewGroup) findViewById(R.id.all_apps_button_cluster);
885 View.OnKeyListener listener = null;
886 if (LauncherApplication.isScreenLarge()) {
887 // For tablets, AllApps lives in the button bar at the top
888 listener = new ButtonBarKeyEventListener();
889 } else {
890 // For phones, AppsCustomize lives in the "dock" at the bottom
891 listener = new DockKeyEventListener();
892 }
893 int buttonCount = mButtonCluster.getChildCount();
894 for (int i = 0; i < buttonCount; ++i) {
895 mButtonCluster.getChildAt(i).setOnKeyListener(listener);
896 }
Winson Chung97d85d22011-04-13 11:27:36 -0700897 }
Michael Jurka838a4ca2011-02-07 13:33:06 -0800898
Winson Chung4c98d922011-05-31 16:50:48 -0700899 // Setup the drag controller (the drop targets have to be added in reverse order)
900 dragController.setDragScoller(mWorkspace);
901 dragController.setScrollView(mDragLayer);
902 dragController.setMoveTarget(mWorkspace);
903 dragController.addDropTarget(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700904 if (mSearchDeleteBar != null) {
905 mSearchDeleteBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800906 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 }
908
Romain Guy8a73c512009-11-09 19:19:59 -0800909 @SuppressWarnings({"UnusedDeclaration"})
910 public void previousScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700911 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800912 mWorkspace.scrollLeft();
913 }
Romain Guy8a73c512009-11-09 19:19:59 -0800914 }
915
916 @SuppressWarnings({"UnusedDeclaration"})
917 public void nextScreen(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700918 if (mState != State.APPS_CUSTOMIZE) {
Joe Onorato61597bd2009-11-19 12:51:57 -0800919 mWorkspace.scrollRight();
920 }
Romain Guy8a73c512009-11-09 19:19:59 -0800921 }
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400922
923 @SuppressWarnings({"UnusedDeclaration"})
924 public void launchHotSeat(View v) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700925 if (mState == State.APPS_CUSTOMIZE) return;
Daniel Sandler3e9454a2010-05-24 11:22:41 -0400926
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400927 int index = -1;
928 if (v.getId() == R.id.hotseat_left) {
929 index = 0;
930 } else if (v.getId() == R.id.hotseat_right) {
931 index = 1;
932 }
933
Daniel Sandlerab1ebd72010-04-27 16:57:25 -0400934 // reload these every tap; you never know when they might change
935 loadHotseats();
936 if (index >= 0 && index < mHotseats.length && mHotseats[index] != null) {
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400937 startActivitySafely(
938 mHotseats[index],
939 "hotseat"
940 );
941 }
942 }
Winson Chungaafa03c2010-06-11 17:34:16 -0700943
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 /**
945 * Creates a view representing a shortcut.
946 *
947 * @param info The data structure describing the shortcut.
948 *
949 * @return A View inflated from R.layout.application.
950 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800951 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700953 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 }
955
956 /**
957 * Creates a view representing a shortcut inflated from the specified resource.
958 *
959 * @param layoutResId The id of the XML layout used to create the shortcut.
960 * @param parent The group the shortcut belongs to.
961 * @param info The data structure describing the shortcut.
962 *
963 * @return A View inflated from layoutResId.
964 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800965 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800966 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
967 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800968 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 return favorite;
970 }
971
972 /**
973 * Add an application shortcut to the workspace.
974 *
975 * @param data The intent describing the application.
976 * @param cellInfo The position on screen where to create the shortcut.
977 */
Adam Cohenfbba09b2011-07-18 21:43:05 -0700978 void completeAddApplication(Intent data, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700979 final int[] cellXY = mTmpAddItemCellCoordinates;
980 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
981
Adam Cohenfbba09b2011-07-18 21:43:05 -0700982 // First we check if we already know the exact location where we want to add this item.
983 if (cellX >= 0 && cellY >= 0) {
984 cellXY[0] = cellX;
985 cellXY[1] = cellY;
986 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700987 showOutOfSpaceMessage();
988 return;
989 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800991 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800992
Romain Guy73b979d2009-06-09 12:57:21 -0700993 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800994 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800996 info.container = ItemInfo.NO_ID;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700997 mWorkspace.addApplicationShortcut(info, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700998 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800999 } else {
1000 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 }
1002 }
Romain Guycbb89e42009-06-08 15:52:54 -07001003
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 /**
1005 * Add a shortcut to the workspace.
1006 *
1007 * @param data The intent describing the shortcut.
1008 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 */
Adam Cohenfbba09b2011-07-18 21:43:05 -07001010 private void completeAddShortcut(Intent data, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001011 final int[] cellXY = mTmpAddItemCellCoordinates;
1012 final CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001013
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001014 int[] touchXY = mAddDropPosition;
Michael Jurkad3ef3062010-11-23 16:23:58 -08001015 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001016
1017 // First we check if we already know the exact location where we want to add this item.
1018 if (cellX >= 0 && cellY >= 0) {
1019 cellXY[0] = cellX;
1020 cellXY[1] = cellY;
1021 foundCellSpan = true;
1022 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001023 // when dragging and dropping, just find the closest free spot
1024 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1025 int[] result = screenLayout.findNearestVacantArea(
1026 touchXY[0], touchXY[1], 1, 1, cellXY);
1027 foundCellSpan = (result != null);
1028 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001029 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001030 }
1031
1032 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001033 showOutOfSpaceMessage();
1034 return;
1035 }
1036
1037 final ShortcutInfo info = mModel.addShortcut(
1038 this, data, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001039
1040 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001041 final View view = createShortcut(info);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001042 mWorkspace.addInScreen(view, screen, cellXY[0], cellXY[1], 1, 1, isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001043 }
1044 }
1045
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001046 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001047 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001049 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001050 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 */
Winson Chungf7640c82011-02-28 13:47:29 -08001052 private void completeAddAppWidget(final int appWidgetId, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001053 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -07001054
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001055 // Calculate the grid spans needed to fit this widget
Michael Jurka0280c3b2010-09-17 15:00:07 -07001056 CellLayout layout = (CellLayout) mWorkspace.getChildAt(screen);
Adam Cohen09353862011-07-14 16:10:03 -07001057
1058 // We want to account for the extra amount of padding that we are adding to the widget
1059 // to ensure that it gets the full amount of space that it has requested
1060 Resources r = getResources();
1061 int requiredWidth = appWidgetInfo.minWidth +
1062 r.getDimensionPixelSize(R.dimen.app_widget_padding_left) +
1063 r.getDimensionPixelSize(R.dimen.app_widget_padding_right);
1064 int requiredHeight = appWidgetInfo.minHeight +
1065 r.getDimensionPixelSize(R.dimen.app_widget_padding_top) +
1066 r.getDimensionPixelSize(R.dimen.app_widget_padding_bottom);
1067 int[] spanXY = layout.rectToCell(requiredWidth, requiredHeight, null);
Romain Guycbb89e42009-06-08 15:52:54 -07001068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001069 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001070 // We have saved the position to which the widget was dragged-- this really only matters
1071 // if we are placing widgets on a "spring-loaded" screen
Michael Jurka0280c3b2010-09-17 15:00:07 -07001072 final int[] cellXY = mTmpAddItemCellCoordinates;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001073
Patrick Dubroy8b1fe772011-01-25 05:32:24 -08001074 int[] touchXY = mAddDropPosition;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001075 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001076 if (mAddCellX >= 0 && mAddCellY >= 0) {
1077 cellXY[0] = mAddCellX;
1078 cellXY[1] = mAddCellY;
1079 foundCellSpan = true;
1080 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001081 // when dragging and dropping, just find the closest free spot
1082 CellLayout screenLayout = (CellLayout) mWorkspace.getChildAt(screen);
1083 int[] result = screenLayout.findNearestVacantArea(
1084 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001085 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001086 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001087 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001088 }
1089
Michael Jurka0280c3b2010-09-17 15:00:07 -07001090 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001091 if (appWidgetId != -1) {
1092 // Deleting an app widget ID is a void call but writes to disk before returning
1093 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001094 new Thread("deleteAppWidgetId") {
1095 public void run() {
1096 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1097 }
1098 }.start();
1099 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001100 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001101 return;
1102 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001103
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001104 // Build Launcher-specific widget info and save to database
1105 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001106 launcherInfo.spanX = spanXY[0];
1107 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 LauncherModel.addItemToDatabase(this, launcherInfo,
1110 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Michael Jurka0280c3b2010-09-17 15:00:07 -07001111 screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001112
1113 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001115 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001116
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001117 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001118 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001119
Michael Jurka0280c3b2010-09-17 15:00:07 -07001120 mWorkspace.addInScreen(launcherInfo.hostView, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001121 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001122
1123 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001124 }
1125 }
Romain Guycbb89e42009-06-08 15:52:54 -07001126
Adam Cohended9f8d2010-11-03 13:25:16 -07001127 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1128 @Override
1129 public void onReceive(Context context, Intent intent) {
1130 final String action = intent.getAction();
1131 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1132 mUserPresent = false;
1133 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001134
1135 // Reset AllApps to it's initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001136 if (mAppsCustomizeContent != null) {
1137 mAppsCustomizeContent.reset();
1138 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001139 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1140 mUserPresent = true;
1141 updateRunning();
1142 }
1143 }
1144 };
1145
1146 @Override
1147 public void onAttachedToWindow() {
1148 super.onAttachedToWindow();
1149
1150 // Listen for broadcasts related to user-presence
1151 final IntentFilter filter = new IntentFilter();
1152 filter.addAction(Intent.ACTION_SCREEN_OFF);
1153 filter.addAction(Intent.ACTION_USER_PRESENT);
1154 registerReceiver(mReceiver, filter);
1155
Adam Cohend113e0c2010-11-11 10:48:05 -08001156 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001157 mVisible = true;
1158 }
1159
1160 @Override
1161 public void onDetachedFromWindow() {
1162 super.onDetachedFromWindow();
1163 mVisible = false;
1164
Adam Cohend113e0c2010-11-11 10:48:05 -08001165 if (mAttached) {
1166 unregisterReceiver(mReceiver);
1167 mAttached = false;
1168 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001169 updateRunning();
1170 }
1171
1172 public void onWindowVisibilityChanged(int visibility) {
1173 mVisible = visibility == View.VISIBLE;
1174 updateRunning();
1175 }
1176
1177 private void sendAdvanceMessage(long delay) {
1178 mHandler.removeMessages(ADVANCE_MSG);
1179 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1180 mHandler.sendMessageDelayed(msg, delay);
1181 mAutoAdvanceSentTime = System.currentTimeMillis();
1182 }
1183
1184 private void updateRunning() {
1185 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1186 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1187 mAutoAdvanceRunning = autoAdvanceRunning;
1188 if (autoAdvanceRunning) {
1189 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1190 sendAdvanceMessage(delay);
1191 } else {
1192 if (!mWidgetsToAdvance.isEmpty()) {
1193 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1194 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1195 }
1196 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001197 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001198 }
1199 }
1200 }
1201
1202 private final Handler mHandler = new Handler() {
1203 @Override
1204 public void handleMessage(Message msg) {
1205 if (msg.what == ADVANCE_MSG) {
1206 int i = 0;
1207 for (View key: mWidgetsToAdvance.keySet()) {
1208 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1209 final int delay = mAdvanceStagger * i;
1210 if (v instanceof Advanceable) {
1211 postDelayed(new Runnable() {
1212 public void run() {
1213 ((Advanceable) v).advance();
1214 }
1215 }, delay);
1216 }
1217 i++;
1218 }
1219 sendAdvanceMessage(mAdvanceInterval);
1220 }
1221 }
1222 };
1223
1224 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001225 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001226 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1227 if (v instanceof Advanceable) {
1228 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001229 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001230 updateRunning();
1231 }
1232 }
1233
1234 void removeWidgetToAutoAdvance(View hostView) {
1235 if (mWidgetsToAdvance.containsKey(hostView)) {
1236 mWidgetsToAdvance.remove(hostView);
1237 updateRunning();
1238 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001239 }
1240
Joe Onorato9c1289c2009-08-17 11:03:03 -04001241 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001242 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001243 launcherInfo.hostView = null;
1244 }
1245
Adam Cohended9f8d2010-11-03 13:25:16 -07001246 void showOutOfSpaceMessage() {
1247 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1248 }
1249
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001250 public LauncherAppWidgetHost getAppWidgetHost() {
1251 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 }
Romain Guycbb89e42009-06-08 15:52:54 -07001253
Winson Chunga9abd0e2010-10-27 17:18:37 -07001254 public LauncherModel getModel() {
1255 return mModel;
1256 }
1257
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001258 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001259 getWindow().closeAllPanels();
1260
1261 try {
1262 dismissDialog(DIALOG_CREATE_SHORTCUT);
1263 // Unlock the workspace if the dialog was showing
1264 } catch (Exception e) {
1265 // An exception is thrown if the dialog is not visible, which is fine
1266 }
1267
1268 try {
1269 dismissDialog(DIALOG_RENAME_FOLDER);
1270 // Unlock the workspace if the dialog was showing
1271 } catch (Exception e) {
1272 // An exception is thrown if the dialog is not visible, which is fine
1273 }
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001274
1275 // Whatever we were doing is hereby canceled.
1276 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001277 }
1278
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279 @Override
1280 protected void onNewIntent(Intent intent) {
1281 super.onNewIntent(intent);
1282
1283 // Close the menu
1284 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001285 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001286 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001287
Adam Cohen95bb8002011-07-03 23:40:28 -07001288 closeFolder();
1289
Joe Onorato14f122b2009-11-19 14:06:36 -08001290 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1291 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001292
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001293 // In all these cases, only animate if we're already on home
Winson Chung785d2eb2011-04-14 16:08:02 -07001294 mWorkspace.unshrink(alreadyOnHome);
Patrick Dubroy758a9232011-03-03 19:54:56 -08001295 mWorkspace.exitWidgetResizeMode();
Patrick Dubroya0aa0122011-02-24 11:42:23 -08001296 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive()) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001297 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001298 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001299 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001300
Joe Onorato3a8820b2009-11-10 15:06:42 -08001301 final View v = getWindow().peekDecorView();
1302 if (v != null && v.getWindowToken() != null) {
1303 InputMethodManager imm = (InputMethodManager)getSystemService(
1304 INPUT_METHOD_SERVICE);
1305 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001306 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001307
Michael Jurka35aa14d2011-07-07 17:01:08 -07001308 // Reset AllApps to its initial state
Winson Chung785d2eb2011-04-14 16:08:02 -07001309 if (mAppsCustomizeContent != null) {
1310 mAppsCustomizeContent.reset();
1311 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
1313 }
1314
1315 @Override
1316 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1317 // Do not call super here
1318 mSavedInstanceState = savedInstanceState;
1319 }
1320
1321 @Override
1322 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001323 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001324 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
Michael Jurka883f55b2010-10-21 15:47:14 -07001326 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001327 // We close any open folder since it will not be re-opened, and we need to make sure
1328 // this state is reflected.
1329 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330
Michael Jurka0280c3b2010-09-17 15:00:07 -07001331 if (mAddScreen > -1 && mWaitingForResult) {
1332 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mAddScreen);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001333 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mAddCellX);
1334 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mAddCellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 }
1336
1337 if (mFolderInfo != null && mWaitingForResult) {
1338 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1339 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1340 }
Michael Jurka946ad472010-07-09 18:05:18 -07001341
Winson Chung785d2eb2011-04-14 16:08:02 -07001342 // Save the current AppsCustomize tab
1343 if (mAppsCustomizeTabHost != null) {
1344 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1345 if (currentTabTag != null) {
1346 outState.putString("apps_customize_currentTab", currentTabTag);
1347 }
1348 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001349 }
1350
1351 @Override
1352 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001354
Winson Chungcd2b0142011-06-08 16:02:26 -07001355 // Stop callbacks from LauncherModel
1356 LauncherApplication app = ((LauncherApplication) getApplication());
1357 mModel.stopLoader();
1358 app.setLauncher(null);
1359
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001361 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001363 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001365 mAppWidgetHost = null;
1366
1367 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368
1369 TextKeyListener.getInstance().release();
1370
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371
Adam Cohen4eac29a2011-07-11 17:53:37 -07001372 unbindWorkspaceItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001373
1374 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001375 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001376
1377 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1378 mWorkspace.removeAllViews();
1379 mWorkspace = null;
1380 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001381
1382 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 }
1384
Adam Cohena9cf38f2011-05-02 15:36:58 -07001385 public DragController getDragController() {
1386 return mDragController;
1387 }
1388
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 @Override
1390 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001391 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 super.startActivityForResult(intent, requestCode);
1393 }
1394
1395 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001396 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001398
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001399 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001400
Karl Rosaen138a0412009-04-23 19:00:21 -07001401 if (initialQuery == null) {
1402 // Use any text typed in the launcher as the initial query
1403 initialQuery = getTypedText();
1404 clearTypedText();
1405 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 if (appSearchData == null) {
1407 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001408 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409 }
Romain Guycbb89e42009-06-08 15:52:54 -07001410
Karl Rosaen138a0412009-04-23 19:00:21 -07001411 final SearchManager searchManager =
1412 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001413 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001414 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 }
1416
1417 @Override
1418 public boolean onCreateOptionsMenu(Menu menu) {
Patrick Dubroy5905bca2010-09-08 22:43:38 -07001419 if (isWorkspaceLocked()) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001420 return false;
1421 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422
1423 super.onCreateOptionsMenu(menu);
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001424
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001425 menu.add(MENU_GROUP_ADD, MENU_ADD, 0, R.string.menu_add)
1426 .setIcon(android.R.drawable.ic_menu_add)
1427 .setAlphabeticShortcut('A');
Winson Chung7ad01412010-09-27 11:33:03 -07001428 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1429 .setIcon(android.R.drawable.ic_menu_manage)
1430 .setAlphabeticShortcut('M');
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001431 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 .setIcon(android.R.drawable.ic_menu_gallery)
1433 .setAlphabeticShortcut('W');
1434 menu.add(0, MENU_SEARCH, 0, R.string.menu_search)
1435 .setIcon(android.R.drawable.ic_search_category_default)
1436 .setAlphabeticShortcut(SearchManager.MENU_KEY);
1437 menu.add(0, MENU_NOTIFICATIONS, 0, R.string.menu_notifications)
1438 .setIcon(com.android.internal.R.drawable.ic_menu_notifications)
1439 .setAlphabeticShortcut('N');
1440
1441 final Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
Romain Guy5a941392009-04-28 15:18:25 -07001442 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1443 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444
1445 menu.add(0, MENU_SETTINGS, 0, R.string.menu_settings)
1446 .setIcon(android.R.drawable.ic_menu_preferences).setAlphabeticShortcut('P')
1447 .setIntent(settings);
1448
1449 return true;
1450 }
1451
1452 @Override
1453 public boolean onPrepareOptionsMenu(Menu menu) {
1454 super.onPrepareOptionsMenu(menu);
1455
Winson Chung785d2eb2011-04-14 16:08:02 -07001456 // TODO-APPS_CUSTOMIZE: Remove this for the phone UI at some point, along with all the menu
1457 // related code?
1458 if (mAppsCustomizeContent != null && mAppsCustomizeContent.isAnimating()) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459
1460 return true;
1461 }
1462
1463 @Override
1464 public boolean onOptionsItemSelected(MenuItem item) {
1465 switch (item.getItemId()) {
1466 case MENU_ADD:
1467 addItems();
1468 return true;
Winson Chung7ad01412010-09-27 11:33:03 -07001469 case MENU_MANAGE_APPS:
1470 manageApps();
1471 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 case MENU_WALLPAPER_SETTINGS:
1473 startWallpaper();
1474 return true;
1475 case MENU_SEARCH:
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001476 onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 return true;
1478 case MENU_NOTIFICATIONS:
1479 showNotifications();
1480 return true;
1481 }
1482
1483 return super.onOptionsItemSelected(item);
1484 }
Romain Guycbb89e42009-06-08 15:52:54 -07001485
Karl Rosaen138a0412009-04-23 19:00:21 -07001486 /**
1487 * Indicates that we want global search for this activity by setting the globalSearch
1488 * argument for {@link #startSearch} to true.
1489 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001490
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001491 @Override
1492 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001493 startSearch(null, false, null, true);
Karl Rosaen138a0412009-04-23 19:00:21 -07001494 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001495 }
1496
Joe Onorato9c1289c2009-08-17 11:03:03 -04001497 public boolean isWorkspaceLocked() {
1498 return mWorkspaceLoading || mWaitingForResult;
1499 }
1500
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001502 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001503 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 }
1505
Michael Jurka0280c3b2010-09-17 15:00:07 -07001506 private void resetAddInfo() {
1507 mAddScreen = -1;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001508 mAddCellX = -1;
1509 mAddCellY = -1;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001510 mAddDropPosition = null;
1511 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001512
Winson Chung7ad01412010-09-27 11:33:03 -07001513 private void manageApps() {
1514 startActivity(new Intent(android.provider.Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS));
1515 }
1516
Michael Jurkaaf442092010-06-10 17:01:57 -07001517 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001518 // TODO: catch bad widget exception when sent
1519 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001520 // TODO: Is this log message meaningful?
1521 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001522 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001523 }
1524
Winson Chung55cef262010-10-28 14:14:18 -07001525 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001526 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527
Bjorn Bringert7984c942009-12-09 15:38:25 +00001528 if (appWidget.configure != null) {
1529 // Launch over to configure widget, if needed
1530 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1531 intent.setComponent(appWidget.configure);
1532 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001533 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001534 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1535 intent.putExtra(
1536 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1537 info.mimeType);
1538
1539 final String mimeType = info.mimeType;
1540 final ClipData clipData = (ClipData) info.configurationData;
1541 final ClipDescription clipDesc = clipData.getDescription();
1542 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1543 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001544 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001545 final CharSequence stringData = item.getText();
1546 final Uri uriData = item.getUri();
1547 final Intent intentData = item.getIntent();
1548 final String key =
1549 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1550 if (uriData != null) {
1551 intent.putExtra(key, uriData);
1552 } else if (intentData != null) {
1553 intent.putExtra(key, intentData);
1554 } else if (stringData != null) {
1555 intent.putExtra(key, stringData);
1556 }
1557 break;
1558 }
1559 }
1560 }
Winson Chung55cef262010-10-28 14:14:18 -07001561 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001562
Romain Guy8e633c52010-03-04 12:51:36 -08001563 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001564 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001565 // Otherwise just add it
Michael Jurka0280c3b2010-09-17 15:00:07 -07001566 completeAddAppWidget(appWidgetId, mAddScreen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001567
1568 // Exit spring loaded mode if necessary after adding the widget
1569 exitSpringLoadedDragModeDelayed(false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
1571 }
Romain Guycbb89e42009-06-08 15:52:54 -07001572
Adam Cohenfbba09b2011-07-18 21:43:05 -07001573 /**
1574 * Process a shortcut drop.
1575 *
1576 * @param componentName The name of the component
1577 * @param screen The screen where it should be added
1578 * @param cell The cell it should be added to, optional
1579 * @param position The location on the screen where it was dropped, optional
1580 */
1581 void processShortcutFromDrop(ComponentName componentName, int screen, int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001582 resetAddInfo();
1583 mAddScreen = screen;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001584 mAddDropPosition = loc;
1585
1586 if (cell != null) {
1587 mAddCellX = cell[0];
1588 mAddCellY = cell[1];
1589 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001590
1591 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1592 createShortcutIntent.setComponent(componentName);
1593 processShortcut(createShortcutIntent);
1594 }
1595
Adam Cohenfbba09b2011-07-18 21:43:05 -07001596 /**
1597 * Process a widget drop.
1598 *
1599 * @param info The PendingAppWidgetInfo of the widget being added.
1600 * @param screen The screen where it should be added
1601 * @param cell The cell it should be added to, optional
1602 * @param position The location on the screen where it was dropped, optional
1603 */
1604 void addAppWidgetFromDrop(PendingAddWidgetInfo info, int screen, int[] cell, int[] loc) {
1605 resetAddInfo();
1606 mAddScreen = screen;
1607 mAddDropPosition = loc;
1608
1609 if (cell != null) {
1610 mAddCellX = cell[0];
1611 mAddCellY = cell[1];
1612 }
1613
1614 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1615 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1616 addAppWidgetImpl(appWidgetId, info);
1617 }
1618
Joe Onoratodeb98af2010-02-19 14:59:39 -08001619 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001620 // Handle case where user selected "Applications"
1621 String applicationName = getResources().getString(R.string.group_applications);
1622 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001623
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001624 if (applicationName != null && applicationName.equals(shortcutName)) {
1625 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1626 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001627
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001628 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1629 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001630 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001631 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001632 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001633 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001634 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 }
1636
Winson Chung24ab2f12010-09-16 14:10:47 -07001637 void processWallpaper(Intent intent) {
1638 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1639 }
1640
Adam Cohenfbba09b2011-07-18 21:43:05 -07001641 FolderIcon addFolder(final int screen, int cellX, int cellY) {
Adam Cohend0445262011-07-04 23:53:22 -07001642 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 folderInfo.title = getText(R.string.folder_name);
1644
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 // Update the model
Adam Cohend0445262011-07-04 23:53:22 -07001646 LauncherModel.addItemToDatabase(Launcher.this, folderInfo,
Joe Onorato9c1289c2009-08-17 11:03:03 -04001647 LauncherSettings.Favorites.CONTAINER_DESKTOP,
Adam Cohenfbba09b2011-07-18 21:43:05 -07001648 screen, cellX, cellY, false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001649 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650
1651 // Create the view
1652 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurkac9a96192010-11-01 11:52:08 -07001653 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()),
1654 folderInfo, mIconCache);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001655 mWorkspace.addInScreen(newFolder, screen, cellX, cellY, 1, 1, isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001656 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 }
Romain Guycbb89e42009-06-08 15:52:54 -07001658
Joe Onorato9c1289c2009-08-17 11:03:03 -04001659 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001660 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001661 }
1662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 private void showNotifications() {
1664 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1665 if (statusBar != null) {
1666 statusBar.expand();
1667 }
1668 }
1669
1670 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001671 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001672 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001673 Intent chooser = Intent.createChooser(pickWallpaper,
1674 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001675 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1676 // Removed in Eclair MR1
1677// WallpaperManager wm = (WallpaperManager)
1678// getSystemService(Context.WALLPAPER_SERVICE);
1679// WallpaperInfo wi = wm.getWallpaperInfo();
1680// if (wi != null && wi.getSettingsActivity() != null) {
1681// LabeledIntent li = new LabeledIntent(getPackageName(),
1682// R.string.configure_wallpaper, 0);
1683// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1684// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1685// }
Mike Clerona0618e42009-10-22 13:55:21 -07001686 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 }
1688
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001689 /**
1690 * Registers various content observers. The current implementation registers
1691 * only a favorites observer to keep track of the favorites applications.
1692 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001693 private void registerContentObservers() {
1694 ContentResolver resolver = getContentResolver();
1695 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1696 true, mWidgetObserver);
1697 }
1698
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 @Override
1700 public boolean dispatchKeyEvent(KeyEvent event) {
1701 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1702 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001704 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001705 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001706 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001707 dumpState();
1708 return true;
1709 }
1710 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001711 }
1712 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1713 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001714 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 return true;
1716 }
1717 }
1718
1719 return super.dispatchKeyEvent(event);
1720 }
1721
Joe Onorato88ec0992009-11-19 13:16:06 -08001722 @Override
1723 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001724 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001725 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001726 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001727 Folder openFolder = mWorkspace.getOpenFolder();
1728 if (openFolder.isEditingName()) {
1729 openFolder.dismissEditingName();
1730 } else {
1731 closeFolder();
1732 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001733 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001734 mWorkspace.exitWidgetResizeMode();
1735
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001736 // Back button is a no-op here, but give at least some feedback for the button press
1737 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001738 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001739 }
1740
Adam Cohen2801caf2011-05-13 20:57:39 -07001741 public void closeFolder() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 Folder folder = mWorkspace.getOpenFolder();
1743 if (folder != null) {
1744 closeFolder(folder);
1745 }
1746 }
1747
1748 void closeFolder(Folder folder) {
1749 folder.getInfo().opened = false;
Adam Cohen7f4eabe2011-04-21 16:19:16 -07001750
Michael Jurka8c920dd2011-01-20 14:16:56 -08001751 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 if (parent != null) {
Adam Cohen8e776a62011-06-28 18:10:06 -07001753 CellLayout cl = (CellLayout) mWorkspace.getChildAt(folder.mInfo.screen);
Adam Cohen2801caf2011-05-13 20:57:39 -07001754 FolderIcon fi = (FolderIcon) cl.getChildAt(folder.mInfo.cellX, folder.mInfo.cellY);
1755 shrinkAndFadeInFolderIcon(fi);
Adam Cohendf2cc412011-04-27 16:56:57 -07001756 mDragController.removeDropTarget((DropTarget)folder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 }
Adam Cohendf2cc412011-04-27 16:56:57 -07001758 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 }
1760
1761 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001762 * Re-listen when widgets are reset.
1763 */
1764 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001765 if (mAppWidgetHost != null) {
1766 mAppWidgetHost.startListening();
1767 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001768 }
1769
1770 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001771 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1772 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 */
Adam Cohen4eac29a2011-07-11 17:53:37 -07001774 private void unbindWorkspaceItems() {
1775 LauncherModel.unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001777
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 /**
1779 * Launches the intent referred by the clicked shortcut.
1780 *
1781 * @param v The view representing the clicked shortcut.
1782 */
1783 public void onClick(View v) {
1784 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001785 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001787 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001788 int[] pos = new int[2];
1789 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001790 intent.setSourceBounds(new Rect(pos[0], pos[1],
1791 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001792 boolean success = startActivitySafely(intent, tag);
1793
1794 if (success && v instanceof BubbleTextView) {
1795 mWaitingForResume = (BubbleTextView) v;
1796 mWaitingForResume.setStayPressed(true);
1797 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001799 if (v instanceof FolderIcon) {
1800 FolderIcon fi = (FolderIcon) v;
1801 handleFolderClick(fi);
1802 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001803 } else if (v == mAllAppsButton) {
1804 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001805 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001806 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001807 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001808 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 }
1810 }
1811
Michael Jurka0e260592010-06-30 17:07:39 -07001812 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001813 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001814 // clicking anywhere on the workspace causes the customization drawer to slide down
1815 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001816 return false;
1817 }
1818
Michael Jurkaaf442092010-06-10 17:01:57 -07001819 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001820 * Event handler for the search button
1821 *
1822 * @param v The view that was clicked.
1823 */
1824 public void onClickSearchButton(View v) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07001825 startSearch(null, false, null, true);
Amith Yamasani1f878a12010-11-22 14:58:22 -08001826 // Use a custom animation for launching search
1827 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001828 }
1829
1830 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001831 * Event handler for the voice button
1832 *
1833 * @param v The view that was clicked.
1834 */
1835 public void onClickVoiceButton(View v) {
1836 startVoiceSearch();
1837 }
1838
1839 private void startVoiceSearch() {
1840 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
1841 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1842 startActivity(intent);
1843 }
1844
1845 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001846 * Event handler for the "gear" button that appears on the home screen, which
Michael Jurkaaf442092010-06-10 17:01:57 -07001847 * enters home screen customization mode.
1848 *
1849 * @param v The view that was clicked.
1850 */
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001851 public void onClickConfigureButton(View v) {
Patrick Dubroy558654c2010-07-23 16:48:11 -07001852 addItems();
Michael Jurkaaf442092010-06-10 17:01:57 -07001853 }
1854
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001855 /**
1856 * Event handler for the "grid" button that appears on the home screen, which
1857 * enters all apps mode.
1858 *
1859 * @param v The view that was clicked.
1860 */
1861 public void onClickAllAppsButton(View v) {
1862 showAllApps(true);
1863 }
1864
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001865 public void onClickAppMarketButton(View v) {
1866 if (mAppMarketIntent != null) {
1867 startActivitySafely(mAppMarketIntent, "app market");
1868 }
1869 }
1870
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001871 void startApplicationDetailsActivity(ComponentName componentName) {
1872 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001873 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1874 Uri.fromParts("package", packageName, null));
1875 startActivity(intent);
1876 }
1877
Patrick Dubroy5539af72010-09-07 15:22:01 -07001878 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1879 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1880 // System applications cannot be installed. For now, show a toast explaining that.
1881 // We may give them the option of disabling apps this way.
1882 int messageId = R.string.uninstall_system_app_text;
1883 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1884 } else {
1885 String packageName = appInfo.componentName.getPackageName();
1886 String className = appInfo.componentName.getClassName();
1887 Intent intent = new Intent(
1888 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
1889 startActivity(intent);
1890 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001891 }
1892
Michael Jurkaddd62e92011-02-16 17:49:14 -08001893 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1895 try {
1896 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001897 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 } catch (ActivityNotFoundException e) {
1899 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001900 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 } catch (SecurityException e) {
1902 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001903 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001904 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001905 "or use the exported attribute for this activity. "
1906 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001908 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001910
Romain Guy8e633c52010-03-04 12:51:36 -08001911 void startActivityForResultSafely(Intent intent, int requestCode) {
1912 try {
1913 startActivityForResult(intent, requestCode);
1914 } catch (ActivityNotFoundException e) {
1915 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1916 } catch (SecurityException e) {
1917 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1918 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1919 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1920 "or use the exported attribute for this activity.", e);
1921 }
1922 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923
Adam Cohena9cf38f2011-05-02 15:36:58 -07001924 private void handleFolderClick(FolderIcon folderIcon) {
1925 final FolderInfo info = folderIcon.mInfo;
1926 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 // Close any open folder
1928 closeFolder();
1929 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001930 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 } else {
1932 // Find the open folder...
Adam Cohena9cf38f2011-05-02 15:36:58 -07001933 Folder openFolder = mWorkspace.getFolderForTag(info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001934 int folderScreen;
1935 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001936 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 // .. and close it
1938 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001939 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001940 // Close any folder open on the current screen
1941 closeFolder();
1942 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001943 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 }
1945 }
1946 }
1947 }
1948
Adam Cohen2801caf2011-05-13 20:57:39 -07001949 private void growAndFadeOutFolderIcon(FolderIcon fi) {
1950 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1951 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1952 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1953
1954 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1955 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1956 oa.start();
1957 }
1958
1959 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
1960 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1961 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1962 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1963
1964 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1965 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1966 oa.start();
1967 }
1968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001970 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 * is animated relative to the specified View. If the View is null, no animation
1972 * is played.
1973 *
1974 * @param folderInfo The FolderInfo describing the folder to open.
1975 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001976 public void openFolder(FolderIcon folderIcon) {
1977 Folder folder = folderIcon.mFolder;
1978 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979
Adam Cohen2801caf2011-05-13 20:57:39 -07001980 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001981 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982
Adam Cohen8e776a62011-06-28 18:10:06 -07001983 mDragLayer.addView(folder);
1984 mDragController.addDropTarget((DropTarget) folder);
1985
Adam Cohena9cf38f2011-05-02 15:36:58 -07001986 folder.animateOpen();
1987 folder.onOpen();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 }
1989
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001991 if (mState != State.WORKSPACE) {
1992 return false;
1993 }
1994
Romain Guy1fbc1c82009-11-09 20:43:08 -08001995 switch (v.getId()) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001996 case R.id.all_apps_button:
Winson Chungf0ea4d32011-06-06 14:27:16 -07001997 if (mState != State.APPS_CUSTOMIZE) {
Romain Guyf8e6a802009-12-07 17:48:02 -08001998 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1999 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2000 showPreviews(v);
Joe Onorato0d44e942009-11-16 18:20:51 -08002001 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002002 return true;
2003 }
2004
Joe Onorato9c1289c2009-08-17 11:03:03 -04002005 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 return false;
2007 }
2008
2009 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002010 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 }
2012
Michael Jurka0280c3b2010-09-17 15:00:07 -07002013 resetAddInfo();
2014 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 // This happens when long clicking an item with the dpad/trackball
Michael Jurka0280c3b2010-09-17 15:00:07 -07002016 if (longClickCellInfo == null || !longClickCellInfo.valid) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 return true;
2018 }
2019
Michael Jurka0280c3b2010-09-17 15:00:07 -07002020 final View itemUnderLongClick = longClickCellInfo.cell;
2021
Winson Chung304dcde2011-01-07 11:17:23 -08002022 if (mWorkspace.allowLongPress() && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002023 if (itemUnderLongClick == null) {
2024 // User long pressed on empty space
2025 mWorkspace.setAllowLongPress(false);
2026 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2027 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung55b65502011-05-26 12:03:43 -07002028 addItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002030 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 // User long pressed on an item
Joe Onorato0d44e942009-11-16 18:20:51 -08002032 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2033 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002034 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 }
2036 }
2037 }
2038 return true;
2039 }
2040
Romain Guye6b8e2f2009-11-10 11:56:55 -08002041 @SuppressWarnings({"unchecked"})
Romain Guy9d31e9f2009-11-11 18:54:28 -08002042 private void dismissPreview(final View v) {
2043 final PopupWindow window = (PopupWindow) v.getTag();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002044 if (window != null) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002045 window.setOnDismissListener(new PopupWindow.OnDismissListener() {
2046 public void onDismiss() {
2047 ViewGroup group = (ViewGroup) v.getTag(R.id.workspace);
2048 int count = group.getChildCount();
2049 for (int i = 0; i < count; i++) {
2050 ((ImageView) group.getChildAt(i)).setImageDrawable(null);
2051 }
Michael Jurka25913ca2011-05-04 17:45:33 -07002052 ArrayList<Bitmap> bitmaps =
2053 (ArrayList<Bitmap>) v.getTag(R.id.all_apps_button_cluster);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002054 for (Bitmap bitmap : bitmaps) bitmap.recycle();
2055
2056 v.setTag(R.id.workspace, null);
Michael Jurka25913ca2011-05-04 17:45:33 -07002057 v.setTag(R.id.all_apps_button_cluster, null);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002058 window.setOnDismissListener(null);
2059 }
2060 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002061 window.dismiss();
Romain Guy1fbc1c82009-11-09 20:43:08 -08002062 }
2063 v.setTag(null);
2064 }
2065
Romain Guyf8e6a802009-12-07 17:48:02 -08002066 private void showPreviews(View anchor) {
Romain Guy9d31e9f2009-11-11 18:54:28 -08002067 showPreviews(anchor, 0, mWorkspace.getChildCount());
Romain Guy1fbc1c82009-11-09 20:43:08 -08002068 }
2069
Romain Guya6abce82009-11-10 02:54:41 -08002070 private void showPreviews(final View anchor, int start, int end) {
Romain Guyf8e6a802009-12-07 17:48:02 -08002071 final Resources resources = getResources();
2072 final Workspace workspace = mWorkspace;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002073
Romain Guya6abce82009-11-10 02:54:41 -08002074 CellLayout cell = ((CellLayout) workspace.getChildAt(start));
Winson Chungaafa03c2010-06-11 17:34:16 -07002075
Romain Guy9d31e9f2009-11-11 18:54:28 -08002076 float max = workspace.getChildCount();
Winson Chungaafa03c2010-06-11 17:34:16 -07002077
Romain Guyf8e6a802009-12-07 17:48:02 -08002078 final Rect r = new Rect();
Romain Guy9d31e9f2009-11-11 18:54:28 -08002079 resources.getDrawable(R.drawable.preview_background).getPadding(r);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002080 int extraW = (int) ((r.left + r.right) * max);
2081 int extraH = r.top + r.bottom;
Romain Guya6abce82009-11-10 02:54:41 -08002082
2083 int aW = cell.getWidth() - extraW;
2084 float w = aW / max;
2085
2086 int width = cell.getWidth();
2087 int height = cell.getHeight();
Winson Chung4b825dcd2011-06-19 12:41:22 -07002088 int x = cell.getPaddingLeft();
2089 int y = cell.getPaddingTop();
2090 width -= (x + cell.getPaddingRight());
2091 height -= (y + cell.getPaddingBottom());
Romain Guya6abce82009-11-10 02:54:41 -08002092
2093 float scale = w / width;
2094
2095 int count = end - start;
2096
2097 final float sWidth = width * scale;
2098 float sHeight = height * scale;
2099
Romain Guye6b8e2f2009-11-10 11:56:55 -08002100 LinearLayout preview = new LinearLayout(this);
Romain Guya6abce82009-11-10 02:54:41 -08002101
Romain Guye6b8e2f2009-11-10 11:56:55 -08002102 PreviewTouchHandler handler = new PreviewTouchHandler(anchor);
2103 ArrayList<Bitmap> bitmaps = new ArrayList<Bitmap>(count);
Romain Guya6abce82009-11-10 02:54:41 -08002104
2105 for (int i = start; i < end; i++) {
Romain Guye6b8e2f2009-11-10 11:56:55 -08002106 ImageView image = new ImageView(this);
Romain Guya6abce82009-11-10 02:54:41 -08002107 cell = (CellLayout) workspace.getChildAt(i);
2108
Romain Guyf8e6a802009-12-07 17:48:02 -08002109 final Bitmap bitmap = Bitmap.createBitmap((int) sWidth, (int) sHeight,
Romain Guye6b8e2f2009-11-10 11:56:55 -08002110 Bitmap.Config.ARGB_8888);
Romain Guyf8e6a802009-12-07 17:48:02 -08002111
2112 final Canvas c = new Canvas(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002113 c.scale(scale, scale);
Winson Chung4b825dcd2011-06-19 12:41:22 -07002114 c.translate(-cell.getPaddingLeft(), -cell.getPaddingTop());
Patrick Dubroy1262e362010-10-06 15:49:50 -07002115 cell.drawChildren(c);
Romain Guya6abce82009-11-10 02:54:41 -08002116
Romain Guy9d31e9f2009-11-11 18:54:28 -08002117 image.setBackgroundDrawable(resources.getDrawable(R.drawable.preview_background));
Romain Guye6b8e2f2009-11-10 11:56:55 -08002118 image.setImageBitmap(bitmap);
2119 image.setTag(i);
2120 image.setOnClickListener(handler);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002121 image.setOnFocusChangeListener(handler);
2122 image.setFocusable(true);
Michael Jurka0142d492010-08-25 17:46:15 -07002123 if (i == mWorkspace.getCurrentPage()) image.requestFocus();
Romain Guye6b8e2f2009-11-10 11:56:55 -08002124
2125 preview.addView(image,
Romain Guy9d31e9f2009-11-11 18:54:28 -08002126 LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
2127
Winson Chungaafa03c2010-06-11 17:34:16 -07002128 bitmaps.add(bitmap);
Romain Guya6abce82009-11-10 02:54:41 -08002129 }
Romain Guyf8e6a802009-12-07 17:48:02 -08002130
2131 final PopupWindow p = new PopupWindow(this);
Romain Guye6b8e2f2009-11-10 11:56:55 -08002132 p.setContentView(preview);
2133 p.setWidth((int) (sWidth * count + extraW));
2134 p.setHeight((int) (sHeight + extraH));
2135 p.setAnimationStyle(R.style.AnimationPreview);
2136 p.setOutsideTouchable(true);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002137 p.setFocusable(true);
Romain Guyff0c2e22009-11-10 12:09:59 -08002138 p.setBackgroundDrawable(new ColorDrawable(0));
Romain Guy9d31e9f2009-11-11 18:54:28 -08002139 p.showAsDropDown(anchor, 0, 0);
Romain Guya6abce82009-11-10 02:54:41 -08002140
Romain Guye6b8e2f2009-11-10 11:56:55 -08002141 p.setOnDismissListener(new PopupWindow.OnDismissListener() {
2142 public void onDismiss() {
2143 dismissPreview(anchor);
2144 }
2145 });
Romain Guy1fbc1c82009-11-09 20:43:08 -08002146
2147 anchor.setTag(p);
2148 anchor.setTag(R.id.workspace, preview);
Michael Jurka25913ca2011-05-04 17:45:33 -07002149 anchor.setTag(R.id.all_apps_button_cluster, bitmaps);
Romain Guy1fbc1c82009-11-09 20:43:08 -08002150 }
2151
Romain Guy9d31e9f2009-11-11 18:54:28 -08002152 class PreviewTouchHandler implements View.OnClickListener, Runnable, View.OnFocusChangeListener {
Romain Guya6abce82009-11-10 02:54:41 -08002153 private final View mAnchor;
Romain Guya6abce82009-11-10 02:54:41 -08002154
Romain Guye6b8e2f2009-11-10 11:56:55 -08002155 public PreviewTouchHandler(View anchor) {
Romain Guya6abce82009-11-10 02:54:41 -08002156 mAnchor = anchor;
Romain Guya6abce82009-11-10 02:54:41 -08002157 }
2158
2159 public void onClick(View v) {
Michael Jurka0142d492010-08-25 17:46:15 -07002160 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002161 v.post(this);
2162 }
2163
2164 public void run() {
Winson Chungaafa03c2010-06-11 17:34:16 -07002165 dismissPreview(mAnchor);
Romain Guy9d31e9f2009-11-11 18:54:28 -08002166 }
2167
2168 public void onFocusChange(View v, boolean hasFocus) {
2169 if (hasFocus) {
Michael Jurka0142d492010-08-25 17:46:15 -07002170 mWorkspace.snapToPage((Integer) v.getTag());
Romain Guy9d31e9f2009-11-11 18:54:28 -08002171 }
Romain Guya6abce82009-11-10 02:54:41 -08002172 }
2173 }
2174
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002175 Workspace getWorkspace() {
2176 return mWorkspace;
2177 }
2178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179 @Override
2180 protected Dialog onCreateDialog(int id) {
2181 switch (id) {
2182 case DIALOG_CREATE_SHORTCUT:
2183 return new CreateShortcut().createDialog();
2184 case DIALOG_RENAME_FOLDER:
2185 return new RenameFolder().createDialog();
2186 }
2187
2188 return super.onCreateDialog(id);
2189 }
2190
2191 @Override
2192 protected void onPrepareDialog(int id, Dialog dialog) {
2193 switch (id) {
2194 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 break;
2196 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002197 if (mFolderInfo != null) {
2198 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2199 final CharSequence text = mFolderInfo.title;
2200 input.setText(text);
2201 input.setSelection(0, text.length());
2202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 break;
2204 }
2205 }
2206
2207 void showRenameDialog(FolderInfo info) {
2208 mFolderInfo = info;
2209 mWaitingForResult = true;
2210 showDialog(DIALOG_RENAME_FOLDER);
2211 }
2212
Adam Cohenfbba09b2011-07-18 21:43:05 -07002213 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002214 resetAddInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -07002215 mAddScreen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 mWaitingForResult = true;
2217 showDialog(DIALOG_CREATE_SHORTCUT);
2218 }
2219
Joe Onoratodeb98af2010-02-19 14:59:39 -08002220 private void pickShortcut() {
Michael Jurka0e260592010-06-30 17:07:39 -07002221 // Insert extra item to handle picking application
Romain Guy73b979d2009-06-09 12:57:21 -07002222 Bundle bundle = new Bundle();
2223
2224 ArrayList<String> shortcutNames = new ArrayList<String>();
2225 shortcutNames.add(getString(R.string.group_applications));
2226 bundle.putStringArrayList(Intent.EXTRA_SHORTCUT_NAME, shortcutNames);
2227
2228 ArrayList<ShortcutIconResource> shortcutIcons = new ArrayList<ShortcutIconResource>();
2229 shortcutIcons.add(ShortcutIconResource.fromContext(Launcher.this,
2230 R.drawable.ic_launcher_application));
2231 bundle.putParcelableArrayList(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, shortcutIcons);
2232
2233 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2234 pickIntent.putExtra(Intent.EXTRA_INTENT, new Intent(Intent.ACTION_CREATE_SHORTCUT));
Joe Onoratodeb98af2010-02-19 14:59:39 -08002235 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_shortcut));
Romain Guy73b979d2009-06-09 12:57:21 -07002236 pickIntent.putExtras(bundle);
2237
Joe Onoratodeb98af2010-02-19 14:59:39 -08002238 startActivityForResult(pickIntent, REQUEST_PICK_SHORTCUT);
Romain Guy73b979d2009-06-09 12:57:21 -07002239 }
2240
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 private class RenameFolder {
2242 private EditText mInput;
2243
2244 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002245 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2246 mInput = (EditText) layout.findViewById(R.id.folder_name);
2247
2248 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2249 builder.setIcon(0);
2250 builder.setTitle(getString(R.string.rename_folder_title));
2251 builder.setCancelable(true);
2252 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2253 public void onCancel(DialogInterface dialog) {
2254 cleanup();
2255 }
2256 });
2257 builder.setNegativeButton(getString(R.string.cancel_action),
2258 new Dialog.OnClickListener() {
2259 public void onClick(DialogInterface dialog, int which) {
2260 cleanup();
2261 }
2262 }
2263 );
2264 builder.setPositiveButton(getString(R.string.rename_action),
2265 new Dialog.OnClickListener() {
2266 public void onClick(DialogInterface dialog, int which) {
2267 changeFolderName();
2268 }
2269 }
2270 );
2271 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002272
2273 final AlertDialog dialog = builder.create();
2274 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2275 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002276 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002277 mInput.requestFocus();
2278 InputMethodManager inputManager = (InputMethodManager)
2279 getSystemService(Context.INPUT_METHOD_SERVICE);
2280 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002281 }
2282 });
2283
2284 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002285 }
2286
2287 private void changeFolderName() {
2288 final String name = mInput.getText().toString();
2289 if (!TextUtils.isEmpty(name)) {
2290 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002291 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002292 mFolderInfo.title = name;
2293 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2294
Joe Onorato9c1289c2009-08-17 11:03:03 -04002295 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002296 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002297 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002298 } else {
2299 final FolderIcon folderIcon = (FolderIcon)
2300 mWorkspace.getViewForTag(mFolderInfo);
2301 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002302 // TODO: At some point we'll probably want some version of setting
2303 // the text for a folder icon.
2304 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002305 getWorkspace().requestLayout();
2306 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002307 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002308 mWorkspaceLoading = true;
2309 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002310 }
2311 }
2312 }
2313 cleanup();
2314 }
2315
2316 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002317 dismissDialog(DIALOG_RENAME_FOLDER);
2318 mWaitingForResult = false;
2319 mFolderInfo = null;
2320 }
2321 }
2322
Daniel Sandler843e8602010-06-07 14:59:01 -04002323 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2324 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002325 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002326 }
2327
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002328 // AllAppsView.Watcher
2329 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002330 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002331 mWorkspace.setVisibility(View.GONE);
2332 }
2333 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002334
2335 /**
2336 * Helper method for the cameraZoomIn/cameraZoomOut animations
2337 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002338 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002339 * @param scaleFactor The scale factor used for the zoom
2340 */
2341 private void setPivotsForZoom(View view, State state, float scaleFactor) {
2342 final int height = view.getHeight();
Adam Cohen61033d32010-11-15 18:29:44 -08002343
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002344 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen61033d32010-11-15 18:29:44 -08002345 // Set pivotY so that at the starting zoom factor, the view is partially
2346 // visible. Modifying initialHeightFactor changes how much of the view is
2347 // initially showing, and hence the perceived angle from which the view enters.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002348 if (state == State.APPS_CUSTOMIZE) {
Michael Jurkaea573482011-02-03 19:48:18 -08002349 final float initialHeightFactor = 0.175f;
Adam Cohenf16e5712011-01-13 13:31:45 -08002350 view.setPivotY((1 - initialHeightFactor) * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002351 } else {
Adam Cohenf16e5712011-01-13 13:31:45 -08002352 final float initialHeightFactor = 0.2f;
Adam Cohen61033d32010-11-15 18:29:44 -08002353 view.setPivotY(-initialHeightFactor * height);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002354 }
2355 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002356
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002357 /**
2358 * Zoom the camera out from the workspace to reveal 'toView'.
2359 * Assumes that the view to show is anchored at either the very top or very bottom
2360 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002361 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002362 */
Winson Chungc07918d2011-07-01 15:35:26 -07002363 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002364 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002365
Winson Chungf0ea4d32011-06-06 14:27:16 -07002366 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2367 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2368 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2369 final View toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07002370
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002371 setPivotsForZoom(toView, toState, scale);
2372
Michael Jurkad74c9842011-07-10 12:44:21 -07002373 // Shrink workspaces away if going to AppsCustomize from workspace
2374 mWorkspace.shrink(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002375
2376 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002377 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002378 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002379 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2380 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002381 ((View) toView.getParent()).fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002382 toView.setFastScaleX(a * scale + b * 1f);
2383 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002384 }
2385 });
Adam Cohen61033d32010-11-15 18:29:44 -08002386
Winson Chungf0ea4d32011-06-06 14:27:16 -07002387 toView.setVisibility(View.VISIBLE);
2388 toView.setFastAlpha(0f);
2389 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2390 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2391 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2392 public void onAnimationUpdate(float a, float b) {
2393 // don't need to invalidate because we do so above
2394 toView.setFastAlpha(a * 0f + b * 1f);
2395 }
2396 });
2397 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002398
Michael Jurkaabded662011-03-04 12:06:57 -08002399 if (toView instanceof LauncherTransitionable) {
2400 ((LauncherTransitionable) toView).onLauncherTransitionStart(scaleAnim);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002401 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002402 scaleAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002403 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002404 public void onAnimationStart(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002405 // Prepare the position
2406 toView.setTranslationX(0.0f);
2407 toView.setTranslationY(0.0f);
2408 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002409 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002410
Winson Chungc07918d2011-07-01 15:35:26 -07002411 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2412 // Hide the workspace scrollbar
2413 mWorkspace.hideScrollingIndicator(true);
2414 mWorkspace.hideScrollIndicatorTrack();
2415 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002416 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002417 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002418 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002419 // If we don't set the final scale values here, if this animation is cancelled
2420 // it will have the wrong scale value and subsequent cameraPan animations will
2421 // not fix that
2422 toView.setScaleX(1.0f);
2423 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002424 if (toView instanceof LauncherTransitionable) {
2425 ((LauncherTransitionable) toView).onLauncherTransitionEnd(scaleAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002426 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002427 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002428 });
2429
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002430 // toView should appear right at the end of the workspace shrink animation
Adam Cohenf16e5712011-01-13 13:31:45 -08002431 final int startDelay = 0;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002432
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002433 if (mStateAnimation != null) mStateAnimation.cancel();
2434 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002435 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002436 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002437 } else {
2438 toView.setTranslationX(0.0f);
2439 toView.setTranslationY(0.0f);
2440 toView.setScaleX(1.0f);
2441 toView.setScaleY(1.0f);
2442 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002443 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002444 if (toView instanceof LauncherTransitionable) {
2445 ((LauncherTransitionable) toView).onLauncherTransitionStart(null);
2446 ((LauncherTransitionable) toView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002447
Winson Chungc07918d2011-07-01 15:35:26 -07002448 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2449 // Hide the workspace scrollbar
2450 mWorkspace.hideScrollingIndicator(true);
2451 mWorkspace.hideScrollIndicatorTrack();
2452 }
Michael Jurkaabded662011-03-04 12:06:57 -08002453 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002454 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002455 }
2456
2457 /**
2458 * Zoom the camera back into the workspace, hiding 'fromView'.
2459 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002460 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002461 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002462 */
Winson Chungc07918d2011-07-01 15:35:26 -07002463 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002464 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002465
Winson Chungf0ea4d32011-06-06 14:27:16 -07002466 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2467 final float scaleFactor = (float)
2468 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2469 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002470
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002471 setPivotsForZoom(fromView, fromState, scaleFactor);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002472
Michael Jurkad3ef3062010-11-23 16:23:58 -08002473 if (!springLoaded) {
2474 mWorkspace.unshrink(animated);
2475 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002476 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002477 if (mStateAnimation != null) mStateAnimation.cancel();
2478 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002479
Michael Jurka742574b2011-02-02 23:51:01 -08002480 final float oldScaleX = fromView.getScaleX();
2481 final float oldScaleY = fromView.getScaleY();
2482
2483 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2484 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002485 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2486 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002487 ((View)fromView.getParent()).fastInvalidate();
2488 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2489 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2490 }
2491 });
Michael Jurkaabded662011-03-04 12:06:57 -08002492 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002493 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Michael Jurkaea573482011-02-03 19:48:18 -08002494 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Michael Jurkac2e26a02011-03-24 15:20:26 -07002495 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2496 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002497 // don't need to invalidate because we do so above
2498 fromView.setFastAlpha(a * 1f + b * 0f);
2499 }
2500 });
Michael Jurkaabded662011-03-04 12:06:57 -08002501 if (fromView instanceof LauncherTransitionable) {
2502 ((LauncherTransitionable) fromView).onLauncherTransitionStart(alphaAnim);
Michael Jurka2763be32011-02-24 11:19:57 -08002503 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002504 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002505 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002506 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002507 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002508 if (fromView instanceof LauncherTransitionable) {
2509 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(alphaAnim);
Winson Chung3ac74c52011-06-30 17:39:37 -07002510
Winson Chungc07918d2011-07-01 15:35:26 -07002511 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2512 // Show the workspace scrollbar
2513 mWorkspace.showScrollIndicatorTrack();
2514 mWorkspace.flashScrollingIndicator();
2515 }
Michael Jurka2763be32011-02-24 11:19:57 -08002516 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002517 }
2518 });
2519
Winson Chungf0ea4d32011-06-06 14:27:16 -07002520 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002521 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002522 } else {
2523 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002524 if (fromView instanceof LauncherTransitionable) {
2525 ((LauncherTransitionable) fromView).onLauncherTransitionStart(null);
2526 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(null);
Winson Chung3ac74c52011-06-30 17:39:37 -07002527
Winson Chungc07918d2011-07-01 15:35:26 -07002528 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2529 // Show the workspace scrollbar
2530 mWorkspace.showScrollIndicatorTrack();
2531 mWorkspace.flashScrollingIndicator();
2532 }
Michael Jurkaabded662011-03-04 12:06:57 -08002533 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002534 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002535 }
2536
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002537 void showWorkspace(boolean animated) {
2538 showWorkspace(animated, null);
2539 }
2540
2541 void showWorkspace(boolean animated, CellLayout layout) {
Michael Jurka9c6de3d2010-11-23 16:23:58 -08002542 if (layout != null) {
2543 // always animated, but that's ok since we never specify a layout and
2544 // want no animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002545 mWorkspace.unshrink(layout);
2546 } else {
2547 mWorkspace.unshrink(animated);
2548 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002549 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002550 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002551 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002552
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002553 // Change the state *after* we've called all the transition code
2554 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002555
Winson Chung5fb63472011-02-02 17:03:37 -08002556 // Resume the auto-advance of widgets
2557 mUserPresent = true;
2558 updateRunning();
2559
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002560 // send an accessibility event to announce the context change
2561 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002562 }
2563
Michael Jurkad3ef3062010-11-23 16:23:58 -08002564 void enterSpringLoadedDragMode(CellLayout layout) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002565 if (mState == State.APPS_CUSTOMIZE) {
Winson Chungc07918d2011-07-01 15:35:26 -07002566 mWorkspace.enterSpringLoadedDragMode(layout);
Winson Chungb26f3d62011-06-02 10:49:29 -07002567 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002568 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002569 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002570 // Otherwise, we are not in spring loaded mode, so don't do anything.
Michael Jurkad3ef3062010-11-23 16:23:58 -08002571 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002572 void exitSpringLoadedDragModeDelayed(boolean extendedDelay) {
2573 mWorkspace.postDelayed(new Runnable() {
2574 @Override
2575 public void run() {
2576 exitSpringLoadedDragMode();
2577 }
2578 }, (extendedDelay ?
2579 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2580 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2581 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002582 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002583 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkad74c9842011-07-10 12:44:21 -07002584 mWorkspace.exitSpringLoadedDragMode(Workspace.State.SMALL);
Winson Chungb26f3d62011-06-02 10:49:29 -07002585 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2586 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002587 }
2588 // Otherwise, we are not in spring loaded mode, so don't do anything.
2589 }
2590
2591 /**
2592 * Shows the dock/hotseat area.
2593 */
2594 void showDock(boolean animated) {
2595 if (!LauncherApplication.isScreenLarge()) {
2596 if (animated) {
2597 int duration = mSearchDeleteBar.getTransitionInDuration();
2598 mButtonCluster.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002599 } else {
2600 mButtonCluster.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002601 }
2602 }
2603 }
2604
2605 /**
2606 * Hides the dock/hotseat area.
2607 */
2608 void hideDock(boolean animated) {
2609 if (!LauncherApplication.isScreenLarge()) {
2610 if (animated) {
2611 int duration = mSearchDeleteBar.getTransitionOutDuration();
2612 mButtonCluster.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002613 } else {
2614 mButtonCluster.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002615 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002616 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002617 }
2618
Winson Chung785d2eb2011-04-14 16:08:02 -07002619 void showAllApps(boolean animated) {
2620 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002621
Winson Chungf0ea4d32011-06-06 14:27:16 -07002622 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2623 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002624
Winson Chungf0ea4d32011-06-06 14:27:16 -07002625 // Hide the search bar and dock
2626 mSearchDeleteBar.hideSearchBar(animated);
2627 hideDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002628
Winson Chungf0ea4d32011-06-06 14:27:16 -07002629 // Change the state *after* we've called all the transition code
2630 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002631
2632 // Pause the auto-advance of widgets until we are out of AllApps
2633 mUserPresent = false;
2634 updateRunning();
2635
2636 // Send an accessibility event to announce the context change
2637 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2638 }
2639
Joe Onoratob2061212009-11-24 16:13:54 -05002640 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002641 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002642 * - Home from workspace
2643 * - from center screen
2644 * - from other screens
2645 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002646 * - from center screen
2647 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002648 * - Back 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 * - Launch app from workspace and quit
2652 * - with back
2653 * - with home
2654 * - Launch app from all apps and quit
2655 * - with back
2656 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002657 * - Go to a screen that's not the default, then all
2658 * apps, and launch and app, and go back
2659 * - with back
2660 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002661 * - On workspace, long press power and go back
2662 * - with back
2663 * - with home
2664 * - On all apps, long press power and go back
2665 * - with back
2666 * - with home
2667 * - On workspace, power off
2668 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002669 * - Launch an app and turn off the screen while in that app
2670 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002671 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002672 * - From all apps
2673 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002674 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2675 * - From all apps
2676 * - From the center workspace
2677 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002678 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002679 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002680 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2681 mWorkspace.setVisibility(View.VISIBLE);
2682 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002683
Winson Chungf0ea4d32011-06-06 14:27:16 -07002684 // Show the search bar and dock
2685 mSearchDeleteBar.showSearchBar(animated);
2686 showDock(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002687
Winson Chungf0ea4d32011-06-06 14:27:16 -07002688 // Set focus to the AppsCustomize button
2689 findViewById(R.id.all_apps_button).requestFocus();
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002690 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002691 }
2692
Joe Onorato7c312c12009-08-13 21:36:53 -07002693 void lockAllApps() {
2694 // TODO
2695 }
2696
2697 void unlockAllApps() {
2698 // TODO
2699 }
2700
Patrick Dubroy5f445422011-02-18 14:35:21 -08002701 /**
2702 * Add an item from all apps or customize onto the given workspace screen.
2703 * If layout is null, add to the current screen.
2704 */
2705 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002706 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2707 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002708 } else {
2709 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002710 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002711 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002712
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002713 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002714 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002715 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002716 // Look for the toolbar icon specified in the activity meta-data
2717 Bundle metaData = packageManager.getActivityInfo(
2718 activityName, PackageManager.GET_META_DATA).metaData;
2719 if (metaData != null) {
2720 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2721 if (iconResId != 0) {
2722 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002723 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002724 }
2725 }
2726 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002727 // This can happen if the activity defines an invalid drawable
2728 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2729 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002730 } catch (Resources.NotFoundException nfe) {
2731 // This can happen if the activity defines an invalid drawable
2732 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2733 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002734 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002735 return null;
2736 }
2737
2738 // if successful in getting icon, return it; otherwise, set button to use default drawable
2739 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2740 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2741 TextView button = (TextView) findViewById(buttonId);
2742 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2743
2744 // If we were unable to find the icon via the meta-data, use a generic one
2745 if (toolbarIcon == null) {
2746 button.setCompoundDrawablesWithIntrinsicBounds(fallbackDrawableId, 0, 0, 0);
2747 return null;
2748 } else {
2749 button.setCompoundDrawablesWithIntrinsicBounds(toolbarIcon, null, null, null);
2750 return toolbarIcon.getConstantState();
2751 }
2752 }
2753
2754 // if successful in getting icon, return it; otherwise, set button to use default drawable
2755 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2756 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2757 ImageView button = (ImageView) findViewById(buttonId);
2758 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2759
Michael Jurka0423dcf2010-10-05 14:56:18 -07002760 // If we were unable to find the icon via the meta-data, use a generic one
2761 if (toolbarIcon == null) {
2762 button.setImageResource(fallbackDrawableId);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002763 return null;
Michael Jurka0423dcf2010-10-05 14:56:18 -07002764 } else {
2765 button.setImageDrawable(toolbarIcon);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002766 return toolbarIcon.getConstantState();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002767 }
2768 }
2769
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002770 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2771 TextView button = (TextView) findViewById(buttonId);
2772 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2773 }
2774
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002775 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002776 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002777 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002778 }
2779
Michael Jurka0423dcf2010-10-05 14:56:18 -07002780 private void updateGlobalSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002781 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2782 final View searchDivider = findViewById(R.id.search_divider);
Winson Chung97d85d22011-04-13 11:27:36 -07002783
Winson Chung1cad91e2011-05-25 17:41:01 -07002784 final SearchManager searchManager =
2785 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2786 ComponentName activityName = searchManager.getGlobalSearchActivity();
2787 if (activityName != null) {
Mathew Inwood68524cd2011-07-01 13:59:38 +01002788 sGlobalSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002789 R.id.search_button, activityName, R.drawable.ic_search_normal_holo);
Winson Chung1cad91e2011-05-25 17:41:01 -07002790 searchButton.setVisibility(View.VISIBLE);
Winson Chung649723c2011-07-06 20:41:23 -07002791 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002792 } else {
2793 searchButton.setVisibility(View.GONE);
Winson Chung649723c2011-07-06 20:41:23 -07002794 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002795 }
2796 }
2797
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002798 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002799 updateButtonWithDrawable(R.id.search_button, d);
2800 }
2801
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002802 private void updateVoiceSearchIcon() {
Winson Chung1cad91e2011-05-25 17:41:01 -07002803 final View searchDivider = findViewById(R.id.search_divider);
2804 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002805
Winson Chung1cad91e2011-05-25 17:41:01 -07002806 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2807 ComponentName activityName = intent.resolveActivity(getPackageManager());
2808 if (activityName != null) {
2809 sVoiceSearchIcon = updateButtonWithIconFromExternalActivity(
Winson Chung649723c2011-07-06 20:41:23 -07002810 R.id.voice_button, activityName, R.drawable.ic_voice_search_holo);
2811 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002812 voiceButton.setVisibility(View.VISIBLE);
2813 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002814 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002815 voiceButton.setVisibility(View.GONE);
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002816 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002817 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002818
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002819 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002820 updateButtonWithDrawable(R.id.voice_button, d);
2821 }
2822
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002823 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002824 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002825 */
2826 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002827 final View marketButton = findViewById(R.id.market_button);
2828 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2829 // Find the app market activity by resolving an intent.
2830 // (If multiple app markets are installed, it will return the ResolverActivity.)
2831 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002832 if (activityName != null) {
Winson Chung785d2eb2011-04-14 16:08:02 -07002833 mAppMarketIntent = intent;
2834 sAppMarketIcon = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002835 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002836 marketButton.setVisibility(View.VISIBLE);
Winson Chungeb66b142011-06-16 13:14:22 -07002837
2838 // Remove the shop icon text in the Phone UI
2839 if (!LauncherApplication.isScreenLarge()) {
2840 ((TextView) marketButton).setText("");
2841 }
Winson Chung785d2eb2011-04-14 16:08:02 -07002842 } else {
2843 // We should hide and disable the view so that we don't try and restore the visibility
2844 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2845 marketButton.setVisibility(View.GONE);
2846 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002847 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002848 }
2849
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002850 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002851 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002852 }
2853
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002854 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002855 * Displays the shortcut creation dialog and launches, if necessary, the
2856 * appropriate activity.
2857 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002858 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002859 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2860 DialogInterface.OnShowListener {
2861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002862 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002863
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002864 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002865 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002866
Winson Chung55b65502011-05-26 12:03:43 -07002867 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2868 AlertDialog.THEME_HOLO_DARK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002869 builder.setTitle(getString(R.string.menu_item_add_item));
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002870 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002871
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002872 AlertDialog dialog = builder.create();
2873 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002874 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002875 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002876
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002877 return dialog;
2878 }
2879
2880 public void onCancel(DialogInterface dialog) {
2881 mWaitingForResult = false;
2882 cleanup();
2883 }
2884
Romain Guycbb89e42009-06-08 15:52:54 -07002885 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002886 mWaitingForResult = false;
2887 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002888 }
2889
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002890 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002891 try {
2892 dismissDialog(DIALOG_CREATE_SHORTCUT);
2893 } catch (Exception e) {
2894 // An exception is thrown if the dialog is not visible, which is fine
2895 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002896 }
2897
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002898 /**
2899 * Handle the action clicked in the "Add to home" dialog.
2900 */
2901 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002902 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002903
Winson Chung55b65502011-05-26 12:03:43 -07002904 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2905 switch (item.actionTag) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002906 case AddAdapter.ITEM_SHORTCUT: {
Winson Chungd2945262011-06-24 15:22:14 -07002907 if (mAppsCustomizeTabHost != null) {
2908 mAppsCustomizeTabHost.selectWidgetsTab();
2909 }
2910 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002911 break;
2912 }
Winson Chung55b65502011-05-26 12:03:43 -07002913 case AddAdapter.ITEM_APPLICATION: {
2914 if (mAppsCustomizeTabHost != null) {
2915 mAppsCustomizeTabHost.selectAppsTab();
2916 }
2917 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002918 break;
2919 }
Winson Chung55b65502011-05-26 12:03:43 -07002920 case AddAdapter.ITEM_APPWIDGET: {
2921 if (mAppsCustomizeTabHost != null) {
2922 mAppsCustomizeTabHost.selectWidgetsTab();
2923 }
2924 showAllApps(true);
2925 break;
2926 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002927 case AddAdapter.ITEM_WALLPAPER: {
2928 startWallpaper();
2929 break;
2930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002931 }
2932 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002933
2934 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002935 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002936 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002937 }
2938
2939 /**
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002940 * Receives notifications when applications are added/removed.
2941 */
2942 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2943 @Override
2944 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002945 closeSystemDialogs();
Joe Onoratob2061212009-11-24 16:13:54 -05002946 String reason = intent.getStringExtra("reason");
2947 if (!"homekey".equals(reason)) {
2948 boolean animate = true;
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002949 if (mPaused || "lock".equals(reason)) {
Joe Onoratob2061212009-11-24 16:13:54 -05002950 animate = false;
2951 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002952 showWorkspace(animate);
Joe Onoratob2061212009-11-24 16:13:54 -05002953 }
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002954 }
2955 }
2956
2957 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002958 * Receives notifications whenever the appwidgets are reset.
2959 */
2960 private class AppWidgetResetObserver extends ContentObserver {
2961 public AppWidgetResetObserver() {
2962 super(new Handler());
2963 }
2964
2965 @Override
2966 public void onChange(boolean selfChange) {
2967 onAppWidgetReset();
2968 }
2969 }
2970
2971 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002972 * If the activity is currently paused, signal that we need to re-run the loader
2973 * in onResume.
2974 *
2975 * This needs to be called from incoming places where resources might have been loaded
2976 * while we are paused. That is becaues the Configuration might be wrong
2977 * when we're not running, and if it comes back to what it was when we
2978 * were paused, we are not restarted.
2979 *
2980 * Implementation of the method from LauncherModel.Callbacks.
2981 *
2982 * @return true if we are currently paused. The caller might be able to
2983 * skip some work in that case since we will come back again.
2984 */
2985 public boolean setLoadOnResume() {
2986 if (mPaused) {
2987 Log.i(TAG, "setLoadOnResume");
2988 mOnResumeNeedsLoad = true;
2989 return true;
2990 } else {
2991 return false;
2992 }
2993 }
2994
2995 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002996 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002997 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002998 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002999 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003000 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003001 } else {
3002 return SCREEN_COUNT / 2;
3003 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003004 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003005
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003006
Joe Onorato9c1289c2009-08-17 11:03:03 -04003007 /**
3008 * Refreshes the shortcuts shown on the workspace.
3009 *
3010 * Implementation of the method from LauncherModel.Callbacks.
3011 */
3012 public void startBinding() {
3013 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003014
3015 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003016 int count = workspace.getChildCount();
3017 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003018 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003019 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3020 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003021 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003022
Joe Onorato9c1289c2009-08-17 11:03:03 -04003023 if (DEBUG_USER_INTERFACE) {
3024 android.widget.Button finishButton = new android.widget.Button(this);
3025 finishButton.setText("Finish");
3026 workspace.addInScreen(finishButton, 1, 0, 0, 1, 1);
3027
3028 finishButton.setOnClickListener(new android.widget.Button.OnClickListener() {
3029 public void onClick(View v) {
3030 finish();
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003031 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003032 });
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003033 }
Adam Cohenf5d77c92011-02-03 12:55:38 -08003034
Adam Cohen4eac29a2011-07-11 17:53:37 -07003035 // This wasn't being called before which resulted in a leak of AppWidgetHostViews
3036 unbindWorkspaceItems();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003037 }
3038
3039 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003040 * Bind the items start-end from the list.
3041 *
3042 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003043 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003044 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
3045
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003046 setLoadOnResume();
3047
Joe Onorato9c1289c2009-08-17 11:03:03 -04003048 final Workspace workspace = mWorkspace;
3049
3050 for (int i=start; i<end; i++) {
3051 final ItemInfo item = shortcuts.get(i);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003052 switch (item.itemType) {
3053 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3054 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Joe Onorato0589f0f2010-02-08 13:44:00 -08003055 final View shortcut = createShortcut((ShortcutInfo)item);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003056 workspace.addInScreen(shortcut, item.screen, item.cellX, item.cellY, 1, 1,
3057 false);
3058 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003059 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Joe Onorato9c1289c2009-08-17 11:03:03 -04003060 final FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003061 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003062 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003063 workspace.addInScreen(newFolder, item.screen, item.cellX, item.cellY, 1, 1,
3064 false);
3065 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003066 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003067 }
3068
Joe Onorato9c1289c2009-08-17 11:03:03 -04003069 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003070 }
3071
Joe Onorato9c1289c2009-08-17 11:03:03 -04003072 /**
3073 * Implementation of the method from LauncherModel.Callbacks.
3074 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003075 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003076 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003077 sFolders.clear();
3078 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003079 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003080
3081 /**
3082 * Add the views for a widget to the workspace.
3083 *
3084 * Implementation of the method from LauncherModel.Callbacks.
3085 */
3086 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003087 setLoadOnResume();
3088
Daniel Sandler843e8602010-06-07 14:59:01 -04003089 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3090 if (DEBUG_WIDGETS) {
3091 Log.d(TAG, "bindAppWidget: " + item);
3092 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003093 final Workspace workspace = mWorkspace;
3094
3095 final int appWidgetId = item.appWidgetId;
3096 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003097 if (DEBUG_WIDGETS) {
3098 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3099 }
3100
Joe Onorato9c1289c2009-08-17 11:03:03 -04003101 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3102
Joe Onorato9c1289c2009-08-17 11:03:03 -04003103 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3104 item.hostView.setTag(item);
3105
3106 workspace.addInScreen(item.hostView, item.screen, item.cellX,
3107 item.cellY, item.spanX, item.spanY, false);
3108
Adam Cohended9f8d2010-11-03 13:25:16 -07003109 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3110
Joe Onorato9c1289c2009-08-17 11:03:03 -04003111 workspace.requestLayout();
3112
Daniel Sandler843e8602010-06-07 14:59:01 -04003113 if (DEBUG_WIDGETS) {
3114 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3115 + (SystemClock.uptimeMillis()-start) + "ms");
3116 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003117 }
3118
3119 /**
3120 * Callback saying that there aren't any more items to bind.
3121 *
3122 * Implementation of the method from LauncherModel.Callbacks.
3123 */
3124 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003125 setLoadOnResume();
3126
Joe Onorato9c1289c2009-08-17 11:03:03 -04003127 if (mSavedState != null) {
3128 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003129 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003130 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003131 mSavedState = null;
3132 }
3133
3134 if (mSavedInstanceState != null) {
3135 super.onRestoreInstanceState(mSavedInstanceState);
3136 mSavedInstanceState = null;
3137 }
3138
Joe Onorato9c1289c2009-08-17 11:03:03 -04003139 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003140
3141 // If we received the result of any pending adds while the loader was running (e.g. the
3142 // widget configuration forced an orientation change), process them now.
3143 for (int i = 0; i < sPendingAddList.size(); i++) {
3144 completeAdd(sPendingAddList.get(i));
3145 }
3146 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003147 }
3148
3149 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003150 * Updates the icons on the launcher that are affected by changes to the package list
3151 * on the device.
3152 */
3153 private void updateIconsAffectedByPackageManagerChanges() {
3154 updateAppMarketIcon();
3155 updateGlobalSearchIcon();
3156 updateVoiceSearchIcon();
3157 }
3158
3159 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003160 * Add the icons for all apps.
3161 *
3162 * Implementation of the method from LauncherModel.Callbacks.
3163 */
3164 public void bindAllApplications(ArrayList<ApplicationInfo> apps) {
Winson Chung785d2eb2011-04-14 16:08:02 -07003165 if (mAppsCustomizeContent != null) {
3166 mAppsCustomizeContent.setApps(apps);
3167 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003168 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003169 }
3170
3171 /**
3172 * A package was installed.
3173 *
3174 * Implementation of the method from LauncherModel.Callbacks.
3175 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003176 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003177 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003178 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003179
Winson Chung785d2eb2011-04-14 16:08:02 -07003180 if (mAppsCustomizeContent != null) {
3181 mAppsCustomizeContent.addApps(apps);
3182 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003183 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003184 }
3185
3186 /**
3187 * A package was updated.
3188 *
3189 * Implementation of the method from LauncherModel.Callbacks.
3190 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003191 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003192 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003193 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003194 if (mWorkspace != null) {
3195 mWorkspace.updateShortcuts(apps);
3196 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003197
Winson Chung785d2eb2011-04-14 16:08:02 -07003198 if (mAppsCustomizeContent != null) {
3199 mAppsCustomizeContent.updateApps(apps);
3200 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003201 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003202 }
3203
3204 /**
3205 * A package was uninstalled.
3206 *
3207 * Implementation of the method from LauncherModel.Callbacks.
3208 */
Joe Onorato36115782010-06-17 13:28:48 -04003209 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003210 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003211 if (permanent) {
3212 mWorkspace.removeItems(apps);
3213 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003214
Winson Chung785d2eb2011-04-14 16:08:02 -07003215 if (mAppsCustomizeContent != null) {
3216 mAppsCustomizeContent.removeApps(apps);
3217 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003218 updateIconsAffectedByPackageManagerChanges();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003219 }
Joe Onoratobe386092009-11-17 17:32:16 -08003220
3221 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003222 * A number of packages were updated.
3223 */
3224 public void bindPackagesUpdated() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003225
Winson Chung785d2eb2011-04-14 16:08:02 -07003226 if (mAppsCustomizeContent != null) {
3227 mAppsCustomizeContent.onPackagesUpdated();
3228 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003229 }
3230
Winson Chung400438b2011-01-16 17:53:48 -08003231 private int mapConfigurationOriActivityInfoOri(int configOri) {
3232 final Display d = getWindowManager().getDefaultDisplay();
3233 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3234 switch (d.getRotation()) {
3235 case Surface.ROTATION_0:
3236 case Surface.ROTATION_180:
3237 // We are currently in the same basic orientation as the natural orientation
3238 naturalOri = configOri;
3239 break;
3240 case Surface.ROTATION_90:
3241 case Surface.ROTATION_270:
3242 // We are currently in the other basic orientation to the natural orientation
3243 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3244 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3245 break;
3246 }
3247
3248 int[] oriMap = {
3249 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3250 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3251 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3252 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3253 };
3254 // Since the map starts at portrait, we need to offset if this device's natural orientation
3255 // is landscape.
3256 int indexOffset = 0;
3257 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3258 indexOffset = 1;
3259 }
3260 return oriMap[(d.getRotation() + indexOffset) % 4];
3261 }
3262 public void lockScreenOrientation() {
3263 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3264 .getConfiguration().orientation));
3265 }
3266 public void unlockScreenOrientation() {
3267 mHandler.postDelayed(new Runnable() {
3268 public void run() {
3269 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3270 }
3271 }, mRestoreScreenOrientationDelay);
3272 }
3273
Winson Chung80baf5a2010-08-09 16:03:15 -07003274 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003275 * Prints out out state for debugging.
3276 */
3277 public void dumpState() {
3278 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003279 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003280 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3281 Log.d(TAG, "mRestoring=" + mRestoring);
3282 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3283 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003284 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003285 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003286
Winson Chung785d2eb2011-04-14 16:08:02 -07003287 if (mAppsCustomizeContent != null) {
3288 mAppsCustomizeContent.dumpState();
3289 }
Joe Onoratobe386092009-11-17 17:32:16 -08003290 Log.d(TAG, "END launcher2 dump state");
3291 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003292}
Michael Jurka2763be32011-02-24 11:19:57 -08003293
Michael Jurkaabded662011-03-04 12:06:57 -08003294interface LauncherTransitionable {
3295 void onLauncherTransitionStart(Animator animation);
3296 void onLauncherTransitionEnd(Animator animation);
Michael Jurka2763be32011-02-24 11:19:57 -08003297}