blob: 17320507697720b6cbba4c069cedf6d8fa7e983f [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.Dialog;
30import android.app.SearchManager;
31import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070036import android.content.ClipData;
37import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
41import android.content.DialogInterface;
42import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040053import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070054import android.os.AsyncTask;
Adam Cohen50370f32011-08-25 18:57:14 -070055import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020057import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080058import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070059import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040060import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080061import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070062import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080063import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
66import android.text.TextUtils;
67import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070068import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080069import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080070import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.view.KeyEvent;
72import android.view.LayoutInflater;
73import android.view.Menu;
74import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070075import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080076import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.view.View;
Winson Chung82f55532011-08-09 14:14:23 -070078import android.view.ViewGroup;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070079import android.view.WindowManager;
Adam Cohen82ebbd22011-09-30 15:05:40 -070080import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080081import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070082import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070083import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080084import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070086import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070088import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070089import android.widget.TextView;
90import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070091
Adam Cohendf2cc412011-04-27 16:56:57 -070092import com.android.common.Search;
93import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070094import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080095
Adam Cohenc0dcf592011-06-01 15:30:43 -070096import java.io.DataInputStream;
97import java.io.DataOutputStream;
98import java.io.FileNotFoundException;
99import java.io.IOException;
100import java.util.ArrayList;
101import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800103/**
104 * Default launcher application.
105 */
Michael Jurka946ad472010-07-09 18:05:18 -0700106public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700107 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
108 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800109 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700110 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800111
Joe Onorato9c1289c2009-08-17 11:03:03 -0400112 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400113 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700114
Winson Chung70d72102011-08-12 11:24:35 -0700115 private static final int MENU_GROUP_WALLPAPER = 1;
116 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
117 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700118 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
119 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800120
121 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700122 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123 private static final int REQUEST_PICK_APPLICATION = 6;
124 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700125 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700126 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
128 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
129
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800130 static final int SCREEN_COUNT = 5;
131 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
Joe Onorato7c312c12009-08-13 21:36:53 -0700133 static final int DIALOG_CREATE_SHORTCUT = 1;
134 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
Romain Guy98d01652009-06-30 16:21:04 -0700136 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
138 // Type: int
139 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700140 // Type: int
141 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700143 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
144 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800145 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
146 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700147 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700149 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 // Type: boolean
151 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
152 // Type: long
153 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
154
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700155 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
156
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700157 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700158 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700159 private State mState = State.WORKSPACE;
160 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700161
Joe Onorato9c1289c2009-08-17 11:03:03 -0400162 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700163 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
164 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700165 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700166 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800169 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800171 private final BroadcastReceiver mCloseSystemDialogsReceiver
172 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800173 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private LayoutInflater mInflater;
176
Joe Onorato00acb122009-08-04 16:04:30 -0400177 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700179
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700180 private AppWidgetManager mAppWidgetManager;
181 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700182
Michael Jurkac9d95c52011-08-29 14:03:34 -0700183 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700184 private int[] mTmpAddItemCellCoordinates = new int[2];
185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private FolderInfo mFolderInfo;
187
Winson Chung3d503fb2011-07-13 17:25:49 -0700188 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800189 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700190
Winson Chungc51db6a2011-10-05 11:44:49 -0700191 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700192 private AppsCustomizeTabHost mAppsCustomizeTabHost;
193 private AppsCustomizePagedView mAppsCustomizeContent;
194 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196 private Bundle mSavedState;
197
198 private SpannableStringBuilder mDefaultKeySsb = null;
199
Joe Onorato9c1289c2009-08-17 11:03:03 -0400200 private boolean mWorkspaceLoading = true;
201
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800202 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203 private boolean mRestoring;
204 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700205 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
207 private Bundle mSavedInstanceState;
208
Joe Onorato9c1289c2009-08-17 11:03:03 -0400209 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800210 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800211 private boolean mUserPresent = true;
212 private boolean mVisible = false;
213 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400214
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700215 private static LocaleConfiguration sLocaleConfiguration = null;
216
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700217 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700218
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700219 private Intent mAppMarketIntent = null;
220
Adam Cohended9f8d2010-11-03 13:25:16 -0700221 // Related to the auto-advancing of widgets
222 private final int ADVANCE_MSG = 1;
223 private final int mAdvanceInterval = 20000;
224 private final int mAdvanceStagger = 250;
225 private long mAutoAdvanceSentTime;
226 private long mAutoAdvanceTimeLeft = -1;
227 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
228 new HashMap<View, AppWidgetProviderInfo>();
229
Winson Chung400438b2011-01-16 17:53:48 -0800230 // Determines how long to wait after a rotation before restoring the screen orientation to
231 // match the sensor state.
232 private final int mRestoreScreenOrientationDelay = 500;
233
Michael Jurka4ef207b2010-11-29 17:05:45 -0800234 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700235 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
236 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
237 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800238
Adam Cohen2801caf2011-05-13 20:57:39 -0700239 private DragLayer mDragLayer;
240
Michael Jurkaddd62e92011-02-16 17:49:14 -0800241 private BubbleTextView mWaitingForResume;
242
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800243 private static ArrayList<PendingAddArguments> sPendingAddList
244 = new ArrayList<PendingAddArguments>();
245
246 private static class PendingAddArguments {
247 int requestCode;
248 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700249 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800250 int screen;
251 int cellX;
252 int cellY;
253 }
254
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 @Override
256 protected void onCreate(Bundle savedInstanceState) {
257 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800258 LauncherApplication app = ((LauncherApplication)getApplication());
259 mModel = app.setLauncher(this);
260 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400261 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800262 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700263
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700264 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700265 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
266 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700267
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200269 android.os.Debug.startMethodTracing(
270 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 }
272
273 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 setContentView(R.layout.launcher);
275 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700276 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800278 registerContentObservers();
279
Joe Onorato7c312c12009-08-13 21:36:53 -0700280 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 mSavedState = savedInstanceState;
283 restoreState(mSavedState);
284
Winson Chunga12a2502010-12-20 14:41:35 -0800285 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700286 if (mAppsCustomizeContent != null) {
287 mAppsCustomizeContent.onPackagesUpdated();
288 }
Winson Chunga12a2502010-12-20 14:41:35 -0800289
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 if (PROFILE_STARTUP) {
291 android.os.Debug.stopMethodTracing();
292 }
293
294 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400295 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 }
297
Michael Jurkac57b7a82011-08-09 22:02:20 -0700298 if (!mModel.isAllAppsLoaded()) {
299 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
300 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
301 }
302
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 // For handling default keys
304 mDefaultKeySsb = new SpannableStringBuilder();
305 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800306
307 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
308 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800309
Winson Chungc51db6a2011-10-05 11:44:49 -0700310 boolean searchVisible = false;
311 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800312 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700313 int coi = getCurrentOrientationIndexForGlobalIcons();
314 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
315 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700316 updateAppMarketIcon();
317 searchVisible = updateGlobalSearchIcon();
318 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700319 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700320 if (sGlobalSearchIcon[coi] != null) {
321 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700322 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700323 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700324 if (sVoiceSearchIcon[coi] != null) {
325 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700326 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700327 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700328 if (sAppMarketIcon[coi] != null) {
329 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800330 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700331 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700332
333 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohendc16b602011-09-26 15:12:33 -0700334 if (LauncherApplication.isScreenLarge() || Build.TYPE.contentEquals("eng")) {
Adam Cohen446e9402011-09-15 18:21:21 -0700335 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 }
Romain Guycbb89e42009-06-08 15:52:54 -0700338
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800339 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700340 if (sLocaleConfiguration == null) {
341 new AsyncTask<Void, Void, LocaleConfiguration>() {
342 @Override
343 protected LocaleConfiguration doInBackground(Void... unused) {
344 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
345 readConfiguration(Launcher.this, localeConfiguration);
346 return localeConfiguration;
347 }
348
349 @Override
350 protected void onPostExecute(LocaleConfiguration result) {
351 sLocaleConfiguration = result;
352 checkForLocaleChange(); // recursive, but now with a locale configuration
353 }
354 }.execute();
355 return;
356 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700357
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 final Configuration configuration = getResources().getConfiguration();
359
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700360 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 final String locale = configuration.locale.toString();
362
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700363 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 final int mcc = configuration.mcc;
365
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700366 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800367 final int mnc = configuration.mnc;
368
Romain Guy84f296c2009-11-04 15:00:44 -0800369 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370
Romain Guy84f296c2009-11-04 15:00:44 -0800371 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700372 sLocaleConfiguration.locale = locale;
373 sLocaleConfiguration.mcc = mcc;
374 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700375
Joe Onorato0589f0f2010-02-08 13:44:00 -0800376 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700377
378 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
379 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700380 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700381 public void run() {
382 writeConfiguration(Launcher.this, localeConfiguration);
383 }
384 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700385 }
386 }
387
388 private static class LocaleConfiguration {
389 public String locale;
390 public int mcc = -1;
391 public int mnc = -1;
392 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700393
Romain Guy98d01652009-06-30 16:21:04 -0700394 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
395 DataInputStream in = null;
396 try {
397 in = new DataInputStream(context.openFileInput(PREFERENCES));
398 configuration.locale = in.readUTF();
399 configuration.mcc = in.readInt();
400 configuration.mnc = in.readInt();
401 } catch (FileNotFoundException e) {
402 // Ignore
403 } catch (IOException e) {
404 // Ignore
405 } finally {
406 if (in != null) {
407 try {
408 in.close();
409 } catch (IOException e) {
410 // Ignore
411 }
412 }
413 }
414 }
415
416 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
417 DataOutputStream out = null;
418 try {
419 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
420 out.writeUTF(configuration.locale);
421 out.writeInt(configuration.mcc);
422 out.writeInt(configuration.mnc);
423 out.flush();
424 } catch (FileNotFoundException e) {
425 // Ignore
426 } catch (IOException e) {
427 //noinspection ResultOfMethodCallIgnored
428 context.getFileStreamPath(PREFERENCES).delete();
429 } finally {
430 if (out != null) {
431 try {
432 out.close();
433 } catch (IOException e) {
434 // Ignore
435 }
436 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800437 }
438 }
439
Adam Cohen716b51e2011-06-30 12:09:54 -0700440 public DragLayer getDragLayer() {
441 return mDragLayer;
442 }
443
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 static int getScreen() {
445 synchronized (sLock) {
446 return sScreen;
447 }
448 }
449
450 static void setScreen(int screen) {
451 synchronized (sLock) {
452 sScreen = screen;
453 }
454 }
455
Winson Chung557d6ed2011-07-08 15:34:52 -0700456 /**
457 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
458 * a configuration step, this allows the proper animations to run after other transitions.
459 */
460 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700461 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800462 switch (args.requestCode) {
463 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700464 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
465 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800466 break;
467 case REQUEST_PICK_SHORTCUT:
468 processShortcut(args.intent);
469 break;
470 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700471 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
472 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700473 result = true;
474 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800475 case REQUEST_PICK_APPWIDGET:
476 addAppWidgetFromPick(args.intent);
477 break;
478 case REQUEST_CREATE_APPWIDGET:
479 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700480 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700481 result = true;
482 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800483 case REQUEST_PICK_WALLPAPER:
484 // We just wanted the activity result here so we can clear mWaitingForResult
485 break;
486 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700487 // In any situation where we have a multi-step drop, we should reset the add info only after
488 // we complete the drop
489 resetAddInfo();
490 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800491 }
492
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800494 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700495 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700496 mWaitingForResult = false;
497
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 // The pattern used here is that a user PICKs a specific application,
499 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700500
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
502 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700503 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800504 final PendingAddArguments args = new PendingAddArguments();
505 args.requestCode = requestCode;
506 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700507 args.container = mPendingAddInfo.container;
508 args.screen = mPendingAddInfo.screen;
509 args.cellX = mPendingAddInfo.cellX;
510 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800511
512 // If the loader is still running, defer the add until it is done.
513 if (isWorkspaceLocked()) {
514 sPendingAddList.add(args);
515 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700516 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800517 }
Romain Guy18042c82009-11-06 11:44:55 -0800518 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700519 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
520 if (data != null) {
521 // Clean up the appWidgetId if we canceled
522 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
523 if (appWidgetId != -1) {
524 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
525 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800526 }
527 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700528
529 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700530 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531 }
532
533 @Override
534 protected void onResume() {
535 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800536 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700537 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400538 mWorkspaceLoading = true;
539 mModel.startLoader(this, true);
540 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700541 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800542 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800543 if (mWaitingForResume != null) {
544 mWaitingForResume.setStayPressed(false);
545 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700546 // When we resume Launcher, a different Activity might be responsible for the app
547 // market intent, so refresh the icon
548 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800549 }
550
551 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700552 protected void onPause() {
553 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700554 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800555 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700556 }
Romain Guycbb89e42009-06-08 15:52:54 -0700557
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700558 @Override
559 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400560 // Flag the loader to stop early before switching
561 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700562 if (mAppsCustomizeContent != null) {
563 mAppsCustomizeContent.surrender();
564 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800565 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700566 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700567
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800568 // We can't hide the IME if it was forced open. So don't bother
569 /*
570 @Override
571 public void onWindowFocusChanged(boolean hasFocus) {
572 super.onWindowFocusChanged(hasFocus);
573
574 if (hasFocus) {
575 final InputMethodManager inputManager = (InputMethodManager)
576 getSystemService(Context.INPUT_METHOD_SERVICE);
577 WindowManager.LayoutParams lp = getWindow().getAttributes();
578 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
579 android.os.Handler()) {
580 protected void onReceiveResult(int resultCode, Bundle resultData) {
581 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
582 }
583 });
584 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
585 }
586 }
587 */
588
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800589 private boolean acceptFilter() {
590 final InputMethodManager inputManager = (InputMethodManager)
591 getSystemService(Context.INPUT_METHOD_SERVICE);
592 return !inputManager.isFullscreenMode();
593 }
594
595 @Override
596 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700597 final int uniChar = event.getUnicodeChar();
598 final boolean handled = super.onKeyDown(keyCode, event);
599 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
600 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800601 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
602 keyCode, event);
603 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700604 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700605 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700606 // showSearchDialog()
607 // If there are multiple keystrokes before the search dialog takes focus,
608 // onSearchRequested() will be called for every keystroke,
609 // but it is idempotent, so it's fine.
610 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 }
612 }
613
Joe Onorato8a9625e2010-01-28 15:55:35 -0800614 // Eat the long press event so the keyboard doesn't come up.
615 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
616 return true;
617 }
618
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 return handled;
620 }
621
Karl Rosaen138a0412009-04-23 19:00:21 -0700622 private String getTypedText() {
623 return mDefaultKeySsb.toString();
624 }
625
626 private void clearTypedText() {
627 mDefaultKeySsb.clear();
628 mDefaultKeySsb.clearSpans();
629 Selection.setSelection(mDefaultKeySsb, 0);
630 }
631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700633 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
634 * State
635 */
636 private static State intToState(int stateOrdinal) {
637 State state = State.WORKSPACE;
638 final State[] stateValues = State.values();
639 for (int i = 0; i < stateValues.length; i++) {
640 if (stateValues[i].ordinal() == stateOrdinal) {
641 state = stateValues[i];
642 break;
643 }
644 }
645 return state;
646 }
647
648 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 * Restores the previous state, if it exists.
650 *
651 * @param savedState The previous state.
652 */
653 private void restoreState(Bundle savedState) {
654 if (savedState == null) {
655 return;
656 }
657
Michael Jurka883f55b2010-10-21 15:47:14 -0700658 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700659 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800660 showAllApps(false);
661 }
662
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
664 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700665 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 }
667
Winson Chung3d503fb2011-07-13 17:25:49 -0700668 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
669 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700670
Winson Chung3d503fb2011-07-13 17:25:49 -0700671 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
672 mPendingAddInfo.container = pendingAddContainer;
673 mPendingAddInfo.screen = pendingAddScreen;
674 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
675 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 mRestoring = true;
677 }
678
679 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
680 if (renameFolder) {
681 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700682 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 mRestoring = true;
684 }
Winson Chunga12a2502010-12-20 14:41:35 -0800685
Winson Chung785d2eb2011-04-14 16:08:02 -0700686
687 // Restore the AppsCustomize tab
688 if (mAppsCustomizeTabHost != null) {
689 String curTab = savedState.getString("apps_customize_currentTab");
690 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700691 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700692 mAppsCustomizeContent.setContentType(
693 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
694 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700695 mAppsCustomizeContent.loadAssociatedPages(
696 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700697 }
698
Winson Chung5afbf7b2011-07-25 11:53:08 -0700699 int currentIndex = savedState.getInt("apps_customize_currentIndex");
700 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700701 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800702 }
703
704 /**
705 * Finds all the views we need and configure them properly.
706 */
707 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700708 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400709
Winson Chung4c98d922011-05-31 16:50:48 -0700710 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
711 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800712
Winson Chung4c98d922011-05-31 16:50:48 -0700713 // Setup the drag layer
714 mDragLayer.setup(this, dragController);
715
Winson Chung3d503fb2011-07-13 17:25:49 -0700716 // Setup the hotseat
717 mHotseat = (Hotseat) findViewById(R.id.hotseat);
718 if (mHotseat != null) {
719 mHotseat.setup(this);
720 }
721
Winson Chung4c98d922011-05-31 16:50:48 -0700722 // Setup the workspace
723 mWorkspace.setHapticFeedbackEnabled(false);
724 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700725 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700726 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700727
Winson Chungf0ea4d32011-06-06 14:27:16 -0700728 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700729 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700730
Winson Chungf0ea4d32011-06-06 14:27:16 -0700731 // Setup AppsCustomize
732 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
733 findViewById(R.id.apps_customize_pane);
734 mAppsCustomizeContent = (AppsCustomizePagedView)
735 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
736 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400737
Romain Guy1fbc1c82009-11-09 20:43:08 -0800738
Winson Chung4c98d922011-05-31 16:50:48 -0700739
Winson Chung3d503fb2011-07-13 17:25:49 -0700740 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700741 dragController.setDragScoller(mWorkspace);
742 dragController.setScrollView(mDragLayer);
743 dragController.setMoveTarget(mWorkspace);
744 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700745 if (mSearchDropTargetBar != null) {
746 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800747 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800748 }
749
750 /**
751 * Creates a view representing a shortcut.
752 *
753 * @param info The data structure describing the shortcut.
754 *
755 * @return A View inflated from R.layout.application.
756 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800757 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700759 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
761
762 /**
763 * Creates a view representing a shortcut inflated from the specified resource.
764 *
765 * @param layoutResId The id of the XML layout used to create the shortcut.
766 * @param parent The group the shortcut belongs to.
767 * @param info The data structure describing the shortcut.
768 *
769 * @return A View inflated from layoutResId.
770 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800771 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800772 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
773 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 return favorite;
776 }
777
778 /**
779 * Add an application shortcut to the workspace.
780 *
781 * @param data The intent describing the application.
782 * @param cellInfo The position on screen where to create the shortcut.
783 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700784 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700785 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700786 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700787
Adam Cohenfbba09b2011-07-18 21:43:05 -0700788 // First we check if we already know the exact location where we want to add this item.
789 if (cellX >= 0 && cellY >= 0) {
790 cellXY[0] = cellX;
791 cellXY[1] = cellY;
792 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700793 showOutOfSpaceMessage();
794 return;
795 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800797 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800798
Romain Guy73b979d2009-06-09 12:57:21 -0700799 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800800 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800802 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700803 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700804 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800805 } else {
806 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800807 }
808 }
Romain Guycbb89e42009-06-08 15:52:54 -0700809
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810 /**
811 * Add a shortcut to the workspace.
812 *
813 * @param data The intent describing the shortcut.
814 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700816 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
817 int cellY) {
818 int[] cellXY = mTmpAddItemCellCoordinates;
819 int[] touchXY = mPendingAddInfo.dropPos;
820 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700821
Michael Jurkad3ef3062010-11-23 16:23:58 -0800822 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700823
Adam Cohen558baaf2011-08-15 15:22:57 -0700824 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
825 final View view = createShortcut(info);
826
Adam Cohenfbba09b2011-07-18 21:43:05 -0700827 // First we check if we already know the exact location where we want to add this item.
828 if (cellX >= 0 && cellY >= 0) {
829 cellXY[0] = cellX;
830 cellXY[1] = cellY;
831 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700832
833 // If appropriate, either create a folder or add to an existing folder
834 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
835 true, null,null)) {
836 return;
837 }
838 DragObject dragObject = new DragObject();
839 dragObject.dragInfo = info;
840 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
841 return;
842 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700843 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800844 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700845 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800846 foundCellSpan = (result != null);
847 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700848 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800849 }
850
851 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700852 showOutOfSpaceMessage();
853 return;
854 }
855
Adam Cohen558baaf2011-08-15 15:22:57 -0700856 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857
858 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700859 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
860 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 }
862 }
863
Adam Cohen50370f32011-08-25 18:57:14 -0700864 class Padding {
865 int left = 0;
866 int right = 0;
867 int top = 0;
868 int bottom = 0;
869 }
870
Adam Cohenf814aa02011-09-01 13:48:05 -0700871 Padding getPaddingForWidget(ComponentName component) {
Adam Cohen50370f32011-08-25 18:57:14 -0700872 PackageManager packageManager = getPackageManager();
873 Padding p = new Padding();
874 android.content.pm.ApplicationInfo appInfo;
875
876 try {
Adam Cohenf814aa02011-09-01 13:48:05 -0700877 appInfo = packageManager.getApplicationInfo(component.getPackageName(), 0);
Adam Cohen50370f32011-08-25 18:57:14 -0700878 } catch (Exception e) {
879 // if we can't find the package, return 0 padding
880 return p;
881 }
882
Adam Cohenff648b52011-09-19 11:57:30 -0700883 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Adam Cohen50370f32011-08-25 18:57:14 -0700884 Resources r = getResources();
Adam Cohen82ebbd22011-09-30 15:05:40 -0700885 // The default padding values are private API currently, but will be added in
886 // API level 15. The current values are (8, 8, 8, 8).
887 p.left = r.getDimensionPixelSize(com.android.internal.
888 R.dimen.default_app_widget_padding_left);
889 p.right = r.getDimensionPixelSize(com.android.internal.
890 R.dimen.default_app_widget_padding_right);
891 p.top = r.getDimensionPixelSize(com.android.internal.
892 R.dimen.default_app_widget_padding_top);
893 p.bottom = r.getDimensionPixelSize(com.android.internal.
894 R.dimen.default_app_widget_padding_bottom);
Adam Cohen50370f32011-08-25 18:57:14 -0700895 }
896
897 return p;
898 }
899
Adam Cohenf814aa02011-09-01 13:48:05 -0700900 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
901 if (spanXY == null) {
902 spanXY = new int[2];
903 }
904
905 Padding padding = getPaddingForWidget(component);
906 // We want to account for the extra amount of padding that we are adding to the widget
907 // to ensure that it gets the full amount of space that it has requested
908 int requiredWidth = minWidth + padding.left + padding.right;
909 int requiredHeight = minHeight + padding.top + padding.bottom;
910 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
911 }
912
913 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
914 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
915 }
916
Adam Cohencbf47e32011-09-16 17:32:37 -0700917 int[] getMinResizeSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
918 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
919 }
920
Adam Cohenf814aa02011-09-01 13:48:05 -0700921 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
922 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
923 }
924
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800925 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700926 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800927 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700928 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700929 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800930 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700931 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700932 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700933
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700934 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700935 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700936
Adam Cohenf814aa02011-09-01 13:48:05 -0700937 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700938
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700940 // We have saved the position to which the widget was dragged-- this really only matters
941 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700942 int[] cellXY = mTmpAddItemCellCoordinates;
943 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700944 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700945 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
946 cellXY[0] = mPendingAddInfo.cellX;
947 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700948 foundCellSpan = true;
949 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700950 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700951 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700952 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800953 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700954 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700955 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700956 }
957
Michael Jurka0280c3b2010-09-17 15:00:07 -0700958 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800959 if (appWidgetId != -1) {
960 // Deleting an app widget ID is a void call but writes to disk before returning
961 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800962 new Thread("deleteAppWidgetId") {
963 public void run() {
964 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
965 }
966 }.start();
967 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700968 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800969 return;
970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800971
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700972 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -0700973 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700974 launcherInfo.spanX = spanXY[0];
975 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700976
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700978 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979
980 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700982 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700983
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700984 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700986
Winson Chung3d503fb2011-07-13 17:25:49 -0700987 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400988 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700989
990 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 }
992 }
Romain Guycbb89e42009-06-08 15:52:54 -0700993
Adam Cohended9f8d2010-11-03 13:25:16 -0700994 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
995 @Override
996 public void onReceive(Context context, Intent intent) {
997 final String action = intent.getAction();
998 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
999 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001000 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001001 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001002
Winson Chungc100e8e2011-08-09 16:02:43 -07001003 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001004 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001005 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1006 mAppsCustomizeTabHost.reset();
1007 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001008 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001009 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1010 mUserPresent = true;
1011 updateRunning();
1012 }
1013 }
1014 };
1015
1016 @Override
1017 public void onAttachedToWindow() {
1018 super.onAttachedToWindow();
1019
1020 // Listen for broadcasts related to user-presence
1021 final IntentFilter filter = new IntentFilter();
1022 filter.addAction(Intent.ACTION_SCREEN_OFF);
1023 filter.addAction(Intent.ACTION_USER_PRESENT);
1024 registerReceiver(mReceiver, filter);
1025
Adam Cohend113e0c2010-11-11 10:48:05 -08001026 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001027 mVisible = true;
1028 }
1029
1030 @Override
1031 public void onDetachedFromWindow() {
1032 super.onDetachedFromWindow();
1033 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001034 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001035
Adam Cohend113e0c2010-11-11 10:48:05 -08001036 if (mAttached) {
1037 unregisterReceiver(mReceiver);
1038 mAttached = false;
1039 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001040 updateRunning();
1041 }
1042
1043 public void onWindowVisibilityChanged(int visibility) {
1044 mVisible = visibility == View.VISIBLE;
1045 updateRunning();
1046 }
1047
1048 private void sendAdvanceMessage(long delay) {
1049 mHandler.removeMessages(ADVANCE_MSG);
1050 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1051 mHandler.sendMessageDelayed(msg, delay);
1052 mAutoAdvanceSentTime = System.currentTimeMillis();
1053 }
1054
1055 private void updateRunning() {
1056 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1057 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1058 mAutoAdvanceRunning = autoAdvanceRunning;
1059 if (autoAdvanceRunning) {
1060 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1061 sendAdvanceMessage(delay);
1062 } else {
1063 if (!mWidgetsToAdvance.isEmpty()) {
1064 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1065 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1066 }
1067 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001068 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001069 }
1070 }
1071 }
1072
1073 private final Handler mHandler = new Handler() {
1074 @Override
1075 public void handleMessage(Message msg) {
1076 if (msg.what == ADVANCE_MSG) {
1077 int i = 0;
1078 for (View key: mWidgetsToAdvance.keySet()) {
1079 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1080 final int delay = mAdvanceStagger * i;
1081 if (v instanceof Advanceable) {
1082 postDelayed(new Runnable() {
1083 public void run() {
1084 ((Advanceable) v).advance();
1085 }
1086 }, delay);
1087 }
1088 i++;
1089 }
1090 sendAdvanceMessage(mAdvanceInterval);
1091 }
1092 }
1093 };
1094
1095 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001096 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001097 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1098 if (v instanceof Advanceable) {
1099 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001100 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001101 updateRunning();
1102 }
1103 }
1104
1105 void removeWidgetToAutoAdvance(View hostView) {
1106 if (mWidgetsToAdvance.containsKey(hostView)) {
1107 mWidgetsToAdvance.remove(hostView);
1108 updateRunning();
1109 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001110 }
1111
Joe Onorato9c1289c2009-08-17 11:03:03 -04001112 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001113 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001114 launcherInfo.hostView = null;
1115 }
1116
Adam Cohended9f8d2010-11-03 13:25:16 -07001117 void showOutOfSpaceMessage() {
1118 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1119 }
1120
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001121 public LauncherAppWidgetHost getAppWidgetHost() {
1122 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 }
Romain Guycbb89e42009-06-08 15:52:54 -07001124
Winson Chunga9abd0e2010-10-27 17:18:37 -07001125 public LauncherModel getModel() {
1126 return mModel;
1127 }
1128
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001129 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001130 getWindow().closeAllPanels();
1131
Winson Chung87acb482011-08-23 17:28:05 -07001132 /**
1133 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001134 try {
1135 dismissDialog(DIALOG_CREATE_SHORTCUT);
1136 // Unlock the workspace if the dialog was showing
1137 } catch (Exception e) {
1138 // An exception is thrown if the dialog is not visible, which is fine
1139 }
1140
1141 try {
1142 dismissDialog(DIALOG_RENAME_FOLDER);
1143 // Unlock the workspace if the dialog was showing
1144 } catch (Exception e) {
1145 // An exception is thrown if the dialog is not visible, which is fine
1146 }
Winson Chung87acb482011-08-23 17:28:05 -07001147 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001148
1149 // Whatever we were doing is hereby canceled.
1150 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001151 }
1152
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001153 @Override
1154 protected void onNewIntent(Intent intent) {
1155 super.onNewIntent(intent);
1156
1157 // Close the menu
1158 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001159 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001160 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001161
Joe Onorato14f122b2009-11-19 14:06:36 -08001162 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1163 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001164
Adam Cohenac56cff2011-09-28 20:45:37 -07001165 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001166 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001167 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001168 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1169 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001170 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001171 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001172
1173 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001174 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001175 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001176
Joe Onorato3a8820b2009-11-10 15:06:42 -08001177 final View v = getWindow().peekDecorView();
1178 if (v != null && v.getWindowToken() != null) {
1179 InputMethodManager imm = (InputMethodManager)getSystemService(
1180 INPUT_METHOD_SERVICE);
1181 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001183
Michael Jurka35aa14d2011-07-07 17:01:08 -07001184 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001185 if (mAppsCustomizeTabHost != null) {
1186 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001187 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 }
1189 }
1190
1191 @Override
1192 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1193 // Do not call super here
1194 mSavedInstanceState = savedInstanceState;
1195 }
1196
1197 @Override
1198 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001199 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001200 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201
Michael Jurka883f55b2010-10-21 15:47:14 -07001202 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001203 // We close any open folder since it will not be re-opened, and we need to make sure
1204 // this state is reflected.
1205 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206
Winson Chung3d503fb2011-07-13 17:25:49 -07001207 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1208 mWaitingForResult) {
1209 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1210 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1211 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1212 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001213 }
1214
1215 if (mFolderInfo != null && mWaitingForResult) {
1216 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1217 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1218 }
Michael Jurka946ad472010-07-09 18:05:18 -07001219
Winson Chung785d2eb2011-04-14 16:08:02 -07001220 // Save the current AppsCustomize tab
1221 if (mAppsCustomizeTabHost != null) {
1222 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1223 if (currentTabTag != null) {
1224 outState.putString("apps_customize_currentTab", currentTabTag);
1225 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001226 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1227 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001228 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001229 }
1230
1231 @Override
1232 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001234
Winson Chunge7a03942011-08-05 15:05:12 -07001235 // Remove all pending runnables
1236 mHandler.removeMessages(ADVANCE_MSG);
1237 mHandler.removeMessages(0);
1238
Winson Chungcd2b0142011-06-08 16:02:26 -07001239 // Stop callbacks from LauncherModel
1240 LauncherApplication app = ((LauncherApplication) getApplication());
1241 mModel.stopLoader();
1242 app.setLauncher(null);
1243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001245 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001246 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001247 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001249 mAppWidgetHost = null;
1250
1251 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252
1253 TextKeyListener.getInstance().release();
1254
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001255
Winson Chung3d503fb2011-07-13 17:25:49 -07001256 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001257
1258 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001259 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001260
1261 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1262 mWorkspace.removeAllViews();
1263 mWorkspace = null;
1264 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001265
1266 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 }
1268
Adam Cohena9cf38f2011-05-02 15:36:58 -07001269 public DragController getDragController() {
1270 return mDragController;
1271 }
1272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 @Override
1274 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001275 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 super.startActivityForResult(intent, requestCode);
1277 }
1278
Winson Chung70d72102011-08-12 11:24:35 -07001279 /**
1280 * Indicates that we want global search for this activity by setting the globalSearch
1281 * argument for {@link #startSearch} to true.
1282 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001284 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001286
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001287 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001288
Karl Rosaen138a0412009-04-23 19:00:21 -07001289 if (initialQuery == null) {
1290 // Use any text typed in the launcher as the initial query
1291 initialQuery = getTypedText();
1292 clearTypedText();
1293 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001294 if (appSearchData == null) {
1295 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001296 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 }
Romain Guycbb89e42009-06-08 15:52:54 -07001298
Karl Rosaen138a0412009-04-23 19:00:21 -07001299 final SearchManager searchManager =
1300 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001301 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001302 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 }
1304
Winson Chung70d72102011-08-12 11:24:35 -07001305 @Override
1306 public boolean onCreateOptionsMenu(Menu menu) {
1307 if (isWorkspaceLocked()) {
1308 return false;
1309 }
1310
1311 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001312
1313 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1314 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1315 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001316 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1317 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1318 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001319 String helpUrl = getString(R.string.help_url);
1320 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001321 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1322 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1323
Winson Chung70d72102011-08-12 11:24:35 -07001324 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1325 .setIcon(android.R.drawable.ic_menu_gallery)
1326 .setAlphabeticShortcut('W');
1327 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1328 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001329 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001330 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001331 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1332 .setIcon(android.R.drawable.ic_menu_preferences)
1333 .setIntent(settings)
1334 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001335 if (!helpUrl.isEmpty()) {
1336 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1337 .setIcon(android.R.drawable.ic_menu_help)
1338 .setIntent(help)
1339 .setAlphabeticShortcut('H');
1340 }
Winson Chung70d72102011-08-12 11:24:35 -07001341 return true;
1342 }
1343
1344 @Override
1345 public boolean onPrepareOptionsMenu(Menu menu) {
1346 super.onPrepareOptionsMenu(menu);
1347
1348 if (mAppsCustomizeTabHost.isTransitioning()) {
1349 return false;
1350 }
1351 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1352 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1353
1354 return true;
1355 }
1356
1357 @Override
1358 public boolean onOptionsItemSelected(MenuItem item) {
1359 switch (item.getItemId()) {
1360 case MENU_WALLPAPER_SETTINGS:
1361 startWallpaper();
1362 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001363 }
1364
1365 return super.onOptionsItemSelected(item);
1366 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001368 @Override
1369 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001370 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001371 // Use a custom animation for launching search
1372 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001373 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001374 }
1375
Joe Onorato9c1289c2009-08-17 11:03:03 -04001376 public boolean isWorkspaceLocked() {
1377 return mWorkspaceLoading || mWaitingForResult;
1378 }
1379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001381 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001382 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 }
1384
Michael Jurka0280c3b2010-09-17 15:00:07 -07001385 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001386 mPendingAddInfo.container = ItemInfo.NO_ID;
1387 mPendingAddInfo.screen = -1;
1388 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1389 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1390 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001391 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001392
Michael Jurkaaf442092010-06-10 17:01:57 -07001393 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001394 // TODO: catch bad widget exception when sent
1395 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001396 // TODO: Is this log message meaningful?
1397 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001398 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001399 }
1400
Winson Chung55cef262010-10-28 14:14:18 -07001401 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001402 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403
Bjorn Bringert7984c942009-12-09 15:38:25 +00001404 if (appWidget.configure != null) {
1405 // Launch over to configure widget, if needed
1406 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1407 intent.setComponent(appWidget.configure);
1408 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001409 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001410 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1411 intent.putExtra(
1412 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1413 info.mimeType);
1414
1415 final String mimeType = info.mimeType;
1416 final ClipData clipData = (ClipData) info.configurationData;
1417 final ClipDescription clipDesc = clipData.getDescription();
1418 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1419 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001420 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001421 final CharSequence stringData = item.getText();
1422 final Uri uriData = item.getUri();
1423 final Intent intentData = item.getIntent();
1424 final String key =
1425 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1426 if (uriData != null) {
1427 intent.putExtra(key, uriData);
1428 } else if (intentData != null) {
1429 intent.putExtra(key, intentData);
1430 } else if (stringData != null) {
1431 intent.putExtra(key, stringData);
1432 }
1433 break;
1434 }
1435 }
1436 }
Winson Chung55cef262010-10-28 14:14:18 -07001437 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001438
Romain Guy8e633c52010-03-04 12:51:36 -08001439 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001440 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001441 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001442 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001443
1444 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001445 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 }
1447 }
Romain Guycbb89e42009-06-08 15:52:54 -07001448
Adam Cohenfbba09b2011-07-18 21:43:05 -07001449 /**
1450 * Process a shortcut drop.
1451 *
1452 * @param componentName The name of the component
1453 * @param screen The screen where it should be added
1454 * @param cell The cell it should be added to, optional
1455 * @param position The location on the screen where it was dropped, optional
1456 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001457 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1458 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001459 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001460 mPendingAddInfo.container = container;
1461 mPendingAddInfo.screen = screen;
1462 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001463
1464 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001465 mPendingAddInfo.cellX = cell[0];
1466 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001467 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001468
1469 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1470 createShortcutIntent.setComponent(componentName);
1471 processShortcut(createShortcutIntent);
1472 }
1473
Adam Cohenfbba09b2011-07-18 21:43:05 -07001474 /**
1475 * Process a widget drop.
1476 *
1477 * @param info The PendingAppWidgetInfo of the widget being added.
1478 * @param screen The screen where it should be added
1479 * @param cell The cell it should be added to, optional
1480 * @param position The location on the screen where it was dropped, optional
1481 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001482 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1483 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001484 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001485 mPendingAddInfo.container = info.container = container;
1486 mPendingAddInfo.screen = info.screen = screen;
1487 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001488 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001489 mPendingAddInfo.cellX = cell[0];
1490 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001491 }
1492
1493 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1494 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1495 addAppWidgetImpl(appWidgetId, info);
1496 }
1497
Joe Onoratodeb98af2010-02-19 14:59:39 -08001498 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001499 // Handle case where user selected "Applications"
1500 String applicationName = getResources().getString(R.string.group_applications);
1501 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001502
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001503 if (applicationName != null && applicationName.equals(shortcutName)) {
1504 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1505 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001506
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001507 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1508 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001509 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001510 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001511 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001512 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001513 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 }
1515
Winson Chung24ab2f12010-09-16 14:10:47 -07001516 void processWallpaper(Intent intent) {
1517 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1518 }
1519
Winson Chung3d503fb2011-07-13 17:25:49 -07001520 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1521 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001522 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001523 folderInfo.title = getText(R.string.folder_name);
1524
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001526 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1527 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001528 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529
1530 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001531 FolderIcon newFolder =
1532 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1533 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1534 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001535 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 }
Romain Guycbb89e42009-06-08 15:52:54 -07001537
Joe Onorato9c1289c2009-08-17 11:03:03 -04001538 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001539 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001540 }
1541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 private void showNotifications() {
1543 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1544 if (statusBar != null) {
1545 statusBar.expand();
1546 }
1547 }
1548
1549 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001550 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001551 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001552 Intent chooser = Intent.createChooser(pickWallpaper,
1553 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001554 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1555 // Removed in Eclair MR1
1556// WallpaperManager wm = (WallpaperManager)
1557// getSystemService(Context.WALLPAPER_SERVICE);
1558// WallpaperInfo wi = wm.getWallpaperInfo();
1559// if (wi != null && wi.getSettingsActivity() != null) {
1560// LabeledIntent li = new LabeledIntent(getPackageName(),
1561// R.string.configure_wallpaper, 0);
1562// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1563// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1564// }
Mike Clerona0618e42009-10-22 13:55:21 -07001565 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 }
1567
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001568 /**
1569 * Registers various content observers. The current implementation registers
1570 * only a favorites observer to keep track of the favorites applications.
1571 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001572 private void registerContentObservers() {
1573 ContentResolver resolver = getContentResolver();
1574 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1575 true, mWidgetObserver);
1576 }
1577
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 @Override
1579 public boolean dispatchKeyEvent(KeyEvent event) {
1580 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1581 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001583 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001584 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001585 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001586 dumpState();
1587 return true;
1588 }
1589 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001590 }
1591 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1592 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001593 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 return true;
1595 }
1596 }
1597
1598 return super.dispatchKeyEvent(event);
1599 }
1600
Joe Onorato88ec0992009-11-19 13:16:06 -08001601 @Override
1602 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001603 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001604 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001605 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001606 Folder openFolder = mWorkspace.getOpenFolder();
1607 if (openFolder.isEditingName()) {
1608 openFolder.dismissEditingName();
1609 } else {
1610 closeFolder();
1611 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001612 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001613 mWorkspace.exitWidgetResizeMode();
1614
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001615 // Back button is a no-op here, but give at least some feedback for the button press
1616 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001617 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001618 }
1619
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001620 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001621 * Re-listen when widgets are reset.
1622 */
1623 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001624 if (mAppWidgetHost != null) {
1625 mAppWidgetHost.startListening();
1626 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001627 }
1628
1629 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001630 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1631 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001633 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001634 if (mModel != null) {
1635 mModel.unbindWorkspaceItems();
1636 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001638
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001639 /**
1640 * Launches the intent referred by the clicked shortcut.
1641 *
1642 * @param v The view representing the clicked shortcut.
1643 */
1644 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001645 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1646 // view has detached (it's possible for this to happen if the view is removed mid touch).
1647 if (v.getWindowToken() == null) {
1648 return;
1649 }
1650
1651 if (mWorkspace.isSwitchingState()) {
1652 return;
1653 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001654
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001656 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001658 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001659 int[] pos = new int[2];
1660 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001661 intent.setSourceBounds(new Rect(pos[0], pos[1],
1662 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001663 boolean success = startActivitySafely(intent, tag);
1664
1665 if (success && v instanceof BubbleTextView) {
1666 mWaitingForResume = (BubbleTextView) v;
1667 mWaitingForResume.setStayPressed(true);
1668 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001670 if (v instanceof FolderIcon) {
1671 FolderIcon fi = (FolderIcon) v;
1672 handleFolderClick(fi);
1673 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001674 } else if (v == mAllAppsButton) {
1675 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001676 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001677 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001678 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001679 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 }
1681 }
1682
Michael Jurka0e260592010-06-30 17:07:39 -07001683 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001684 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001685 // clicking anywhere on the workspace causes the customization drawer to slide down
1686 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001687 return false;
1688 }
1689
Michael Jurkaaf442092010-06-10 17:01:57 -07001690 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001691 * Event handler for the search button
1692 *
1693 * @param v The view that was clicked.
1694 */
1695 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001696 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001697 }
1698
1699 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001700 * Event handler for the voice button
1701 *
1702 * @param v The view that was clicked.
1703 */
1704 public void onClickVoiceButton(View v) {
1705 startVoiceSearch();
1706 }
1707
1708 private void startVoiceSearch() {
1709 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001710 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001711 startActivity(intent);
1712 }
1713
1714 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001715 * Event handler for the "grid" button that appears on the home screen, which
1716 * enters all apps mode.
1717 *
1718 * @param v The view that was clicked.
1719 */
1720 public void onClickAllAppsButton(View v) {
1721 showAllApps(true);
1722 }
1723
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001724 public void onClickAppMarketButton(View v) {
1725 if (mAppMarketIntent != null) {
1726 startActivitySafely(mAppMarketIntent, "app market");
1727 }
1728 }
1729
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001730 void startApplicationDetailsActivity(ComponentName componentName) {
1731 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001732 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1733 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001734 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001735 startActivity(intent);
1736 }
1737
Patrick Dubroy5539af72010-09-07 15:22:01 -07001738 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1739 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1740 // System applications cannot be installed. For now, show a toast explaining that.
1741 // We may give them the option of disabling apps this way.
1742 int messageId = R.string.uninstall_system_app_text;
1743 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1744 } else {
1745 String packageName = appInfo.componentName.getPackageName();
1746 String className = appInfo.componentName.getClassName();
1747 Intent intent = new Intent(
1748 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001749 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1750 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001751 startActivity(intent);
1752 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001753 }
1754
Michael Jurkaddd62e92011-02-16 17:49:14 -08001755 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001756 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1757 try {
1758 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001759 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 } catch (ActivityNotFoundException e) {
1761 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001762 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 } catch (SecurityException e) {
1764 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001765 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001767 "or use the exported attribute for this activity. "
1768 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001770 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001771 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001772
Romain Guy8e633c52010-03-04 12:51:36 -08001773 void startActivityForResultSafely(Intent intent, int requestCode) {
1774 try {
1775 startActivityForResult(intent, requestCode);
1776 } catch (ActivityNotFoundException e) {
1777 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1778 } catch (SecurityException e) {
1779 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1780 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1781 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1782 "or use the exported attribute for this activity.", e);
1783 }
1784 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785
Adam Cohena9cf38f2011-05-02 15:36:58 -07001786 private void handleFolderClick(FolderIcon folderIcon) {
1787 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001788 Folder openFolder = mWorkspace.getFolderForTag(info);
1789
1790 // If the folder info reports that the associated folder is open, then verify that
1791 // it is actually opened. There have been a few instances where this gets out of sync.
1792 if (info.opened && openFolder == null) {
1793 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1794 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1795 info.opened = false;
1796 }
1797
Adam Cohena9cf38f2011-05-02 15:36:58 -07001798 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 // Close any open folder
1800 closeFolder();
1801 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001802 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 } else {
1804 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 int folderScreen;
1806 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001807 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001808 // .. and close it
1809 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001810 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 // Close any folder open on the current screen
1812 closeFolder();
1813 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001814 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 }
1816 }
1817 }
1818 }
1819
Adam Cohen2801caf2011-05-13 20:57:39 -07001820 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001821 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001822 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1823 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1824 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1825
Adam Cohenc51934b2011-07-26 21:07:43 -07001826 FolderInfo info = (FolderInfo) fi.getTag();
1827 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1828 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001829 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1830 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001831 }
1832
Adam Cohen2801caf2011-05-13 20:57:39 -07001833 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1834 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1835 oa.start();
1836 }
1837
1838 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001839 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001840 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1841 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1842 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1843
Adam Cohenc51934b2011-07-26 21:07:43 -07001844 FolderInfo info = (FolderInfo) fi.getTag();
1845 CellLayout cl = null;
1846 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1847 cl = (CellLayout) fi.getParent().getParent();
1848 }
1849
1850 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001851 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1852 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001853 oa.addListener(new AnimatorListenerAdapter() {
1854 @Override
1855 public void onAnimationEnd(Animator animation) {
1856 if (layout != null) {
1857 layout.clearFolderLeaveBehind();
1858 }
1859 }
1860 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001861 oa.start();
1862 }
1863
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001865 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001866 * is animated relative to the specified View. If the View is null, no animation
1867 * is played.
1868 *
1869 * @param folderInfo The FolderInfo describing the folder to open.
1870 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001871 public void openFolder(FolderIcon folderIcon) {
1872 Folder folder = folderIcon.mFolder;
1873 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874
Adam Cohen2801caf2011-05-13 20:57:39 -07001875 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001876 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001877
Adam Cohen4554ee12011-08-03 16:13:21 -07001878 // Just verify that the folder hasn't already been added to the DragLayer.
1879 // There was a one-off crash where the folder had a parent already.
1880 if (folder.getParent() == null) {
1881 mDragLayer.addView(folder);
1882 mDragController.addDropTarget((DropTarget) folder);
1883 } else {
1884 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1885 folder.getParent() + ").");
1886 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001887 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001888 }
1889
1890 public void closeFolder() {
1891 Folder folder = mWorkspace.getOpenFolder();
1892 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07001893 if (folder.isEditingName()) {
1894 folder.dismissEditingName();
1895 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001896 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07001897
1898 // Dismiss the folder cling
1899 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07001900 }
1901 }
1902
1903 void closeFolder(Folder folder) {
1904 folder.getInfo().opened = false;
1905
1906 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1907 if (parent != null) {
1908 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1909 shrinkAndFadeInFolderIcon(fi);
1910 }
1911 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001912 }
1913
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001915 if (mState != State.WORKSPACE) {
1916 return false;
1917 }
1918
Joe Onorato9c1289c2009-08-17 11:03:03 -04001919 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 return false;
1921 }
1922
1923 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001924 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001925 }
1926
Michael Jurka0280c3b2010-09-17 15:00:07 -07001927 resetAddInfo();
1928 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001930 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 return true;
1932 }
1933
Winson Chung3d503fb2011-07-13 17:25:49 -07001934 // The hotseat touch handling does not go through Workspace, and we always allow long press
1935 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001936 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001937 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1938 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001939 if (itemUnderLongClick == null) {
1940 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001941 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1942 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001943 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001945 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001947 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 }
1949 }
1950 }
1951 return true;
1952 }
1953
Winson Chung3d503fb2011-07-13 17:25:49 -07001954 boolean isHotseatLayout(View layout) {
1955 return mHotseat != null && layout != null &&
1956 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1957 }
1958 Hotseat getHotseat() {
1959 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001960 }
1961
Winson Chung3d503fb2011-07-13 17:25:49 -07001962 /**
1963 * Returns the CellLayout of the specified container at the specified screen.
1964 */
1965 CellLayout getCellLayout(long container, int screen) {
1966 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1967 if (mHotseat != null) {
1968 return mHotseat.getLayout();
1969 } else {
1970 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001971 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001972 } else {
1973 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001974 }
1975 }
1976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 Workspace getWorkspace() {
1978 return mWorkspace;
1979 }
1980
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 @Override
1982 protected Dialog onCreateDialog(int id) {
1983 switch (id) {
1984 case DIALOG_CREATE_SHORTCUT:
1985 return new CreateShortcut().createDialog();
1986 case DIALOG_RENAME_FOLDER:
1987 return new RenameFolder().createDialog();
1988 }
1989
1990 return super.onCreateDialog(id);
1991 }
1992
1993 @Override
1994 protected void onPrepareDialog(int id, Dialog dialog) {
1995 switch (id) {
1996 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 break;
1998 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07001999 if (mFolderInfo != null) {
2000 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2001 final CharSequence text = mFolderInfo.title;
2002 input.setText(text);
2003 input.setSelection(0, text.length());
2004 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 break;
2006 }
2007 }
2008
2009 void showRenameDialog(FolderInfo info) {
2010 mFolderInfo = info;
2011 mWaitingForResult = true;
2012 showDialog(DIALOG_RENAME_FOLDER);
2013 }
2014
Adam Cohenfbba09b2011-07-18 21:43:05 -07002015 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002016 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002017 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2018 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 mWaitingForResult = true;
2020 showDialog(DIALOG_CREATE_SHORTCUT);
2021 }
2022
2023 private class RenameFolder {
2024 private EditText mInput;
2025
2026 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2028 mInput = (EditText) layout.findViewById(R.id.folder_name);
2029
2030 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2031 builder.setIcon(0);
2032 builder.setTitle(getString(R.string.rename_folder_title));
2033 builder.setCancelable(true);
2034 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2035 public void onCancel(DialogInterface dialog) {
2036 cleanup();
2037 }
2038 });
2039 builder.setNegativeButton(getString(R.string.cancel_action),
2040 new Dialog.OnClickListener() {
2041 public void onClick(DialogInterface dialog, int which) {
2042 cleanup();
2043 }
2044 }
2045 );
2046 builder.setPositiveButton(getString(R.string.rename_action),
2047 new Dialog.OnClickListener() {
2048 public void onClick(DialogInterface dialog, int which) {
2049 changeFolderName();
2050 }
2051 }
2052 );
2053 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002054
2055 final AlertDialog dialog = builder.create();
2056 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2057 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002058 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002059 mInput.requestFocus();
2060 InputMethodManager inputManager = (InputMethodManager)
2061 getSystemService(Context.INPUT_METHOD_SERVICE);
2062 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002063 }
2064 });
2065
2066 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 }
2068
2069 private void changeFolderName() {
2070 final String name = mInput.getText().toString();
2071 if (!TextUtils.isEmpty(name)) {
2072 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002073 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 mFolderInfo.title = name;
2075 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2076
Joe Onorato9c1289c2009-08-17 11:03:03 -04002077 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002078 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002079 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 } else {
2081 final FolderIcon folderIcon = (FolderIcon)
2082 mWorkspace.getViewForTag(mFolderInfo);
2083 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002084 // TODO: At some point we'll probably want some version of setting
2085 // the text for a folder icon.
2086 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002087 getWorkspace().requestLayout();
2088 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002089 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002090 mWorkspaceLoading = true;
2091 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 }
2093 }
2094 }
2095 cleanup();
2096 }
2097
2098 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 dismissDialog(DIALOG_RENAME_FOLDER);
2100 mWaitingForResult = false;
2101 mFolderInfo = null;
2102 }
2103 }
2104
Daniel Sandler843e8602010-06-07 14:59:01 -04002105 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2106 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002107 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002108 }
2109
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002110 // AllAppsView.Watcher
2111 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002112 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002113 mWorkspace.setVisibility(View.GONE);
2114 }
2115 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002116
2117 /**
2118 * Helper method for the cameraZoomIn/cameraZoomOut animations
2119 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002120 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002121 * @param scaleFactor The scale factor used for the zoom
2122 */
2123 private void setPivotsForZoom(View view, State state, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002124 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002125 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002126 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002127
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002128 void updateWallpaperVisibility(boolean visible) {
2129 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2130 int curflags = getWindow().getAttributes().flags
2131 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2132 if (wpflags != curflags) {
2133 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2134 }
2135 }
2136
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002137 /**
2138 * Zoom the camera out from the workspace to reveal 'toView'.
2139 * Assumes that the view to show is anchored at either the very top or very bottom
2140 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002141 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002142 */
Winson Chungc07918d2011-07-01 15:35:26 -07002143 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002144 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002145 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002146
Winson Chungf0ea4d32011-06-06 14:27:16 -07002147 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2148 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2149 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2150 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002151 final int startDelay =
2152 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002153
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002154 setPivotsForZoom(toView, toState, scale);
2155
Michael Jurkad74c9842011-07-10 12:44:21 -07002156 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002157 mWorkspace.changeState(Workspace.State.SMALL, animated);
Adam Cohencff6af82011-09-13 14:51:53 -07002158 //hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002159
2160 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002161 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002162 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002163 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2164 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002165 ((View) toView.getParent()).invalidate();
2166 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002167 toView.setFastScaleX(a * scale + b * 1f);
2168 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002169 }
2170 });
Adam Cohen61033d32010-11-15 18:29:44 -08002171
Winson Chungf0ea4d32011-06-06 14:27:16 -07002172 toView.setVisibility(View.VISIBLE);
2173 toView.setFastAlpha(0f);
2174 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2175 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2176 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2177 public void onAnimationUpdate(float a, float b) {
2178 // don't need to invalidate because we do so above
2179 toView.setFastAlpha(a * 0f + b * 1f);
2180 }
2181 });
Adam Cohencff6af82011-09-13 14:51:53 -07002182 alphaAnim.setStartDelay(startDelay);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002183 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002184
Michael Jurkaabded662011-03-04 12:06:57 -08002185 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002186 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, scaleAnim,
2187 false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002188 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002189 scaleAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002190 boolean animationCancelled = false;
2191
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002192 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002193 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002194 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002195 // Prepare the position
2196 toView.setTranslationX(0.0f);
2197 toView.setTranslationY(0.0f);
2198 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002199 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002200 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002201 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002202 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002203 // If we don't set the final scale values here, if this animation is cancelled
2204 // it will have the wrong scale value and subsequent cameraPan animations will
2205 // not fix that
2206 toView.setScaleX(1.0f);
2207 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002208 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002209 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance,
2210 scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002211 }
Winson Chung32174c82011-07-19 15:47:55 -07002212
2213 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2214 // Hide the workspace scrollbar
2215 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002216 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002217 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002218 if (!animationCancelled) {
2219 updateWallpaperVisibility(false);
2220 }
2221 }
2222
Adam Cohencff6af82011-09-13 14:51:53 -07002223 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002224 public void onAnimationCancel(Animator animation) {
2225 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002226 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002227 });
2228
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002229 // toView should appear right at the end of the workspace shrink animation
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002230
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002231 if (mStateAnimation != null) mStateAnimation.cancel();
2232 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002233 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002234 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002235 } else {
2236 toView.setTranslationX(0.0f);
2237 toView.setTranslationY(0.0f);
2238 toView.setScaleX(1.0f);
2239 toView.setScaleY(1.0f);
2240 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002241 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002242 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002243 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, null, false);
2244 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance, null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002245
Winson Chungc07918d2011-07-01 15:35:26 -07002246 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2247 // Hide the workspace scrollbar
2248 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002249 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002250 }
Michael Jurkaabded662011-03-04 12:06:57 -08002251 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002252 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002253 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002254 }
2255
2256 /**
2257 * Zoom the camera back into the workspace, hiding 'fromView'.
2258 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002259 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002260 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002261 */
Winson Chungc07918d2011-07-01 15:35:26 -07002262 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002263 Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002264 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002265
Winson Chungf0ea4d32011-06-06 14:27:16 -07002266 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2267 final float scaleFactor = (float)
2268 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2269 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002270
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002271 setPivotsForZoom(fromView, fromState, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002272 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002273 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002274 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002275 if (mStateAnimation != null) mStateAnimation.cancel();
2276 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002277
Michael Jurka742574b2011-02-02 23:51:01 -08002278 final float oldScaleX = fromView.getScaleX();
2279 final float oldScaleY = fromView.getScaleY();
2280
2281 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2282 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002283 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2284 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002285 ((View)fromView.getParent()).fastInvalidate();
2286 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2287 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2288 }
2289 });
Michael Jurkaabded662011-03-04 12:06:57 -08002290 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002291 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Adam Cohencff6af82011-09-13 14:51:53 -07002292 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002293 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2294 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002295 // don't need to invalidate because we do so above
2296 fromView.setFastAlpha(a * 1f + b * 0f);
2297 }
2298 });
Michael Jurkaabded662011-03-04 12:06:57 -08002299 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002300 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, alphaAnim,
2301 true);
Michael Jurka2763be32011-02-24 11:19:57 -08002302 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002303 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002304 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002305 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002306 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002307 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002308 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002309 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance,
2310 alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002311 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002312 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002313 }
2314 });
2315
Winson Chungf0ea4d32011-06-06 14:27:16 -07002316 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002317 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002318 } else {
2319 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002320 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002321 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, null, true);
2322 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance, null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002323 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002324 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002325 }
2326
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002327 void showWorkspace(boolean animated) {
Adam Cohencff6af82011-09-13 14:51:53 -07002328 Resources res = getResources();
2329 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2330
2331 mWorkspace.changeState(Workspace.State.NORMAL, animated, stagger);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002332 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002333 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002334 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002335
Adam Cohen7777d962011-08-18 18:58:38 -07002336 mWorkspace.showDockDivider(!animated);
2337 mWorkspace.flashScrollingIndicator();
2338
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002339 // Change the state *after* we've called all the transition code
2340 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002341
Winson Chung5fb63472011-02-02 17:03:37 -08002342 // Resume the auto-advance of widgets
2343 mUserPresent = true;
2344 updateRunning();
2345
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002346 // send an accessibility event to announce the context change
2347 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002348 }
2349
Adam Cohen7777d962011-08-18 18:58:38 -07002350 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002351 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002352 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Winson Chungb26f3d62011-06-02 10:49:29 -07002353 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002354 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002355 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002356 }
Adam Cohen7777d962011-08-18 18:58:38 -07002357
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002358 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chung09bfc452011-09-09 11:30:20 -07002359 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2360
Winson Chunge7a03942011-08-05 15:05:12 -07002361 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002362 @Override
2363 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002364 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002365 // Before we show workspace, hide all apps again because
2366 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2367 // clean up our state transition functions
2368 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002369 mSearchDropTargetBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002370 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002371 } else {
2372 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002373 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002374 }
2375 }, (extendedDelay ?
2376 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2377 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2378 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002379 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002380 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002381 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2382 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002383 }
2384 // Otherwise, we are not in spring loaded mode, so don't do anything.
2385 }
2386
Adam Cohenfc53cd22011-07-20 15:45:11 -07002387 public boolean isAllAppsCustomizeOpen() {
2388 return mState == State.APPS_CUSTOMIZE;
2389 }
2390
Winson Chungf0ea4d32011-06-06 14:27:16 -07002391 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002392 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002393 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002394 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002395 if (!LauncherApplication.isScreenLarge()) {
2396 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002397 int duration = mSearchDropTargetBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002398 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002399 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002400 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002401 }
2402 }
2403 }
2404
2405 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002406 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002407 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002408 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002409 if (!LauncherApplication.isScreenLarge()) {
2410 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002411 int duration = mSearchDropTargetBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002412 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002413 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002414 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002415 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002416 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002417 }
2418
Winson Chung785d2eb2011-04-14 16:08:02 -07002419 void showAllApps(boolean animated) {
2420 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002421
Winson Chungf0ea4d32011-06-06 14:27:16 -07002422 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2423 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002424
Winson Chung3d503fb2011-07-13 17:25:49 -07002425 // Hide the search bar and hotseat
Winson Chungc51db6a2011-10-05 11:44:49 -07002426 mSearchDropTargetBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002427
Winson Chungf0ea4d32011-06-06 14:27:16 -07002428 // Change the state *after* we've called all the transition code
2429 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002430
2431 // Pause the auto-advance of widgets until we are out of AllApps
2432 mUserPresent = false;
2433 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002434 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002435
2436 // Send an accessibility event to announce the context change
2437 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2438 }
2439
Joe Onoratob2061212009-11-24 16:13:54 -05002440 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002441 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002442 * - Home from workspace
2443 * - from center screen
2444 * - from other screens
2445 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002446 * - from center screen
2447 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002448 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002449 * - from center screen
2450 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002451 * - Launch app from workspace and quit
2452 * - with back
2453 * - with home
2454 * - Launch app from all apps and quit
2455 * - with back
2456 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002457 * - Go to a screen that's not the default, then all
2458 * apps, and launch and app, and go back
2459 * - with back
2460 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002461 * - On workspace, long press power and go back
2462 * - with back
2463 * - with home
2464 * - On all apps, long press power and go back
2465 * - with back
2466 * - with home
2467 * - On workspace, power off
2468 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002469 * - Launch an app and turn off the screen while in that app
2470 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002471 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002472 * - From all apps
2473 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002474 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2475 * - From all apps
2476 * - From the center workspace
2477 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002478 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002479 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002480 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2481 mWorkspace.setVisibility(View.VISIBLE);
2482 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002483
Winson Chung3d503fb2011-07-13 17:25:49 -07002484 // Show the search bar and hotseat
Winson Chungc51db6a2011-10-05 11:44:49 -07002485 mSearchDropTargetBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002486
Winson Chungf0ea4d32011-06-06 14:27:16 -07002487 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002488 if (mAllAppsButton != null) {
2489 mAllAppsButton.requestFocus();
2490 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002491 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002492 }
2493
Joe Onorato7c312c12009-08-13 21:36:53 -07002494 void lockAllApps() {
2495 // TODO
2496 }
2497
2498 void unlockAllApps() {
2499 // TODO
2500 }
2501
Patrick Dubroy5f445422011-02-18 14:35:21 -08002502 /**
2503 * Add an item from all apps or customize onto the given workspace screen.
2504 * If layout is null, add to the current screen.
2505 */
2506 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002507 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2508 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002509 } else {
2510 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002511 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002512 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002513
Winson Chungdff8ebb2011-09-08 17:25:31 -07002514 /** Maps the current orientation to an index for referencing orientation correct global icons */
2515 private int getCurrentOrientationIndexForGlobalIcons() {
2516 // default - 0, landscape - 1
2517 switch (getResources().getConfiguration().orientation) {
2518 case Configuration.ORIENTATION_LANDSCAPE:
2519 return 1;
2520 default:
2521 return 0;
2522 }
2523 }
2524
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002525 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002526 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002527 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002528 // Look for the toolbar icon specified in the activity meta-data
2529 Bundle metaData = packageManager.getActivityInfo(
2530 activityName, PackageManager.GET_META_DATA).metaData;
2531 if (metaData != null) {
2532 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2533 if (iconResId != 0) {
2534 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002535 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002536 }
2537 }
2538 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002539 // This can happen if the activity defines an invalid drawable
2540 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2541 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002542 } catch (Resources.NotFoundException nfe) {
2543 // This can happen if the activity defines an invalid drawable
2544 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2545 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002546 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002547 return null;
2548 }
2549
2550 // if successful in getting icon, return it; otherwise, set button to use default drawable
2551 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2552 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2553 TextView button = (TextView) findViewById(buttonId);
2554 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002555 Resources r = getResources();
2556 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2557 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002558
2559 // If we were unable to find the icon via the meta-data, use a generic one
2560 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002561 toolbarIcon = r.getDrawable(fallbackDrawableId);
2562 toolbarIcon.setBounds(0, 0, w, h);
2563 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002564 return null;
2565 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002566 toolbarIcon.setBounds(0, 0, w, h);
2567 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002568 return toolbarIcon.getConstantState();
2569 }
2570 }
2571
2572 // if successful in getting icon, return it; otherwise, set button to use default drawable
2573 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2574 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2575 ImageView button = (ImageView) findViewById(buttonId);
2576 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2577
Michael Jurka19e0fc52011-07-22 18:00:21 -07002578 if (button != null) {
2579 // If we were unable to find the icon via the meta-data, use a
2580 // generic one
2581 if (toolbarIcon == null) {
2582 button.setImageResource(fallbackDrawableId);
2583 } else {
2584 button.setImageDrawable(toolbarIcon);
2585 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002586 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002587
2588 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2589
Michael Jurka0423dcf2010-10-05 14:56:18 -07002590 }
2591
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002592 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2593 TextView button = (TextView) findViewById(buttonId);
2594 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2595 }
2596
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002597 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002598 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002599 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002600 }
2601
Winson Chungc51db6a2011-10-05 11:44:49 -07002602 private boolean updateGlobalSearchIcon() {
2603 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002604 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2605 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002606 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002607 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002608
Winson Chung1cad91e2011-05-25 17:41:01 -07002609 final SearchManager searchManager =
2610 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2611 ComponentName activityName = searchManager.getGlobalSearchActivity();
2612 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002613 int coi = getCurrentOrientationIndexForGlobalIcons();
2614 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002615 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002616 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002617 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2618 searchButton.setVisibility(View.VISIBLE);
2619 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002620 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002621 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002622 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002623 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2624 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2625 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002626 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002627 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002628 }
2629 }
2630
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002631 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002632 updateButtonWithDrawable(R.id.search_button, d);
2633 }
2634
Winson Chungc51db6a2011-10-05 11:44:49 -07002635 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002636 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002637 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002638 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002639
Winson Chungc51db6a2011-10-05 11:44:49 -07002640 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002641 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2642 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002643 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002644 int coi = getCurrentOrientationIndexForGlobalIcons();
2645 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002646 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002647 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002648 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002649 voiceButton.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002650 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002651 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002652 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002653 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002654 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002655 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002656 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002657 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002658
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002659 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002660 updateButtonWithDrawable(R.id.voice_button, d);
2661 }
2662
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002663 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002664 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002665 */
2666 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002667 final View marketButton = findViewById(R.id.market_button);
2668 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2669 // Find the app market activity by resolving an intent.
2670 // (If multiple app markets are installed, it will return the ResolverActivity.)
2671 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002672 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002673 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002674 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002675 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002676 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002677 marketButton.setVisibility(View.VISIBLE);
2678 } else {
2679 // We should hide and disable the view so that we don't try and restore the visibility
2680 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2681 marketButton.setVisibility(View.GONE);
2682 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002683 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002684 }
2685
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002686 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002687 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002688 }
2689
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002690 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002691 * Displays the shortcut creation dialog and launches, if necessary, the
2692 * appropriate activity.
2693 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002694 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002695 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2696 DialogInterface.OnShowListener {
2697
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002698 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002699
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002700 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002701 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002702
Winson Chung55b65502011-05-26 12:03:43 -07002703 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2704 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002705 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002706
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002707 AlertDialog dialog = builder.create();
2708 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002709 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002710 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002711
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002712 return dialog;
2713 }
2714
2715 public void onCancel(DialogInterface dialog) {
2716 mWaitingForResult = false;
2717 cleanup();
2718 }
2719
Romain Guycbb89e42009-06-08 15:52:54 -07002720 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002721 mWaitingForResult = false;
2722 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002723 }
2724
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002725 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002726 try {
2727 dismissDialog(DIALOG_CREATE_SHORTCUT);
2728 } catch (Exception e) {
2729 // An exception is thrown if the dialog is not visible, which is fine
2730 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002731 }
2732
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002733 /**
2734 * Handle the action clicked in the "Add to home" dialog.
2735 */
2736 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002737 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002738
Winson Chung55b65502011-05-26 12:03:43 -07002739 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2740 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002741 case AddAdapter.ITEM_APPLICATION: {
2742 if (mAppsCustomizeTabHost != null) {
2743 mAppsCustomizeTabHost.selectAppsTab();
2744 }
2745 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002746 break;
2747 }
Winson Chung55b65502011-05-26 12:03:43 -07002748 case AddAdapter.ITEM_APPWIDGET: {
2749 if (mAppsCustomizeTabHost != null) {
2750 mAppsCustomizeTabHost.selectWidgetsTab();
2751 }
2752 showAllApps(true);
2753 break;
2754 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002755 case AddAdapter.ITEM_WALLPAPER: {
2756 startWallpaper();
2757 break;
2758 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002759 }
2760 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002761
2762 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002763 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002764 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002765 }
2766
2767 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002768 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002769 */
2770 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2771 @Override
2772 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002773 closeSystemDialogs();
2774 }
2775 }
2776
2777 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002778 * Receives notifications whenever the appwidgets are reset.
2779 */
2780 private class AppWidgetResetObserver extends ContentObserver {
2781 public AppWidgetResetObserver() {
2782 super(new Handler());
2783 }
2784
2785 @Override
2786 public void onChange(boolean selfChange) {
2787 onAppWidgetReset();
2788 }
2789 }
2790
2791 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002792 * If the activity is currently paused, signal that we need to re-run the loader
2793 * in onResume.
2794 *
2795 * This needs to be called from incoming places where resources might have been loaded
2796 * while we are paused. That is becaues the Configuration might be wrong
2797 * when we're not running, and if it comes back to what it was when we
2798 * were paused, we are not restarted.
2799 *
2800 * Implementation of the method from LauncherModel.Callbacks.
2801 *
2802 * @return true if we are currently paused. The caller might be able to
2803 * skip some work in that case since we will come back again.
2804 */
2805 public boolean setLoadOnResume() {
2806 if (mPaused) {
2807 Log.i(TAG, "setLoadOnResume");
2808 mOnResumeNeedsLoad = true;
2809 return true;
2810 } else {
2811 return false;
2812 }
2813 }
2814
2815 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002816 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002817 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002818 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002819 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002820 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002821 } else {
2822 return SCREEN_COUNT / 2;
2823 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002824 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002825
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002826
Joe Onorato9c1289c2009-08-17 11:03:03 -04002827 /**
2828 * Refreshes the shortcuts shown on the workspace.
2829 *
2830 * Implementation of the method from LauncherModel.Callbacks.
2831 */
2832 public void startBinding() {
2833 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002834
2835 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002836 int count = workspace.getChildCount();
2837 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002838 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002839 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2840 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002841 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002842 if (mHotseat != null) {
2843 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002844 }
2845 }
2846
2847 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002848 * Bind the items start-end from the list.
2849 *
2850 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002851 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002852 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002853 setLoadOnResume();
2854
Joe Onorato9c1289c2009-08-17 11:03:03 -04002855 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002856 for (int i=start; i<end; i++) {
2857 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002858
2859 // Short circuit if we are loading dock items for a configuration which has no dock
2860 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2861 mHotseat == null) {
2862 continue;
2863 }
2864
Joe Onorato9c1289c2009-08-17 11:03:03 -04002865 switch (item.itemType) {
2866 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2867 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002868 View shortcut = createShortcut((ShortcutInfo)item);
2869 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2870 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002871 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002872 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002873 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002874 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002875 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002876 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2877 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002878 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002880 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002881 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002882 }
2883
Joe Onorato9c1289c2009-08-17 11:03:03 -04002884 /**
2885 * Implementation of the method from LauncherModel.Callbacks.
2886 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002887 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002888 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002889 sFolders.clear();
2890 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002891 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002892
2893 /**
2894 * Add the views for a widget to the workspace.
2895 *
2896 * Implementation of the method from LauncherModel.Callbacks.
2897 */
2898 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002899 setLoadOnResume();
2900
Daniel Sandler843e8602010-06-07 14:59:01 -04002901 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2902 if (DEBUG_WIDGETS) {
2903 Log.d(TAG, "bindAppWidget: " + item);
2904 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002905 final Workspace workspace = mWorkspace;
2906
2907 final int appWidgetId = item.appWidgetId;
2908 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002909 if (DEBUG_WIDGETS) {
2910 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2911 }
2912
Joe Onorato9c1289c2009-08-17 11:03:03 -04002913 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2914
Joe Onorato9c1289c2009-08-17 11:03:03 -04002915 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2916 item.hostView.setTag(item);
2917
Winson Chung3d503fb2011-07-13 17:25:49 -07002918 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002919 item.cellY, item.spanX, item.spanY, false);
2920
Adam Cohended9f8d2010-11-03 13:25:16 -07002921 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2922
Joe Onorato9c1289c2009-08-17 11:03:03 -04002923 workspace.requestLayout();
2924
Daniel Sandler843e8602010-06-07 14:59:01 -04002925 if (DEBUG_WIDGETS) {
2926 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2927 + (SystemClock.uptimeMillis()-start) + "ms");
2928 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002929 }
2930
2931 /**
2932 * Callback saying that there aren't any more items to bind.
2933 *
2934 * Implementation of the method from LauncherModel.Callbacks.
2935 */
2936 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002937 setLoadOnResume();
2938
Joe Onorato9c1289c2009-08-17 11:03:03 -04002939 if (mSavedState != null) {
2940 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002941 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002942 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002943 mSavedState = null;
2944 }
2945
2946 if (mSavedInstanceState != null) {
2947 super.onRestoreInstanceState(mSavedInstanceState);
2948 mSavedInstanceState = null;
2949 }
2950
Joe Onorato9c1289c2009-08-17 11:03:03 -04002951 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002952
2953 // If we received the result of any pending adds while the loader was running (e.g. the
2954 // widget configuration forced an orientation change), process them now.
2955 for (int i = 0; i < sPendingAddList.size(); i++) {
2956 completeAdd(sPendingAddList.get(i));
2957 }
2958 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002959
Winson Chungc51db6a2011-10-05 11:44:49 -07002960 // Update the market app icon as necessary (the other icons will be managed in response to
2961 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002962 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002963 }
2964
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002965 @Override
2966 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07002967 boolean searchVisible = updateGlobalSearchIcon();
2968 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
2969 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002970 }
2971
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002972 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002973 * Add the icons for all apps.
2974 *
2975 * Implementation of the method from LauncherModel.Callbacks.
2976 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002977 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2978 // Remove the progress bar entirely; we could also make it GONE
2979 // but better to remove it since we know it's not going to be used
2980 View progressBar = mAppsCustomizeTabHost.
2981 findViewById(R.id.apps_customize_progress_bar);
2982 if (progressBar != null) {
2983 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002984 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002985 // We just post the call to setApps so the user sees the progress bar
2986 // disappear-- otherwise, it just looks like the progress bar froze
2987 // which doesn't look great
2988 mAppsCustomizeTabHost.post(new Runnable() {
2989 public void run() {
2990 if (mAppsCustomizeContent != null) {
2991 mAppsCustomizeContent.setApps(apps);
2992 }
2993 }
2994 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002995 }
2996
2997 /**
2998 * A package was installed.
2999 *
3000 * Implementation of the method from LauncherModel.Callbacks.
3001 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003002 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003003 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003004 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003005
Winson Chung785d2eb2011-04-14 16:08:02 -07003006 if (mAppsCustomizeContent != null) {
3007 mAppsCustomizeContent.addApps(apps);
3008 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003009 }
3010
3011 /**
3012 * A package was updated.
3013 *
3014 * Implementation of the method from LauncherModel.Callbacks.
3015 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003016 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003017 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003018 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003019 if (mWorkspace != null) {
3020 mWorkspace.updateShortcuts(apps);
3021 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003022
Winson Chung785d2eb2011-04-14 16:08:02 -07003023 if (mAppsCustomizeContent != null) {
3024 mAppsCustomizeContent.updateApps(apps);
3025 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003026 }
3027
3028 /**
3029 * A package was uninstalled.
3030 *
3031 * Implementation of the method from LauncherModel.Callbacks.
3032 */
Joe Onorato36115782010-06-17 13:28:48 -04003033 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003034 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003035 if (permanent) {
3036 mWorkspace.removeItems(apps);
3037 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003038
Winson Chung785d2eb2011-04-14 16:08:02 -07003039 if (mAppsCustomizeContent != null) {
3040 mAppsCustomizeContent.removeApps(apps);
3041 }
Winson Chunga1820962011-10-03 16:31:06 -07003042
3043 // Notify the drag controller
3044 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003045 }
Joe Onoratobe386092009-11-17 17:32:16 -08003046
3047 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003048 * A number of packages were updated.
3049 */
3050 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003051 if (mAppsCustomizeContent != null) {
3052 mAppsCustomizeContent.onPackagesUpdated();
3053 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003054 }
3055
Winson Chung400438b2011-01-16 17:53:48 -08003056 private int mapConfigurationOriActivityInfoOri(int configOri) {
3057 final Display d = getWindowManager().getDefaultDisplay();
3058 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3059 switch (d.getRotation()) {
3060 case Surface.ROTATION_0:
3061 case Surface.ROTATION_180:
3062 // We are currently in the same basic orientation as the natural orientation
3063 naturalOri = configOri;
3064 break;
3065 case Surface.ROTATION_90:
3066 case Surface.ROTATION_270:
3067 // We are currently in the other basic orientation to the natural orientation
3068 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3069 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3070 break;
3071 }
3072
3073 int[] oriMap = {
3074 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3075 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3076 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3077 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3078 };
3079 // Since the map starts at portrait, we need to offset if this device's natural orientation
3080 // is landscape.
3081 int indexOffset = 0;
3082 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3083 indexOffset = 1;
3084 }
3085 return oriMap[(d.getRotation() + indexOffset) % 4];
3086 }
Adam Cohen446e9402011-09-15 18:21:21 -07003087
3088 public void lockScreenOrientationOnLargeUI() {
3089 if (LauncherApplication.isScreenLarge()) {
3090 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3091 .getConfiguration().orientation));
3092 }
Winson Chung400438b2011-01-16 17:53:48 -08003093 }
Adam Cohen446e9402011-09-15 18:21:21 -07003094 public void unlockScreenOrientationOnLargeUI() {
3095 if (LauncherApplication.isScreenLarge()) {
3096 mHandler.postDelayed(new Runnable() {
3097 public void run() {
3098 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3099 }
3100 }, mRestoreScreenOrientationDelay);
3101 }
Winson Chung400438b2011-01-16 17:53:48 -08003102 }
3103
Winson Chung82f55532011-08-09 14:14:23 -07003104 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003105 private static final String PREFS_KEY = "com.android.launcher2.prefs";
3106 private boolean isClingsEnabled() {
Winson Chung82f55532011-08-09 14:14:23 -07003107 // TEMPORARY: DISABLE CLINGS ON LARGE UI
Winson Chung7d7541e2011-09-16 20:14:36 -07003108 if (LauncherApplication.isScreenLarge()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003109 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003110 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003111
Winson Chung7d7541e2011-09-16 20:14:36 -07003112 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003113 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003114 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3115 Cling cling = (Cling) findViewById(clingId);
3116 if (cling != null) {
3117 cling.init(this, positionData);
3118 cling.setVisibility(View.VISIBLE);
3119 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3120 if (animate) {
3121 cling.buildLayer();
3122 cling.setAlpha(0f);
3123 cling.animate()
3124 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003125 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003126 .setDuration(SHOW_CLING_DURATION)
3127 .setStartDelay(delay)
3128 .start();
3129 } else {
3130 cling.setAlpha(1f);
3131 }
3132 }
3133 return cling;
3134 }
3135 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003136 if (cling != null) {
3137 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003138 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003139 anim.addListener(new AnimatorListenerAdapter() {
3140 public void onAnimationEnd(Animator animation) {
3141 cling.setVisibility(View.GONE);
3142 cling.cleanup();
3143 SharedPreferences prefs =
3144 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3145 SharedPreferences.Editor editor = prefs.edit();
3146 editor.putBoolean(flag, true);
3147 editor.commit();
3148 };
3149 });
3150 anim.start();
3151 }
3152 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003153 private void removeCling(int id) {
3154 final View cling = findViewById(id);
3155 if (cling != null) {
3156 final ViewGroup parent = (ViewGroup) cling.getParent();
3157 parent.post(new Runnable() {
3158 @Override
3159 public void run() {
3160 parent.removeView(cling);
3161 }
3162 });
3163 }
3164 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003165 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003166 // Enable the clings only if they have not been dismissed before
3167 SharedPreferences prefs =
3168 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003169 if (isClingsEnabled() && !prefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003170 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003171 } else {
3172 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003173 }
3174 }
3175 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003176 // Enable the clings only if they have not been dismissed before
3177 SharedPreferences prefs =
3178 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003179 if (isClingsEnabled() && !prefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003180 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003181 } else {
3182 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003183 }
3184 }
3185 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003186 // Enable the clings only if they have not been dismissed before
3187 SharedPreferences prefs =
3188 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
3189 Cling cling = null;
Winson Chung9d9d74f2011-09-19 11:49:12 -07003190 if (isClingsEnabled() && !prefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003191 cling = initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003192 } else {
3193 removeCling(R.id.folder_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003194 }
3195 return cling;
3196 }
3197 public boolean isFolderClingVisible() {
3198 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003199 if (cling != null) {
3200 return cling.getVisibility() == View.VISIBLE;
3201 }
3202 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003203 }
Winson Chung82f55532011-08-09 14:14:23 -07003204 public void dismissWorkspaceCling(View v) {
3205 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003206 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003207 }
3208 public void dismissAllAppsCling(View v) {
3209 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003210 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3211 }
3212 public void dismissFolderCling(View v) {
3213 Cling cling = (Cling) findViewById(R.id.folder_cling);
3214 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003215 }
3216
Winson Chung80baf5a2010-08-09 16:03:15 -07003217 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003218 * Prints out out state for debugging.
3219 */
3220 public void dumpState() {
3221 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003222 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003223 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3224 Log.d(TAG, "mRestoring=" + mRestoring);
3225 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3226 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003227 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003228 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003229
Winson Chung785d2eb2011-04-14 16:08:02 -07003230 if (mAppsCustomizeContent != null) {
3231 mAppsCustomizeContent.dumpState();
3232 }
Joe Onoratobe386092009-11-17 17:32:16 -08003233 Log.d(TAG, "END launcher2 dump state");
3234 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003235}
Michael Jurka2763be32011-02-24 11:19:57 -08003236
Michael Jurkaabded662011-03-04 12:06:57 -08003237interface LauncherTransitionable {
Winson Chung7d7541e2011-09-16 20:14:36 -07003238 void onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace);
3239 void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003240}