blob: a62dfa6f7502001a803a715b7f286ca6539744d9 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.Dialog;
30import android.app.SearchManager;
31import android.app.StatusBarManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070037import android.content.ClipData;
38import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
42import android.content.DialogInterface;
43import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070050import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040054import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070055import android.os.AsyncTask;
Adam Cohen50370f32011-08-25 18:57:14 -070056import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020058import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080059import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070060import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040061import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080062import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070063import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080064import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.text.Selection;
66import android.text.SpannableStringBuilder;
67import android.text.TextUtils;
68import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070069import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080070import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080071import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.view.KeyEvent;
73import android.view.LayoutInflater;
74import android.view.Menu;
75import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070076import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080077import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080079import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070080import android.view.ViewGroup;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070081import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080082import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070083import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070084import android.view.animation.AccelerateInterpolator;
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;
Winson Chung68846fd2010-10-29 11:00:27 -070090import android.widget.TextView;
91import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070092
Adam Cohendf2cc412011-04-27 16:56:57 -070093import com.android.common.Search;
94import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070095import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080096
Adam Cohenc0dcf592011-06-01 15:30:43 -070097import java.io.DataInputStream;
98import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070099import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.io.FileNotFoundException;
101import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700102import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.util.ArrayList;
104import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800106/**
107 * Default launcher application.
108 */
Michael Jurka946ad472010-07-09 18:05:18 -0700109public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700110 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
111 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800112 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700113 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114
Joe Onorato9c1289c2009-08-17 11:03:03 -0400115 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400116 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700117
Winson Chung70d72102011-08-12 11:24:35 -0700118 private static final int MENU_GROUP_WALLPAPER = 1;
119 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
120 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700121 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
122 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
124 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700125 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126 private static final int REQUEST_PICK_APPLICATION = 6;
127 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700128 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700129 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130
131 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
132
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800133 static final int SCREEN_COUNT = 5;
134 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Joe Onorato7c312c12009-08-13 21:36:53 -0700136 static final int DIALOG_CREATE_SHORTCUT = 1;
137 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Romain Guy98d01652009-06-30 16:21:04 -0700139 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
141 // Type: int
142 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700143 // Type: int
144 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700146 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
147 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
149 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700150 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700152 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 // Type: boolean
154 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
155 // Type: long
156 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
157
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700158 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
159
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700160 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700161 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700162 private State mState = State.WORKSPACE;
163 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700164
Joe Onorato9c1289c2009-08-17 11:03:03 -0400165 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700166 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
167 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700168 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700169 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800172 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800174 private final BroadcastReceiver mCloseSystemDialogsReceiver
175 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800176 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private LayoutInflater mInflater;
179
Joe Onorato00acb122009-08-04 16:04:30 -0400180 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800181 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700182
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700183 private AppWidgetManager mAppWidgetManager;
184 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700185
Michael Jurkac9d95c52011-08-29 14:03:34 -0700186 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700187 private int[] mTmpAddItemCellCoordinates = new int[2];
188
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 private FolderInfo mFolderInfo;
190
Winson Chung3d503fb2011-07-13 17:25:49 -0700191 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800192 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700193
Winson Chungc51db6a2011-10-05 11:44:49 -0700194 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700195 private AppsCustomizeTabHost mAppsCustomizeTabHost;
196 private AppsCustomizePagedView mAppsCustomizeContent;
197 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private Bundle mSavedState;
200
201 private SpannableStringBuilder mDefaultKeySsb = null;
202
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 private boolean mWorkspaceLoading = true;
204
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800205 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206 private boolean mRestoring;
207 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700208 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
210 private Bundle mSavedInstanceState;
211
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800213 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800214 private boolean mUserPresent = true;
215 private boolean mVisible = false;
216 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400217
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700218 private static LocaleConfiguration sLocaleConfiguration = null;
219
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700220 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700221
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700222 private Intent mAppMarketIntent = null;
223
Adam Cohended9f8d2010-11-03 13:25:16 -0700224 // Related to the auto-advancing of widgets
225 private final int ADVANCE_MSG = 1;
226 private final int mAdvanceInterval = 20000;
227 private final int mAdvanceStagger = 250;
228 private long mAutoAdvanceSentTime;
229 private long mAutoAdvanceTimeLeft = -1;
230 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
231 new HashMap<View, AppWidgetProviderInfo>();
232
Winson Chung400438b2011-01-16 17:53:48 -0800233 // Determines how long to wait after a rotation before restoring the screen orientation to
234 // match the sensor state.
235 private final int mRestoreScreenOrientationDelay = 500;
236
Michael Jurka4ef207b2010-11-29 17:05:45 -0800237 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700238 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
239 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
240 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800241
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700242 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
243
Adam Cohen2801caf2011-05-13 20:57:39 -0700244 private DragLayer mDragLayer;
245
Michael Jurkaddd62e92011-02-16 17:49:14 -0800246 private BubbleTextView mWaitingForResume;
247
Michael Jurkac1f5d262011-09-30 19:32:27 -0700248 private Runnable mBuildLayersRunnable = new Runnable() {
249 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700250 if (mWorkspace != null) {
251 mWorkspace.buildPageHardwareLayers();
252 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700253 }
254 };
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;
Winson Chung3d503fb2011-07-13 17:25:49 -0700262 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800263 int screen;
264 int cellX;
265 int cellY;
266 }
267
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 @Override
269 protected void onCreate(Bundle savedInstanceState) {
270 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800271 LauncherApplication app = ((LauncherApplication)getApplication());
272 mModel = app.setLauncher(this);
273 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400274 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700276
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700277 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700278 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
279 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200282 android.os.Debug.startMethodTracing(
283 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 }
285
286 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 setContentView(R.layout.launcher);
288 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700289 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800291 registerContentObservers();
292
Joe Onorato7c312c12009-08-13 21:36:53 -0700293 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 mSavedState = savedInstanceState;
296 restoreState(mSavedState);
297
Winson Chunga12a2502010-12-20 14:41:35 -0800298 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700299 if (mAppsCustomizeContent != null) {
300 mAppsCustomizeContent.onPackagesUpdated();
301 }
Winson Chunga12a2502010-12-20 14:41:35 -0800302
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 if (PROFILE_STARTUP) {
304 android.os.Debug.stopMethodTracing();
305 }
306
307 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400308 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 }
310
Michael Jurkac57b7a82011-08-09 22:02:20 -0700311 if (!mModel.isAllAppsLoaded()) {
312 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
313 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
314 }
315
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 // For handling default keys
317 mDefaultKeySsb = new SpannableStringBuilder();
318 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800319
320 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
321 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800322
Winson Chungc51db6a2011-10-05 11:44:49 -0700323 boolean searchVisible = false;
324 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800325 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700326 int coi = getCurrentOrientationIndexForGlobalIcons();
327 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
328 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700329 updateAppMarketIcon();
330 searchVisible = updateGlobalSearchIcon();
331 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700332 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700333 if (sGlobalSearchIcon[coi] != null) {
334 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700335 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700336 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700337 if (sVoiceSearchIcon[coi] != null) {
338 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700339 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700340 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700341 if (sAppMarketIcon[coi] != null) {
342 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800343 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700344 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700345
346 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohendc16b602011-09-26 15:12:33 -0700347 if (LauncherApplication.isScreenLarge() || Build.TYPE.contentEquals("eng")) {
Adam Cohen446e9402011-09-15 18:21:21 -0700348 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
349 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 }
Romain Guycbb89e42009-06-08 15:52:54 -0700351
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800352 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700353 if (sLocaleConfiguration == null) {
354 new AsyncTask<Void, Void, LocaleConfiguration>() {
355 @Override
356 protected LocaleConfiguration doInBackground(Void... unused) {
357 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
358 readConfiguration(Launcher.this, localeConfiguration);
359 return localeConfiguration;
360 }
361
362 @Override
363 protected void onPostExecute(LocaleConfiguration result) {
364 sLocaleConfiguration = result;
365 checkForLocaleChange(); // recursive, but now with a locale configuration
366 }
367 }.execute();
368 return;
369 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700370
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 final Configuration configuration = getResources().getConfiguration();
372
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700373 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 final String locale = configuration.locale.toString();
375
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700376 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 final int mcc = configuration.mcc;
378
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700379 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 final int mnc = configuration.mnc;
381
Romain Guy84f296c2009-11-04 15:00:44 -0800382 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800383
Romain Guy84f296c2009-11-04 15:00:44 -0800384 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700385 sLocaleConfiguration.locale = locale;
386 sLocaleConfiguration.mcc = mcc;
387 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700388
Joe Onorato0589f0f2010-02-08 13:44:00 -0800389 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700390
391 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
392 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700393 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700394 public void run() {
395 writeConfiguration(Launcher.this, localeConfiguration);
396 }
397 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700398 }
399 }
400
401 private static class LocaleConfiguration {
402 public String locale;
403 public int mcc = -1;
404 public int mnc = -1;
405 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700406
Romain Guy98d01652009-06-30 16:21:04 -0700407 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
408 DataInputStream in = null;
409 try {
410 in = new DataInputStream(context.openFileInput(PREFERENCES));
411 configuration.locale = in.readUTF();
412 configuration.mcc = in.readInt();
413 configuration.mnc = in.readInt();
414 } catch (FileNotFoundException e) {
415 // Ignore
416 } catch (IOException e) {
417 // Ignore
418 } finally {
419 if (in != null) {
420 try {
421 in.close();
422 } catch (IOException e) {
423 // Ignore
424 }
425 }
426 }
427 }
428
429 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
430 DataOutputStream out = null;
431 try {
432 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
433 out.writeUTF(configuration.locale);
434 out.writeInt(configuration.mcc);
435 out.writeInt(configuration.mnc);
436 out.flush();
437 } catch (FileNotFoundException e) {
438 // Ignore
439 } catch (IOException e) {
440 //noinspection ResultOfMethodCallIgnored
441 context.getFileStreamPath(PREFERENCES).delete();
442 } finally {
443 if (out != null) {
444 try {
445 out.close();
446 } catch (IOException e) {
447 // Ignore
448 }
449 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 }
451 }
452
Adam Cohen716b51e2011-06-30 12:09:54 -0700453 public DragLayer getDragLayer() {
454 return mDragLayer;
455 }
456
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 static int getScreen() {
458 synchronized (sLock) {
459 return sScreen;
460 }
461 }
462
463 static void setScreen(int screen) {
464 synchronized (sLock) {
465 sScreen = screen;
466 }
467 }
468
Winson Chung557d6ed2011-07-08 15:34:52 -0700469 /**
470 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
471 * a configuration step, this allows the proper animations to run after other transitions.
472 */
473 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700474 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800475 switch (args.requestCode) {
476 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700477 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
478 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800479 break;
480 case REQUEST_PICK_SHORTCUT:
481 processShortcut(args.intent);
482 break;
483 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700484 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
485 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700486 result = true;
487 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800488 case REQUEST_PICK_APPWIDGET:
489 addAppWidgetFromPick(args.intent);
490 break;
491 case REQUEST_CREATE_APPWIDGET:
492 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700493 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700494 result = true;
495 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800496 case REQUEST_PICK_WALLPAPER:
497 // We just wanted the activity result here so we can clear mWaitingForResult
498 break;
499 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700500 // In any situation where we have a multi-step drop, we should reset the add info only after
501 // we complete the drop
502 resetAddInfo();
503 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800504 }
505
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800506 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800507 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700508 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700509 mWaitingForResult = false;
510
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800511 // The pattern used here is that a user PICKs a specific application,
512 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700513
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800514 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
515 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700516 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800517 final PendingAddArguments args = new PendingAddArguments();
518 args.requestCode = requestCode;
519 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700520 args.container = mPendingAddInfo.container;
521 args.screen = mPendingAddInfo.screen;
522 args.cellX = mPendingAddInfo.cellX;
523 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800524
525 // If the loader is still running, defer the add until it is done.
526 if (isWorkspaceLocked()) {
527 sPendingAddList.add(args);
528 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700529 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 }
Romain Guy18042c82009-11-06 11:44:55 -0800531 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700532 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
533 if (data != null) {
534 // Clean up the appWidgetId if we canceled
535 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
536 if (appWidgetId != -1) {
537 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
538 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 }
540 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700541
542 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700543 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800544 }
545
546 @Override
547 protected void onResume() {
548 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800549 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700550 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400551 mWorkspaceLoading = true;
552 mModel.startLoader(this, true);
553 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700554 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800555 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800556 if (mWaitingForResume != null) {
557 mWaitingForResume.setStayPressed(false);
558 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700559 // When we resume Launcher, a different Activity might be responsible for the app
560 // market intent, so refresh the icon
561 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -0700562 if (!mWorkspaceLoading) {
563 mWorkspace.post(mBuildLayersRunnable);
564 }
Mathew Inwood8193f692011-10-12 17:30:58 +0100565 clearTypedText();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800566 }
567
568 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700569 protected void onPause() {
570 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700571 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800572 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700573 }
Romain Guycbb89e42009-06-08 15:52:54 -0700574
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700575 @Override
576 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400577 // Flag the loader to stop early before switching
578 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700579 if (mAppsCustomizeContent != null) {
580 mAppsCustomizeContent.surrender();
581 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800582 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700583 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700584
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800585 // We can't hide the IME if it was forced open. So don't bother
586 /*
587 @Override
588 public void onWindowFocusChanged(boolean hasFocus) {
589 super.onWindowFocusChanged(hasFocus);
590
591 if (hasFocus) {
592 final InputMethodManager inputManager = (InputMethodManager)
593 getSystemService(Context.INPUT_METHOD_SERVICE);
594 WindowManager.LayoutParams lp = getWindow().getAttributes();
595 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
596 android.os.Handler()) {
597 protected void onReceiveResult(int resultCode, Bundle resultData) {
598 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
599 }
600 });
601 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
602 }
603 }
604 */
605
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 private boolean acceptFilter() {
607 final InputMethodManager inputManager = (InputMethodManager)
608 getSystemService(Context.INPUT_METHOD_SERVICE);
609 return !inputManager.isFullscreenMode();
610 }
611
612 @Override
613 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700614 final int uniChar = event.getUnicodeChar();
615 final boolean handled = super.onKeyDown(keyCode, event);
616 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
617 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
619 keyCode, event);
620 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700621 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700622 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700623 // showSearchDialog()
624 // If there are multiple keystrokes before the search dialog takes focus,
625 // onSearchRequested() will be called for every keystroke,
626 // but it is idempotent, so it's fine.
627 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 }
629 }
630
Joe Onorato8a9625e2010-01-28 15:55:35 -0800631 // Eat the long press event so the keyboard doesn't come up.
632 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
633 return true;
634 }
635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 return handled;
637 }
638
Karl Rosaen138a0412009-04-23 19:00:21 -0700639 private String getTypedText() {
640 return mDefaultKeySsb.toString();
641 }
642
643 private void clearTypedText() {
644 mDefaultKeySsb.clear();
645 mDefaultKeySsb.clearSpans();
646 Selection.setSelection(mDefaultKeySsb, 0);
647 }
648
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700650 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
651 * State
652 */
653 private static State intToState(int stateOrdinal) {
654 State state = State.WORKSPACE;
655 final State[] stateValues = State.values();
656 for (int i = 0; i < stateValues.length; i++) {
657 if (stateValues[i].ordinal() == stateOrdinal) {
658 state = stateValues[i];
659 break;
660 }
661 }
662 return state;
663 }
664
665 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 * Restores the previous state, if it exists.
667 *
668 * @param savedState The previous state.
669 */
670 private void restoreState(Bundle savedState) {
671 if (savedState == null) {
672 return;
673 }
674
Michael Jurka883f55b2010-10-21 15:47:14 -0700675 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700676 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800677 showAllApps(false);
678 }
679
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
681 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700682 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 }
684
Winson Chung3d503fb2011-07-13 17:25:49 -0700685 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
686 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700687
Winson Chung3d503fb2011-07-13 17:25:49 -0700688 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
689 mPendingAddInfo.container = pendingAddContainer;
690 mPendingAddInfo.screen = pendingAddScreen;
691 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
692 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 mRestoring = true;
694 }
695
696 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
697 if (renameFolder) {
698 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700699 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800700 mRestoring = true;
701 }
Winson Chunga12a2502010-12-20 14:41:35 -0800702
Winson Chung785d2eb2011-04-14 16:08:02 -0700703
704 // Restore the AppsCustomize tab
705 if (mAppsCustomizeTabHost != null) {
706 String curTab = savedState.getString("apps_customize_currentTab");
707 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700708 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700709 mAppsCustomizeContent.setContentType(
710 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
711 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700712 mAppsCustomizeContent.loadAssociatedPages(
713 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700714 }
715
Winson Chung5afbf7b2011-07-25 11:53:08 -0700716 int currentIndex = savedState.getInt("apps_customize_currentIndex");
717 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700718 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 }
720
721 /**
722 * Finds all the views we need and configure them properly.
723 */
724 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700725 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400726
Winson Chung4c98d922011-05-31 16:50:48 -0700727 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
728 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800729
Winson Chung4c98d922011-05-31 16:50:48 -0700730 // Setup the drag layer
731 mDragLayer.setup(this, dragController);
732
Winson Chung3d503fb2011-07-13 17:25:49 -0700733 // Setup the hotseat
734 mHotseat = (Hotseat) findViewById(R.id.hotseat);
735 if (mHotseat != null) {
736 mHotseat.setup(this);
737 }
738
Winson Chung4c98d922011-05-31 16:50:48 -0700739 // Setup the workspace
740 mWorkspace.setHapticFeedbackEnabled(false);
741 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700742 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700743 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700744
Winson Chungf0ea4d32011-06-06 14:27:16 -0700745 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700746 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700747
Winson Chungf0ea4d32011-06-06 14:27:16 -0700748 // Setup AppsCustomize
749 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
750 findViewById(R.id.apps_customize_pane);
751 mAppsCustomizeContent = (AppsCustomizePagedView)
752 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
753 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400754
Michael Jurka5130e402011-10-13 04:55:35 -0700755 // Get the all apps button
756 mAllAppsButton = findViewById(R.id.all_apps_button);
757 if (mAllAppsButton != null) {
758 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
759 @Override
760 public boolean onTouch(View v, MotionEvent event) {
761 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
762 onTouchDownAllAppsButton(v);
763 }
764 return false;
765 }
766 });
767 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700768 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700769 dragController.setDragScoller(mWorkspace);
770 dragController.setScrollView(mDragLayer);
771 dragController.setMoveTarget(mWorkspace);
772 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700773 if (mSearchDropTargetBar != null) {
774 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800775 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800776 }
777
778 /**
779 * Creates a view representing a shortcut.
780 *
781 * @param info The data structure describing the shortcut.
782 *
783 * @return A View inflated from R.layout.application.
784 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800785 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700787 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 }
789
790 /**
791 * Creates a view representing a shortcut inflated from the specified resource.
792 *
793 * @param layoutResId The id of the XML layout used to create the shortcut.
794 * @param parent The group the shortcut belongs to.
795 * @param info The data structure describing the shortcut.
796 *
797 * @return A View inflated from layoutResId.
798 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800799 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800800 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
801 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800802 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800803 return favorite;
804 }
805
806 /**
807 * Add an application shortcut to the workspace.
808 *
809 * @param data The intent describing the application.
810 * @param cellInfo The position on screen where to create the shortcut.
811 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700812 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700813 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700814 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700815
Adam Cohenfbba09b2011-07-18 21:43:05 -0700816 // First we check if we already know the exact location where we want to add this item.
817 if (cellX >= 0 && cellY >= 0) {
818 cellXY[0] = cellX;
819 cellXY[1] = cellY;
820 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700821 showOutOfSpaceMessage();
822 return;
823 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800825 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800826
Romain Guy73b979d2009-06-09 12:57:21 -0700827 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800828 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800830 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700831 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700832 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800833 } else {
834 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 }
836 }
Romain Guycbb89e42009-06-08 15:52:54 -0700837
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 /**
839 * Add a shortcut to the workspace.
840 *
841 * @param data The intent describing the shortcut.
842 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700844 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
845 int cellY) {
846 int[] cellXY = mTmpAddItemCellCoordinates;
847 int[] touchXY = mPendingAddInfo.dropPos;
848 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700849
Michael Jurkad3ef3062010-11-23 16:23:58 -0800850 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700851
Adam Cohen558baaf2011-08-15 15:22:57 -0700852 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
853 final View view = createShortcut(info);
854
Adam Cohenfbba09b2011-07-18 21:43:05 -0700855 // First we check if we already know the exact location where we want to add this item.
856 if (cellX >= 0 && cellY >= 0) {
857 cellXY[0] = cellX;
858 cellXY[1] = cellY;
859 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700860
861 // If appropriate, either create a folder or add to an existing folder
862 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
863 true, null,null)) {
864 return;
865 }
866 DragObject dragObject = new DragObject();
867 dragObject.dragInfo = info;
868 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
869 return;
870 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700871 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800872 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700873 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800874 foundCellSpan = (result != null);
875 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700876 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800877 }
878
879 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700880 showOutOfSpaceMessage();
881 return;
882 }
883
Adam Cohen558baaf2011-08-15 15:22:57 -0700884 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885
886 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700887 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
888 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 }
890 }
891
Adam Cohenf814aa02011-09-01 13:48:05 -0700892 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
893 if (spanXY == null) {
894 spanXY = new int[2];
895 }
896
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800897 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700898 // We want to account for the extra amount of padding that we are adding to the widget
899 // to ensure that it gets the full amount of space that it has requested
900 int requiredWidth = minWidth + padding.left + padding.right;
901 int requiredHeight = minHeight + padding.top + padding.bottom;
902 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
903 }
904
905 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
906 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
907 }
908
Adam Cohencbf47e32011-09-16 17:32:37 -0700909 int[] getMinResizeSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
910 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
911 }
912
Adam Cohenf814aa02011-09-01 13:48:05 -0700913 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
914 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
915 }
916
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800917 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700918 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800919 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700920 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700921 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800922 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700923 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700924 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700925
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700926 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700927 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700928
Adam Cohenf814aa02011-09-01 13:48:05 -0700929 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700930
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700932 // We have saved the position to which the widget was dragged-- this really only matters
933 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700934 int[] cellXY = mTmpAddItemCellCoordinates;
935 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700936 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700937 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
938 cellXY[0] = mPendingAddInfo.cellX;
939 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700940 foundCellSpan = true;
941 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700942 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700943 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700944 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800945 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700946 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700947 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700948 }
949
Michael Jurka0280c3b2010-09-17 15:00:07 -0700950 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800951 if (appWidgetId != -1) {
952 // Deleting an app widget ID is a void call but writes to disk before returning
953 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800954 new Thread("deleteAppWidgetId") {
955 public void run() {
956 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
957 }
958 }.start();
959 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700960 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800961 return;
962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700964 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -0700965 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700966 launcherInfo.spanX = spanXY[0];
967 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700968
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700970 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971
972 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700974 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700975
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700976 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700978
Winson Chung3d503fb2011-07-13 17:25:49 -0700979 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400980 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700981
982 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983 }
984 }
Romain Guycbb89e42009-06-08 15:52:54 -0700985
Adam Cohended9f8d2010-11-03 13:25:16 -0700986 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
987 @Override
988 public void onReceive(Context context, Intent intent) {
989 final String action = intent.getAction();
990 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
991 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -0700992 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -0700993 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -0700994
Winson Chungc100e8e2011-08-09 16:02:43 -0700995 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -0700996 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -0700997 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
998 mAppsCustomizeTabHost.reset();
999 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001000 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001001 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1002 mUserPresent = true;
1003 updateRunning();
1004 }
1005 }
1006 };
1007
1008 @Override
1009 public void onAttachedToWindow() {
1010 super.onAttachedToWindow();
1011
1012 // Listen for broadcasts related to user-presence
1013 final IntentFilter filter = new IntentFilter();
1014 filter.addAction(Intent.ACTION_SCREEN_OFF);
1015 filter.addAction(Intent.ACTION_USER_PRESENT);
1016 registerReceiver(mReceiver, filter);
1017
Adam Cohend113e0c2010-11-11 10:48:05 -08001018 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001019 mVisible = true;
1020 }
1021
1022 @Override
1023 public void onDetachedFromWindow() {
1024 super.onDetachedFromWindow();
1025 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001026 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001027
Adam Cohend113e0c2010-11-11 10:48:05 -08001028 if (mAttached) {
1029 unregisterReceiver(mReceiver);
1030 mAttached = false;
1031 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001032 updateRunning();
1033 }
1034
1035 public void onWindowVisibilityChanged(int visibility) {
1036 mVisible = visibility == View.VISIBLE;
1037 updateRunning();
1038 }
1039
1040 private void sendAdvanceMessage(long delay) {
1041 mHandler.removeMessages(ADVANCE_MSG);
1042 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1043 mHandler.sendMessageDelayed(msg, delay);
1044 mAutoAdvanceSentTime = System.currentTimeMillis();
1045 }
1046
1047 private void updateRunning() {
1048 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1049 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1050 mAutoAdvanceRunning = autoAdvanceRunning;
1051 if (autoAdvanceRunning) {
1052 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1053 sendAdvanceMessage(delay);
1054 } else {
1055 if (!mWidgetsToAdvance.isEmpty()) {
1056 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1057 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1058 }
1059 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001060 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001061 }
1062 }
1063 }
1064
1065 private final Handler mHandler = new Handler() {
1066 @Override
1067 public void handleMessage(Message msg) {
1068 if (msg.what == ADVANCE_MSG) {
1069 int i = 0;
1070 for (View key: mWidgetsToAdvance.keySet()) {
1071 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1072 final int delay = mAdvanceStagger * i;
1073 if (v instanceof Advanceable) {
1074 postDelayed(new Runnable() {
1075 public void run() {
1076 ((Advanceable) v).advance();
1077 }
1078 }, delay);
1079 }
1080 i++;
1081 }
1082 sendAdvanceMessage(mAdvanceInterval);
1083 }
1084 }
1085 };
1086
1087 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001088 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001089 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1090 if (v instanceof Advanceable) {
1091 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001092 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001093 updateRunning();
1094 }
1095 }
1096
1097 void removeWidgetToAutoAdvance(View hostView) {
1098 if (mWidgetsToAdvance.containsKey(hostView)) {
1099 mWidgetsToAdvance.remove(hostView);
1100 updateRunning();
1101 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001102 }
1103
Joe Onorato9c1289c2009-08-17 11:03:03 -04001104 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001105 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001106 launcherInfo.hostView = null;
1107 }
1108
Adam Cohended9f8d2010-11-03 13:25:16 -07001109 void showOutOfSpaceMessage() {
1110 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1111 }
1112
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001113 public LauncherAppWidgetHost getAppWidgetHost() {
1114 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001115 }
Romain Guycbb89e42009-06-08 15:52:54 -07001116
Winson Chunga9abd0e2010-10-27 17:18:37 -07001117 public LauncherModel getModel() {
1118 return mModel;
1119 }
1120
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001121 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001122 getWindow().closeAllPanels();
1123
Winson Chung87acb482011-08-23 17:28:05 -07001124 /**
1125 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001126 try {
1127 dismissDialog(DIALOG_CREATE_SHORTCUT);
1128 // Unlock the workspace if the dialog was showing
1129 } catch (Exception e) {
1130 // An exception is thrown if the dialog is not visible, which is fine
1131 }
1132
1133 try {
1134 dismissDialog(DIALOG_RENAME_FOLDER);
1135 // Unlock the workspace if the dialog was showing
1136 } catch (Exception e) {
1137 // An exception is thrown if the dialog is not visible, which is fine
1138 }
Winson Chung87acb482011-08-23 17:28:05 -07001139 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001140
1141 // Whatever we were doing is hereby canceled.
1142 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001143 }
1144
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001145 @Override
1146 protected void onNewIntent(Intent intent) {
1147 super.onNewIntent(intent);
1148
1149 // Close the menu
1150 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001151 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001152 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001153
Joe Onorato14f122b2009-11-19 14:06:36 -08001154 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1155 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001156
Adam Cohenac56cff2011-09-28 20:45:37 -07001157 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001158 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001159 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001160 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1161 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001162 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001163 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001164
1165 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001166 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001167 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001168
Joe Onorato3a8820b2009-11-10 15:06:42 -08001169 final View v = getWindow().peekDecorView();
1170 if (v != null && v.getWindowToken() != null) {
1171 InputMethodManager imm = (InputMethodManager)getSystemService(
1172 INPUT_METHOD_SERVICE);
1173 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001175
Michael Jurka35aa14d2011-07-07 17:01:08 -07001176 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001177 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001178 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001179 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 }
1181 }
1182
1183 @Override
1184 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1185 // Do not call super here
1186 mSavedInstanceState = savedInstanceState;
1187 }
1188
1189 @Override
1190 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001191 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001192 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001193
Michael Jurka883f55b2010-10-21 15:47:14 -07001194 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001195 // We close any open folder since it will not be re-opened, and we need to make sure
1196 // this state is reflected.
1197 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198
Winson Chung3d503fb2011-07-13 17:25:49 -07001199 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1200 mWaitingForResult) {
1201 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1202 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1203 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1204 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 }
1206
1207 if (mFolderInfo != null && mWaitingForResult) {
1208 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1209 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1210 }
Michael Jurka946ad472010-07-09 18:05:18 -07001211
Winson Chung785d2eb2011-04-14 16:08:02 -07001212 // Save the current AppsCustomize tab
1213 if (mAppsCustomizeTabHost != null) {
1214 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1215 if (currentTabTag != null) {
1216 outState.putString("apps_customize_currentTab", currentTabTag);
1217 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001218 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1219 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001220 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221 }
1222
1223 @Override
1224 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001226
Winson Chunge7a03942011-08-05 15:05:12 -07001227 // Remove all pending runnables
1228 mHandler.removeMessages(ADVANCE_MSG);
1229 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001230 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001231
Winson Chungcd2b0142011-06-08 16:02:26 -07001232 // Stop callbacks from LauncherModel
1233 LauncherApplication app = ((LauncherApplication) getApplication());
1234 mModel.stopLoader();
1235 app.setLauncher(null);
1236
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001238 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001239 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001240 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001242 mAppWidgetHost = null;
1243
1244 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001245
1246 TextKeyListener.getInstance().release();
1247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248
Winson Chung3d503fb2011-07-13 17:25:49 -07001249 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001250
1251 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001252 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001253
1254 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1255 mWorkspace.removeAllViews();
1256 mWorkspace = null;
1257 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001258
1259 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 }
1261
Adam Cohena9cf38f2011-05-02 15:36:58 -07001262 public DragController getDragController() {
1263 return mDragController;
1264 }
1265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266 @Override
1267 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001268 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269 super.startActivityForResult(intent, requestCode);
1270 }
1271
Winson Chung70d72102011-08-12 11:24:35 -07001272 /**
1273 * Indicates that we want global search for this activity by setting the globalSearch
1274 * argument for {@link #startSearch} to true.
1275 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001277 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001278 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001279
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001280 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001281
Karl Rosaen138a0412009-04-23 19:00:21 -07001282 if (initialQuery == null) {
1283 // Use any text typed in the launcher as the initial query
1284 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001285 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286 if (appSearchData == null) {
1287 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001288 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001290 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001291
Karl Rosaen138a0412009-04-23 19:00:21 -07001292 final SearchManager searchManager =
1293 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001294 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001295 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 }
1297
Winson Chung70d72102011-08-12 11:24:35 -07001298 @Override
1299 public boolean onCreateOptionsMenu(Menu menu) {
1300 if (isWorkspaceLocked()) {
1301 return false;
1302 }
1303
1304 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001305
1306 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1307 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1308 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001309 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1310 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1311 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001312 String helpUrl = getString(R.string.help_url);
1313 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001314 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1315 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1316
Winson Chung70d72102011-08-12 11:24:35 -07001317 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1318 .setIcon(android.R.drawable.ic_menu_gallery)
1319 .setAlphabeticShortcut('W');
1320 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1321 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001322 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001323 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001324 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1325 .setIcon(android.R.drawable.ic_menu_preferences)
1326 .setIntent(settings)
1327 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001328 if (!helpUrl.isEmpty()) {
1329 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1330 .setIcon(android.R.drawable.ic_menu_help)
1331 .setIntent(help)
1332 .setAlphabeticShortcut('H');
1333 }
Winson Chung70d72102011-08-12 11:24:35 -07001334 return true;
1335 }
1336
1337 @Override
1338 public boolean onPrepareOptionsMenu(Menu menu) {
1339 super.onPrepareOptionsMenu(menu);
1340
1341 if (mAppsCustomizeTabHost.isTransitioning()) {
1342 return false;
1343 }
1344 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1345 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1346
1347 return true;
1348 }
1349
1350 @Override
1351 public boolean onOptionsItemSelected(MenuItem item) {
1352 switch (item.getItemId()) {
1353 case MENU_WALLPAPER_SETTINGS:
1354 startWallpaper();
1355 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001356 }
1357
1358 return super.onOptionsItemSelected(item);
1359 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001361 @Override
1362 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001363 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001364 // Use a custom animation for launching search
1365 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001366 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001367 }
1368
Joe Onorato9c1289c2009-08-17 11:03:03 -04001369 public boolean isWorkspaceLocked() {
1370 return mWorkspaceLoading || mWaitingForResult;
1371 }
1372
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001374 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001375 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 }
1377
Michael Jurka0280c3b2010-09-17 15:00:07 -07001378 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001379 mPendingAddInfo.container = ItemInfo.NO_ID;
1380 mPendingAddInfo.screen = -1;
1381 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1382 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1383 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001384 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001385
Michael Jurkaaf442092010-06-10 17:01:57 -07001386 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001387 // TODO: catch bad widget exception when sent
1388 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001389 // TODO: Is this log message meaningful?
1390 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001391 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001392 }
1393
Winson Chung55cef262010-10-28 14:14:18 -07001394 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001395 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396
Bjorn Bringert7984c942009-12-09 15:38:25 +00001397 if (appWidget.configure != null) {
1398 // Launch over to configure widget, if needed
1399 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1400 intent.setComponent(appWidget.configure);
1401 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001402 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001403 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1404 intent.putExtra(
1405 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1406 info.mimeType);
1407
1408 final String mimeType = info.mimeType;
1409 final ClipData clipData = (ClipData) info.configurationData;
1410 final ClipDescription clipDesc = clipData.getDescription();
1411 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1412 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001413 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001414 final CharSequence stringData = item.getText();
1415 final Uri uriData = item.getUri();
1416 final Intent intentData = item.getIntent();
1417 final String key =
1418 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1419 if (uriData != null) {
1420 intent.putExtra(key, uriData);
1421 } else if (intentData != null) {
1422 intent.putExtra(key, intentData);
1423 } else if (stringData != null) {
1424 intent.putExtra(key, stringData);
1425 }
1426 break;
1427 }
1428 }
1429 }
Winson Chung55cef262010-10-28 14:14:18 -07001430 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001431
Romain Guy8e633c52010-03-04 12:51:36 -08001432 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001434 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001435 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001436
1437 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001438 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 }
1440 }
Romain Guycbb89e42009-06-08 15:52:54 -07001441
Adam Cohenfbba09b2011-07-18 21:43:05 -07001442 /**
1443 * Process a shortcut drop.
1444 *
1445 * @param componentName The name of the component
1446 * @param screen The screen where it should be added
1447 * @param cell The cell it should be added to, optional
1448 * @param position The location on the screen where it was dropped, optional
1449 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001450 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1451 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001452 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001453 mPendingAddInfo.container = container;
1454 mPendingAddInfo.screen = screen;
1455 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001456
1457 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001458 mPendingAddInfo.cellX = cell[0];
1459 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001460 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001461
1462 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1463 createShortcutIntent.setComponent(componentName);
1464 processShortcut(createShortcutIntent);
1465 }
1466
Adam Cohenfbba09b2011-07-18 21:43:05 -07001467 /**
1468 * Process a widget drop.
1469 *
1470 * @param info The PendingAppWidgetInfo of the widget being added.
1471 * @param screen The screen where it should be added
1472 * @param cell The cell it should be added to, optional
1473 * @param position The location on the screen where it was dropped, optional
1474 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001475 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1476 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001477 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001478 mPendingAddInfo.container = info.container = container;
1479 mPendingAddInfo.screen = info.screen = screen;
1480 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001481 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001482 mPendingAddInfo.cellX = cell[0];
1483 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001484 }
1485
1486 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1487 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1488 addAppWidgetImpl(appWidgetId, info);
1489 }
1490
Joe Onoratodeb98af2010-02-19 14:59:39 -08001491 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001492 // Handle case where user selected "Applications"
1493 String applicationName = getResources().getString(R.string.group_applications);
1494 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001495
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001496 if (applicationName != null && applicationName.equals(shortcutName)) {
1497 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1498 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001499
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001500 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1501 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001502 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001503 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001504 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001505 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001506 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
1508
Winson Chung24ab2f12010-09-16 14:10:47 -07001509 void processWallpaper(Intent intent) {
1510 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1511 }
1512
Winson Chung3d503fb2011-07-13 17:25:49 -07001513 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1514 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001515 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 folderInfo.title = getText(R.string.folder_name);
1517
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001519 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1520 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001521 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522
1523 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001524 FolderIcon newFolder =
1525 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1526 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1527 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001528 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 }
Romain Guycbb89e42009-06-08 15:52:54 -07001530
Joe Onorato9c1289c2009-08-17 11:03:03 -04001531 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001532 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001533 }
1534
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 private void showNotifications() {
1536 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1537 if (statusBar != null) {
1538 statusBar.expand();
1539 }
1540 }
1541
1542 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001543 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001545 Intent chooser = Intent.createChooser(pickWallpaper,
1546 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001547 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1548 // Removed in Eclair MR1
1549// WallpaperManager wm = (WallpaperManager)
1550// getSystemService(Context.WALLPAPER_SERVICE);
1551// WallpaperInfo wi = wm.getWallpaperInfo();
1552// if (wi != null && wi.getSettingsActivity() != null) {
1553// LabeledIntent li = new LabeledIntent(getPackageName(),
1554// R.string.configure_wallpaper, 0);
1555// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1556// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1557// }
Mike Clerona0618e42009-10-22 13:55:21 -07001558 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001559 }
1560
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001561 /**
1562 * Registers various content observers. The current implementation registers
1563 * only a favorites observer to keep track of the favorites applications.
1564 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001565 private void registerContentObservers() {
1566 ContentResolver resolver = getContentResolver();
1567 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1568 true, mWidgetObserver);
1569 }
1570
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 @Override
1572 public boolean dispatchKeyEvent(KeyEvent event) {
1573 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1574 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001576 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001577 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001578 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001579 dumpState();
1580 return true;
1581 }
1582 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001583 }
1584 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1585 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001586 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 return true;
1588 }
1589 }
1590
1591 return super.dispatchKeyEvent(event);
1592 }
1593
Joe Onorato88ec0992009-11-19 13:16:06 -08001594 @Override
1595 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001596 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001597 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001598 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001599 Folder openFolder = mWorkspace.getOpenFolder();
1600 if (openFolder.isEditingName()) {
1601 openFolder.dismissEditingName();
1602 } else {
1603 closeFolder();
1604 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001605 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001606 mWorkspace.exitWidgetResizeMode();
1607
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001608 // Back button is a no-op here, but give at least some feedback for the button press
1609 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001610 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001611 }
1612
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001614 * Re-listen when widgets are reset.
1615 */
1616 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001617 if (mAppWidgetHost != null) {
1618 mAppWidgetHost.startListening();
1619 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001620 }
1621
1622 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001623 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1624 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001626 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001627 if (mModel != null) {
1628 mModel.unbindWorkspaceItems();
1629 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001631
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 /**
1633 * Launches the intent referred by the clicked shortcut.
1634 *
1635 * @param v The view representing the clicked shortcut.
1636 */
1637 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001638 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1639 // view has detached (it's possible for this to happen if the view is removed mid touch).
1640 if (v.getWindowToken() == null) {
1641 return;
1642 }
1643
1644 if (mWorkspace.isSwitchingState()) {
1645 return;
1646 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001647
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001649 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001651 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001652 int[] pos = new int[2];
1653 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001654 intent.setSourceBounds(new Rect(pos[0], pos[1],
1655 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001656 boolean success = startActivitySafely(intent, tag);
1657
1658 if (success && v instanceof BubbleTextView) {
1659 mWaitingForResume = (BubbleTextView) v;
1660 mWaitingForResume.setStayPressed(true);
1661 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001663 if (v instanceof FolderIcon) {
1664 FolderIcon fi = (FolderIcon) v;
1665 handleFolderClick(fi);
1666 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001667 } else if (v == mAllAppsButton) {
1668 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001669 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001670 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001671 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001672 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 }
1674 }
1675
Michael Jurka0e260592010-06-30 17:07:39 -07001676 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001677 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001678 // clicking anywhere on the workspace causes the customization drawer to slide down
1679 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001680 return false;
1681 }
1682
Michael Jurkaaf442092010-06-10 17:01:57 -07001683 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001684 * Event handler for the search button
1685 *
1686 * @param v The view that was clicked.
1687 */
1688 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001689 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001690 }
1691
1692 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001693 * Event handler for the voice button
1694 *
1695 * @param v The view that was clicked.
1696 */
1697 public void onClickVoiceButton(View v) {
1698 startVoiceSearch();
1699 }
1700
1701 private void startVoiceSearch() {
1702 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001703 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001704 startActivity(intent);
1705 }
1706
1707 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001708 * Event handler for the "grid" button that appears on the home screen, which
1709 * enters all apps mode.
1710 *
1711 * @param v The view that was clicked.
1712 */
1713 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001714 showAllApps(true);
1715 }
1716
1717 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001718 // Provide the same haptic feedback that the system offers for virtual keys.
1719 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001720 }
1721
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001722 public void onClickAppMarketButton(View v) {
1723 if (mAppMarketIntent != null) {
1724 startActivitySafely(mAppMarketIntent, "app market");
1725 }
1726 }
1727
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001728 void startApplicationDetailsActivity(ComponentName componentName) {
1729 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001730 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1731 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001732 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001733 startActivity(intent);
1734 }
1735
Patrick Dubroy5539af72010-09-07 15:22:01 -07001736 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1737 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1738 // System applications cannot be installed. For now, show a toast explaining that.
1739 // We may give them the option of disabling apps this way.
1740 int messageId = R.string.uninstall_system_app_text;
1741 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1742 } else {
1743 String packageName = appInfo.componentName.getPackageName();
1744 String className = appInfo.componentName.getClassName();
1745 Intent intent = new Intent(
1746 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001747 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1748 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001749 startActivity(intent);
1750 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001751 }
1752
Michael Jurkaddd62e92011-02-16 17:49:14 -08001753 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001754 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1755 try {
1756 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001757 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001758 } catch (ActivityNotFoundException e) {
1759 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001760 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001761 } catch (SecurityException e) {
1762 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001763 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001765 "or use the exported attribute for this activity. "
1766 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001768 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001770
Romain Guy8e633c52010-03-04 12:51:36 -08001771 void startActivityForResultSafely(Intent intent, int requestCode) {
1772 try {
1773 startActivityForResult(intent, requestCode);
1774 } catch (ActivityNotFoundException e) {
1775 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1776 } catch (SecurityException e) {
1777 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1778 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1779 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1780 "or use the exported attribute for this activity.", e);
1781 }
1782 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001783
Adam Cohena9cf38f2011-05-02 15:36:58 -07001784 private void handleFolderClick(FolderIcon folderIcon) {
1785 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001786 Folder openFolder = mWorkspace.getFolderForTag(info);
1787
1788 // If the folder info reports that the associated folder is open, then verify that
1789 // it is actually opened. There have been a few instances where this gets out of sync.
1790 if (info.opened && openFolder == null) {
1791 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1792 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1793 info.opened = false;
1794 }
1795
Adam Cohena9cf38f2011-05-02 15:36:58 -07001796 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001797 // Close any open folder
1798 closeFolder();
1799 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001800 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 } else {
1802 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 int folderScreen;
1804 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001805 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 // .. and close it
1807 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001808 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 // Close any folder open on the current screen
1810 closeFolder();
1811 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001812 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 }
1814 }
1815 }
1816 }
1817
Adam Cohen2801caf2011-05-13 20:57:39 -07001818 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001819 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001820 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1821 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1822 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1823
Adam Cohenc51934b2011-07-26 21:07:43 -07001824 FolderInfo info = (FolderInfo) fi.getTag();
1825 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1826 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001827 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1828 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001829 }
1830
Adam Cohen2801caf2011-05-13 20:57:39 -07001831 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1832 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1833 oa.start();
1834 }
1835
1836 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001837 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001838 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1839 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1840 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1841
Adam Cohenc51934b2011-07-26 21:07:43 -07001842 FolderInfo info = (FolderInfo) fi.getTag();
1843 CellLayout cl = null;
1844 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1845 cl = (CellLayout) fi.getParent().getParent();
1846 }
1847
1848 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001849 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1850 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001851 oa.addListener(new AnimatorListenerAdapter() {
1852 @Override
1853 public void onAnimationEnd(Animator animation) {
1854 if (layout != null) {
1855 layout.clearFolderLeaveBehind();
1856 }
1857 }
1858 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001859 oa.start();
1860 }
1861
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001863 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 * is animated relative to the specified View. If the View is null, no animation
1865 * is played.
1866 *
1867 * @param folderInfo The FolderInfo describing the folder to open.
1868 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001869 public void openFolder(FolderIcon folderIcon) {
1870 Folder folder = folderIcon.mFolder;
1871 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872
Adam Cohen2801caf2011-05-13 20:57:39 -07001873 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001874 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875
Adam Cohen4554ee12011-08-03 16:13:21 -07001876 // Just verify that the folder hasn't already been added to the DragLayer.
1877 // There was a one-off crash where the folder had a parent already.
1878 if (folder.getParent() == null) {
1879 mDragLayer.addView(folder);
1880 mDragController.addDropTarget((DropTarget) folder);
1881 } else {
1882 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1883 folder.getParent() + ").");
1884 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001885 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001886 }
1887
1888 public void closeFolder() {
1889 Folder folder = mWorkspace.getOpenFolder();
1890 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07001891 if (folder.isEditingName()) {
1892 folder.dismissEditingName();
1893 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001894 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07001895
1896 // Dismiss the folder cling
1897 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07001898 }
1899 }
1900
1901 void closeFolder(Folder folder) {
1902 folder.getInfo().opened = false;
1903
1904 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1905 if (parent != null) {
1906 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1907 shrinkAndFadeInFolderIcon(fi);
1908 }
1909 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001910 }
1911
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001913 if (mState != State.WORKSPACE) {
1914 return false;
1915 }
1916
Joe Onorato9c1289c2009-08-17 11:03:03 -04001917 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 return false;
1919 }
1920
1921 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001922 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 }
1924
Michael Jurka0280c3b2010-09-17 15:00:07 -07001925 resetAddInfo();
1926 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001928 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 return true;
1930 }
1931
Winson Chung3d503fb2011-07-13 17:25:49 -07001932 // The hotseat touch handling does not go through Workspace, and we always allow long press
1933 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001934 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001935 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1936 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001937 if (itemUnderLongClick == null) {
1938 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001939 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1940 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001941 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001943 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001945 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
1947 }
1948 }
1949 return true;
1950 }
1951
Winson Chung3d503fb2011-07-13 17:25:49 -07001952 boolean isHotseatLayout(View layout) {
1953 return mHotseat != null && layout != null &&
1954 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1955 }
1956 Hotseat getHotseat() {
1957 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001958 }
1959
Winson Chung3d503fb2011-07-13 17:25:49 -07001960 /**
1961 * Returns the CellLayout of the specified container at the specified screen.
1962 */
1963 CellLayout getCellLayout(long container, int screen) {
1964 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1965 if (mHotseat != null) {
1966 return mHotseat.getLayout();
1967 } else {
1968 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001969 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001970 } else {
1971 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001972 }
1973 }
1974
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 Workspace getWorkspace() {
1976 return mWorkspace;
1977 }
1978
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 @Override
1980 protected Dialog onCreateDialog(int id) {
1981 switch (id) {
1982 case DIALOG_CREATE_SHORTCUT:
1983 return new CreateShortcut().createDialog();
1984 case DIALOG_RENAME_FOLDER:
1985 return new RenameFolder().createDialog();
1986 }
1987
1988 return super.onCreateDialog(id);
1989 }
1990
1991 @Override
1992 protected void onPrepareDialog(int id, Dialog dialog) {
1993 switch (id) {
1994 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 break;
1996 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001997 if (mFolderInfo != null) {
1998 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
1999 final CharSequence text = mFolderInfo.title;
2000 input.setText(text);
2001 input.setSelection(0, text.length());
2002 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 break;
2004 }
2005 }
2006
2007 void showRenameDialog(FolderInfo info) {
2008 mFolderInfo = info;
2009 mWaitingForResult = true;
2010 showDialog(DIALOG_RENAME_FOLDER);
2011 }
2012
Adam Cohenfbba09b2011-07-18 21:43:05 -07002013 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002014 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002015 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2016 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 mWaitingForResult = true;
2018 showDialog(DIALOG_CREATE_SHORTCUT);
2019 }
2020
2021 private class RenameFolder {
2022 private EditText mInput;
2023
2024 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2026 mInput = (EditText) layout.findViewById(R.id.folder_name);
2027
2028 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2029 builder.setIcon(0);
2030 builder.setTitle(getString(R.string.rename_folder_title));
2031 builder.setCancelable(true);
2032 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2033 public void onCancel(DialogInterface dialog) {
2034 cleanup();
2035 }
2036 });
2037 builder.setNegativeButton(getString(R.string.cancel_action),
2038 new Dialog.OnClickListener() {
2039 public void onClick(DialogInterface dialog, int which) {
2040 cleanup();
2041 }
2042 }
2043 );
2044 builder.setPositiveButton(getString(R.string.rename_action),
2045 new Dialog.OnClickListener() {
2046 public void onClick(DialogInterface dialog, int which) {
2047 changeFolderName();
2048 }
2049 }
2050 );
2051 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002052
2053 final AlertDialog dialog = builder.create();
2054 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2055 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002056 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002057 mInput.requestFocus();
2058 InputMethodManager inputManager = (InputMethodManager)
2059 getSystemService(Context.INPUT_METHOD_SERVICE);
2060 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002061 }
2062 });
2063
2064 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 }
2066
2067 private void changeFolderName() {
2068 final String name = mInput.getText().toString();
2069 if (!TextUtils.isEmpty(name)) {
2070 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002071 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 mFolderInfo.title = name;
2073 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2074
Joe Onorato9c1289c2009-08-17 11:03:03 -04002075 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002076 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002077 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 } else {
2079 final FolderIcon folderIcon = (FolderIcon)
2080 mWorkspace.getViewForTag(mFolderInfo);
2081 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002082 // TODO: At some point we'll probably want some version of setting
2083 // the text for a folder icon.
2084 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 getWorkspace().requestLayout();
2086 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002087 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002088 mWorkspaceLoading = true;
2089 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 }
2091 }
2092 }
2093 cleanup();
2094 }
2095
2096 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 dismissDialog(DIALOG_RENAME_FOLDER);
2098 mWaitingForResult = false;
2099 mFolderInfo = null;
2100 }
2101 }
2102
Daniel Sandler843e8602010-06-07 14:59:01 -04002103 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2104 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002105 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002106 }
2107
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002108 // AllAppsView.Watcher
2109 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002110 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002111 mWorkspace.setVisibility(View.GONE);
2112 }
2113 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002114
2115 /**
2116 * Helper method for the cameraZoomIn/cameraZoomOut animations
2117 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002118 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002119 * @param scaleFactor The scale factor used for the zoom
2120 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002121 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002122 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002123 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002124 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002125
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002126 void updateWallpaperVisibility(boolean visible) {
2127 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2128 int curflags = getWindow().getAttributes().flags
2129 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2130 if (wpflags != curflags) {
2131 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2132 }
2133 }
2134
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002135 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002136 * Things to test when changing the following seven functions.
2137 * - Home from workspace
2138 * - from center screen
2139 * - from other screens
2140 * - Home from all apps
2141 * - from center screen
2142 * - from other screens
2143 * - Back from all apps
2144 * - from center screen
2145 * - from other screens
2146 * - Launch app from workspace and quit
2147 * - with back
2148 * - with home
2149 * - Launch app from all apps and quit
2150 * - with back
2151 * - with home
2152 * - Go to a screen that's not the default, then all
2153 * apps, and launch and app, and go back
2154 * - with back
2155 * -with home
2156 * - On workspace, long press power and go back
2157 * - with back
2158 * - with home
2159 * - On all apps, long press power and go back
2160 * - with back
2161 * - with home
2162 * - On workspace, power off
2163 * - On all apps, power off
2164 * - Launch an app and turn off the screen while in that app
2165 * - Go back with home key
2166 * - Go back with back key TODO: make this not go to workspace
2167 * - From all apps
2168 * - From workspace
2169 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2170 * - From all apps
2171 * - From the center workspace
2172 * - From another workspace
2173 */
2174
2175 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002176 * Zoom the camera out from the workspace to reveal 'toView'.
2177 * Assumes that the view to show is anchored at either the very top or very bottom
2178 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002179 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002180 private void showAppsCustomizeHelper(boolean animated, final boolean springLoaded) {
2181 if (mStateAnimation != null) {
2182 mStateAnimation.cancel();
2183 mStateAnimation = null;
2184 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002185 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002186 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002187
Winson Chungf0ea4d32011-06-06 14:27:16 -07002188 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2189 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2190 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2191 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002192 final int startDelay =
2193 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002194
Michael Jurkab3e22d92011-10-31 15:58:33 -07002195 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002196
Michael Jurkad74c9842011-07-10 12:44:21 -07002197 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002198 mWorkspace.changeState(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002199
2200 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002201 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002202 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002203 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2204 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002205 ((View) toView.getParent()).invalidate();
2206 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002207 toView.setFastScaleX(a * scale + b * 1f);
2208 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002209 }
2210 });
Adam Cohen61033d32010-11-15 18:29:44 -08002211
Winson Chungf0ea4d32011-06-06 14:27:16 -07002212 toView.setVisibility(View.VISIBLE);
2213 toView.setFastAlpha(0f);
2214 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2215 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2216 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2217 public void onAnimationUpdate(float a, float b) {
2218 // don't need to invalidate because we do so above
2219 toView.setFastAlpha(a * 0f + b * 1f);
2220 }
2221 });
Adam Cohencff6af82011-09-13 14:51:53 -07002222 alphaAnim.setStartDelay(startDelay);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002223 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002224
Michael Jurkaabded662011-03-04 12:06:57 -08002225 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002226 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, scaleAnim,
2227 false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002228 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002229 scaleAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002230 boolean animationCancelled = false;
2231
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002232 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002233 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002234 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002235 // Prepare the position
2236 toView.setTranslationX(0.0f);
2237 toView.setTranslationY(0.0f);
2238 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002239 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002240 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002241 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002242 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002243 // If we don't set the final scale values here, if this animation is cancelled
2244 // it will have the wrong scale value and subsequent cameraPan animations will
2245 // not fix that
2246 toView.setScaleX(1.0f);
2247 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002248 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002249 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance,
2250 scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002251 }
Winson Chung32174c82011-07-19 15:47:55 -07002252
2253 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2254 // Hide the workspace scrollbar
2255 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002256 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002257 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002258 if (!animationCancelled) {
2259 updateWallpaperVisibility(false);
2260 }
2261 }
2262
Adam Cohencff6af82011-09-13 14:51:53 -07002263 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002264 public void onAnimationCancel(Animator animation) {
2265 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002266 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002267 });
2268
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002269 // toView should appear right at the end of the workspace shrink animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002270 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002271 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002272 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002273 } else {
2274 toView.setTranslationX(0.0f);
2275 toView.setTranslationY(0.0f);
2276 toView.setScaleX(1.0f);
2277 toView.setScaleY(1.0f);
2278 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002279 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002280 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002281 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, null, false);
2282 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance, null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002283
Winson Chungc07918d2011-07-01 15:35:26 -07002284 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2285 // Hide the workspace scrollbar
2286 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002287 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002288 }
Michael Jurkaabded662011-03-04 12:06:57 -08002289 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002290 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002291 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002292 }
2293
2294 /**
2295 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002296 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002297 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002298 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002299 private void hideAppsCustomizeHelper(boolean animated) {
2300 if (mStateAnimation != null) {
2301 mStateAnimation.cancel();
2302 mStateAnimation = null;
2303 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002304 Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002305 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002306
Winson Chungf0ea4d32011-06-06 14:27:16 -07002307 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2308 final float scaleFactor = (float)
2309 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2310 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002311
Michael Jurkab3e22d92011-10-31 15:58:33 -07002312 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002313 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002314 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002315 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002316 final float oldScaleX = fromView.getScaleX();
2317 final float oldScaleY = fromView.getScaleY();
2318
2319 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2320 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002321 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2322 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002323 ((View)fromView.getParent()).fastInvalidate();
2324 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2325 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2326 }
2327 });
Michael Jurkaabded662011-03-04 12:06:57 -08002328 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002329 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Adam Cohencff6af82011-09-13 14:51:53 -07002330 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002331 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2332 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002333 // don't need to invalidate because we do so above
2334 fromView.setFastAlpha(a * 1f + b * 0f);
2335 }
2336 });
Michael Jurkaabded662011-03-04 12:06:57 -08002337 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002338 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, alphaAnim,
2339 true);
Michael Jurka2763be32011-02-24 11:19:57 -08002340 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002341 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002342 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002343 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002344 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002345 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002346 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002347 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance,
2348 alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002349 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002350 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002351 }
2352 });
2353
Michael Jurkab3e22d92011-10-31 15:58:33 -07002354 mStateAnimation = new AnimatorSet();
Winson Chungf0ea4d32011-06-06 14:27:16 -07002355 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002356 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002357 } else {
2358 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002359 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002360 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, null, true);
2361 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance, null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002362 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002363 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002364 }
2365
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002366 void showWorkspace(boolean animated) {
Adam Cohencff6af82011-09-13 14:51:53 -07002367 Resources res = getResources();
2368 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2369
2370 mWorkspace.changeState(Workspace.State.NORMAL, animated, stagger);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002371 if (mState != State.WORKSPACE) {
2372 mWorkspace.setVisibility(View.VISIBLE);
2373 hideAppsCustomizeHelper(animated);
2374
2375 // Show the search bar and hotseat
2376 mSearchDropTargetBar.showSearchBar(animated);
2377
2378 // Set focus to the AppsCustomize button
2379 if (mAllAppsButton != null) {
2380 mAllAppsButton.requestFocus();
2381 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002382 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002383
Adam Cohen7777d962011-08-18 18:58:38 -07002384 mWorkspace.showDockDivider(!animated);
2385 mWorkspace.flashScrollingIndicator();
2386
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002387 // Change the state *after* we've called all the transition code
2388 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002389
Winson Chung5fb63472011-02-02 17:03:37 -08002390 // Resume the auto-advance of widgets
2391 mUserPresent = true;
2392 updateRunning();
2393
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002394 // send an accessibility event to announce the context change
2395 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002396 }
2397
Michael Jurkab3e22d92011-10-31 15:58:33 -07002398 void showAllApps(boolean animated) {
2399 if (mState != State.WORKSPACE) return;
2400
2401 showAppsCustomizeHelper(animated, false);
2402 mAppsCustomizeTabHost.requestFocus();
2403
2404 // Hide the search bar and hotseat
2405 mSearchDropTargetBar.hideSearchBar(animated);
2406
2407 // Change the state *after* we've called all the transition code
2408 mState = State.APPS_CUSTOMIZE;
2409
2410 // Pause the auto-advance of widgets until we are out of AllApps
2411 mUserPresent = false;
2412 updateRunning();
2413 closeFolder();
2414
2415 // Send an accessibility event to announce the context change
2416 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2417 }
2418
Adam Cohen7777d962011-08-18 18:58:38 -07002419 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002420 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002421 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002422 hideAppsCustomizeHelper(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002423 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002424 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002425 }
Adam Cohen7777d962011-08-18 18:58:38 -07002426
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002427 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chung09bfc452011-09-09 11:30:20 -07002428 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2429
Winson Chunge7a03942011-08-05 15:05:12 -07002430 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002431 @Override
2432 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002433 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002434 // Before we show workspace, hide all apps again because
2435 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2436 // clean up our state transition functions
2437 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002438 mSearchDropTargetBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002439 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002440 } else {
2441 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002442 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002443 }
2444 }, (extendedDelay ?
2445 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2446 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2447 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002448
Michael Jurkad3ef3062010-11-23 16:23:58 -08002449 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002450 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002451 final boolean animated = true;
2452 final boolean springLoaded = true;
2453 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002454 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002455 }
2456 // Otherwise, we are not in spring loaded mode, so don't do anything.
2457 }
2458
Michael Jurkab3e22d92011-10-31 15:58:33 -07002459 void lockAllApps() {
2460 // TODO
2461 }
2462
2463 void unlockAllApps() {
2464 // TODO
2465 }
2466
Adam Cohenfc53cd22011-07-20 15:45:11 -07002467 public boolean isAllAppsCustomizeOpen() {
2468 return mState == State.APPS_CUSTOMIZE;
2469 }
2470
Winson Chungf0ea4d32011-06-06 14:27:16 -07002471 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002472 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002473 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002474 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002475 if (!LauncherApplication.isScreenLarge()) {
2476 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002477 int duration = mSearchDropTargetBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002478 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002479 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002480 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002481 }
2482 }
2483 }
2484
2485 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002486 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002487 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002488 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002489 if (!LauncherApplication.isScreenLarge()) {
2490 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002491 int duration = mSearchDropTargetBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002492 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002493 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002494 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002495 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002496 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002497 }
2498
Patrick Dubroy5f445422011-02-18 14:35:21 -08002499 /**
2500 * Add an item from all apps or customize onto the given workspace screen.
2501 * If layout is null, add to the current screen.
2502 */
2503 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002504 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2505 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002506 } else {
2507 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002508 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002509 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002510
Winson Chungdff8ebb2011-09-08 17:25:31 -07002511 /** Maps the current orientation to an index for referencing orientation correct global icons */
2512 private int getCurrentOrientationIndexForGlobalIcons() {
2513 // default - 0, landscape - 1
2514 switch (getResources().getConfiguration().orientation) {
2515 case Configuration.ORIENTATION_LANDSCAPE:
2516 return 1;
2517 default:
2518 return 0;
2519 }
2520 }
2521
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002522 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002523 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002524 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002525 // Look for the toolbar icon specified in the activity meta-data
2526 Bundle metaData = packageManager.getActivityInfo(
2527 activityName, PackageManager.GET_META_DATA).metaData;
2528 if (metaData != null) {
2529 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2530 if (iconResId != 0) {
2531 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002532 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002533 }
2534 }
2535 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002536 // This can happen if the activity defines an invalid drawable
2537 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2538 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002539 } catch (Resources.NotFoundException nfe) {
2540 // This can happen if the activity defines an invalid drawable
2541 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2542 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002543 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002544 return null;
2545 }
2546
2547 // if successful in getting icon, return it; otherwise, set button to use default drawable
2548 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2549 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002550 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002551 Resources r = getResources();
2552 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2553 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002554
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002555 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002556 // If we were unable to find the icon via the meta-data, use a generic one
2557 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002558 toolbarIcon = r.getDrawable(fallbackDrawableId);
2559 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002560 if (button != null) {
2561 button.setCompoundDrawables(toolbarIcon, null, null, null);
2562 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002563 return null;
2564 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002565 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002566 if (button != null) {
2567 button.setCompoundDrawables(toolbarIcon, null, null, null);
2568 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002569 return toolbarIcon.getConstantState();
2570 }
2571 }
2572
2573 // if successful in getting icon, return it; otherwise, set button to use default drawable
2574 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2575 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2576 ImageView button = (ImageView) findViewById(buttonId);
2577 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2578
Michael Jurka19e0fc52011-07-22 18:00:21 -07002579 if (button != null) {
2580 // If we were unable to find the icon via the meta-data, use a
2581 // generic one
2582 if (toolbarIcon == null) {
2583 button.setImageResource(fallbackDrawableId);
2584 } else {
2585 button.setImageDrawable(toolbarIcon);
2586 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002587 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002588
2589 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2590
Michael Jurka0423dcf2010-10-05 14:56:18 -07002591 }
2592
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002593 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2594 TextView button = (TextView) findViewById(buttonId);
2595 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2596 }
2597
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002598 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002599 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002600 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002601 }
2602
Winson Chungc51db6a2011-10-05 11:44:49 -07002603 private boolean updateGlobalSearchIcon() {
2604 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002605 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2606 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002607 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002608 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002609
Winson Chung1cad91e2011-05-25 17:41:01 -07002610 final SearchManager searchManager =
2611 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2612 ComponentName activityName = searchManager.getGlobalSearchActivity();
2613 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002614 int coi = getCurrentOrientationIndexForGlobalIcons();
2615 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002616 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002617 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002618 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2619 searchButton.setVisibility(View.VISIBLE);
2620 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002621 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002622 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002623 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002624 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2625 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2626 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002627 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002628 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002629 }
2630 }
2631
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002632 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002633 updateButtonWithDrawable(R.id.search_button, d);
2634 }
2635
Winson Chungc51db6a2011-10-05 11:44:49 -07002636 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002637 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002638 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002639 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002640
Winson Chungc51db6a2011-10-05 11:44:49 -07002641 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002642 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2643 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002644 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002645 int coi = getCurrentOrientationIndexForGlobalIcons();
2646 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002647 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002648 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002649 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002650 voiceButton.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002651 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002652 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002653 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002654 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002655 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002656 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002657 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002658 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002659
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002660 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002661 updateButtonWithDrawable(R.id.voice_button, d);
2662 }
2663
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002664 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002665 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002666 */
2667 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002668 final View marketButton = findViewById(R.id.market_button);
2669 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2670 // Find the app market activity by resolving an intent.
2671 // (If multiple app markets are installed, it will return the ResolverActivity.)
2672 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002673 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002674 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002675 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002676 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002677 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002678 marketButton.setVisibility(View.VISIBLE);
2679 } else {
2680 // We should hide and disable the view so that we don't try and restore the visibility
2681 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2682 marketButton.setVisibility(View.GONE);
2683 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002684 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002685 }
2686
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002687 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002688 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002689 }
2690
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002691 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002692 * Displays the shortcut creation dialog and launches, if necessary, the
2693 * appropriate activity.
2694 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002695 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002696 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2697 DialogInterface.OnShowListener {
2698
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002699 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002700
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002701 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002702 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002703
Winson Chung55b65502011-05-26 12:03:43 -07002704 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2705 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002706 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002707
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002708 AlertDialog dialog = builder.create();
2709 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002710 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002711 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002712
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002713 return dialog;
2714 }
2715
2716 public void onCancel(DialogInterface dialog) {
2717 mWaitingForResult = false;
2718 cleanup();
2719 }
2720
Romain Guycbb89e42009-06-08 15:52:54 -07002721 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002722 mWaitingForResult = false;
2723 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002724 }
2725
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002726 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002727 try {
2728 dismissDialog(DIALOG_CREATE_SHORTCUT);
2729 } catch (Exception e) {
2730 // An exception is thrown if the dialog is not visible, which is fine
2731 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002732 }
2733
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002734 /**
2735 * Handle the action clicked in the "Add to home" dialog.
2736 */
2737 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002738 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002739
Winson Chung55b65502011-05-26 12:03:43 -07002740 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2741 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002742 case AddAdapter.ITEM_APPLICATION: {
2743 if (mAppsCustomizeTabHost != null) {
2744 mAppsCustomizeTabHost.selectAppsTab();
2745 }
2746 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002747 break;
2748 }
Winson Chung55b65502011-05-26 12:03:43 -07002749 case AddAdapter.ITEM_APPWIDGET: {
2750 if (mAppsCustomizeTabHost != null) {
2751 mAppsCustomizeTabHost.selectWidgetsTab();
2752 }
2753 showAllApps(true);
2754 break;
2755 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002756 case AddAdapter.ITEM_WALLPAPER: {
2757 startWallpaper();
2758 break;
2759 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002760 }
2761 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002762
2763 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002764 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002765 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002766 }
2767
2768 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002769 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002770 */
2771 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2772 @Override
2773 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002774 closeSystemDialogs();
2775 }
2776 }
2777
2778 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002779 * Receives notifications whenever the appwidgets are reset.
2780 */
2781 private class AppWidgetResetObserver extends ContentObserver {
2782 public AppWidgetResetObserver() {
2783 super(new Handler());
2784 }
2785
2786 @Override
2787 public void onChange(boolean selfChange) {
2788 onAppWidgetReset();
2789 }
2790 }
2791
2792 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002793 * If the activity is currently paused, signal that we need to re-run the loader
2794 * in onResume.
2795 *
2796 * This needs to be called from incoming places where resources might have been loaded
2797 * while we are paused. That is becaues the Configuration might be wrong
2798 * when we're not running, and if it comes back to what it was when we
2799 * were paused, we are not restarted.
2800 *
2801 * Implementation of the method from LauncherModel.Callbacks.
2802 *
2803 * @return true if we are currently paused. The caller might be able to
2804 * skip some work in that case since we will come back again.
2805 */
2806 public boolean setLoadOnResume() {
2807 if (mPaused) {
2808 Log.i(TAG, "setLoadOnResume");
2809 mOnResumeNeedsLoad = true;
2810 return true;
2811 } else {
2812 return false;
2813 }
2814 }
2815
2816 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002817 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002818 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002819 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002820 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002821 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002822 } else {
2823 return SCREEN_COUNT / 2;
2824 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002825 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002826
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002827
Joe Onorato9c1289c2009-08-17 11:03:03 -04002828 /**
2829 * Refreshes the shortcuts shown on the workspace.
2830 *
2831 * Implementation of the method from LauncherModel.Callbacks.
2832 */
2833 public void startBinding() {
2834 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002835
2836 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002837 int count = workspace.getChildCount();
2838 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002839 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002840 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2841 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002842 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002843 if (mHotseat != null) {
2844 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845 }
2846 }
2847
2848 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002849 * Bind the items start-end from the list.
2850 *
2851 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002852 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002853 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002854 setLoadOnResume();
2855
Joe Onorato9c1289c2009-08-17 11:03:03 -04002856 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002857 for (int i=start; i<end; i++) {
2858 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002859
2860 // Short circuit if we are loading dock items for a configuration which has no dock
2861 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2862 mHotseat == null) {
2863 continue;
2864 }
2865
Joe Onorato9c1289c2009-08-17 11:03:03 -04002866 switch (item.itemType) {
2867 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2868 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002869 View shortcut = createShortcut((ShortcutInfo)item);
2870 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2871 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002872 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002873 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002874 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002875 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002876 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002877 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2878 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002879 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002880 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002881 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002882 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002883 }
2884
Joe Onorato9c1289c2009-08-17 11:03:03 -04002885 /**
2886 * Implementation of the method from LauncherModel.Callbacks.
2887 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002888 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002889 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002890 sFolders.clear();
2891 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002892 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002893
2894 /**
2895 * Add the views for a widget to the workspace.
2896 *
2897 * Implementation of the method from LauncherModel.Callbacks.
2898 */
2899 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002900 setLoadOnResume();
2901
Daniel Sandler843e8602010-06-07 14:59:01 -04002902 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2903 if (DEBUG_WIDGETS) {
2904 Log.d(TAG, "bindAppWidget: " + item);
2905 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002906 final Workspace workspace = mWorkspace;
2907
2908 final int appWidgetId = item.appWidgetId;
2909 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002910 if (DEBUG_WIDGETS) {
2911 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2912 }
2913
Joe Onorato9c1289c2009-08-17 11:03:03 -04002914 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2915
Joe Onorato9c1289c2009-08-17 11:03:03 -04002916 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2917 item.hostView.setTag(item);
2918
Winson Chung3d503fb2011-07-13 17:25:49 -07002919 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002920 item.cellY, item.spanX, item.spanY, false);
2921
Adam Cohended9f8d2010-11-03 13:25:16 -07002922 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2923
Joe Onorato9c1289c2009-08-17 11:03:03 -04002924 workspace.requestLayout();
2925
Daniel Sandler843e8602010-06-07 14:59:01 -04002926 if (DEBUG_WIDGETS) {
2927 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2928 + (SystemClock.uptimeMillis()-start) + "ms");
2929 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002930 }
2931
2932 /**
2933 * Callback saying that there aren't any more items to bind.
2934 *
2935 * Implementation of the method from LauncherModel.Callbacks.
2936 */
2937 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002938 setLoadOnResume();
2939
Joe Onorato9c1289c2009-08-17 11:03:03 -04002940 if (mSavedState != null) {
2941 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002942 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002943 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002944 mSavedState = null;
2945 }
2946
2947 if (mSavedInstanceState != null) {
2948 super.onRestoreInstanceState(mSavedInstanceState);
2949 mSavedInstanceState = null;
2950 }
2951
Joe Onorato9c1289c2009-08-17 11:03:03 -04002952 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002953
2954 // If we received the result of any pending adds while the loader was running (e.g. the
2955 // widget configuration forced an orientation change), process them now.
2956 for (int i = 0; i < sPendingAddList.size(); i++) {
2957 completeAdd(sPendingAddList.get(i));
2958 }
2959 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002960
Winson Chungc51db6a2011-10-05 11:44:49 -07002961 // Update the market app icon as necessary (the other icons will be managed in response to
2962 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002963 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07002964
2965 mWorkspace.post(mBuildLayersRunnable);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002966 }
2967
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002968 @Override
2969 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07002970 boolean searchVisible = updateGlobalSearchIcon();
2971 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
2972 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002973 }
2974
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002975 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002976 * Add the icons for all apps.
2977 *
2978 * Implementation of the method from LauncherModel.Callbacks.
2979 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002980 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2981 // Remove the progress bar entirely; we could also make it GONE
2982 // but better to remove it since we know it's not going to be used
2983 View progressBar = mAppsCustomizeTabHost.
2984 findViewById(R.id.apps_customize_progress_bar);
2985 if (progressBar != null) {
2986 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002987 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002988 // We just post the call to setApps so the user sees the progress bar
2989 // disappear-- otherwise, it just looks like the progress bar froze
2990 // which doesn't look great
2991 mAppsCustomizeTabHost.post(new Runnable() {
2992 public void run() {
2993 if (mAppsCustomizeContent != null) {
2994 mAppsCustomizeContent.setApps(apps);
2995 }
2996 }
2997 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002998 }
2999
3000 /**
3001 * A package was installed.
3002 *
3003 * Implementation of the method from LauncherModel.Callbacks.
3004 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003005 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003006 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003007 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003008
Winson Chung785d2eb2011-04-14 16:08:02 -07003009 if (mAppsCustomizeContent != null) {
3010 mAppsCustomizeContent.addApps(apps);
3011 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003012 }
3013
3014 /**
3015 * A package was updated.
3016 *
3017 * Implementation of the method from LauncherModel.Callbacks.
3018 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003019 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003020 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003021 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003022 if (mWorkspace != null) {
3023 mWorkspace.updateShortcuts(apps);
3024 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003025
Winson Chung785d2eb2011-04-14 16:08:02 -07003026 if (mAppsCustomizeContent != null) {
3027 mAppsCustomizeContent.updateApps(apps);
3028 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003029 }
3030
3031 /**
3032 * A package was uninstalled.
3033 *
3034 * Implementation of the method from LauncherModel.Callbacks.
3035 */
Joe Onorato36115782010-06-17 13:28:48 -04003036 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003037 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003038 if (permanent) {
3039 mWorkspace.removeItems(apps);
3040 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003041
Winson Chung785d2eb2011-04-14 16:08:02 -07003042 if (mAppsCustomizeContent != null) {
3043 mAppsCustomizeContent.removeApps(apps);
3044 }
Winson Chunga1820962011-10-03 16:31:06 -07003045
3046 // Notify the drag controller
3047 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003048 }
Joe Onoratobe386092009-11-17 17:32:16 -08003049
3050 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003051 * A number of packages were updated.
3052 */
3053 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003054 if (mAppsCustomizeContent != null) {
3055 mAppsCustomizeContent.onPackagesUpdated();
3056 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003057 }
3058
Winson Chung400438b2011-01-16 17:53:48 -08003059 private int mapConfigurationOriActivityInfoOri(int configOri) {
3060 final Display d = getWindowManager().getDefaultDisplay();
3061 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3062 switch (d.getRotation()) {
3063 case Surface.ROTATION_0:
3064 case Surface.ROTATION_180:
3065 // We are currently in the same basic orientation as the natural orientation
3066 naturalOri = configOri;
3067 break;
3068 case Surface.ROTATION_90:
3069 case Surface.ROTATION_270:
3070 // We are currently in the other basic orientation to the natural orientation
3071 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3072 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3073 break;
3074 }
3075
3076 int[] oriMap = {
3077 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3078 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3079 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3080 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3081 };
3082 // Since the map starts at portrait, we need to offset if this device's natural orientation
3083 // is landscape.
3084 int indexOffset = 0;
3085 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3086 indexOffset = 1;
3087 }
3088 return oriMap[(d.getRotation() + indexOffset) % 4];
3089 }
Adam Cohen446e9402011-09-15 18:21:21 -07003090
3091 public void lockScreenOrientationOnLargeUI() {
3092 if (LauncherApplication.isScreenLarge()) {
3093 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3094 .getConfiguration().orientation));
3095 }
Winson Chung400438b2011-01-16 17:53:48 -08003096 }
Adam Cohen446e9402011-09-15 18:21:21 -07003097 public void unlockScreenOrientationOnLargeUI() {
3098 if (LauncherApplication.isScreenLarge()) {
3099 mHandler.postDelayed(new Runnable() {
3100 public void run() {
3101 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3102 }
3103 }, mRestoreScreenOrientationDelay);
3104 }
Winson Chung400438b2011-01-16 17:53:48 -08003105 }
3106
Winson Chung82f55532011-08-09 14:14:23 -07003107 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003108 private static final String PREFS_KEY = "com.android.launcher2.prefs";
3109 private boolean isClingsEnabled() {
Winson Chung82f55532011-08-09 14:14:23 -07003110 // TEMPORARY: DISABLE CLINGS ON LARGE UI
Winson Chung7d7541e2011-09-16 20:14:36 -07003111 if (LauncherApplication.isScreenLarge()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003112 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003113 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003114
Winson Chung7d7541e2011-09-16 20:14:36 -07003115 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003116 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003117 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3118 Cling cling = (Cling) findViewById(clingId);
3119 if (cling != null) {
3120 cling.init(this, positionData);
3121 cling.setVisibility(View.VISIBLE);
3122 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3123 if (animate) {
3124 cling.buildLayer();
3125 cling.setAlpha(0f);
3126 cling.animate()
3127 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003128 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003129 .setDuration(SHOW_CLING_DURATION)
3130 .setStartDelay(delay)
3131 .start();
3132 } else {
3133 cling.setAlpha(1f);
3134 }
3135 }
3136 return cling;
3137 }
3138 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003139 if (cling != null) {
3140 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003141 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003142 anim.addListener(new AnimatorListenerAdapter() {
3143 public void onAnimationEnd(Animator animation) {
3144 cling.setVisibility(View.GONE);
3145 cling.cleanup();
3146 SharedPreferences prefs =
3147 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3148 SharedPreferences.Editor editor = prefs.edit();
3149 editor.putBoolean(flag, true);
3150 editor.commit();
3151 };
3152 });
3153 anim.start();
3154 }
3155 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003156 private void removeCling(int id) {
3157 final View cling = findViewById(id);
3158 if (cling != null) {
3159 final ViewGroup parent = (ViewGroup) cling.getParent();
3160 parent.post(new Runnable() {
3161 @Override
3162 public void run() {
3163 parent.removeView(cling);
3164 }
3165 });
3166 }
3167 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003168 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003169 // Enable the clings only if they have not been dismissed before
3170 SharedPreferences prefs =
3171 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003172 if (isClingsEnabled() && !prefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003173 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003174 } else {
3175 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003176 }
3177 }
3178 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003179 // Enable the clings only if they have not been dismissed before
3180 SharedPreferences prefs =
3181 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003182 if (isClingsEnabled() && !prefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003183 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003184 } else {
3185 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003186 }
3187 }
3188 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003189 // Enable the clings only if they have not been dismissed before
3190 SharedPreferences prefs =
3191 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
3192 Cling cling = null;
Winson Chung9d9d74f2011-09-19 11:49:12 -07003193 if (isClingsEnabled() && !prefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003194 cling = initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003195 } else {
3196 removeCling(R.id.folder_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003197 }
3198 return cling;
3199 }
3200 public boolean isFolderClingVisible() {
3201 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003202 if (cling != null) {
3203 return cling.getVisibility() == View.VISIBLE;
3204 }
3205 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003206 }
Winson Chung82f55532011-08-09 14:14:23 -07003207 public void dismissWorkspaceCling(View v) {
3208 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003209 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003210 }
3211 public void dismissAllAppsCling(View v) {
3212 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003213 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3214 }
3215 public void dismissFolderCling(View v) {
3216 Cling cling = (Cling) findViewById(R.id.folder_cling);
3217 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003218 }
3219
Winson Chung80baf5a2010-08-09 16:03:15 -07003220 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003221 * Prints out out state for debugging.
3222 */
3223 public void dumpState() {
3224 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003225 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003226 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3227 Log.d(TAG, "mRestoring=" + mRestoring);
3228 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3229 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003230 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003231 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003232
Winson Chung785d2eb2011-04-14 16:08:02 -07003233 if (mAppsCustomizeContent != null) {
3234 mAppsCustomizeContent.dumpState();
3235 }
Joe Onoratobe386092009-11-17 17:32:16 -08003236 Log.d(TAG, "END launcher2 dump state");
3237 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003238
3239 @Override
3240 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3241 super.dump(prefix, fd, writer, args);
3242 writer.println(" ");
3243 writer.println("Debug logs: ");
3244 for (int i = 0; i < sDumpLogs.size(); i++) {
3245 writer.println(" " + sDumpLogs.get(i));
3246 }
3247 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003248}
Michael Jurka2763be32011-02-24 11:19:57 -08003249
Michael Jurkaabded662011-03-04 12:06:57 -08003250interface LauncherTransitionable {
Winson Chung7d7541e2011-09-16 20:14:36 -07003251 void onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace);
3252 void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003253}