blob: 02a55a81400a8d019698b94a25f37985f99ccec2 [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;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070037import android.content.ClipData;
38import android.content.ClipDescription;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
42import android.content.DialogInterface;
43import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070048import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080049import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070050import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070053import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040054import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070055import android.os.AsyncTask;
Adam Cohen50370f32011-08-25 18:57:14 -070056import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080057import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020058import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080059import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070060import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040061import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080062import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070063import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080064import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.text.Selection;
66import android.text.SpannableStringBuilder;
67import android.text.TextUtils;
68import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070069import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080070import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080071import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.view.KeyEvent;
73import android.view.LayoutInflater;
74import android.view.Menu;
75import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070076import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080077import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080078import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080079import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070080import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080081import android.view.ViewTreeObserver;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070082import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080083import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070084import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070085import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080086import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080087import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070088import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070090import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070091import android.widget.TextView;
92import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070093
Adam Cohendf2cc412011-04-27 16:56:57 -070094import com.android.common.Search;
95import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070096import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080097
Adam Cohenc0dcf592011-06-01 15:30:43 -070098import java.io.DataInputStream;
99import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700100import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700101import java.io.FileNotFoundException;
102import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700103import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700104import java.util.ArrayList;
105import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800107/**
108 * Default launcher application.
109 */
Michael Jurka946ad472010-07-09 18:05:18 -0700110public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700111 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
112 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800113 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700114 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115
Joe Onorato9c1289c2009-08-17 11:03:03 -0400116 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400117 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700118
Winson Chung70d72102011-08-12 11:24:35 -0700119 private static final int MENU_GROUP_WALLPAPER = 1;
120 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
121 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700122 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
123 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
125 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700126 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127 private static final int REQUEST_PICK_APPLICATION = 6;
128 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700129 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700130 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131
132 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
133
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800134 static final int SCREEN_COUNT = 5;
135 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
Joe Onorato7c312c12009-08-13 21:36:53 -0700137 static final int DIALOG_CREATE_SHORTCUT = 1;
138 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Romain Guy98d01652009-06-30 16:21:04 -0700140 private static final String PREFERENCES = "launcher.preferences";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
142 // Type: int
143 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700144 // Type: int
145 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700147 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
148 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
150 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700151 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700153 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 // Type: boolean
155 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
156 // Type: long
157 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
158
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700159 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
160
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700161 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700162 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700163 private State mState = State.WORKSPACE;
164 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800165 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700166
Joe Onorato9c1289c2009-08-17 11:03:03 -0400167 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700168 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
169 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700170 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700171 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800174 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800176 private final BroadcastReceiver mCloseSystemDialogsReceiver
177 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800178 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
179
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 private LayoutInflater mInflater;
181
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800183 private View mQsbDivider;
184 private View mDockDivider;
185 private DragLayer mDragLayer;
186 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700187
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700188 private AppWidgetManager mAppWidgetManager;
189 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700190
Michael Jurkac9d95c52011-08-29 14:03:34 -0700191 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700192 private int[] mTmpAddItemCellCoordinates = new int[2];
193
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 private FolderInfo mFolderInfo;
195
Winson Chung3d503fb2011-07-13 17:25:49 -0700196 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800197 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700198
Winson Chungc51db6a2011-10-05 11:44:49 -0700199 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700200 private AppsCustomizeTabHost mAppsCustomizeTabHost;
201 private AppsCustomizePagedView mAppsCustomizeContent;
202 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800203
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204 private Bundle mSavedState;
205
206 private SpannableStringBuilder mDefaultKeySsb = null;
207
Joe Onorato9c1289c2009-08-17 11:03:03 -0400208 private boolean mWorkspaceLoading = true;
209
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800210 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211 private boolean mRestoring;
212 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700213 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214
215 private Bundle mSavedInstanceState;
216
Joe Onorato9c1289c2009-08-17 11:03:03 -0400217 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800218 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800219 private boolean mUserPresent = true;
220 private boolean mVisible = false;
221 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400222
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700223 private static LocaleConfiguration sLocaleConfiguration = null;
224
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700225 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700226
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700227 private Intent mAppMarketIntent = null;
228
Adam Cohended9f8d2010-11-03 13:25:16 -0700229 // Related to the auto-advancing of widgets
230 private final int ADVANCE_MSG = 1;
231 private final int mAdvanceInterval = 20000;
232 private final int mAdvanceStagger = 250;
233 private long mAutoAdvanceSentTime;
234 private long mAutoAdvanceTimeLeft = -1;
235 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
236 new HashMap<View, AppWidgetProviderInfo>();
237
Winson Chung400438b2011-01-16 17:53:48 -0800238 // Determines how long to wait after a rotation before restoring the screen orientation to
239 // match the sensor state.
240 private final int mRestoreScreenOrientationDelay = 500;
241
Michael Jurka4ef207b2010-11-29 17:05:45 -0800242 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700243 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
244 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
245 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800246
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700247 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
248
Adam Cohen2801caf2011-05-13 20:57:39 -0700249
Michael Jurkaddd62e92011-02-16 17:49:14 -0800250 private BubbleTextView mWaitingForResume;
251
Michael Jurkac1f5d262011-09-30 19:32:27 -0700252 private Runnable mBuildLayersRunnable = new Runnable() {
253 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700254 if (mWorkspace != null) {
255 mWorkspace.buildPageHardwareLayers();
256 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700257 }
258 };
259
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800260 private static ArrayList<PendingAddArguments> sPendingAddList
261 = new ArrayList<PendingAddArguments>();
262
263 private static class PendingAddArguments {
264 int requestCode;
265 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700266 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800267 int screen;
268 int cellX;
269 int cellY;
270 }
271
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800272 @Override
273 protected void onCreate(Bundle savedInstanceState) {
274 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800275 LauncherApplication app = ((LauncherApplication)getApplication());
276 mModel = app.setLauncher(this);
277 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400278 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700280
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700281 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700282 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
283 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200286 android.os.Debug.startMethodTracing(
287 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 }
289
290 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 setContentView(R.layout.launcher);
292 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700293 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800295 registerContentObservers();
296
Joe Onorato7c312c12009-08-13 21:36:53 -0700297 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700298
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 mSavedState = savedInstanceState;
300 restoreState(mSavedState);
301
Winson Chunga12a2502010-12-20 14:41:35 -0800302 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700303 if (mAppsCustomizeContent != null) {
304 mAppsCustomizeContent.onPackagesUpdated();
305 }
Winson Chunga12a2502010-12-20 14:41:35 -0800306
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800307 if (PROFILE_STARTUP) {
308 android.os.Debug.stopMethodTracing();
309 }
310
311 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400312 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800313 }
314
Michael Jurkac57b7a82011-08-09 22:02:20 -0700315 if (!mModel.isAllAppsLoaded()) {
316 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
317 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
318 }
319
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 // For handling default keys
321 mDefaultKeySsb = new SpannableStringBuilder();
322 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800323
324 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
325 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800326
Winson Chungc51db6a2011-10-05 11:44:49 -0700327 boolean searchVisible = false;
328 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800329 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700330 int coi = getCurrentOrientationIndexForGlobalIcons();
331 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
332 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700333 updateAppMarketIcon();
334 searchVisible = updateGlobalSearchIcon();
335 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700336 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700337 if (sGlobalSearchIcon[coi] != null) {
338 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700339 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700340 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700341 if (sVoiceSearchIcon[coi] != null) {
342 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700343 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700344 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700345 if (sAppMarketIcon[coi] != null) {
346 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800347 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700348 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700349
350 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohendc16b602011-09-26 15:12:33 -0700351 if (LauncherApplication.isScreenLarge() || Build.TYPE.contentEquals("eng")) {
Adam Cohen446e9402011-09-15 18:21:21 -0700352 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
353 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 }
Romain Guycbb89e42009-06-08 15:52:54 -0700355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700357 if (sLocaleConfiguration == null) {
358 new AsyncTask<Void, Void, LocaleConfiguration>() {
359 @Override
360 protected LocaleConfiguration doInBackground(Void... unused) {
361 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
362 readConfiguration(Launcher.this, localeConfiguration);
363 return localeConfiguration;
364 }
365
366 @Override
367 protected void onPostExecute(LocaleConfiguration result) {
368 sLocaleConfiguration = result;
369 checkForLocaleChange(); // recursive, but now with a locale configuration
370 }
371 }.execute();
372 return;
373 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 final Configuration configuration = getResources().getConfiguration();
376
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700377 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 final String locale = configuration.locale.toString();
379
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700380 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 final int mcc = configuration.mcc;
382
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700383 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 final int mnc = configuration.mnc;
385
Romain Guy84f296c2009-11-04 15:00:44 -0800386 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387
Romain Guy84f296c2009-11-04 15:00:44 -0800388 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700389 sLocaleConfiguration.locale = locale;
390 sLocaleConfiguration.mcc = mcc;
391 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700392
Joe Onorato0589f0f2010-02-08 13:44:00 -0800393 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700394
395 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
396 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700397 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700398 public void run() {
399 writeConfiguration(Launcher.this, localeConfiguration);
400 }
401 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700402 }
403 }
404
405 private static class LocaleConfiguration {
406 public String locale;
407 public int mcc = -1;
408 public int mnc = -1;
409 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700410
Romain Guy98d01652009-06-30 16:21:04 -0700411 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
412 DataInputStream in = null;
413 try {
414 in = new DataInputStream(context.openFileInput(PREFERENCES));
415 configuration.locale = in.readUTF();
416 configuration.mcc = in.readInt();
417 configuration.mnc = in.readInt();
418 } catch (FileNotFoundException e) {
419 // Ignore
420 } catch (IOException e) {
421 // Ignore
422 } finally {
423 if (in != null) {
424 try {
425 in.close();
426 } catch (IOException e) {
427 // Ignore
428 }
429 }
430 }
431 }
432
433 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
434 DataOutputStream out = null;
435 try {
436 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
437 out.writeUTF(configuration.locale);
438 out.writeInt(configuration.mcc);
439 out.writeInt(configuration.mnc);
440 out.flush();
441 } catch (FileNotFoundException e) {
442 // Ignore
443 } catch (IOException e) {
444 //noinspection ResultOfMethodCallIgnored
445 context.getFileStreamPath(PREFERENCES).delete();
446 } finally {
447 if (out != null) {
448 try {
449 out.close();
450 } catch (IOException e) {
451 // Ignore
452 }
453 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 }
455 }
456
Adam Cohen716b51e2011-06-30 12:09:54 -0700457 public DragLayer getDragLayer() {
458 return mDragLayer;
459 }
460
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 static int getScreen() {
462 synchronized (sLock) {
463 return sScreen;
464 }
465 }
466
467 static void setScreen(int screen) {
468 synchronized (sLock) {
469 sScreen = screen;
470 }
471 }
472
Winson Chung557d6ed2011-07-08 15:34:52 -0700473 /**
474 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
475 * a configuration step, this allows the proper animations to run after other transitions.
476 */
477 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700478 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800479 switch (args.requestCode) {
480 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700481 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
482 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800483 break;
484 case REQUEST_PICK_SHORTCUT:
485 processShortcut(args.intent);
486 break;
487 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700488 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
489 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700490 result = true;
491 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800492 case REQUEST_PICK_APPWIDGET:
493 addAppWidgetFromPick(args.intent);
494 break;
495 case REQUEST_CREATE_APPWIDGET:
496 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Winson Chung3d503fb2011-07-13 17:25:49 -0700497 completeAddAppWidget(appWidgetId, args.container, args.screen);
Winson Chungb8472bb2011-08-05 13:49:21 -0700498 result = true;
499 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800500 case REQUEST_PICK_WALLPAPER:
501 // We just wanted the activity result here so we can clear mWaitingForResult
502 break;
503 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700504 // In any situation where we have a multi-step drop, we should reset the add info only after
505 // we complete the drop
506 resetAddInfo();
507 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800508 }
509
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510 @Override
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800511 protected void onActivityResult(final int requestCode, int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700512 boolean delayExitSpringLoadedMode = false;
Romain Guyaad5ef42009-06-10 02:48:37 -0700513 mWaitingForResult = false;
514
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800515 // The pattern used here is that a user PICKs a specific application,
516 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700517
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
519 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700520 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800521 final PendingAddArguments args = new PendingAddArguments();
522 args.requestCode = requestCode;
523 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700524 args.container = mPendingAddInfo.container;
525 args.screen = mPendingAddInfo.screen;
526 args.cellX = mPendingAddInfo.cellX;
527 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800528
529 // If the loader is still running, defer the add until it is done.
530 if (isWorkspaceLocked()) {
531 sPendingAddList.add(args);
532 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700533 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800534 }
Romain Guy18042c82009-11-06 11:44:55 -0800535 } else if ((requestCode == REQUEST_PICK_APPWIDGET ||
Winson Chung557d6ed2011-07-08 15:34:52 -0700536 requestCode == REQUEST_CREATE_APPWIDGET) && resultCode == RESULT_CANCELED) {
537 if (data != null) {
538 // Clean up the appWidgetId if we canceled
539 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
540 if (appWidgetId != -1) {
541 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
542 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800543 }
544 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700545
546 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -0700547 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800548 }
549
550 @Override
551 protected void onResume() {
552 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800553 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700554 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400555 mWorkspaceLoading = true;
556 mModel.startLoader(this, true);
557 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700558 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800559 }
Michael Jurkaddd62e92011-02-16 17:49:14 -0800560 if (mWaitingForResume != null) {
561 mWaitingForResume.setStayPressed(false);
562 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700563 // When we resume Launcher, a different Activity might be responsible for the app
564 // market intent, so refresh the icon
565 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -0700566 if (!mWorkspaceLoading) {
Michael Jurkab737ee62011-11-15 15:57:22 -0800567 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
568 final Workspace workspace = mWorkspace;
569 // We want to let Launcher draw itself at least once before we force it to build
570 // layers on all the workspace pages, so that transitioning to Launcher from other
571 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
572 // a true draw so we wait until the second preDraw call to be safe
573 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
574 boolean mFirstTime = true;
575 public boolean onPreDraw() {
576 if (mFirstTime) {
577 mFirstTime = false;
578 } else {
579 workspace.post(mBuildLayersRunnable);
580 observer.removeOnPreDrawListener(this);
581 }
582 return true;
583 }
584 });
Michael Jurkac1f5d262011-09-30 19:32:27 -0700585 }
Mathew Inwood8193f692011-10-12 17:30:58 +0100586 clearTypedText();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 }
588
589 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700590 protected void onPause() {
591 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700592 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800593 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700594 }
Romain Guycbb89e42009-06-08 15:52:54 -0700595
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700596 @Override
597 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400598 // Flag the loader to stop early before switching
599 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700600 if (mAppsCustomizeContent != null) {
601 mAppsCustomizeContent.surrender();
602 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800603 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700604 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700605
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800606 // We can't hide the IME if it was forced open. So don't bother
607 /*
608 @Override
609 public void onWindowFocusChanged(boolean hasFocus) {
610 super.onWindowFocusChanged(hasFocus);
611
612 if (hasFocus) {
613 final InputMethodManager inputManager = (InputMethodManager)
614 getSystemService(Context.INPUT_METHOD_SERVICE);
615 WindowManager.LayoutParams lp = getWindow().getAttributes();
616 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
617 android.os.Handler()) {
618 protected void onReceiveResult(int resultCode, Bundle resultData) {
619 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
620 }
621 });
622 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
623 }
624 }
625 */
626
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800627 private boolean acceptFilter() {
628 final InputMethodManager inputManager = (InputMethodManager)
629 getSystemService(Context.INPUT_METHOD_SERVICE);
630 return !inputManager.isFullscreenMode();
631 }
632
633 @Override
634 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700635 final int uniChar = event.getUnicodeChar();
636 final boolean handled = super.onKeyDown(keyCode, event);
637 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
638 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800639 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
640 keyCode, event);
641 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700642 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700643 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700644 // showSearchDialog()
645 // If there are multiple keystrokes before the search dialog takes focus,
646 // onSearchRequested() will be called for every keystroke,
647 // but it is idempotent, so it's fine.
648 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
650 }
651
Joe Onorato8a9625e2010-01-28 15:55:35 -0800652 // Eat the long press event so the keyboard doesn't come up.
653 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
654 return true;
655 }
656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 return handled;
658 }
659
Karl Rosaen138a0412009-04-23 19:00:21 -0700660 private String getTypedText() {
661 return mDefaultKeySsb.toString();
662 }
663
664 private void clearTypedText() {
665 mDefaultKeySsb.clear();
666 mDefaultKeySsb.clearSpans();
667 Selection.setSelection(mDefaultKeySsb, 0);
668 }
669
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700671 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
672 * State
673 */
674 private static State intToState(int stateOrdinal) {
675 State state = State.WORKSPACE;
676 final State[] stateValues = State.values();
677 for (int i = 0; i < stateValues.length; i++) {
678 if (stateValues[i].ordinal() == stateOrdinal) {
679 state = stateValues[i];
680 break;
681 }
682 }
683 return state;
684 }
685
686 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800687 * Restores the previous state, if it exists.
688 *
689 * @param savedState The previous state.
690 */
691 private void restoreState(Bundle savedState) {
692 if (savedState == null) {
693 return;
694 }
695
Michael Jurka883f55b2010-10-21 15:47:14 -0700696 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700697 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800698 showAllApps(false);
699 }
700
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
702 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700703 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 }
705
Winson Chung3d503fb2011-07-13 17:25:49 -0700706 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
707 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700708
Winson Chung3d503fb2011-07-13 17:25:49 -0700709 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
710 mPendingAddInfo.container = pendingAddContainer;
711 mPendingAddInfo.screen = pendingAddScreen;
712 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
713 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 mRestoring = true;
715 }
716
717 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
718 if (renameFolder) {
719 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700720 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800721 mRestoring = true;
722 }
Winson Chunga12a2502010-12-20 14:41:35 -0800723
Winson Chung785d2eb2011-04-14 16:08:02 -0700724
725 // Restore the AppsCustomize tab
726 if (mAppsCustomizeTabHost != null) {
727 String curTab = savedState.getString("apps_customize_currentTab");
728 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700729 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700730 mAppsCustomizeContent.setContentType(
731 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
732 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700733 mAppsCustomizeContent.loadAssociatedPages(
734 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700735 }
736
Winson Chung5afbf7b2011-07-25 11:53:08 -0700737 int currentIndex = savedState.getInt("apps_customize_currentIndex");
738 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700739 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 }
741
742 /**
743 * Finds all the views we need and configure them properly.
744 */
745 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700746 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400747
Winson Chung4c98d922011-05-31 16:50:48 -0700748 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
749 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800750 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
751 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800752
Winson Chung4c98d922011-05-31 16:50:48 -0700753 // Setup the drag layer
754 mDragLayer.setup(this, dragController);
755
Winson Chung3d503fb2011-07-13 17:25:49 -0700756 // Setup the hotseat
757 mHotseat = (Hotseat) findViewById(R.id.hotseat);
758 if (mHotseat != null) {
759 mHotseat.setup(this);
760 }
761
Winson Chung4c98d922011-05-31 16:50:48 -0700762 // Setup the workspace
763 mWorkspace.setHapticFeedbackEnabled(false);
764 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700765 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700766 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700767
Winson Chungf0ea4d32011-06-06 14:27:16 -0700768 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700769 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700770
Winson Chungf0ea4d32011-06-06 14:27:16 -0700771 // Setup AppsCustomize
772 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
773 findViewById(R.id.apps_customize_pane);
774 mAppsCustomizeContent = (AppsCustomizePagedView)
775 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
776 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400777
Michael Jurka5130e402011-10-13 04:55:35 -0700778 // Get the all apps button
779 mAllAppsButton = findViewById(R.id.all_apps_button);
780 if (mAllAppsButton != null) {
781 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
782 @Override
783 public boolean onTouch(View v, MotionEvent event) {
784 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
785 onTouchDownAllAppsButton(v);
786 }
787 return false;
788 }
789 });
790 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700791 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700792 dragController.setDragScoller(mWorkspace);
793 dragController.setScrollView(mDragLayer);
794 dragController.setMoveTarget(mWorkspace);
795 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700796 if (mSearchDropTargetBar != null) {
797 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800798 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800799 }
800
801 /**
802 * Creates a view representing a shortcut.
803 *
804 * @param info The data structure describing the shortcut.
805 *
806 * @return A View inflated from R.layout.application.
807 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800808 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700810 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800811 }
812
813 /**
814 * Creates a view representing a shortcut inflated from the specified resource.
815 *
816 * @param layoutResId The id of the XML layout used to create the shortcut.
817 * @param parent The group the shortcut belongs to.
818 * @param info The data structure describing the shortcut.
819 *
820 * @return A View inflated from layoutResId.
821 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800822 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800823 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
824 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800825 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 return favorite;
827 }
828
829 /**
830 * Add an application shortcut to the workspace.
831 *
832 * @param data The intent describing the application.
833 * @param cellInfo The position on screen where to create the shortcut.
834 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700835 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700836 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700837 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700838
Adam Cohenfbba09b2011-07-18 21:43:05 -0700839 // First we check if we already know the exact location where we want to add this item.
840 if (cellX >= 0 && cellY >= 0) {
841 cellXY[0] = cellX;
842 cellXY[1] = cellY;
843 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700844 showOutOfSpaceMessage();
845 return;
846 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800847
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800848 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800849
Romain Guy73b979d2009-06-09 12:57:21 -0700850 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800851 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800852 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800853 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700854 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700855 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800856 } else {
857 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 }
859 }
Romain Guycbb89e42009-06-08 15:52:54 -0700860
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 /**
862 * Add a shortcut to the workspace.
863 *
864 * @param data The intent describing the shortcut.
865 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700867 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
868 int cellY) {
869 int[] cellXY = mTmpAddItemCellCoordinates;
870 int[] touchXY = mPendingAddInfo.dropPos;
871 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700872
Michael Jurkad3ef3062010-11-23 16:23:58 -0800873 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700874
Adam Cohen558baaf2011-08-15 15:22:57 -0700875 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
876 final View view = createShortcut(info);
877
Adam Cohenfbba09b2011-07-18 21:43:05 -0700878 // First we check if we already know the exact location where we want to add this item.
879 if (cellX >= 0 && cellY >= 0) {
880 cellXY[0] = cellX;
881 cellXY[1] = cellY;
882 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700883
884 // If appropriate, either create a folder or add to an existing folder
885 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
886 true, null,null)) {
887 return;
888 }
889 DragObject dragObject = new DragObject();
890 dragObject.dragInfo = info;
891 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
892 return;
893 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700894 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800895 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700896 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800897 foundCellSpan = (result != null);
898 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700899 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800900 }
901
902 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700903 showOutOfSpaceMessage();
904 return;
905 }
906
Adam Cohen558baaf2011-08-15 15:22:57 -0700907 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908
909 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700910 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
911 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 }
913 }
914
Adam Cohenf814aa02011-09-01 13:48:05 -0700915 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
916 if (spanXY == null) {
917 spanXY = new int[2];
918 }
919
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800920 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700921 // We want to account for the extra amount of padding that we are adding to the widget
922 // to ensure that it gets the full amount of space that it has requested
923 int requiredWidth = minWidth + padding.left + padding.right;
924 int requiredHeight = minHeight + padding.top + padding.bottom;
925 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
926 }
927
928 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
929 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
930 }
931
Adam Cohencbf47e32011-09-16 17:32:37 -0700932 int[] getMinResizeSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
933 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
934 }
935
Adam Cohenf814aa02011-09-01 13:48:05 -0700936 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
937 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
938 }
939
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700941 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700943 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700944 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800945 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700946 private void completeAddAppWidget(final int appWidgetId, long container, int screen) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700947 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Romain Guycbb89e42009-06-08 15:52:54 -0700948
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700949 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700950 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700951
Adam Cohenf814aa02011-09-01 13:48:05 -0700952 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700953
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700955 // We have saved the position to which the widget was dragged-- this really only matters
956 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700957 int[] cellXY = mTmpAddItemCellCoordinates;
958 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700959 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700960 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
961 cellXY[0] = mPendingAddInfo.cellX;
962 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700963 foundCellSpan = true;
964 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700965 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700966 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -0700967 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800968 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700969 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700970 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -0700971 }
972
Michael Jurka0280c3b2010-09-17 15:00:07 -0700973 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -0800974 if (appWidgetId != -1) {
975 // Deleting an app widget ID is a void call but writes to disk before returning
976 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -0800977 new Thread("deleteAppWidgetId") {
978 public void run() {
979 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
980 }
981 }.start();
982 }
Michael Jurka0280c3b2010-09-17 15:00:07 -0700983 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -0800984 return;
985 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700987 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -0700988 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700989 launcherInfo.spanX = spanXY[0];
990 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -0700991
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800992 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -0700993 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994
995 if (!mRestoring) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 // Perform actual inflation because we're live
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700997 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -0700998
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700999 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001000 launcherInfo.hostView.setTag(launcherInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001001
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001003 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001004
1005 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001006 }
1007 }
Romain Guycbb89e42009-06-08 15:52:54 -07001008
Adam Cohended9f8d2010-11-03 13:25:16 -07001009 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1010 @Override
1011 public void onReceive(Context context, Intent intent) {
1012 final String action = intent.getAction();
1013 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1014 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001015 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001016 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001017
Winson Chungc100e8e2011-08-09 16:02:43 -07001018 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001019 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001020 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1021 mAppsCustomizeTabHost.reset();
1022 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001023 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001024 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1025 mUserPresent = true;
1026 updateRunning();
1027 }
1028 }
1029 };
1030
1031 @Override
1032 public void onAttachedToWindow() {
1033 super.onAttachedToWindow();
1034
1035 // Listen for broadcasts related to user-presence
1036 final IntentFilter filter = new IntentFilter();
1037 filter.addAction(Intent.ACTION_SCREEN_OFF);
1038 filter.addAction(Intent.ACTION_USER_PRESENT);
1039 registerReceiver(mReceiver, filter);
1040
Adam Cohend113e0c2010-11-11 10:48:05 -08001041 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001042 mVisible = true;
1043 }
1044
1045 @Override
1046 public void onDetachedFromWindow() {
1047 super.onDetachedFromWindow();
1048 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001049 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001050
Adam Cohend113e0c2010-11-11 10:48:05 -08001051 if (mAttached) {
1052 unregisterReceiver(mReceiver);
1053 mAttached = false;
1054 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001055 updateRunning();
1056 }
1057
1058 public void onWindowVisibilityChanged(int visibility) {
1059 mVisible = visibility == View.VISIBLE;
1060 updateRunning();
1061 }
1062
1063 private void sendAdvanceMessage(long delay) {
1064 mHandler.removeMessages(ADVANCE_MSG);
1065 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1066 mHandler.sendMessageDelayed(msg, delay);
1067 mAutoAdvanceSentTime = System.currentTimeMillis();
1068 }
1069
1070 private void updateRunning() {
1071 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1072 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1073 mAutoAdvanceRunning = autoAdvanceRunning;
1074 if (autoAdvanceRunning) {
1075 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1076 sendAdvanceMessage(delay);
1077 } else {
1078 if (!mWidgetsToAdvance.isEmpty()) {
1079 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1080 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1081 }
1082 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001083 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001084 }
1085 }
1086 }
1087
1088 private final Handler mHandler = new Handler() {
1089 @Override
1090 public void handleMessage(Message msg) {
1091 if (msg.what == ADVANCE_MSG) {
1092 int i = 0;
1093 for (View key: mWidgetsToAdvance.keySet()) {
1094 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1095 final int delay = mAdvanceStagger * i;
1096 if (v instanceof Advanceable) {
1097 postDelayed(new Runnable() {
1098 public void run() {
1099 ((Advanceable) v).advance();
1100 }
1101 }, delay);
1102 }
1103 i++;
1104 }
1105 sendAdvanceMessage(mAdvanceInterval);
1106 }
1107 }
1108 };
1109
1110 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001111 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001112 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1113 if (v instanceof Advanceable) {
1114 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001115 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001116 updateRunning();
1117 }
1118 }
1119
1120 void removeWidgetToAutoAdvance(View hostView) {
1121 if (mWidgetsToAdvance.containsKey(hostView)) {
1122 mWidgetsToAdvance.remove(hostView);
1123 updateRunning();
1124 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001125 }
1126
Joe Onorato9c1289c2009-08-17 11:03:03 -04001127 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001128 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001129 launcherInfo.hostView = null;
1130 }
1131
Adam Cohended9f8d2010-11-03 13:25:16 -07001132 void showOutOfSpaceMessage() {
1133 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1134 }
1135
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001136 public LauncherAppWidgetHost getAppWidgetHost() {
1137 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 }
Romain Guycbb89e42009-06-08 15:52:54 -07001139
Winson Chunga9abd0e2010-10-27 17:18:37 -07001140 public LauncherModel getModel() {
1141 return mModel;
1142 }
1143
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001144 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001145 getWindow().closeAllPanels();
1146
Winson Chung87acb482011-08-23 17:28:05 -07001147 /**
1148 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001149 try {
1150 dismissDialog(DIALOG_CREATE_SHORTCUT);
1151 // Unlock the workspace if the dialog was showing
1152 } catch (Exception e) {
1153 // An exception is thrown if the dialog is not visible, which is fine
1154 }
1155
1156 try {
1157 dismissDialog(DIALOG_RENAME_FOLDER);
1158 // Unlock the workspace if the dialog was showing
1159 } catch (Exception e) {
1160 // An exception is thrown if the dialog is not visible, which is fine
1161 }
Winson Chung87acb482011-08-23 17:28:05 -07001162 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001163
1164 // Whatever we were doing is hereby canceled.
1165 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001166 }
1167
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 @Override
1169 protected void onNewIntent(Intent intent) {
1170 super.onNewIntent(intent);
1171
1172 // Close the menu
1173 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001174 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001175 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001176
Joe Onorato14f122b2009-11-19 14:06:36 -08001177 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1178 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001179
Adam Cohenac56cff2011-09-28 20:45:37 -07001180 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001181 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001182 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001183 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1184 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001185 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001186 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001187
1188 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001189 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001190 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001191
Joe Onorato3a8820b2009-11-10 15:06:42 -08001192 final View v = getWindow().peekDecorView();
1193 if (v != null && v.getWindowToken() != null) {
1194 InputMethodManager imm = (InputMethodManager)getSystemService(
1195 INPUT_METHOD_SERVICE);
1196 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001198
Michael Jurka35aa14d2011-07-07 17:01:08 -07001199 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001200 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001201 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 }
1204 }
1205
1206 @Override
1207 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1208 // Do not call super here
1209 mSavedInstanceState = savedInstanceState;
1210 }
1211
1212 @Override
1213 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001214 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001215 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216
Michael Jurka883f55b2010-10-21 15:47:14 -07001217 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001218 // We close any open folder since it will not be re-opened, and we need to make sure
1219 // this state is reflected.
1220 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001221
Winson Chung3d503fb2011-07-13 17:25:49 -07001222 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1223 mWaitingForResult) {
1224 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1225 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1226 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1227 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 }
1229
1230 if (mFolderInfo != null && mWaitingForResult) {
1231 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1232 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1233 }
Michael Jurka946ad472010-07-09 18:05:18 -07001234
Winson Chung785d2eb2011-04-14 16:08:02 -07001235 // Save the current AppsCustomize tab
1236 if (mAppsCustomizeTabHost != null) {
1237 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1238 if (currentTabTag != null) {
1239 outState.putString("apps_customize_currentTab", currentTabTag);
1240 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001241 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1242 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001243 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001244 }
1245
1246 @Override
1247 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001249
Winson Chunge7a03942011-08-05 15:05:12 -07001250 // Remove all pending runnables
1251 mHandler.removeMessages(ADVANCE_MSG);
1252 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001253 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001254
Winson Chungcd2b0142011-06-08 16:02:26 -07001255 // Stop callbacks from LauncherModel
1256 LauncherApplication app = ((LauncherApplication) getApplication());
1257 mModel.stopLoader();
1258 app.setLauncher(null);
1259
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001261 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001262 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001263 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001264 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001265 mAppWidgetHost = null;
1266
1267 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001268
1269 TextKeyListener.getInstance().release();
1270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271
Winson Chung3d503fb2011-07-13 17:25:49 -07001272 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001273
1274 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001275 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001276
1277 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1278 mWorkspace.removeAllViews();
1279 mWorkspace = null;
1280 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001281
1282 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001283 }
1284
Adam Cohena9cf38f2011-05-02 15:36:58 -07001285 public DragController getDragController() {
1286 return mDragController;
1287 }
1288
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 @Override
1290 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001291 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001292 super.startActivityForResult(intent, requestCode);
1293 }
1294
Winson Chung70d72102011-08-12 11:24:35 -07001295 /**
1296 * Indicates that we want global search for this activity by setting the globalSearch
1297 * argument for {@link #startSearch} to true.
1298 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001299 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001300 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001302
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001303 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001304
Karl Rosaen138a0412009-04-23 19:00:21 -07001305 if (initialQuery == null) {
1306 // Use any text typed in the launcher as the initial query
1307 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001308 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 if (appSearchData == null) {
1310 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001311 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001312 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001313 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001314
Karl Rosaen138a0412009-04-23 19:00:21 -07001315 final SearchManager searchManager =
1316 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001317 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001318 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001319 }
1320
Winson Chung70d72102011-08-12 11:24:35 -07001321 @Override
1322 public boolean onCreateOptionsMenu(Menu menu) {
1323 if (isWorkspaceLocked()) {
1324 return false;
1325 }
1326
1327 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001328
1329 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1330 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1331 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001332 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1333 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1334 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001335 String helpUrl = getString(R.string.help_url);
1336 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001337 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1338 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1339
Winson Chung70d72102011-08-12 11:24:35 -07001340 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1341 .setIcon(android.R.drawable.ic_menu_gallery)
1342 .setAlphabeticShortcut('W');
1343 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1344 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001345 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001346 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001347 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1348 .setIcon(android.R.drawable.ic_menu_preferences)
1349 .setIntent(settings)
1350 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001351 if (!helpUrl.isEmpty()) {
1352 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1353 .setIcon(android.R.drawable.ic_menu_help)
1354 .setIntent(help)
1355 .setAlphabeticShortcut('H');
1356 }
Winson Chung70d72102011-08-12 11:24:35 -07001357 return true;
1358 }
1359
1360 @Override
1361 public boolean onPrepareOptionsMenu(Menu menu) {
1362 super.onPrepareOptionsMenu(menu);
1363
1364 if (mAppsCustomizeTabHost.isTransitioning()) {
1365 return false;
1366 }
1367 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1368 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1369
1370 return true;
1371 }
1372
1373 @Override
1374 public boolean onOptionsItemSelected(MenuItem item) {
1375 switch (item.getItemId()) {
1376 case MENU_WALLPAPER_SETTINGS:
1377 startWallpaper();
1378 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001379 }
1380
1381 return super.onOptionsItemSelected(item);
1382 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001384 @Override
1385 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001386 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001387 // Use a custom animation for launching search
1388 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001389 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001390 }
1391
Joe Onorato9c1289c2009-08-17 11:03:03 -04001392 public boolean isWorkspaceLocked() {
1393 return mWorkspaceLoading || mWaitingForResult;
1394 }
1395
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396 private void addItems() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001397 showWorkspace(true);
Adam Cohenfbba09b2011-07-18 21:43:05 -07001398 showAddDialog();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 }
1400
Michael Jurka0280c3b2010-09-17 15:00:07 -07001401 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001402 mPendingAddInfo.container = ItemInfo.NO_ID;
1403 mPendingAddInfo.screen = -1;
1404 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1405 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1406 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001407 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001408
Michael Jurkaaf442092010-06-10 17:01:57 -07001409 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001410 // TODO: catch bad widget exception when sent
1411 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001412 // TODO: Is this log message meaningful?
1413 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001414 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001415 }
1416
Winson Chung55cef262010-10-28 14:14:18 -07001417 void addAppWidgetImpl(int appWidgetId, PendingAddWidgetInfo info) {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001418 AppWidgetProviderInfo appWidget = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419
Bjorn Bringert7984c942009-12-09 15:38:25 +00001420 if (appWidget.configure != null) {
1421 // Launch over to configure widget, if needed
1422 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1423 intent.setComponent(appWidget.configure);
1424 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001425 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001426 if (info.mimeType != null && !info.mimeType.isEmpty()) {
1427 intent.putExtra(
1428 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE,
1429 info.mimeType);
1430
1431 final String mimeType = info.mimeType;
1432 final ClipData clipData = (ClipData) info.configurationData;
1433 final ClipDescription clipDesc = clipData.getDescription();
1434 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1435 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001436 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001437 final CharSequence stringData = item.getText();
1438 final Uri uriData = item.getUri();
1439 final Intent intentData = item.getIntent();
1440 final String key =
1441 InstallWidgetReceiver.EXTRA_APPWIDGET_CONFIGURATION_DATA;
1442 if (uriData != null) {
1443 intent.putExtra(key, uriData);
1444 } else if (intentData != null) {
1445 intent.putExtra(key, intentData);
1446 } else if (stringData != null) {
1447 intent.putExtra(key, stringData);
1448 }
1449 break;
1450 }
1451 }
1452 }
Winson Chung55cef262010-10-28 14:14:18 -07001453 }
Bjorn Bringert7984c942009-12-09 15:38:25 +00001454
Romain Guy8e633c52010-03-04 12:51:36 -08001455 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001457 // Otherwise just add it
Winson Chung3d503fb2011-07-13 17:25:49 -07001458 completeAddAppWidget(appWidgetId, info.container, info.screen);
Winson Chung557d6ed2011-07-08 15:34:52 -07001459
1460 // Exit spring loaded mode if necessary after adding the widget
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001461 exitSpringLoadedDragModeDelayed(true, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 }
1463 }
Romain Guycbb89e42009-06-08 15:52:54 -07001464
Adam Cohenfbba09b2011-07-18 21:43:05 -07001465 /**
1466 * Process a shortcut drop.
1467 *
1468 * @param componentName The name of the component
1469 * @param screen The screen where it should be added
1470 * @param cell The cell it should be added to, optional
1471 * @param position The location on the screen where it was dropped, optional
1472 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001473 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1474 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001475 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001476 mPendingAddInfo.container = container;
1477 mPendingAddInfo.screen = screen;
1478 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001479
1480 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001481 mPendingAddInfo.cellX = cell[0];
1482 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001483 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001484
1485 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1486 createShortcutIntent.setComponent(componentName);
1487 processShortcut(createShortcutIntent);
1488 }
1489
Adam Cohenfbba09b2011-07-18 21:43:05 -07001490 /**
1491 * Process a widget drop.
1492 *
1493 * @param info The PendingAppWidgetInfo of the widget being added.
1494 * @param screen The screen where it should be added
1495 * @param cell The cell it should be added to, optional
1496 * @param position The location on the screen where it was dropped, optional
1497 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001498 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1499 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001500 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001501 mPendingAddInfo.container = info.container = container;
1502 mPendingAddInfo.screen = info.screen = screen;
1503 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001504 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001505 mPendingAddInfo.cellX = cell[0];
1506 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001507 }
1508
1509 int appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1510 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1511 addAppWidgetImpl(appWidgetId, info);
1512 }
1513
Joe Onoratodeb98af2010-02-19 14:59:39 -08001514 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001515 // Handle case where user selected "Applications"
1516 String applicationName = getResources().getString(R.string.group_applications);
1517 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001518
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001519 if (applicationName != null && applicationName.equals(shortcutName)) {
1520 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1521 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001522
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001523 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1524 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001525 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001526 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001527 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001528 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001529 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 }
1531
Winson Chung24ab2f12010-09-16 14:10:47 -07001532 void processWallpaper(Intent intent) {
1533 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1534 }
1535
Winson Chung3d503fb2011-07-13 17:25:49 -07001536 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1537 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001538 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 folderInfo.title = getText(R.string.folder_name);
1540
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001541 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001542 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1543 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001544 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545
1546 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001547 FolderIcon newFolder =
1548 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1549 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1550 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001551 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 }
Romain Guycbb89e42009-06-08 15:52:54 -07001553
Joe Onorato9c1289c2009-08-17 11:03:03 -04001554 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001555 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001556 }
1557
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 private void showNotifications() {
1559 final StatusBarManager statusBar = (StatusBarManager) getSystemService(STATUS_BAR_SERVICE);
1560 if (statusBar != null) {
1561 statusBar.expand();
1562 }
1563 }
1564
1565 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001566 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001568 Intent chooser = Intent.createChooser(pickWallpaper,
1569 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001570 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1571 // Removed in Eclair MR1
1572// WallpaperManager wm = (WallpaperManager)
1573// getSystemService(Context.WALLPAPER_SERVICE);
1574// WallpaperInfo wi = wm.getWallpaperInfo();
1575// if (wi != null && wi.getSettingsActivity() != null) {
1576// LabeledIntent li = new LabeledIntent(getPackageName(),
1577// R.string.configure_wallpaper, 0);
1578// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1579// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1580// }
Mike Clerona0618e42009-10-22 13:55:21 -07001581 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 }
1583
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001584 /**
1585 * Registers various content observers. The current implementation registers
1586 * only a favorites observer to keep track of the favorites applications.
1587 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001588 private void registerContentObservers() {
1589 ContentResolver resolver = getContentResolver();
1590 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1591 true, mWidgetObserver);
1592 }
1593
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 @Override
1595 public boolean dispatchKeyEvent(KeyEvent event) {
1596 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1597 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001599 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001600 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001601 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001602 dumpState();
1603 return true;
1604 }
1605 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001606 }
1607 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1608 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001609 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001610 return true;
1611 }
1612 }
1613
1614 return super.dispatchKeyEvent(event);
1615 }
1616
Joe Onorato88ec0992009-11-19 13:16:06 -08001617 @Override
1618 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001619 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001620 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001621 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001622 Folder openFolder = mWorkspace.getOpenFolder();
1623 if (openFolder.isEditingName()) {
1624 openFolder.dismissEditingName();
1625 } else {
1626 closeFolder();
1627 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001628 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001629 mWorkspace.exitWidgetResizeMode();
1630
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001631 // Back button is a no-op here, but give at least some feedback for the button press
1632 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001633 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001634 }
1635
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001637 * Re-listen when widgets are reset.
1638 */
1639 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001640 if (mAppWidgetHost != null) {
1641 mAppWidgetHost.startListening();
1642 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001643 }
1644
1645 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001646 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1647 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001649 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001650 if (mModel != null) {
1651 mModel.unbindWorkspaceItems();
1652 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001653 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001654
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001655 /**
1656 * Launches the intent referred by the clicked shortcut.
1657 *
1658 * @param v The view representing the clicked shortcut.
1659 */
1660 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001661 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1662 // view has detached (it's possible for this to happen if the view is removed mid touch).
1663 if (v.getWindowToken() == null) {
1664 return;
1665 }
1666
1667 if (mWorkspace.isSwitchingState()) {
1668 return;
1669 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001670
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001672 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001674 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001675 int[] pos = new int[2];
1676 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001677 intent.setSourceBounds(new Rect(pos[0], pos[1],
1678 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001679 boolean success = startActivitySafely(intent, tag);
1680
1681 if (success && v instanceof BubbleTextView) {
1682 mWaitingForResume = (BubbleTextView) v;
1683 mWaitingForResume.setStayPressed(true);
1684 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001685 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001686 if (v instanceof FolderIcon) {
1687 FolderIcon fi = (FolderIcon) v;
1688 handleFolderClick(fi);
1689 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001690 } else if (v == mAllAppsButton) {
1691 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001692 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001693 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001694 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001695 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 }
1697 }
1698
Michael Jurka0e260592010-06-30 17:07:39 -07001699 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001700 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001701 // clicking anywhere on the workspace causes the customization drawer to slide down
1702 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001703 return false;
1704 }
1705
Michael Jurkaaf442092010-06-10 17:01:57 -07001706 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001707 * Event handler for the search button
1708 *
1709 * @param v The view that was clicked.
1710 */
1711 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001712 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1713
Amith Yamasania135ba82011-08-09 17:42:01 -07001714 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001715 }
1716
1717 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001718 * Event handler for the voice button
1719 *
1720 * @param v The view that was clicked.
1721 */
1722 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001723 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001724
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001725 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001726 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001727 startActivity(intent);
1728 }
1729
1730 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001731 * Event handler for the "grid" button that appears on the home screen, which
1732 * enters all apps mode.
1733 *
1734 * @param v The view that was clicked.
1735 */
1736 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001737 showAllApps(true);
1738 }
1739
1740 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001741 // Provide the same haptic feedback that the system offers for virtual keys.
1742 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001743 }
1744
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001745 public void onClickAppMarketButton(View v) {
1746 if (mAppMarketIntent != null) {
1747 startActivitySafely(mAppMarketIntent, "app market");
1748 }
1749 }
1750
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001751 void startApplicationDetailsActivity(ComponentName componentName) {
1752 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001753 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1754 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001755 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001756 startActivity(intent);
1757 }
1758
Patrick Dubroy5539af72010-09-07 15:22:01 -07001759 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1760 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1761 // System applications cannot be installed. For now, show a toast explaining that.
1762 // We may give them the option of disabling apps this way.
1763 int messageId = R.string.uninstall_system_app_text;
1764 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1765 } else {
1766 String packageName = appInfo.componentName.getPackageName();
1767 String className = appInfo.componentName.getClassName();
1768 Intent intent = new Intent(
1769 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001770 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1771 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001772 startActivity(intent);
1773 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001774 }
1775
Michael Jurkaddd62e92011-02-16 17:49:14 -08001776 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1778 try {
1779 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001780 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001781 } catch (ActivityNotFoundException e) {
1782 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001783 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 } catch (SecurityException e) {
1785 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001786 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001788 "or use the exported attribute for this activity. "
1789 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001791 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001793
Romain Guy8e633c52010-03-04 12:51:36 -08001794 void startActivityForResultSafely(Intent intent, int requestCode) {
1795 try {
1796 startActivityForResult(intent, requestCode);
1797 } catch (ActivityNotFoundException e) {
1798 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1799 } catch (SecurityException e) {
1800 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1801 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1802 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1803 "or use the exported attribute for this activity.", e);
1804 }
1805 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806
Adam Cohena9cf38f2011-05-02 15:36:58 -07001807 private void handleFolderClick(FolderIcon folderIcon) {
1808 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001809 Folder openFolder = mWorkspace.getFolderForTag(info);
1810
1811 // If the folder info reports that the associated folder is open, then verify that
1812 // it is actually opened. There have been a few instances where this gets out of sync.
1813 if (info.opened && openFolder == null) {
1814 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1815 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1816 info.opened = false;
1817 }
1818
Adam Cohena9cf38f2011-05-02 15:36:58 -07001819 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 // Close any open folder
1821 closeFolder();
1822 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001823 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001824 } else {
1825 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 int folderScreen;
1827 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001828 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001829 // .. and close it
1830 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001831 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001832 // Close any folder open on the current screen
1833 closeFolder();
1834 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001835 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
1837 }
1838 }
1839 }
1840
Adam Cohen2801caf2011-05-13 20:57:39 -07001841 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001842 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001843 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1844 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1845 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1846
Adam Cohenc51934b2011-07-26 21:07:43 -07001847 FolderInfo info = (FolderInfo) fi.getTag();
1848 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1849 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001850 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1851 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001852 }
1853
Adam Cohen2801caf2011-05-13 20:57:39 -07001854 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1855 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1856 oa.start();
1857 }
1858
1859 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001860 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001861 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1862 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1863 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1864
Adam Cohenc51934b2011-07-26 21:07:43 -07001865 FolderInfo info = (FolderInfo) fi.getTag();
1866 CellLayout cl = null;
1867 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1868 cl = (CellLayout) fi.getParent().getParent();
1869 }
1870
1871 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001872 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1873 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001874 oa.addListener(new AnimatorListenerAdapter() {
1875 @Override
1876 public void onAnimationEnd(Animator animation) {
1877 if (layout != null) {
1878 layout.clearFolderLeaveBehind();
1879 }
1880 }
1881 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001882 oa.start();
1883 }
1884
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001885 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001886 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 * is animated relative to the specified View. If the View is null, no animation
1888 * is played.
1889 *
1890 * @param folderInfo The FolderInfo describing the folder to open.
1891 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001892 public void openFolder(FolderIcon folderIcon) {
1893 Folder folder = folderIcon.mFolder;
1894 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895
Adam Cohen2801caf2011-05-13 20:57:39 -07001896 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001897 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898
Adam Cohen4554ee12011-08-03 16:13:21 -07001899 // Just verify that the folder hasn't already been added to the DragLayer.
1900 // There was a one-off crash where the folder had a parent already.
1901 if (folder.getParent() == null) {
1902 mDragLayer.addView(folder);
1903 mDragController.addDropTarget((DropTarget) folder);
1904 } else {
1905 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1906 folder.getParent() + ").");
1907 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001908 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001909 }
1910
1911 public void closeFolder() {
1912 Folder folder = mWorkspace.getOpenFolder();
1913 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07001914 if (folder.isEditingName()) {
1915 folder.dismissEditingName();
1916 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001917 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07001918
1919 // Dismiss the folder cling
1920 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07001921 }
1922 }
1923
1924 void closeFolder(Folder folder) {
1925 folder.getInfo().opened = false;
1926
1927 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1928 if (parent != null) {
1929 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1930 shrinkAndFadeInFolderIcon(fi);
1931 }
1932 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 }
1934
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001936 if (mState != State.WORKSPACE) {
1937 return false;
1938 }
1939
Joe Onorato9c1289c2009-08-17 11:03:03 -04001940 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 return false;
1942 }
1943
1944 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08001945 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001946 }
1947
Michael Jurka0280c3b2010-09-17 15:00:07 -07001948 resetAddInfo();
1949 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07001951 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001952 return true;
1953 }
1954
Winson Chung3d503fb2011-07-13 17:25:49 -07001955 // The hotseat touch handling does not go through Workspace, and we always allow long press
1956 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07001957 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07001958 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
1959 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001960 if (itemUnderLongClick == null) {
1961 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07001962 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
1963 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07001964 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001966 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07001968 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 }
1970 }
1971 }
1972 return true;
1973 }
1974
Winson Chung3d503fb2011-07-13 17:25:49 -07001975 boolean isHotseatLayout(View layout) {
1976 return mHotseat != null && layout != null &&
1977 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
1978 }
1979 Hotseat getHotseat() {
1980 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08001981 }
Adam Cohenebea84d2011-11-09 17:20:41 -08001982 SearchDropTargetBar getSearchBar() {
1983 return mSearchDropTargetBar;
1984 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08001985
Winson Chung3d503fb2011-07-13 17:25:49 -07001986 /**
1987 * Returns the CellLayout of the specified container at the specified screen.
1988 */
1989 CellLayout getCellLayout(long container, int screen) {
1990 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1991 if (mHotseat != null) {
1992 return mHotseat.getLayout();
1993 } else {
1994 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08001995 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001996 } else {
1997 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08001998 }
1999 }
2000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 Workspace getWorkspace() {
2002 return mWorkspace;
2003 }
2004
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 @Override
2006 protected Dialog onCreateDialog(int id) {
2007 switch (id) {
2008 case DIALOG_CREATE_SHORTCUT:
2009 return new CreateShortcut().createDialog();
2010 case DIALOG_RENAME_FOLDER:
2011 return new RenameFolder().createDialog();
2012 }
2013
2014 return super.onCreateDialog(id);
2015 }
2016
2017 @Override
2018 protected void onPrepareDialog(int id, Dialog dialog) {
2019 switch (id) {
2020 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 break;
2022 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002023 if (mFolderInfo != null) {
2024 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2025 final CharSequence text = mFolderInfo.title;
2026 input.setText(text);
2027 input.setSelection(0, text.length());
2028 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 break;
2030 }
2031 }
2032
2033 void showRenameDialog(FolderInfo info) {
2034 mFolderInfo = info;
2035 mWaitingForResult = true;
2036 showDialog(DIALOG_RENAME_FOLDER);
2037 }
2038
Adam Cohenfbba09b2011-07-18 21:43:05 -07002039 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002040 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002041 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2042 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 mWaitingForResult = true;
2044 showDialog(DIALOG_CREATE_SHORTCUT);
2045 }
2046
2047 private class RenameFolder {
2048 private EditText mInput;
2049
2050 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2052 mInput = (EditText) layout.findViewById(R.id.folder_name);
2053
2054 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2055 builder.setIcon(0);
2056 builder.setTitle(getString(R.string.rename_folder_title));
2057 builder.setCancelable(true);
2058 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2059 public void onCancel(DialogInterface dialog) {
2060 cleanup();
2061 }
2062 });
2063 builder.setNegativeButton(getString(R.string.cancel_action),
2064 new Dialog.OnClickListener() {
2065 public void onClick(DialogInterface dialog, int which) {
2066 cleanup();
2067 }
2068 }
2069 );
2070 builder.setPositiveButton(getString(R.string.rename_action),
2071 new Dialog.OnClickListener() {
2072 public void onClick(DialogInterface dialog, int which) {
2073 changeFolderName();
2074 }
2075 }
2076 );
2077 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002078
2079 final AlertDialog dialog = builder.create();
2080 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2081 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002082 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002083 mInput.requestFocus();
2084 InputMethodManager inputManager = (InputMethodManager)
2085 getSystemService(Context.INPUT_METHOD_SERVICE);
2086 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002087 }
2088 });
2089
2090 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 }
2092
2093 private void changeFolderName() {
2094 final String name = mInput.getText().toString();
2095 if (!TextUtils.isEmpty(name)) {
2096 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002097 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 mFolderInfo.title = name;
2099 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2100
Joe Onorato9c1289c2009-08-17 11:03:03 -04002101 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002102 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002103 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 } else {
2105 final FolderIcon folderIcon = (FolderIcon)
2106 mWorkspace.getViewForTag(mFolderInfo);
2107 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002108 // TODO: At some point we'll probably want some version of setting
2109 // the text for a folder icon.
2110 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 getWorkspace().requestLayout();
2112 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002113 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002114 mWorkspaceLoading = true;
2115 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 }
2117 }
2118 }
2119 cleanup();
2120 }
2121
2122 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 dismissDialog(DIALOG_RENAME_FOLDER);
2124 mWaitingForResult = false;
2125 mFolderInfo = null;
2126 }
2127 }
2128
Daniel Sandler843e8602010-06-07 14:59:01 -04002129 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2130 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002131 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002132 }
2133
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002134 // AllAppsView.Watcher
2135 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002136 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002137 mWorkspace.setVisibility(View.GONE);
2138 }
2139 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002140
2141 /**
2142 * Helper method for the cameraZoomIn/cameraZoomOut animations
2143 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002144 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002145 * @param scaleFactor The scale factor used for the zoom
2146 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002147 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002148 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002149 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002150 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002151
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002152 void updateWallpaperVisibility(boolean visible) {
2153 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2154 int curflags = getWindow().getAttributes().flags
2155 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2156 if (wpflags != curflags) {
2157 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2158 }
2159 }
2160
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002161 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002162 * Things to test when changing the following seven functions.
2163 * - Home from workspace
2164 * - from center screen
2165 * - from other screens
2166 * - Home from all apps
2167 * - from center screen
2168 * - from other screens
2169 * - Back from all apps
2170 * - from center screen
2171 * - from other screens
2172 * - Launch app from workspace and quit
2173 * - with back
2174 * - with home
2175 * - Launch app from all apps and quit
2176 * - with back
2177 * - with home
2178 * - Go to a screen that's not the default, then all
2179 * apps, and launch and app, and go back
2180 * - with back
2181 * -with home
2182 * - On workspace, long press power and go back
2183 * - with back
2184 * - with home
2185 * - On all apps, long press power and go back
2186 * - with back
2187 * - with home
2188 * - On workspace, power off
2189 * - On all apps, power off
2190 * - Launch an app and turn off the screen while in that app
2191 * - Go back with home key
2192 * - Go back with back key TODO: make this not go to workspace
2193 * - From all apps
2194 * - From workspace
2195 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2196 * - From all apps
2197 * - From the center workspace
2198 * - From another workspace
2199 */
2200
2201 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002202 * Zoom the camera out from the workspace to reveal 'toView'.
2203 * Assumes that the view to show is anchored at either the very top or very bottom
2204 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002205 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002206 private void showAppsCustomizeHelper(boolean animated, final boolean springLoaded) {
2207 if (mStateAnimation != null) {
2208 mStateAnimation.cancel();
2209 mStateAnimation = null;
2210 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002211 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002212 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002213
Winson Chungf0ea4d32011-06-06 14:27:16 -07002214 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2215 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2216 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2217 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002218 final int startDelay =
2219 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002220
Michael Jurkab3e22d92011-10-31 15:58:33 -07002221 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002222
Michael Jurkad74c9842011-07-10 12:44:21 -07002223 // Shrink workspaces away if going to AppsCustomize from workspace
Adam Cohen7777d962011-08-18 18:58:38 -07002224 mWorkspace.changeState(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002225
2226 if (animated) {
Michael Jurkaabded662011-03-04 12:06:57 -08002227 final ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
Michael Jurka742574b2011-02-02 23:51:01 -08002228 scaleAnim.setInterpolator(new Workspace.ZoomOutInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002229 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2230 public void onAnimationUpdate(float a, float b) {
Adam Cohen7777d962011-08-18 18:58:38 -07002231 ((View) toView.getParent()).invalidate();
2232 toView.fastInvalidate();
Michael Jurka11148e52011-02-03 18:20:25 -08002233 toView.setFastScaleX(a * scale + b * 1f);
2234 toView.setFastScaleY(a * scale + b * 1f);
Michael Jurka742574b2011-02-02 23:51:01 -08002235 }
2236 });
Adam Cohen61033d32010-11-15 18:29:44 -08002237
Winson Chungf0ea4d32011-06-06 14:27:16 -07002238 toView.setVisibility(View.VISIBLE);
2239 toView.setFastAlpha(0f);
2240 ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(fadeDuration);
2241 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
2242 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2243 public void onAnimationUpdate(float a, float b) {
2244 // don't need to invalidate because we do so above
2245 toView.setFastAlpha(a * 0f + b * 1f);
2246 }
2247 });
Adam Cohencff6af82011-09-13 14:51:53 -07002248 alphaAnim.setStartDelay(startDelay);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002249 alphaAnim.start();
Adam Cohenf16e5712011-01-13 13:31:45 -08002250
Michael Jurka8edd75c2010-12-17 20:15:06 -08002251 scaleAnim.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002252 boolean animationCancelled = false;
2253
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002254 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002255 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002256 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002257 // Prepare the position
2258 toView.setTranslationX(0.0f);
2259 toView.setTranslationY(0.0f);
2260 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002261 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002262 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002263 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002264 public void onAnimationEnd(Animator animation) {
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002265 // If we don't set the final scale values here, if this animation is cancelled
2266 // it will have the wrong scale value and subsequent cameraPan animations will
2267 // not fix that
2268 toView.setScaleX(1.0f);
2269 toView.setScaleY(1.0f);
Michael Jurkaabded662011-03-04 12:06:57 -08002270 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002271 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance,
2272 scaleAnim, false);
Michael Jurka2763be32011-02-24 11:19:57 -08002273 }
Winson Chung32174c82011-07-19 15:47:55 -07002274
2275 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2276 // Hide the workspace scrollbar
2277 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002278 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002279 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002280 if (!animationCancelled) {
2281 updateWallpaperVisibility(false);
2282 }
2283 }
2284
Adam Cohencff6af82011-09-13 14:51:53 -07002285 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002286 public void onAnimationCancel(Animator animation) {
2287 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002288 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002289 });
2290
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002291 // toView should appear right at the end of the workspace shrink animation
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002292 mStateAnimation = new AnimatorSet();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002293 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka1899a362011-11-03 13:50:45 -07002294
2295 boolean delayAnim = false;
2296 if (toView instanceof LauncherTransitionable) {
2297 LauncherTransitionable lt = (LauncherTransitionable) toView;
2298 delayAnim = lt.onLauncherTransitionStart(instance, mStateAnimation, false);
2299 }
2300 // if the anim is delayed, the LauncherTransitionable is responsible for starting it
2301 if (!delayAnim) {
2302 // TODO: q-- what if this anim is cancelled before being started? or started after
2303 // being cancelled?
2304 mStateAnimation.start();
2305 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002306 } else {
2307 toView.setTranslationX(0.0f);
2308 toView.setTranslationY(0.0f);
2309 toView.setScaleX(1.0f);
2310 toView.setScaleY(1.0f);
2311 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002312 toView.bringToFront();
Michael Jurkaabded662011-03-04 12:06:57 -08002313 if (toView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002314 ((LauncherTransitionable) toView).onLauncherTransitionStart(instance, null, false);
2315 ((LauncherTransitionable) toView).onLauncherTransitionEnd(instance, null, false);
Winson Chung3ac74c52011-06-30 17:39:37 -07002316
Winson Chungc07918d2011-07-01 15:35:26 -07002317 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2318 // Hide the workspace scrollbar
2319 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002320 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002321 }
Michael Jurkaabded662011-03-04 12:06:57 -08002322 }
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002323 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002324 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002325 }
2326
2327 /**
2328 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002329 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002330 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002331 */
Michael Jurkab737ee62011-11-15 15:57:22 -08002332 private void hideAppsCustomizeHelper(boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002333 if (mStateAnimation != null) {
2334 mStateAnimation.cancel();
2335 mStateAnimation = null;
2336 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002337 Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002338 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002339
Winson Chungf0ea4d32011-06-06 14:27:16 -07002340 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
2341 final float scaleFactor = (float)
2342 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2343 final View fromView = mAppsCustomizeTabHost;
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002344
Michael Jurkab3e22d92011-10-31 15:58:33 -07002345 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002346 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002347 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002348 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002349 final float oldScaleX = fromView.getScaleX();
2350 final float oldScaleY = fromView.getScaleY();
2351
2352 ValueAnimator scaleAnim = ValueAnimator.ofFloat(0f, 1f).setDuration(duration);
2353 scaleAnim.setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002354 scaleAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2355 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002356 ((View)fromView.getParent()).fastInvalidate();
2357 fromView.setFastScaleX(a * oldScaleX + b * scaleFactor);
2358 fromView.setFastScaleY(a * oldScaleY + b * scaleFactor);
2359 }
2360 });
Michael Jurkaabded662011-03-04 12:06:57 -08002361 final ValueAnimator alphaAnim = ValueAnimator.ofFloat(0f, 1f);
Winson Chung785d2eb2011-04-14 16:08:02 -07002362 alphaAnim.setDuration(res.getInteger(R.integer.config_appsCustomizeFadeOutTime));
Adam Cohencff6af82011-09-13 14:51:53 -07002363 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Michael Jurkac2e26a02011-03-24 15:20:26 -07002364 alphaAnim.addUpdateListener(new LauncherAnimatorUpdateListener() {
2365 public void onAnimationUpdate(float a, float b) {
Michael Jurka742574b2011-02-02 23:51:01 -08002366 // don't need to invalidate because we do so above
2367 fromView.setFastAlpha(a * 1f + b * 0f);
2368 }
2369 });
Michael Jurkaabded662011-03-04 12:06:57 -08002370 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002371 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, alphaAnim,
2372 true);
Michael Jurka2763be32011-02-24 11:19:57 -08002373 }
Michael Jurka8edd75c2010-12-17 20:15:06 -08002374 alphaAnim.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002375 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002376 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002377 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002378 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002379 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002380 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance,
2381 alphaAnim, true);
Michael Jurka2763be32011-02-24 11:19:57 -08002382 }
Michael Jurka430e8a52011-08-08 15:52:14 -07002383 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002384 }
2385 });
2386
Michael Jurkab3e22d92011-10-31 15:58:33 -07002387 mStateAnimation = new AnimatorSet();
Winson Chungf0ea4d32011-06-06 14:27:16 -07002388 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002389 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002390 } else {
2391 fromView.setVisibility(View.GONE);
Michael Jurkaabded662011-03-04 12:06:57 -08002392 if (fromView instanceof LauncherTransitionable) {
Winson Chung7d7541e2011-09-16 20:14:36 -07002393 ((LauncherTransitionable) fromView).onLauncherTransitionStart(instance, null, true);
2394 ((LauncherTransitionable) fromView).onLauncherTransitionEnd(instance, null, true);
Michael Jurkaabded662011-03-04 12:06:57 -08002395 }
Michael Jurkab737ee62011-11-15 15:57:22 -08002396 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002397 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002398 }
2399
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002400 void showWorkspace(boolean animated) {
Adam Cohencff6af82011-09-13 14:51:53 -07002401 Resources res = getResources();
2402 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2403
2404 mWorkspace.changeState(Workspace.State.NORMAL, animated, stagger);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002405 if (mState != State.WORKSPACE) {
2406 mWorkspace.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08002407 hideAppsCustomizeHelper(animated, false);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002408
2409 // Show the search bar and hotseat
2410 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002411 // We only need to animate in the dock divider if we're going from spring loaded mode
2412 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002413
2414 // Set focus to the AppsCustomize button
2415 if (mAllAppsButton != null) {
2416 mAllAppsButton.requestFocus();
2417 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002418 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002419
Michael Jurkab737ee62011-11-15 15:57:22 -08002420 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002421
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002422 // Change the state *after* we've called all the transition code
2423 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002424
Winson Chung5fb63472011-02-02 17:03:37 -08002425 // Resume the auto-advance of widgets
2426 mUserPresent = true;
2427 updateRunning();
2428
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002429 // send an accessibility event to announce the context change
2430 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002431 }
2432
Michael Jurkab3e22d92011-10-31 15:58:33 -07002433 void showAllApps(boolean animated) {
2434 if (mState != State.WORKSPACE) return;
2435
2436 showAppsCustomizeHelper(animated, false);
2437 mAppsCustomizeTabHost.requestFocus();
2438
2439 // Hide the search bar and hotseat
2440 mSearchDropTargetBar.hideSearchBar(animated);
2441
2442 // Change the state *after* we've called all the transition code
2443 mState = State.APPS_CUSTOMIZE;
2444
2445 // Pause the auto-advance of widgets until we are out of AllApps
2446 mUserPresent = false;
2447 updateRunning();
2448 closeFolder();
2449
2450 // Send an accessibility event to announce the context change
2451 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2452 }
2453
Adam Cohen7777d962011-08-18 18:58:38 -07002454 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002455 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen7777d962011-08-18 18:58:38 -07002456 mWorkspace.changeState(Workspace.State.SPRING_LOADED);
Michael Jurkab737ee62011-11-15 15:57:22 -08002457 hideAppsCustomizeHelper(true, true);
2458 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002459 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002460 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002461 }
Adam Cohen7777d962011-08-18 18:58:38 -07002462
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002463 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay) {
Winson Chung09bfc452011-09-09 11:30:20 -07002464 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2465
Winson Chunge7a03942011-08-05 15:05:12 -07002466 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002467 @Override
2468 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002469 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002470 // Before we show workspace, hide all apps again because
2471 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2472 // clean up our state transition functions
2473 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002474 mSearchDropTargetBar.showSearchBar(true);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002475 showWorkspace(true);
Adam Cohen7777d962011-08-18 18:58:38 -07002476 } else {
2477 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002478 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002479 }
2480 }, (extendedDelay ?
2481 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2482 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2483 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002484
Michael Jurkad3ef3062010-11-23 16:23:58 -08002485 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002486 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002487 final boolean animated = true;
2488 final boolean springLoaded = true;
2489 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002490 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002491 }
2492 // Otherwise, we are not in spring loaded mode, so don't do anything.
2493 }
2494
Michael Jurkab737ee62011-11-15 15:57:22 -08002495 void hideDockDivider() {
2496 if (mQsbDivider != null && mDockDivider != null) {
2497 mQsbDivider.setVisibility(View.INVISIBLE);
2498 mDockDivider.setVisibility(View.INVISIBLE);
2499 }
2500 }
2501
2502 void showDockDivider(boolean animated) {
2503 if (mQsbDivider != null && mDockDivider != null) {
2504 mQsbDivider.setVisibility(View.VISIBLE);
2505 mDockDivider.setVisibility(View.VISIBLE);
2506 if (mDividerAnimator != null) {
2507 mDividerAnimator.cancel();
2508 mQsbDivider.setAlpha(1f);
2509 mDockDivider.setAlpha(1f);
2510 mDividerAnimator = null;
2511 }
2512 if (animated) {
2513 mDividerAnimator = new AnimatorSet();
2514 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2515 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2516 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2517 mDividerAnimator.start();
2518 }
2519 }
2520 }
2521
Michael Jurkab3e22d92011-10-31 15:58:33 -07002522 void lockAllApps() {
2523 // TODO
2524 }
2525
2526 void unlockAllApps() {
2527 // TODO
2528 }
2529
Adam Cohenfc53cd22011-07-20 15:45:11 -07002530 public boolean isAllAppsCustomizeOpen() {
2531 return mState == State.APPS_CUSTOMIZE;
2532 }
2533
Winson Chungf0ea4d32011-06-06 14:27:16 -07002534 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002535 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002536 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002537 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002538 if (!LauncherApplication.isScreenLarge()) {
2539 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002540 int duration = mSearchDropTargetBar.getTransitionInDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002541 mHotseat.animate().alpha(1f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002542 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002543 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002544 }
2545 }
2546 }
2547
2548 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002549 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002550 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002551 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002552 if (!LauncherApplication.isScreenLarge()) {
2553 if (animated) {
Winson Chungc51db6a2011-10-05 11:44:49 -07002554 int duration = mSearchDropTargetBar.getTransitionOutDuration();
Winson Chung3d503fb2011-07-13 17:25:49 -07002555 mHotseat.animate().alpha(0f).setDuration(duration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002556 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002557 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002558 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002559 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002560 }
2561
Patrick Dubroy5f445422011-02-18 14:35:21 -08002562 /**
2563 * Add an item from all apps or customize onto the given workspace screen.
2564 * If layout is null, add to the current screen.
2565 */
2566 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002567 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2568 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002569 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002570 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002571
Winson Chungdff8ebb2011-09-08 17:25:31 -07002572 /** Maps the current orientation to an index for referencing orientation correct global icons */
2573 private int getCurrentOrientationIndexForGlobalIcons() {
2574 // default - 0, landscape - 1
2575 switch (getResources().getConfiguration().orientation) {
2576 case Configuration.ORIENTATION_LANDSCAPE:
2577 return 1;
2578 default:
2579 return 0;
2580 }
2581 }
2582
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002583 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002584 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002585 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002586 // Look for the toolbar icon specified in the activity meta-data
2587 Bundle metaData = packageManager.getActivityInfo(
2588 activityName, PackageManager.GET_META_DATA).metaData;
2589 if (metaData != null) {
2590 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2591 if (iconResId != 0) {
2592 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002593 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002594 }
2595 }
2596 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002597 // This can happen if the activity defines an invalid drawable
2598 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2599 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002600 } catch (Resources.NotFoundException nfe) {
2601 // This can happen if the activity defines an invalid drawable
2602 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2603 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002604 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002605 return null;
2606 }
2607
2608 // if successful in getting icon, return it; otherwise, set button to use default drawable
2609 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2610 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002611 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002612 Resources r = getResources();
2613 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2614 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002615
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002616 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002617 // If we were unable to find the icon via the meta-data, use a generic one
2618 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002619 toolbarIcon = r.getDrawable(fallbackDrawableId);
2620 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002621 if (button != null) {
2622 button.setCompoundDrawables(toolbarIcon, null, null, null);
2623 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002624 return null;
2625 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002626 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002627 if (button != null) {
2628 button.setCompoundDrawables(toolbarIcon, null, null, null);
2629 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002630 return toolbarIcon.getConstantState();
2631 }
2632 }
2633
2634 // if successful in getting icon, return it; otherwise, set button to use default drawable
2635 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2636 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2637 ImageView button = (ImageView) findViewById(buttonId);
2638 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2639
Michael Jurka19e0fc52011-07-22 18:00:21 -07002640 if (button != null) {
2641 // If we were unable to find the icon via the meta-data, use a
2642 // generic one
2643 if (toolbarIcon == null) {
2644 button.setImageResource(fallbackDrawableId);
2645 } else {
2646 button.setImageDrawable(toolbarIcon);
2647 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002648 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002649
2650 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2651
Michael Jurka0423dcf2010-10-05 14:56:18 -07002652 }
2653
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002654 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2655 TextView button = (TextView) findViewById(buttonId);
2656 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2657 }
2658
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002659 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002660 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002661 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002662 }
2663
Winson Chungbb185bd2011-11-21 12:31:42 -08002664 private void invalidatePressedFocusedStates(View container, View button) {
2665 if (container instanceof HolographicLinearLayout) {
2666 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2667 layout.invalidatePressedFocusedStates();
2668 } else if (button instanceof HolographicImageView) {
2669 HolographicImageView view = (HolographicImageView) button;
2670 view.invalidatePressedFocusedStates();
2671 }
2672 }
2673
Winson Chungc51db6a2011-10-05 11:44:49 -07002674 private boolean updateGlobalSearchIcon() {
2675 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002676 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2677 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002678 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002679 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002680
Winson Chung1cad91e2011-05-25 17:41:01 -07002681 final SearchManager searchManager =
2682 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2683 ComponentName activityName = searchManager.getGlobalSearchActivity();
2684 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002685 int coi = getCurrentOrientationIndexForGlobalIcons();
2686 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002687 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002688 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002689 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2690 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002691 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002692 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002693 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002694 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002695 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002696 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2697 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2698 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002699 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002700 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002701 }
2702 }
2703
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002704 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002705 final View searchButtonContainer = findViewById(R.id.search_button_container);
2706 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002707 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002708 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002709 }
2710
Winson Chungc51db6a2011-10-05 11:44:49 -07002711 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002712 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002713 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002714 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002715
Winson Chungc51db6a2011-10-05 11:44:49 -07002716 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002717 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2718 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002719 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002720 int coi = getCurrentOrientationIndexForGlobalIcons();
2721 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002722 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002723 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002724 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002725 voiceButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002726 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002727 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002728 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002729 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002730 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002731 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002732 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002733 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002734 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002735
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002736 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002737 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2738 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002739 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002740 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002741 }
2742
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002743 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002744 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002745 */
2746 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002747 final View marketButton = findViewById(R.id.market_button);
2748 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2749 // Find the app market activity by resolving an intent.
2750 // (If multiple app markets are installed, it will return the ResolverActivity.)
2751 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002752 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002753 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002754 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002755 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002756 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002757 marketButton.setVisibility(View.VISIBLE);
2758 } else {
2759 // We should hide and disable the view so that we don't try and restore the visibility
2760 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2761 marketButton.setVisibility(View.GONE);
2762 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002763 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002764 }
2765
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002766 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002767 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002768 }
2769
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002770 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002771 * Displays the shortcut creation dialog and launches, if necessary, the
2772 * appropriate activity.
2773 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002774 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002775 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2776 DialogInterface.OnShowListener {
2777
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002778 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002779
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002780 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002781 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002782
Winson Chung55b65502011-05-26 12:03:43 -07002783 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2784 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002785 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002786
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002787 AlertDialog dialog = builder.create();
2788 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002789 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002790 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002791
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002792 return dialog;
2793 }
2794
2795 public void onCancel(DialogInterface dialog) {
2796 mWaitingForResult = false;
2797 cleanup();
2798 }
2799
Romain Guycbb89e42009-06-08 15:52:54 -07002800 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002801 mWaitingForResult = false;
2802 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002803 }
2804
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002805 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002806 try {
2807 dismissDialog(DIALOG_CREATE_SHORTCUT);
2808 } catch (Exception e) {
2809 // An exception is thrown if the dialog is not visible, which is fine
2810 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002811 }
2812
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002813 /**
2814 * Handle the action clicked in the "Add to home" dialog.
2815 */
2816 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002817 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002818
Winson Chung55b65502011-05-26 12:03:43 -07002819 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2820 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002821 case AddAdapter.ITEM_APPLICATION: {
2822 if (mAppsCustomizeTabHost != null) {
2823 mAppsCustomizeTabHost.selectAppsTab();
2824 }
2825 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002826 break;
2827 }
Winson Chung55b65502011-05-26 12:03:43 -07002828 case AddAdapter.ITEM_APPWIDGET: {
2829 if (mAppsCustomizeTabHost != null) {
2830 mAppsCustomizeTabHost.selectWidgetsTab();
2831 }
2832 showAllApps(true);
2833 break;
2834 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002835 case AddAdapter.ITEM_WALLPAPER: {
2836 startWallpaper();
2837 break;
2838 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002839 }
2840 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002841
2842 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002843 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002845 }
2846
2847 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002848 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002849 */
2850 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2851 @Override
2852 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002853 closeSystemDialogs();
2854 }
2855 }
2856
2857 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002858 * Receives notifications whenever the appwidgets are reset.
2859 */
2860 private class AppWidgetResetObserver extends ContentObserver {
2861 public AppWidgetResetObserver() {
2862 super(new Handler());
2863 }
2864
2865 @Override
2866 public void onChange(boolean selfChange) {
2867 onAppWidgetReset();
2868 }
2869 }
2870
2871 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002872 * If the activity is currently paused, signal that we need to re-run the loader
2873 * in onResume.
2874 *
2875 * This needs to be called from incoming places where resources might have been loaded
2876 * while we are paused. That is becaues the Configuration might be wrong
2877 * when we're not running, and if it comes back to what it was when we
2878 * were paused, we are not restarted.
2879 *
2880 * Implementation of the method from LauncherModel.Callbacks.
2881 *
2882 * @return true if we are currently paused. The caller might be able to
2883 * skip some work in that case since we will come back again.
2884 */
2885 public boolean setLoadOnResume() {
2886 if (mPaused) {
2887 Log.i(TAG, "setLoadOnResume");
2888 mOnResumeNeedsLoad = true;
2889 return true;
2890 } else {
2891 return false;
2892 }
2893 }
2894
2895 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002896 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002897 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002898 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002899 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002900 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002901 } else {
2902 return SCREEN_COUNT / 2;
2903 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002904 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002905
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002906
Joe Onorato9c1289c2009-08-17 11:03:03 -04002907 /**
2908 * Refreshes the shortcuts shown on the workspace.
2909 *
2910 * Implementation of the method from LauncherModel.Callbacks.
2911 */
2912 public void startBinding() {
2913 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002914
2915 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002916 int count = workspace.getChildCount();
2917 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002918 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002919 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2920 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002921 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002922 if (mHotseat != null) {
2923 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002924 }
2925 }
2926
2927 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002928 * Bind the items start-end from the list.
2929 *
2930 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002931 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002932 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002933 setLoadOnResume();
2934
Joe Onorato9c1289c2009-08-17 11:03:03 -04002935 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002936 for (int i=start; i<end; i++) {
2937 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002938
2939 // Short circuit if we are loading dock items for a configuration which has no dock
2940 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2941 mHotseat == null) {
2942 continue;
2943 }
2944
Joe Onorato9c1289c2009-08-17 11:03:03 -04002945 switch (item.itemType) {
2946 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2947 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07002948 View shortcut = createShortcut((ShortcutInfo)item);
2949 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2950 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002951 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002952 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002953 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002954 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002955 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002956 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2957 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002958 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002960 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002961 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 }
2963
Joe Onorato9c1289c2009-08-17 11:03:03 -04002964 /**
2965 * Implementation of the method from LauncherModel.Callbacks.
2966 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002967 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002968 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002969 sFolders.clear();
2970 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002972
2973 /**
2974 * Add the views for a widget to the workspace.
2975 *
2976 * Implementation of the method from LauncherModel.Callbacks.
2977 */
2978 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002979 setLoadOnResume();
2980
Daniel Sandler843e8602010-06-07 14:59:01 -04002981 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2982 if (DEBUG_WIDGETS) {
2983 Log.d(TAG, "bindAppWidget: " + item);
2984 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002985 final Workspace workspace = mWorkspace;
2986
2987 final int appWidgetId = item.appWidgetId;
2988 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002989 if (DEBUG_WIDGETS) {
2990 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2991 }
2992
Joe Onorato9c1289c2009-08-17 11:03:03 -04002993 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2994
Joe Onorato9c1289c2009-08-17 11:03:03 -04002995 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2996 item.hostView.setTag(item);
2997
Winson Chung3d503fb2011-07-13 17:25:49 -07002998 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002999 item.cellY, item.spanX, item.spanY, false);
3000
Adam Cohended9f8d2010-11-03 13:25:16 -07003001 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3002
Joe Onorato9c1289c2009-08-17 11:03:03 -04003003 workspace.requestLayout();
3004
Daniel Sandler843e8602010-06-07 14:59:01 -04003005 if (DEBUG_WIDGETS) {
3006 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3007 + (SystemClock.uptimeMillis()-start) + "ms");
3008 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003009 }
3010
3011 /**
3012 * Callback saying that there aren't any more items to bind.
3013 *
3014 * Implementation of the method from LauncherModel.Callbacks.
3015 */
3016 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003017 setLoadOnResume();
3018
Joe Onorato9c1289c2009-08-17 11:03:03 -04003019 if (mSavedState != null) {
3020 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003021 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003022 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003023 mSavedState = null;
3024 }
3025
3026 if (mSavedInstanceState != null) {
3027 super.onRestoreInstanceState(mSavedInstanceState);
3028 mSavedInstanceState = null;
3029 }
3030
Joe Onorato9c1289c2009-08-17 11:03:03 -04003031 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003032
3033 // If we received the result of any pending adds while the loader was running (e.g. the
3034 // widget configuration forced an orientation change), process them now.
3035 for (int i = 0; i < sPendingAddList.size(); i++) {
3036 completeAdd(sPendingAddList.get(i));
3037 }
3038 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003039
Winson Chungc51db6a2011-10-05 11:44:49 -07003040 // Update the market app icon as necessary (the other icons will be managed in response to
3041 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003042 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003043
3044 mWorkspace.post(mBuildLayersRunnable);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003045 }
3046
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003047 @Override
3048 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003049 boolean searchVisible = updateGlobalSearchIcon();
3050 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3051 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003052 }
3053
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003054 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003055 * Add the icons for all apps.
3056 *
3057 * Implementation of the method from LauncherModel.Callbacks.
3058 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003059 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3060 // Remove the progress bar entirely; we could also make it GONE
3061 // but better to remove it since we know it's not going to be used
3062 View progressBar = mAppsCustomizeTabHost.
3063 findViewById(R.id.apps_customize_progress_bar);
3064 if (progressBar != null) {
3065 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003066 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003067 // We just post the call to setApps so the user sees the progress bar
3068 // disappear-- otherwise, it just looks like the progress bar froze
3069 // which doesn't look great
3070 mAppsCustomizeTabHost.post(new Runnable() {
3071 public void run() {
3072 if (mAppsCustomizeContent != null) {
3073 mAppsCustomizeContent.setApps(apps);
3074 }
3075 }
3076 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003077 }
3078
3079 /**
3080 * A package was installed.
3081 *
3082 * Implementation of the method from LauncherModel.Callbacks.
3083 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003084 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003085 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003086 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003087
Winson Chung785d2eb2011-04-14 16:08:02 -07003088 if (mAppsCustomizeContent != null) {
3089 mAppsCustomizeContent.addApps(apps);
3090 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003091 }
3092
3093 /**
3094 * A package was updated.
3095 *
3096 * Implementation of the method from LauncherModel.Callbacks.
3097 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003098 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003099 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003100 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003101 if (mWorkspace != null) {
3102 mWorkspace.updateShortcuts(apps);
3103 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003104
Winson Chung785d2eb2011-04-14 16:08:02 -07003105 if (mAppsCustomizeContent != null) {
3106 mAppsCustomizeContent.updateApps(apps);
3107 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003108 }
3109
3110 /**
3111 * A package was uninstalled.
3112 *
3113 * Implementation of the method from LauncherModel.Callbacks.
3114 */
Joe Onorato36115782010-06-17 13:28:48 -04003115 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003116 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003117 if (permanent) {
3118 mWorkspace.removeItems(apps);
3119 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003120
Winson Chung785d2eb2011-04-14 16:08:02 -07003121 if (mAppsCustomizeContent != null) {
3122 mAppsCustomizeContent.removeApps(apps);
3123 }
Winson Chunga1820962011-10-03 16:31:06 -07003124
3125 // Notify the drag controller
3126 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003127 }
Joe Onoratobe386092009-11-17 17:32:16 -08003128
3129 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003130 * A number of packages were updated.
3131 */
3132 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003133 if (mAppsCustomizeContent != null) {
3134 mAppsCustomizeContent.onPackagesUpdated();
3135 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003136 }
3137
Winson Chung400438b2011-01-16 17:53:48 -08003138 private int mapConfigurationOriActivityInfoOri(int configOri) {
3139 final Display d = getWindowManager().getDefaultDisplay();
3140 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3141 switch (d.getRotation()) {
3142 case Surface.ROTATION_0:
3143 case Surface.ROTATION_180:
3144 // We are currently in the same basic orientation as the natural orientation
3145 naturalOri = configOri;
3146 break;
3147 case Surface.ROTATION_90:
3148 case Surface.ROTATION_270:
3149 // We are currently in the other basic orientation to the natural orientation
3150 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3151 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3152 break;
3153 }
3154
3155 int[] oriMap = {
3156 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3157 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3158 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3159 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3160 };
3161 // Since the map starts at portrait, we need to offset if this device's natural orientation
3162 // is landscape.
3163 int indexOffset = 0;
3164 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3165 indexOffset = 1;
3166 }
3167 return oriMap[(d.getRotation() + indexOffset) % 4];
3168 }
Adam Cohen446e9402011-09-15 18:21:21 -07003169
3170 public void lockScreenOrientationOnLargeUI() {
3171 if (LauncherApplication.isScreenLarge()) {
3172 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3173 .getConfiguration().orientation));
3174 }
Winson Chung400438b2011-01-16 17:53:48 -08003175 }
Adam Cohen446e9402011-09-15 18:21:21 -07003176 public void unlockScreenOrientationOnLargeUI() {
3177 if (LauncherApplication.isScreenLarge()) {
3178 mHandler.postDelayed(new Runnable() {
3179 public void run() {
3180 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3181 }
3182 }, mRestoreScreenOrientationDelay);
3183 }
Winson Chung400438b2011-01-16 17:53:48 -08003184 }
3185
Winson Chung82f55532011-08-09 14:14:23 -07003186 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003187 private static final String PREFS_KEY = "com.android.launcher2.prefs";
3188 private boolean isClingsEnabled() {
Winson Chung82f55532011-08-09 14:14:23 -07003189 // TEMPORARY: DISABLE CLINGS ON LARGE UI
Winson Chung7d7541e2011-09-16 20:14:36 -07003190 if (LauncherApplication.isScreenLarge()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003191 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003192 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003193
Winson Chung7d7541e2011-09-16 20:14:36 -07003194 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003195 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003196 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3197 Cling cling = (Cling) findViewById(clingId);
3198 if (cling != null) {
3199 cling.init(this, positionData);
3200 cling.setVisibility(View.VISIBLE);
3201 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3202 if (animate) {
3203 cling.buildLayer();
3204 cling.setAlpha(0f);
3205 cling.animate()
3206 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003207 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003208 .setDuration(SHOW_CLING_DURATION)
3209 .setStartDelay(delay)
3210 .start();
3211 } else {
3212 cling.setAlpha(1f);
3213 }
3214 }
3215 return cling;
3216 }
3217 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003218 if (cling != null) {
3219 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003220 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003221 anim.addListener(new AnimatorListenerAdapter() {
3222 public void onAnimationEnd(Animator animation) {
3223 cling.setVisibility(View.GONE);
3224 cling.cleanup();
3225 SharedPreferences prefs =
3226 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3227 SharedPreferences.Editor editor = prefs.edit();
3228 editor.putBoolean(flag, true);
3229 editor.commit();
3230 };
3231 });
3232 anim.start();
3233 }
3234 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003235 private void removeCling(int id) {
3236 final View cling = findViewById(id);
3237 if (cling != null) {
3238 final ViewGroup parent = (ViewGroup) cling.getParent();
3239 parent.post(new Runnable() {
3240 @Override
3241 public void run() {
3242 parent.removeView(cling);
3243 }
3244 });
3245 }
3246 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003247 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003248 // Enable the clings only if they have not been dismissed before
3249 SharedPreferences prefs =
3250 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003251 if (isClingsEnabled() && !prefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003252 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003253 } else {
3254 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003255 }
3256 }
3257 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003258 // Enable the clings only if they have not been dismissed before
3259 SharedPreferences prefs =
3260 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003261 if (isClingsEnabled() && !prefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003262 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003263 } else {
3264 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003265 }
3266 }
3267 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003268 // Enable the clings only if they have not been dismissed before
3269 SharedPreferences prefs =
3270 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
3271 Cling cling = null;
Winson Chung9d9d74f2011-09-19 11:49:12 -07003272 if (isClingsEnabled() && !prefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003273 cling = initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003274 } else {
3275 removeCling(R.id.folder_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003276 }
3277 return cling;
3278 }
3279 public boolean isFolderClingVisible() {
3280 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003281 if (cling != null) {
3282 return cling.getVisibility() == View.VISIBLE;
3283 }
3284 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003285 }
Winson Chung82f55532011-08-09 14:14:23 -07003286 public void dismissWorkspaceCling(View v) {
3287 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003288 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003289 }
3290 public void dismissAllAppsCling(View v) {
3291 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003292 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3293 }
3294 public void dismissFolderCling(View v) {
3295 Cling cling = (Cling) findViewById(R.id.folder_cling);
3296 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003297 }
3298
Winson Chung80baf5a2010-08-09 16:03:15 -07003299 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003300 * Prints out out state for debugging.
3301 */
3302 public void dumpState() {
3303 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003304 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003305 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3306 Log.d(TAG, "mRestoring=" + mRestoring);
3307 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3308 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003309 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003310 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003311
Winson Chung785d2eb2011-04-14 16:08:02 -07003312 if (mAppsCustomizeContent != null) {
3313 mAppsCustomizeContent.dumpState();
3314 }
Joe Onoratobe386092009-11-17 17:32:16 -08003315 Log.d(TAG, "END launcher2 dump state");
3316 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003317
3318 @Override
3319 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3320 super.dump(prefix, fd, writer, args);
3321 writer.println(" ");
3322 writer.println("Debug logs: ");
3323 for (int i = 0; i < sDumpLogs.size(); i++) {
3324 writer.println(" " + sDumpLogs.get(i));
3325 }
3326 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003327}
Michael Jurka2763be32011-02-24 11:19:57 -08003328
Michael Jurkaabded662011-03-04 12:06:57 -08003329interface LauncherTransitionable {
Michael Jurka1899a362011-11-03 13:50:45 -07003330 // return true if the callee will take care of start the animation by itself
3331 boolean onLauncherTransitionStart(Launcher l, Animator animation, boolean toWorkspace);
Winson Chung7d7541e2011-09-16 20:14:36 -07003332 void onLauncherTransitionEnd(Launcher l, Animator animation, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003333}