blob: 5eaf7730af69231c9daa252bb38a88bc309f10b3 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Michael Jurka946ad472010-07-09 18:05:18 -070026import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070027import android.app.ActivityManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080028import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.Dialog;
30import android.app.SearchManager;
31import android.app.StatusBarManager;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070036import android.content.ClipData;
37import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
41import android.content.DialogInterface;
42import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040053import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070054import android.os.AsyncTask;
Adam Cohen50370f32011-08-25 18:57:14 -070055import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080056import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020057import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080058import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070059import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040060import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080061import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070062import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080063import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
66import android.text.TextUtils;
67import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070068import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080069import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080070import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.view.KeyEvent;
72import android.view.LayoutInflater;
73import android.view.Menu;
74import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070075import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080076import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080077import android.view.View;
Winson Chung82f55532011-08-09 14:14:23 -070078import android.view.ViewGroup;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070079import android.view.WindowManager;
Adam Cohen82ebbd22011-09-30 15:05:40 -070080import android.view.View.OnLongClickListener;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080081import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070082import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070083import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080084import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070086import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070088import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070089import android.widget.TextView;
90import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070091
Adam Cohendf2cc412011-04-27 16:56:57 -070092import com.android.common.Search;
93import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070094import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080095
Adam Cohenc0dcf592011-06-01 15:30:43 -070096import java.io.DataInputStream;
97import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070098import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.FileNotFoundException;
100import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700101import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.util.ArrayList;
103import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700104
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800105/**
106 * Default launcher application.
107 */
Michael Jurka946ad472010-07-09 18:05:18 -0700108public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700109 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
110 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800111 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700112 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800113
Joe Onorato9c1289c2009-08-17 11:03:03 -0400114 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400115 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700116
Winson Chung70d72102011-08-12 11:24:35 -0700117 private static final int MENU_GROUP_WALLPAPER = 1;
118 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
119 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700120 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
121 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
123 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700124 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125 private static final int REQUEST_PICK_APPLICATION = 6;
126 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700127 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700128 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129
130 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
131
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800132 static final int SCREEN_COUNT = 5;
133 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
Joe Onorato7c312c12009-08-13 21:36:53 -0700135 static final int DIALOG_CREATE_SHORTCUT = 1;
136 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Romain Guy98d01652009-06-30 16:21:04 -0700138 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
140 // Type: int
141 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700142 // Type: int
143 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700145 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
146 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
148 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700149 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700151 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 // Type: boolean
153 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
154 // Type: long
155 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
156
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700157 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
158
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700159 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700160 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700161 private State mState = State.WORKSPACE;
162 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700163
Joe Onorato9c1289c2009-08-17 11:03:03 -0400164 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700165 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
166 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700167 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700168 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800170 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800171 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800173 private final BroadcastReceiver mCloseSystemDialogsReceiver
174 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800175 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
176
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 private LayoutInflater mInflater;
178
Joe Onorato00acb122009-08-04 16:04:30 -0400179 private DragController mDragController;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private Workspace mWorkspace;
Romain Guycbb89e42009-06-08 15:52:54 -0700181
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700182 private AppWidgetManager mAppWidgetManager;
183 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700184
Michael Jurkac9d95c52011-08-29 14:03:34 -0700185 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700186 private int[] mTmpAddItemCellCoordinates = new int[2];
187
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 private FolderInfo mFolderInfo;
189
Winson Chung3d503fb2011-07-13 17:25:49 -0700190 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800191 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700192
Winson Chungc51db6a2011-10-05 11:44:49 -0700193 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700194 private AppsCustomizeTabHost mAppsCustomizeTabHost;
195 private AppsCustomizePagedView mAppsCustomizeContent;
196 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private Bundle mSavedState;
199
200 private SpannableStringBuilder mDefaultKeySsb = null;
201
Joe Onorato9c1289c2009-08-17 11:03:03 -0400202 private boolean mWorkspaceLoading = true;
203
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800204 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 private boolean mRestoring;
206 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700207 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
209 private Bundle mSavedInstanceState;
210
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800212 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800213 private boolean mUserPresent = true;
214 private boolean mVisible = false;
215 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400216
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700217 private static LocaleConfiguration sLocaleConfiguration = null;
218
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700219 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700220
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700221 private Intent mAppMarketIntent = null;
222
Adam Cohended9f8d2010-11-03 13:25:16 -0700223 // Related to the auto-advancing of widgets
224 private final int ADVANCE_MSG = 1;
225 private final int mAdvanceInterval = 20000;
226 private final int mAdvanceStagger = 250;
227 private long mAutoAdvanceSentTime;
228 private long mAutoAdvanceTimeLeft = -1;
229 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
230 new HashMap<View, AppWidgetProviderInfo>();
231
Winson Chung400438b2011-01-16 17:53:48 -0800232 // Determines how long to wait after a rotation before restoring the screen orientation to
233 // match the sensor state.
234 private final int mRestoreScreenOrientationDelay = 500;
235
Michael Jurka4ef207b2010-11-29 17:05:45 -0800236 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700237 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
238 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
239 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800240
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700241 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
242
Adam Cohen2801caf2011-05-13 20:57:39 -0700243 private DragLayer mDragLayer;
244
Michael Jurkaddd62e92011-02-16 17:49:14 -0800245 private BubbleTextView mWaitingForResume;
246
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800247 private static ArrayList<PendingAddArguments> sPendingAddList
248 = new ArrayList<PendingAddArguments>();
249
250 private static class PendingAddArguments {
251 int requestCode;
252 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700253 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800254 int screen;
255 int cellX;
256 int cellY;
257 }
258
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800259 @Override
260 protected void onCreate(Bundle savedInstanceState) {
261 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800262 LauncherApplication app = ((LauncherApplication)getApplication());
263 mModel = app.setLauncher(this);
264 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400265 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700267
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700268 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700269 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
270 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700271
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200273 android.os.Debug.startMethodTracing(
274 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800275 }
276
277 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 setContentView(R.layout.launcher);
279 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700280 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800282 registerContentObservers();
283
Joe Onorato7c312c12009-08-13 21:36:53 -0700284 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 mSavedState = savedInstanceState;
287 restoreState(mSavedState);
288
Winson Chunga12a2502010-12-20 14:41:35 -0800289 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700290 if (mAppsCustomizeContent != null) {
291 mAppsCustomizeContent.onPackagesUpdated();
292 }
Winson Chunga12a2502010-12-20 14:41:35 -0800293
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294 if (PROFILE_STARTUP) {
295 android.os.Debug.stopMethodTracing();
296 }
297
298 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 }
301
Michael Jurkac57b7a82011-08-09 22:02:20 -0700302 if (!mModel.isAllAppsLoaded()) {
303 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
304 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
305 }
306
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 // For handling default keys
308 mDefaultKeySsb = new SpannableStringBuilder();
309 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800310
311 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
312 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800313
Winson Chungc51db6a2011-10-05 11:44:49 -0700314 boolean searchVisible = false;
315 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800316 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700317 int coi = getCurrentOrientationIndexForGlobalIcons();
318 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
319 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700320 updateAppMarketIcon();
321 searchVisible = updateGlobalSearchIcon();
322 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700323 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700324 if (sGlobalSearchIcon[coi] != null) {
325 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700326 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700327 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700328 if (sVoiceSearchIcon[coi] != null) {
329 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700330 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700331 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700332 if (sAppMarketIcon[coi] != null) {
333 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800334 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700335 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700336
337 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohendc16b602011-09-26 15:12:33 -0700338 if (LauncherApplication.isScreenLarge() || Build.TYPE.contentEquals("eng")) {
Adam Cohen446e9402011-09-15 18:21:21 -0700339 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
340 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800341 }
Romain Guycbb89e42009-06-08 15:52:54 -0700342
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700344 if (sLocaleConfiguration == null) {
345 new AsyncTask<Void, Void, LocaleConfiguration>() {
346 @Override
347 protected LocaleConfiguration doInBackground(Void... unused) {
348 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
349 readConfiguration(Launcher.this, localeConfiguration);
350 return localeConfiguration;
351 }
352
353 @Override
354 protected void onPostExecute(LocaleConfiguration result) {
355 sLocaleConfiguration = result;
356 checkForLocaleChange(); // recursive, but now with a locale configuration
357 }
358 }.execute();
359 return;
360 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700361
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 final Configuration configuration = getResources().getConfiguration();
363
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700364 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 final String locale = configuration.locale.toString();
366
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700367 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 final int mcc = configuration.mcc;
369
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700370 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 final int mnc = configuration.mnc;
372
Romain Guy84f296c2009-11-04 15:00:44 -0800373 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374
Romain Guy84f296c2009-11-04 15:00:44 -0800375 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700376 sLocaleConfiguration.locale = locale;
377 sLocaleConfiguration.mcc = mcc;
378 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700379
Joe Onorato0589f0f2010-02-08 13:44:00 -0800380 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700381
382 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
383 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700384 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700385 public void run() {
386 writeConfiguration(Launcher.this, localeConfiguration);
387 }
388 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700389 }
390 }
391
392 private static class LocaleConfiguration {
393 public String locale;
394 public int mcc = -1;
395 public int mnc = -1;
396 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700397
Romain Guy98d01652009-06-30 16:21:04 -0700398 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
399 DataInputStream in = null;
400 try {
401 in = new DataInputStream(context.openFileInput(PREFERENCES));
402 configuration.locale = in.readUTF();
403 configuration.mcc = in.readInt();
404 configuration.mnc = in.readInt();
405 } catch (FileNotFoundException e) {
406 // Ignore
407 } catch (IOException e) {
408 // Ignore
409 } finally {
410 if (in != null) {
411 try {
412 in.close();
413 } catch (IOException e) {
414 // Ignore
415 }
416 }
417 }
418 }
419
420 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
421 DataOutputStream out = null;
422 try {
423 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
424 out.writeUTF(configuration.locale);
425 out.writeInt(configuration.mcc);
426 out.writeInt(configuration.mnc);
427 out.flush();
428 } catch (FileNotFoundException e) {
429 // Ignore
430 } catch (IOException e) {
431 //noinspection ResultOfMethodCallIgnored
432 context.getFileStreamPath(PREFERENCES).delete();
433 } finally {
434 if (out != null) {
435 try {
436 out.close();
437 } catch (IOException e) {
438 // Ignore
439 }
440 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 }
442 }
443
Adam Cohen716b51e2011-06-30 12:09:54 -0700444 public DragLayer getDragLayer() {
445 return mDragLayer;
446 }
447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 static int getScreen() {
449 synchronized (sLock) {
450 return sScreen;
451 }
452 }
453
454 static void setScreen(int screen) {
455 synchronized (sLock) {
456 sScreen = screen;
457 }
458 }
459
Winson Chung557d6ed2011-07-08 15:34:52 -0700460 /**
461 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
462 * a configuration step, this allows the proper animations to run after other transitions.
463 */
464 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700465 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800466 switch (args.requestCode) {
467 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700468 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
469 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800470 break;
471 case REQUEST_PICK_SHORTCUT:
472 processShortcut(args.intent);
473 break;
474 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700475 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
476 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700477 result = true;
478 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800479 case REQUEST_PICK_APPWIDGET:
480 addAppWidgetFromPick(args.intent);
481 break;
482 case REQUEST_CREATE_APPWIDGET:
483 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700484 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700485 result = true;
486 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800487 case REQUEST_PICK_WALLPAPER:
488 // We just wanted the activity result here so we can clear mWaitingForResult
489 break;
490 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700491 // In any situation where we have a multi-step drop, we should reset the add info only after
492 // we complete the drop
493 resetAddInfo();
494 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800495 }
496
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800497 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800498 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700499 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700500 mWaitingForResult = false;
501
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800502 // The pattern used here is that a user PICKs a specific application,
503 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700504
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800505 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
506 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700507 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800508 final PendingAddArguments args = new PendingAddArguments();
509 args.requestCode = requestCode;
510 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700511 args.container = mPendingAddInfo.container;
512 args.screen = mPendingAddInfo.screen;
513 args.cellX = mPendingAddInfo.cellX;
514 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800515
516 // If the loader is still running, defer the add until it is done.
517 if (isWorkspaceLocked()) {
518 sPendingAddList.add(args);
519 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700520 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800521 }
Romain Guy18042c82009-11-06 11:44:55 -0800522 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700523 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
524 if (data != null) {
525 // Clean up the appWidgetId if we canceled
526 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
527 if (appWidgetId != -1) {
528 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
529 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800530 }
531 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700532
533 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700534 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800535 }
536
537 @Override
538 protected void onResume() {
539 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800540 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700541 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400542 mWorkspaceLoading = true;
543 mModel.startLoader(this, true);
544 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700545 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800547 if (mWaitingForResume != null) {
548 mWaitingForResume.setStayPressed(false);
549 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700550 // When we resume Launcher, a different Activity might be responsible for the app
551 // market intent, so refresh the icon
552 updateAppMarketIcon();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800553 }
554
555 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700556 protected void onPause() {
557 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700558 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800559 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700560 }
Romain Guycbb89e42009-06-08 15:52:54 -0700561
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700562 @Override
563 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400564 // Flag the loader to stop early before switching
565 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700566 if (mAppsCustomizeContent != null) {
567 mAppsCustomizeContent.surrender();
568 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800569 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700570 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700571
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800572 // We can't hide the IME if it was forced open. So don't bother
573 /*
574 @Override
575 public void onWindowFocusChanged(boolean hasFocus) {
576 super.onWindowFocusChanged(hasFocus);
577
578 if (hasFocus) {
579 final InputMethodManager inputManager = (InputMethodManager)
580 getSystemService(Context.INPUT_METHOD_SERVICE);
581 WindowManager.LayoutParams lp = getWindow().getAttributes();
582 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
583 android.os.Handler()) {
584 protected void onReceiveResult(int resultCode, Bundle resultData) {
585 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
586 }
587 });
588 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
589 }
590 }
591 */
592
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 private boolean acceptFilter() {
594 final InputMethodManager inputManager = (InputMethodManager)
595 getSystemService(Context.INPUT_METHOD_SERVICE);
596 return !inputManager.isFullscreenMode();
597 }
598
599 @Override
600 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700601 final int uniChar = event.getUnicodeChar();
602 final boolean handled = super.onKeyDown(keyCode, event);
603 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
604 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800605 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
606 keyCode, event);
607 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700608 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700609 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700610 // showSearchDialog()
611 // If there are multiple keystrokes before the search dialog takes focus,
612 // onSearchRequested() will be called for every keystroke,
613 // but it is idempotent, so it's fine.
614 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 }
616 }
617
Joe Onorato8a9625e2010-01-28 15:55:35 -0800618 // Eat the long press event so the keyboard doesn't come up.
619 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
620 return true;
621 }
622
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 return handled;
624 }
625
Karl Rosaen138a0412009-04-23 19:00:21 -0700626 private String getTypedText() {
627 return mDefaultKeySsb.toString();
628 }
629
630 private void clearTypedText() {
631 mDefaultKeySsb.clear();
632 mDefaultKeySsb.clearSpans();
633 Selection.setSelection(mDefaultKeySsb, 0);
634 }
635
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800636 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700637 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
638 * State
639 */
640 private static State intToState(int stateOrdinal) {
641 State state = State.WORKSPACE;
642 final State[] stateValues = State.values();
643 for (int i = 0; i < stateValues.length; i++) {
644 if (stateValues[i].ordinal() == stateOrdinal) {
645 state = stateValues[i];
646 break;
647 }
648 }
649 return state;
650 }
651
652 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 * Restores the previous state, if it exists.
654 *
655 * @param savedState The previous state.
656 */
657 private void restoreState(Bundle savedState) {
658 if (savedState == null) {
659 return;
660 }
661
Michael Jurka883f55b2010-10-21 15:47:14 -0700662 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700663 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800664 showAllApps(false);
665 }
666
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
668 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700669 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
671
Winson Chung3d503fb2011-07-13 17:25:49 -0700672 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
673 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700674
Winson Chung3d503fb2011-07-13 17:25:49 -0700675 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
676 mPendingAddInfo.container = pendingAddContainer;
677 mPendingAddInfo.screen = pendingAddScreen;
678 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
679 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 mRestoring = true;
681 }
682
683 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
684 if (renameFolder) {
685 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700686 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 mRestoring = true;
688 }
Winson Chunga12a2502010-12-20 14:41:35 -0800689
Winson Chung785d2eb2011-04-14 16:08:02 -0700690
691 // Restore the AppsCustomize tab
692 if (mAppsCustomizeTabHost != null) {
693 String curTab = savedState.getString("apps_customize_currentTab");
694 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700695 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700696 mAppsCustomizeContent.setContentType(
697 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
698 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700699 mAppsCustomizeContent.loadAssociatedPages(
700 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700701 }
702
Winson Chung5afbf7b2011-07-25 11:53:08 -0700703 int currentIndex = savedState.getInt("apps_customize_currentIndex");
704 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700705 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 }
707
708 /**
709 * Finds all the views we need and configure them properly.
710 */
711 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700712 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400713
Winson Chung4c98d922011-05-31 16:50:48 -0700714 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
715 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716
Winson Chung4c98d922011-05-31 16:50:48 -0700717 // Setup the drag layer
718 mDragLayer.setup(this, dragController);
719
Winson Chung3d503fb2011-07-13 17:25:49 -0700720 // Setup the hotseat
721 mHotseat = (Hotseat) findViewById(R.id.hotseat);
722 if (mHotseat != null) {
723 mHotseat.setup(this);
724 }
725
Winson Chung4c98d922011-05-31 16:50:48 -0700726 // Setup the workspace
727 mWorkspace.setHapticFeedbackEnabled(false);
728 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700729 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700730 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700731
Winson Chungf0ea4d32011-06-06 14:27:16 -0700732 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700733 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700734
Winson Chungf0ea4d32011-06-06 14:27:16 -0700735 // Setup AppsCustomize
736 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
737 findViewById(R.id.apps_customize_pane);
738 mAppsCustomizeContent = (AppsCustomizePagedView)
739 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
740 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400741
Romain Guy1fbc1c82009-11-09 20:43:08 -0800742
Winson Chung4c98d922011-05-31 16:50:48 -0700743
Winson Chung3d503fb2011-07-13 17:25:49 -0700744 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700745 dragController.setDragScoller(mWorkspace);
746 dragController.setScrollView(mDragLayer);
747 dragController.setMoveTarget(mWorkspace);
748 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700749 if (mSearchDropTargetBar != null) {
750 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800751 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752 }
753
754 /**
755 * Creates a view representing a shortcut.
756 *
757 * @param info The data structure describing the shortcut.
758 *
759 * @return A View inflated from R.layout.application.
760 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800761 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700763 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 }
765
766 /**
767 * Creates a view representing a shortcut inflated from the specified resource.
768 *
769 * @param layoutResId The id of the XML layout used to create the shortcut.
770 * @param parent The group the shortcut belongs to.
771 * @param info The data structure describing the shortcut.
772 *
773 * @return A View inflated from layoutResId.
774 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800775 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800776 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
777 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800779 return favorite;
780 }
781
782 /**
783 * Add an application shortcut to the workspace.
784 *
785 * @param data The intent describing the application.
786 * @param cellInfo The position on screen where to create the shortcut.
787 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700788 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700789 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700790 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700791
Adam Cohenfbba09b2011-07-18 21:43:05 -0700792 // First we check if we already know the exact location where we want to add this item.
793 if (cellX >= 0 && cellY >= 0) {
794 cellXY[0] = cellX;
795 cellXY[1] = cellY;
796 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700797 showOutOfSpaceMessage();
798 return;
799 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800800
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800801 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800802
Romain Guy73b979d2009-06-09 12:57:21 -0700803 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800804 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800806 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700807 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700808 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800809 } else {
810 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 }
812 }
Romain Guycbb89e42009-06-08 15:52:54 -0700813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 /**
815 * Add a shortcut to the workspace.
816 *
817 * @param data The intent describing the shortcut.
818 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800819 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700820 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
821 int cellY) {
822 int[] cellXY = mTmpAddItemCellCoordinates;
823 int[] touchXY = mPendingAddInfo.dropPos;
824 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700825
Michael Jurkad3ef3062010-11-23 16:23:58 -0800826 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700827
Adam Cohen558baaf2011-08-15 15:22:57 -0700828 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
829 final View view = createShortcut(info);
830
Adam Cohenfbba09b2011-07-18 21:43:05 -0700831 // First we check if we already know the exact location where we want to add this item.
832 if (cellX >= 0 && cellY >= 0) {
833 cellXY[0] = cellX;
834 cellXY[1] = cellY;
835 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700836
837 // If appropriate, either create a folder or add to an existing folder
838 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
839 true, null,null)) {
840 return;
841 }
842 DragObject dragObject = new DragObject();
843 dragObject.dragInfo = info;
844 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
845 return;
846 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700847 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800848 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700849 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800850 foundCellSpan = (result != null);
851 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700852 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800853 }
854
855 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700856 showOutOfSpaceMessage();
857 return;
858 }
859
Adam Cohen558baaf2011-08-15 15:22:57 -0700860 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861
862 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700863 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
864 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 }
866 }
867
Adam Cohen50370f32011-08-25 18:57:14 -0700868 class Padding {
869 int left = 0;
870 int right = 0;
871 int top = 0;
872 int bottom = 0;
873 }
874
Adam Cohenf814aa02011-09-01 13:48:05 -0700875 Padding getPaddingForWidget(ComponentName component) {
Adam Cohen50370f32011-08-25 18:57:14 -0700876 PackageManager packageManager = getPackageManager();
877 Padding p = new Padding();
878 android.content.pm.ApplicationInfo appInfo;
879
880 try {
Adam Cohenf814aa02011-09-01 13:48:05 -0700881 appInfo = packageManager.getApplicationInfo(component.getPackageName(), 0);
Adam Cohen50370f32011-08-25 18:57:14 -0700882 } catch (Exception e) {
883 // if we can't find the package, return 0 padding
884 return p;
885 }
886
Adam Cohenff648b52011-09-19 11:57:30 -0700887 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Adam Cohen50370f32011-08-25 18:57:14 -0700888 Resources r = getResources();
Adam Cohen82ebbd22011-09-30 15:05:40 -0700889 // The default padding values are private API currently, but will be added in
890 // API level 15. The current values are (8, 8, 8, 8).
891 p.left = r.getDimensionPixelSize(com.android.internal.
892 R.dimen.default_app_widget_padding_left);
893 p.right = r.getDimensionPixelSize(com.android.internal.
894 R.dimen.default_app_widget_padding_right);
895 p.top = r.getDimensionPixelSize(com.android.internal.
896 R.dimen.default_app_widget_padding_top);
897 p.bottom = r.getDimensionPixelSize(com.android.internal.
898 R.dimen.default_app_widget_padding_bottom);
Adam Cohen50370f32011-08-25 18:57:14 -0700899 }
900
901 return p;
902 }
903
Adam Cohenf814aa02011-09-01 13:48:05 -0700904 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
905 if (spanXY == null) {
906 spanXY = new int[2];
907 }
908
909 Padding padding = getPaddingForWidget(component);
910 // We want to account for the extra amount of padding that we are adding to the widget
911 // to ensure that it gets the full amount of space that it has requested
912 int requiredWidth = minWidth + padding.left + padding.right;
913 int requiredHeight = minHeight + padding.top + padding.bottom;
914 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
915 }
916
917 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
918 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
919 }
920
Adam Cohencbf47e32011-09-16 17:32:37 -0700921 int[] getMinResizeSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
922 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
923 }
924
Adam Cohenf814aa02011-09-01 13:48:05 -0700925 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
926 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
927 }
928
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700930 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700932 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700933 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700935 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700936 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700937
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700938 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700939 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700940
Adam Cohenf814aa02011-09-01 13:48:05 -0700941 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700942
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700944 // We have saved the position to which the widget was dragged-- this really only matters
945 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700946 int[] cellXY = mTmpAddItemCellCoordinates;
947 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700948 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700949 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
950 cellXY[0] = mPendingAddInfo.cellX;
951 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700952 foundCellSpan = true;
953 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700954 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700955 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700956 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800957 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700958 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700959 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700960 }
961
Michael Jurka0280c3b2010-09-17 15:00:07 -0700962 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800963 if (appWidgetId != -1) {
964 // Deleting an app widget ID is a void call but writes to disk before returning
965 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800966 new Thread("deleteAppWidgetId") {
967 public void run() {
968 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
969 }
970 }.start();
971 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700972 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800973 return;
974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700976 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -0700977 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700978 launcherInfo.spanX = spanXY[0];
979 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700980
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700982 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800983
984 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700986 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700987
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700988 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700990
Winson Chung3d503fb2011-07-13 17:25:49 -0700991 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -0400992 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -0700993
994 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 }
996 }
Romain Guycbb89e42009-06-08 15:52:54 -0700997
Adam Cohended9f8d2010-11-03 13:25:16 -0700998 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
999 @Override
1000 public void onReceive(Context context, Intent intent) {
1001 final String action = intent.getAction();
1002 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1003 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001004 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001005 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001006
Winson Chungc100e8e2011-08-09 16:02:43 -07001007 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001008 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001009 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1010 mAppsCustomizeTabHost.reset();
1011 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001012 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001013 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1014 mUserPresent = true;
1015 updateRunning();
1016 }
1017 }
1018 };
1019
1020 @Override
1021 public void onAttachedToWindow() {
1022 super.onAttachedToWindow();
1023
1024 // Listen for broadcasts related to user-presence
1025 final IntentFilter filter = new IntentFilter();
1026 filter.addAction(Intent.ACTION_SCREEN_OFF);
1027 filter.addAction(Intent.ACTION_USER_PRESENT);
1028 registerReceiver(mReceiver, filter);
1029
Adam Cohend113e0c2010-11-11 10:48:05 -08001030 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001031 mVisible = true;
1032 }
1033
1034 @Override
1035 public void onDetachedFromWindow() {
1036 super.onDetachedFromWindow();
1037 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001038 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001039
Adam Cohend113e0c2010-11-11 10:48:05 -08001040 if (mAttached) {
1041 unregisterReceiver(mReceiver);
1042 mAttached = false;
1043 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001044 updateRunning();
1045 }
1046
1047 public void onWindowVisibilityChanged(int visibility) {
1048 mVisible = visibility == View.VISIBLE;
1049 updateRunning();
1050 }
1051
1052 private void sendAdvanceMessage(long delay) {
1053 mHandler.removeMessages(ADVANCE_MSG);
1054 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1055 mHandler.sendMessageDelayed(msg, delay);
1056 mAutoAdvanceSentTime = System.currentTimeMillis();
1057 }
1058
1059 private void updateRunning() {
1060 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1061 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1062 mAutoAdvanceRunning = autoAdvanceRunning;
1063 if (autoAdvanceRunning) {
1064 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1065 sendAdvanceMessage(delay);
1066 } else {
1067 if (!mWidgetsToAdvance.isEmpty()) {
1068 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1069 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1070 }
1071 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001072 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001073 }
1074 }
1075 }
1076
1077 private final Handler mHandler = new Handler() {
1078 @Override
1079 public void handleMessage(Message msg) {
1080 if (msg.what == ADVANCE_MSG) {
1081 int i = 0;
1082 for (View key: mWidgetsToAdvance.keySet()) {
1083 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1084 final int delay = mAdvanceStagger * i;
1085 if (v instanceof Advanceable) {
1086 postDelayed(new Runnable() {
1087 public void run() {
1088 ((Advanceable) v).advance();
1089 }
1090 }, delay);
1091 }
1092 i++;
1093 }
1094 sendAdvanceMessage(mAdvanceInterval);
1095 }
1096 }
1097 };
1098
1099 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001100 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001101 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1102 if (v instanceof Advanceable) {
1103 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001104 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001105 updateRunning();
1106 }
1107 }
1108
1109 void removeWidgetToAutoAdvance(View hostView) {
1110 if (mWidgetsToAdvance.containsKey(hostView)) {
1111 mWidgetsToAdvance.remove(hostView);
1112 updateRunning();
1113 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001114 }
1115
Joe Onorato9c1289c2009-08-17 11:03:03 -04001116 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001117 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001118 launcherInfo.hostView = null;
1119 }
1120
Adam Cohended9f8d2010-11-03 13:25:16 -07001121 void showOutOfSpaceMessage() {
1122 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1123 }
1124
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001125 public LauncherAppWidgetHost getAppWidgetHost() {
1126 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 }
Romain Guycbb89e42009-06-08 15:52:54 -07001128
Winson Chunga9abd0e2010-10-27 17:18:37 -07001129 public LauncherModel getModel() {
1130 return mModel;
1131 }
1132
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001133 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001134 getWindow().closeAllPanels();
1135
Winson Chung87acb482011-08-23 17:28:05 -07001136 /**
1137 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001138 try {
1139 dismissDialog(DIALOG_CREATE_SHORTCUT);
1140 // Unlock the workspace if the dialog was showing
1141 } catch (Exception e) {
1142 // An exception is thrown if the dialog is not visible, which is fine
1143 }
1144
1145 try {
1146 dismissDialog(DIALOG_RENAME_FOLDER);
1147 // Unlock the workspace if the dialog was showing
1148 } catch (Exception e) {
1149 // An exception is thrown if the dialog is not visible, which is fine
1150 }
Winson Chung87acb482011-08-23 17:28:05 -07001151 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001152
1153 // Whatever we were doing is hereby canceled.
1154 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001155 }
1156
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157 @Override
1158 protected void onNewIntent(Intent intent) {
1159 super.onNewIntent(intent);
1160
1161 // Close the menu
1162 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001163 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001164 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001165
Joe Onorato14f122b2009-11-19 14:06:36 -08001166 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1167 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001168
Adam Cohenac56cff2011-09-28 20:45:37 -07001169 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001170 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001171 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001172 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1173 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001174 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001175 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001176
1177 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001178 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001179 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001180
Joe Onorato3a8820b2009-11-10 15:06:42 -08001181 final View v = getWindow().peekDecorView();
1182 if (v != null && v.getWindowToken() != null) {
1183 InputMethodManager imm = (InputMethodManager)getSystemService(
1184 INPUT_METHOD_SERVICE);
1185 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001186 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001187
Michael Jurka35aa14d2011-07-07 17:01:08 -07001188 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001189 if (mAppsCustomizeTabHost != null) {
1190 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 }
1193 }
1194
1195 @Override
1196 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1197 // Do not call super here
1198 mSavedInstanceState = savedInstanceState;
1199 }
1200
1201 @Override
1202 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001203 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001204 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205
Michael Jurka883f55b2010-10-21 15:47:14 -07001206 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001207 // We close any open folder since it will not be re-opened, and we need to make sure
1208 // this state is reflected.
1209 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210
Winson Chung3d503fb2011-07-13 17:25:49 -07001211 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1212 mWaitingForResult) {
1213 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1214 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1215 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1216 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001217 }
1218
1219 if (mFolderInfo != null && mWaitingForResult) {
1220 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1221 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1222 }
Michael Jurka946ad472010-07-09 18:05:18 -07001223
Winson Chung785d2eb2011-04-14 16:08:02 -07001224 // Save the current AppsCustomize tab
1225 if (mAppsCustomizeTabHost != null) {
1226 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1227 if (currentTabTag != null) {
1228 outState.putString("apps_customize_currentTab", currentTabTag);
1229 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001230 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1231 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001232 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001233 }
1234
1235 @Override
1236 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001238
Winson Chunge7a03942011-08-05 15:05:12 -07001239 // Remove all pending runnables
1240 mHandler.removeMessages(ADVANCE_MSG);
1241 mHandler.removeMessages(0);
1242
Winson Chungcd2b0142011-06-08 16:02:26 -07001243 // Stop callbacks from LauncherModel
1244 LauncherApplication app = ((LauncherApplication) getApplication());
1245 mModel.stopLoader();
1246 app.setLauncher(null);
1247
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001249 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001251 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001252 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001253 mAppWidgetHost = null;
1254
1255 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256
1257 TextKeyListener.getInstance().release();
1258
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259
Winson Chung3d503fb2011-07-13 17:25:49 -07001260 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001261
1262 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001263 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001264
1265 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1266 mWorkspace.removeAllViews();
1267 mWorkspace = null;
1268 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001269
1270 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 }
1272
Adam Cohena9cf38f2011-05-02 15:36:58 -07001273 public DragController getDragController() {
1274 return mDragController;
1275 }
1276
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 @Override
1278 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001279 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 super.startActivityForResult(intent, requestCode);
1281 }
1282
Winson Chung70d72102011-08-12 11:24:35 -07001283 /**
1284 * Indicates that we want global search for this activity by setting the globalSearch
1285 * argument for {@link #startSearch} to true.
1286 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001288 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001290
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001291 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001292
Karl Rosaen138a0412009-04-23 19:00:21 -07001293 if (initialQuery == null) {
1294 // Use any text typed in the launcher as the initial query
1295 initialQuery = getTypedText();
1296 clearTypedText();
1297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 if (appSearchData == null) {
1299 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001300 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 }
Romain Guycbb89e42009-06-08 15:52:54 -07001302
Karl Rosaen138a0412009-04-23 19:00:21 -07001303 final SearchManager searchManager =
1304 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001305 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001306 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
1308
Winson Chung70d72102011-08-12 11:24:35 -07001309 @Override
1310 public boolean onCreateOptionsMenu(Menu menu) {
1311 if (isWorkspaceLocked()) {
1312 return false;
1313 }
1314
1315 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001316
1317 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1318 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1319 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001320 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1321 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1322 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001323 String helpUrl = getString(R.string.help_url);
1324 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001325 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1326 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1327
Winson Chung70d72102011-08-12 11:24:35 -07001328 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1329 .setIcon(android.R.drawable.ic_menu_gallery)
1330 .setAlphabeticShortcut('W');
1331 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1332 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001333 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001334 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001335 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1336 .setIcon(android.R.drawable.ic_menu_preferences)
1337 .setIntent(settings)
1338 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001339 if (!helpUrl.isEmpty()) {
1340 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1341 .setIcon(android.R.drawable.ic_menu_help)
1342 .setIntent(help)
1343 .setAlphabeticShortcut('H');
1344 }
Winson Chung70d72102011-08-12 11:24:35 -07001345 return true;
1346 }
1347
1348 @Override
1349 public boolean onPrepareOptionsMenu(Menu menu) {
1350 super.onPrepareOptionsMenu(menu);
1351
1352 if (mAppsCustomizeTabHost.isTransitioning()) {
1353 return false;
1354 }
1355 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1356 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1357
1358 return true;
1359 }
1360
1361 @Override
1362 public boolean onOptionsItemSelected(MenuItem item) {
1363 switch (item.getItemId()) {
1364 case MENU_WALLPAPER_SETTINGS:
1365 startWallpaper();
1366 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001367 }
1368
1369 return super.onOptionsItemSelected(item);
1370 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001372 @Override
1373 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001374 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001375 // Use a custom animation for launching search
1376 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001377 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001378 }
1379
Joe Onorato9c1289c2009-08-17 11:03:03 -04001380 public boolean isWorkspaceLocked() {
1381 return mWorkspaceLoading || mWaitingForResult;
1382 }
1383
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001385 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001386 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 }
1388
Michael Jurka0280c3b2010-09-17 15:00:07 -07001389 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001390 mPendingAddInfo.container = ItemInfo.NO_ID;
1391 mPendingAddInfo.screen = -1;
1392 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1393 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1394 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001395 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001396
Michael Jurkaaf442092010-06-10 17:01:57 -07001397 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001398 // TODO: catch bad widget exception when sent
1399 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001400 // TODO: Is this log message meaningful?
1401 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001402 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001403 }
1404
Winson Chung55cef262010-10-28 14:14:18 -07001405 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001406 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407
Bjorn Bringert7984c942009-12-09 15:38:25 +00001408 if (appWidget.configure != null) {
1409 // Launch over to configure widget, if needed
1410 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1411 intent.setComponent(appWidget.configure);
1412 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001413 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001414 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1415 intent.putExtra(
1416 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1417 info.mimeType);
1418
1419 final String mimeType = info.mimeType;
1420 final ClipData clipData = (ClipData) info.configurationData;
1421 final ClipDescription clipDesc = clipData.getDescription();
1422 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1423 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001424 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001425 final CharSequence stringData = item.getText();
1426 final Uri uriData = item.getUri();
1427 final Intent intentData = item.getIntent();
1428 final String key =
1429 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1430 if (uriData != null) {
1431 intent.putExtra(key, uriData);
1432 } else if (intentData != null) {
1433 intent.putExtra(key, intentData);
1434 } else if (stringData != null) {
1435 intent.putExtra(key, stringData);
1436 }
1437 break;
1438 }
1439 }
1440 }
Winson Chung55cef262010-10-28 14:14:18 -07001441 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001442
Romain Guy8e633c52010-03-04 12:51:36 -08001443 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001445 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001446 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001447
1448 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001449 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 }
1451 }
Romain Guycbb89e42009-06-08 15:52:54 -07001452
Adam Cohenfbba09b2011-07-18 21:43:05 -07001453 /**
1454 * Process a shortcut drop.
1455 *
1456 * @param componentName The name of the component
1457 * @param screen The screen where it should be added
1458 * @param cell The cell it should be added to, optional
1459 * @param position The location on the screen where it was dropped, optional
1460 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001461 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1462 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001463 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001464 mPendingAddInfo.container = container;
1465 mPendingAddInfo.screen = screen;
1466 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001467
1468 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001469 mPendingAddInfo.cellX = cell[0];
1470 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001471 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001472
1473 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1474 createShortcutIntent.setComponent(componentName);
1475 processShortcut(createShortcutIntent);
1476 }
1477
Adam Cohenfbba09b2011-07-18 21:43:05 -07001478 /**
1479 * Process a widget drop.
1480 *
1481 * @param info The PendingAppWidgetInfo of the widget being added.
1482 * @param screen The screen where it should be added
1483 * @param cell The cell it should be added to, optional
1484 * @param position The location on the screen where it was dropped, optional
1485 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001486 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1487 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001488 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001489 mPendingAddInfo.container = info.container = container;
1490 mPendingAddInfo.screen = info.screen = screen;
1491 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001492 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001493 mPendingAddInfo.cellX = cell[0];
1494 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001495 }
1496
1497 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1498 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1499 addAppWidgetImpl(appWidgetId, info);
1500 }
1501
Joe Onoratodeb98af2010-02-19 14:59:39 -08001502 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001503 // Handle case where user selected "Applications"
1504 String applicationName = getResources().getString(R.string.group_applications);
1505 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001506
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001507 if (applicationName != null && applicationName.equals(shortcutName)) {
1508 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1509 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001510
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001511 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1512 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001513 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001514 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001515 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001516 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001517 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 }
1519
Winson Chung24ab2f12010-09-16 14:10:47 -07001520 void processWallpaper(Intent intent) {
1521 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1522 }
1523
Winson Chung3d503fb2011-07-13 17:25:49 -07001524 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1525 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001526 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 folderInfo.title = getText(R.string.folder_name);
1528
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001530 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1531 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001532 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533
1534 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001535 FolderIcon newFolder =
1536 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1537 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1538 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001539 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
Romain Guycbb89e42009-06-08 15:52:54 -07001541
Joe Onorato9c1289c2009-08-17 11:03:03 -04001542 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001543 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001544 }
1545
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001546 private void showNotifications() {
1547 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1548 if (statusBar != null) {
1549 statusBar.expand();
1550 }
1551 }
1552
1553 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001554 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001555 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001556 Intent chooser = Intent.createChooser(pickWallpaper,
1557 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001558 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1559 // Removed in Eclair MR1
1560// WallpaperManager wm = (WallpaperManager)
1561// getSystemService(Context.WALLPAPER_SERVICE);
1562// WallpaperInfo wi = wm.getWallpaperInfo();
1563// if (wi != null && wi.getSettingsActivity() != null) {
1564// LabeledIntent li = new LabeledIntent(getPackageName(),
1565// R.string.configure_wallpaper, 0);
1566// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1567// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1568// }
Mike Clerona0618e42009-10-22 13:55:21 -07001569 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 }
1571
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001572 /**
1573 * Registers various content observers. The current implementation registers
1574 * only a favorites observer to keep track of the favorites applications.
1575 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001576 private void registerContentObservers() {
1577 ContentResolver resolver = getContentResolver();
1578 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1579 true, mWidgetObserver);
1580 }
1581
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 @Override
1583 public boolean dispatchKeyEvent(KeyEvent event) {
1584 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1585 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001586 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001587 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001588 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001589 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001590 dumpState();
1591 return true;
1592 }
1593 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001594 }
1595 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1596 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001597 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 return true;
1599 }
1600 }
1601
1602 return super.dispatchKeyEvent(event);
1603 }
1604
Joe Onorato88ec0992009-11-19 13:16:06 -08001605 @Override
1606 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001607 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001608 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001609 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001610 Folder openFolder = mWorkspace.getOpenFolder();
1611 if (openFolder.isEditingName()) {
1612 openFolder.dismissEditingName();
1613 } else {
1614 closeFolder();
1615 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001616 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001617 mWorkspace.exitWidgetResizeMode();
1618
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001619 // Back button is a no-op here, but give at least some feedback for the button press
1620 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001621 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001622 }
1623
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001625 * Re-listen when widgets are reset.
1626 */
1627 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001628 if (mAppWidgetHost != null) {
1629 mAppWidgetHost.startListening();
1630 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001631 }
1632
1633 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001634 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1635 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001637 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001638 if (mModel != null) {
1639 mModel.unbindWorkspaceItems();
1640 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001641 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001642
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 /**
1644 * Launches the intent referred by the clicked shortcut.
1645 *
1646 * @param v The view representing the clicked shortcut.
1647 */
1648 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001649 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1650 // view has detached (it's possible for this to happen if the view is removed mid touch).
1651 if (v.getWindowToken() == null) {
1652 return;
1653 }
1654
1655 if (mWorkspace.isSwitchingState()) {
1656 return;
1657 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001658
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001660 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001662 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001663 int[] pos = new int[2];
1664 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001665 intent.setSourceBounds(new Rect(pos[0], pos[1],
1666 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001667 boolean success = startActivitySafely(intent, tag);
1668
1669 if (success && v instanceof BubbleTextView) {
1670 mWaitingForResume = (BubbleTextView) v;
1671 mWaitingForResume.setStayPressed(true);
1672 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001674 if (v instanceof FolderIcon) {
1675 FolderIcon fi = (FolderIcon) v;
1676 handleFolderClick(fi);
1677 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001678 } else if (v == mAllAppsButton) {
1679 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001680 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001681 } else {
Joe Onorato3a8820b2009-11-10 15:06:42 -08001682 showAllApps(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 }
1685 }
1686
Michael Jurka0e260592010-06-30 17:07:39 -07001687 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001688 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001689 // clicking anywhere on the workspace causes the customization drawer to slide down
1690 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001691 return false;
1692 }
1693
Michael Jurkaaf442092010-06-10 17:01:57 -07001694 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001695 * Event handler for the search button
1696 *
1697 * @param v The view that was clicked.
1698 */
1699 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001700 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001701 }
1702
1703 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001704 * Event handler for the voice button
1705 *
1706 * @param v The view that was clicked.
1707 */
1708 public void onClickVoiceButton(View v) {
1709 startVoiceSearch();
1710 }
1711
1712 private void startVoiceSearch() {
1713 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001714 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001715 startActivity(intent);
1716 }
1717
1718 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001719 * Event handler for the "grid" button that appears on the home screen, which
1720 * enters all apps mode.
1721 *
1722 * @param v The view that was clicked.
1723 */
1724 public void onClickAllAppsButton(View v) {
1725 showAllApps(true);
1726 }
1727
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001728 public void onClickAppMarketButton(View v) {
1729 if (mAppMarketIntent != null) {
1730 startActivitySafely(mAppMarketIntent, "app market");
1731 }
1732 }
1733
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001734 void startApplicationDetailsActivity(ComponentName componentName) {
1735 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001736 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1737 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001738 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001739 startActivity(intent);
1740 }
1741
Patrick Dubroy5539af72010-09-07 15:22:01 -07001742 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1743 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1744 // System applications cannot be installed. For now, show a toast explaining that.
1745 // We may give them the option of disabling apps this way.
1746 int messageId = R.string.uninstall_system_app_text;
1747 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1748 } else {
1749 String packageName = appInfo.componentName.getPackageName();
1750 String className = appInfo.componentName.getClassName();
1751 Intent intent = new Intent(
1752 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001753 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1754 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001755 startActivity(intent);
1756 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001757 }
1758
Michael Jurkaddd62e92011-02-16 17:49:14 -08001759 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001760 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1761 try {
1762 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001763 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 } catch (ActivityNotFoundException e) {
1765 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001766 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 } catch (SecurityException e) {
1768 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001769 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001771 "or use the exported attribute for this activity. "
1772 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001773 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001774 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001776
Romain Guy8e633c52010-03-04 12:51:36 -08001777 void startActivityForResultSafely(Intent intent, int requestCode) {
1778 try {
1779 startActivityForResult(intent, requestCode);
1780 } catch (ActivityNotFoundException e) {
1781 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1782 } catch (SecurityException e) {
1783 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1784 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1785 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1786 "or use the exported attribute for this activity.", e);
1787 }
1788 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789
Adam Cohena9cf38f2011-05-02 15:36:58 -07001790 private void handleFolderClick(FolderIcon folderIcon) {
1791 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001792 Folder openFolder = mWorkspace.getFolderForTag(info);
1793
1794 // If the folder info reports that the associated folder is open, then verify that
1795 // it is actually opened. There have been a few instances where this gets out of sync.
1796 if (info.opened && openFolder == null) {
1797 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1798 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1799 info.opened = false;
1800 }
1801
Adam Cohena9cf38f2011-05-02 15:36:58 -07001802 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 // Close any open folder
1804 closeFolder();
1805 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001806 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807 } else {
1808 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 int folderScreen;
1810 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001811 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001812 // .. and close it
1813 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001814 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 // Close any folder open on the current screen
1816 closeFolder();
1817 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001818 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 }
1820 }
1821 }
1822 }
1823
Adam Cohen2801caf2011-05-13 20:57:39 -07001824 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001825 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001826 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1827 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1828 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1829
Adam Cohenc51934b2011-07-26 21:07:43 -07001830 FolderInfo info = (FolderInfo) fi.getTag();
1831 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1832 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001833 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1834 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001835 }
1836
Adam Cohen2801caf2011-05-13 20:57:39 -07001837 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1838 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1839 oa.start();
1840 }
1841
1842 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001843 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001844 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1845 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1846 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1847
Adam Cohenc51934b2011-07-26 21:07:43 -07001848 FolderInfo info = (FolderInfo) fi.getTag();
1849 CellLayout cl = null;
1850 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1851 cl = (CellLayout) fi.getParent().getParent();
1852 }
1853
1854 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001855 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1856 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001857 oa.addListener(new AnimatorListenerAdapter() {
1858 @Override
1859 public void onAnimationEnd(Animator animation) {
1860 if (layout != null) {
1861 layout.clearFolderLeaveBehind();
1862 }
1863 }
1864 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001865 oa.start();
1866 }
1867
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001868 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001869 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 * is animated relative to the specified View. If the View is null, no animation
1871 * is played.
1872 *
1873 * @param folderInfo The FolderInfo describing the folder to open.
1874 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001875 public void openFolder(FolderIcon folderIcon) {
1876 Folder folder = folderIcon.mFolder;
1877 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878
Adam Cohen2801caf2011-05-13 20:57:39 -07001879 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001880 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881
Adam Cohen4554ee12011-08-03 16:13:21 -07001882 // Just verify that the folder hasn't already been added to the DragLayer.
1883 // There was a one-off crash where the folder had a parent already.
1884 if (folder.getParent() == null) {
1885 mDragLayer.addView(folder);
1886 mDragController.addDropTarget((DropTarget) folder);
1887 } else {
1888 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1889 folder.getParent() + ").");
1890 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001891 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001892 }
1893
1894 public void closeFolder() {
1895 Folder folder = mWorkspace.getOpenFolder();
1896 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07001897 if (folder.isEditingName()) {
1898 folder.dismissEditingName();
1899 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001900 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07001901
1902 // Dismiss the folder cling
1903 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07001904 }
1905 }
1906
1907 void closeFolder(Folder folder) {
1908 folder.getInfo().opened = false;
1909
1910 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1911 if (parent != null) {
1912 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1913 shrinkAndFadeInFolderIcon(fi);
1914 }
1915 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 }
1917
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001919 if (mState != State.WORKSPACE) {
1920 return false;
1921 }
1922
Joe Onorato9c1289c2009-08-17 11:03:03 -04001923 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001924 return false;
1925 }
1926
1927 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001928 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 }
1930
Michael Jurka0280c3b2010-09-17 15:00:07 -07001931 resetAddInfo();
1932 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001934 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 return true;
1936 }
1937
Winson Chung3d503fb2011-07-13 17:25:49 -07001938 // The hotseat touch handling does not go through Workspace, and we always allow long press
1939 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001940 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001941 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1942 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001943 if (itemUnderLongClick == null) {
1944 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001945 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1946 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001947 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001949 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001951 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 }
1953 }
1954 }
1955 return true;
1956 }
1957
Winson Chung3d503fb2011-07-13 17:25:49 -07001958 boolean isHotseatLayout(View layout) {
1959 return mHotseat != null && layout != null &&
1960 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1961 }
1962 Hotseat getHotseat() {
1963 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001964 }
1965
Winson Chung3d503fb2011-07-13 17:25:49 -07001966 /**
1967 * Returns the CellLayout of the specified container at the specified screen.
1968 */
1969 CellLayout getCellLayout(long container, int screen) {
1970 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1971 if (mHotseat != null) {
1972 return mHotseat.getLayout();
1973 } else {
1974 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001975 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001976 } else {
1977 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001978 }
1979 }
1980
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 Workspace getWorkspace() {
1982 return mWorkspace;
1983 }
1984
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 @Override
1986 protected Dialog onCreateDialog(int id) {
1987 switch (id) {
1988 case DIALOG_CREATE_SHORTCUT:
1989 return new CreateShortcut().createDialog();
1990 case DIALOG_RENAME_FOLDER:
1991 return new RenameFolder().createDialog();
1992 }
1993
1994 return super.onCreateDialog(id);
1995 }
1996
1997 @Override
1998 protected void onPrepareDialog(int id, Dialog dialog) {
1999 switch (id) {
2000 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 break;
2002 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002003 if (mFolderInfo != null) {
2004 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2005 final CharSequence text = mFolderInfo.title;
2006 input.setText(text);
2007 input.setSelection(0, text.length());
2008 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 break;
2010 }
2011 }
2012
2013 void showRenameDialog(FolderInfo info) {
2014 mFolderInfo = info;
2015 mWaitingForResult = true;
2016 showDialog(DIALOG_RENAME_FOLDER);
2017 }
2018
Adam Cohenfbba09b2011-07-18 21:43:05 -07002019 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002020 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002021 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2022 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 mWaitingForResult = true;
2024 showDialog(DIALOG_CREATE_SHORTCUT);
2025 }
2026
2027 private class RenameFolder {
2028 private EditText mInput;
2029
2030 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2032 mInput = (EditText) layout.findViewById(R.id.folder_name);
2033
2034 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2035 builder.setIcon(0);
2036 builder.setTitle(getString(R.string.rename_folder_title));
2037 builder.setCancelable(true);
2038 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2039 public void onCancel(DialogInterface dialog) {
2040 cleanup();
2041 }
2042 });
2043 builder.setNegativeButton(getString(R.string.cancel_action),
2044 new Dialog.OnClickListener() {
2045 public void onClick(DialogInterface dialog, int which) {
2046 cleanup();
2047 }
2048 }
2049 );
2050 builder.setPositiveButton(getString(R.string.rename_action),
2051 new Dialog.OnClickListener() {
2052 public void onClick(DialogInterface dialog, int which) {
2053 changeFolderName();
2054 }
2055 }
2056 );
2057 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002058
2059 final AlertDialog dialog = builder.create();
2060 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2061 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002062 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002063 mInput.requestFocus();
2064 InputMethodManager inputManager = (InputMethodManager)
2065 getSystemService(Context.INPUT_METHOD_SERVICE);
2066 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002067 }
2068 });
2069
2070 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 }
2072
2073 private void changeFolderName() {
2074 final String name = mInput.getText().toString();
2075 if (!TextUtils.isEmpty(name)) {
2076 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002077 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 mFolderInfo.title = name;
2079 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2080
Joe Onorato9c1289c2009-08-17 11:03:03 -04002081 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002082 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002083 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 } else {
2085 final FolderIcon folderIcon = (FolderIcon)
2086 mWorkspace.getViewForTag(mFolderInfo);
2087 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002088 // TODO: At some point we'll probably want some version of setting
2089 // the text for a folder icon.
2090 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 getWorkspace().requestLayout();
2092 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002093 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002094 mWorkspaceLoading = true;
2095 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 }
2097 }
2098 }
2099 cleanup();
2100 }
2101
2102 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 dismissDialog(DIALOG_RENAME_FOLDER);
2104 mWaitingForResult = false;
2105 mFolderInfo = null;
2106 }
2107 }
2108
Daniel Sandler843e8602010-06-07 14:59:01 -04002109 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2110 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002111 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002112 }
2113
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002114 // AllAppsView.Watcher
2115 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002116 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002117 mWorkspace.setVisibility(View.GONE);
2118 }
2119 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002120
2121 /**
2122 * Helper method for the cameraZoomIn/cameraZoomOut animations
2123 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002124 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002125 * @param scaleFactor The scale factor used for the zoom
2126 */
2127 private void setPivotsForZoom(View view, State state, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002128 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002129 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002130 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002131
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002132 void updateWallpaperVisibility(boolean visible) {
2133 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2134 int curflags = getWindow().getAttributes().flags
2135 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2136 if (wpflags != curflags) {
2137 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2138 }
2139 }
2140
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002141 /**
2142 * Zoom the camera out from the workspace to reveal 'toView'.
2143 * Assumes that the view to show is anchored at either the very top or very bottom
2144 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002145 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002146 */
Winson Chungc07918d2011-07-01 15:35:26 -07002147 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002148 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002149 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002150
Winson Chungf0ea4d32011-06-06 14:27:16 -07002151 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2152 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2153 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2154 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002155 final int startDelay =
2156 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002157
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002158 setPivotsForZoom(toView, toState, scale);
2159
Michael Jurkad74c9842011-07-10 12:44:21 -07002160 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002161 mWorkspace.changeState(Workspace.State.SMALL, animated);
Adam Cohencff6af82011-09-13 14:51:53 -07002162 //hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002163
2164 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002165 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002166 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002167 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2168 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002169 ((View) toView.getParent()).invalidate();
2170 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002171 toView.setFastScaleX(a * scale + b * 1f);
2172 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002173 }
2174 });
Adam Cohen61033d32010-11-15 18:29:44 -08002175
Winson Chungf0ea4d32011-06-06 14:27:16 -07002176 toView.setVisibility(View.VISIBLE);
2177 toView.setFastAlpha(0f);
2178 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2179 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2180 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2181 public void onAnimationUpdate(float a, float b) {
2182 // don't need to invalidate because we do so above
2183 toView.setFastAlpha(a * 0f + b * 1f);
2184 }
2185 });
Adam Cohencff6af82011-09-13 14:51:53 -07002186 alphaAnim.setStartDelay(startDelay);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002187 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002188
Michael Jurkaabded662011-03-04 12:06:57 -08002189 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002190 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, scaleAnim,
2191 false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002192 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002193 scaleAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002194 boolean animationCancelled = false;
2195
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002196 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002197 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002198 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002199 // Prepare the position
2200 toView.setTranslationX(0.0f);
2201 toView.setTranslationY(0.0f);
2202 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002203 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002204 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002205 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002206 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002207 // If we don't set the final scale values here, if this animation is cancelled
2208 // it will have the wrong scale value and subsequent cameraPan animations will
2209 // not fix that
2210 toView.setScaleX(1.0f);
2211 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002212 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002213 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance,
2214 scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002215 }
Winson Chung32174c82011-07-19 15:47:55 -07002216
2217 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2218 // Hide the workspace scrollbar
2219 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002220 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002221 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002222 if (!animationCancelled) {
2223 updateWallpaperVisibility(false);
2224 }
2225 }
2226
Adam Cohencff6af82011-09-13 14:51:53 -07002227 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002228 public void onAnimationCancel(Animator animation) {
2229 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002230 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002231 });
2232
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002233 // toView should appear right at the end of the workspace shrink animation
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002234
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002235 if (mStateAnimation != null) mStateAnimation.cancel();
2236 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002237 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002238 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002239 } else {
2240 toView.setTranslationX(0.0f);
2241 toView.setTranslationY(0.0f);
2242 toView.setScaleX(1.0f);
2243 toView.setScaleY(1.0f);
2244 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002245 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002246 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002247 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, null, false);
2248 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance, null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002249
Winson Chungc07918d2011-07-01 15:35:26 -07002250 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2251 // Hide the workspace scrollbar
2252 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002253 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002254 }
Michael Jurkaabded662011-03-04 12:06:57 -08002255 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002256 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002257 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002258 }
2259
2260 /**
2261 * Zoom the camera back into the workspace, hiding 'fromView'.
2262 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002263 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002264 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002265 */
Winson Chungc07918d2011-07-01 15:35:26 -07002266 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002267 Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002268 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002269
Winson Chungf0ea4d32011-06-06 14:27:16 -07002270 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2271 final float scaleFactor = (float)
2272 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2273 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002274
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002275 setPivotsForZoom(fromView, fromState, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002276 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002277 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002278 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002279 if (mStateAnimation != null) mStateAnimation.cancel();
2280 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002281
Michael Jurka742574b2011-02-02 23:51:01 -08002282 final float oldScaleX = fromView.getScaleX();
2283 final float oldScaleY = fromView.getScaleY();
2284
2285 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2286 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002287 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2288 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002289 ((View)fromView.getParent()).fastInvalidate();
2290 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2291 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2292 }
2293 });
Michael Jurkaabded662011-03-04 12:06:57 -08002294 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002295 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Adam Cohencff6af82011-09-13 14:51:53 -07002296 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002297 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2298 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002299 // don't need to invalidate because we do so above
2300 fromView.setFastAlpha(a * 1f + b * 0f);
2301 }
2302 });
Michael Jurkaabded662011-03-04 12:06:57 -08002303 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002304 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, alphaAnim,
2305 true);
Michael Jurka2763be32011-02-24 11:19:57 -08002306 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002307 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002308 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002309 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002310 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002311 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002312 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002313 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance,
2314 alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002315 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002316 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002317 }
2318 });
2319
Winson Chungf0ea4d32011-06-06 14:27:16 -07002320 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002321 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002322 } else {
2323 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002324 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002325 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, null, true);
2326 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance, null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002327 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002328 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002329 }
2330
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002331 void showWorkspace(boolean animated) {
Adam Cohencff6af82011-09-13 14:51:53 -07002332 Resources res = getResources();
2333 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2334
2335 mWorkspace.changeState(Workspace.State.NORMAL, animated, stagger);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002336 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002337 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002338 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002339
Adam Cohen7777d962011-08-18 18:58:38 -07002340 mWorkspace.showDockDivider(!animated);
2341 mWorkspace.flashScrollingIndicator();
2342
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002343 // Change the state *after* we've called all the transition code
2344 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002345
Winson Chung5fb63472011-02-02 17:03:37 -08002346 // Resume the auto-advance of widgets
2347 mUserPresent = true;
2348 updateRunning();
2349
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002350 // send an accessibility event to announce the context change
2351 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002352 }
2353
Adam Cohen7777d962011-08-18 18:58:38 -07002354 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002355 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002356 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Winson Chungb26f3d62011-06-02 10:49:29 -07002357 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002358 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002359 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002360 }
Adam Cohen7777d962011-08-18 18:58:38 -07002361
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002362 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chung09bfc452011-09-09 11:30:20 -07002363 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2364
Winson Chunge7a03942011-08-05 15:05:12 -07002365 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002366 @Override
2367 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002368 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002369 // Before we show workspace, hide all apps again because
2370 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2371 // clean up our state transition functions
2372 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002373 mSearchDropTargetBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002374 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002375 } else {
2376 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002377 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002378 }
2379 }, (extendedDelay ?
2380 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2381 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2382 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002383 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002384 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002385 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2386 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002387 }
2388 // Otherwise, we are not in spring loaded mode, so don't do anything.
2389 }
2390
Adam Cohenfc53cd22011-07-20 15:45:11 -07002391 public boolean isAllAppsCustomizeOpen() {
2392 return mState == State.APPS_CUSTOMIZE;
2393 }
2394
Winson Chungf0ea4d32011-06-06 14:27:16 -07002395 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002396 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002397 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002398 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002399 if (!LauncherApplication.isScreenLarge()) {
2400 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002401 int duration = mSearchDropTargetBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002402 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002403 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002404 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002405 }
2406 }
2407 }
2408
2409 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002410 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002411 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002412 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002413 if (!LauncherApplication.isScreenLarge()) {
2414 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002415 int duration = mSearchDropTargetBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002416 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002417 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002418 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002419 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002420 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002421 }
2422
Winson Chung785d2eb2011-04-14 16:08:02 -07002423 void showAllApps(boolean animated) {
2424 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002425
Winson Chungf0ea4d32011-06-06 14:27:16 -07002426 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2427 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002428
Winson Chung3d503fb2011-07-13 17:25:49 -07002429 // Hide the search bar and hotseat
Winson Chungc51db6a2011-10-05 11:44:49 -07002430 mSearchDropTargetBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002431
Winson Chungf0ea4d32011-06-06 14:27:16 -07002432 // Change the state *after* we've called all the transition code
2433 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002434
2435 // Pause the auto-advance of widgets until we are out of AllApps
2436 mUserPresent = false;
2437 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002438 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002439
2440 // Send an accessibility event to announce the context change
2441 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2442 }
2443
Joe Onoratob2061212009-11-24 16:13:54 -05002444 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002445 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002446 * - Home from workspace
2447 * - from center screen
2448 * - from other screens
2449 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002450 * - from center screen
2451 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002452 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002453 * - from center screen
2454 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002455 * - Launch app from workspace and quit
2456 * - with back
2457 * - with home
2458 * - Launch app from all apps and quit
2459 * - with back
2460 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002461 * - Go to a screen that's not the default, then all
2462 * apps, and launch and app, and go back
2463 * - with back
2464 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002465 * - On workspace, long press power and go back
2466 * - with back
2467 * - with home
2468 * - On all apps, long press power and go back
2469 * - with back
2470 * - with home
2471 * - On workspace, power off
2472 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002473 * - Launch an app and turn off the screen while in that app
2474 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002475 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002476 * - From all apps
2477 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002478 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2479 * - From all apps
2480 * - From the center workspace
2481 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002482 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002483 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002484 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2485 mWorkspace.setVisibility(View.VISIBLE);
2486 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002487
Winson Chung3d503fb2011-07-13 17:25:49 -07002488 // Show the search bar and hotseat
Winson Chungc51db6a2011-10-05 11:44:49 -07002489 mSearchDropTargetBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002490
Winson Chungf0ea4d32011-06-06 14:27:16 -07002491 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002492 if (mAllAppsButton != null) {
2493 mAllAppsButton.requestFocus();
2494 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002495 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002496 }
2497
Joe Onorato7c312c12009-08-13 21:36:53 -07002498 void lockAllApps() {
2499 // TODO
2500 }
2501
2502 void unlockAllApps() {
2503 // TODO
2504 }
2505
Patrick Dubroy5f445422011-02-18 14:35:21 -08002506 /**
2507 * Add an item from all apps or customize onto the given workspace screen.
2508 * If layout is null, add to the current screen.
2509 */
2510 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002511 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2512 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002513 } else {
2514 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002515 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002516 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002517
Winson Chungdff8ebb2011-09-08 17:25:31 -07002518 /** Maps the current orientation to an index for referencing orientation correct global icons */
2519 private int getCurrentOrientationIndexForGlobalIcons() {
2520 // default - 0, landscape - 1
2521 switch (getResources().getConfiguration().orientation) {
2522 case Configuration.ORIENTATION_LANDSCAPE:
2523 return 1;
2524 default:
2525 return 0;
2526 }
2527 }
2528
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002529 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002530 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002531 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002532 // Look for the toolbar icon specified in the activity meta-data
2533 Bundle metaData = packageManager.getActivityInfo(
2534 activityName, PackageManager.GET_META_DATA).metaData;
2535 if (metaData != null) {
2536 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2537 if (iconResId != 0) {
2538 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002539 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002540 }
2541 }
2542 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002543 // This can happen if the activity defines an invalid drawable
2544 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2545 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002546 } catch (Resources.NotFoundException nfe) {
2547 // This can happen if the activity defines an invalid drawable
2548 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2549 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002550 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002551 return null;
2552 }
2553
2554 // if successful in getting icon, return it; otherwise, set button to use default drawable
2555 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2556 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2557 TextView button = (TextView) findViewById(buttonId);
2558 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002559 Resources r = getResources();
2560 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2561 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002562
2563 // If we were unable to find the icon via the meta-data, use a generic one
2564 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002565 toolbarIcon = r.getDrawable(fallbackDrawableId);
2566 toolbarIcon.setBounds(0, 0, w, h);
2567 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002568 return null;
2569 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002570 toolbarIcon.setBounds(0, 0, w, h);
2571 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002572 return toolbarIcon.getConstantState();
2573 }
2574 }
2575
2576 // if successful in getting icon, return it; otherwise, set button to use default drawable
2577 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2578 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2579 ImageView button = (ImageView) findViewById(buttonId);
2580 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2581
Michael Jurka19e0fc52011-07-22 18:00:21 -07002582 if (button != null) {
2583 // If we were unable to find the icon via the meta-data, use a
2584 // generic one
2585 if (toolbarIcon == null) {
2586 button.setImageResource(fallbackDrawableId);
2587 } else {
2588 button.setImageDrawable(toolbarIcon);
2589 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002590 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002591
2592 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2593
Michael Jurka0423dcf2010-10-05 14:56:18 -07002594 }
2595
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002596 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2597 TextView button = (TextView) findViewById(buttonId);
2598 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2599 }
2600
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002601 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002602 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002603 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002604 }
2605
Winson Chungc51db6a2011-10-05 11:44:49 -07002606 private boolean updateGlobalSearchIcon() {
2607 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002608 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2609 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002610 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002611 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002612
Winson Chung1cad91e2011-05-25 17:41:01 -07002613 final SearchManager searchManager =
2614 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2615 ComponentName activityName = searchManager.getGlobalSearchActivity();
2616 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002617 int coi = getCurrentOrientationIndexForGlobalIcons();
2618 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002619 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002620 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002621 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2622 searchButton.setVisibility(View.VISIBLE);
2623 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002624 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002625 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002626 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002627 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2628 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2629 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002630 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002631 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002632 }
2633 }
2634
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002635 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002636 updateButtonWithDrawable(R.id.search_button, d);
2637 }
2638
Winson Chungc51db6a2011-10-05 11:44:49 -07002639 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002640 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002641 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002642 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002643
Winson Chungc51db6a2011-10-05 11:44:49 -07002644 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002645 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2646 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002647 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002648 int coi = getCurrentOrientationIndexForGlobalIcons();
2649 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002650 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002651 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002652 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002653 voiceButton.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002654 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002655 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002656 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002657 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002658 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002659 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002660 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002661 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002662
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002663 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002664 updateButtonWithDrawable(R.id.voice_button, d);
2665 }
2666
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002667 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002668 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002669 */
2670 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002671 final View marketButton = findViewById(R.id.market_button);
2672 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2673 // Find the app market activity by resolving an intent.
2674 // (If multiple app markets are installed, it will return the ResolverActivity.)
2675 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002676 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002677 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002678 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002679 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002680 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002681 marketButton.setVisibility(View.VISIBLE);
2682 } else {
2683 // We should hide and disable the view so that we don't try and restore the visibility
2684 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2685 marketButton.setVisibility(View.GONE);
2686 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002687 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002688 }
2689
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002690 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002691 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002692 }
2693
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002694 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002695 * Displays the shortcut creation dialog and launches, if necessary, the
2696 * appropriate activity.
2697 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002698 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002699 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2700 DialogInterface.OnShowListener {
2701
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002702 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002703
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002704 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002705 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002706
Winson Chung55b65502011-05-26 12:03:43 -07002707 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2708 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002709 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002710
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002711 AlertDialog dialog = builder.create();
2712 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002713 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002714 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002715
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002716 return dialog;
2717 }
2718
2719 public void onCancel(DialogInterface dialog) {
2720 mWaitingForResult = false;
2721 cleanup();
2722 }
2723
Romain Guycbb89e42009-06-08 15:52:54 -07002724 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002725 mWaitingForResult = false;
2726 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002727 }
2728
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002729 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002730 try {
2731 dismissDialog(DIALOG_CREATE_SHORTCUT);
2732 } catch (Exception e) {
2733 // An exception is thrown if the dialog is not visible, which is fine
2734 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002735 }
2736
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002737 /**
2738 * Handle the action clicked in the "Add to home" dialog.
2739 */
2740 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002741 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002742
Winson Chung55b65502011-05-26 12:03:43 -07002743 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2744 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002745 case AddAdapter.ITEM_APPLICATION: {
2746 if (mAppsCustomizeTabHost != null) {
2747 mAppsCustomizeTabHost.selectAppsTab();
2748 }
2749 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002750 break;
2751 }
Winson Chung55b65502011-05-26 12:03:43 -07002752 case AddAdapter.ITEM_APPWIDGET: {
2753 if (mAppsCustomizeTabHost != null) {
2754 mAppsCustomizeTabHost.selectWidgetsTab();
2755 }
2756 showAllApps(true);
2757 break;
2758 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002759 case AddAdapter.ITEM_WALLPAPER: {
2760 startWallpaper();
2761 break;
2762 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002763 }
2764 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002765
2766 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002767 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002768 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002769 }
2770
2771 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002772 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002773 */
2774 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2775 @Override
2776 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002777 closeSystemDialogs();
2778 }
2779 }
2780
2781 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002782 * Receives notifications whenever the appwidgets are reset.
2783 */
2784 private class AppWidgetResetObserver extends ContentObserver {
2785 public AppWidgetResetObserver() {
2786 super(new Handler());
2787 }
2788
2789 @Override
2790 public void onChange(boolean selfChange) {
2791 onAppWidgetReset();
2792 }
2793 }
2794
2795 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002796 * If the activity is currently paused, signal that we need to re-run the loader
2797 * in onResume.
2798 *
2799 * This needs to be called from incoming places where resources might have been loaded
2800 * while we are paused. That is becaues the Configuration might be wrong
2801 * when we're not running, and if it comes back to what it was when we
2802 * were paused, we are not restarted.
2803 *
2804 * Implementation of the method from LauncherModel.Callbacks.
2805 *
2806 * @return true if we are currently paused. The caller might be able to
2807 * skip some work in that case since we will come back again.
2808 */
2809 public boolean setLoadOnResume() {
2810 if (mPaused) {
2811 Log.i(TAG, "setLoadOnResume");
2812 mOnResumeNeedsLoad = true;
2813 return true;
2814 } else {
2815 return false;
2816 }
2817 }
2818
2819 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002820 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002821 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002822 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002823 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002824 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002825 } else {
2826 return SCREEN_COUNT / 2;
2827 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002828 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002829
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002830
Joe Onorato9c1289c2009-08-17 11:03:03 -04002831 /**
2832 * Refreshes the shortcuts shown on the workspace.
2833 *
2834 * Implementation of the method from LauncherModel.Callbacks.
2835 */
2836 public void startBinding() {
2837 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002838
2839 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002840 int count = workspace.getChildCount();
2841 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002842 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002843 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2844 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002845 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002846 if (mHotseat != null) {
2847 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002848 }
2849 }
2850
2851 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002852 * Bind the items start-end from the list.
2853 *
2854 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002855 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002856 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002857 setLoadOnResume();
2858
Joe Onorato9c1289c2009-08-17 11:03:03 -04002859 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002860 for (int i=start; i<end; i++) {
2861 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002862
2863 // Short circuit if we are loading dock items for a configuration which has no dock
2864 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2865 mHotseat == null) {
2866 continue;
2867 }
2868
Joe Onorato9c1289c2009-08-17 11:03:03 -04002869 switch (item.itemType) {
2870 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2871 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002872 View shortcut = createShortcut((ShortcutInfo)item);
2873 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2874 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002875 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002876 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002877 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002878 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002879 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002880 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2881 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002882 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002883 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002884 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002885 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002886 }
2887
Joe Onorato9c1289c2009-08-17 11:03:03 -04002888 /**
2889 * Implementation of the method from LauncherModel.Callbacks.
2890 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002891 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002892 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002893 sFolders.clear();
2894 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002895 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002896
2897 /**
2898 * Add the views for a widget to the workspace.
2899 *
2900 * Implementation of the method from LauncherModel.Callbacks.
2901 */
2902 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002903 setLoadOnResume();
2904
Daniel Sandler843e8602010-06-07 14:59:01 -04002905 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2906 if (DEBUG_WIDGETS) {
2907 Log.d(TAG, "bindAppWidget: " + item);
2908 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002909 final Workspace workspace = mWorkspace;
2910
2911 final int appWidgetId = item.appWidgetId;
2912 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002913 if (DEBUG_WIDGETS) {
2914 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2915 }
2916
Joe Onorato9c1289c2009-08-17 11:03:03 -04002917 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2918
Joe Onorato9c1289c2009-08-17 11:03:03 -04002919 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2920 item.hostView.setTag(item);
2921
Winson Chung3d503fb2011-07-13 17:25:49 -07002922 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002923 item.cellY, item.spanX, item.spanY, false);
2924
Adam Cohended9f8d2010-11-03 13:25:16 -07002925 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2926
Joe Onorato9c1289c2009-08-17 11:03:03 -04002927 workspace.requestLayout();
2928
Daniel Sandler843e8602010-06-07 14:59:01 -04002929 if (DEBUG_WIDGETS) {
2930 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2931 + (SystemClock.uptimeMillis()-start) + "ms");
2932 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002933 }
2934
2935 /**
2936 * Callback saying that there aren't any more items to bind.
2937 *
2938 * Implementation of the method from LauncherModel.Callbacks.
2939 */
2940 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002941 setLoadOnResume();
2942
Joe Onorato9c1289c2009-08-17 11:03:03 -04002943 if (mSavedState != null) {
2944 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002945 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002946 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002947 mSavedState = null;
2948 }
2949
2950 if (mSavedInstanceState != null) {
2951 super.onRestoreInstanceState(mSavedInstanceState);
2952 mSavedInstanceState = null;
2953 }
2954
Joe Onorato9c1289c2009-08-17 11:03:03 -04002955 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002956
2957 // If we received the result of any pending adds while the loader was running (e.g. the
2958 // widget configuration forced an orientation change), process them now.
2959 for (int i = 0; i < sPendingAddList.size(); i++) {
2960 completeAdd(sPendingAddList.get(i));
2961 }
2962 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002963
Winson Chungc51db6a2011-10-05 11:44:49 -07002964 // Update the market app icon as necessary (the other icons will be managed in response to
2965 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002966 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002967 }
2968
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002969 @Override
2970 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07002971 boolean searchVisible = updateGlobalSearchIcon();
2972 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
2973 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002974 }
2975
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002976 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002977 * Add the icons for all apps.
2978 *
2979 * Implementation of the method from LauncherModel.Callbacks.
2980 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002981 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2982 // Remove the progress bar entirely; we could also make it GONE
2983 // but better to remove it since we know it's not going to be used
2984 View progressBar = mAppsCustomizeTabHost.
2985 findViewById(R.id.apps_customize_progress_bar);
2986 if (progressBar != null) {
2987 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002988 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002989 // We just post the call to setApps so the user sees the progress bar
2990 // disappear-- otherwise, it just looks like the progress bar froze
2991 // which doesn't look great
2992 mAppsCustomizeTabHost.post(new Runnable() {
2993 public void run() {
2994 if (mAppsCustomizeContent != null) {
2995 mAppsCustomizeContent.setApps(apps);
2996 }
2997 }
2998 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04002999 }
3000
3001 /**
3002 * A package was installed.
3003 *
3004 * Implementation of the method from LauncherModel.Callbacks.
3005 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003006 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003007 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003008 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003009
Winson Chung785d2eb2011-04-14 16:08:02 -07003010 if (mAppsCustomizeContent != null) {
3011 mAppsCustomizeContent.addApps(apps);
3012 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003013 }
3014
3015 /**
3016 * A package was updated.
3017 *
3018 * Implementation of the method from LauncherModel.Callbacks.
3019 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003020 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003021 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003022 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003023 if (mWorkspace != null) {
3024 mWorkspace.updateShortcuts(apps);
3025 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003026
Winson Chung785d2eb2011-04-14 16:08:02 -07003027 if (mAppsCustomizeContent != null) {
3028 mAppsCustomizeContent.updateApps(apps);
3029 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003030 }
3031
3032 /**
3033 * A package was uninstalled.
3034 *
3035 * Implementation of the method from LauncherModel.Callbacks.
3036 */
Joe Onorato36115782010-06-17 13:28:48 -04003037 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003038 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003039 if (permanent) {
3040 mWorkspace.removeItems(apps);
3041 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003042
Winson Chung785d2eb2011-04-14 16:08:02 -07003043 if (mAppsCustomizeContent != null) {
3044 mAppsCustomizeContent.removeApps(apps);
3045 }
Winson Chunga1820962011-10-03 16:31:06 -07003046
3047 // Notify the drag controller
3048 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003049 }
Joe Onoratobe386092009-11-17 17:32:16 -08003050
3051 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003052 * A number of packages were updated.
3053 */
3054 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003055 if (mAppsCustomizeContent != null) {
3056 mAppsCustomizeContent.onPackagesUpdated();
3057 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003058 }
3059
Winson Chung400438b2011-01-16 17:53:48 -08003060 private int mapConfigurationOriActivityInfoOri(int configOri) {
3061 final Display d = getWindowManager().getDefaultDisplay();
3062 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3063 switch (d.getRotation()) {
3064 case Surface.ROTATION_0:
3065 case Surface.ROTATION_180:
3066 // We are currently in the same basic orientation as the natural orientation
3067 naturalOri = configOri;
3068 break;
3069 case Surface.ROTATION_90:
3070 case Surface.ROTATION_270:
3071 // We are currently in the other basic orientation to the natural orientation
3072 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3073 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3074 break;
3075 }
3076
3077 int[] oriMap = {
3078 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3079 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3080 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3081 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3082 };
3083 // Since the map starts at portrait, we need to offset if this device's natural orientation
3084 // is landscape.
3085 int indexOffset = 0;
3086 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3087 indexOffset = 1;
3088 }
3089 return oriMap[(d.getRotation() + indexOffset) % 4];
3090 }
Adam Cohen446e9402011-09-15 18:21:21 -07003091
3092 public void lockScreenOrientationOnLargeUI() {
3093 if (LauncherApplication.isScreenLarge()) {
3094 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3095 .getConfiguration().orientation));
3096 }
Winson Chung400438b2011-01-16 17:53:48 -08003097 }
Adam Cohen446e9402011-09-15 18:21:21 -07003098 public void unlockScreenOrientationOnLargeUI() {
3099 if (LauncherApplication.isScreenLarge()) {
3100 mHandler.postDelayed(new Runnable() {
3101 public void run() {
3102 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3103 }
3104 }, mRestoreScreenOrientationDelay);
3105 }
Winson Chung400438b2011-01-16 17:53:48 -08003106 }
3107
Winson Chung82f55532011-08-09 14:14:23 -07003108 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003109 private static final String PREFS_KEY = "com.android.launcher2.prefs";
3110 private boolean isClingsEnabled() {
Winson Chung82f55532011-08-09 14:14:23 -07003111 // TEMPORARY: DISABLE CLINGS ON LARGE UI
Winson Chung7d7541e2011-09-16 20:14:36 -07003112 if (LauncherApplication.isScreenLarge()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003113 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003114 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003115
Winson Chung7d7541e2011-09-16 20:14:36 -07003116 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003117 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003118 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3119 Cling cling = (Cling) findViewById(clingId);
3120 if (cling != null) {
3121 cling.init(this, positionData);
3122 cling.setVisibility(View.VISIBLE);
3123 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3124 if (animate) {
3125 cling.buildLayer();
3126 cling.setAlpha(0f);
3127 cling.animate()
3128 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003129 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003130 .setDuration(SHOW_CLING_DURATION)
3131 .setStartDelay(delay)
3132 .start();
3133 } else {
3134 cling.setAlpha(1f);
3135 }
3136 }
3137 return cling;
3138 }
3139 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003140 if (cling != null) {
3141 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003142 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003143 anim.addListener(new AnimatorListenerAdapter() {
3144 public void onAnimationEnd(Animator animation) {
3145 cling.setVisibility(View.GONE);
3146 cling.cleanup();
3147 SharedPreferences prefs =
3148 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3149 SharedPreferences.Editor editor = prefs.edit();
3150 editor.putBoolean(flag, true);
3151 editor.commit();
3152 };
3153 });
3154 anim.start();
3155 }
3156 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003157 private void removeCling(int id) {
3158 final View cling = findViewById(id);
3159 if (cling != null) {
3160 final ViewGroup parent = (ViewGroup) cling.getParent();
3161 parent.post(new Runnable() {
3162 @Override
3163 public void run() {
3164 parent.removeView(cling);
3165 }
3166 });
3167 }
3168 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003169 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003170 // Enable the clings only if they have not been dismissed before
3171 SharedPreferences prefs =
3172 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003173 if (isClingsEnabled() && !prefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003174 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003175 } else {
3176 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003177 }
3178 }
3179 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003180 // Enable the clings only if they have not been dismissed before
3181 SharedPreferences prefs =
3182 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003183 if (isClingsEnabled() && !prefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003184 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003185 } else {
3186 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003187 }
3188 }
3189 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003190 // Enable the clings only if they have not been dismissed before
3191 SharedPreferences prefs =
3192 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
3193 Cling cling = null;
Winson Chung9d9d74f2011-09-19 11:49:12 -07003194 if (isClingsEnabled() && !prefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003195 cling = initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003196 } else {
3197 removeCling(R.id.folder_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003198 }
3199 return cling;
3200 }
3201 public boolean isFolderClingVisible() {
3202 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003203 if (cling != null) {
3204 return cling.getVisibility() == View.VISIBLE;
3205 }
3206 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003207 }
Winson Chung82f55532011-08-09 14:14:23 -07003208 public void dismissWorkspaceCling(View v) {
3209 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003210 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003211 }
3212 public void dismissAllAppsCling(View v) {
3213 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003214 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3215 }
3216 public void dismissFolderCling(View v) {
3217 Cling cling = (Cling) findViewById(R.id.folder_cling);
3218 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003219 }
3220
Winson Chung80baf5a2010-08-09 16:03:15 -07003221 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003222 * Prints out out state for debugging.
3223 */
3224 public void dumpState() {
3225 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003226 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003227 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3228 Log.d(TAG, "mRestoring=" + mRestoring);
3229 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3230 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003231 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003232 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003233
Winson Chung785d2eb2011-04-14 16:08:02 -07003234 if (mAppsCustomizeContent != null) {
3235 mAppsCustomizeContent.dumpState();
3236 }
Joe Onoratobe386092009-11-17 17:32:16 -08003237 Log.d(TAG, "END launcher2 dump state");
3238 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003239
3240 @Override
3241 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3242 super.dump(prefix, fd, writer, args);
3243 writer.println(" ");
3244 writer.println("Debug logs: ");
3245 for (int i = 0; i < sDumpLogs.size(); i++) {
3246 writer.println(" " + sDumpLogs.get(i));
3247 }
3248 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003249}
Michael Jurka2763be32011-02-24 11:19:57 -08003250
Michael Jurkaabded662011-03-04 12:06:57 -08003251interface LauncherTransitionable {
Winson Chung7d7541e2011-09-16 20:14:36 -07003252 void onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace);
3253 void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003254}