blob: 1a6691db07436d972ccf88e949513d1d8eec6fc1 [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;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.Dialog;
31import android.app.SearchManager;
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;
Michael Jurkae326f182011-11-21 14:05:46 -080039import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040040import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080041import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Context;
43import android.content.DialogInterface;
44import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070045import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070046import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070047import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Adam Cohened66b2b2012-01-23 17:28:51 -080053import android.graphics.Bitmap;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040056import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070057import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020059import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070061import android.os.Message;
Daniel Sandler843e8602010-06-07 14:59:01 -040062import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080063import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070064import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080065import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
68import android.text.TextUtils;
69import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070070import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080071import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080072import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.KeyEvent;
74import android.view.LayoutInflater;
75import android.view.Menu;
76import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070077import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080078import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080080import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070081import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080082import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080083import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070084import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070086import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070087import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080088import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070090import android.widget.Advanceable;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.widget.EditText;
Michael Jurkaaf442092010-06-10 17:01:57 -070092import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070093import android.widget.TextView;
94import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070095
Adam Cohendf2cc412011-04-27 16:56:57 -070096import com.android.common.Search;
97import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070098import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080099
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.io.DataInputStream;
101import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700102import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.FileNotFoundException;
104import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.ArrayList;
107import java.util.HashMap;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109/**
110 * Default launcher application.
111 */
Michael Jurka946ad472010-07-09 18:05:18 -0700112public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700113 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
114 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800115 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700116 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400119 static final boolean DEBUG_WIDGETS = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700120
Winson Chung70d72102011-08-12 11:24:35 -0700121 private static final int MENU_GROUP_WALLPAPER = 1;
122 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
123 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700124 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
125 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800126
127 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700128 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129 private static final int REQUEST_PICK_APPLICATION = 6;
130 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700131 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700132 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
135
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800136 static final int SCREEN_COUNT = 5;
137 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Joe Onorato7c312c12009-08-13 21:36:53 -0700139 static final int DIALOG_CREATE_SHORTCUT = 1;
140 static final int DIALOG_RENAME_FOLDER = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Romain Guy98d01652009-06-30 16:21:04 -0700142 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800143 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144
145 // Type: int
146 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700147 // Type: int
148 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700150 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
151 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
153 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700154 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700156 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: boolean
158 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
159 // Type: long
160 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
161
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700162 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
163
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700164 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700165 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700166 private State mState = State.WORKSPACE;
167 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800168 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700169
Joe Onorato9c1289c2009-08-17 11:03:03 -0400170 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700171 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
172 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700173 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700174 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800177 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800179 private final BroadcastReceiver mCloseSystemDialogsReceiver
180 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800181 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private LayoutInflater mInflater;
184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800186 private View mQsbDivider;
187 private View mDockDivider;
188 private DragLayer mDragLayer;
189 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700190
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700191 private AppWidgetManager mAppWidgetManager;
192 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700193
Michael Jurkac9d95c52011-08-29 14:03:34 -0700194 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700195 private int[] mTmpAddItemCellCoordinates = new int[2];
196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private FolderInfo mFolderInfo;
198
Winson Chung3d503fb2011-07-13 17:25:49 -0700199 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800200 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700201
Winson Chungc51db6a2011-10-05 11:44:49 -0700202 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700203 private AppsCustomizeTabHost mAppsCustomizeTabHost;
204 private AppsCustomizePagedView mAppsCustomizeContent;
205 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 private Bundle mSavedState;
208
209 private SpannableStringBuilder mDefaultKeySsb = null;
210
Joe Onorato9c1289c2009-08-17 11:03:03 -0400211 private boolean mWorkspaceLoading = true;
212
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800213 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800214 private boolean mRestoring;
215 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700216 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800217
218 private Bundle mSavedInstanceState;
219
Joe Onorato9c1289c2009-08-17 11:03:03 -0400220 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800221 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800222 private boolean mUserPresent = true;
223 private boolean mVisible = false;
224 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700226 private static LocaleConfiguration sLocaleConfiguration = null;
227
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700228 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700229
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700230 private Intent mAppMarketIntent = null;
231
Adam Cohended9f8d2010-11-03 13:25:16 -0700232 // Related to the auto-advancing of widgets
233 private final int ADVANCE_MSG = 1;
234 private final int mAdvanceInterval = 20000;
235 private final int mAdvanceStagger = 250;
236 private long mAutoAdvanceSentTime;
237 private long mAutoAdvanceTimeLeft = -1;
238 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
239 new HashMap<View, AppWidgetProviderInfo>();
240
Winson Chung400438b2011-01-16 17:53:48 -0800241 // Determines how long to wait after a rotation before restoring the screen orientation to
242 // match the sensor state.
243 private final int mRestoreScreenOrientationDelay = 500;
244
Michael Jurka4ef207b2010-11-29 17:05:45 -0800245 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700246 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
247 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
248 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800249
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700250 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohened66b2b2012-01-23 17:28:51 -0800251 PendingAddWidgetInfo mWidgetBeingConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700252
Adam Cohen2801caf2011-05-13 20:57:39 -0700253
Michael Jurkaddd62e92011-02-16 17:49:14 -0800254 private BubbleTextView mWaitingForResume;
255
Michael Jurkac1f5d262011-09-30 19:32:27 -0700256 private Runnable mBuildLayersRunnable = new Runnable() {
257 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700258 if (mWorkspace != null) {
259 mWorkspace.buildPageHardwareLayers();
260 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700261 }
262 };
263
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800264 private static ArrayList<PendingAddArguments> sPendingAddList
265 = new ArrayList<PendingAddArguments>();
266
267 private static class PendingAddArguments {
268 int requestCode;
269 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700270 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800271 int screen;
272 int cellX;
273 int cellY;
274 }
275
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276 @Override
277 protected void onCreate(Bundle savedInstanceState) {
278 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800279 LauncherApplication app = ((LauncherApplication)getApplication());
280 mModel = app.setLauncher(this);
281 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400282 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800283 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700284
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700285 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700286 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
287 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700288
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800289 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200290 android.os.Debug.startMethodTracing(
291 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 }
293
294 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 setContentView(R.layout.launcher);
296 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700297 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800299 registerContentObservers();
300
Joe Onorato7c312c12009-08-13 21:36:53 -0700301 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700302
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 mSavedState = savedInstanceState;
304 restoreState(mSavedState);
305
Winson Chunga12a2502010-12-20 14:41:35 -0800306 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700307 if (mAppsCustomizeContent != null) {
308 mAppsCustomizeContent.onPackagesUpdated();
309 }
Winson Chunga12a2502010-12-20 14:41:35 -0800310
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800311 if (PROFILE_STARTUP) {
312 android.os.Debug.stopMethodTracing();
313 }
314
315 if (!mRestoring) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400316 mModel.startLoader(this, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800317 }
318
Michael Jurkac57b7a82011-08-09 22:02:20 -0700319 if (!mModel.isAllAppsLoaded()) {
320 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
321 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
322 }
323
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324 // For handling default keys
325 mDefaultKeySsb = new SpannableStringBuilder();
326 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800327
328 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
329 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800330
Winson Chungc51db6a2011-10-05 11:44:49 -0700331 boolean searchVisible = false;
332 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800333 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700334 int coi = getCurrentOrientationIndexForGlobalIcons();
335 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
336 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700337 updateAppMarketIcon();
338 searchVisible = updateGlobalSearchIcon();
339 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700340 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700341 if (sGlobalSearchIcon[coi] != null) {
342 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700343 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700344 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700345 if (sVoiceSearchIcon[coi] != null) {
346 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700347 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700348 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700349 if (sAppMarketIcon[coi] != null) {
350 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800351 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700352 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700353
Adam Cohen23a4d302011-12-01 17:26:44 -0800354 final String forceEnableRotation =
355 SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
356
Adam Cohen446e9402011-09-15 18:21:21 -0700357 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohen23a4d302011-12-01 17:26:44 -0800358 if (LauncherApplication.isScreenLarge() || "true".equalsIgnoreCase(forceEnableRotation)) {
Adam Cohen446e9402011-09-15 18:21:21 -0700359 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
360 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 }
Romain Guycbb89e42009-06-08 15:52:54 -0700362
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700364 if (sLocaleConfiguration == null) {
365 new AsyncTask<Void, Void, LocaleConfiguration>() {
366 @Override
367 protected LocaleConfiguration doInBackground(Void... unused) {
368 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
369 readConfiguration(Launcher.this, localeConfiguration);
370 return localeConfiguration;
371 }
372
373 @Override
374 protected void onPostExecute(LocaleConfiguration result) {
375 sLocaleConfiguration = result;
376 checkForLocaleChange(); // recursive, but now with a locale configuration
377 }
378 }.execute();
379 return;
380 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700381
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800382 final Configuration configuration = getResources().getConfiguration();
383
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700384 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 final String locale = configuration.locale.toString();
386
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700387 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 final int mcc = configuration.mcc;
389
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700390 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 final int mnc = configuration.mnc;
392
Romain Guy84f296c2009-11-04 15:00:44 -0800393 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394
Romain Guy84f296c2009-11-04 15:00:44 -0800395 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700396 sLocaleConfiguration.locale = locale;
397 sLocaleConfiguration.mcc = mcc;
398 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700399
Joe Onorato0589f0f2010-02-08 13:44:00 -0800400 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700401
402 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
403 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700404 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700405 public void run() {
406 writeConfiguration(Launcher.this, localeConfiguration);
407 }
408 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700409 }
410 }
411
412 private static class LocaleConfiguration {
413 public String locale;
414 public int mcc = -1;
415 public int mnc = -1;
416 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700417
Romain Guy98d01652009-06-30 16:21:04 -0700418 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
419 DataInputStream in = null;
420 try {
421 in = new DataInputStream(context.openFileInput(PREFERENCES));
422 configuration.locale = in.readUTF();
423 configuration.mcc = in.readInt();
424 configuration.mnc = in.readInt();
425 } catch (FileNotFoundException e) {
426 // Ignore
427 } catch (IOException e) {
428 // Ignore
429 } finally {
430 if (in != null) {
431 try {
432 in.close();
433 } catch (IOException e) {
434 // Ignore
435 }
436 }
437 }
438 }
439
440 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
441 DataOutputStream out = null;
442 try {
443 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
444 out.writeUTF(configuration.locale);
445 out.writeInt(configuration.mcc);
446 out.writeInt(configuration.mnc);
447 out.flush();
448 } catch (FileNotFoundException e) {
449 // Ignore
450 } catch (IOException e) {
451 //noinspection ResultOfMethodCallIgnored
452 context.getFileStreamPath(PREFERENCES).delete();
453 } finally {
454 if (out != null) {
455 try {
456 out.close();
457 } catch (IOException e) {
458 // Ignore
459 }
460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 }
462 }
463
Adam Cohen716b51e2011-06-30 12:09:54 -0700464 public DragLayer getDragLayer() {
465 return mDragLayer;
466 }
467
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 static int getScreen() {
469 synchronized (sLock) {
470 return sScreen;
471 }
472 }
473
474 static void setScreen(int screen) {
475 synchronized (sLock) {
476 sScreen = screen;
477 }
478 }
479
Winson Chung557d6ed2011-07-08 15:34:52 -0700480 /**
481 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
482 * a configuration step, this allows the proper animations to run after other transitions.
483 */
484 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700485 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800486 switch (args.requestCode) {
487 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700488 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
489 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800490 break;
491 case REQUEST_PICK_SHORTCUT:
492 processShortcut(args.intent);
493 break;
494 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700495 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
496 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700497 result = true;
498 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800499 case REQUEST_PICK_APPWIDGET:
500 addAppWidgetFromPick(args.intent);
501 break;
502 case REQUEST_CREATE_APPWIDGET:
503 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800504 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700505 result = true;
506 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800507 case REQUEST_PICK_WALLPAPER:
508 // We just wanted the activity result here so we can clear mWaitingForResult
509 break;
510 }
Winson Chungb8472bb2011-08-05 13:49:21 -0700511 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800512 }
513
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800514 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800515 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700516 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800517 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
518 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700519 mWaitingForResult = false;
520
Adam Cohened66b2b2012-01-23 17:28:51 -0800521 // We have special handling for widgets
522 if (isWidgetDrop) {
523 int appWidgetId = data != null ?
524 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
525 completeTwoStageWidgetDrop(resultCode, appWidgetId);
526 return;
527 }
528
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800529 // The pattern used here is that a user PICKs a specific application,
530 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700531
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800532 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
533 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700534 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800535 final PendingAddArguments args = new PendingAddArguments();
536 args.requestCode = requestCode;
537 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700538 args.container = mPendingAddInfo.container;
539 args.screen = mPendingAddInfo.screen;
540 args.cellX = mPendingAddInfo.cellX;
541 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800542 if (isWorkspaceLocked()) {
543 sPendingAddList.add(args);
544 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700545 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800547 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800548 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700549 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800550 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
551 null);
552 }
553
554 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
555 CellLayout cellLayout = (CellLayout) mWorkspace.getChildAt(mWidgetBeingConfigured.screen);
556 Runnable onCompleteRunnable = null;
557 int animationType = 0;
558
559 if (resultCode == RESULT_OK) {
560 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
561 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
562 mWidgetBeingConfigured.info);
563 mWidgetBeingConfigured.boundWidget = layout;
564 onCompleteRunnable = new Runnable() {
565 @Override
566 public void run() {
567 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
568 mPendingAddInfo.screen, layout, null);
569 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
570 null);
571 }
572 };
573 } else if (resultCode == RESULT_CANCELED) {
574 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
575 onCompleteRunnable = new Runnable() {
576 @Override
577 public void run() {
578 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
579 null);
580 }
581 };
582 }
583 mWorkspace.animateExternalDrop(mWidgetBeingConfigured, cellLayout,
584 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
585 animationType);
Adam Cohen1b36dc32012-02-13 19:27:37 -0800586 mWidgetBeingConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800587 }
588
589 @Override
590 protected void onResume() {
591 super.onResume();
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800592 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700593 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400594 mWorkspaceLoading = true;
595 mModel.startLoader(this, true);
596 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700597 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
Winson Chunge4e50662012-01-23 14:45:13 -0800599
600 // Reset the pressed state of icons that were locked in the press state while activities
601 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800602 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800603 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800604 mWaitingForResume.setStayPressed(false);
605 }
Winson Chunge4e50662012-01-23 14:45:13 -0800606 if (mAppsCustomizeContent != null) {
607 // Resets the previous all apps icon press state
608 mAppsCustomizeContent.resetDrawableState();
609 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 }
611
612 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700613 protected void onPause() {
614 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700615 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800616 mDragController.cancelDrag();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700617 }
Romain Guycbb89e42009-06-08 15:52:54 -0700618
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700619 @Override
Michael Jurkabed61d22012-02-14 22:51:29 -0800620 protected void onStart() {
621 super.onStart();
622 // When Launcher comes back to foreground, a different Activity might be responsible for
623 // the app market intent, so refresh the icon
624 // We don't do this in onResume() because onResume() is triggered every time the home
625 // button is pressed (even if we were already in Launcher)
626 updateAppMarketIcon();
627 }
628
629 @Override
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700630 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400631 // Flag the loader to stop early before switching
632 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700633 if (mAppsCustomizeContent != null) {
634 mAppsCustomizeContent.surrender();
635 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800636 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700637 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700638
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800639 // We can't hide the IME if it was forced open. So don't bother
640 /*
641 @Override
642 public void onWindowFocusChanged(boolean hasFocus) {
643 super.onWindowFocusChanged(hasFocus);
644
645 if (hasFocus) {
646 final InputMethodManager inputManager = (InputMethodManager)
647 getSystemService(Context.INPUT_METHOD_SERVICE);
648 WindowManager.LayoutParams lp = getWindow().getAttributes();
649 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
650 android.os.Handler()) {
651 protected void onReceiveResult(int resultCode, Bundle resultData) {
652 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
653 }
654 });
655 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
656 }
657 }
658 */
659
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 private boolean acceptFilter() {
661 final InputMethodManager inputManager = (InputMethodManager)
662 getSystemService(Context.INPUT_METHOD_SERVICE);
663 return !inputManager.isFullscreenMode();
664 }
665
666 @Override
667 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700668 final int uniChar = event.getUnicodeChar();
669 final boolean handled = super.onKeyDown(keyCode, event);
670 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
671 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800672 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
673 keyCode, event);
674 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700675 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700676 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700677 // showSearchDialog()
678 // If there are multiple keystrokes before the search dialog takes focus,
679 // onSearchRequested() will be called for every keystroke,
680 // but it is idempotent, so it's fine.
681 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800682 }
683 }
684
Joe Onorato8a9625e2010-01-28 15:55:35 -0800685 // Eat the long press event so the keyboard doesn't come up.
686 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
687 return true;
688 }
689
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800690 return handled;
691 }
692
Karl Rosaen138a0412009-04-23 19:00:21 -0700693 private String getTypedText() {
694 return mDefaultKeySsb.toString();
695 }
696
697 private void clearTypedText() {
698 mDefaultKeySsb.clear();
699 mDefaultKeySsb.clearSpans();
700 Selection.setSelection(mDefaultKeySsb, 0);
701 }
702
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800703 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700704 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
705 * State
706 */
707 private static State intToState(int stateOrdinal) {
708 State state = State.WORKSPACE;
709 final State[] stateValues = State.values();
710 for (int i = 0; i < stateValues.length; i++) {
711 if (stateValues[i].ordinal() == stateOrdinal) {
712 state = stateValues[i];
713 break;
714 }
715 }
716 return state;
717 }
718
719 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800720 * Restores the previous state, if it exists.
721 *
722 * @param savedState The previous state.
723 */
724 private void restoreState(Bundle savedState) {
725 if (savedState == null) {
726 return;
727 }
728
Michael Jurka883f55b2010-10-21 15:47:14 -0700729 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700730 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800731 showAllApps(false);
732 }
733
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800734 final int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
735 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700736 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 }
738
Winson Chung3d503fb2011-07-13 17:25:49 -0700739 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
740 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700741
Winson Chung3d503fb2011-07-13 17:25:49 -0700742 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
743 mPendingAddInfo.container = pendingAddContainer;
744 mPendingAddInfo.screen = pendingAddScreen;
745 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
746 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800747 mRestoring = true;
748 }
749
750 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
751 if (renameFolder) {
752 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700753 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800754 mRestoring = true;
755 }
Winson Chunga12a2502010-12-20 14:41:35 -0800756
Winson Chung785d2eb2011-04-14 16:08:02 -0700757
758 // Restore the AppsCustomize tab
759 if (mAppsCustomizeTabHost != null) {
760 String curTab = savedState.getString("apps_customize_currentTab");
761 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700762 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700763 mAppsCustomizeContent.setContentType(
764 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
765 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700766 mAppsCustomizeContent.loadAssociatedPages(
767 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700768 }
769
Winson Chung5afbf7b2011-07-25 11:53:08 -0700770 int currentIndex = savedState.getInt("apps_customize_currentIndex");
771 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700772 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 }
774
775 /**
776 * Finds all the views we need and configure them properly.
777 */
778 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700779 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400780
Winson Chung4c98d922011-05-31 16:50:48 -0700781 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
782 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800783 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
784 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800785
Winson Chung4c98d922011-05-31 16:50:48 -0700786 // Setup the drag layer
787 mDragLayer.setup(this, dragController);
788
Winson Chung3d503fb2011-07-13 17:25:49 -0700789 // Setup the hotseat
790 mHotseat = (Hotseat) findViewById(R.id.hotseat);
791 if (mHotseat != null) {
792 mHotseat.setup(this);
793 }
794
Winson Chung4c98d922011-05-31 16:50:48 -0700795 // Setup the workspace
796 mWorkspace.setHapticFeedbackEnabled(false);
797 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700798 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700799 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700800
Winson Chungf0ea4d32011-06-06 14:27:16 -0700801 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700802 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700803
Winson Chungf0ea4d32011-06-06 14:27:16 -0700804 // Setup AppsCustomize
805 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
806 findViewById(R.id.apps_customize_pane);
807 mAppsCustomizeContent = (AppsCustomizePagedView)
808 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
809 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400810
Michael Jurka5130e402011-10-13 04:55:35 -0700811 // Get the all apps button
812 mAllAppsButton = findViewById(R.id.all_apps_button);
813 if (mAllAppsButton != null) {
814 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
815 @Override
816 public boolean onTouch(View v, MotionEvent event) {
817 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
818 onTouchDownAllAppsButton(v);
819 }
820 return false;
821 }
822 });
823 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700824 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700825 dragController.setDragScoller(mWorkspace);
826 dragController.setScrollView(mDragLayer);
827 dragController.setMoveTarget(mWorkspace);
828 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700829 if (mSearchDropTargetBar != null) {
830 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800831 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800832 }
833
834 /**
835 * Creates a view representing a shortcut.
836 *
837 * @param info The data structure describing the shortcut.
838 *
839 * @return A View inflated from R.layout.application.
840 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800841 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800842 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700843 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800844 }
845
846 /**
847 * Creates a view representing a shortcut inflated from the specified resource.
848 *
849 * @param layoutResId The id of the XML layout used to create the shortcut.
850 * @param parent The group the shortcut belongs to.
851 * @param info The data structure describing the shortcut.
852 *
853 * @return A View inflated from layoutResId.
854 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800855 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800856 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
857 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 return favorite;
860 }
861
862 /**
863 * Add an application shortcut to the workspace.
864 *
865 * @param data The intent describing the application.
866 * @param cellInfo The position on screen where to create the shortcut.
867 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700868 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700869 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700870 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700871
Adam Cohenfbba09b2011-07-18 21:43:05 -0700872 // First we check if we already know the exact location where we want to add this item.
873 if (cellX >= 0 && cellY >= 0) {
874 cellXY[0] = cellX;
875 cellXY[1] = cellY;
876 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700877 showOutOfSpaceMessage();
878 return;
879 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800881 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800882
Romain Guy73b979d2009-06-09 12:57:21 -0700883 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800884 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800886 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700887 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700888 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800889 } else {
890 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800891 }
892 }
Romain Guycbb89e42009-06-08 15:52:54 -0700893
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 /**
895 * Add a shortcut to the workspace.
896 *
897 * @param data The intent describing the shortcut.
898 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700900 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
901 int cellY) {
902 int[] cellXY = mTmpAddItemCellCoordinates;
903 int[] touchXY = mPendingAddInfo.dropPos;
904 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700905
Michael Jurkad3ef3062010-11-23 16:23:58 -0800906 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700907
Adam Cohen558baaf2011-08-15 15:22:57 -0700908 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800909 if (info == null) {
910 return;
911 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700912 final View view = createShortcut(info);
913
Adam Cohenfbba09b2011-07-18 21:43:05 -0700914 // First we check if we already know the exact location where we want to add this item.
915 if (cellX >= 0 && cellY >= 0) {
916 cellXY[0] = cellX;
917 cellXY[1] = cellY;
918 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700919
920 // If appropriate, either create a folder or add to an existing folder
921 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY,
922 true, null,null)) {
923 return;
924 }
925 DragObject dragObject = new DragObject();
926 dragObject.dragInfo = info;
927 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, dragObject, true)) {
928 return;
929 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700930 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800931 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700932 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800933 foundCellSpan = (result != null);
934 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700935 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800936 }
937
938 if (!foundCellSpan) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700939 showOutOfSpaceMessage();
940 return;
941 }
942
Adam Cohen558baaf2011-08-15 15:22:57 -0700943 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800944
945 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700946 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
947 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 }
949 }
950
Adam Cohenf814aa02011-09-01 13:48:05 -0700951 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight, int[] spanXY) {
952 if (spanXY == null) {
953 spanXY = new int[2];
954 }
955
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800956 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700957 // We want to account for the extra amount of padding that we are adding to the widget
958 // to ensure that it gets the full amount of space that it has requested
959 int requiredWidth = minWidth + padding.left + padding.right;
960 int requiredHeight = minHeight + padding.top + padding.bottom;
961 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
962 }
963
964 int[] getSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
965 return getSpanForWidget(info.provider, info.minWidth, info.minHeight, spanXY);
966 }
967
Adam Cohencbf47e32011-09-16 17:32:37 -0700968 int[] getMinResizeSpanForWidget(AppWidgetProviderInfo info, int[] spanXY) {
969 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight, spanXY);
970 }
971
Adam Cohenf814aa02011-09-01 13:48:05 -0700972 int[] getSpanForWidget(PendingAddWidgetInfo info, int[] spanXY) {
973 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight, spanXY);
974 }
975
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700977 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 *
Romain Guy5bbc91b2010-08-18 11:38:46 -0700979 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700980 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 */
Adam Cohened66b2b2012-01-23 17:28:51 -0800982 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
983 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
984 if (appWidgetInfo == null) {
985 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
986 }
Romain Guycbb89e42009-06-08 15:52:54 -0700987
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700988 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -0700989 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -0700990
Adam Cohenf814aa02011-09-01 13:48:05 -0700991 int[] spanXY = getSpanForWidget(appWidgetInfo, null);
Romain Guycbb89e42009-06-08 15:52:54 -0700992
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -0700994 // We have saved the position to which the widget was dragged-- this really only matters
995 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -0700996 int[] cellXY = mTmpAddItemCellCoordinates;
997 int[] touchXY = mPendingAddInfo.dropPos;
Michael Jurka0280c3b2010-09-17 15:00:07 -0700998 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -0700999 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1000 cellXY[0] = mPendingAddInfo.cellX;
1001 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001002 foundCellSpan = true;
1003 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001004 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001005 int[] result = layout.findNearestVacantArea(
Michael Jurka0280c3b2010-09-17 15:00:07 -07001006 touchXY[0], touchXY[1], spanXY[0], spanXY[1], cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001007 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001008 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001009 foundCellSpan = layout.findCellForSpan(cellXY, spanXY[0], spanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001010 }
1011
Michael Jurka0280c3b2010-09-17 15:00:07 -07001012 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001013 if (appWidgetId != -1) {
1014 // Deleting an app widget ID is a void call but writes to disk before returning
1015 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001016 new Thread("deleteAppWidgetId") {
1017 public void run() {
1018 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1019 }
1020 }.start();
1021 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001022 showOutOfSpaceMessage();
Romain Guy18042c82009-11-06 11:44:55 -08001023 return;
1024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001026 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -07001027 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001028 launcherInfo.spanX = spanXY[0];
1029 launcherInfo.spanY = spanXY[1];
Romain Guycbb89e42009-06-08 15:52:54 -07001030
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001032 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001033
1034 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001035 if (hostView == null) {
1036 // Perform actual inflation because we're live
1037 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1038 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1039 } else {
1040 // The AppWidgetHostView has already been inflated and instantiated
1041 launcherInfo.hostView = hostView;
1042 }
Romain Guycbb89e42009-06-08 15:52:54 -07001043
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001044 launcherInfo.hostView.setTag(launcherInfo);
Winson Chung3d503fb2011-07-13 17:25:49 -07001045 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001046 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001047
1048 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001050 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001051 }
Romain Guycbb89e42009-06-08 15:52:54 -07001052
Adam Cohended9f8d2010-11-03 13:25:16 -07001053 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1054 @Override
1055 public void onReceive(Context context, Intent intent) {
1056 final String action = intent.getAction();
1057 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1058 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001059 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001060 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001061
Winson Chungc100e8e2011-08-09 16:02:43 -07001062 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001063 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001064 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1065 mAppsCustomizeTabHost.reset();
1066 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001067 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001068 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1069 mUserPresent = true;
1070 updateRunning();
1071 }
1072 }
1073 };
1074
1075 @Override
1076 public void onAttachedToWindow() {
1077 super.onAttachedToWindow();
1078
1079 // Listen for broadcasts related to user-presence
1080 final IntentFilter filter = new IntentFilter();
1081 filter.addAction(Intent.ACTION_SCREEN_OFF);
1082 filter.addAction(Intent.ACTION_USER_PRESENT);
1083 registerReceiver(mReceiver, filter);
1084
Adam Cohend113e0c2010-11-11 10:48:05 -08001085 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001086 mVisible = true;
1087 }
1088
1089 @Override
1090 public void onDetachedFromWindow() {
1091 super.onDetachedFromWindow();
1092 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001093 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001094
Adam Cohend113e0c2010-11-11 10:48:05 -08001095 if (mAttached) {
1096 unregisterReceiver(mReceiver);
1097 mAttached = false;
1098 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001099 updateRunning();
1100 }
1101
1102 public void onWindowVisibilityChanged(int visibility) {
1103 mVisible = visibility == View.VISIBLE;
1104 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001105 // The following code used to be in onResume, but it turns out onResume is called when
1106 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1107 // is a more appropriate event to handle
1108 if (mVisible) {
1109 mAppsCustomizeTabHost.onWindowVisible();
1110 if (!mWorkspaceLoading) {
1111 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001112 // We want to let Launcher draw itself at least once before we force it to build
1113 // layers on all the workspace pages, so that transitioning to Launcher from other
1114 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1115 // a true draw so we wait until the second preDraw call to be safe
1116 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
1117 boolean mFirstTime = true;
1118 public boolean onPreDraw() {
1119 if (mFirstTime) {
1120 mFirstTime = false;
1121 } else {
1122 //workspace.post(mBuildLayersRunnable);
1123 observer.removeOnPreDrawListener(this);
1124 }
1125 return true;
1126 }
1127 });
1128 }
1129 clearTypedText();
1130 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001131 }
1132
1133 private void sendAdvanceMessage(long delay) {
1134 mHandler.removeMessages(ADVANCE_MSG);
1135 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1136 mHandler.sendMessageDelayed(msg, delay);
1137 mAutoAdvanceSentTime = System.currentTimeMillis();
1138 }
1139
1140 private void updateRunning() {
1141 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1142 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1143 mAutoAdvanceRunning = autoAdvanceRunning;
1144 if (autoAdvanceRunning) {
1145 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1146 sendAdvanceMessage(delay);
1147 } else {
1148 if (!mWidgetsToAdvance.isEmpty()) {
1149 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1150 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1151 }
1152 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001153 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001154 }
1155 }
1156 }
1157
1158 private final Handler mHandler = new Handler() {
1159 @Override
1160 public void handleMessage(Message msg) {
1161 if (msg.what == ADVANCE_MSG) {
1162 int i = 0;
1163 for (View key: mWidgetsToAdvance.keySet()) {
1164 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1165 final int delay = mAdvanceStagger * i;
1166 if (v instanceof Advanceable) {
1167 postDelayed(new Runnable() {
1168 public void run() {
1169 ((Advanceable) v).advance();
1170 }
1171 }, delay);
1172 }
1173 i++;
1174 }
1175 sendAdvanceMessage(mAdvanceInterval);
1176 }
1177 }
1178 };
1179
1180 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001181 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001182 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1183 if (v instanceof Advanceable) {
1184 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001185 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001186 updateRunning();
1187 }
1188 }
1189
1190 void removeWidgetToAutoAdvance(View hostView) {
1191 if (mWidgetsToAdvance.containsKey(hostView)) {
1192 mWidgetsToAdvance.remove(hostView);
1193 updateRunning();
1194 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001195 }
1196
Joe Onorato9c1289c2009-08-17 11:03:03 -04001197 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001198 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001199 launcherInfo.hostView = null;
1200 }
1201
Adam Cohended9f8d2010-11-03 13:25:16 -07001202 void showOutOfSpaceMessage() {
1203 Toast.makeText(this, getString(R.string.out_of_space), Toast.LENGTH_SHORT).show();
1204 }
1205
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001206 public LauncherAppWidgetHost getAppWidgetHost() {
1207 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001208 }
Romain Guycbb89e42009-06-08 15:52:54 -07001209
Winson Chunga9abd0e2010-10-27 17:18:37 -07001210 public LauncherModel getModel() {
1211 return mModel;
1212 }
1213
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001214 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001215 getWindow().closeAllPanels();
1216
Winson Chung87acb482011-08-23 17:28:05 -07001217 /**
1218 * We should remove this code when we remove all the dialog code.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001219 try {
1220 dismissDialog(DIALOG_CREATE_SHORTCUT);
1221 // Unlock the workspace if the dialog was showing
1222 } catch (Exception e) {
1223 // An exception is thrown if the dialog is not visible, which is fine
1224 }
1225
1226 try {
1227 dismissDialog(DIALOG_RENAME_FOLDER);
1228 // Unlock the workspace if the dialog was showing
1229 } catch (Exception e) {
1230 // An exception is thrown if the dialog is not visible, which is fine
1231 }
Winson Chung87acb482011-08-23 17:28:05 -07001232 */
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001233
1234 // Whatever we were doing is hereby canceled.
1235 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001236 }
1237
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 @Override
1239 protected void onNewIntent(Intent intent) {
1240 super.onNewIntent(intent);
1241
1242 // Close the menu
1243 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001244 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001245 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001246
Joe Onorato14f122b2009-11-19 14:06:36 -08001247 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1248 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001249
Adam Cohenac56cff2011-09-28 20:45:37 -07001250 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001251 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001252 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001253 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1254 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001255 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001256 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001257
1258 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001259 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001260 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001261
Joe Onorato3a8820b2009-11-10 15:06:42 -08001262 final View v = getWindow().peekDecorView();
1263 if (v != null && v.getWindowToken() != null) {
1264 InputMethodManager imm = (InputMethodManager)getSystemService(
1265 INPUT_METHOD_SERVICE);
1266 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001268
Michael Jurka35aa14d2011-07-07 17:01:08 -07001269 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001270 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001271 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001272 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 }
1274 }
1275
1276 @Override
1277 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1278 // Do not call super here
1279 mSavedInstanceState = savedInstanceState;
1280 }
1281
1282 @Override
1283 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001284 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001285 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001286
Michael Jurka883f55b2010-10-21 15:47:14 -07001287 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001288 // We close any open folder since it will not be re-opened, and we need to make sure
1289 // this state is reflected.
1290 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291
Winson Chung3d503fb2011-07-13 17:25:49 -07001292 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1293 mWaitingForResult) {
1294 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1295 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1296 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1297 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001298 }
1299
1300 if (mFolderInfo != null && mWaitingForResult) {
1301 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1302 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1303 }
Michael Jurka946ad472010-07-09 18:05:18 -07001304
Winson Chung785d2eb2011-04-14 16:08:02 -07001305 // Save the current AppsCustomize tab
1306 if (mAppsCustomizeTabHost != null) {
1307 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1308 if (currentTabTag != null) {
1309 outState.putString("apps_customize_currentTab", currentTabTag);
1310 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001311 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1312 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001313 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314 }
1315
1316 @Override
1317 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001319
Winson Chunge7a03942011-08-05 15:05:12 -07001320 // Remove all pending runnables
1321 mHandler.removeMessages(ADVANCE_MSG);
1322 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001323 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001324
Winson Chungcd2b0142011-06-08 16:02:26 -07001325 // Stop callbacks from LauncherModel
1326 LauncherApplication app = ((LauncherApplication) getApplication());
1327 mModel.stopLoader();
1328 app.setLauncher(null);
1329
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001331 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001332 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001333 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001334 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001335 mAppWidgetHost = null;
1336
1337 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338
1339 TextKeyListener.getInstance().release();
1340
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341
Winson Chung3d503fb2011-07-13 17:25:49 -07001342 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001343
1344 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001345 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001346
1347 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1348 mWorkspace.removeAllViews();
1349 mWorkspace = null;
1350 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001351
1352 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 }
1354
Adam Cohena9cf38f2011-05-02 15:36:58 -07001355 public DragController getDragController() {
1356 return mDragController;
1357 }
1358
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 @Override
1360 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001361 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001362 super.startActivityForResult(intent, requestCode);
1363 }
1364
Winson Chung70d72102011-08-12 11:24:35 -07001365 /**
1366 * Indicates that we want global search for this activity by setting the globalSearch
1367 * argument for {@link #startSearch} to true.
1368 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001370 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001372
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001373 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001374
Karl Rosaen138a0412009-04-23 19:00:21 -07001375 if (initialQuery == null) {
1376 // Use any text typed in the launcher as the initial query
1377 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001378 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 if (appSearchData == null) {
1380 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001381 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001383 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001384
Karl Rosaen138a0412009-04-23 19:00:21 -07001385 final SearchManager searchManager =
1386 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001387 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001388 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
1390
Winson Chung70d72102011-08-12 11:24:35 -07001391 @Override
1392 public boolean onCreateOptionsMenu(Menu menu) {
1393 if (isWorkspaceLocked()) {
1394 return false;
1395 }
1396
1397 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001398
1399 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1400 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1401 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001402 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1403 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1404 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001405 String helpUrl = getString(R.string.help_url);
1406 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001407 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1408 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1409
Winson Chung70d72102011-08-12 11:24:35 -07001410 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1411 .setIcon(android.R.drawable.ic_menu_gallery)
1412 .setAlphabeticShortcut('W');
1413 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1414 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001415 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001416 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001417 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1418 .setIcon(android.R.drawable.ic_menu_preferences)
1419 .setIntent(settings)
1420 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001421 if (!helpUrl.isEmpty()) {
1422 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1423 .setIcon(android.R.drawable.ic_menu_help)
1424 .setIntent(help)
1425 .setAlphabeticShortcut('H');
1426 }
Winson Chung70d72102011-08-12 11:24:35 -07001427 return true;
1428 }
1429
1430 @Override
1431 public boolean onPrepareOptionsMenu(Menu menu) {
1432 super.onPrepareOptionsMenu(menu);
1433
1434 if (mAppsCustomizeTabHost.isTransitioning()) {
1435 return false;
1436 }
1437 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1438 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1439
1440 return true;
1441 }
1442
1443 @Override
1444 public boolean onOptionsItemSelected(MenuItem item) {
1445 switch (item.getItemId()) {
1446 case MENU_WALLPAPER_SETTINGS:
1447 startWallpaper();
1448 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001449 }
1450
1451 return super.onOptionsItemSelected(item);
1452 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001454 @Override
1455 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001456 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001457 // Use a custom animation for launching search
1458 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001459 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001460 }
1461
Joe Onorato9c1289c2009-08-17 11:03:03 -04001462 public boolean isWorkspaceLocked() {
1463 return mWorkspaceLoading || mWaitingForResult;
1464 }
1465
Michael Jurka0280c3b2010-09-17 15:00:07 -07001466 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001467 mPendingAddInfo.container = ItemInfo.NO_ID;
1468 mPendingAddInfo.screen = -1;
1469 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1470 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
1471 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001472 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001473
Michael Jurkaaf442092010-06-10 17:01:57 -07001474 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001475 // TODO: catch bad widget exception when sent
1476 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001477 // TODO: Is this log message meaningful?
1478 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001479 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001480 }
1481
Adam Cohened66b2b2012-01-23 17:28:51 -08001482 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1483 final AppWidgetProviderInfo appWidget = info.info;
1484 Runnable configurationActivity = null;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001485 if (appWidget.configure != null) {
1486 // Launch over to configure widget, if needed
1487 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1488 intent.setComponent(appWidget.configure);
1489 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001490 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001491 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001492 intent.putExtra(InstallWidgetReceiver.
1493 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001494
1495 final String mimeType = info.mimeType;
1496 final ClipData clipData = (ClipData) info.configurationData;
1497 final ClipDescription clipDesc = clipData.getDescription();
1498 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1499 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001500 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001501 final CharSequence stringData = item.getText();
1502 final Uri uriData = item.getUri();
1503 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001504 final String key = InstallWidgetReceiver.
1505 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001506 if (uriData != null) {
1507 intent.putExtra(key, uriData);
1508 } else if (intentData != null) {
1509 intent.putExtra(key, intentData);
1510 } else if (stringData != null) {
1511 intent.putExtra(key, stringData);
1512 }
1513 break;
1514 }
1515 }
1516 }
Winson Chung55cef262010-10-28 14:14:18 -07001517 }
Romain Guy8e633c52010-03-04 12:51:36 -08001518 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Adam Cohened66b2b2012-01-23 17:28:51 -08001519 mWidgetBeingConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001521 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001522 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001523 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001524 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001525 }
1526 }
Romain Guycbb89e42009-06-08 15:52:54 -07001527
Adam Cohenfbba09b2011-07-18 21:43:05 -07001528 /**
1529 * Process a shortcut drop.
1530 *
1531 * @param componentName The name of the component
1532 * @param screen The screen where it should be added
1533 * @param cell The cell it should be added to, optional
1534 * @param position The location on the screen where it was dropped, optional
1535 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001536 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1537 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001538 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001539 mPendingAddInfo.container = container;
1540 mPendingAddInfo.screen = screen;
1541 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001542
1543 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001544 mPendingAddInfo.cellX = cell[0];
1545 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001546 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001547
1548 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1549 createShortcutIntent.setComponent(componentName);
1550 processShortcut(createShortcutIntent);
1551 }
1552
Adam Cohenfbba09b2011-07-18 21:43:05 -07001553 /**
1554 * Process a widget drop.
1555 *
1556 * @param info The PendingAppWidgetInfo of the widget being added.
1557 * @param screen The screen where it should be added
1558 * @param cell The cell it should be added to, optional
1559 * @param position The location on the screen where it was dropped, optional
1560 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001561 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
1562 int[] cell, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001563 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001564 mPendingAddInfo.container = info.container = container;
1565 mPendingAddInfo.screen = info.screen = screen;
1566 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001567 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001568 mPendingAddInfo.cellX = cell[0];
1569 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001570 }
1571
Adam Cohened66b2b2012-01-23 17:28:51 -08001572 AppWidgetHostView hostView = info.boundWidget;
1573 int appWidgetId;
1574 if (hostView != null) {
1575 appWidgetId = hostView.getAppWidgetId();
1576 } else {
1577 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1578 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1579 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001580 addAppWidgetImpl(appWidgetId, info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001581
Adam Cohenfbba09b2011-07-18 21:43:05 -07001582 }
1583
Joe Onoratodeb98af2010-02-19 14:59:39 -08001584 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001585 // Handle case where user selected "Applications"
1586 String applicationName = getResources().getString(R.string.group_applications);
1587 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001588
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001589 if (applicationName != null && applicationName.equals(shortcutName)) {
1590 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1591 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001592
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001593 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1594 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001595 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001596 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001597 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001598 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 }
1601
Winson Chung24ab2f12010-09-16 14:10:47 -07001602 void processWallpaper(Intent intent) {
1603 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1604 }
1605
Winson Chung3d503fb2011-07-13 17:25:49 -07001606 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1607 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001608 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 folderInfo.title = getText(R.string.folder_name);
1610
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001612 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1613 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001614 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615
1616 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001617 FolderIcon newFolder =
1618 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1619 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1620 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001621 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 }
Romain Guycbb89e42009-06-08 15:52:54 -07001623
Joe Onorato9c1289c2009-08-17 11:03:03 -04001624 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001625 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001626 }
1627
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001629 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001631 Intent chooser = Intent.createChooser(pickWallpaper,
1632 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001633 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1634 // Removed in Eclair MR1
1635// WallpaperManager wm = (WallpaperManager)
1636// getSystemService(Context.WALLPAPER_SERVICE);
1637// WallpaperInfo wi = wm.getWallpaperInfo();
1638// if (wi != null && wi.getSettingsActivity() != null) {
1639// LabeledIntent li = new LabeledIntent(getPackageName(),
1640// R.string.configure_wallpaper, 0);
1641// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1642// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1643// }
Mike Clerona0618e42009-10-22 13:55:21 -07001644 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 }
1646
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001647 /**
1648 * Registers various content observers. The current implementation registers
1649 * only a favorites observer to keep track of the favorites applications.
1650 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001651 private void registerContentObservers() {
1652 ContentResolver resolver = getContentResolver();
1653 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1654 true, mWidgetObserver);
1655 }
1656
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 @Override
1658 public boolean dispatchKeyEvent(KeyEvent event) {
1659 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1660 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001661 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001662 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001663 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001664 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001665 dumpState();
1666 return true;
1667 }
1668 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001669 }
1670 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1671 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001672 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 return true;
1674 }
1675 }
1676
1677 return super.dispatchKeyEvent(event);
1678 }
1679
Joe Onorato88ec0992009-11-19 13:16:06 -08001680 @Override
1681 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001682 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001683 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001684 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001685 Folder openFolder = mWorkspace.getOpenFolder();
1686 if (openFolder.isEditingName()) {
1687 openFolder.dismissEditingName();
1688 } else {
1689 closeFolder();
1690 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001691 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001692 mWorkspace.exitWidgetResizeMode();
1693
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001694 // Back button is a no-op here, but give at least some feedback for the button press
1695 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001696 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001697 }
1698
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001700 * Re-listen when widgets are reset.
1701 */
1702 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001703 if (mAppWidgetHost != null) {
1704 mAppWidgetHost.startListening();
1705 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001706 }
1707
1708 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001709 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1710 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001711 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001712 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001713 if (mModel != null) {
1714 mModel.unbindWorkspaceItems();
1715 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001716 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001717
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001718 /**
1719 * Launches the intent referred by the clicked shortcut.
1720 *
1721 * @param v The view representing the clicked shortcut.
1722 */
1723 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001724 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1725 // view has detached (it's possible for this to happen if the view is removed mid touch).
1726 if (v.getWindowToken() == null) {
1727 return;
1728 }
1729
1730 if (mWorkspace.isSwitchingState()) {
1731 return;
1732 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001733
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001734 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001735 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001737 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001738 int[] pos = new int[2];
1739 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001740 intent.setSourceBounds(new Rect(pos[0], pos[1],
1741 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001742 boolean success = startActivitySafely(intent, tag);
1743
1744 if (success && v instanceof BubbleTextView) {
1745 mWaitingForResume = (BubbleTextView) v;
1746 mWaitingForResume.setStayPressed(true);
1747 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001749 if (v instanceof FolderIcon) {
1750 FolderIcon fi = (FolderIcon) v;
1751 handleFolderClick(fi);
1752 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001753 } else if (v == mAllAppsButton) {
1754 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001755 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001756 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001757 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001758 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 }
1760 }
1761
Michael Jurka0e260592010-06-30 17:07:39 -07001762 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001763 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001764 // clicking anywhere on the workspace causes the customization drawer to slide down
1765 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001766 return false;
1767 }
1768
Michael Jurkaaf442092010-06-10 17:01:57 -07001769 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001770 * Event handler for the search button
1771 *
1772 * @param v The view that was clicked.
1773 */
1774 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001775 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1776
Amith Yamasania135ba82011-08-09 17:42:01 -07001777 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001778 }
1779
1780 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001781 * Event handler for the voice button
1782 *
1783 * @param v The view that was clicked.
1784 */
1785 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001786 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001787
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001788 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001789 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001790 startActivity(intent);
1791 }
1792
1793 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001794 * Event handler for the "grid" button that appears on the home screen, which
1795 * enters all apps mode.
1796 *
1797 * @param v The view that was clicked.
1798 */
1799 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001800 showAllApps(true);
1801 }
1802
1803 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001804 // Provide the same haptic feedback that the system offers for virtual keys.
1805 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001806 }
1807
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001808 public void onClickAppMarketButton(View v) {
1809 if (mAppMarketIntent != null) {
1810 startActivitySafely(mAppMarketIntent, "app market");
1811 }
1812 }
1813
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001814 void startApplicationDetailsActivity(ComponentName componentName) {
1815 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001816 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1817 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001818 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001819 startActivity(intent);
1820 }
1821
Patrick Dubroy5539af72010-09-07 15:22:01 -07001822 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1823 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1824 // System applications cannot be installed. For now, show a toast explaining that.
1825 // We may give them the option of disabling apps this way.
1826 int messageId = R.string.uninstall_system_app_text;
1827 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1828 } else {
1829 String packageName = appInfo.componentName.getPackageName();
1830 String className = appInfo.componentName.getClassName();
1831 Intent intent = new Intent(
1832 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001833 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1834 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001835 startActivity(intent);
1836 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001837 }
1838
Michael Jurkaddd62e92011-02-16 17:49:14 -08001839 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1841 try {
1842 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001843 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001844 } catch (ActivityNotFoundException e) {
1845 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001846 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 } catch (SecurityException e) {
1848 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001849 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001851 "or use the exported attribute for this activity. "
1852 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001854 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001856
Romain Guy8e633c52010-03-04 12:51:36 -08001857 void startActivityForResultSafely(Intent intent, int requestCode) {
1858 try {
1859 startActivityForResult(intent, requestCode);
1860 } catch (ActivityNotFoundException e) {
1861 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1862 } catch (SecurityException e) {
1863 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1864 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1865 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1866 "or use the exported attribute for this activity.", e);
1867 }
1868 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001869
Adam Cohena9cf38f2011-05-02 15:36:58 -07001870 private void handleFolderClick(FolderIcon folderIcon) {
1871 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001872 Folder openFolder = mWorkspace.getFolderForTag(info);
1873
1874 // If the folder info reports that the associated folder is open, then verify that
1875 // it is actually opened. There have been a few instances where this gets out of sync.
1876 if (info.opened && openFolder == null) {
1877 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1878 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1879 info.opened = false;
1880 }
1881
Adam Cohena9cf38f2011-05-02 15:36:58 -07001882 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 // Close any open folder
1884 closeFolder();
1885 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001886 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 } else {
1888 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 int folderScreen;
1890 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001891 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001892 // .. and close it
1893 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001894 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 // Close any folder open on the current screen
1896 closeFolder();
1897 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001898 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 }
1900 }
1901 }
1902 }
1903
Adam Cohen2801caf2011-05-13 20:57:39 -07001904 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001905 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001906 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1907 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1908 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1909
Adam Cohenc51934b2011-07-26 21:07:43 -07001910 FolderInfo info = (FolderInfo) fi.getTag();
1911 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1912 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001913 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1914 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001915 }
1916
Adam Cohen2801caf2011-05-13 20:57:39 -07001917 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1918 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1919 oa.start();
1920 }
1921
1922 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001923 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001924 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1925 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1926 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1927
Adam Cohenc51934b2011-07-26 21:07:43 -07001928 FolderInfo info = (FolderInfo) fi.getTag();
1929 CellLayout cl = null;
1930 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1931 cl = (CellLayout) fi.getParent().getParent();
1932 }
1933
1934 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001935 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1936 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001937 oa.addListener(new AnimatorListenerAdapter() {
1938 @Override
1939 public void onAnimationEnd(Animator animation) {
1940 if (layout != null) {
1941 layout.clearFolderLeaveBehind();
1942 }
1943 }
1944 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001945 oa.start();
1946 }
1947
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001949 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 * is animated relative to the specified View. If the View is null, no animation
1951 * is played.
1952 *
1953 * @param folderInfo The FolderInfo describing the folder to open.
1954 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001955 public void openFolder(FolderIcon folderIcon) {
1956 Folder folder = folderIcon.mFolder;
1957 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958
Adam Cohen2801caf2011-05-13 20:57:39 -07001959 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001960 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961
Adam Cohen4554ee12011-08-03 16:13:21 -07001962 // Just verify that the folder hasn't already been added to the DragLayer.
1963 // There was a one-off crash where the folder had a parent already.
1964 if (folder.getParent() == null) {
1965 mDragLayer.addView(folder);
1966 mDragController.addDropTarget((DropTarget) folder);
1967 } else {
1968 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
1969 folder.getParent() + ").");
1970 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07001971 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07001972 }
1973
1974 public void closeFolder() {
1975 Folder folder = mWorkspace.getOpenFolder();
1976 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07001977 if (folder.isEditingName()) {
1978 folder.dismissEditingName();
1979 }
Adam Cohen4554ee12011-08-03 16:13:21 -07001980 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07001981
1982 // Dismiss the folder cling
1983 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07001984 }
1985 }
1986
1987 void closeFolder(Folder folder) {
1988 folder.getInfo().opened = false;
1989
1990 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
1991 if (parent != null) {
1992 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
1993 shrinkAndFadeInFolderIcon(fi);
1994 }
1995 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 }
1997
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 public boolean onLongClick(View v) {
Patrick Dubroye708c522011-03-01 16:03:43 -08001999 if (mState != State.WORKSPACE) {
2000 return false;
2001 }
2002
Joe Onorato9c1289c2009-08-17 11:03:03 -04002003 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 return false;
2005 }
2006
2007 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002008 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
2010
Michael Jurka0280c3b2010-09-17 15:00:07 -07002011 resetAddInfo();
2012 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002014 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 return true;
2016 }
2017
Winson Chung3d503fb2011-07-13 17:25:49 -07002018 // The hotseat touch handling does not go through Workspace, and we always allow long press
2019 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002020 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002021 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2022 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002023 if (itemUnderLongClick == null) {
2024 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002025 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2026 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002027 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002029 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002031 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 }
2033 }
2034 }
2035 return true;
2036 }
2037
Winson Chung3d503fb2011-07-13 17:25:49 -07002038 boolean isHotseatLayout(View layout) {
2039 return mHotseat != null && layout != null &&
2040 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2041 }
2042 Hotseat getHotseat() {
2043 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002044 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002045 SearchDropTargetBar getSearchBar() {
2046 return mSearchDropTargetBar;
2047 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002048
Winson Chung3d503fb2011-07-13 17:25:49 -07002049 /**
2050 * Returns the CellLayout of the specified container at the specified screen.
2051 */
2052 CellLayout getCellLayout(long container, int screen) {
2053 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2054 if (mHotseat != null) {
2055 return mHotseat.getLayout();
2056 } else {
2057 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002058 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002059 } else {
2060 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002061 }
2062 }
2063
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 Workspace getWorkspace() {
2065 return mWorkspace;
2066 }
2067
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 @Override
2069 protected Dialog onCreateDialog(int id) {
2070 switch (id) {
2071 case DIALOG_CREATE_SHORTCUT:
2072 return new CreateShortcut().createDialog();
2073 case DIALOG_RENAME_FOLDER:
2074 return new RenameFolder().createDialog();
2075 }
2076
2077 return super.onCreateDialog(id);
2078 }
2079
2080 @Override
2081 protected void onPrepareDialog(int id, Dialog dialog) {
2082 switch (id) {
2083 case DIALOG_CREATE_SHORTCUT:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 break;
2085 case DIALOG_RENAME_FOLDER:
Romain Guy7b4ef332009-07-14 13:58:08 -07002086 if (mFolderInfo != null) {
2087 EditText input = (EditText) dialog.findViewById(R.id.folder_name);
2088 final CharSequence text = mFolderInfo.title;
2089 input.setText(text);
2090 input.setSelection(0, text.length());
2091 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 break;
2093 }
2094 }
2095
2096 void showRenameDialog(FolderInfo info) {
2097 mFolderInfo = info;
2098 mWaitingForResult = true;
2099 showDialog(DIALOG_RENAME_FOLDER);
2100 }
2101
Adam Cohenfbba09b2011-07-18 21:43:05 -07002102 private void showAddDialog() {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002103 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002104 mPendingAddInfo.container = LauncherSettings.Favorites.CONTAINER_DESKTOP;
2105 mPendingAddInfo.screen = mWorkspace.getCurrentPage();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 mWaitingForResult = true;
2107 showDialog(DIALOG_CREATE_SHORTCUT);
2108 }
2109
2110 private class RenameFolder {
2111 private EditText mInput;
2112
2113 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 final View layout = View.inflate(Launcher.this, R.layout.rename_folder, null);
2115 mInput = (EditText) layout.findViewById(R.id.folder_name);
2116
2117 AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this);
2118 builder.setIcon(0);
2119 builder.setTitle(getString(R.string.rename_folder_title));
2120 builder.setCancelable(true);
2121 builder.setOnCancelListener(new Dialog.OnCancelListener() {
2122 public void onCancel(DialogInterface dialog) {
2123 cleanup();
2124 }
2125 });
2126 builder.setNegativeButton(getString(R.string.cancel_action),
2127 new Dialog.OnClickListener() {
2128 public void onClick(DialogInterface dialog, int which) {
2129 cleanup();
2130 }
2131 }
2132 );
2133 builder.setPositiveButton(getString(R.string.rename_action),
2134 new Dialog.OnClickListener() {
2135 public void onClick(DialogInterface dialog, int which) {
2136 changeFolderName();
2137 }
2138 }
2139 );
2140 builder.setView(layout);
Romain Guy7b4ef332009-07-14 13:58:08 -07002141
2142 final AlertDialog dialog = builder.create();
2143 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
2144 public void onShow(DialogInterface dialog) {
Joe Onorato7018d8e2010-04-13 20:25:47 -07002145 mWaitingForResult = true;
Joe Onoratod753b422009-11-08 13:31:11 -05002146 mInput.requestFocus();
2147 InputMethodManager inputManager = (InputMethodManager)
2148 getSystemService(Context.INPUT_METHOD_SERVICE);
2149 inputManager.showSoftInput(mInput, 0);
Romain Guy7b4ef332009-07-14 13:58:08 -07002150 }
2151 });
2152
2153 return dialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 }
2155
2156 private void changeFolderName() {
2157 final String name = mInput.getText().toString();
2158 if (!TextUtils.isEmpty(name)) {
2159 // Make sure we have the right folder info
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002160 mFolderInfo = sFolders.get(mFolderInfo.id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 mFolderInfo.title = name;
2162 LauncherModel.updateItemInDatabase(Launcher.this, mFolderInfo);
2163
Joe Onorato9c1289c2009-08-17 11:03:03 -04002164 if (mWorkspaceLoading) {
Joe Onorato7c312c12009-08-13 21:36:53 -07002165 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002166 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 } else {
2168 final FolderIcon folderIcon = (FolderIcon)
2169 mWorkspace.getViewForTag(mFolderInfo);
2170 if (folderIcon != null) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002171 // TODO: At some point we'll probably want some version of setting
2172 // the text for a folder icon.
2173 //folderIcon.setText(name);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002174 getWorkspace().requestLayout();
2175 } else {
Joe Onorato7c312c12009-08-13 21:36:53 -07002176 lockAllApps();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002177 mWorkspaceLoading = true;
2178 mModel.startLoader(Launcher.this, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179 }
2180 }
2181 }
2182 cleanup();
2183 }
2184
2185 private void cleanup() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002186 dismissDialog(DIALOG_RENAME_FOLDER);
2187 mWaitingForResult = false;
2188 mFolderInfo = null;
2189 }
2190 }
2191
Daniel Sandler843e8602010-06-07 14:59:01 -04002192 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2193 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002194 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002195 }
2196
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002197 // AllAppsView.Watcher
2198 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002199 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002200 mWorkspace.setVisibility(View.GONE);
2201 }
2202 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002203
2204 /**
2205 * Helper method for the cameraZoomIn/cameraZoomOut animations
2206 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002207 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002208 * @param scaleFactor The scale factor used for the zoom
2209 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002210 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002211 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002212 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002213 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002214
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002215 void updateWallpaperVisibility(boolean visible) {
2216 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2217 int curflags = getWindow().getAttributes().flags
2218 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2219 if (wpflags != curflags) {
2220 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2221 }
2222 }
2223
Michael Jurkabed61d22012-02-14 22:51:29 -08002224 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2225 if (v instanceof LauncherTransitionable) {
2226 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2227 }
Winson Chung70442722012-02-10 15:43:22 -08002228
2229 // Update the workspace transition step as well
2230 dispatchOnLauncherTransitionStep(v, 0f);
2231 }
2232
2233 private void dispatchOnLauncherTransitionStep(View v, float t) {
2234 if (v instanceof LauncherTransitionable) {
2235 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2236 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002237 }
2238
2239 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2240 if (v instanceof LauncherTransitionable) {
2241 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2242 }
Winson Chung70442722012-02-10 15:43:22 -08002243
2244 // Update the workspace transition step as well
2245 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002246 }
2247
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002248 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002249 * Things to test when changing the following seven functions.
2250 * - Home from workspace
2251 * - from center screen
2252 * - from other screens
2253 * - Home from all apps
2254 * - from center screen
2255 * - from other screens
2256 * - Back from all apps
2257 * - from center screen
2258 * - from other screens
2259 * - Launch app from workspace and quit
2260 * - with back
2261 * - with home
2262 * - Launch app from all apps and quit
2263 * - with back
2264 * - with home
2265 * - Go to a screen that's not the default, then all
2266 * apps, and launch and app, and go back
2267 * - with back
2268 * -with home
2269 * - On workspace, long press power and go back
2270 * - with back
2271 * - with home
2272 * - On all apps, long press power and go back
2273 * - with back
2274 * - with home
2275 * - On workspace, power off
2276 * - On all apps, power off
2277 * - Launch an app and turn off the screen while in that app
2278 * - Go back with home key
2279 * - Go back with back key TODO: make this not go to workspace
2280 * - From all apps
2281 * - From workspace
2282 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2283 * - From all apps
2284 * - From the center workspace
2285 * - From another workspace
2286 */
2287
2288 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002289 * Zoom the camera out from the workspace to reveal 'toView'.
2290 * Assumes that the view to show is anchored at either the very top or very bottom
2291 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002292 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002293 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002294 if (mStateAnimation != null) {
2295 mStateAnimation.cancel();
2296 mStateAnimation = null;
2297 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002298 final Resources res = getResources();
Winson Chung7d7541e2011-09-16 20:14:36 -07002299 final Launcher instance = this;
Adam Cohenf16e5712011-01-13 13:31:45 -08002300
Winson Chungf0ea4d32011-06-06 14:27:16 -07002301 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2302 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2303 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002304 final View fromView = mWorkspace;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002305 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002306 final int startDelay =
2307 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002308
Michael Jurkab3e22d92011-10-31 15:58:33 -07002309 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002310
Michael Jurkad74c9842011-07-10 12:44:21 -07002311 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002312 Animator workspaceAnim =
2313 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002314
2315 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002316 toView.setScaleX(scale);
2317 toView.setScaleY(scale);
2318 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2319 scaleAnim.
2320 scaleX(1f).scaleY(1f).
2321 setDuration(duration).
2322 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002323
Winson Chungf0ea4d32011-06-06 14:27:16 -07002324 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002325 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002326 final ObjectAnimator alphaAnim = ObjectAnimator
2327 .ofFloat(toView, "alpha", 0f, 1f)
2328 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002329 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002330 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2331 @Override
2332 public void onAnimationUpdate(ValueAnimator animation) {
2333 float t = (Float) animation.getAnimatedValue();
2334 dispatchOnLauncherTransitionStep(fromView, t);
2335 dispatchOnLauncherTransitionStep(toView, t);
2336 }
2337 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002338
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002339 // toView should appear right at the end of the workspace shrink
2340 // animation
2341 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002342 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002343 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002344
2345 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002346 boolean animationCancelled = false;
2347
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002348 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002349 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002350 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002351 // Prepare the position
2352 toView.setTranslationX(0.0f);
2353 toView.setTranslationY(0.0f);
2354 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002355 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002356 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002357 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002358 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002359 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2360 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002361
2362 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2363 // Hide the workspace scrollbar
2364 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002365 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002366 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002367 if (!animationCancelled) {
2368 updateWallpaperVisibility(false);
2369 }
2370 }
2371
Adam Cohencff6af82011-09-13 14:51:53 -07002372 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002373 public void onAnimationCancel(Animator animation) {
2374 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002375 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002376 });
2377
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002378 if (workspaceAnim != null) {
2379 mStateAnimation.play(workspaceAnim);
2380 }
Michael Jurka1899a362011-11-03 13:50:45 -07002381
2382 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002383 final ViewTreeObserver observer;
2384
Michael Jurkabed61d22012-02-14 22:51:29 -08002385 dispatchOnLauncherTransitionStart(fromView, animated, false);
2386 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002387
2388 // If any of the objects being animated haven't been measured/laid out
2389 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002390 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002391 (mWorkspace.getMeasuredWidth() == 0) ||
2392 (toView.getMeasuredWidth() == 0)) {
2393 observer = mWorkspace.getViewTreeObserver();
2394 delayAnim = true;
2395 } else {
2396 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002397 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002398
2399 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002400 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002401 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2402 public void onGlobalLayout() {
2403 mWorkspace.post(new Runnable() {
2404 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002405 // Check that mStateAnimation hasn't changed while
2406 // we waited for a layout pass
2407 if (mStateAnimation == stateAnimation) {
2408 // Need to update pivots for zoom if layout changed
2409 setPivotsForZoom(toView, scale);
2410 mStateAnimation.start();
2411 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002412 }
2413 });
2414 observer.removeGlobalOnLayoutListener(this);
2415 }
2416 };
2417 observer.addOnGlobalLayoutListener(delayedStart);
2418 } else {
2419 setPivotsForZoom(toView, scale);
Michael Jurka1899a362011-11-03 13:50:45 -07002420 mStateAnimation.start();
2421 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002422 } else {
2423 toView.setTranslationX(0.0f);
2424 toView.setTranslationY(0.0f);
2425 toView.setScaleX(1.0f);
2426 toView.setScaleY(1.0f);
2427 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002428 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002429
Michael Jurkabed61d22012-02-14 22:51:29 -08002430 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2431 // Hide the workspace scrollbar
2432 mWorkspace.hideScrollingIndicator(true);
2433 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002434 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002435 dispatchOnLauncherTransitionStart(fromView, animated, false);
2436 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2437 dispatchOnLauncherTransitionStart(toView, animated, false);
2438 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002439 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002440 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002441 }
2442
2443 /**
2444 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002445 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002446 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002447 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002448 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2449 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002450
Michael Jurkab3e22d92011-10-31 15:58:33 -07002451 if (mStateAnimation != null) {
2452 mStateAnimation.cancel();
2453 mStateAnimation = null;
2454 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002455 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002456
Winson Chungf0ea4d32011-06-06 14:27:16 -07002457 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002458 final int fadeOutDuration =
2459 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002460 final float scaleFactor = (float)
2461 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2462 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002463 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002464 Animator workspaceAnim = null;
2465
2466 if (toState == State.WORKSPACE) {
2467 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2468 workspaceAnim = mWorkspace.getChangeStateAnimation(
2469 Workspace.State.NORMAL, animated, stagger);
2470 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2471 workspaceAnim = mWorkspace.getChangeStateAnimation(
2472 Workspace.State.SPRING_LOADED, animated);
2473 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002474
Michael Jurkab3e22d92011-10-31 15:58:33 -07002475 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002476 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002477 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002478 if (animated) {
Michael Jurka742574b2011-02-02 23:51:01 -08002479 final float oldScaleX = fromView.getScaleX();
2480 final float oldScaleY = fromView.getScaleY();
2481
Michael Jurka159b4cc2012-01-17 03:00:35 -08002482 final LauncherViewPropertyAnimator scaleAnim =
2483 new LauncherViewPropertyAnimator(fromView);
2484 scaleAnim.
2485 scaleX(scaleFactor).scaleY(scaleFactor).
2486 setDuration(duration).
2487 setInterpolator(new Workspace.ZoomInInterpolator());
2488
2489 final ObjectAnimator alphaAnim = ObjectAnimator
2490 .ofFloat(fromView, "alpha", 1f, 0f)
2491 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002492 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002493 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2494 @Override
2495 public void onAnimationUpdate(ValueAnimator animation) {
2496 float t = 1f - (Float) animation.getAnimatedValue();
2497 dispatchOnLauncherTransitionStep(fromView, t);
2498 dispatchOnLauncherTransitionStep(toView, t);
2499 }
2500 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002501
Michael Jurkabed61d22012-02-14 22:51:29 -08002502 mStateAnimation = new AnimatorSet();
2503
2504 dispatchOnLauncherTransitionStart(fromView, animated, true);
2505 dispatchOnLauncherTransitionStart(toView, animated, true);
2506
2507 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002508 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002509 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002510 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002511 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002512 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2513 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurka430e8a52011-08-08 15:52:14 -07002514 mWorkspace.hideScrollingIndicator(false);
Adam Cohened66b2b2012-01-23 17:28:51 -08002515 if (onCompleteRunnable != null) {
2516 onCompleteRunnable.run();
2517 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002518 }
2519 });
2520
Winson Chungf0ea4d32011-06-06 14:27:16 -07002521 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002522 if (workspaceAnim != null) {
2523 mStateAnimation.play(workspaceAnim);
2524 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002525 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002526 } else {
2527 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002528 dispatchOnLauncherTransitionStart(fromView, animated, true);
2529 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2530 dispatchOnLauncherTransitionStart(toView, animated, true);
2531 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002532 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002533 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002534 }
2535
Michael Jurkae326f182011-11-21 14:05:46 -08002536 @Override
2537 public void onTrimMemory(int level) {
2538 super.onTrimMemory(level);
2539 if (level == ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
2540 mAppsCustomizeTabHost.onTrimMemory();
2541 }
2542 }
2543
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002544 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002545 showWorkspace(animated, null);
2546 }
2547
2548 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002549 if (mState != State.WORKSPACE) {
2550 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002551 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002552
2553 // Show the search bar and hotseat
2554 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002555 // We only need to animate in the dock divider if we're going from spring loaded mode
2556 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002557
2558 // Set focus to the AppsCustomize button
2559 if (mAllAppsButton != null) {
2560 mAllAppsButton.requestFocus();
2561 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002562 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002563
Michael Jurkab737ee62011-11-15 15:57:22 -08002564 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002565
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002566 // Change the state *after* we've called all the transition code
2567 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002568
Winson Chung5fb63472011-02-02 17:03:37 -08002569 // Resume the auto-advance of widgets
2570 mUserPresent = true;
2571 updateRunning();
2572
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002573 // send an accessibility event to announce the context change
2574 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002575 }
2576
Michael Jurkab3e22d92011-10-31 15:58:33 -07002577 void showAllApps(boolean animated) {
2578 if (mState != State.WORKSPACE) return;
2579
2580 showAppsCustomizeHelper(animated, false);
2581 mAppsCustomizeTabHost.requestFocus();
2582
2583 // Hide the search bar and hotseat
2584 mSearchDropTargetBar.hideSearchBar(animated);
2585
2586 // Change the state *after* we've called all the transition code
2587 mState = State.APPS_CUSTOMIZE;
2588
2589 // Pause the auto-advance of widgets until we are out of AllApps
2590 mUserPresent = false;
2591 updateRunning();
2592 closeFolder();
2593
2594 // Send an accessibility event to announce the context change
2595 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2596 }
2597
Adam Cohen7777d962011-08-18 18:58:38 -07002598 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002599 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002600 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002601 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002602 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002603 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002604 }
Adam Cohen7777d962011-08-18 18:58:38 -07002605
Adam Cohened66b2b2012-01-23 17:28:51 -08002606 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2607 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002608 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2609
Winson Chunge7a03942011-08-05 15:05:12 -07002610 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002611 @Override
2612 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002613 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002614 // Before we show workspace, hide all apps again because
2615 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2616 // clean up our state transition functions
2617 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002618 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002619 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002620 } else {
2621 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002622 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002623 }
2624 }, (extendedDelay ?
2625 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2626 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2627 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002628
Michael Jurkad3ef3062010-11-23 16:23:58 -08002629 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002630 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002631 final boolean animated = true;
2632 final boolean springLoaded = true;
2633 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002634 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002635 }
2636 // Otherwise, we are not in spring loaded mode, so don't do anything.
2637 }
2638
Michael Jurkab737ee62011-11-15 15:57:22 -08002639 void hideDockDivider() {
2640 if (mQsbDivider != null && mDockDivider != null) {
2641 mQsbDivider.setVisibility(View.INVISIBLE);
2642 mDockDivider.setVisibility(View.INVISIBLE);
2643 }
2644 }
2645
2646 void showDockDivider(boolean animated) {
2647 if (mQsbDivider != null && mDockDivider != null) {
2648 mQsbDivider.setVisibility(View.VISIBLE);
2649 mDockDivider.setVisibility(View.VISIBLE);
2650 if (mDividerAnimator != null) {
2651 mDividerAnimator.cancel();
2652 mQsbDivider.setAlpha(1f);
2653 mDockDivider.setAlpha(1f);
2654 mDividerAnimator = null;
2655 }
2656 if (animated) {
2657 mDividerAnimator = new AnimatorSet();
2658 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2659 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2660 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2661 mDividerAnimator.start();
2662 }
2663 }
2664 }
2665
Michael Jurkab3e22d92011-10-31 15:58:33 -07002666 void lockAllApps() {
2667 // TODO
2668 }
2669
2670 void unlockAllApps() {
2671 // TODO
2672 }
2673
Adam Cohenfc53cd22011-07-20 15:45:11 -07002674 public boolean isAllAppsCustomizeOpen() {
2675 return mState == State.APPS_CUSTOMIZE;
2676 }
2677
Winson Chungf0ea4d32011-06-06 14:27:16 -07002678 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002679 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002680 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002681 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002682 if (!LauncherApplication.isScreenLarge()) {
2683 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002684 if (mHotseat.getAlpha() != 1f) {
2685 int duration = mSearchDropTargetBar.getTransitionInDuration();
2686 mHotseat.animate().alpha(1f).setDuration(duration);
2687 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002688 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002689 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002690 }
2691 }
2692 }
2693
2694 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002695 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002696 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002697 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002698 if (!LauncherApplication.isScreenLarge()) {
2699 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002700 if (mHotseat.getAlpha() != 0f) {
2701 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2702 mHotseat.animate().alpha(0f).setDuration(duration);
2703 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002704 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002705 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002706 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002707 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002708 }
2709
Patrick Dubroy5f445422011-02-18 14:35:21 -08002710 /**
2711 * Add an item from all apps or customize onto the given workspace screen.
2712 * If layout is null, add to the current screen.
2713 */
2714 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002715 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
2716 showOutOfSpaceMessage();
Michael Jurka213d9632010-07-28 11:29:25 -07002717 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002718 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002719
Winson Chungdff8ebb2011-09-08 17:25:31 -07002720 /** Maps the current orientation to an index for referencing orientation correct global icons */
2721 private int getCurrentOrientationIndexForGlobalIcons() {
2722 // default - 0, landscape - 1
2723 switch (getResources().getConfiguration().orientation) {
2724 case Configuration.ORIENTATION_LANDSCAPE:
2725 return 1;
2726 default:
2727 return 0;
2728 }
2729 }
2730
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002731 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002732 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002733 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002734 // Look for the toolbar icon specified in the activity meta-data
2735 Bundle metaData = packageManager.getActivityInfo(
2736 activityName, PackageManager.GET_META_DATA).metaData;
2737 if (metaData != null) {
2738 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2739 if (iconResId != 0) {
2740 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002741 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002742 }
2743 }
2744 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002745 // This can happen if the activity defines an invalid drawable
2746 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2747 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002748 } catch (Resources.NotFoundException nfe) {
2749 // This can happen if the activity defines an invalid drawable
2750 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2751 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002752 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002753 return null;
2754 }
2755
2756 // if successful in getting icon, return it; otherwise, set button to use default drawable
2757 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2758 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002759 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002760 Resources r = getResources();
2761 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2762 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002763
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002764 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002765 // If we were unable to find the icon via the meta-data, use a generic one
2766 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002767 toolbarIcon = r.getDrawable(fallbackDrawableId);
2768 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002769 if (button != null) {
2770 button.setCompoundDrawables(toolbarIcon, null, null, null);
2771 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002772 return null;
2773 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002774 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002775 if (button != null) {
2776 button.setCompoundDrawables(toolbarIcon, null, null, null);
2777 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002778 return toolbarIcon.getConstantState();
2779 }
2780 }
2781
2782 // if successful in getting icon, return it; otherwise, set button to use default drawable
2783 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2784 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2785 ImageView button = (ImageView) findViewById(buttonId);
2786 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2787
Michael Jurka19e0fc52011-07-22 18:00:21 -07002788 if (button != null) {
2789 // If we were unable to find the icon via the meta-data, use a
2790 // generic one
2791 if (toolbarIcon == null) {
2792 button.setImageResource(fallbackDrawableId);
2793 } else {
2794 button.setImageDrawable(toolbarIcon);
2795 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002796 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002797
2798 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2799
Michael Jurka0423dcf2010-10-05 14:56:18 -07002800 }
2801
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002802 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2803 TextView button = (TextView) findViewById(buttonId);
2804 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2805 }
2806
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002807 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002808 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002809 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002810 }
2811
Winson Chungbb185bd2011-11-21 12:31:42 -08002812 private void invalidatePressedFocusedStates(View container, View button) {
2813 if (container instanceof HolographicLinearLayout) {
2814 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2815 layout.invalidatePressedFocusedStates();
2816 } else if (button instanceof HolographicImageView) {
2817 HolographicImageView view = (HolographicImageView) button;
2818 view.invalidatePressedFocusedStates();
2819 }
2820 }
2821
Winson Chungc51db6a2011-10-05 11:44:49 -07002822 private boolean updateGlobalSearchIcon() {
2823 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002824 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2825 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002826 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002827 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002828
Winson Chung1cad91e2011-05-25 17:41:01 -07002829 final SearchManager searchManager =
2830 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2831 ComponentName activityName = searchManager.getGlobalSearchActivity();
2832 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002833 int coi = getCurrentOrientationIndexForGlobalIcons();
2834 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002835 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002836 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002837 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2838 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002839 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002840 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002841 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002842 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002843 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002844 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2845 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2846 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002847 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002848 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002849 }
2850 }
2851
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002852 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002853 final View searchButtonContainer = findViewById(R.id.search_button_container);
2854 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002855 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002856 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002857 }
2858
Winson Chungc51db6a2011-10-05 11:44:49 -07002859 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002860 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002861 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002862 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002863
Winson Chungc51db6a2011-10-05 11:44:49 -07002864 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002865 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2866 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002867 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002868 int coi = getCurrentOrientationIndexForGlobalIcons();
2869 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002870 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002871 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002872 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002873 voiceButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002874 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002875 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002876 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002877 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002878 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002879 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002880 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002881 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002882 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002883
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002884 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002885 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2886 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002887 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002888 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002889 }
2890
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002891 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002892 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002893 */
2894 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002895 final View marketButton = findViewById(R.id.market_button);
2896 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2897 // Find the app market activity by resolving an intent.
2898 // (If multiple app markets are installed, it will return the ResolverActivity.)
2899 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002900 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002901 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002902 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002903 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002904 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002905 marketButton.setVisibility(View.VISIBLE);
2906 } else {
2907 // We should hide and disable the view so that we don't try and restore the visibility
2908 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2909 marketButton.setVisibility(View.GONE);
2910 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002911 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002912 }
2913
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002914 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002915 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002916 }
2917
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002918 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002919 * Displays the shortcut creation dialog and launches, if necessary, the
2920 * appropriate activity.
2921 */
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002922 private class CreateShortcut implements DialogInterface.OnClickListener,
Romain Guy7b4ef332009-07-14 13:58:08 -07002923 DialogInterface.OnCancelListener, DialogInterface.OnDismissListener,
2924 DialogInterface.OnShowListener {
2925
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002926 private AddAdapter mAdapter;
Romain Guy9ffb5432009-03-24 21:04:15 -07002927
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002928 Dialog createDialog() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002929 mAdapter = new AddAdapter(Launcher.this);
Romain Guycbb89e42009-06-08 15:52:54 -07002930
Winson Chung55b65502011-05-26 12:03:43 -07002931 final AlertDialog.Builder builder = new AlertDialog.Builder(Launcher.this,
2932 AlertDialog.THEME_HOLO_DARK);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002933 builder.setAdapter(mAdapter, this);
Romain Guycbb89e42009-06-08 15:52:54 -07002934
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002935 AlertDialog dialog = builder.create();
2936 dialog.setOnCancelListener(this);
Romain Guycbb89e42009-06-08 15:52:54 -07002937 dialog.setOnDismissListener(this);
Romain Guy7b4ef332009-07-14 13:58:08 -07002938 dialog.setOnShowListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002939
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002940 return dialog;
2941 }
2942
2943 public void onCancel(DialogInterface dialog) {
2944 mWaitingForResult = false;
2945 cleanup();
2946 }
2947
Romain Guycbb89e42009-06-08 15:52:54 -07002948 public void onDismiss(DialogInterface dialog) {
Winson Chung55b65502011-05-26 12:03:43 -07002949 mWaitingForResult = false;
2950 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002951 }
2952
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002953 private void cleanup() {
Joe Onoratocc19a532009-11-19 14:19:17 -08002954 try {
2955 dismissDialog(DIALOG_CREATE_SHORTCUT);
2956 } catch (Exception e) {
2957 // An exception is thrown if the dialog is not visible, which is fine
2958 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 }
2960
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002961 /**
2962 * Handle the action clicked in the "Add to home" dialog.
2963 */
2964 public void onClick(DialogInterface dialog, int which) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002965 cleanup();
Romain Guycbb89e42009-06-08 15:52:54 -07002966
Winson Chung55b65502011-05-26 12:03:43 -07002967 AddAdapter.ListItem item = (AddAdapter.ListItem) mAdapter.getItem(which);
2968 switch (item.actionTag) {
Winson Chung55b65502011-05-26 12:03:43 -07002969 case AddAdapter.ITEM_APPLICATION: {
2970 if (mAppsCustomizeTabHost != null) {
2971 mAppsCustomizeTabHost.selectAppsTab();
2972 }
2973 showAllApps(true);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002974 break;
2975 }
Winson Chung55b65502011-05-26 12:03:43 -07002976 case AddAdapter.ITEM_APPWIDGET: {
2977 if (mAppsCustomizeTabHost != null) {
2978 mAppsCustomizeTabHost.selectWidgetsTab();
2979 }
2980 showAllApps(true);
2981 break;
2982 }
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002983 case AddAdapter.ITEM_WALLPAPER: {
2984 startWallpaper();
2985 break;
2986 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002987 }
2988 }
Romain Guy7b4ef332009-07-14 13:58:08 -07002989
2990 public void onShow(DialogInterface dialog) {
Winson Chungaafa03c2010-06-11 17:34:16 -07002991 mWaitingForResult = true;
Romain Guy7b4ef332009-07-14 13:58:08 -07002992 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002993 }
2994
2995 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002996 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002997 */
2998 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2999 @Override
3000 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003001 closeSystemDialogs();
3002 }
3003 }
3004
3005 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003006 * Receives notifications whenever the appwidgets are reset.
3007 */
3008 private class AppWidgetResetObserver extends ContentObserver {
3009 public AppWidgetResetObserver() {
3010 super(new Handler());
3011 }
3012
3013 @Override
3014 public void onChange(boolean selfChange) {
3015 onAppWidgetReset();
3016 }
3017 }
3018
3019 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003020 * If the activity is currently paused, signal that we need to re-run the loader
3021 * in onResume.
3022 *
3023 * This needs to be called from incoming places where resources might have been loaded
3024 * while we are paused. That is becaues the Configuration might be wrong
3025 * when we're not running, and if it comes back to what it was when we
3026 * were paused, we are not restarted.
3027 *
3028 * Implementation of the method from LauncherModel.Callbacks.
3029 *
3030 * @return true if we are currently paused. The caller might be able to
3031 * skip some work in that case since we will come back again.
3032 */
3033 public boolean setLoadOnResume() {
3034 if (mPaused) {
3035 Log.i(TAG, "setLoadOnResume");
3036 mOnResumeNeedsLoad = true;
3037 return true;
3038 } else {
3039 return false;
3040 }
3041 }
3042
3043 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003044 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003045 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003046 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003047 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003048 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003049 } else {
3050 return SCREEN_COUNT / 2;
3051 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003052 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003053
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003054
Joe Onorato9c1289c2009-08-17 11:03:03 -04003055 /**
3056 * Refreshes the shortcuts shown on the workspace.
3057 *
3058 * Implementation of the method from LauncherModel.Callbacks.
3059 */
3060 public void startBinding() {
3061 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003062
3063 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003064 int count = workspace.getChildCount();
3065 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003066 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003067 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3068 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003069 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003070 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003071 if (mHotseat != null) {
3072 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003073 }
3074 }
3075
3076 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003077 * Bind the items start-end from the list.
3078 *
3079 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003080 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003081 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003082 setLoadOnResume();
3083
Joe Onorato9c1289c2009-08-17 11:03:03 -04003084 final Workspace workspace = mWorkspace;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003085 for (int i=start; i<end; i++) {
3086 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003087
3088 // Short circuit if we are loading dock items for a configuration which has no dock
3089 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3090 mHotseat == null) {
3091 continue;
3092 }
3093
Joe Onorato9c1289c2009-08-17 11:03:03 -04003094 switch (item.itemType) {
3095 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3096 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -07003097 View shortcut = createShortcut((ShortcutInfo)item);
3098 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3099 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003100 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003101 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003102 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003103 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003104 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003105 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3106 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003107 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003108 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003109 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003110 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003111 }
3112
Joe Onorato9c1289c2009-08-17 11:03:03 -04003113 /**
3114 * Implementation of the method from LauncherModel.Callbacks.
3115 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003116 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003117 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003118 sFolders.clear();
3119 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003121
3122 /**
3123 * Add the views for a widget to the workspace.
3124 *
3125 * Implementation of the method from LauncherModel.Callbacks.
3126 */
3127 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003128 setLoadOnResume();
3129
Daniel Sandler843e8602010-06-07 14:59:01 -04003130 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3131 if (DEBUG_WIDGETS) {
3132 Log.d(TAG, "bindAppWidget: " + item);
3133 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003134 final Workspace workspace = mWorkspace;
3135
3136 final int appWidgetId = item.appWidgetId;
3137 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003138 if (DEBUG_WIDGETS) {
3139 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3140 }
3141
Joe Onorato9c1289c2009-08-17 11:03:03 -04003142 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3143
Joe Onorato9c1289c2009-08-17 11:03:03 -04003144 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3145 item.hostView.setTag(item);
3146
Winson Chung3d503fb2011-07-13 17:25:49 -07003147 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003148 item.cellY, item.spanX, item.spanY, false);
3149
Adam Cohended9f8d2010-11-03 13:25:16 -07003150 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3151
Joe Onorato9c1289c2009-08-17 11:03:03 -04003152 workspace.requestLayout();
3153
Daniel Sandler843e8602010-06-07 14:59:01 -04003154 if (DEBUG_WIDGETS) {
3155 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3156 + (SystemClock.uptimeMillis()-start) + "ms");
3157 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003158 }
3159
3160 /**
3161 * Callback saying that there aren't any more items to bind.
3162 *
3163 * Implementation of the method from LauncherModel.Callbacks.
3164 */
3165 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003166 setLoadOnResume();
3167
Joe Onorato9c1289c2009-08-17 11:03:03 -04003168 if (mSavedState != null) {
3169 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003170 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003171 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003172 mSavedState = null;
3173 }
3174
3175 if (mSavedInstanceState != null) {
3176 super.onRestoreInstanceState(mSavedInstanceState);
3177 mSavedInstanceState = null;
3178 }
3179
Joe Onorato9c1289c2009-08-17 11:03:03 -04003180 mWorkspaceLoading = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003181
3182 // If we received the result of any pending adds while the loader was running (e.g. the
3183 // widget configuration forced an orientation change), process them now.
3184 for (int i = 0; i < sPendingAddList.size(); i++) {
3185 completeAdd(sPendingAddList.get(i));
3186 }
3187 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003188
Winson Chungc51db6a2011-10-05 11:44:49 -07003189 // Update the market app icon as necessary (the other icons will be managed in response to
3190 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003191 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003192
3193 mWorkspace.post(mBuildLayersRunnable);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003194 }
3195
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003196 @Override
3197 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003198 boolean searchVisible = updateGlobalSearchIcon();
3199 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3200 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003201 }
3202
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003203 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003204 * Add the icons for all apps.
3205 *
3206 * Implementation of the method from LauncherModel.Callbacks.
3207 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003208 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3209 // Remove the progress bar entirely; we could also make it GONE
3210 // but better to remove it since we know it's not going to be used
3211 View progressBar = mAppsCustomizeTabHost.
3212 findViewById(R.id.apps_customize_progress_bar);
3213 if (progressBar != null) {
3214 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003215 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003216 // We just post the call to setApps so the user sees the progress bar
3217 // disappear-- otherwise, it just looks like the progress bar froze
3218 // which doesn't look great
3219 mAppsCustomizeTabHost.post(new Runnable() {
3220 public void run() {
3221 if (mAppsCustomizeContent != null) {
3222 mAppsCustomizeContent.setApps(apps);
3223 }
3224 }
3225 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003226 }
3227
3228 /**
3229 * A package was installed.
3230 *
3231 * Implementation of the method from LauncherModel.Callbacks.
3232 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003233 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003234 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003235 removeDialog(DIALOG_CREATE_SHORTCUT);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003236
Winson Chung785d2eb2011-04-14 16:08:02 -07003237 if (mAppsCustomizeContent != null) {
3238 mAppsCustomizeContent.addApps(apps);
3239 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003240 }
3241
3242 /**
3243 * A package was updated.
3244 *
3245 * Implementation of the method from LauncherModel.Callbacks.
3246 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003247 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003248 setLoadOnResume();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003249 removeDialog(DIALOG_CREATE_SHORTCUT);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003250 if (mWorkspace != null) {
3251 mWorkspace.updateShortcuts(apps);
3252 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003253
Winson Chung785d2eb2011-04-14 16:08:02 -07003254 if (mAppsCustomizeContent != null) {
3255 mAppsCustomizeContent.updateApps(apps);
3256 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003257 }
3258
3259 /**
3260 * A package was uninstalled.
3261 *
3262 * Implementation of the method from LauncherModel.Callbacks.
3263 */
Joe Onorato36115782010-06-17 13:28:48 -04003264 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003265 removeDialog(DIALOG_CREATE_SHORTCUT);
Joe Onorato36115782010-06-17 13:28:48 -04003266 if (permanent) {
3267 mWorkspace.removeItems(apps);
3268 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003269
Winson Chung785d2eb2011-04-14 16:08:02 -07003270 if (mAppsCustomizeContent != null) {
3271 mAppsCustomizeContent.removeApps(apps);
3272 }
Winson Chunga1820962011-10-03 16:31:06 -07003273
3274 // Notify the drag controller
3275 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003276 }
Joe Onoratobe386092009-11-17 17:32:16 -08003277
3278 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003279 * A number of packages were updated.
3280 */
3281 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003282 if (mAppsCustomizeContent != null) {
3283 mAppsCustomizeContent.onPackagesUpdated();
3284 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003285 }
3286
Winson Chung400438b2011-01-16 17:53:48 -08003287 private int mapConfigurationOriActivityInfoOri(int configOri) {
3288 final Display d = getWindowManager().getDefaultDisplay();
3289 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3290 switch (d.getRotation()) {
3291 case Surface.ROTATION_0:
3292 case Surface.ROTATION_180:
3293 // We are currently in the same basic orientation as the natural orientation
3294 naturalOri = configOri;
3295 break;
3296 case Surface.ROTATION_90:
3297 case Surface.ROTATION_270:
3298 // We are currently in the other basic orientation to the natural orientation
3299 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3300 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3301 break;
3302 }
3303
3304 int[] oriMap = {
3305 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3306 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3307 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3308 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3309 };
3310 // Since the map starts at portrait, we need to offset if this device's natural orientation
3311 // is landscape.
3312 int indexOffset = 0;
3313 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3314 indexOffset = 1;
3315 }
3316 return oriMap[(d.getRotation() + indexOffset) % 4];
3317 }
Adam Cohen446e9402011-09-15 18:21:21 -07003318
3319 public void lockScreenOrientationOnLargeUI() {
3320 if (LauncherApplication.isScreenLarge()) {
3321 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3322 .getConfiguration().orientation));
3323 }
Winson Chung400438b2011-01-16 17:53:48 -08003324 }
Adam Cohen446e9402011-09-15 18:21:21 -07003325 public void unlockScreenOrientationOnLargeUI() {
3326 if (LauncherApplication.isScreenLarge()) {
3327 mHandler.postDelayed(new Runnable() {
3328 public void run() {
3329 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3330 }
3331 }, mRestoreScreenOrientationDelay);
3332 }
Winson Chung400438b2011-01-16 17:53:48 -08003333 }
3334
Winson Chung82f55532011-08-09 14:14:23 -07003335 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003336 private static final String PREFS_KEY = "com.android.launcher2.prefs";
3337 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003338 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003339 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003340
Winson Chung7d7541e2011-09-16 20:14:36 -07003341 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003342 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003343 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3344 Cling cling = (Cling) findViewById(clingId);
3345 if (cling != null) {
3346 cling.init(this, positionData);
3347 cling.setVisibility(View.VISIBLE);
3348 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3349 if (animate) {
3350 cling.buildLayer();
3351 cling.setAlpha(0f);
3352 cling.animate()
3353 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003354 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003355 .setDuration(SHOW_CLING_DURATION)
3356 .setStartDelay(delay)
3357 .start();
3358 } else {
3359 cling.setAlpha(1f);
3360 }
3361 }
3362 return cling;
3363 }
3364 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003365 if (cling != null) {
3366 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003367 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003368 anim.addListener(new AnimatorListenerAdapter() {
3369 public void onAnimationEnd(Animator animation) {
3370 cling.setVisibility(View.GONE);
3371 cling.cleanup();
3372 SharedPreferences prefs =
3373 getSharedPreferences("com.android.launcher2.prefs", Context.MODE_PRIVATE);
3374 SharedPreferences.Editor editor = prefs.edit();
3375 editor.putBoolean(flag, true);
3376 editor.commit();
3377 };
3378 });
3379 anim.start();
3380 }
3381 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003382 private void removeCling(int id) {
3383 final View cling = findViewById(id);
3384 if (cling != null) {
3385 final ViewGroup parent = (ViewGroup) cling.getParent();
3386 parent.post(new Runnable() {
3387 @Override
3388 public void run() {
3389 parent.removeView(cling);
3390 }
3391 });
3392 }
3393 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003394 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003395 // Enable the clings only if they have not been dismissed before
3396 SharedPreferences prefs =
3397 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003398 if (isClingsEnabled() && !prefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003399 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003400 } else {
3401 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003402 }
3403 }
3404 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003405 // Enable the clings only if they have not been dismissed before
3406 SharedPreferences prefs =
3407 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003408 if (isClingsEnabled() && !prefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003409 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003410 } else {
3411 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003412 }
3413 }
3414 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003415 // Enable the clings only if they have not been dismissed before
3416 SharedPreferences prefs =
3417 getSharedPreferences(PREFS_KEY, Context.MODE_PRIVATE);
3418 Cling cling = null;
Winson Chung9d9d74f2011-09-19 11:49:12 -07003419 if (isClingsEnabled() && !prefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003420 cling = initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003421 } else {
3422 removeCling(R.id.folder_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003423 }
3424 return cling;
3425 }
3426 public boolean isFolderClingVisible() {
3427 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003428 if (cling != null) {
3429 return cling.getVisibility() == View.VISIBLE;
3430 }
3431 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003432 }
Winson Chung82f55532011-08-09 14:14:23 -07003433 public void dismissWorkspaceCling(View v) {
3434 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003435 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003436 }
3437 public void dismissAllAppsCling(View v) {
3438 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003439 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3440 }
3441 public void dismissFolderCling(View v) {
3442 Cling cling = (Cling) findViewById(R.id.folder_cling);
3443 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003444 }
3445
Winson Chung80baf5a2010-08-09 16:03:15 -07003446 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003447 * Prints out out state for debugging.
3448 */
3449 public void dumpState() {
3450 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003451 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003452 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3453 Log.d(TAG, "mRestoring=" + mRestoring);
3454 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3455 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003456 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003457 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003458
Winson Chung785d2eb2011-04-14 16:08:02 -07003459 if (mAppsCustomizeContent != null) {
3460 mAppsCustomizeContent.dumpState();
3461 }
Joe Onoratobe386092009-11-17 17:32:16 -08003462 Log.d(TAG, "END launcher2 dump state");
3463 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003464
3465 @Override
3466 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3467 super.dump(prefix, fd, writer, args);
3468 writer.println(" ");
3469 writer.println("Debug logs: ");
3470 for (int i = 0; i < sDumpLogs.size(); i++) {
3471 writer.println(" " + sDumpLogs.get(i));
3472 }
3473 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003474}
Michael Jurka2763be32011-02-24 11:19:57 -08003475
Michael Jurkaabded662011-03-04 12:06:57 -08003476interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003477 View getContent();
Michael Jurkabed61d22012-02-14 22:51:29 -08003478 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003479 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003480 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003481}