blob: 3f6ac9db61288e39aa5047743d159d7d5a349037 [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 }
Mathew Inwood8193f692011-10-12 17:30:58 +0100562 clearTypedText();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 }
564
565 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700566 protected void onPause() {
567 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700568 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800569 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700570 }
Romain Guycbb89e42009-06-08 15:52:54 -0700571
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700572 @Override
573 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400574 // Flag the loader to stop early before switching
575 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700576 if (mAppsCustomizeContent != null) {
577 mAppsCustomizeContent.surrender();
578 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800579 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700580 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700581
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800582 // We can't hide the IME if it was forced open. So don't bother
583 /*
584 @Override
585 public void onWindowFocusChanged(boolean hasFocus) {
586 super.onWindowFocusChanged(hasFocus);
587
588 if (hasFocus) {
589 final InputMethodManager inputManager = (InputMethodManager)
590 getSystemService(Context.INPUT_METHOD_SERVICE);
591 WindowManager.LayoutParams lp = getWindow().getAttributes();
592 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
593 android.os.Handler()) {
594 protected void onReceiveResult(int resultCode, Bundle resultData) {
595 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
596 }
597 });
598 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
599 }
600 }
601 */
602
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 private boolean acceptFilter() {
604 final InputMethodManager inputManager = (InputMethodManager)
605 getSystemService(Context.INPUT_METHOD_SERVICE);
606 return !inputManager.isFullscreenMode();
607 }
608
609 @Override
610 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700611 final int uniChar = event.getUnicodeChar();
612 final boolean handled = super.onKeyDown(keyCode, event);
613 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
614 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
616 keyCode, event);
617 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700618 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700619 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700620 // showSearchDialog()
621 // If there are multiple keystrokes before the search dialog takes focus,
622 // onSearchRequested() will be called for every keystroke,
623 // but it is idempotent, so it's fine.
624 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 }
626 }
627
Joe Onorato8a9625e2010-01-28 15:55:35 -0800628 // Eat the long press event so the keyboard doesn't come up.
629 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
630 return true;
631 }
632
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800633 return handled;
634 }
635
Karl Rosaen138a0412009-04-23 19:00:21 -0700636 private String getTypedText() {
637 return mDefaultKeySsb.toString();
638 }
639
640 private void clearTypedText() {
641 mDefaultKeySsb.clear();
642 mDefaultKeySsb.clearSpans();
643 Selection.setSelection(mDefaultKeySsb, 0);
644 }
645
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800646 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700647 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
648 * State
649 */
650 private static State intToState(int stateOrdinal) {
651 State state = State.WORKSPACE;
652 final State[] stateValues = State.values();
653 for (int i = 0; i < stateValues.length; i++) {
654 if (stateValues[i].ordinal() == stateOrdinal) {
655 state = stateValues[i];
656 break;
657 }
658 }
659 return state;
660 }
661
662 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 * Restores the previous state, if it exists.
664 *
665 * @param savedState The previous state.
666 */
667 private void restoreState(Bundle savedState) {
668 if (savedState == null) {
669 return;
670 }
671
Michael Jurka883f55b2010-10-21 15:47:14 -0700672 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700673 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800674 showAllApps(false);
675 }
676
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800677 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
678 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700679 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800680 }
681
Winson Chung3d503fb2011-07-13 17:25:49 -0700682 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
683 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700684
Winson Chung3d503fb2011-07-13 17:25:49 -0700685 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
686 mPendingAddInfo.container = pendingAddContainer;
687 mPendingAddInfo.screen = pendingAddScreen;
688 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
689 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 mRestoring = true;
691 }
692
693 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
694 if (renameFolder) {
695 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700696 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800697 mRestoring = true;
698 }
Winson Chunga12a2502010-12-20 14:41:35 -0800699
Winson Chung785d2eb2011-04-14 16:08:02 -0700700
701 // Restore the AppsCustomize tab
702 if (mAppsCustomizeTabHost != null) {
703 String curTab = savedState.getString("apps_customize_currentTab");
704 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700705 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700706 mAppsCustomizeContent.setContentType(
707 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
708 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700709 mAppsCustomizeContent.loadAssociatedPages(
710 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700711 }
712
Winson Chung5afbf7b2011-07-25 11:53:08 -0700713 int currentIndex = savedState.getInt("apps_customize_currentIndex");
714 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700715 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800716 }
717
718 /**
719 * Finds all the views we need and configure them properly.
720 */
721 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700722 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400723
Winson Chung4c98d922011-05-31 16:50:48 -0700724 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
725 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726
Winson Chung4c98d922011-05-31 16:50:48 -0700727 // Setup the drag layer
728 mDragLayer.setup(this, dragController);
729
Winson Chung3d503fb2011-07-13 17:25:49 -0700730 // Setup the hotseat
731 mHotseat = (Hotseat) findViewById(R.id.hotseat);
732 if (mHotseat != null) {
733 mHotseat.setup(this);
734 }
735
Winson Chung4c98d922011-05-31 16:50:48 -0700736 // Setup the workspace
737 mWorkspace.setHapticFeedbackEnabled(false);
738 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700739 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700740 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700741
Winson Chungf0ea4d32011-06-06 14:27:16 -0700742 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700743 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700744
Winson Chungf0ea4d32011-06-06 14:27:16 -0700745 // Setup AppsCustomize
746 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
747 findViewById(R.id.apps_customize_pane);
748 mAppsCustomizeContent = (AppsCustomizePagedView)
749 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
750 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400751
Romain Guy1fbc1c82009-11-09 20:43:08 -0800752
Winson Chung4c98d922011-05-31 16:50:48 -0700753
Winson Chung3d503fb2011-07-13 17:25:49 -0700754 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700755 dragController.setDragScoller(mWorkspace);
756 dragController.setScrollView(mDragLayer);
757 dragController.setMoveTarget(mWorkspace);
758 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700759 if (mSearchDropTargetBar != null) {
760 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800761 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800762 }
763
764 /**
765 * Creates a view representing a shortcut.
766 *
767 * @param info The data structure describing the shortcut.
768 *
769 * @return A View inflated from R.layout.application.
770 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800771 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800772 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700773 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800774 }
775
776 /**
777 * Creates a view representing a shortcut inflated from the specified resource.
778 *
779 * @param layoutResId The id of the XML layout used to create the shortcut.
780 * @param parent The group the shortcut belongs to.
781 * @param info The data structure describing the shortcut.
782 *
783 * @return A View inflated from layoutResId.
784 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800785 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800786 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
787 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800789 return favorite;
790 }
791
792 /**
793 * Add an application shortcut to the workspace.
794 *
795 * @param data The intent describing the application.
796 * @param cellInfo The position on screen where to create the shortcut.
797 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700798 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700799 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700800 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700801
Adam Cohenfbba09b2011-07-18 21:43:05 -0700802 // First we check if we already know the exact location where we want to add this item.
803 if (cellX >= 0 && cellY >= 0) {
804 cellXY[0] = cellX;
805 cellXY[1] = cellY;
806 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700807 showOutOfSpaceMessage();
808 return;
809 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800810
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800811 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800812
Romain Guy73b979d2009-06-09 12:57:21 -0700813 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800814 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800815 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800816 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700817 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700818 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800819 } else {
820 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800821 }
822 }
Romain Guycbb89e42009-06-08 15:52:54 -0700823
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800824 /**
825 * Add a shortcut to the workspace.
826 *
827 * @param data The intent describing the shortcut.
828 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700830 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
831 int cellY) {
832 int[] cellXY = mTmpAddItemCellCoordinates;
833 int[] touchXY = mPendingAddInfo.dropPos;
834 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700835
Michael Jurkad3ef3062010-11-23 16:23:58 -0800836 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700837
Adam Cohen558baaf2011-08-15 15:22:57 -0700838 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
839 final View view = createShortcut(info);
840
Adam Cohenfbba09b2011-07-18 21:43:05 -0700841 // First we check if we already know the exact location where we want to add this item.
842 if (cellX >= 0 && cellY >= 0) {
843 cellXY[0] = cellX;
844 cellXY[1] = cellY;
845 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700846
847 // If appropriate, either create a folder or add to an existing folder
848 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
849 true, null,null)) {
850 return;
851 }
852 DragObject dragObject = new DragObject();
853 dragObject.dragInfo = info;
854 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
855 return;
856 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700857 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800858 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700859 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800860 foundCellSpan = (result != null);
861 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700862 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800863 }
864
865 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700866 showOutOfSpaceMessage();
867 return;
868 }
869
Adam Cohen558baaf2011-08-15 15:22:57 -0700870 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871
872 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700873 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
874 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 }
876 }
877
Adam Cohen50370f32011-08-25 18:57:14 -0700878 class Padding {
879 int left = 0;
880 int right = 0;
881 int top = 0;
882 int bottom = 0;
883 }
884
Adam Cohenf814aa02011-09-01 13:48:05 -0700885 Padding getPaddingForWidget(ComponentName component) {
Adam Cohen50370f32011-08-25 18:57:14 -0700886 PackageManager packageManager = getPackageManager();
887 Padding p = new Padding();
888 android.content.pm.ApplicationInfo appInfo;
889
890 try {
Adam Cohenf814aa02011-09-01 13:48:05 -0700891 appInfo = packageManager.getApplicationInfo(component.getPackageName(), 0);
Adam Cohen50370f32011-08-25 18:57:14 -0700892 } catch (Exception e) {
893 // if we can't find the package, return 0 padding
894 return p;
895 }
896
Adam Cohenff648b52011-09-19 11:57:30 -0700897 if (appInfo.targetSdkVersion >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
Adam Cohen50370f32011-08-25 18:57:14 -0700898 Resources r = getResources();
Adam Cohen82ebbd22011-09-30 15:05:40 -0700899 // The default padding values are private API currently, but will be added in
900 // API level 15. The current values are (8, 8, 8, 8).
901 p.left = r.getDimensionPixelSize(com.android.internal.
902 R.dimen.default_app_widget_padding_left);
903 p.right = r.getDimensionPixelSize(com.android.internal.
904 R.dimen.default_app_widget_padding_right);
905 p.top = r.getDimensionPixelSize(com.android.internal.
906 R.dimen.default_app_widget_padding_top);
907 p.bottom = r.getDimensionPixelSize(com.android.internal.
908 R.dimen.default_app_widget_padding_bottom);
Adam Cohen50370f32011-08-25 18:57:14 -0700909 }
910
911 return p;
912 }
913
Adam Cohenf814aa02011-09-01 13:48:05 -0700914 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
915 if (spanXY == null) {
916 spanXY = new int[2];
917 }
918
919 Padding padding = getPaddingForWidget(component);
920 // We want to account for the extra amount of padding that we are adding to the widget
921 // to ensure that it gets the full amount of space that it has requested
922 int requiredWidth = minWidth + padding.left + padding.right;
923 int requiredHeight = minHeight + padding.top + padding.bottom;
924 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
925 }
926
927 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
928 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
929 }
930
Adam Cohencbf47e32011-09-16 17:32:37 -0700931 int[] getMinResizeSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
932 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
933 }
934
Adam Cohenf814aa02011-09-01 13:48:05 -0700935 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
936 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
937 }
938
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800939 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700940 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800941 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700942 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700943 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700945 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700946 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700947
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700948 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700949 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700950
Adam Cohenf814aa02011-09-01 13:48:05 -0700951 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700952
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700954 // We have saved the position to which the widget was dragged-- this really only matters
955 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700956 int[] cellXY = mTmpAddItemCellCoordinates;
957 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700958 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700959 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
960 cellXY[0] = mPendingAddInfo.cellX;
961 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700962 foundCellSpan = true;
963 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700964 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700965 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700966 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800967 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700968 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700969 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700970 }
971
Michael Jurka0280c3b2010-09-17 15:00:07 -0700972 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800973 if (appWidgetId != -1) {
974 // Deleting an app widget ID is a void call but writes to disk before returning
975 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800976 new Thread("deleteAppWidgetId") {
977 public void run() {
978 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
979 }
980 }.start();
981 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700982 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800983 return;
984 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800985
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700986 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -0700987 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700988 launcherInfo.spanX = spanXY[0];
989 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700990
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700992 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993
994 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800995 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700996 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700997
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700998 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800999 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001000
Winson Chung3d503fb2011-07-13 17:25:49 -07001001 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001002 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001003
1004 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 }
1006 }
Romain Guycbb89e42009-06-08 15:52:54 -07001007
Adam Cohended9f8d2010-11-03 13:25:16 -07001008 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1009 @Override
1010 public void onReceive(Context context, Intent intent) {
1011 final String action = intent.getAction();
1012 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1013 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001014 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001015 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001016
Winson Chungc100e8e2011-08-09 16:02:43 -07001017 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001018 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001019 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1020 mAppsCustomizeTabHost.reset();
1021 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001022 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001023 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1024 mUserPresent = true;
1025 updateRunning();
1026 }
1027 }
1028 };
1029
1030 @Override
1031 public void onAttachedToWindow() {
1032 super.onAttachedToWindow();
1033
1034 // Listen for broadcasts related to user-presence
1035 final IntentFilter filter = new IntentFilter();
1036 filter.addAction(Intent.ACTION_SCREEN_OFF);
1037 filter.addAction(Intent.ACTION_USER_PRESENT);
1038 registerReceiver(mReceiver, filter);
1039
Adam Cohend113e0c2010-11-11 10:48:05 -08001040 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001041 mVisible = true;
1042 }
1043
1044 @Override
1045 public void onDetachedFromWindow() {
1046 super.onDetachedFromWindow();
1047 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001048 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001049
Adam Cohend113e0c2010-11-11 10:48:05 -08001050 if (mAttached) {
1051 unregisterReceiver(mReceiver);
1052 mAttached = false;
1053 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001054 updateRunning();
1055 }
1056
1057 public void onWindowVisibilityChanged(int visibility) {
1058 mVisible = visibility == View.VISIBLE;
1059 updateRunning();
1060 }
1061
1062 private void sendAdvanceMessage(long delay) {
1063 mHandler.removeMessages(ADVANCE_MSG);
1064 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1065 mHandler.sendMessageDelayed(msg, delay);
1066 mAutoAdvanceSentTime = System.currentTimeMillis();
1067 }
1068
1069 private void updateRunning() {
1070 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1071 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1072 mAutoAdvanceRunning = autoAdvanceRunning;
1073 if (autoAdvanceRunning) {
1074 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1075 sendAdvanceMessage(delay);
1076 } else {
1077 if (!mWidgetsToAdvance.isEmpty()) {
1078 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1079 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1080 }
1081 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001082 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001083 }
1084 }
1085 }
1086
1087 private final Handler mHandler = new Handler() {
1088 @Override
1089 public void handleMessage(Message msg) {
1090 if (msg.what == ADVANCE_MSG) {
1091 int i = 0;
1092 for (View key: mWidgetsToAdvance.keySet()) {
1093 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1094 final int delay = mAdvanceStagger * i;
1095 if (v instanceof Advanceable) {
1096 postDelayed(new Runnable() {
1097 public void run() {
1098 ((Advanceable) v).advance();
1099 }
1100 }, delay);
1101 }
1102 i++;
1103 }
1104 sendAdvanceMessage(mAdvanceInterval);
1105 }
1106 }
1107 };
1108
1109 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001110 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001111 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1112 if (v instanceof Advanceable) {
1113 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001114 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001115 updateRunning();
1116 }
1117 }
1118
1119 void removeWidgetToAutoAdvance(View hostView) {
1120 if (mWidgetsToAdvance.containsKey(hostView)) {
1121 mWidgetsToAdvance.remove(hostView);
1122 updateRunning();
1123 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001124 }
1125
Joe Onorato9c1289c2009-08-17 11:03:03 -04001126 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001127 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001128 launcherInfo.hostView = null;
1129 }
1130
Adam Cohended9f8d2010-11-03 13:25:16 -07001131 void showOutOfSpaceMessage() {
1132 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1133 }
1134
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001135 public LauncherAppWidgetHost getAppWidgetHost() {
1136 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 }
Romain Guycbb89e42009-06-08 15:52:54 -07001138
Winson Chunga9abd0e2010-10-27 17:18:37 -07001139 public LauncherModel getModel() {
1140 return mModel;
1141 }
1142
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001143 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001144 getWindow().closeAllPanels();
1145
Winson Chung87acb482011-08-23 17:28:05 -07001146 /**
1147 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001148 try {
1149 dismissDialog(DIALOG_CREATE_SHORTCUT);
1150 // Unlock the workspace if the dialog was showing
1151 } catch (Exception e) {
1152 // An exception is thrown if the dialog is not visible, which is fine
1153 }
1154
1155 try {
1156 dismissDialog(DIALOG_RENAME_FOLDER);
1157 // Unlock the workspace if the dialog was showing
1158 } catch (Exception e) {
1159 // An exception is thrown if the dialog is not visible, which is fine
1160 }
Winson Chung87acb482011-08-23 17:28:05 -07001161 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001162
1163 // Whatever we were doing is hereby canceled.
1164 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001165 }
1166
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 @Override
1168 protected void onNewIntent(Intent intent) {
1169 super.onNewIntent(intent);
1170
1171 // Close the menu
1172 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001173 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001174 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001175
Joe Onorato14f122b2009-11-19 14:06:36 -08001176 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1177 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001178
Adam Cohenac56cff2011-09-28 20:45:37 -07001179 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001180 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001181 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001182 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1183 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001184 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001185 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001186
1187 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001188 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001189 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001190
Joe Onorato3a8820b2009-11-10 15:06:42 -08001191 final View v = getWindow().peekDecorView();
1192 if (v != null && v.getWindowToken() != null) {
1193 InputMethodManager imm = (InputMethodManager)getSystemService(
1194 INPUT_METHOD_SERVICE);
1195 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001197
Michael Jurka35aa14d2011-07-07 17:01:08 -07001198 // Reset AllApps to its initial state
Winson Chungc100e8e2011-08-09 16:02:43 -07001199 if (mAppsCustomizeTabHost != null) {
1200 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001201 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 }
1203 }
1204
1205 @Override
1206 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1207 // Do not call super here
1208 mSavedInstanceState = savedInstanceState;
1209 }
1210
1211 @Override
1212 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001213 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001214 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001215
Michael Jurka883f55b2010-10-21 15:47:14 -07001216 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001217 // We close any open folder since it will not be re-opened, and we need to make sure
1218 // this state is reflected.
1219 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001220
Winson Chung3d503fb2011-07-13 17:25:49 -07001221 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1222 mWaitingForResult) {
1223 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1224 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1225 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1226 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001227 }
1228
1229 if (mFolderInfo != null && mWaitingForResult) {
1230 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1231 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1232 }
Michael Jurka946ad472010-07-09 18:05:18 -07001233
Winson Chung785d2eb2011-04-14 16:08:02 -07001234 // Save the current AppsCustomize tab
1235 if (mAppsCustomizeTabHost != null) {
1236 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1237 if (currentTabTag != null) {
1238 outState.putString("apps_customize_currentTab", currentTabTag);
1239 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001240 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1241 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001242 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001243 }
1244
1245 @Override
1246 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001247 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001248
Winson Chunge7a03942011-08-05 15:05:12 -07001249 // Remove all pending runnables
1250 mHandler.removeMessages(ADVANCE_MSG);
1251 mHandler.removeMessages(0);
1252
Winson Chungcd2b0142011-06-08 16:02:26 -07001253 // Stop callbacks from LauncherModel
1254 LauncherApplication app = ((LauncherApplication) getApplication());
1255 mModel.stopLoader();
1256 app.setLauncher(null);
1257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001259 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001261 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001263 mAppWidgetHost = null;
1264
1265 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001266
1267 TextKeyListener.getInstance().release();
1268
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001269
Winson Chung3d503fb2011-07-13 17:25:49 -07001270 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001271
1272 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001273 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001274
1275 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1276 mWorkspace.removeAllViews();
1277 mWorkspace = null;
1278 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001279
1280 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001281 }
1282
Adam Cohena9cf38f2011-05-02 15:36:58 -07001283 public DragController getDragController() {
1284 return mDragController;
1285 }
1286
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 @Override
1288 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001289 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 super.startActivityForResult(intent, requestCode);
1291 }
1292
Winson Chung70d72102011-08-12 11:24:35 -07001293 /**
1294 * Indicates that we want global search for this activity by setting the globalSearch
1295 * argument for {@link #startSearch} to true.
1296 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001298 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001300
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001301 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001302
Karl Rosaen138a0412009-04-23 19:00:21 -07001303 if (initialQuery == null) {
1304 // Use any text typed in the launcher as the initial query
1305 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001306 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 if (appSearchData == null) {
1308 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001309 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001310 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001311 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001312
Karl Rosaen138a0412009-04-23 19:00:21 -07001313 final SearchManager searchManager =
1314 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001315 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001316 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001317 }
1318
Winson Chung70d72102011-08-12 11:24:35 -07001319 @Override
1320 public boolean onCreateOptionsMenu(Menu menu) {
1321 if (isWorkspaceLocked()) {
1322 return false;
1323 }
1324
1325 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001326
1327 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1328 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1329 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001330 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1331 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1332 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001333 String helpUrl = getString(R.string.help_url);
1334 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001335 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1336 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1337
Winson Chung70d72102011-08-12 11:24:35 -07001338 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1339 .setIcon(android.R.drawable.ic_menu_gallery)
1340 .setAlphabeticShortcut('W');
1341 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1342 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001343 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001344 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001345 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1346 .setIcon(android.R.drawable.ic_menu_preferences)
1347 .setIntent(settings)
1348 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001349 if (!helpUrl.isEmpty()) {
1350 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1351 .setIcon(android.R.drawable.ic_menu_help)
1352 .setIntent(help)
1353 .setAlphabeticShortcut('H');
1354 }
Winson Chung70d72102011-08-12 11:24:35 -07001355 return true;
1356 }
1357
1358 @Override
1359 public boolean onPrepareOptionsMenu(Menu menu) {
1360 super.onPrepareOptionsMenu(menu);
1361
1362 if (mAppsCustomizeTabHost.isTransitioning()) {
1363 return false;
1364 }
1365 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1366 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1367
1368 return true;
1369 }
1370
1371 @Override
1372 public boolean onOptionsItemSelected(MenuItem item) {
1373 switch (item.getItemId()) {
1374 case MENU_WALLPAPER_SETTINGS:
1375 startWallpaper();
1376 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001377 }
1378
1379 return super.onOptionsItemSelected(item);
1380 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001382 @Override
1383 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001384 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001385 // Use a custom animation for launching search
1386 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001387 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001388 }
1389
Joe Onorato9c1289c2009-08-17 11:03:03 -04001390 public boolean isWorkspaceLocked() {
1391 return mWorkspaceLoading || mWaitingForResult;
1392 }
1393
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001395 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001396 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001397 }
1398
Michael Jurka0280c3b2010-09-17 15:00:07 -07001399 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001400 mPendingAddInfo.container = ItemInfo.NO_ID;
1401 mPendingAddInfo.screen = -1;
1402 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1403 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1404 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001405 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001406
Michael Jurkaaf442092010-06-10 17:01:57 -07001407 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001408 // TODO: catch bad widget exception when sent
1409 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001410 // TODO: Is this log message meaningful?
1411 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001412 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001413 }
1414
Winson Chung55cef262010-10-28 14:14:18 -07001415 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001416 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417
Bjorn Bringert7984c942009-12-09 15:38:25 +00001418 if (appWidget.configure != null) {
1419 // Launch over to configure widget, if needed
1420 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1421 intent.setComponent(appWidget.configure);
1422 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001423 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001424 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1425 intent.putExtra(
1426 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1427 info.mimeType);
1428
1429 final String mimeType = info.mimeType;
1430 final ClipData clipData = (ClipData) info.configurationData;
1431 final ClipDescription clipDesc = clipData.getDescription();
1432 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1433 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001434 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001435 final CharSequence stringData = item.getText();
1436 final Uri uriData = item.getUri();
1437 final Intent intentData = item.getIntent();
1438 final String key =
1439 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1440 if (uriData != null) {
1441 intent.putExtra(key, uriData);
1442 } else if (intentData != null) {
1443 intent.putExtra(key, intentData);
1444 } else if (stringData != null) {
1445 intent.putExtra(key, stringData);
1446 }
1447 break;
1448 }
1449 }
1450 }
Winson Chung55cef262010-10-28 14:14:18 -07001451 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001452
Romain Guy8e633c52010-03-04 12:51:36 -08001453 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001455 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001456 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001457
1458 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001459 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 }
1461 }
Romain Guycbb89e42009-06-08 15:52:54 -07001462
Adam Cohenfbba09b2011-07-18 21:43:05 -07001463 /**
1464 * Process a shortcut drop.
1465 *
1466 * @param componentName The name of the component
1467 * @param screen The screen where it should be added
1468 * @param cell The cell it should be added to, optional
1469 * @param position The location on the screen where it was dropped, optional
1470 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001471 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1472 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001473 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001474 mPendingAddInfo.container = container;
1475 mPendingAddInfo.screen = screen;
1476 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001477
1478 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001479 mPendingAddInfo.cellX = cell[0];
1480 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001481 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001482
1483 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1484 createShortcutIntent.setComponent(componentName);
1485 processShortcut(createShortcutIntent);
1486 }
1487
Adam Cohenfbba09b2011-07-18 21:43:05 -07001488 /**
1489 * Process a widget drop.
1490 *
1491 * @param info The PendingAppWidgetInfo of the widget being added.
1492 * @param screen The screen where it should be added
1493 * @param cell The cell it should be added to, optional
1494 * @param position The location on the screen where it was dropped, optional
1495 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001496 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1497 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001498 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001499 mPendingAddInfo.container = info.container = container;
1500 mPendingAddInfo.screen = info.screen = screen;
1501 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001502 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001503 mPendingAddInfo.cellX = cell[0];
1504 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001505 }
1506
1507 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1508 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1509 addAppWidgetImpl(appWidgetId, info);
1510 }
1511
Joe Onoratodeb98af2010-02-19 14:59:39 -08001512 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001513 // Handle case where user selected "Applications"
1514 String applicationName = getResources().getString(R.string.group_applications);
1515 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001516
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001517 if (applicationName != null && applicationName.equals(shortcutName)) {
1518 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1519 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001520
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001521 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1522 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001523 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001524 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001525 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001526 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 }
1529
Winson Chung24ab2f12010-09-16 14:10:47 -07001530 void processWallpaper(Intent intent) {
1531 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1532 }
1533
Winson Chung3d503fb2011-07-13 17:25:49 -07001534 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1535 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001536 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 folderInfo.title = getText(R.string.folder_name);
1538
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001540 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1541 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001542 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543
1544 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001545 FolderIcon newFolder =
1546 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1547 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1548 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001549 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 }
Romain Guycbb89e42009-06-08 15:52:54 -07001551
Joe Onorato9c1289c2009-08-17 11:03:03 -04001552 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001553 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001554 }
1555
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001556 private void showNotifications() {
1557 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1558 if (statusBar != null) {
1559 statusBar.expand();
1560 }
1561 }
1562
1563 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001564 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001565 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001566 Intent chooser = Intent.createChooser(pickWallpaper,
1567 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001568 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1569 // Removed in Eclair MR1
1570// WallpaperManager wm = (WallpaperManager)
1571// getSystemService(Context.WALLPAPER_SERVICE);
1572// WallpaperInfo wi = wm.getWallpaperInfo();
1573// if (wi != null && wi.getSettingsActivity() != null) {
1574// LabeledIntent li = new LabeledIntent(getPackageName(),
1575// R.string.configure_wallpaper, 0);
1576// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1577// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1578// }
Mike Clerona0618e42009-10-22 13:55:21 -07001579 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
1581
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001582 /**
1583 * Registers various content observers. The current implementation registers
1584 * only a favorites observer to keep track of the favorites applications.
1585 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001586 private void registerContentObservers() {
1587 ContentResolver resolver = getContentResolver();
1588 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1589 true, mWidgetObserver);
1590 }
1591
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001592 @Override
1593 public boolean dispatchKeyEvent(KeyEvent event) {
1594 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1595 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001596 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001597 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001598 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001599 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001600 dumpState();
1601 return true;
1602 }
1603 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001604 }
1605 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1606 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001607 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608 return true;
1609 }
1610 }
1611
1612 return super.dispatchKeyEvent(event);
1613 }
1614
Joe Onorato88ec0992009-11-19 13:16:06 -08001615 @Override
1616 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001617 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001618 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001619 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001620 Folder openFolder = mWorkspace.getOpenFolder();
1621 if (openFolder.isEditingName()) {
1622 openFolder.dismissEditingName();
1623 } else {
1624 closeFolder();
1625 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001626 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001627 mWorkspace.exitWidgetResizeMode();
1628
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001629 // Back button is a no-op here, but give at least some feedback for the button press
1630 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001631 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001632 }
1633
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001634 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001635 * Re-listen when widgets are reset.
1636 */
1637 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001638 if (mAppWidgetHost != null) {
1639 mAppWidgetHost.startListening();
1640 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001641 }
1642
1643 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001644 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1645 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001647 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001648 if (mModel != null) {
1649 mModel.unbindWorkspaceItems();
1650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001652
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 /**
1654 * Launches the intent referred by the clicked shortcut.
1655 *
1656 * @param v The view representing the clicked shortcut.
1657 */
1658 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001659 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1660 // view has detached (it's possible for this to happen if the view is removed mid touch).
1661 if (v.getWindowToken() == null) {
1662 return;
1663 }
1664
1665 if (mWorkspace.isSwitchingState()) {
1666 return;
1667 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001668
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001670 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001672 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001673 int[] pos = new int[2];
1674 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001675 intent.setSourceBounds(new Rect(pos[0], pos[1],
1676 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001677 boolean success = startActivitySafely(intent, tag);
1678
1679 if (success && v instanceof BubbleTextView) {
1680 mWaitingForResume = (BubbleTextView) v;
1681 mWaitingForResume.setStayPressed(true);
1682 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001684 if (v instanceof FolderIcon) {
1685 FolderIcon fi = (FolderIcon) v;
1686 handleFolderClick(fi);
1687 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001688 } else if (v == mAllAppsButton) {
1689 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001690 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001691 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001692 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694 }
1695 }
1696
Michael Jurka0e260592010-06-30 17:07:39 -07001697 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001698 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001699 // clicking anywhere on the workspace causes the customization drawer to slide down
1700 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001701 return false;
1702 }
1703
Michael Jurkaaf442092010-06-10 17:01:57 -07001704 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001705 * Event handler for the search button
1706 *
1707 * @param v The view that was clicked.
1708 */
1709 public void onClickSearchButton(View v) {
Amith Yamasania135ba82011-08-09 17:42:01 -07001710 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001711 }
1712
1713 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001714 * Event handler for the voice button
1715 *
1716 * @param v The view that was clicked.
1717 */
1718 public void onClickVoiceButton(View v) {
1719 startVoiceSearch();
1720 }
1721
1722 private void startVoiceSearch() {
1723 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001724 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001725 startActivity(intent);
1726 }
1727
1728 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001729 * Event handler for the "grid" button that appears on the home screen, which
1730 * enters all apps mode.
1731 *
1732 * @param v The view that was clicked.
1733 */
1734 public void onClickAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001735 // Provide the same haptic feedback that the system offers for virtual keys.
1736 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001737 showAllApps(true);
1738 }
1739
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001740 public void onClickAppMarketButton(View v) {
1741 if (mAppMarketIntent != null) {
1742 startActivitySafely(mAppMarketIntent, "app market");
1743 }
1744 }
1745
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001746 void startApplicationDetailsActivity(ComponentName componentName) {
1747 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001748 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1749 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001750 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001751 startActivity(intent);
1752 }
1753
Patrick Dubroy5539af72010-09-07 15:22:01 -07001754 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1755 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1756 // System applications cannot be installed. For now, show a toast explaining that.
1757 // We may give them the option of disabling apps this way.
1758 int messageId = R.string.uninstall_system_app_text;
1759 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1760 } else {
1761 String packageName = appInfo.componentName.getPackageName();
1762 String className = appInfo.componentName.getClassName();
1763 Intent intent = new Intent(
1764 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001765 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1766 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001767 startActivity(intent);
1768 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001769 }
1770
Michael Jurkaddd62e92011-02-16 17:49:14 -08001771 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1773 try {
1774 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001775 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001776 } catch (ActivityNotFoundException e) {
1777 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001778 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001779 } catch (SecurityException e) {
1780 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001781 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001783 "or use the exported attribute for this activity. "
1784 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001785 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001786 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001788
Romain Guy8e633c52010-03-04 12:51:36 -08001789 void startActivityForResultSafely(Intent intent, int requestCode) {
1790 try {
1791 startActivityForResult(intent, requestCode);
1792 } catch (ActivityNotFoundException e) {
1793 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1794 } catch (SecurityException e) {
1795 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1796 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1797 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1798 "or use the exported attribute for this activity.", e);
1799 }
1800 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801
Adam Cohena9cf38f2011-05-02 15:36:58 -07001802 private void handleFolderClick(FolderIcon folderIcon) {
1803 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001804 Folder openFolder = mWorkspace.getFolderForTag(info);
1805
1806 // If the folder info reports that the associated folder is open, then verify that
1807 // it is actually opened. There have been a few instances where this gets out of sync.
1808 if (info.opened && openFolder == null) {
1809 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1810 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1811 info.opened = false;
1812 }
1813
Adam Cohena9cf38f2011-05-02 15:36:58 -07001814 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 // Close any open folder
1816 closeFolder();
1817 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001818 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 } else {
1820 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 int folderScreen;
1822 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001823 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 // .. and close it
1825 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001826 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001827 // Close any folder open on the current screen
1828 closeFolder();
1829 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001830 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001831 }
1832 }
1833 }
1834 }
1835
Adam Cohen2801caf2011-05-13 20:57:39 -07001836 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001837 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001838 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1839 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1840 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1841
Adam Cohenc51934b2011-07-26 21:07:43 -07001842 FolderInfo info = (FolderInfo) fi.getTag();
1843 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1844 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001845 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1846 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001847 }
1848
Adam Cohen2801caf2011-05-13 20:57:39 -07001849 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1850 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1851 oa.start();
1852 }
1853
1854 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001855 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001856 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1857 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1858 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1859
Adam Cohenc51934b2011-07-26 21:07:43 -07001860 FolderInfo info = (FolderInfo) fi.getTag();
1861 CellLayout cl = null;
1862 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1863 cl = (CellLayout) fi.getParent().getParent();
1864 }
1865
1866 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001867 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1868 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001869 oa.addListener(new AnimatorListenerAdapter() {
1870 @Override
1871 public void onAnimationEnd(Animator animation) {
1872 if (layout != null) {
1873 layout.clearFolderLeaveBehind();
1874 }
1875 }
1876 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001877 oa.start();
1878 }
1879
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001881 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 * is animated relative to the specified View. If the View is null, no animation
1883 * is played.
1884 *
1885 * @param folderInfo The FolderInfo describing the folder to open.
1886 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001887 public void openFolder(FolderIcon folderIcon) {
1888 Folder folder = folderIcon.mFolder;
1889 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890
Adam Cohen2801caf2011-05-13 20:57:39 -07001891 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001892 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893
Adam Cohen4554ee12011-08-03 16:13:21 -07001894 // Just verify that the folder hasn't already been added to the DragLayer.
1895 // There was a one-off crash where the folder had a parent already.
1896 if (folder.getParent() == null) {
1897 mDragLayer.addView(folder);
1898 mDragController.addDropTarget((DropTarget) folder);
1899 } else {
1900 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1901 folder.getParent() + ").");
1902 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001903 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001904 }
1905
1906 public void closeFolder() {
1907 Folder folder = mWorkspace.getOpenFolder();
1908 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07001909 if (folder.isEditingName()) {
1910 folder.dismissEditingName();
1911 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001912 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07001913
1914 // Dismiss the folder cling
1915 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07001916 }
1917 }
1918
1919 void closeFolder(Folder folder) {
1920 folder.getInfo().opened = false;
1921
1922 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1923 if (parent != null) {
1924 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1925 shrinkAndFadeInFolderIcon(fi);
1926 }
1927 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 }
1929
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001931 if (mState != State.WORKSPACE) {
1932 return false;
1933 }
1934
Joe Onorato9c1289c2009-08-17 11:03:03 -04001935 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 return false;
1937 }
1938
1939 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001940 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 }
1942
Michael Jurka0280c3b2010-09-17 15:00:07 -07001943 resetAddInfo();
1944 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001946 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001947 return true;
1948 }
1949
Winson Chung3d503fb2011-07-13 17:25:49 -07001950 // The hotseat touch handling does not go through Workspace, and we always allow long press
1951 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001952 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001953 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1954 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001955 if (itemUnderLongClick == null) {
1956 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001957 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1958 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001959 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001961 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001963 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 }
1965 }
1966 }
1967 return true;
1968 }
1969
Winson Chung3d503fb2011-07-13 17:25:49 -07001970 boolean isHotseatLayout(View layout) {
1971 return mHotseat != null && layout != null &&
1972 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1973 }
1974 Hotseat getHotseat() {
1975 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001976 }
1977
Winson Chung3d503fb2011-07-13 17:25:49 -07001978 /**
1979 * Returns the CellLayout of the specified container at the specified screen.
1980 */
1981 CellLayout getCellLayout(long container, int screen) {
1982 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1983 if (mHotseat != null) {
1984 return mHotseat.getLayout();
1985 } else {
1986 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001987 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001988 } else {
1989 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001990 }
1991 }
1992
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 Workspace getWorkspace() {
1994 return mWorkspace;
1995 }
1996
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 @Override
1998 protected Dialog onCreateDialog(int id) {
1999 switch (id) {
2000 case DIALOG_CREATE_SHORTCUT:
2001 return new CreateShortcut().createDialog();
2002 case DIALOG_RENAME_FOLDER:
2003 return new RenameFolder().createDialog();
2004 }
2005
2006 return super.onCreateDialog(id);
2007 }
2008
2009 @Override
2010 protected void onPrepareDialog(int id, Dialog dialog) {
2011 switch (id) {
2012 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 break;
2014 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002015 if (mFolderInfo != null) {
2016 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2017 final CharSequence text = mFolderInfo.title;
2018 input.setText(text);
2019 input.setSelection(0, text.length());
2020 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 break;
2022 }
2023 }
2024
2025 void showRenameDialog(FolderInfo info) {
2026 mFolderInfo = info;
2027 mWaitingForResult = true;
2028 showDialog(DIALOG_RENAME_FOLDER);
2029 }
2030
Adam Cohenfbba09b2011-07-18 21:43:05 -07002031 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002032 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002033 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2034 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 mWaitingForResult = true;
2036 showDialog(DIALOG_CREATE_SHORTCUT);
2037 }
2038
2039 private class RenameFolder {
2040 private EditText mInput;
2041
2042 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2044 mInput = (EditText) layout.findViewById(R.id.folder_name);
2045
2046 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2047 builder.setIcon(0);
2048 builder.setTitle(getString(R.string.rename_folder_title));
2049 builder.setCancelable(true);
2050 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2051 public void onCancel(DialogInterface dialog) {
2052 cleanup();
2053 }
2054 });
2055 builder.setNegativeButton(getString(R.string.cancel_action),
2056 new Dialog.OnClickListener() {
2057 public void onClick(DialogInterface dialog, int which) {
2058 cleanup();
2059 }
2060 }
2061 );
2062 builder.setPositiveButton(getString(R.string.rename_action),
2063 new Dialog.OnClickListener() {
2064 public void onClick(DialogInterface dialog, int which) {
2065 changeFolderName();
2066 }
2067 }
2068 );
2069 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002070
2071 final AlertDialog dialog = builder.create();
2072 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2073 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002074 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002075 mInput.requestFocus();
2076 InputMethodManager inputManager = (InputMethodManager)
2077 getSystemService(Context.INPUT_METHOD_SERVICE);
2078 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002079 }
2080 });
2081
2082 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 }
2084
2085 private void changeFolderName() {
2086 final String name = mInput.getText().toString();
2087 if (!TextUtils.isEmpty(name)) {
2088 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002089 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 mFolderInfo.title = name;
2091 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2092
Joe Onorato9c1289c2009-08-17 11:03:03 -04002093 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002094 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002095 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 } else {
2097 final FolderIcon folderIcon = (FolderIcon)
2098 mWorkspace.getViewForTag(mFolderInfo);
2099 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002100 // TODO: At some point we'll probably want some version of setting
2101 // the text for a folder icon.
2102 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 getWorkspace().requestLayout();
2104 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002105 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002106 mWorkspaceLoading = true;
2107 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 }
2109 }
2110 }
2111 cleanup();
2112 }
2113
2114 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 dismissDialog(DIALOG_RENAME_FOLDER);
2116 mWaitingForResult = false;
2117 mFolderInfo = null;
2118 }
2119 }
2120
Daniel Sandler843e8602010-06-07 14:59:01 -04002121 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2122 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002123 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002124 }
2125
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002126 // AllAppsView.Watcher
2127 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002128 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002129 mWorkspace.setVisibility(View.GONE);
2130 }
2131 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002132
2133 /**
2134 * Helper method for the cameraZoomIn/cameraZoomOut animations
2135 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002136 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002137 * @param scaleFactor The scale factor used for the zoom
2138 */
2139 private void setPivotsForZoom(View view, State state, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002140 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002141 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002142 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002143
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002144 void updateWallpaperVisibility(boolean visible) {
2145 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2146 int curflags = getWindow().getAttributes().flags
2147 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2148 if (wpflags != curflags) {
2149 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2150 }
2151 }
2152
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002153 /**
2154 * Zoom the camera out from the workspace to reveal 'toView'.
2155 * Assumes that the view to show is anchored at either the very top or very bottom
2156 * of the screen.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002157 * @param toState The state to zoom out to. Must be APPS_CUSTOMIZE.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002158 */
Winson Chungc07918d2011-07-01 15:35:26 -07002159 private void cameraZoomOut(State toState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002160 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002161 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002162
Winson Chungf0ea4d32011-06-06 14:27:16 -07002163 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2164 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2165 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2166 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002167 final int startDelay =
2168 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002169
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002170 setPivotsForZoom(toView, toState, scale);
2171
Michael Jurkad74c9842011-07-10 12:44:21 -07002172 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002173 mWorkspace.changeState(Workspace.State.SMALL, animated);
Adam Cohencff6af82011-09-13 14:51:53 -07002174 //hideHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002175
2176 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002177 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002178 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002179 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2180 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002181 ((View) toView.getParent()).invalidate();
2182 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002183 toView.setFastScaleX(a * scale + b * 1f);
2184 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002185 }
2186 });
Adam Cohen61033d32010-11-15 18:29:44 -08002187
Winson Chungf0ea4d32011-06-06 14:27:16 -07002188 toView.setVisibility(View.VISIBLE);
2189 toView.setFastAlpha(0f);
2190 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2191 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2192 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2193 public void onAnimationUpdate(float a, float b) {
2194 // don't need to invalidate because we do so above
2195 toView.setFastAlpha(a * 0f + b * 1f);
2196 }
2197 });
Adam Cohencff6af82011-09-13 14:51:53 -07002198 alphaAnim.setStartDelay(startDelay);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002199 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002200
Michael Jurkaabded662011-03-04 12:06:57 -08002201 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002202 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, scaleAnim,
2203 false);
Michael Jurkabfadaad2011-01-31 21:35:39 -08002204 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002205 scaleAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002206 boolean animationCancelled = false;
2207
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002208 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002209 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002210 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002211 // Prepare the position
2212 toView.setTranslationX(0.0f);
2213 toView.setTranslationY(0.0f);
2214 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002215 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002216 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002217 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002218 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002219 // If we don't set the final scale values here, if this animation is cancelled
2220 // it will have the wrong scale value and subsequent cameraPan animations will
2221 // not fix that
2222 toView.setScaleX(1.0f);
2223 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002224 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002225 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance,
2226 scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002227 }
Winson Chung32174c82011-07-19 15:47:55 -07002228
2229 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2230 // Hide the workspace scrollbar
2231 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002232 mWorkspace.hideDockDivider(true);
Winson Chung32174c82011-07-19 15:47:55 -07002233 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002234 if (!animationCancelled) {
2235 updateWallpaperVisibility(false);
2236 }
2237 }
2238
Adam Cohencff6af82011-09-13 14:51:53 -07002239 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002240 public void onAnimationCancel(Animator animation) {
2241 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002242 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002243 });
2244
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002245 // toView should appear right at the end of the workspace shrink animation
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002246
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002247 if (mStateAnimation != null) mStateAnimation.cancel();
2248 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002249 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002250 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002251 } else {
2252 toView.setTranslationX(0.0f);
2253 toView.setTranslationY(0.0f);
2254 toView.setScaleX(1.0f);
2255 toView.setScaleY(1.0f);
2256 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002257 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002258 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002259 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, null, false);
2260 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance, null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002261
Winson Chungc07918d2011-07-01 15:35:26 -07002262 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2263 // Hide the workspace scrollbar
2264 mWorkspace.hideScrollingIndicator(true);
Winson Chungbb6f6a52011-07-25 17:55:44 -07002265 mWorkspace.hideDockDivider(true);
Winson Chungc07918d2011-07-01 15:35:26 -07002266 }
Michael Jurkaabded662011-03-04 12:06:57 -08002267 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002268 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002269 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002270 }
2271
2272 /**
2273 * Zoom the camera back into the workspace, hiding 'fromView'.
2274 * This is the opposite of cameraZoomOut.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002275 * @param fromState The current state (must be APPS_CUSTOMIZE).
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002276 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002277 */
Winson Chungc07918d2011-07-01 15:35:26 -07002278 private void cameraZoomIn(State fromState, boolean animated, final boolean springLoaded) {
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002279 Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002280 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002281
Winson Chungf0ea4d32011-06-06 14:27:16 -07002282 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2283 final float scaleFactor = (float)
2284 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2285 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002286
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002287 setPivotsForZoom(fromView, fromState, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002288 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002289 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002290 if (animated) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002291 if (mStateAnimation != null) mStateAnimation.cancel();
2292 mStateAnimation = new AnimatorSet();
Adam Cohen61033d32010-11-15 18:29:44 -08002293
Michael Jurka742574b2011-02-02 23:51:01 -08002294 final float oldScaleX = fromView.getScaleX();
2295 final float oldScaleY = fromView.getScaleY();
2296
2297 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2298 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002299 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2300 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002301 ((View)fromView.getParent()).fastInvalidate();
2302 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2303 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2304 }
2305 });
Michael Jurkaabded662011-03-04 12:06:57 -08002306 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002307 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Adam Cohencff6af82011-09-13 14:51:53 -07002308 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002309 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2310 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002311 // don't need to invalidate because we do so above
2312 fromView.setFastAlpha(a * 1f + b * 0f);
2313 }
2314 });
Michael Jurkaabded662011-03-04 12:06:57 -08002315 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002316 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, alphaAnim,
2317 true);
Michael Jurka2763be32011-02-24 11:19:57 -08002318 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002319 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002320 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002321 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002322 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002323 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002324 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002325 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance,
2326 alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002327 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002328 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002329 }
2330 });
2331
Winson Chungf0ea4d32011-06-06 14:27:16 -07002332 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002333 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002334 } else {
2335 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002336 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002337 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, null, true);
2338 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance, null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002339 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002340 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002341 }
2342
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002343 void showWorkspace(boolean animated) {
Adam Cohencff6af82011-09-13 14:51:53 -07002344 Resources res = getResources();
2345 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2346
2347 mWorkspace.changeState(Workspace.State.NORMAL, animated, stagger);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002348 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002349 closeAllApps(animated);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002350 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002351
Adam Cohen7777d962011-08-18 18:58:38 -07002352 mWorkspace.showDockDivider(!animated);
2353 mWorkspace.flashScrollingIndicator();
2354
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002355 // Change the state *after* we've called all the transition code
2356 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002357
Winson Chung5fb63472011-02-02 17:03:37 -08002358 // Resume the auto-advance of widgets
2359 mUserPresent = true;
2360 updateRunning();
2361
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002362 // send an accessibility event to announce the context change
2363 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002364 }
2365
Adam Cohen7777d962011-08-18 18:58:38 -07002366 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002367 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002368 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Winson Chungb26f3d62011-06-02 10:49:29 -07002369 cameraZoomIn(State.APPS_CUSTOMIZE, true, true);
Winson Chungc07918d2011-07-01 15:35:26 -07002370 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002371 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002372 }
Adam Cohen7777d962011-08-18 18:58:38 -07002373
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002374 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chung09bfc452011-09-09 11:30:20 -07002375 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2376
Winson Chunge7a03942011-08-05 15:05:12 -07002377 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002378 @Override
2379 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002380 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002381 // Before we show workspace, hide all apps again because
2382 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2383 // clean up our state transition functions
2384 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002385 mSearchDropTargetBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002386 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002387 } else {
2388 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002389 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002390 }
2391 }, (extendedDelay ?
2392 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2393 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2394 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002395 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002396 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Winson Chungb26f3d62011-06-02 10:49:29 -07002397 cameraZoomOut(State.APPS_CUSTOMIZE, true, true);
2398 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002399 }
2400 // Otherwise, we are not in spring loaded mode, so don't do anything.
2401 }
2402
Adam Cohenfc53cd22011-07-20 15:45:11 -07002403 public boolean isAllAppsCustomizeOpen() {
2404 return mState == State.APPS_CUSTOMIZE;
2405 }
2406
Winson Chungf0ea4d32011-06-06 14:27:16 -07002407 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002408 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002409 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002410 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002411 if (!LauncherApplication.isScreenLarge()) {
2412 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002413 int duration = mSearchDropTargetBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002414 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002415 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002416 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002417 }
2418 }
2419 }
2420
2421 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002422 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002423 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002424 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002425 if (!LauncherApplication.isScreenLarge()) {
2426 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002427 int duration = mSearchDropTargetBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002428 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002429 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002430 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002431 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002432 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002433 }
2434
Winson Chung785d2eb2011-04-14 16:08:02 -07002435 void showAllApps(boolean animated) {
2436 if (mState != State.WORKSPACE) return;
Winson Chung785d2eb2011-04-14 16:08:02 -07002437
Winson Chungf0ea4d32011-06-06 14:27:16 -07002438 cameraZoomOut(State.APPS_CUSTOMIZE, animated, false);
2439 mAppsCustomizeTabHost.requestFocus();
Winson Chung785d2eb2011-04-14 16:08:02 -07002440
Winson Chung3d503fb2011-07-13 17:25:49 -07002441 // Hide the search bar and hotseat
Winson Chungc51db6a2011-10-05 11:44:49 -07002442 mSearchDropTargetBar.hideSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002443
Winson Chungf0ea4d32011-06-06 14:27:16 -07002444 // Change the state *after* we've called all the transition code
2445 mState = State.APPS_CUSTOMIZE;
Winson Chung785d2eb2011-04-14 16:08:02 -07002446
2447 // Pause the auto-advance of widgets until we are out of AllApps
2448 mUserPresent = false;
2449 updateRunning();
Adam Cohen2f84ef22011-07-26 17:16:44 -07002450 closeFolder();
Winson Chung785d2eb2011-04-14 16:08:02 -07002451
2452 // Send an accessibility event to announce the context change
2453 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2454 }
2455
Joe Onoratob2061212009-11-24 16:13:54 -05002456 /**
Joe Onorato7e4ed992009-12-03 13:10:49 -08002457 * Things to test when changing this code.
Joe Onoratob2061212009-11-24 16:13:54 -05002458 * - Home from workspace
2459 * - from center screen
2460 * - from other screens
2461 * - Home from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002462 * - from center screen
2463 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002464 * - Back from all apps
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002465 * - from center screen
2466 * - from other screens
Joe Onoratob2061212009-11-24 16:13:54 -05002467 * - Launch app from workspace and quit
2468 * - with back
2469 * - with home
2470 * - Launch app from all apps and quit
2471 * - with back
2472 * - with home
Joe Onorato7e4ed992009-12-03 13:10:49 -08002473 * - Go to a screen that's not the default, then all
2474 * apps, and launch and app, and go back
2475 * - with back
2476 * -with home
Joe Onoratob2061212009-11-24 16:13:54 -05002477 * - On workspace, long press power and go back
2478 * - with back
2479 * - with home
2480 * - On all apps, long press power and go back
2481 * - with back
2482 * - with home
2483 * - On workspace, power off
2484 * - On all apps, power off
Joe Onorato7e4ed992009-12-03 13:10:49 -08002485 * - Launch an app and turn off the screen while in that app
2486 * - Go back with home key
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002487 * - Go back with back key TODO: make this not go to workspace
Joe Onorato7e4ed992009-12-03 13:10:49 -08002488 * - From all apps
2489 * - From workspace
Joe Onoratoeffc4a82010-04-15 11:48:13 -07002490 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2491 * - From all apps
2492 * - From the center workspace
2493 * - From another workspace
Joe Onoratob2061212009-11-24 16:13:54 -05002494 */
Joe Onorato7bb17492009-09-24 17:51:01 -07002495 void closeAllApps(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002496 if (mState == State.APPS_CUSTOMIZE || mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2497 mWorkspace.setVisibility(View.VISIBLE);
2498 cameraZoomIn(State.APPS_CUSTOMIZE, animated, false);
Winson Chung785d2eb2011-04-14 16:08:02 -07002499
Winson Chung3d503fb2011-07-13 17:25:49 -07002500 // Show the search bar and hotseat
Winson Chungc51db6a2011-10-05 11:44:49 -07002501 mSearchDropTargetBar.showSearchBar(animated);
Winson Chung785d2eb2011-04-14 16:08:02 -07002502
Winson Chungf0ea4d32011-06-06 14:27:16 -07002503 // Set focus to the AppsCustomize button
Winson Chung3d503fb2011-07-13 17:25:49 -07002504 if (mAllAppsButton != null) {
2505 mAllAppsButton.requestFocus();
2506 }
Joe Onoratoe77c08d2009-08-01 00:01:20 -07002507 }
Joe Onorato7404ee42009-07-31 11:54:44 -07002508 }
2509
Joe Onorato7c312c12009-08-13 21:36:53 -07002510 void lockAllApps() {
2511 // TODO
2512 }
2513
2514 void unlockAllApps() {
2515 // TODO
2516 }
2517
Patrick Dubroy5f445422011-02-18 14:35:21 -08002518 /**
2519 * Add an item from all apps or customize onto the given workspace screen.
2520 * If layout is null, add to the current screen.
2521 */
2522 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002523 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2524 showOutOfSpaceMessage();
Patrick Dubroy5f445422011-02-18 14:35:21 -08002525 } else {
2526 layout.animateDrop();
Michael Jurka213d9632010-07-28 11:29:25 -07002527 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002528 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002529
Winson Chungdff8ebb2011-09-08 17:25:31 -07002530 /** Maps the current orientation to an index for referencing orientation correct global icons */
2531 private int getCurrentOrientationIndexForGlobalIcons() {
2532 // default - 0, landscape - 1
2533 switch (getResources().getConfiguration().orientation) {
2534 case Configuration.ORIENTATION_LANDSCAPE:
2535 return 1;
2536 default:
2537 return 0;
2538 }
2539 }
2540
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002541 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002542 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002543 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002544 // Look for the toolbar icon specified in the activity meta-data
2545 Bundle metaData = packageManager.getActivityInfo(
2546 activityName, PackageManager.GET_META_DATA).metaData;
2547 if (metaData != null) {
2548 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2549 if (iconResId != 0) {
2550 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002551 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002552 }
2553 }
2554 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002555 // This can happen if the activity defines an invalid drawable
2556 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2557 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002558 } catch (Resources.NotFoundException nfe) {
2559 // This can happen if the activity defines an invalid drawable
2560 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2561 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002562 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002563 return null;
2564 }
2565
2566 // if successful in getting icon, return it; otherwise, set button to use default drawable
2567 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2568 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2569 TextView button = (TextView) findViewById(buttonId);
2570 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002571 Resources r = getResources();
2572 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2573 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002574
2575 // If we were unable to find the icon via the meta-data, use a generic one
2576 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002577 toolbarIcon = r.getDrawable(fallbackDrawableId);
2578 toolbarIcon.setBounds(0, 0, w, h);
2579 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002580 return null;
2581 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002582 toolbarIcon.setBounds(0, 0, w, h);
2583 button.setCompoundDrawables(toolbarIcon, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002584 return toolbarIcon.getConstantState();
2585 }
2586 }
2587
2588 // if successful in getting icon, return it; otherwise, set button to use default drawable
2589 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2590 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2591 ImageView button = (ImageView) findViewById(buttonId);
2592 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2593
Michael Jurka19e0fc52011-07-22 18:00:21 -07002594 if (button != null) {
2595 // If we were unable to find the icon via the meta-data, use a
2596 // generic one
2597 if (toolbarIcon == null) {
2598 button.setImageResource(fallbackDrawableId);
2599 } else {
2600 button.setImageDrawable(toolbarIcon);
2601 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002602 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002603
2604 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2605
Michael Jurka0423dcf2010-10-05 14:56:18 -07002606 }
2607
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002608 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2609 TextView button = (TextView) findViewById(buttonId);
2610 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2611 }
2612
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002613 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002614 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002615 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002616 }
2617
Winson Chungc51db6a2011-10-05 11:44:49 -07002618 private boolean updateGlobalSearchIcon() {
2619 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002620 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2621 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002622 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002623 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002624
Winson Chung1cad91e2011-05-25 17:41:01 -07002625 final SearchManager searchManager =
2626 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2627 ComponentName activityName = searchManager.getGlobalSearchActivity();
2628 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002629 int coi = getCurrentOrientationIndexForGlobalIcons();
2630 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002631 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002632 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002633 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2634 searchButton.setVisibility(View.VISIBLE);
2635 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002636 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002637 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002638 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002639 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2640 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2641 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002642 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002643 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002644 }
2645 }
2646
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002647 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002648 updateButtonWithDrawable(R.id.search_button, d);
2649 }
2650
Winson Chungc51db6a2011-10-05 11:44:49 -07002651 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002652 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002653 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002654 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002655
Winson Chungc51db6a2011-10-05 11:44:49 -07002656 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002657 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2658 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002659 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002660 int coi = getCurrentOrientationIndexForGlobalIcons();
2661 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002662 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002663 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002664 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002665 voiceButton.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002666 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002667 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002668 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002669 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002670 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002671 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002672 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002673 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002674
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002675 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002676 updateButtonWithDrawable(R.id.voice_button, d);
2677 }
2678
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002679 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002680 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002681 */
2682 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002683 final View marketButton = findViewById(R.id.market_button);
2684 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2685 // Find the app market activity by resolving an intent.
2686 // (If multiple app markets are installed, it will return the ResolverActivity.)
2687 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002688 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002689 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002690 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002691 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002692 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002693 marketButton.setVisibility(View.VISIBLE);
2694 } else {
2695 // We should hide and disable the view so that we don't try and restore the visibility
2696 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2697 marketButton.setVisibility(View.GONE);
2698 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002699 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002700 }
2701
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002702 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002703 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002704 }
2705
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002706 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002707 * Displays the shortcut creation dialog and launches, if necessary, the
2708 * appropriate activity.
2709 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002710 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002711 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2712 DialogInterface.OnShowListener {
2713
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002714 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002715
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002716 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002717 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002718
Winson Chung55b65502011-05-26 12:03:43 -07002719 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2720 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002721 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002722
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002723 AlertDialog dialog = builder.create();
2724 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002725 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002726 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002727
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002728 return dialog;
2729 }
2730
2731 public void onCancel(DialogInterface dialog) {
2732 mWaitingForResult = false;
2733 cleanup();
2734 }
2735
Romain Guycbb89e42009-06-08 15:52:54 -07002736 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002737 mWaitingForResult = false;
2738 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002739 }
2740
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002741 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002742 try {
2743 dismissDialog(DIALOG_CREATE_SHORTCUT);
2744 } catch (Exception e) {
2745 // An exception is thrown if the dialog is not visible, which is fine
2746 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002747 }
2748
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002749 /**
2750 * Handle the action clicked in the "Add to home" dialog.
2751 */
2752 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002753 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002754
Winson Chung55b65502011-05-26 12:03:43 -07002755 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2756 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002757 case AddAdapter.ITEM_APPLICATION: {
2758 if (mAppsCustomizeTabHost != null) {
2759 mAppsCustomizeTabHost.selectAppsTab();
2760 }
2761 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002762 break;
2763 }
Winson Chung55b65502011-05-26 12:03:43 -07002764 case AddAdapter.ITEM_APPWIDGET: {
2765 if (mAppsCustomizeTabHost != null) {
2766 mAppsCustomizeTabHost.selectWidgetsTab();
2767 }
2768 showAllApps(true);
2769 break;
2770 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002771 case AddAdapter.ITEM_WALLPAPER: {
2772 startWallpaper();
2773 break;
2774 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002775 }
2776 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002777
2778 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002779 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002780 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002781 }
2782
2783 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002784 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002785 */
2786 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2787 @Override
2788 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002789 closeSystemDialogs();
2790 }
2791 }
2792
2793 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002794 * Receives notifications whenever the appwidgets are reset.
2795 */
2796 private class AppWidgetResetObserver extends ContentObserver {
2797 public AppWidgetResetObserver() {
2798 super(new Handler());
2799 }
2800
2801 @Override
2802 public void onChange(boolean selfChange) {
2803 onAppWidgetReset();
2804 }
2805 }
2806
2807 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002808 * If the activity is currently paused, signal that we need to re-run the loader
2809 * in onResume.
2810 *
2811 * This needs to be called from incoming places where resources might have been loaded
2812 * while we are paused. That is becaues the Configuration might be wrong
2813 * when we're not running, and if it comes back to what it was when we
2814 * were paused, we are not restarted.
2815 *
2816 * Implementation of the method from LauncherModel.Callbacks.
2817 *
2818 * @return true if we are currently paused. The caller might be able to
2819 * skip some work in that case since we will come back again.
2820 */
2821 public boolean setLoadOnResume() {
2822 if (mPaused) {
2823 Log.i(TAG, "setLoadOnResume");
2824 mOnResumeNeedsLoad = true;
2825 return true;
2826 } else {
2827 return false;
2828 }
2829 }
2830
2831 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002832 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002833 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002834 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002835 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002836 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002837 } else {
2838 return SCREEN_COUNT / 2;
2839 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002840 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002841
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002842
Joe Onorato9c1289c2009-08-17 11:03:03 -04002843 /**
2844 * Refreshes the shortcuts shown on the workspace.
2845 *
2846 * Implementation of the method from LauncherModel.Callbacks.
2847 */
2848 public void startBinding() {
2849 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002850
2851 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002852 int count = workspace.getChildCount();
2853 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002854 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002855 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2856 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002857 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002858 if (mHotseat != null) {
2859 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002860 }
2861 }
2862
2863 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002864 * Bind the items start-end from the list.
2865 *
2866 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002867 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002868 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002869 setLoadOnResume();
2870
Joe Onorato9c1289c2009-08-17 11:03:03 -04002871 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002872 for (int i=start; i<end; i++) {
2873 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002874
2875 // Short circuit if we are loading dock items for a configuration which has no dock
2876 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2877 mHotseat == null) {
2878 continue;
2879 }
2880
Joe Onorato9c1289c2009-08-17 11:03:03 -04002881 switch (item.itemType) {
2882 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2883 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002884 View shortcut = createShortcut((ShortcutInfo)item);
2885 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2886 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002887 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002888 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002889 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002890 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002891 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002892 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2893 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002894 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002895 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002896 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002897 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002898 }
2899
Joe Onorato9c1289c2009-08-17 11:03:03 -04002900 /**
2901 * Implementation of the method from LauncherModel.Callbacks.
2902 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002903 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002904 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002905 sFolders.clear();
2906 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002907 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002908
2909 /**
2910 * Add the views for a widget to the workspace.
2911 *
2912 * Implementation of the method from LauncherModel.Callbacks.
2913 */
2914 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002915 setLoadOnResume();
2916
Daniel Sandler843e8602010-06-07 14:59:01 -04002917 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2918 if (DEBUG_WIDGETS) {
2919 Log.d(TAG, "bindAppWidget: " + item);
2920 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002921 final Workspace workspace = mWorkspace;
2922
2923 final int appWidgetId = item.appWidgetId;
2924 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002925 if (DEBUG_WIDGETS) {
2926 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2927 }
2928
Joe Onorato9c1289c2009-08-17 11:03:03 -04002929 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2930
Joe Onorato9c1289c2009-08-17 11:03:03 -04002931 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2932 item.hostView.setTag(item);
2933
Winson Chung3d503fb2011-07-13 17:25:49 -07002934 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002935 item.cellY, item.spanX, item.spanY, false);
2936
Adam Cohended9f8d2010-11-03 13:25:16 -07002937 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
2938
Joe Onorato9c1289c2009-08-17 11:03:03 -04002939 workspace.requestLayout();
2940
Daniel Sandler843e8602010-06-07 14:59:01 -04002941 if (DEBUG_WIDGETS) {
2942 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
2943 + (SystemClock.uptimeMillis()-start) + "ms");
2944 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002945 }
2946
2947 /**
2948 * Callback saying that there aren't any more items to bind.
2949 *
2950 * Implementation of the method from LauncherModel.Callbacks.
2951 */
2952 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002953 setLoadOnResume();
2954
Joe Onorato9c1289c2009-08-17 11:03:03 -04002955 if (mSavedState != null) {
2956 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07002957 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002958 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002959 mSavedState = null;
2960 }
2961
2962 if (mSavedInstanceState != null) {
2963 super.onRestoreInstanceState(mSavedInstanceState);
2964 mSavedInstanceState = null;
2965 }
2966
Joe Onorato9c1289c2009-08-17 11:03:03 -04002967 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08002968
2969 // If we received the result of any pending adds while the loader was running (e.g. the
2970 // widget configuration forced an orientation change), process them now.
2971 for (int i = 0; i < sPendingAddList.size(); i++) {
2972 completeAdd(sPendingAddList.get(i));
2973 }
2974 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002975
Winson Chungc51db6a2011-10-05 11:44:49 -07002976 // Update the market app icon as necessary (the other icons will be managed in response to
2977 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002978 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07002979
2980 mWorkspace.post(mBuildLayersRunnable);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002981 }
2982
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002983 @Override
2984 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07002985 boolean searchVisible = updateGlobalSearchIcon();
2986 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
2987 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01002988 }
2989
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002990 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002991 * Add the icons for all apps.
2992 *
2993 * Implementation of the method from LauncherModel.Callbacks.
2994 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07002995 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
2996 // Remove the progress bar entirely; we could also make it GONE
2997 // but better to remove it since we know it's not going to be used
2998 View progressBar = mAppsCustomizeTabHost.
2999 findViewById(R.id.apps_customize_progress_bar);
3000 if (progressBar != null) {
3001 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003002 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003003 // We just post the call to setApps so the user sees the progress bar
3004 // disappear-- otherwise, it just looks like the progress bar froze
3005 // which doesn't look great
3006 mAppsCustomizeTabHost.post(new Runnable() {
3007 public void run() {
3008 if (mAppsCustomizeContent != null) {
3009 mAppsCustomizeContent.setApps(apps);
3010 }
3011 }
3012 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003013 }
3014
3015 /**
3016 * A package was installed.
3017 *
3018 * Implementation of the method from LauncherModel.Callbacks.
3019 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003020 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003021 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003022 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003023
Winson Chung785d2eb2011-04-14 16:08:02 -07003024 if (mAppsCustomizeContent != null) {
3025 mAppsCustomizeContent.addApps(apps);
3026 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003027 }
3028
3029 /**
3030 * A package was updated.
3031 *
3032 * Implementation of the method from LauncherModel.Callbacks.
3033 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003034 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003035 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003036 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003037 if (mWorkspace != null) {
3038 mWorkspace.updateShortcuts(apps);
3039 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003040
Winson Chung785d2eb2011-04-14 16:08:02 -07003041 if (mAppsCustomizeContent != null) {
3042 mAppsCustomizeContent.updateApps(apps);
3043 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003044 }
3045
3046 /**
3047 * A package was uninstalled.
3048 *
3049 * Implementation of the method from LauncherModel.Callbacks.
3050 */
Joe Onorato36115782010-06-17 13:28:48 -04003051 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003052 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003053 if (permanent) {
3054 mWorkspace.removeItems(apps);
3055 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003056
Winson Chung785d2eb2011-04-14 16:08:02 -07003057 if (mAppsCustomizeContent != null) {
3058 mAppsCustomizeContent.removeApps(apps);
3059 }
Winson Chunga1820962011-10-03 16:31:06 -07003060
3061 // Notify the drag controller
3062 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003063 }
Joe Onoratobe386092009-11-17 17:32:16 -08003064
3065 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003066 * A number of packages were updated.
3067 */
3068 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003069 if (mAppsCustomizeContent != null) {
3070 mAppsCustomizeContent.onPackagesUpdated();
3071 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003072 }
3073
Winson Chung400438b2011-01-16 17:53:48 -08003074 private int mapConfigurationOriActivityInfoOri(int configOri) {
3075 final Display d = getWindowManager().getDefaultDisplay();
3076 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3077 switch (d.getRotation()) {
3078 case Surface.ROTATION_0:
3079 case Surface.ROTATION_180:
3080 // We are currently in the same basic orientation as the natural orientation
3081 naturalOri = configOri;
3082 break;
3083 case Surface.ROTATION_90:
3084 case Surface.ROTATION_270:
3085 // We are currently in the other basic orientation to the natural orientation
3086 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3087 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3088 break;
3089 }
3090
3091 int[] oriMap = {
3092 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3093 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3094 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3095 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3096 };
3097 // Since the map starts at portrait, we need to offset if this device's natural orientation
3098 // is landscape.
3099 int indexOffset = 0;
3100 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3101 indexOffset = 1;
3102 }
3103 return oriMap[(d.getRotation() + indexOffset) % 4];
3104 }
Adam Cohen446e9402011-09-15 18:21:21 -07003105
3106 public void lockScreenOrientationOnLargeUI() {
3107 if (LauncherApplication.isScreenLarge()) {
3108 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3109 .getConfiguration().orientation));
3110 }
Winson Chung400438b2011-01-16 17:53:48 -08003111 }
Adam Cohen446e9402011-09-15 18:21:21 -07003112 public void unlockScreenOrientationOnLargeUI() {
3113 if (LauncherApplication.isScreenLarge()) {
3114 mHandler.postDelayed(new Runnable() {
3115 public void run() {
3116 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3117 }
3118 }, mRestoreScreenOrientationDelay);
3119 }
Winson Chung400438b2011-01-16 17:53:48 -08003120 }
3121
Winson Chung82f55532011-08-09 14:14:23 -07003122 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003123 private static final String PREFS_KEY = "com.android.launcher2.prefs";
3124 private boolean isClingsEnabled() {
Winson Chung82f55532011-08-09 14:14:23 -07003125 // TEMPORARY: DISABLE CLINGS ON LARGE UI
Winson Chung7d7541e2011-09-16 20:14:36 -07003126 if (LauncherApplication.isScreenLarge()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003127 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003128 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003129
Winson Chung7d7541e2011-09-16 20:14:36 -07003130 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003131 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003132 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3133 Cling cling = (Cling) findViewById(clingId);
3134 if (cling != null) {
3135 cling.init(this, positionData);
3136 cling.setVisibility(View.VISIBLE);
3137 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3138 if (animate) {
3139 cling.buildLayer();
3140 cling.setAlpha(0f);
3141 cling.animate()
3142 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003143 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003144 .setDuration(SHOW_CLING_DURATION)
3145 .setStartDelay(delay)
3146 .start();
3147 } else {
3148 cling.setAlpha(1f);
3149 }
3150 }
3151 return cling;
3152 }
3153 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003154 if (cling != null) {
3155 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003156 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003157 anim.addListener(new AnimatorListenerAdapter() {
3158 public void onAnimationEnd(Animator animation) {
3159 cling.setVisibility(View.GONE);
3160 cling.cleanup();
3161 SharedPreferences prefs =
3162 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3163 SharedPreferences.Editor editor = prefs.edit();
3164 editor.putBoolean(flag, true);
3165 editor.commit();
3166 };
3167 });
3168 anim.start();
3169 }
3170 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003171 private void removeCling(int id) {
3172 final View cling = findViewById(id);
3173 if (cling != null) {
3174 final ViewGroup parent = (ViewGroup) cling.getParent();
3175 parent.post(new Runnable() {
3176 @Override
3177 public void run() {
3178 parent.removeView(cling);
3179 }
3180 });
3181 }
3182 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003183 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003184 // Enable the clings only if they have not been dismissed before
3185 SharedPreferences prefs =
3186 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003187 if (isClingsEnabled() && !prefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003188 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003189 } else {
3190 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003191 }
3192 }
3193 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003194 // Enable the clings only if they have not been dismissed before
3195 SharedPreferences prefs =
3196 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003197 if (isClingsEnabled() && !prefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003198 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003199 } else {
3200 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003201 }
3202 }
3203 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003204 // Enable the clings only if they have not been dismissed before
3205 SharedPreferences prefs =
3206 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
3207 Cling cling = null;
Winson Chung9d9d74f2011-09-19 11:49:12 -07003208 if (isClingsEnabled() && !prefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003209 cling = initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003210 } else {
3211 removeCling(R.id.folder_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003212 }
3213 return cling;
3214 }
3215 public boolean isFolderClingVisible() {
3216 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003217 if (cling != null) {
3218 return cling.getVisibility() == View.VISIBLE;
3219 }
3220 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003221 }
Winson Chung82f55532011-08-09 14:14:23 -07003222 public void dismissWorkspaceCling(View v) {
3223 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003224 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003225 }
3226 public void dismissAllAppsCling(View v) {
3227 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003228 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3229 }
3230 public void dismissFolderCling(View v) {
3231 Cling cling = (Cling) findViewById(R.id.folder_cling);
3232 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003233 }
3234
Winson Chung80baf5a2010-08-09 16:03:15 -07003235 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003236 * Prints out out state for debugging.
3237 */
3238 public void dumpState() {
3239 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003240 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003241 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3242 Log.d(TAG, "mRestoring=" + mRestoring);
3243 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3244 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003245 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003246 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003247
Winson Chung785d2eb2011-04-14 16:08:02 -07003248 if (mAppsCustomizeContent != null) {
3249 mAppsCustomizeContent.dumpState();
3250 }
Joe Onoratobe386092009-11-17 17:32:16 -08003251 Log.d(TAG, "END launcher2 dump state");
3252 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003253
3254 @Override
3255 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3256 super.dump(prefix, fd, writer, args);
3257 writer.println(" ");
3258 writer.println("Debug logs: ");
3259 for (int i = 0; i < sDumpLogs.size(); i++) {
3260 writer.println(" " + sDumpLogs.get(i));
3261 }
3262 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003263}
Michael Jurka2763be32011-02-24 11:19:57 -08003264
Michael Jurkaabded662011-03-04 12:06:57 -08003265interface LauncherTransitionable {
Winson Chung7d7541e2011-09-16 20:14:36 -07003266 void onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace);
3267 void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003268}