blob: 6727a425957afc888560bc79e0a6b50ed0e2eb89 [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 {
Michael Jurka2a552322011-10-11 15:22:05 -07001682 onClickAllAppsButton(v);
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) {
Michael Jurka2a552322011-10-11 15:22:05 -07001725 // Provide the same haptic feedback that the system offers for virtual keys.
1726 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001727 showAllApps(true);
1728 }
1729
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001730 public void onClickAppMarketButton(View v) {
1731 if (mAppMarketIntent != null) {
1732 startActivitySafely(mAppMarketIntent, "app market");
1733 }
1734 }
1735
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001736 void startApplicationDetailsActivity(ComponentName componentName) {
1737 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001738 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1739 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001740 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001741 startActivity(intent);
1742 }
1743
Patrick Dubroy5539af72010-09-07 15:22:01 -07001744 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1745 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1746 // System applications cannot be installed. For now, show a toast explaining that.
1747 // We may give them the option of disabling apps this way.
1748 int messageId = R.string.uninstall_system_app_text;
1749 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1750 } else {
1751 String packageName = appInfo.componentName.getPackageName();
1752 String className = appInfo.componentName.getClassName();
1753 Intent intent = new Intent(
1754 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001755 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1756 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001757 startActivity(intent);
1758 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001759 }
1760
Michael Jurkaddd62e92011-02-16 17:49:14 -08001761 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001762 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1763 try {
1764 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001765 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001766 } catch (ActivityNotFoundException e) {
1767 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001768 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001769 } catch (SecurityException e) {
1770 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001771 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001773 "or use the exported attribute for this activity. "
1774 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001775 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001776 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001778
Romain Guy8e633c52010-03-04 12:51:36 -08001779 void startActivityForResultSafely(Intent intent, int requestCode) {
1780 try {
1781 startActivityForResult(intent, requestCode);
1782 } catch (ActivityNotFoundException e) {
1783 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1784 } catch (SecurityException e) {
1785 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1786 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1787 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1788 "or use the exported attribute for this activity.", e);
1789 }
1790 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791
Adam Cohena9cf38f2011-05-02 15:36:58 -07001792 private void handleFolderClick(FolderIcon folderIcon) {
1793 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001794 Folder openFolder = mWorkspace.getFolderForTag(info);
1795
1796 // If the folder info reports that the associated folder is open, then verify that
1797 // it is actually opened. There have been a few instances where this gets out of sync.
1798 if (info.opened && openFolder == null) {
1799 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1800 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1801 info.opened = false;
1802 }
1803
Adam Cohena9cf38f2011-05-02 15:36:58 -07001804 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001805 // Close any open folder
1806 closeFolder();
1807 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001808 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001809 } else {
1810 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 int folderScreen;
1812 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001813 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 // .. and close it
1815 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001816 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 // Close any folder open on the current screen
1818 closeFolder();
1819 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001820 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 }
1822 }
1823 }
1824 }
1825
Adam Cohen2801caf2011-05-13 20:57:39 -07001826 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001827 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001828 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1829 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1830 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1831
Adam Cohenc51934b2011-07-26 21:07:43 -07001832 FolderInfo info = (FolderInfo) fi.getTag();
1833 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1834 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001835 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1836 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001837 }
1838
Adam Cohen2801caf2011-05-13 20:57:39 -07001839 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1840 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1841 oa.start();
1842 }
1843
1844 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001845 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001846 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1847 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1848 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1849
Adam Cohenc51934b2011-07-26 21:07:43 -07001850 FolderInfo info = (FolderInfo) fi.getTag();
1851 CellLayout cl = null;
1852 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1853 cl = (CellLayout) fi.getParent().getParent();
1854 }
1855
1856 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001857 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1858 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001859 oa.addListener(new AnimatorListenerAdapter() {
1860 @Override
1861 public void onAnimationEnd(Animator animation) {
1862 if (layout != null) {
1863 layout.clearFolderLeaveBehind();
1864 }
1865 }
1866 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001867 oa.start();
1868 }
1869
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001871 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 * is animated relative to the specified View. If the View is null, no animation
1873 * is played.
1874 *
1875 * @param folderInfo The FolderInfo describing the folder to open.
1876 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001877 public void openFolder(FolderIcon folderIcon) {
1878 Folder folder = folderIcon.mFolder;
1879 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880
Adam Cohen2801caf2011-05-13 20:57:39 -07001881 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001882 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883
Adam Cohen4554ee12011-08-03 16:13:21 -07001884 // Just verify that the folder hasn't already been added to the DragLayer.
1885 // There was a one-off crash where the folder had a parent already.
1886 if (folder.getParent() == null) {
1887 mDragLayer.addView(folder);
1888 mDragController.addDropTarget((DropTarget) folder);
1889 } else {
1890 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1891 folder.getParent() + ").");
1892 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001893 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001894 }
1895
1896 public void closeFolder() {
1897 Folder folder = mWorkspace.getOpenFolder();
1898 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07001899 if (folder.isEditingName()) {
1900 folder.dismissEditingName();
1901 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001902 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07001903
1904 // Dismiss the folder cling
1905 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07001906 }
1907 }
1908
1909 void closeFolder(Folder folder) {
1910 folder.getInfo().opened = false;
1911
1912 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1913 if (parent != null) {
1914 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1915 shrinkAndFadeInFolderIcon(fi);
1916 }
1917 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 }
1919
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001921 if (mState != State.WORKSPACE) {
1922 return false;
1923 }
1924
Joe Onorato9c1289c2009-08-17 11:03:03 -04001925 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 return false;
1927 }
1928
1929 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001930 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 }
1932
Michael Jurka0280c3b2010-09-17 15:00:07 -07001933 resetAddInfo();
1934 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001936 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001937 return true;
1938 }
1939
Winson Chung3d503fb2011-07-13 17:25:49 -07001940 // The hotseat touch handling does not go through Workspace, and we always allow long press
1941 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001942 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001943 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1944 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001945 if (itemUnderLongClick == null) {
1946 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001947 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1948 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001949 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001951 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001953 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
1955 }
1956 }
1957 return true;
1958 }
1959
Winson Chung3d503fb2011-07-13 17:25:49 -07001960 boolean isHotseatLayout(View layout) {
1961 return mHotseat != null && layout != null &&
1962 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1963 }
1964 Hotseat getHotseat() {
1965 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001966 }
1967
Winson Chung3d503fb2011-07-13 17:25:49 -07001968 /**
1969 * Returns the CellLayout of the specified container at the specified screen.
1970 */
1971 CellLayout getCellLayout(long container, int screen) {
1972 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1973 if (mHotseat != null) {
1974 return mHotseat.getLayout();
1975 } else {
1976 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001977 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001978 } else {
1979 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001980 }
1981 }
1982
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 Workspace getWorkspace() {
1984 return mWorkspace;
1985 }
1986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 @Override
1988 protected Dialog onCreateDialog(int id) {
1989 switch (id) {
1990 case DIALOG_CREATE_SHORTCUT:
1991 return new CreateShortcut().createDialog();
1992 case DIALOG_RENAME_FOLDER:
1993 return new RenameFolder().createDialog();
1994 }
1995
1996 return super.onCreateDialog(id);
1997 }
1998
1999 @Override
2000 protected void onPrepareDialog(int id, Dialog dialog) {
2001 switch (id) {
2002 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003 break;
2004 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002005 if (mFolderInfo != null) {
2006 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2007 final CharSequence text = mFolderInfo.title;
2008 input.setText(text);
2009 input.setSelection(0, text.length());
2010 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002011 break;
2012 }
2013 }
2014
2015 void showRenameDialog(FolderInfo info) {
2016 mFolderInfo = info;
2017 mWaitingForResult = true;
2018 showDialog(DIALOG_RENAME_FOLDER);
2019 }
2020
Adam Cohenfbba09b2011-07-18 21:43:05 -07002021 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002022 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002023 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2024 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 mWaitingForResult = true;
2026 showDialog(DIALOG_CREATE_SHORTCUT);
2027 }
2028
2029 private class RenameFolder {
2030 private EditText mInput;
2031
2032 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2034 mInput = (EditText) layout.findViewById(R.id.folder_name);
2035
2036 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2037 builder.setIcon(0);
2038 builder.setTitle(getString(R.string.rename_folder_title));
2039 builder.setCancelable(true);
2040 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2041 public void onCancel(DialogInterface dialog) {
2042 cleanup();
2043 }
2044 });
2045 builder.setNegativeButton(getString(R.string.cancel_action),
2046 new Dialog.OnClickListener() {
2047 public void onClick(DialogInterface dialog, int which) {
2048 cleanup();
2049 }
2050 }
2051 );
2052 builder.setPositiveButton(getString(R.string.rename_action),
2053 new Dialog.OnClickListener() {
2054 public void onClick(DialogInterface dialog, int which) {
2055 changeFolderName();
2056 }
2057 }
2058 );
2059 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002060
2061 final AlertDialog dialog = builder.create();
2062 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2063 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002064 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002065 mInput.requestFocus();
2066 InputMethodManager inputManager = (InputMethodManager)
2067 getSystemService(Context.INPUT_METHOD_SERVICE);
2068 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002069 }
2070 });
2071
2072 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 }
2074
2075 private void changeFolderName() {
2076 final String name = mInput.getText().toString();
2077 if (!TextUtils.isEmpty(name)) {
2078 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002079 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 mFolderInfo.title = name;
2081 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2082
Joe Onorato9c1289c2009-08-17 11:03:03 -04002083 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002084 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002085 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 } else {
2087 final FolderIcon folderIcon = (FolderIcon)
2088 mWorkspace.getViewForTag(mFolderInfo);
2089 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002090 // TODO: At some point we'll probably want some version of setting
2091 // the text for a folder icon.
2092 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 getWorkspace().requestLayout();
2094 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002095 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002096 mWorkspaceLoading = true;
2097 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 }
2099 }
2100 }
2101 cleanup();
2102 }
2103
2104 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 dismissDialog(DIALOG_RENAME_FOLDER);
2106 mWaitingForResult = false;
2107 mFolderInfo = null;
2108 }
2109 }
2110
Daniel Sandler843e8602010-06-07 14:59:01 -04002111 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2112 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002113 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002114 }
2115
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002116 // AllAppsView.Watcher
2117 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002118 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002119 mWorkspace.setVisibility(View.GONE);
2120 }
2121 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002122
2123 /**
2124 * Helper method for the cameraZoomIn/cameraZoomOut animations
2125 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002126 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002127 * @param scaleFactor The scale factor used for the zoom
2128 */
2129 private void setPivotsForZoom(View view, State state, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002130 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002131 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002132 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002133
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002134 void updateWallpaperVisibility(boolean visible) {
2135 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2136 int curflags = getWindow().getAttributes().flags
2137 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2138 if (wpflags != curflags) {
2139 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2140 }
2141 }
2142
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002143 /**
2144 * Zoom the camera out from the workspace to reveal 'toView'.
2145 * Assumes that the view to show is anchored at either the very top or very bottom
2146 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002147 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002148 */
Winson Chungc07918d2011-07-01 15:35:26 -07002149 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002150 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002151 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002152
Winson Chungf0ea4d32011-06-06 14:27:16 -07002153 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2154 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2155 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2156 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002157 final int startDelay =
2158 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002159
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002160 setPivotsForZoom(toView, toState, scale);
2161
Michael Jurkad74c9842011-07-10 12:44:21 -07002162 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002163 mWorkspace.changeState(Workspace.State.SMALL, animated);
Adam Cohencff6af82011-09-13 14:51:53 -07002164 //hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002165
2166 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002167 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002168 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002169 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2170 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002171 ((View) toView.getParent()).invalidate();
2172 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002173 toView.setFastScaleX(a * scale + b * 1f);
2174 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002175 }
2176 });
Adam Cohen61033d32010-11-15 18:29:44 -08002177
Winson Chungf0ea4d32011-06-06 14:27:16 -07002178 toView.setVisibility(View.VISIBLE);
2179 toView.setFastAlpha(0f);
2180 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2181 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2182 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2183 public void onAnimationUpdate(float a, float b) {
2184 // don't need to invalidate because we do so above
2185 toView.setFastAlpha(a * 0f + b * 1f);
2186 }
2187 });
Adam Cohencff6af82011-09-13 14:51:53 -07002188 alphaAnim.setStartDelay(startDelay);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002189 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002190
Michael Jurkaabded662011-03-04 12:06:57 -08002191 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002192 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, scaleAnim,
2193 false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002194 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002195 scaleAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002196 boolean animationCancelled = false;
2197
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002198 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002199 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002200 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002201 // Prepare the position
2202 toView.setTranslationX(0.0f);
2203 toView.setTranslationY(0.0f);
2204 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002205 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002206 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002207 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002208 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002209 // If we don't set the final scale values here, if this animation is cancelled
2210 // it will have the wrong scale value and subsequent cameraPan animations will
2211 // not fix that
2212 toView.setScaleX(1.0f);
2213 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002214 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002215 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance,
2216 scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002217 }
Winson Chung32174c82011-07-19 15:47:55 -07002218
2219 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2220 // Hide the workspace scrollbar
2221 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002222 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002223 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002224 if (!animationCancelled) {
2225 updateWallpaperVisibility(false);
2226 }
2227 }
2228
Adam Cohencff6af82011-09-13 14:51:53 -07002229 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002230 public void onAnimationCancel(Animator animation) {
2231 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002232 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002233 });
2234
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002235 // toView should appear right at the end of the workspace shrink animation
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002236
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002237 if (mStateAnimation != null) mStateAnimation.cancel();
2238 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002239 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002240 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002241 } else {
2242 toView.setTranslationX(0.0f);
2243 toView.setTranslationY(0.0f);
2244 toView.setScaleX(1.0f);
2245 toView.setScaleY(1.0f);
2246 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002247 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002248 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002249 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, null, false);
2250 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance, null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002251
Winson Chungc07918d2011-07-01 15:35:26 -07002252 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2253 // Hide the workspace scrollbar
2254 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002255 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002256 }
Michael Jurkaabded662011-03-04 12:06:57 -08002257 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002258 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002259 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002260 }
2261
2262 /**
2263 * Zoom the camera back into the workspace, hiding 'fromView'.
2264 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002265 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002266 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002267 */
Winson Chungc07918d2011-07-01 15:35:26 -07002268 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002269 Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002270 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002271
Winson Chungf0ea4d32011-06-06 14:27:16 -07002272 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2273 final float scaleFactor = (float)
2274 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2275 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002276
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002277 setPivotsForZoom(fromView, fromState, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002278 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002279 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002280 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002281 if (mStateAnimation != null) mStateAnimation.cancel();
2282 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002283
Michael Jurka742574b2011-02-02 23:51:01 -08002284 final float oldScaleX = fromView.getScaleX();
2285 final float oldScaleY = fromView.getScaleY();
2286
2287 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2288 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002289 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2290 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002291 ((View)fromView.getParent()).fastInvalidate();
2292 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2293 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2294 }
2295 });
Michael Jurkaabded662011-03-04 12:06:57 -08002296 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002297 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Adam Cohencff6af82011-09-13 14:51:53 -07002298 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002299 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2300 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002301 // don't need to invalidate because we do so above
2302 fromView.setFastAlpha(a * 1f + b * 0f);
2303 }
2304 });
Michael Jurkaabded662011-03-04 12:06:57 -08002305 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002306 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, alphaAnim,
2307 true);
Michael Jurka2763be32011-02-24 11:19:57 -08002308 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002309 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002310 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002311 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002312 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002313 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002314 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002315 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance,
2316 alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002317 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002318 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002319 }
2320 });
2321
Winson Chungf0ea4d32011-06-06 14:27:16 -07002322 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002323 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002324 } else {
2325 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002326 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002327 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, null, true);
2328 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance, null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002329 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002330 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002331 }
2332
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002333 void showWorkspace(boolean animated) {
Adam Cohencff6af82011-09-13 14:51:53 -07002334 Resources res = getResources();
2335 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2336
2337 mWorkspace.changeState(Workspace.State.NORMAL, animated, stagger);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002338 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002339 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002340 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002341
Adam Cohen7777d962011-08-18 18:58:38 -07002342 mWorkspace.showDockDivider(!animated);
2343 mWorkspace.flashScrollingIndicator();
2344
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002345 // Change the state *after* we've called all the transition code
2346 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002347
Winson Chung5fb63472011-02-02 17:03:37 -08002348 // Resume the auto-advance of widgets
2349 mUserPresent = true;
2350 updateRunning();
2351
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002352 // send an accessibility event to announce the context change
2353 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002354 }
2355
Adam Cohen7777d962011-08-18 18:58:38 -07002356 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002357 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002358 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Winson Chungb26f3d62011-06-02 10:49:29 -07002359 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002360 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002361 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002362 }
Adam Cohen7777d962011-08-18 18:58:38 -07002363
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002364 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chung09bfc452011-09-09 11:30:20 -07002365 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2366
Winson Chunge7a03942011-08-05 15:05:12 -07002367 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002368 @Override
2369 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002370 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002371 // Before we show workspace, hide all apps again because
2372 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2373 // clean up our state transition functions
2374 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002375 mSearchDropTargetBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002376 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002377 } else {
2378 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002379 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002380 }
2381 }, (extendedDelay ?
2382 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2383 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2384 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002385 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002386 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002387 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2388 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002389 }
2390 // Otherwise, we are not in spring loaded mode, so don't do anything.
2391 }
2392
Adam Cohenfc53cd22011-07-20 15:45:11 -07002393 public boolean isAllAppsCustomizeOpen() {
2394 return mState == State.APPS_CUSTOMIZE;
2395 }
2396
Winson Chungf0ea4d32011-06-06 14:27:16 -07002397 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002398 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002399 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002400 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002401 if (!LauncherApplication.isScreenLarge()) {
2402 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002403 int duration = mSearchDropTargetBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002404 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002405 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002406 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002407 }
2408 }
2409 }
2410
2411 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002412 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002413 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002414 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002415 if (!LauncherApplication.isScreenLarge()) {
2416 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002417 int duration = mSearchDropTargetBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002418 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002419 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002420 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002421 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002422 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002423 }
2424
Winson Chung785d2eb2011-04-14 16:08:02 -07002425 void showAllApps(boolean animated) {
2426 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002427
Winson Chungf0ea4d32011-06-06 14:27:16 -07002428 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2429 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002430
Winson Chung3d503fb2011-07-13 17:25:49 -07002431 // Hide the search bar and hotseat
Winson Chungc51db6a2011-10-05 11:44:49 -07002432 mSearchDropTargetBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002433
Winson Chungf0ea4d32011-06-06 14:27:16 -07002434 // Change the state *after* we've called all the transition code
2435 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002436
2437 // Pause the auto-advance of widgets until we are out of AllApps
2438 mUserPresent = false;
2439 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002440 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002441
2442 // Send an accessibility event to announce the context change
2443 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2444 }
2445
Joe Onoratob2061212009-11-24 16:13:54 -05002446 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002447 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002448 * - Home from workspace
2449 * - from center screen
2450 * - from other screens
2451 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002452 * - from center screen
2453 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002454 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002455 * - from center screen
2456 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002457 * - Launch app from workspace and quit
2458 * - with back
2459 * - with home
2460 * - Launch app from all apps and quit
2461 * - with back
2462 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002463 * - Go to a screen that's not the default, then all
2464 * apps, and launch and app, and go back
2465 * - with back
2466 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002467 * - On workspace, long press power and go back
2468 * - with back
2469 * - with home
2470 * - On all apps, long press power and go back
2471 * - with back
2472 * - with home
2473 * - On workspace, power off
2474 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002475 * - Launch an app and turn off the screen while in that app
2476 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002477 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002478 * - From all apps
2479 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002480 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2481 * - From all apps
2482 * - From the center workspace
2483 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002484 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002485 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002486 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2487 mWorkspace.setVisibility(View.VISIBLE);
2488 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002489
Winson Chung3d503fb2011-07-13 17:25:49 -07002490 // Show the search bar and hotseat
Winson Chungc51db6a2011-10-05 11:44:49 -07002491 mSearchDropTargetBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002492
Winson Chungf0ea4d32011-06-06 14:27:16 -07002493 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002494 if (mAllAppsButton != null) {
2495 mAllAppsButton.requestFocus();
2496 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002497 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002498 }
2499
Joe Onorato7c312c12009-08-13 21:36:53 -07002500 void lockAllApps() {
2501 // TODO
2502 }
2503
2504 void unlockAllApps() {
2505 // TODO
2506 }
2507
Patrick Dubroy5f445422011-02-18 14:35:21 -08002508 /**
2509 * Add an item from all apps or customize onto the given workspace screen.
2510 * If layout is null, add to the current screen.
2511 */
2512 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002513 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2514 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002515 } else {
2516 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002517 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002518 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002519
Winson Chungdff8ebb2011-09-08 17:25:31 -07002520 /** Maps the current orientation to an index for referencing orientation correct global icons */
2521 private int getCurrentOrientationIndexForGlobalIcons() {
2522 // default - 0, landscape - 1
2523 switch (getResources().getConfiguration().orientation) {
2524 case Configuration.ORIENTATION_LANDSCAPE:
2525 return 1;
2526 default:
2527 return 0;
2528 }
2529 }
2530
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002531 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002532 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002533 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002534 // Look for the toolbar icon specified in the activity meta-data
2535 Bundle metaData = packageManager.getActivityInfo(
2536 activityName, PackageManager.GET_META_DATA).metaData;
2537 if (metaData != null) {
2538 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2539 if (iconResId != 0) {
2540 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002541 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002542 }
2543 }
2544 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002545 // This can happen if the activity defines an invalid drawable
2546 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2547 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002548 } catch (Resources.NotFoundException nfe) {
2549 // This can happen if the activity defines an invalid drawable
2550 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2551 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002552 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002553 return null;
2554 }
2555
2556 // if successful in getting icon, return it; otherwise, set button to use default drawable
2557 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2558 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2559 TextView button = (TextView) findViewById(buttonId);
2560 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002561 Resources r = getResources();
2562 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2563 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002564
2565 // If we were unable to find the icon via the meta-data, use a generic one
2566 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002567 toolbarIcon = r.getDrawable(fallbackDrawableId);
2568 toolbarIcon.setBounds(0, 0, w, h);
2569 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002570 return null;
2571 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002572 toolbarIcon.setBounds(0, 0, w, h);
2573 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002574 return toolbarIcon.getConstantState();
2575 }
2576 }
2577
2578 // if successful in getting icon, return it; otherwise, set button to use default drawable
2579 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2580 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2581 ImageView button = (ImageView) findViewById(buttonId);
2582 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2583
Michael Jurka19e0fc52011-07-22 18:00:21 -07002584 if (button != null) {
2585 // If we were unable to find the icon via the meta-data, use a
2586 // generic one
2587 if (toolbarIcon == null) {
2588 button.setImageResource(fallbackDrawableId);
2589 } else {
2590 button.setImageDrawable(toolbarIcon);
2591 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002592 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002593
2594 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2595
Michael Jurka0423dcf2010-10-05 14:56:18 -07002596 }
2597
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002598 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2599 TextView button = (TextView) findViewById(buttonId);
2600 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2601 }
2602
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002603 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002604 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002605 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002606 }
2607
Winson Chungc51db6a2011-10-05 11:44:49 -07002608 private boolean updateGlobalSearchIcon() {
2609 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002610 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2611 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002612 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002613 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002614
Winson Chung1cad91e2011-05-25 17:41:01 -07002615 final SearchManager searchManager =
2616 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2617 ComponentName activityName = searchManager.getGlobalSearchActivity();
2618 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002619 int coi = getCurrentOrientationIndexForGlobalIcons();
2620 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002621 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002622 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002623 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2624 searchButton.setVisibility(View.VISIBLE);
2625 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002626 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002627 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002628 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002629 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2630 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2631 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002632 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002633 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002634 }
2635 }
2636
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002637 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002638 updateButtonWithDrawable(R.id.search_button, d);
2639 }
2640
Winson Chungc51db6a2011-10-05 11:44:49 -07002641 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002642 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002643 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002644 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002645
Winson Chungc51db6a2011-10-05 11:44:49 -07002646 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002647 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2648 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002649 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002650 int coi = getCurrentOrientationIndexForGlobalIcons();
2651 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002652 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002653 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002654 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002655 voiceButton.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002656 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002657 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002658 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002659 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002660 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002661 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002662 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002663 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002664
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002665 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002666 updateButtonWithDrawable(R.id.voice_button, d);
2667 }
2668
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002669 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002670 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002671 */
2672 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002673 final View marketButton = findViewById(R.id.market_button);
2674 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2675 // Find the app market activity by resolving an intent.
2676 // (If multiple app markets are installed, it will return the ResolverActivity.)
2677 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002678 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002679 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002680 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002681 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002682 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002683 marketButton.setVisibility(View.VISIBLE);
2684 } else {
2685 // We should hide and disable the view so that we don't try and restore the visibility
2686 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2687 marketButton.setVisibility(View.GONE);
2688 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002689 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002690 }
2691
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002692 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002693 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002694 }
2695
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002696 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002697 * Displays the shortcut creation dialog and launches, if necessary, the
2698 * appropriate activity.
2699 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002700 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002701 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2702 DialogInterface.OnShowListener {
2703
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002704 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002705
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002706 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002707 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002708
Winson Chung55b65502011-05-26 12:03:43 -07002709 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2710 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002711 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002712
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002713 AlertDialog dialog = builder.create();
2714 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002715 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002716 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002717
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002718 return dialog;
2719 }
2720
2721 public void onCancel(DialogInterface dialog) {
2722 mWaitingForResult = false;
2723 cleanup();
2724 }
2725
Romain Guycbb89e42009-06-08 15:52:54 -07002726 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002727 mWaitingForResult = false;
2728 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002729 }
2730
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002731 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002732 try {
2733 dismissDialog(DIALOG_CREATE_SHORTCUT);
2734 } catch (Exception e) {
2735 // An exception is thrown if the dialog is not visible, which is fine
2736 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002737 }
2738
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002739 /**
2740 * Handle the action clicked in the "Add to home" dialog.
2741 */
2742 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002743 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002744
Winson Chung55b65502011-05-26 12:03:43 -07002745 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2746 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002747 case AddAdapter.ITEM_APPLICATION: {
2748 if (mAppsCustomizeTabHost != null) {
2749 mAppsCustomizeTabHost.selectAppsTab();
2750 }
2751 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002752 break;
2753 }
Winson Chung55b65502011-05-26 12:03:43 -07002754 case AddAdapter.ITEM_APPWIDGET: {
2755 if (mAppsCustomizeTabHost != null) {
2756 mAppsCustomizeTabHost.selectWidgetsTab();
2757 }
2758 showAllApps(true);
2759 break;
2760 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002761 case AddAdapter.ITEM_WALLPAPER: {
2762 startWallpaper();
2763 break;
2764 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002765 }
2766 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002767
2768 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002769 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002770 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002771 }
2772
2773 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002774 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002775 */
2776 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2777 @Override
2778 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002779 closeSystemDialogs();
2780 }
2781 }
2782
2783 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002784 * Receives notifications whenever the appwidgets are reset.
2785 */
2786 private class AppWidgetResetObserver extends ContentObserver {
2787 public AppWidgetResetObserver() {
2788 super(new Handler());
2789 }
2790
2791 @Override
2792 public void onChange(boolean selfChange) {
2793 onAppWidgetReset();
2794 }
2795 }
2796
2797 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002798 * If the activity is currently paused, signal that we need to re-run the loader
2799 * in onResume.
2800 *
2801 * This needs to be called from incoming places where resources might have been loaded
2802 * while we are paused. That is becaues the Configuration might be wrong
2803 * when we're not running, and if it comes back to what it was when we
2804 * were paused, we are not restarted.
2805 *
2806 * Implementation of the method from LauncherModel.Callbacks.
2807 *
2808 * @return true if we are currently paused. The caller might be able to
2809 * skip some work in that case since we will come back again.
2810 */
2811 public boolean setLoadOnResume() {
2812 if (mPaused) {
2813 Log.i(TAG, "setLoadOnResume");
2814 mOnResumeNeedsLoad = true;
2815 return true;
2816 } else {
2817 return false;
2818 }
2819 }
2820
2821 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002822 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002823 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002824 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002825 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002826 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002827 } else {
2828 return SCREEN_COUNT / 2;
2829 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002830 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002831
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002832
Joe Onorato9c1289c2009-08-17 11:03:03 -04002833 /**
2834 * Refreshes the shortcuts shown on the workspace.
2835 *
2836 * Implementation of the method from LauncherModel.Callbacks.
2837 */
2838 public void startBinding() {
2839 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002840
2841 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002842 int count = workspace.getChildCount();
2843 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002844 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002845 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2846 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002847 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002848 if (mHotseat != null) {
2849 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002850 }
2851 }
2852
2853 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002854 * Bind the items start-end from the list.
2855 *
2856 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002857 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002858 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002859 setLoadOnResume();
2860
Joe Onorato9c1289c2009-08-17 11:03:03 -04002861 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002862 for (int i=start; i<end; i++) {
2863 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002864
2865 // Short circuit if we are loading dock items for a configuration which has no dock
2866 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2867 mHotseat == null) {
2868 continue;
2869 }
2870
Joe Onorato9c1289c2009-08-17 11:03:03 -04002871 switch (item.itemType) {
2872 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2873 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002874 View shortcut = createShortcut((ShortcutInfo)item);
2875 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2876 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002877 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002878 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002879 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002880 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002881 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002882 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2883 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002884 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002885 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002886 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002887 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002888 }
2889
Joe Onorato9c1289c2009-08-17 11:03:03 -04002890 /**
2891 * Implementation of the method from LauncherModel.Callbacks.
2892 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002893 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002894 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002895 sFolders.clear();
2896 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002897 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002898
2899 /**
2900 * Add the views for a widget to the workspace.
2901 *
2902 * Implementation of the method from LauncherModel.Callbacks.
2903 */
2904 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002905 setLoadOnResume();
2906
Daniel Sandler843e8602010-06-07 14:59:01 -04002907 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2908 if (DEBUG_WIDGETS) {
2909 Log.d(TAG, "bindAppWidget: " + item);
2910 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002911 final Workspace workspace = mWorkspace;
2912
2913 final int appWidgetId = item.appWidgetId;
2914 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002915 if (DEBUG_WIDGETS) {
2916 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2917 }
2918
Joe Onorato9c1289c2009-08-17 11:03:03 -04002919 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2920
Joe Onorato9c1289c2009-08-17 11:03:03 -04002921 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2922 item.hostView.setTag(item);
2923
Winson Chung3d503fb2011-07-13 17:25:49 -07002924 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002925 item.cellY, item.spanX, item.spanY, false);
2926
Adam Cohended9f8d2010-11-03 13:25:16 -07002927 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2928
Joe Onorato9c1289c2009-08-17 11:03:03 -04002929 workspace.requestLayout();
2930
Daniel Sandler843e8602010-06-07 14:59:01 -04002931 if (DEBUG_WIDGETS) {
2932 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2933 + (SystemClock.uptimeMillis()-start) + "ms");
2934 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002935 }
2936
2937 /**
2938 * Callback saying that there aren't any more items to bind.
2939 *
2940 * Implementation of the method from LauncherModel.Callbacks.
2941 */
2942 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002943 setLoadOnResume();
2944
Joe Onorato9c1289c2009-08-17 11:03:03 -04002945 if (mSavedState != null) {
2946 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002947 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002948 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002949 mSavedState = null;
2950 }
2951
2952 if (mSavedInstanceState != null) {
2953 super.onRestoreInstanceState(mSavedInstanceState);
2954 mSavedInstanceState = null;
2955 }
2956
Joe Onorato9c1289c2009-08-17 11:03:03 -04002957 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002958
2959 // If we received the result of any pending adds while the loader was running (e.g. the
2960 // widget configuration forced an orientation change), process them now.
2961 for (int i = 0; i < sPendingAddList.size(); i++) {
2962 completeAdd(sPendingAddList.get(i));
2963 }
2964 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002965
Winson Chungc51db6a2011-10-05 11:44:49 -07002966 // Update the market app icon as necessary (the other icons will be managed in response to
2967 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002968 updateAppMarketIcon();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002969 }
2970
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002971 @Override
2972 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07002973 boolean searchVisible = updateGlobalSearchIcon();
2974 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
2975 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002976 }
2977
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002978 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002979 * Add the icons for all apps.
2980 *
2981 * Implementation of the method from LauncherModel.Callbacks.
2982 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002983 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2984 // Remove the progress bar entirely; we could also make it GONE
2985 // but better to remove it since we know it's not going to be used
2986 View progressBar = mAppsCustomizeTabHost.
2987 findViewById(R.id.apps_customize_progress_bar);
2988 if (progressBar != null) {
2989 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07002990 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07002991 // We just post the call to setApps so the user sees the progress bar
2992 // disappear-- otherwise, it just looks like the progress bar froze
2993 // which doesn't look great
2994 mAppsCustomizeTabHost.post(new Runnable() {
2995 public void run() {
2996 if (mAppsCustomizeContent != null) {
2997 mAppsCustomizeContent.setApps(apps);
2998 }
2999 }
3000 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003001 }
3002
3003 /**
3004 * A package was installed.
3005 *
3006 * Implementation of the method from LauncherModel.Callbacks.
3007 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003008 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003009 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003010 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003011
Winson Chung785d2eb2011-04-14 16:08:02 -07003012 if (mAppsCustomizeContent != null) {
3013 mAppsCustomizeContent.addApps(apps);
3014 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003015 }
3016
3017 /**
3018 * A package was updated.
3019 *
3020 * Implementation of the method from LauncherModel.Callbacks.
3021 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003022 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003023 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003024 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003025 if (mWorkspace != null) {
3026 mWorkspace.updateShortcuts(apps);
3027 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003028
Winson Chung785d2eb2011-04-14 16:08:02 -07003029 if (mAppsCustomizeContent != null) {
3030 mAppsCustomizeContent.updateApps(apps);
3031 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003032 }
3033
3034 /**
3035 * A package was uninstalled.
3036 *
3037 * Implementation of the method from LauncherModel.Callbacks.
3038 */
Joe Onorato36115782010-06-17 13:28:48 -04003039 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003040 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003041 if (permanent) {
3042 mWorkspace.removeItems(apps);
3043 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003044
Winson Chung785d2eb2011-04-14 16:08:02 -07003045 if (mAppsCustomizeContent != null) {
3046 mAppsCustomizeContent.removeApps(apps);
3047 }
Winson Chunga1820962011-10-03 16:31:06 -07003048
3049 // Notify the drag controller
3050 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003051 }
Joe Onoratobe386092009-11-17 17:32:16 -08003052
3053 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003054 * A number of packages were updated.
3055 */
3056 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003057 if (mAppsCustomizeContent != null) {
3058 mAppsCustomizeContent.onPackagesUpdated();
3059 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003060 }
3061
Winson Chung400438b2011-01-16 17:53:48 -08003062 private int mapConfigurationOriActivityInfoOri(int configOri) {
3063 final Display d = getWindowManager().getDefaultDisplay();
3064 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3065 switch (d.getRotation()) {
3066 case Surface.ROTATION_0:
3067 case Surface.ROTATION_180:
3068 // We are currently in the same basic orientation as the natural orientation
3069 naturalOri = configOri;
3070 break;
3071 case Surface.ROTATION_90:
3072 case Surface.ROTATION_270:
3073 // We are currently in the other basic orientation to the natural orientation
3074 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3075 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3076 break;
3077 }
3078
3079 int[] oriMap = {
3080 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3081 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3082 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3083 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3084 };
3085 // Since the map starts at portrait, we need to offset if this device's natural orientation
3086 // is landscape.
3087 int indexOffset = 0;
3088 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3089 indexOffset = 1;
3090 }
3091 return oriMap[(d.getRotation() + indexOffset) % 4];
3092 }
Adam Cohen446e9402011-09-15 18:21:21 -07003093
3094 public void lockScreenOrientationOnLargeUI() {
3095 if (LauncherApplication.isScreenLarge()) {
3096 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3097 .getConfiguration().orientation));
3098 }
Winson Chung400438b2011-01-16 17:53:48 -08003099 }
Adam Cohen446e9402011-09-15 18:21:21 -07003100 public void unlockScreenOrientationOnLargeUI() {
3101 if (LauncherApplication.isScreenLarge()) {
3102 mHandler.postDelayed(new Runnable() {
3103 public void run() {
3104 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3105 }
3106 }, mRestoreScreenOrientationDelay);
3107 }
Winson Chung400438b2011-01-16 17:53:48 -08003108 }
3109
Winson Chung82f55532011-08-09 14:14:23 -07003110 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003111 private static final String PREFS_KEY = "com.android.launcher2.prefs";
3112 private boolean isClingsEnabled() {
Winson Chung82f55532011-08-09 14:14:23 -07003113 // TEMPORARY: DISABLE CLINGS ON LARGE UI
Winson Chung7d7541e2011-09-16 20:14:36 -07003114 if (LauncherApplication.isScreenLarge()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003115 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003116 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003117
Winson Chung7d7541e2011-09-16 20:14:36 -07003118 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003119 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003120 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3121 Cling cling = (Cling) findViewById(clingId);
3122 if (cling != null) {
3123 cling.init(this, positionData);
3124 cling.setVisibility(View.VISIBLE);
3125 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3126 if (animate) {
3127 cling.buildLayer();
3128 cling.setAlpha(0f);
3129 cling.animate()
3130 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003131 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003132 .setDuration(SHOW_CLING_DURATION)
3133 .setStartDelay(delay)
3134 .start();
3135 } else {
3136 cling.setAlpha(1f);
3137 }
3138 }
3139 return cling;
3140 }
3141 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003142 if (cling != null) {
3143 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003144 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003145 anim.addListener(new AnimatorListenerAdapter() {
3146 public void onAnimationEnd(Animator animation) {
3147 cling.setVisibility(View.GONE);
3148 cling.cleanup();
3149 SharedPreferences prefs =
3150 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3151 SharedPreferences.Editor editor = prefs.edit();
3152 editor.putBoolean(flag, true);
3153 editor.commit();
3154 };
3155 });
3156 anim.start();
3157 }
3158 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003159 private void removeCling(int id) {
3160 final View cling = findViewById(id);
3161 if (cling != null) {
3162 final ViewGroup parent = (ViewGroup) cling.getParent();
3163 parent.post(new Runnable() {
3164 @Override
3165 public void run() {
3166 parent.removeView(cling);
3167 }
3168 });
3169 }
3170 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003171 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003172 // Enable the clings only if they have not been dismissed before
3173 SharedPreferences prefs =
3174 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003175 if (isClingsEnabled() && !prefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003176 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003177 } else {
3178 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003179 }
3180 }
3181 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003182 // Enable the clings only if they have not been dismissed before
3183 SharedPreferences prefs =
3184 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003185 if (isClingsEnabled() && !prefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003186 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003187 } else {
3188 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003189 }
3190 }
3191 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003192 // Enable the clings only if they have not been dismissed before
3193 SharedPreferences prefs =
3194 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
3195 Cling cling = null;
Winson Chung9d9d74f2011-09-19 11:49:12 -07003196 if (isClingsEnabled() && !prefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003197 cling = initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003198 } else {
3199 removeCling(R.id.folder_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003200 }
3201 return cling;
3202 }
3203 public boolean isFolderClingVisible() {
3204 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003205 if (cling != null) {
3206 return cling.getVisibility() == View.VISIBLE;
3207 }
3208 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003209 }
Winson Chung82f55532011-08-09 14:14:23 -07003210 public void dismissWorkspaceCling(View v) {
3211 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003212 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003213 }
3214 public void dismissAllAppsCling(View v) {
3215 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003216 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3217 }
3218 public void dismissFolderCling(View v) {
3219 Cling cling = (Cling) findViewById(R.id.folder_cling);
3220 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003221 }
3222
Winson Chung80baf5a2010-08-09 16:03:15 -07003223 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003224 * Prints out out state for debugging.
3225 */
3226 public void dumpState() {
3227 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003228 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003229 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3230 Log.d(TAG, "mRestoring=" + mRestoring);
3231 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3232 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003233 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003234 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003235
Winson Chung785d2eb2011-04-14 16:08:02 -07003236 if (mAppsCustomizeContent != null) {
3237 mAppsCustomizeContent.dumpState();
3238 }
Joe Onoratobe386092009-11-17 17:32:16 -08003239 Log.d(TAG, "END launcher2 dump state");
3240 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003241
3242 @Override
3243 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3244 super.dump(prefix, fd, writer, args);
3245 writer.println(" ");
3246 writer.println("Debug logs: ");
3247 for (int i = 0; i < sDumpLogs.size(); i++) {
3248 writer.println(" " + sDumpLogs.get(i));
3249 }
3250 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003251}
Michael Jurka2763be32011-02-24 11:19:57 -08003252
Michael Jurkaabded662011-03-04 12:06:57 -08003253interface LauncherTransitionable {
Winson Chung7d7541e2011-09-16 20:14:36 -07003254 void onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace);
3255 void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003256}