blob: 72ac39079bada978c9f39e058db106b1ea1c7892 [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
Michael Jurkac1f5d262011-09-30 19:32:27 -0700247 private Runnable mBuildLayersRunnable = new Runnable() {
248 public void run() {
249 mWorkspace.buildPageHardwareLayers();
250 }
251 };
252
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800253 private static ArrayList<PendingAddArguments> sPendingAddList
254 = new ArrayList<PendingAddArguments>();
255
256 private static class PendingAddArguments {
257 int requestCode;
258 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700259 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800260 int screen;
261 int cellX;
262 int cellY;
263 }
264
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800265 @Override
266 protected void onCreate(Bundle savedInstanceState) {
267 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800268 LauncherApplication app = ((LauncherApplication)getApplication());
269 mModel = app.setLauncher(this);
270 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400271 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700273
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700274 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700275 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
276 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700277
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200279 android.os.Debug.startMethodTracing(
280 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 }
282
283 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284 setContentView(R.layout.launcher);
285 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700286 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800288 registerContentObservers();
289
Joe Onorato7c312c12009-08-13 21:36:53 -0700290 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700291
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 mSavedState = savedInstanceState;
293 restoreState(mSavedState);
294
Winson Chunga12a2502010-12-20 14:41:35 -0800295 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700296 if (mAppsCustomizeContent != null) {
297 mAppsCustomizeContent.onPackagesUpdated();
298 }
Winson Chunga12a2502010-12-20 14:41:35 -0800299
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 if (PROFILE_STARTUP) {
301 android.os.Debug.stopMethodTracing();
302 }
303
304 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800306 }
307
Michael Jurkac57b7a82011-08-09 22:02:20 -0700308 if (!mModel.isAllAppsLoaded()) {
309 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
310 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
311 }
312
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313 // For handling default keys
314 mDefaultKeySsb = new SpannableStringBuilder();
315 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800316
317 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
318 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800319
Winson Chungc51db6a2011-10-05 11:44:49 -0700320 boolean searchVisible = false;
321 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800322 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700323 int coi = getCurrentOrientationIndexForGlobalIcons();
324 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
325 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700326 updateAppMarketIcon();
327 searchVisible = updateGlobalSearchIcon();
328 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700329 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700330 if (sGlobalSearchIcon[coi] != null) {
331 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700332 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700333 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700334 if (sVoiceSearchIcon[coi] != null) {
335 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700336 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700337 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700338 if (sAppMarketIcon[coi] != null) {
339 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800340 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700341 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700342
343 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohendc16b602011-09-26 15:12:33 -0700344 if (LauncherApplication.isScreenLarge() || Build.TYPE.contentEquals("eng")) {
Adam Cohen446e9402011-09-15 18:21:21 -0700345 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
346 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800347 }
Romain Guycbb89e42009-06-08 15:52:54 -0700348
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800349 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700350 if (sLocaleConfiguration == null) {
351 new AsyncTask<Void, Void, LocaleConfiguration>() {
352 @Override
353 protected LocaleConfiguration doInBackground(Void... unused) {
354 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
355 readConfiguration(Launcher.this, localeConfiguration);
356 return localeConfiguration;
357 }
358
359 @Override
360 protected void onPostExecute(LocaleConfiguration result) {
361 sLocaleConfiguration = result;
362 checkForLocaleChange(); // recursive, but now with a locale configuration
363 }
364 }.execute();
365 return;
366 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700367
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 final Configuration configuration = getResources().getConfiguration();
369
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700370 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 final String locale = configuration.locale.toString();
372
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700373 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800374 final int mcc = configuration.mcc;
375
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700376 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 final int mnc = configuration.mnc;
378
Romain Guy84f296c2009-11-04 15:00:44 -0800379 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380
Romain Guy84f296c2009-11-04 15:00:44 -0800381 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700382 sLocaleConfiguration.locale = locale;
383 sLocaleConfiguration.mcc = mcc;
384 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700385
Joe Onorato0589f0f2010-02-08 13:44:00 -0800386 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700387
388 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
389 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700390 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700391 public void run() {
392 writeConfiguration(Launcher.this, localeConfiguration);
393 }
394 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700395 }
396 }
397
398 private static class LocaleConfiguration {
399 public String locale;
400 public int mcc = -1;
401 public int mnc = -1;
402 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700403
Romain Guy98d01652009-06-30 16:21:04 -0700404 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
405 DataInputStream in = null;
406 try {
407 in = new DataInputStream(context.openFileInput(PREFERENCES));
408 configuration.locale = in.readUTF();
409 configuration.mcc = in.readInt();
410 configuration.mnc = in.readInt();
411 } catch (FileNotFoundException e) {
412 // Ignore
413 } catch (IOException e) {
414 // Ignore
415 } finally {
416 if (in != null) {
417 try {
418 in.close();
419 } catch (IOException e) {
420 // Ignore
421 }
422 }
423 }
424 }
425
426 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
427 DataOutputStream out = null;
428 try {
429 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
430 out.writeUTF(configuration.locale);
431 out.writeInt(configuration.mcc);
432 out.writeInt(configuration.mnc);
433 out.flush();
434 } catch (FileNotFoundException e) {
435 // Ignore
436 } catch (IOException e) {
437 //noinspection ResultOfMethodCallIgnored
438 context.getFileStreamPath(PREFERENCES).delete();
439 } finally {
440 if (out != null) {
441 try {
442 out.close();
443 } catch (IOException e) {
444 // Ignore
445 }
446 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 }
448 }
449
Adam Cohen716b51e2011-06-30 12:09:54 -0700450 public DragLayer getDragLayer() {
451 return mDragLayer;
452 }
453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 static int getScreen() {
455 synchronized (sLock) {
456 return sScreen;
457 }
458 }
459
460 static void setScreen(int screen) {
461 synchronized (sLock) {
462 sScreen = screen;
463 }
464 }
465
Winson Chung557d6ed2011-07-08 15:34:52 -0700466 /**
467 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
468 * a configuration step, this allows the proper animations to run after other transitions.
469 */
470 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700471 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800472 switch (args.requestCode) {
473 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700474 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
475 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800476 break;
477 case REQUEST_PICK_SHORTCUT:
478 processShortcut(args.intent);
479 break;
480 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700481 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
482 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700483 result = true;
484 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800485 case REQUEST_PICK_APPWIDGET:
486 addAppWidgetFromPick(args.intent);
487 break;
488 case REQUEST_CREATE_APPWIDGET:
489 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700490 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700491 result = true;
492 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800493 case REQUEST_PICK_WALLPAPER:
494 // We just wanted the activity result here so we can clear mWaitingForResult
495 break;
496 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700497 // In any situation where we have a multi-step drop, we should reset the add info only after
498 // we complete the drop
499 resetAddInfo();
500 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800501 }
502
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800503 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800504 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700505 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700506 mWaitingForResult = false;
507
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800508 // The pattern used here is that a user PICKs a specific application,
509 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700510
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800511 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
512 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700513 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800514 final PendingAddArguments args = new PendingAddArguments();
515 args.requestCode = requestCode;
516 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700517 args.container = mPendingAddInfo.container;
518 args.screen = mPendingAddInfo.screen;
519 args.cellX = mPendingAddInfo.cellX;
520 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800521
522 // If the loader is still running, defer the add until it is done.
523 if (isWorkspaceLocked()) {
524 sPendingAddList.add(args);
525 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700526 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800527 }
Romain Guy18042c82009-11-06 11:44:55 -0800528 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700529 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
530 if (data != null) {
531 // Clean up the appWidgetId if we canceled
532 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
533 if (appWidgetId != -1) {
534 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
535 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800536 }
537 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700538
539 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700540 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 }
542
543 @Override
544 protected void onResume() {
545 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800546 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700547 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400548 mWorkspaceLoading = true;
549 mModel.startLoader(this, true);
550 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700551 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800553 if (mWaitingForResume != null) {
554 mWaitingForResume.setStayPressed(false);
555 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700556 // When we resume Launcher, a different Activity might be responsible for the app
557 // market intent, so refresh the icon
558 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -0700559 if (!mWorkspaceLoading) {
560 mWorkspace.post(mBuildLayersRunnable);
561 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800562 }
563
564 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700565 protected void onPause() {
566 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700567 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800568 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700569 }
Romain Guycbb89e42009-06-08 15:52:54 -0700570
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700571 @Override
572 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400573 // Flag the loader to stop early before switching
574 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700575 if (mAppsCustomizeContent != null) {
576 mAppsCustomizeContent.surrender();
577 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800578 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700579 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700580
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800581 // We can't hide the IME if it was forced open. So don't bother
582 /*
583 @Override
584 public void onWindowFocusChanged(boolean hasFocus) {
585 super.onWindowFocusChanged(hasFocus);
586
587 if (hasFocus) {
588 final InputMethodManager inputManager = (InputMethodManager)
589 getSystemService(Context.INPUT_METHOD_SERVICE);
590 WindowManager.LayoutParams lp = getWindow().getAttributes();
591 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
592 android.os.Handler()) {
593 protected void onReceiveResult(int resultCode, Bundle resultData) {
594 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
595 }
596 });
597 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
598 }
599 }
600 */
601
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800602 private boolean acceptFilter() {
603 final InputMethodManager inputManager = (InputMethodManager)
604 getSystemService(Context.INPUT_METHOD_SERVICE);
605 return !inputManager.isFullscreenMode();
606 }
607
608 @Override
609 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700610 final int uniChar = event.getUnicodeChar();
611 final boolean handled = super.onKeyDown(keyCode, event);
612 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
613 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
615 keyCode, event);
616 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700617 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700618 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700619 // showSearchDialog()
620 // If there are multiple keystrokes before the search dialog takes focus,
621 // onSearchRequested() will be called for every keystroke,
622 // but it is idempotent, so it's fine.
623 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624 }
625 }
626
Joe Onorato8a9625e2010-01-28 15:55:35 -0800627 // Eat the long press event so the keyboard doesn't come up.
628 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
629 return true;
630 }
631
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800632 return handled;
633 }
634
Karl Rosaen138a0412009-04-23 19:00:21 -0700635 private String getTypedText() {
636 return mDefaultKeySsb.toString();
637 }
638
639 private void clearTypedText() {
640 mDefaultKeySsb.clear();
641 mDefaultKeySsb.clearSpans();
642 Selection.setSelection(mDefaultKeySsb, 0);
643 }
644
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800645 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700646 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
647 * State
648 */
649 private static State intToState(int stateOrdinal) {
650 State state = State.WORKSPACE;
651 final State[] stateValues = State.values();
652 for (int i = 0; i < stateValues.length; i++) {
653 if (stateValues[i].ordinal() == stateOrdinal) {
654 state = stateValues[i];
655 break;
656 }
657 }
658 return state;
659 }
660
661 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 * Restores the previous state, if it exists.
663 *
664 * @param savedState The previous state.
665 */
666 private void restoreState(Bundle savedState) {
667 if (savedState == null) {
668 return;
669 }
670
Michael Jurka883f55b2010-10-21 15:47:14 -0700671 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700672 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800673 showAllApps(false);
674 }
675
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
677 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700678 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
680
Winson Chung3d503fb2011-07-13 17:25:49 -0700681 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
682 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700683
Winson Chung3d503fb2011-07-13 17:25:49 -0700684 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
685 mPendingAddInfo.container = pendingAddContainer;
686 mPendingAddInfo.screen = pendingAddScreen;
687 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
688 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 mRestoring = true;
690 }
691
692 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
693 if (renameFolder) {
694 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700695 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 mRestoring = true;
697 }
Winson Chunga12a2502010-12-20 14:41:35 -0800698
Winson Chung785d2eb2011-04-14 16:08:02 -0700699
700 // Restore the AppsCustomize tab
701 if (mAppsCustomizeTabHost != null) {
702 String curTab = savedState.getString("apps_customize_currentTab");
703 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700704 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700705 mAppsCustomizeContent.setContentType(
706 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
707 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700708 mAppsCustomizeContent.loadAssociatedPages(
709 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700710 }
711
Winson Chung5afbf7b2011-07-25 11:53:08 -0700712 int currentIndex = savedState.getInt("apps_customize_currentIndex");
713 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700714 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800715 }
716
717 /**
718 * Finds all the views we need and configure them properly.
719 */
720 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700721 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400722
Winson Chung4c98d922011-05-31 16:50:48 -0700723 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
724 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800725
Winson Chung4c98d922011-05-31 16:50:48 -0700726 // Setup the drag layer
727 mDragLayer.setup(this, dragController);
728
Winson Chung3d503fb2011-07-13 17:25:49 -0700729 // Setup the hotseat
730 mHotseat = (Hotseat) findViewById(R.id.hotseat);
731 if (mHotseat != null) {
732 mHotseat.setup(this);
733 }
734
Winson Chung4c98d922011-05-31 16:50:48 -0700735 // Setup the workspace
736 mWorkspace.setHapticFeedbackEnabled(false);
737 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700738 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700739 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700740
Winson Chungf0ea4d32011-06-06 14:27:16 -0700741 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700742 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700743
Winson Chungf0ea4d32011-06-06 14:27:16 -0700744 // Setup AppsCustomize
745 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
746 findViewById(R.id.apps_customize_pane);
747 mAppsCustomizeContent = (AppsCustomizePagedView)
748 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
749 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400750
Michael Jurka5130e402011-10-13 04:55:35 -0700751 // Get the all apps button
752 mAllAppsButton = findViewById(R.id.all_apps_button);
753 if (mAllAppsButton != null) {
754 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
755 @Override
756 public boolean onTouch(View v, MotionEvent event) {
757 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
758 onTouchDownAllAppsButton(v);
759 }
760 return false;
761 }
762 });
763 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700764 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700765 dragController.setDragScoller(mWorkspace);
766 dragController.setScrollView(mDragLayer);
767 dragController.setMoveTarget(mWorkspace);
768 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700769 if (mSearchDropTargetBar != null) {
770 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800771 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 }
773
774 /**
775 * Creates a view representing a shortcut.
776 *
777 * @param info The data structure describing the shortcut.
778 *
779 * @return A View inflated from R.layout.application.
780 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800781 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800782 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700783 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 }
785
786 /**
787 * Creates a view representing a shortcut inflated from the specified resource.
788 *
789 * @param layoutResId The id of the XML layout used to create the shortcut.
790 * @param parent The group the shortcut belongs to.
791 * @param info The data structure describing the shortcut.
792 *
793 * @return A View inflated from layoutResId.
794 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800795 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800796 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
797 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 return favorite;
800 }
801
802 /**
803 * Add an application shortcut to the workspace.
804 *
805 * @param data The intent describing the application.
806 * @param cellInfo The position on screen where to create the shortcut.
807 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700808 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700809 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700810 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700811
Adam Cohenfbba09b2011-07-18 21:43:05 -0700812 // First we check if we already know the exact location where we want to add this item.
813 if (cellX >= 0 && cellY >= 0) {
814 cellXY[0] = cellX;
815 cellXY[1] = cellY;
816 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700817 showOutOfSpaceMessage();
818 return;
819 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800821 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800822
Romain Guy73b979d2009-06-09 12:57:21 -0700823 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800824 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800826 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700827 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700828 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800829 } else {
830 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 }
832 }
Romain Guycbb89e42009-06-08 15:52:54 -0700833
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800834 /**
835 * Add a shortcut to the workspace.
836 *
837 * @param data The intent describing the shortcut.
838 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700840 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
841 int cellY) {
842 int[] cellXY = mTmpAddItemCellCoordinates;
843 int[] touchXY = mPendingAddInfo.dropPos;
844 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700845
Michael Jurkad3ef3062010-11-23 16:23:58 -0800846 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700847
Adam Cohen558baaf2011-08-15 15:22:57 -0700848 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
849 final View view = createShortcut(info);
850
Adam Cohenfbba09b2011-07-18 21:43:05 -0700851 // First we check if we already know the exact location where we want to add this item.
852 if (cellX >= 0 && cellY >= 0) {
853 cellXY[0] = cellX;
854 cellXY[1] = cellY;
855 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700856
857 // If appropriate, either create a folder or add to an existing folder
858 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
859 true, null,null)) {
860 return;
861 }
862 DragObject dragObject = new DragObject();
863 dragObject.dragInfo = info;
864 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
865 return;
866 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700867 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800868 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700869 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800870 foundCellSpan = (result != null);
871 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700872 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800873 }
874
875 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700876 showOutOfSpaceMessage();
877 return;
878 }
879
Adam Cohen558baaf2011-08-15 15:22:57 -0700880 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881
882 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700883 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
884 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886 }
887
Adam Cohen50370f32011-08-25 18:57:14 -0700888 class Padding {
889 int left = 0;
890 int right = 0;
891 int top = 0;
892 int bottom = 0;
893 }
894
Adam Cohenf814aa02011-09-01 13:48:05 -0700895 Padding getPaddingForWidget(ComponentName component) {
Adam Cohen50370f32011-08-25 18:57:14 -0700896 PackageManager packageManager = getPackageManager();
897 Padding p = new Padding();
898 android.content.pm.ApplicationInfo appInfo;
899
900 try {
Adam Cohenf814aa02011-09-01 13:48:05 -0700901 appInfo = packageManager.getApplicationInfo(component.getPackageName(), 0);
Adam Cohen50370f32011-08-25 18:57:14 -0700902 } catch (Exception e) {
903 // if we can't find the package, return 0 padding
904 return p;
905 }
906
Adam Cohenff648b52011-09-19 11:57:30 -0700907 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Adam Cohen50370f32011-08-25 18:57:14 -0700908 Resources r = getResources();
Adam Cohen82ebbd22011-09-30 15:05:40 -0700909 // The default padding values are private API currently, but will be added in
910 // API level 15. The current values are (8, 8, 8, 8).
911 p.left = r.getDimensionPixelSize(com.android.internal.
912 R.dimen.default_app_widget_padding_left);
913 p.right = r.getDimensionPixelSize(com.android.internal.
914 R.dimen.default_app_widget_padding_right);
915 p.top = r.getDimensionPixelSize(com.android.internal.
916 R.dimen.default_app_widget_padding_top);
917 p.bottom = r.getDimensionPixelSize(com.android.internal.
918 R.dimen.default_app_widget_padding_bottom);
Adam Cohen50370f32011-08-25 18:57:14 -0700919 }
920
921 return p;
922 }
923
Adam Cohenf814aa02011-09-01 13:48:05 -0700924 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
925 if (spanXY == null) {
926 spanXY = new int[2];
927 }
928
929 Padding padding = getPaddingForWidget(component);
930 // We want to account for the extra amount of padding that we are adding to the widget
931 // to ensure that it gets the full amount of space that it has requested
932 int requiredWidth = minWidth + padding.left + padding.right;
933 int requiredHeight = minHeight + padding.top + padding.bottom;
934 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
935 }
936
937 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
938 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
939 }
940
Adam Cohencbf47e32011-09-16 17:32:37 -0700941 int[] getMinResizeSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
942 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
943 }
944
Adam Cohenf814aa02011-09-01 13:48:05 -0700945 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
946 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
947 }
948
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800949 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700950 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800951 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700952 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700953 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700955 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700956 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700957
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700958 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700959 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700960
Adam Cohenf814aa02011-09-01 13:48:05 -0700961 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700962
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700964 // We have saved the position to which the widget was dragged-- this really only matters
965 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700966 int[] cellXY = mTmpAddItemCellCoordinates;
967 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700968 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700969 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
970 cellXY[0] = mPendingAddInfo.cellX;
971 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700972 foundCellSpan = true;
973 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700974 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700975 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700976 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800977 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700978 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700979 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700980 }
981
Michael Jurka0280c3b2010-09-17 15:00:07 -0700982 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800983 if (appWidgetId != -1) {
984 // Deleting an app widget ID is a void call but writes to disk before returning
985 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800986 new Thread("deleteAppWidgetId") {
987 public void run() {
988 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
989 }
990 }.start();
991 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700992 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800993 return;
994 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700996 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -0700997 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700998 launcherInfo.spanX = spanXY[0];
999 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003
1004 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001006 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001007
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001008 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001010
Winson Chung3d503fb2011-07-13 17:25:49 -07001011 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001012 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001013
1014 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 }
1016 }
Romain Guycbb89e42009-06-08 15:52:54 -07001017
Adam Cohended9f8d2010-11-03 13:25:16 -07001018 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1019 @Override
1020 public void onReceive(Context context, Intent intent) {
1021 final String action = intent.getAction();
1022 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1023 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001024 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001025 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001026
Winson Chungc100e8e2011-08-09 16:02:43 -07001027 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001028 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001029 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1030 mAppsCustomizeTabHost.reset();
1031 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001032 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001033 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1034 mUserPresent = true;
1035 updateRunning();
1036 }
1037 }
1038 };
1039
1040 @Override
1041 public void onAttachedToWindow() {
1042 super.onAttachedToWindow();
1043
1044 // Listen for broadcasts related to user-presence
1045 final IntentFilter filter = new IntentFilter();
1046 filter.addAction(Intent.ACTION_SCREEN_OFF);
1047 filter.addAction(Intent.ACTION_USER_PRESENT);
1048 registerReceiver(mReceiver, filter);
1049
Adam Cohend113e0c2010-11-11 10:48:05 -08001050 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001051 mVisible = true;
1052 }
1053
1054 @Override
1055 public void onDetachedFromWindow() {
1056 super.onDetachedFromWindow();
1057 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001058 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001059
Adam Cohend113e0c2010-11-11 10:48:05 -08001060 if (mAttached) {
1061 unregisterReceiver(mReceiver);
1062 mAttached = false;
1063 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001064 updateRunning();
1065 }
1066
1067 public void onWindowVisibilityChanged(int visibility) {
1068 mVisible = visibility == View.VISIBLE;
1069 updateRunning();
1070 }
1071
1072 private void sendAdvanceMessage(long delay) {
1073 mHandler.removeMessages(ADVANCE_MSG);
1074 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1075 mHandler.sendMessageDelayed(msg, delay);
1076 mAutoAdvanceSentTime = System.currentTimeMillis();
1077 }
1078
1079 private void updateRunning() {
1080 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1081 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1082 mAutoAdvanceRunning = autoAdvanceRunning;
1083 if (autoAdvanceRunning) {
1084 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1085 sendAdvanceMessage(delay);
1086 } else {
1087 if (!mWidgetsToAdvance.isEmpty()) {
1088 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1089 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1090 }
1091 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001092 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001093 }
1094 }
1095 }
1096
1097 private final Handler mHandler = new Handler() {
1098 @Override
1099 public void handleMessage(Message msg) {
1100 if (msg.what == ADVANCE_MSG) {
1101 int i = 0;
1102 for (View key: mWidgetsToAdvance.keySet()) {
1103 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1104 final int delay = mAdvanceStagger * i;
1105 if (v instanceof Advanceable) {
1106 postDelayed(new Runnable() {
1107 public void run() {
1108 ((Advanceable) v).advance();
1109 }
1110 }, delay);
1111 }
1112 i++;
1113 }
1114 sendAdvanceMessage(mAdvanceInterval);
1115 }
1116 }
1117 };
1118
1119 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001120 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001121 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1122 if (v instanceof Advanceable) {
1123 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001124 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001125 updateRunning();
1126 }
1127 }
1128
1129 void removeWidgetToAutoAdvance(View hostView) {
1130 if (mWidgetsToAdvance.containsKey(hostView)) {
1131 mWidgetsToAdvance.remove(hostView);
1132 updateRunning();
1133 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001134 }
1135
Joe Onorato9c1289c2009-08-17 11:03:03 -04001136 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001137 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001138 launcherInfo.hostView = null;
1139 }
1140
Adam Cohended9f8d2010-11-03 13:25:16 -07001141 void showOutOfSpaceMessage() {
1142 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1143 }
1144
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001145 public LauncherAppWidgetHost getAppWidgetHost() {
1146 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001147 }
Romain Guycbb89e42009-06-08 15:52:54 -07001148
Winson Chunga9abd0e2010-10-27 17:18:37 -07001149 public LauncherModel getModel() {
1150 return mModel;
1151 }
1152
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001153 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001154 getWindow().closeAllPanels();
1155
Winson Chung87acb482011-08-23 17:28:05 -07001156 /**
1157 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001158 try {
1159 dismissDialog(DIALOG_CREATE_SHORTCUT);
1160 // Unlock the workspace if the dialog was showing
1161 } catch (Exception e) {
1162 // An exception is thrown if the dialog is not visible, which is fine
1163 }
1164
1165 try {
1166 dismissDialog(DIALOG_RENAME_FOLDER);
1167 // Unlock the workspace if the dialog was showing
1168 } catch (Exception e) {
1169 // An exception is thrown if the dialog is not visible, which is fine
1170 }
Winson Chung87acb482011-08-23 17:28:05 -07001171 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001172
1173 // Whatever we were doing is hereby canceled.
1174 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001175 }
1176
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 @Override
1178 protected void onNewIntent(Intent intent) {
1179 super.onNewIntent(intent);
1180
1181 // Close the menu
1182 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001183 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001184 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001185
Joe Onorato14f122b2009-11-19 14:06:36 -08001186 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1187 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001188
Adam Cohenac56cff2011-09-28 20:45:37 -07001189 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001190 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001191 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001192 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1193 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001194 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001195 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001196
1197 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001198 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001199 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001200
Joe Onorato3a8820b2009-11-10 15:06:42 -08001201 final View v = getWindow().peekDecorView();
1202 if (v != null && v.getWindowToken() != null) {
1203 InputMethodManager imm = (InputMethodManager)getSystemService(
1204 INPUT_METHOD_SERVICE);
1205 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001207
Michael Jurka35aa14d2011-07-07 17:01:08 -07001208 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001209 if (mAppsCustomizeTabHost != null) {
1210 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001212 }
1213 }
1214
1215 @Override
1216 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1217 // Do not call super here
1218 mSavedInstanceState = savedInstanceState;
1219 }
1220
1221 @Override
1222 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001223 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001224 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001225
Michael Jurka883f55b2010-10-21 15:47:14 -07001226 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001227 // We close any open folder since it will not be re-opened, and we need to make sure
1228 // this state is reflected.
1229 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001230
Winson Chung3d503fb2011-07-13 17:25:49 -07001231 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1232 mWaitingForResult) {
1233 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1234 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1235 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1236 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001237 }
1238
1239 if (mFolderInfo != null && mWaitingForResult) {
1240 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1241 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1242 }
Michael Jurka946ad472010-07-09 18:05:18 -07001243
Winson Chung785d2eb2011-04-14 16:08:02 -07001244 // Save the current AppsCustomize tab
1245 if (mAppsCustomizeTabHost != null) {
1246 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1247 if (currentTabTag != null) {
1248 outState.putString("apps_customize_currentTab", currentTabTag);
1249 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001250 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1251 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001252 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 }
1254
1255 @Override
1256 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001258
Winson Chunge7a03942011-08-05 15:05:12 -07001259 // Remove all pending runnables
1260 mHandler.removeMessages(ADVANCE_MSG);
1261 mHandler.removeMessages(0);
1262
Winson Chungcd2b0142011-06-08 16:02:26 -07001263 // Stop callbacks from LauncherModel
1264 LauncherApplication app = ((LauncherApplication) getApplication());
1265 mModel.stopLoader();
1266 app.setLauncher(null);
1267
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001269 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001271 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001272 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001273 mAppWidgetHost = null;
1274
1275 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276
1277 TextKeyListener.getInstance().release();
1278
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001279
Winson Chung3d503fb2011-07-13 17:25:49 -07001280 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001281
1282 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001283 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001284
1285 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1286 mWorkspace.removeAllViews();
1287 mWorkspace = null;
1288 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001289
1290 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 }
1292
Adam Cohena9cf38f2011-05-02 15:36:58 -07001293 public DragController getDragController() {
1294 return mDragController;
1295 }
1296
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 @Override
1298 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001299 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001300 super.startActivityForResult(intent, requestCode);
1301 }
1302
Winson Chung70d72102011-08-12 11:24:35 -07001303 /**
1304 * Indicates that we want global search for this activity by setting the globalSearch
1305 * argument for {@link #startSearch} to true.
1306 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001308 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001310
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001311 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001312
Karl Rosaen138a0412009-04-23 19:00:21 -07001313 if (initialQuery == null) {
1314 // Use any text typed in the launcher as the initial query
1315 initialQuery = getTypedText();
1316 clearTypedText();
1317 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 if (appSearchData == null) {
1319 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001320 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
Romain Guycbb89e42009-06-08 15:52:54 -07001322
Karl Rosaen138a0412009-04-23 19:00:21 -07001323 final SearchManager searchManager =
1324 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001325 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Romain Guycbb89e42009-06-08 15:52:54 -07001326 appSearchData, globalSearch);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001327 }
1328
Winson Chung70d72102011-08-12 11:24:35 -07001329 @Override
1330 public boolean onCreateOptionsMenu(Menu menu) {
1331 if (isWorkspaceLocked()) {
1332 return false;
1333 }
1334
1335 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001336
1337 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1338 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1339 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001340 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1341 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1342 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001343 String helpUrl = getString(R.string.help_url);
1344 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001345 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1346 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1347
Winson Chung70d72102011-08-12 11:24:35 -07001348 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1349 .setIcon(android.R.drawable.ic_menu_gallery)
1350 .setAlphabeticShortcut('W');
1351 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1352 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001353 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001354 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001355 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1356 .setIcon(android.R.drawable.ic_menu_preferences)
1357 .setIntent(settings)
1358 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001359 if (!helpUrl.isEmpty()) {
1360 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1361 .setIcon(android.R.drawable.ic_menu_help)
1362 .setIntent(help)
1363 .setAlphabeticShortcut('H');
1364 }
Winson Chung70d72102011-08-12 11:24:35 -07001365 return true;
1366 }
1367
1368 @Override
1369 public boolean onPrepareOptionsMenu(Menu menu) {
1370 super.onPrepareOptionsMenu(menu);
1371
1372 if (mAppsCustomizeTabHost.isTransitioning()) {
1373 return false;
1374 }
1375 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1376 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1377
1378 return true;
1379 }
1380
1381 @Override
1382 public boolean onOptionsItemSelected(MenuItem item) {
1383 switch (item.getItemId()) {
1384 case MENU_WALLPAPER_SETTINGS:
1385 startWallpaper();
1386 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001387 }
1388
1389 return super.onOptionsItemSelected(item);
1390 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001392 @Override
1393 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001394 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001395 // Use a custom animation for launching search
1396 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001397 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001398 }
1399
Joe Onorato9c1289c2009-08-17 11:03:03 -04001400 public boolean isWorkspaceLocked() {
1401 return mWorkspaceLoading || mWaitingForResult;
1402 }
1403
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001405 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001406 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 }
1408
Michael Jurka0280c3b2010-09-17 15:00:07 -07001409 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001410 mPendingAddInfo.container = ItemInfo.NO_ID;
1411 mPendingAddInfo.screen = -1;
1412 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1413 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1414 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001415 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001416
Michael Jurkaaf442092010-06-10 17:01:57 -07001417 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001418 // TODO: catch bad widget exception when sent
1419 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001420 // TODO: Is this log message meaningful?
1421 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001422 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001423 }
1424
Winson Chung55cef262010-10-28 14:14:18 -07001425 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001426 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427
Bjorn Bringert7984c942009-12-09 15:38:25 +00001428 if (appWidget.configure != null) {
1429 // Launch over to configure widget, if needed
1430 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1431 intent.setComponent(appWidget.configure);
1432 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001433 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001434 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1435 intent.putExtra(
1436 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1437 info.mimeType);
1438
1439 final String mimeType = info.mimeType;
1440 final ClipData clipData = (ClipData) info.configurationData;
1441 final ClipDescription clipDesc = clipData.getDescription();
1442 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1443 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001444 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001445 final CharSequence stringData = item.getText();
1446 final Uri uriData = item.getUri();
1447 final Intent intentData = item.getIntent();
1448 final String key =
1449 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1450 if (uriData != null) {
1451 intent.putExtra(key, uriData);
1452 } else if (intentData != null) {
1453 intent.putExtra(key, intentData);
1454 } else if (stringData != null) {
1455 intent.putExtra(key, stringData);
1456 }
1457 break;
1458 }
1459 }
1460 }
Winson Chung55cef262010-10-28 14:14:18 -07001461 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001462
Romain Guy8e633c52010-03-04 12:51:36 -08001463 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001465 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001466 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001467
1468 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001469 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 }
1471 }
Romain Guycbb89e42009-06-08 15:52:54 -07001472
Adam Cohenfbba09b2011-07-18 21:43:05 -07001473 /**
1474 * Process a shortcut drop.
1475 *
1476 * @param componentName The name of the component
1477 * @param screen The screen where it should be added
1478 * @param cell The cell it should be added to, optional
1479 * @param position The location on the screen where it was dropped, optional
1480 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001481 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1482 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001483 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001484 mPendingAddInfo.container = container;
1485 mPendingAddInfo.screen = screen;
1486 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001487
1488 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001489 mPendingAddInfo.cellX = cell[0];
1490 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001491 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001492
1493 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1494 createShortcutIntent.setComponent(componentName);
1495 processShortcut(createShortcutIntent);
1496 }
1497
Adam Cohenfbba09b2011-07-18 21:43:05 -07001498 /**
1499 * Process a widget drop.
1500 *
1501 * @param info The PendingAppWidgetInfo of the widget being added.
1502 * @param screen The screen where it should be added
1503 * @param cell The cell it should be added to, optional
1504 * @param position The location on the screen where it was dropped, optional
1505 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001506 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1507 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001508 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001509 mPendingAddInfo.container = info.container = container;
1510 mPendingAddInfo.screen = info.screen = screen;
1511 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001512 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001513 mPendingAddInfo.cellX = cell[0];
1514 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001515 }
1516
1517 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1518 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1519 addAppWidgetImpl(appWidgetId, info);
1520 }
1521
Joe Onoratodeb98af2010-02-19 14:59:39 -08001522 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001523 // Handle case where user selected "Applications"
1524 String applicationName = getResources().getString(R.string.group_applications);
1525 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001526
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001527 if (applicationName != null && applicationName.equals(shortcutName)) {
1528 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1529 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001530
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001531 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1532 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001533 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001534 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001535 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001536 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001537 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001538 }
1539
Winson Chung24ab2f12010-09-16 14:10:47 -07001540 void processWallpaper(Intent intent) {
1541 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1542 }
1543
Winson Chung3d503fb2011-07-13 17:25:49 -07001544 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1545 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001546 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 folderInfo.title = getText(R.string.folder_name);
1548
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001550 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1551 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001552 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553
1554 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001555 FolderIcon newFolder =
1556 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1557 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1558 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001559 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 }
Romain Guycbb89e42009-06-08 15:52:54 -07001561
Joe Onorato9c1289c2009-08-17 11:03:03 -04001562 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001563 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001564 }
1565
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 private void showNotifications() {
1567 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1568 if (statusBar != null) {
1569 statusBar.expand();
1570 }
1571 }
1572
1573 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001574 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001575 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001576 Intent chooser = Intent.createChooser(pickWallpaper,
1577 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001578 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1579 // Removed in Eclair MR1
1580// WallpaperManager wm = (WallpaperManager)
1581// getSystemService(Context.WALLPAPER_SERVICE);
1582// WallpaperInfo wi = wm.getWallpaperInfo();
1583// if (wi != null && wi.getSettingsActivity() != null) {
1584// LabeledIntent li = new LabeledIntent(getPackageName(),
1585// R.string.configure_wallpaper, 0);
1586// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1587// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1588// }
Mike Clerona0618e42009-10-22 13:55:21 -07001589 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 }
1591
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001592 /**
1593 * Registers various content observers. The current implementation registers
1594 * only a favorites observer to keep track of the favorites applications.
1595 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001596 private void registerContentObservers() {
1597 ContentResolver resolver = getContentResolver();
1598 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1599 true, mWidgetObserver);
1600 }
1601
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 @Override
1603 public boolean dispatchKeyEvent(KeyEvent event) {
1604 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1605 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001607 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001608 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001609 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001610 dumpState();
1611 return true;
1612 }
1613 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001614 }
1615 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1616 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001617 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618 return true;
1619 }
1620 }
1621
1622 return super.dispatchKeyEvent(event);
1623 }
1624
Joe Onorato88ec0992009-11-19 13:16:06 -08001625 @Override
1626 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001627 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001628 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001629 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001630 Folder openFolder = mWorkspace.getOpenFolder();
1631 if (openFolder.isEditingName()) {
1632 openFolder.dismissEditingName();
1633 } else {
1634 closeFolder();
1635 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001636 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001637 mWorkspace.exitWidgetResizeMode();
1638
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001639 // Back button is a no-op here, but give at least some feedback for the button press
1640 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001641 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001642 }
1643
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001645 * Re-listen when widgets are reset.
1646 */
1647 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001648 if (mAppWidgetHost != null) {
1649 mAppWidgetHost.startListening();
1650 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001651 }
1652
1653 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001654 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1655 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001656 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001657 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001658 if (mModel != null) {
1659 mModel.unbindWorkspaceItems();
1660 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 /**
1664 * Launches the intent referred by the clicked shortcut.
1665 *
1666 * @param v The view representing the clicked shortcut.
1667 */
1668 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001669 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1670 // view has detached (it's possible for this to happen if the view is removed mid touch).
1671 if (v.getWindowToken() == null) {
1672 return;
1673 }
1674
1675 if (mWorkspace.isSwitchingState()) {
1676 return;
1677 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001678
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001680 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001682 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001683 int[] pos = new int[2];
1684 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001685 intent.setSourceBounds(new Rect(pos[0], pos[1],
1686 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001687 boolean success = startActivitySafely(intent, tag);
1688
1689 if (success && v instanceof BubbleTextView) {
1690 mWaitingForResume = (BubbleTextView) v;
1691 mWaitingForResume.setStayPressed(true);
1692 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001694 if (v instanceof FolderIcon) {
1695 FolderIcon fi = (FolderIcon) v;
1696 handleFolderClick(fi);
1697 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001698 } else if (v == mAllAppsButton) {
1699 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001700 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001701 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001702 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001703 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 }
1705 }
1706
Michael Jurka0e260592010-06-30 17:07:39 -07001707 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001708 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001709 // clicking anywhere on the workspace causes the customization drawer to slide down
1710 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001711 return false;
1712 }
1713
Michael Jurkaaf442092010-06-10 17:01:57 -07001714 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001715 * Event handler for the search button
1716 *
1717 * @param v The view that was clicked.
1718 */
1719 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001720 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001721 }
1722
1723 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001724 * Event handler for the voice button
1725 *
1726 * @param v The view that was clicked.
1727 */
1728 public void onClickVoiceButton(View v) {
1729 startVoiceSearch();
1730 }
1731
1732 private void startVoiceSearch() {
1733 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001734 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001735 startActivity(intent);
1736 }
1737
1738 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001739 * Event handler for the "grid" button that appears on the home screen, which
1740 * enters all apps mode.
1741 *
1742 * @param v The view that was clicked.
1743 */
1744 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001745 showAllApps(true);
1746 }
1747
1748 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001749 // Provide the same haptic feedback that the system offers for virtual keys.
1750 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001751 }
1752
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001753 public void onClickAppMarketButton(View v) {
1754 if (mAppMarketIntent != null) {
1755 startActivitySafely(mAppMarketIntent, "app market");
1756 }
1757 }
1758
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001759 void startApplicationDetailsActivity(ComponentName componentName) {
1760 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001761 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1762 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001763 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001764 startActivity(intent);
1765 }
1766
Patrick Dubroy5539af72010-09-07 15:22:01 -07001767 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1768 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1769 // System applications cannot be installed. For now, show a toast explaining that.
1770 // We may give them the option of disabling apps this way.
1771 int messageId = R.string.uninstall_system_app_text;
1772 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1773 } else {
1774 String packageName = appInfo.componentName.getPackageName();
1775 String className = appInfo.componentName.getClassName();
1776 Intent intent = new Intent(
1777 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001778 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1779 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001780 startActivity(intent);
1781 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001782 }
1783
Michael Jurkaddd62e92011-02-16 17:49:14 -08001784 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1786 try {
1787 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001788 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 } catch (ActivityNotFoundException e) {
1790 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001791 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 } catch (SecurityException e) {
1793 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001794 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001795 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001796 "or use the exported attribute for this activity. "
1797 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001799 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001801
Romain Guy8e633c52010-03-04 12:51:36 -08001802 void startActivityForResultSafely(Intent intent, int requestCode) {
1803 try {
1804 startActivityForResult(intent, requestCode);
1805 } catch (ActivityNotFoundException e) {
1806 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1807 } catch (SecurityException e) {
1808 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1809 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1810 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1811 "or use the exported attribute for this activity.", e);
1812 }
1813 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814
Adam Cohena9cf38f2011-05-02 15:36:58 -07001815 private void handleFolderClick(FolderIcon folderIcon) {
1816 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001817 Folder openFolder = mWorkspace.getFolderForTag(info);
1818
1819 // If the folder info reports that the associated folder is open, then verify that
1820 // it is actually opened. There have been a few instances where this gets out of sync.
1821 if (info.opened && openFolder == null) {
1822 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1823 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1824 info.opened = false;
1825 }
1826
Adam Cohena9cf38f2011-05-02 15:36:58 -07001827 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 // Close any open folder
1829 closeFolder();
1830 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001831 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 } else {
1833 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 int folderScreen;
1835 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001836 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 // .. and close it
1838 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001839 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 // Close any folder open on the current screen
1841 closeFolder();
1842 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001843 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 }
1845 }
1846 }
1847 }
1848
Adam Cohen2801caf2011-05-13 20:57:39 -07001849 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001850 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001851 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1852 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1853 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1854
Adam Cohenc51934b2011-07-26 21:07:43 -07001855 FolderInfo info = (FolderInfo) fi.getTag();
1856 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1857 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001858 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1859 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001860 }
1861
Adam Cohen2801caf2011-05-13 20:57:39 -07001862 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1863 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1864 oa.start();
1865 }
1866
1867 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001868 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001869 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1870 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1871 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1872
Adam Cohenc51934b2011-07-26 21:07:43 -07001873 FolderInfo info = (FolderInfo) fi.getTag();
1874 CellLayout cl = null;
1875 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1876 cl = (CellLayout) fi.getParent().getParent();
1877 }
1878
1879 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001880 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1881 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001882 oa.addListener(new AnimatorListenerAdapter() {
1883 @Override
1884 public void onAnimationEnd(Animator animation) {
1885 if (layout != null) {
1886 layout.clearFolderLeaveBehind();
1887 }
1888 }
1889 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001890 oa.start();
1891 }
1892
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001894 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 * is animated relative to the specified View. If the View is null, no animation
1896 * is played.
1897 *
1898 * @param folderInfo The FolderInfo describing the folder to open.
1899 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001900 public void openFolder(FolderIcon folderIcon) {
1901 Folder folder = folderIcon.mFolder;
1902 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903
Adam Cohen2801caf2011-05-13 20:57:39 -07001904 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001905 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906
Adam Cohen4554ee12011-08-03 16:13:21 -07001907 // Just verify that the folder hasn't already been added to the DragLayer.
1908 // There was a one-off crash where the folder had a parent already.
1909 if (folder.getParent() == null) {
1910 mDragLayer.addView(folder);
1911 mDragController.addDropTarget((DropTarget) folder);
1912 } else {
1913 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1914 folder.getParent() + ").");
1915 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001916 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001917 }
1918
1919 public void closeFolder() {
1920 Folder folder = mWorkspace.getOpenFolder();
1921 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07001922 if (folder.isEditingName()) {
1923 folder.dismissEditingName();
1924 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001925 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07001926
1927 // Dismiss the folder cling
1928 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07001929 }
1930 }
1931
1932 void closeFolder(Folder folder) {
1933 folder.getInfo().opened = false;
1934
1935 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1936 if (parent != null) {
1937 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1938 shrinkAndFadeInFolderIcon(fi);
1939 }
1940 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 }
1942
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001943 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001944 if (mState != State.WORKSPACE) {
1945 return false;
1946 }
1947
Joe Onorato9c1289c2009-08-17 11:03:03 -04001948 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001949 return false;
1950 }
1951
1952 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001953 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001954 }
1955
Michael Jurka0280c3b2010-09-17 15:00:07 -07001956 resetAddInfo();
1957 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001959 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 return true;
1961 }
1962
Winson Chung3d503fb2011-07-13 17:25:49 -07001963 // The hotseat touch handling does not go through Workspace, and we always allow long press
1964 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001965 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001966 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1967 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001968 if (itemUnderLongClick == null) {
1969 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001970 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1971 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001972 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001974 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001976 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 }
1978 }
1979 }
1980 return true;
1981 }
1982
Winson Chung3d503fb2011-07-13 17:25:49 -07001983 boolean isHotseatLayout(View layout) {
1984 return mHotseat != null && layout != null &&
1985 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1986 }
1987 Hotseat getHotseat() {
1988 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001989 }
1990
Winson Chung3d503fb2011-07-13 17:25:49 -07001991 /**
1992 * Returns the CellLayout of the specified container at the specified screen.
1993 */
1994 CellLayout getCellLayout(long container, int screen) {
1995 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1996 if (mHotseat != null) {
1997 return mHotseat.getLayout();
1998 } else {
1999 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002000 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002001 } else {
2002 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002003 }
2004 }
2005
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 Workspace getWorkspace() {
2007 return mWorkspace;
2008 }
2009
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 @Override
2011 protected Dialog onCreateDialog(int id) {
2012 switch (id) {
2013 case DIALOG_CREATE_SHORTCUT:
2014 return new CreateShortcut().createDialog();
2015 case DIALOG_RENAME_FOLDER:
2016 return new RenameFolder().createDialog();
2017 }
2018
2019 return super.onCreateDialog(id);
2020 }
2021
2022 @Override
2023 protected void onPrepareDialog(int id, Dialog dialog) {
2024 switch (id) {
2025 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 break;
2027 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002028 if (mFolderInfo != null) {
2029 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2030 final CharSequence text = mFolderInfo.title;
2031 input.setText(text);
2032 input.setSelection(0, text.length());
2033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 break;
2035 }
2036 }
2037
2038 void showRenameDialog(FolderInfo info) {
2039 mFolderInfo = info;
2040 mWaitingForResult = true;
2041 showDialog(DIALOG_RENAME_FOLDER);
2042 }
2043
Adam Cohenfbba09b2011-07-18 21:43:05 -07002044 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002045 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002046 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2047 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 mWaitingForResult = true;
2049 showDialog(DIALOG_CREATE_SHORTCUT);
2050 }
2051
2052 private class RenameFolder {
2053 private EditText mInput;
2054
2055 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2057 mInput = (EditText) layout.findViewById(R.id.folder_name);
2058
2059 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2060 builder.setIcon(0);
2061 builder.setTitle(getString(R.string.rename_folder_title));
2062 builder.setCancelable(true);
2063 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2064 public void onCancel(DialogInterface dialog) {
2065 cleanup();
2066 }
2067 });
2068 builder.setNegativeButton(getString(R.string.cancel_action),
2069 new Dialog.OnClickListener() {
2070 public void onClick(DialogInterface dialog, int which) {
2071 cleanup();
2072 }
2073 }
2074 );
2075 builder.setPositiveButton(getString(R.string.rename_action),
2076 new Dialog.OnClickListener() {
2077 public void onClick(DialogInterface dialog, int which) {
2078 changeFolderName();
2079 }
2080 }
2081 );
2082 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002083
2084 final AlertDialog dialog = builder.create();
2085 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2086 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002087 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002088 mInput.requestFocus();
2089 InputMethodManager inputManager = (InputMethodManager)
2090 getSystemService(Context.INPUT_METHOD_SERVICE);
2091 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002092 }
2093 });
2094
2095 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 }
2097
2098 private void changeFolderName() {
2099 final String name = mInput.getText().toString();
2100 if (!TextUtils.isEmpty(name)) {
2101 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002102 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 mFolderInfo.title = name;
2104 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2105
Joe Onorato9c1289c2009-08-17 11:03:03 -04002106 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002107 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002108 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 } else {
2110 final FolderIcon folderIcon = (FolderIcon)
2111 mWorkspace.getViewForTag(mFolderInfo);
2112 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002113 // TODO: At some point we'll probably want some version of setting
2114 // the text for a folder icon.
2115 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 getWorkspace().requestLayout();
2117 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002118 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002119 mWorkspaceLoading = true;
2120 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 }
2122 }
2123 }
2124 cleanup();
2125 }
2126
2127 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002128 dismissDialog(DIALOG_RENAME_FOLDER);
2129 mWaitingForResult = false;
2130 mFolderInfo = null;
2131 }
2132 }
2133
Daniel Sandler843e8602010-06-07 14:59:01 -04002134 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2135 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002136 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002137 }
2138
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002139 // AllAppsView.Watcher
2140 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002141 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002142 mWorkspace.setVisibility(View.GONE);
2143 }
2144 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002145
2146 /**
2147 * Helper method for the cameraZoomIn/cameraZoomOut animations
2148 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002149 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002150 * @param scaleFactor The scale factor used for the zoom
2151 */
2152 private void setPivotsForZoom(View view, State state, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002153 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002154 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002155 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002156
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002157 void updateWallpaperVisibility(boolean visible) {
2158 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2159 int curflags = getWindow().getAttributes().flags
2160 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2161 if (wpflags != curflags) {
2162 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2163 }
2164 }
2165
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002166 /**
2167 * Zoom the camera out from the workspace to reveal 'toView'.
2168 * Assumes that the view to show is anchored at either the very top or very bottom
2169 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002170 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002171 */
Winson Chungc07918d2011-07-01 15:35:26 -07002172 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002173 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002174 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002175
Winson Chungf0ea4d32011-06-06 14:27:16 -07002176 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2177 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2178 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2179 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002180 final int startDelay =
2181 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002182
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002183 setPivotsForZoom(toView, toState, scale);
2184
Michael Jurkad74c9842011-07-10 12:44:21 -07002185 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002186 mWorkspace.changeState(Workspace.State.SMALL, animated);
Adam Cohencff6af82011-09-13 14:51:53 -07002187 //hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002188
2189 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002190 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002191 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002192 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2193 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002194 ((View) toView.getParent()).invalidate();
2195 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002196 toView.setFastScaleX(a * scale + b * 1f);
2197 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002198 }
2199 });
Adam Cohen61033d32010-11-15 18:29:44 -08002200
Winson Chungf0ea4d32011-06-06 14:27:16 -07002201 toView.setVisibility(View.VISIBLE);
2202 toView.setFastAlpha(0f);
2203 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2204 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2205 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2206 public void onAnimationUpdate(float a, float b) {
2207 // don't need to invalidate because we do so above
2208 toView.setFastAlpha(a * 0f + b * 1f);
2209 }
2210 });
Adam Cohencff6af82011-09-13 14:51:53 -07002211 alphaAnim.setStartDelay(startDelay);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002212 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002213
Michael Jurkaabded662011-03-04 12:06:57 -08002214 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002215 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, scaleAnim,
2216 false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002217 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002218 scaleAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002219 boolean animationCancelled = false;
2220
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002221 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002222 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002223 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002224 // Prepare the position
2225 toView.setTranslationX(0.0f);
2226 toView.setTranslationY(0.0f);
2227 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002228 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002229 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002230 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002231 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002232 // If we don't set the final scale values here, if this animation is cancelled
2233 // it will have the wrong scale value and subsequent cameraPan animations will
2234 // not fix that
2235 toView.setScaleX(1.0f);
2236 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002237 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002238 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance,
2239 scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002240 }
Winson Chung32174c82011-07-19 15:47:55 -07002241
2242 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2243 // Hide the workspace scrollbar
2244 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002245 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002246 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002247 if (!animationCancelled) {
2248 updateWallpaperVisibility(false);
2249 }
2250 }
2251
Adam Cohencff6af82011-09-13 14:51:53 -07002252 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002253 public void onAnimationCancel(Animator animation) {
2254 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002255 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002256 });
2257
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002258 // toView should appear right at the end of the workspace shrink animation
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002259
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002260 if (mStateAnimation != null) mStateAnimation.cancel();
2261 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002262 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002263 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002264 } else {
2265 toView.setTranslationX(0.0f);
2266 toView.setTranslationY(0.0f);
2267 toView.setScaleX(1.0f);
2268 toView.setScaleY(1.0f);
2269 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002270 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002271 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002272 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, null, false);
2273 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance, null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002274
Winson Chungc07918d2011-07-01 15:35:26 -07002275 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2276 // Hide the workspace scrollbar
2277 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002278 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002279 }
Michael Jurkaabded662011-03-04 12:06:57 -08002280 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002281 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002282 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002283 }
2284
2285 /**
2286 * Zoom the camera back into the workspace, hiding 'fromView'.
2287 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002288 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002289 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002290 */
Winson Chungc07918d2011-07-01 15:35:26 -07002291 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002292 Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002293 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002294
Winson Chungf0ea4d32011-06-06 14:27:16 -07002295 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2296 final float scaleFactor = (float)
2297 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2298 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002299
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002300 setPivotsForZoom(fromView, fromState, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002301 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002302 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002303 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002304 if (mStateAnimation != null) mStateAnimation.cancel();
2305 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002306
Michael Jurka742574b2011-02-02 23:51:01 -08002307 final float oldScaleX = fromView.getScaleX();
2308 final float oldScaleY = fromView.getScaleY();
2309
2310 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2311 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002312 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2313 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002314 ((View)fromView.getParent()).fastInvalidate();
2315 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2316 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2317 }
2318 });
Michael Jurkaabded662011-03-04 12:06:57 -08002319 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002320 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Adam Cohencff6af82011-09-13 14:51:53 -07002321 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002322 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2323 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002324 // don't need to invalidate because we do so above
2325 fromView.setFastAlpha(a * 1f + b * 0f);
2326 }
2327 });
Michael Jurkaabded662011-03-04 12:06:57 -08002328 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002329 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, alphaAnim,
2330 true);
Michael Jurka2763be32011-02-24 11:19:57 -08002331 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002332 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002333 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002334 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002335 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002336 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002337 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002338 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance,
2339 alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002340 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002341 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002342 }
2343 });
2344
Winson Chungf0ea4d32011-06-06 14:27:16 -07002345 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002346 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002347 } else {
2348 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002349 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002350 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, null, true);
2351 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance, null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002352 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002353 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002354 }
2355
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002356 void showWorkspace(boolean animated) {
Adam Cohencff6af82011-09-13 14:51:53 -07002357 Resources res = getResources();
2358 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2359
2360 mWorkspace.changeState(Workspace.State.NORMAL, animated, stagger);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002361 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002362 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002363 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002364
Adam Cohen7777d962011-08-18 18:58:38 -07002365 mWorkspace.showDockDivider(!animated);
2366 mWorkspace.flashScrollingIndicator();
2367
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002368 // Change the state *after* we've called all the transition code
2369 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002370
Winson Chung5fb63472011-02-02 17:03:37 -08002371 // Resume the auto-advance of widgets
2372 mUserPresent = true;
2373 updateRunning();
2374
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002375 // send an accessibility event to announce the context change
2376 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002377 }
2378
Adam Cohen7777d962011-08-18 18:58:38 -07002379 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002380 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002381 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Winson Chungb26f3d62011-06-02 10:49:29 -07002382 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002383 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002384 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002385 }
Adam Cohen7777d962011-08-18 18:58:38 -07002386
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002387 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chung09bfc452011-09-09 11:30:20 -07002388 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2389
Winson Chunge7a03942011-08-05 15:05:12 -07002390 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002391 @Override
2392 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002393 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002394 // Before we show workspace, hide all apps again because
2395 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2396 // clean up our state transition functions
2397 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002398 mSearchDropTargetBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002399 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002400 } else {
2401 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002402 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002403 }
2404 }, (extendedDelay ?
2405 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2406 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2407 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002408 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002409 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002410 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2411 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002412 }
2413 // Otherwise, we are not in spring loaded mode, so don't do anything.
2414 }
2415
Adam Cohenfc53cd22011-07-20 15:45:11 -07002416 public boolean isAllAppsCustomizeOpen() {
2417 return mState == State.APPS_CUSTOMIZE;
2418 }
2419
Winson Chungf0ea4d32011-06-06 14:27:16 -07002420 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002421 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002422 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002423 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002424 if (!LauncherApplication.isScreenLarge()) {
2425 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002426 int duration = mSearchDropTargetBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002427 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002428 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002429 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002430 }
2431 }
2432 }
2433
2434 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002435 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002436 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002437 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002438 if (!LauncherApplication.isScreenLarge()) {
2439 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002440 int duration = mSearchDropTargetBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002441 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002442 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002443 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002444 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002445 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002446 }
2447
Winson Chung785d2eb2011-04-14 16:08:02 -07002448 void showAllApps(boolean animated) {
2449 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002450
Winson Chungf0ea4d32011-06-06 14:27:16 -07002451 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2452 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002453
Winson Chung3d503fb2011-07-13 17:25:49 -07002454 // Hide the search bar and hotseat
Winson Chungc51db6a2011-10-05 11:44:49 -07002455 mSearchDropTargetBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002456
Winson Chungf0ea4d32011-06-06 14:27:16 -07002457 // Change the state *after* we've called all the transition code
2458 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002459
2460 // Pause the auto-advance of widgets until we are out of AllApps
2461 mUserPresent = false;
2462 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002463 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002464
2465 // Send an accessibility event to announce the context change
2466 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2467 }
2468
Joe Onoratob2061212009-11-24 16:13:54 -05002469 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002470 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002471 * - Home from workspace
2472 * - from center screen
2473 * - from other screens
2474 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002475 * - from center screen
2476 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002477 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002478 * - from center screen
2479 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002480 * - Launch app from workspace and quit
2481 * - with back
2482 * - with home
2483 * - Launch app from all apps and quit
2484 * - with back
2485 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002486 * - Go to a screen that's not the default, then all
2487 * apps, and launch and app, and go back
2488 * - with back
2489 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002490 * - On workspace, long press power and go back
2491 * - with back
2492 * - with home
2493 * - On all apps, long press power and go back
2494 * - with back
2495 * - with home
2496 * - On workspace, power off
2497 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002498 * - Launch an app and turn off the screen while in that app
2499 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002500 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002501 * - From all apps
2502 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002503 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2504 * - From all apps
2505 * - From the center workspace
2506 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002507 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002508 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002509 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2510 mWorkspace.setVisibility(View.VISIBLE);
2511 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002512
Winson Chung3d503fb2011-07-13 17:25:49 -07002513 // Show the search bar and hotseat
Winson Chungc51db6a2011-10-05 11:44:49 -07002514 mSearchDropTargetBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002515
Winson Chungf0ea4d32011-06-06 14:27:16 -07002516 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002517 if (mAllAppsButton != null) {
2518 mAllAppsButton.requestFocus();
2519 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002520 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002521 }
2522
Joe Onorato7c312c12009-08-13 21:36:53 -07002523 void lockAllApps() {
2524 // TODO
2525 }
2526
2527 void unlockAllApps() {
2528 // TODO
2529 }
2530
Patrick Dubroy5f445422011-02-18 14:35:21 -08002531 /**
2532 * Add an item from all apps or customize onto the given workspace screen.
2533 * If layout is null, add to the current screen.
2534 */
2535 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002536 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2537 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002538 } else {
2539 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002540 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002541 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002542
Winson Chungdff8ebb2011-09-08 17:25:31 -07002543 /** Maps the current orientation to an index for referencing orientation correct global icons */
2544 private int getCurrentOrientationIndexForGlobalIcons() {
2545 // default - 0, landscape - 1
2546 switch (getResources().getConfiguration().orientation) {
2547 case Configuration.ORIENTATION_LANDSCAPE:
2548 return 1;
2549 default:
2550 return 0;
2551 }
2552 }
2553
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002554 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002555 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002556 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002557 // Look for the toolbar icon specified in the activity meta-data
2558 Bundle metaData = packageManager.getActivityInfo(
2559 activityName, PackageManager.GET_META_DATA).metaData;
2560 if (metaData != null) {
2561 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2562 if (iconResId != 0) {
2563 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002564 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002565 }
2566 }
2567 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002568 // This can happen if the activity defines an invalid drawable
2569 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2570 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002571 } catch (Resources.NotFoundException nfe) {
2572 // This can happen if the activity defines an invalid drawable
2573 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2574 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002575 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002576 return null;
2577 }
2578
2579 // if successful in getting icon, return it; otherwise, set button to use default drawable
2580 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2581 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2582 TextView button = (TextView) findViewById(buttonId);
2583 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002584 Resources r = getResources();
2585 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2586 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002587
2588 // If we were unable to find the icon via the meta-data, use a generic one
2589 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002590 toolbarIcon = r.getDrawable(fallbackDrawableId);
2591 toolbarIcon.setBounds(0, 0, w, h);
2592 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002593 return null;
2594 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002595 toolbarIcon.setBounds(0, 0, w, h);
2596 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002597 return toolbarIcon.getConstantState();
2598 }
2599 }
2600
2601 // if successful in getting icon, return it; otherwise, set button to use default drawable
2602 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2603 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2604 ImageView button = (ImageView) findViewById(buttonId);
2605 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2606
Michael Jurka19e0fc52011-07-22 18:00:21 -07002607 if (button != null) {
2608 // If we were unable to find the icon via the meta-data, use a
2609 // generic one
2610 if (toolbarIcon == null) {
2611 button.setImageResource(fallbackDrawableId);
2612 } else {
2613 button.setImageDrawable(toolbarIcon);
2614 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002615 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002616
2617 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2618
Michael Jurka0423dcf2010-10-05 14:56:18 -07002619 }
2620
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002621 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2622 TextView button = (TextView) findViewById(buttonId);
2623 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2624 }
2625
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002626 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002627 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002628 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002629 }
2630
Winson Chungc51db6a2011-10-05 11:44:49 -07002631 private boolean updateGlobalSearchIcon() {
2632 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002633 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2634 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002635 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002636 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002637
Winson Chung1cad91e2011-05-25 17:41:01 -07002638 final SearchManager searchManager =
2639 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2640 ComponentName activityName = searchManager.getGlobalSearchActivity();
2641 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002642 int coi = getCurrentOrientationIndexForGlobalIcons();
2643 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002644 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002645 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002646 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2647 searchButton.setVisibility(View.VISIBLE);
2648 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002649 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002650 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002651 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002652 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2653 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2654 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002655 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002656 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002657 }
2658 }
2659
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002660 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002661 updateButtonWithDrawable(R.id.search_button, d);
2662 }
2663
Winson Chungc51db6a2011-10-05 11:44:49 -07002664 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002665 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002666 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002667 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002668
Winson Chungc51db6a2011-10-05 11:44:49 -07002669 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002670 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2671 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002672 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002673 int coi = getCurrentOrientationIndexForGlobalIcons();
2674 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002675 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002676 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002677 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002678 voiceButton.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002679 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002680 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002681 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002682 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002683 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002684 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002685 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002686 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002687
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002688 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002689 updateButtonWithDrawable(R.id.voice_button, d);
2690 }
2691
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002692 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002693 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002694 */
2695 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002696 final View marketButton = findViewById(R.id.market_button);
2697 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2698 // Find the app market activity by resolving an intent.
2699 // (If multiple app markets are installed, it will return the ResolverActivity.)
2700 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002701 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002702 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002703 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002704 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002705 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002706 marketButton.setVisibility(View.VISIBLE);
2707 } else {
2708 // We should hide and disable the view so that we don't try and restore the visibility
2709 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2710 marketButton.setVisibility(View.GONE);
2711 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002712 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002713 }
2714
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002715 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002716 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002717 }
2718
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002719 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002720 * Displays the shortcut creation dialog and launches, if necessary, the
2721 * appropriate activity.
2722 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002723 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002724 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2725 DialogInterface.OnShowListener {
2726
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002727 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002728
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002729 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002730 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002731
Winson Chung55b65502011-05-26 12:03:43 -07002732 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2733 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002734 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002735
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002736 AlertDialog dialog = builder.create();
2737 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002738 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002739 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002740
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002741 return dialog;
2742 }
2743
2744 public void onCancel(DialogInterface dialog) {
2745 mWaitingForResult = false;
2746 cleanup();
2747 }
2748
Romain Guycbb89e42009-06-08 15:52:54 -07002749 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002750 mWaitingForResult = false;
2751 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002752 }
2753
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002754 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002755 try {
2756 dismissDialog(DIALOG_CREATE_SHORTCUT);
2757 } catch (Exception e) {
2758 // An exception is thrown if the dialog is not visible, which is fine
2759 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002760 }
2761
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002762 /**
2763 * Handle the action clicked in the "Add to home" dialog.
2764 */
2765 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002766 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002767
Winson Chung55b65502011-05-26 12:03:43 -07002768 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2769 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002770 case AddAdapter.ITEM_APPLICATION: {
2771 if (mAppsCustomizeTabHost != null) {
2772 mAppsCustomizeTabHost.selectAppsTab();
2773 }
2774 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002775 break;
2776 }
Winson Chung55b65502011-05-26 12:03:43 -07002777 case AddAdapter.ITEM_APPWIDGET: {
2778 if (mAppsCustomizeTabHost != null) {
2779 mAppsCustomizeTabHost.selectWidgetsTab();
2780 }
2781 showAllApps(true);
2782 break;
2783 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002784 case AddAdapter.ITEM_WALLPAPER: {
2785 startWallpaper();
2786 break;
2787 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002788 }
2789 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002790
2791 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002792 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002793 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002794 }
2795
2796 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002797 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002798 */
2799 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2800 @Override
2801 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002802 closeSystemDialogs();
2803 }
2804 }
2805
2806 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002807 * Receives notifications whenever the appwidgets are reset.
2808 */
2809 private class AppWidgetResetObserver extends ContentObserver {
2810 public AppWidgetResetObserver() {
2811 super(new Handler());
2812 }
2813
2814 @Override
2815 public void onChange(boolean selfChange) {
2816 onAppWidgetReset();
2817 }
2818 }
2819
2820 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002821 * If the activity is currently paused, signal that we need to re-run the loader
2822 * in onResume.
2823 *
2824 * This needs to be called from incoming places where resources might have been loaded
2825 * while we are paused. That is becaues the Configuration might be wrong
2826 * when we're not running, and if it comes back to what it was when we
2827 * were paused, we are not restarted.
2828 *
2829 * Implementation of the method from LauncherModel.Callbacks.
2830 *
2831 * @return true if we are currently paused. The caller might be able to
2832 * skip some work in that case since we will come back again.
2833 */
2834 public boolean setLoadOnResume() {
2835 if (mPaused) {
2836 Log.i(TAG, "setLoadOnResume");
2837 mOnResumeNeedsLoad = true;
2838 return true;
2839 } else {
2840 return false;
2841 }
2842 }
2843
2844 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002845 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002846 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002847 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002848 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002849 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002850 } else {
2851 return SCREEN_COUNT / 2;
2852 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002853 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002854
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002855
Joe Onorato9c1289c2009-08-17 11:03:03 -04002856 /**
2857 * Refreshes the shortcuts shown on the workspace.
2858 *
2859 * Implementation of the method from LauncherModel.Callbacks.
2860 */
2861 public void startBinding() {
2862 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002863
2864 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002865 int count = workspace.getChildCount();
2866 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002867 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002868 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2869 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002870 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002871 if (mHotseat != null) {
2872 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002873 }
2874 }
2875
2876 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002877 * Bind the items start-end from the list.
2878 *
2879 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002880 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002881 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002882 setLoadOnResume();
2883
Joe Onorato9c1289c2009-08-17 11:03:03 -04002884 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002885 for (int i=start; i<end; i++) {
2886 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002887
2888 // Short circuit if we are loading dock items for a configuration which has no dock
2889 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2890 mHotseat == null) {
2891 continue;
2892 }
2893
Joe Onorato9c1289c2009-08-17 11:03:03 -04002894 switch (item.itemType) {
2895 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2896 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002897 View shortcut = createShortcut((ShortcutInfo)item);
2898 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2899 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002900 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002901 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002902 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002903 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002904 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002905 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2906 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002907 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002908 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002909 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002910 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002911 }
2912
Joe Onorato9c1289c2009-08-17 11:03:03 -04002913 /**
2914 * Implementation of the method from LauncherModel.Callbacks.
2915 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002916 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002917 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002918 sFolders.clear();
2919 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002920 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002921
2922 /**
2923 * Add the views for a widget to the workspace.
2924 *
2925 * Implementation of the method from LauncherModel.Callbacks.
2926 */
2927 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002928 setLoadOnResume();
2929
Daniel Sandler843e8602010-06-07 14:59:01 -04002930 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2931 if (DEBUG_WIDGETS) {
2932 Log.d(TAG, "bindAppWidget: " + item);
2933 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002934 final Workspace workspace = mWorkspace;
2935
2936 final int appWidgetId = item.appWidgetId;
2937 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002938 if (DEBUG_WIDGETS) {
2939 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2940 }
2941
Joe Onorato9c1289c2009-08-17 11:03:03 -04002942 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2943
Joe Onorato9c1289c2009-08-17 11:03:03 -04002944 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2945 item.hostView.setTag(item);
2946
Winson Chung3d503fb2011-07-13 17:25:49 -07002947 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002948 item.cellY, item.spanX, item.spanY, false);
2949
Adam Cohended9f8d2010-11-03 13:25:16 -07002950 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2951
Joe Onorato9c1289c2009-08-17 11:03:03 -04002952 workspace.requestLayout();
2953
Daniel Sandler843e8602010-06-07 14:59:01 -04002954 if (DEBUG_WIDGETS) {
2955 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2956 + (SystemClock.uptimeMillis()-start) + "ms");
2957 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002958 }
2959
2960 /**
2961 * Callback saying that there aren't any more items to bind.
2962 *
2963 * Implementation of the method from LauncherModel.Callbacks.
2964 */
2965 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002966 setLoadOnResume();
2967
Joe Onorato9c1289c2009-08-17 11:03:03 -04002968 if (mSavedState != null) {
2969 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002970 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002971 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002972 mSavedState = null;
2973 }
2974
2975 if (mSavedInstanceState != null) {
2976 super.onRestoreInstanceState(mSavedInstanceState);
2977 mSavedInstanceState = null;
2978 }
2979
Joe Onorato9c1289c2009-08-17 11:03:03 -04002980 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002981
2982 // If we received the result of any pending adds while the loader was running (e.g. the
2983 // widget configuration forced an orientation change), process them now.
2984 for (int i = 0; i < sPendingAddList.size(); i++) {
2985 completeAdd(sPendingAddList.get(i));
2986 }
2987 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002988
Winson Chungc51db6a2011-10-05 11:44:49 -07002989 // Update the market app icon as necessary (the other icons will be managed in response to
2990 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002991 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07002992
2993 mWorkspace.post(mBuildLayersRunnable);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002994 }
2995
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002996 @Override
2997 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07002998 boolean searchVisible = updateGlobalSearchIcon();
2999 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3000 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003001 }
3002
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003003 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003004 * Add the icons for all apps.
3005 *
3006 * Implementation of the method from LauncherModel.Callbacks.
3007 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003008 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3009 // Remove the progress bar entirely; we could also make it GONE
3010 // but better to remove it since we know it's not going to be used
3011 View progressBar = mAppsCustomizeTabHost.
3012 findViewById(R.id.apps_customize_progress_bar);
3013 if (progressBar != null) {
3014 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003015 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003016 // We just post the call to setApps so the user sees the progress bar
3017 // disappear-- otherwise, it just looks like the progress bar froze
3018 // which doesn't look great
3019 mAppsCustomizeTabHost.post(new Runnable() {
3020 public void run() {
3021 if (mAppsCustomizeContent != null) {
3022 mAppsCustomizeContent.setApps(apps);
3023 }
3024 }
3025 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003026 }
3027
3028 /**
3029 * A package was installed.
3030 *
3031 * Implementation of the method from LauncherModel.Callbacks.
3032 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003033 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003034 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003035 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003036
Winson Chung785d2eb2011-04-14 16:08:02 -07003037 if (mAppsCustomizeContent != null) {
3038 mAppsCustomizeContent.addApps(apps);
3039 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003040 }
3041
3042 /**
3043 * A package was updated.
3044 *
3045 * Implementation of the method from LauncherModel.Callbacks.
3046 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003047 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003048 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003049 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003050 if (mWorkspace != null) {
3051 mWorkspace.updateShortcuts(apps);
3052 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003053
Winson Chung785d2eb2011-04-14 16:08:02 -07003054 if (mAppsCustomizeContent != null) {
3055 mAppsCustomizeContent.updateApps(apps);
3056 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003057 }
3058
3059 /**
3060 * A package was uninstalled.
3061 *
3062 * Implementation of the method from LauncherModel.Callbacks.
3063 */
Joe Onorato36115782010-06-17 13:28:48 -04003064 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003065 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003066 if (permanent) {
3067 mWorkspace.removeItems(apps);
3068 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003069
Winson Chung785d2eb2011-04-14 16:08:02 -07003070 if (mAppsCustomizeContent != null) {
3071 mAppsCustomizeContent.removeApps(apps);
3072 }
Winson Chunga1820962011-10-03 16:31:06 -07003073
3074 // Notify the drag controller
3075 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003076 }
Joe Onoratobe386092009-11-17 17:32:16 -08003077
3078 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003079 * A number of packages were updated.
3080 */
3081 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003082 if (mAppsCustomizeContent != null) {
3083 mAppsCustomizeContent.onPackagesUpdated();
3084 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003085 }
3086
Winson Chung400438b2011-01-16 17:53:48 -08003087 private int mapConfigurationOriActivityInfoOri(int configOri) {
3088 final Display d = getWindowManager().getDefaultDisplay();
3089 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3090 switch (d.getRotation()) {
3091 case Surface.ROTATION_0:
3092 case Surface.ROTATION_180:
3093 // We are currently in the same basic orientation as the natural orientation
3094 naturalOri = configOri;
3095 break;
3096 case Surface.ROTATION_90:
3097 case Surface.ROTATION_270:
3098 // We are currently in the other basic orientation to the natural orientation
3099 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3100 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3101 break;
3102 }
3103
3104 int[] oriMap = {
3105 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3106 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3107 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3108 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3109 };
3110 // Since the map starts at portrait, we need to offset if this device's natural orientation
3111 // is landscape.
3112 int indexOffset = 0;
3113 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3114 indexOffset = 1;
3115 }
3116 return oriMap[(d.getRotation() + indexOffset) % 4];
3117 }
Adam Cohen446e9402011-09-15 18:21:21 -07003118
3119 public void lockScreenOrientationOnLargeUI() {
3120 if (LauncherApplication.isScreenLarge()) {
3121 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3122 .getConfiguration().orientation));
3123 }
Winson Chung400438b2011-01-16 17:53:48 -08003124 }
Adam Cohen446e9402011-09-15 18:21:21 -07003125 public void unlockScreenOrientationOnLargeUI() {
3126 if (LauncherApplication.isScreenLarge()) {
3127 mHandler.postDelayed(new Runnable() {
3128 public void run() {
3129 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3130 }
3131 }, mRestoreScreenOrientationDelay);
3132 }
Winson Chung400438b2011-01-16 17:53:48 -08003133 }
3134
Winson Chung82f55532011-08-09 14:14:23 -07003135 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003136 private static final String PREFS_KEY = "com.android.launcher2.prefs";
3137 private boolean isClingsEnabled() {
Winson Chung82f55532011-08-09 14:14:23 -07003138 // TEMPORARY: DISABLE CLINGS ON LARGE UI
Winson Chung7d7541e2011-09-16 20:14:36 -07003139 if (LauncherApplication.isScreenLarge()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003140 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003141 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003142
Winson Chung7d7541e2011-09-16 20:14:36 -07003143 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003144 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003145 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3146 Cling cling = (Cling) findViewById(clingId);
3147 if (cling != null) {
3148 cling.init(this, positionData);
3149 cling.setVisibility(View.VISIBLE);
3150 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3151 if (animate) {
3152 cling.buildLayer();
3153 cling.setAlpha(0f);
3154 cling.animate()
3155 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003156 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003157 .setDuration(SHOW_CLING_DURATION)
3158 .setStartDelay(delay)
3159 .start();
3160 } else {
3161 cling.setAlpha(1f);
3162 }
3163 }
3164 return cling;
3165 }
3166 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003167 if (cling != null) {
3168 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003169 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003170 anim.addListener(new AnimatorListenerAdapter() {
3171 public void onAnimationEnd(Animator animation) {
3172 cling.setVisibility(View.GONE);
3173 cling.cleanup();
3174 SharedPreferences prefs =
3175 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3176 SharedPreferences.Editor editor = prefs.edit();
3177 editor.putBoolean(flag, true);
3178 editor.commit();
3179 };
3180 });
3181 anim.start();
3182 }
3183 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003184 private void removeCling(int id) {
3185 final View cling = findViewById(id);
3186 if (cling != null) {
3187 final ViewGroup parent = (ViewGroup) cling.getParent();
3188 parent.post(new Runnable() {
3189 @Override
3190 public void run() {
3191 parent.removeView(cling);
3192 }
3193 });
3194 }
3195 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003196 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003197 // Enable the clings only if they have not been dismissed before
3198 SharedPreferences prefs =
3199 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003200 if (isClingsEnabled() && !prefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003201 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003202 } else {
3203 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003204 }
3205 }
3206 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003207 // Enable the clings only if they have not been dismissed before
3208 SharedPreferences prefs =
3209 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003210 if (isClingsEnabled() && !prefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003211 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003212 } else {
3213 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003214 }
3215 }
3216 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003217 // Enable the clings only if they have not been dismissed before
3218 SharedPreferences prefs =
3219 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
3220 Cling cling = null;
Winson Chung9d9d74f2011-09-19 11:49:12 -07003221 if (isClingsEnabled() && !prefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003222 cling = initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003223 } else {
3224 removeCling(R.id.folder_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003225 }
3226 return cling;
3227 }
3228 public boolean isFolderClingVisible() {
3229 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003230 if (cling != null) {
3231 return cling.getVisibility() == View.VISIBLE;
3232 }
3233 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003234 }
Winson Chung82f55532011-08-09 14:14:23 -07003235 public void dismissWorkspaceCling(View v) {
3236 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003237 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003238 }
3239 public void dismissAllAppsCling(View v) {
3240 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003241 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3242 }
3243 public void dismissFolderCling(View v) {
3244 Cling cling = (Cling) findViewById(R.id.folder_cling);
3245 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003246 }
3247
Winson Chung80baf5a2010-08-09 16:03:15 -07003248 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003249 * Prints out out state for debugging.
3250 */
3251 public void dumpState() {
3252 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003253 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003254 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3255 Log.d(TAG, "mRestoring=" + mRestoring);
3256 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3257 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003258 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003259 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003260
Winson Chung785d2eb2011-04-14 16:08:02 -07003261 if (mAppsCustomizeContent != null) {
3262 mAppsCustomizeContent.dumpState();
3263 }
Joe Onoratobe386092009-11-17 17:32:16 -08003264 Log.d(TAG, "END launcher2 dump state");
3265 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003266
3267 @Override
3268 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3269 super.dump(prefix, fd, writer, args);
3270 writer.println(" ");
3271 writer.println("Debug logs: ");
3272 for (int i = 0; i < sDumpLogs.size(); i++) {
3273 writer.println(" " + sDumpLogs.get(i));
3274 }
3275 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003276}
Michael Jurka2763be32011-02-24 11:19:57 -08003277
Michael Jurkaabded662011-03-04 12:06:57 -08003278interface LauncherTransitionable {
Winson Chung7d7541e2011-09-16 20:14:36 -07003279 void onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace);
3280 void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003281}