blob: 84a3b4f8c74bef4332dd2deeff46b28b0447a7ab [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;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080030import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080031import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070032import android.appwidget.AppWidgetManager;
33import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080034import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080035import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070036import android.content.ClipData;
37import android.content.ClipDescription;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070052import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040053import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070054import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080055import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020056import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080057import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070058import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070059import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040060import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080061import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070062import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080063import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080064import android.text.Selection;
65import android.text.SpannableStringBuilder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070067import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080068import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080069import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.view.KeyEvent;
71import android.view.LayoutInflater;
72import android.view.Menu;
73import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070074import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080075import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080076import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080077import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070078import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080079import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080080import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070081import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080082import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070083import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070084import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080085import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080086import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070087import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070088import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070089import android.widget.TextView;
90import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070091
Adam Cohendf2cc412011-04-27 16:56:57 -070092import com.android.common.Search;
93import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070094import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080095
Adam Cohenc0dcf592011-06-01 15:30:43 -070096import java.io.DataInputStream;
97import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070098import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.FileNotFoundException;
100import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700101import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700103import java.util.Collection;
104import java.util.Collections;
105import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.HashSet;
108import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800110/**
111 * Default launcher application.
112 */
Michael Jurka946ad472010-07-09 18:05:18 -0700113public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700114 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
115 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800116 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700117 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800118
Joe Onorato9c1289c2009-08-17 11:03:03 -0400119 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400120 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700121 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700122
Winson Chung70d72102011-08-12 11:24:35 -0700123 private static final int MENU_GROUP_WALLPAPER = 1;
124 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
125 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700126 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
127 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800128
129 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700130 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131 private static final int REQUEST_PICK_APPLICATION = 6;
132 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700133 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700134 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135
136 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
137
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800138 static final int SCREEN_COUNT = 5;
139 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Romain Guy98d01652009-06-30 16:21:04 -0700141 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800142 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
144 // Type: int
145 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700146 // Type: int
147 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700149 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
150 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
152 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700153 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700155 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: boolean
157 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
158 // Type: long
159 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
160
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700161 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
162
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700163 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700164 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700165 private State mState = State.WORKSPACE;
166 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800167 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700168
Joe Onorato9c1289c2009-08-17 11:03:03 -0400169 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700170 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
171 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700172 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700173 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800176 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177
Winson Chunga2413752012-04-03 14:22:34 -0700178 // How long to wait before the new-shortcut animation automatically pans the workspace
179 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
180
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800181 private final BroadcastReceiver mCloseSystemDialogsReceiver
182 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800183 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private LayoutInflater mInflater;
186
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800188 private View mQsbDivider;
189 private View mDockDivider;
190 private DragLayer mDragLayer;
191 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700192
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700193 private AppWidgetManager mAppWidgetManager;
194 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700195
Michael Jurkac9d95c52011-08-29 14:03:34 -0700196 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700197 private int[] mTmpAddItemCellCoordinates = new int[2];
198
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199 private FolderInfo mFolderInfo;
200
Winson Chung3d503fb2011-07-13 17:25:49 -0700201 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800202 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700203
Winson Chungc51db6a2011-10-05 11:44:49 -0700204 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700205 private AppsCustomizeTabHost mAppsCustomizeTabHost;
206 private AppsCustomizePagedView mAppsCustomizeContent;
207 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 private Bundle mSavedState;
210
211 private SpannableStringBuilder mDefaultKeySsb = null;
212
Joe Onorato9c1289c2009-08-17 11:03:03 -0400213 private boolean mWorkspaceLoading = true;
214
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800215 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800216 private boolean mRestoring;
217 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700218 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800219
220 private Bundle mSavedInstanceState;
221
Joe Onorato9c1289c2009-08-17 11:03:03 -0400222 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800223 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800224 private boolean mUserPresent = true;
225 private boolean mVisible = false;
226 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400227
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700228 private static LocaleConfiguration sLocaleConfiguration = null;
229
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700230 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700231
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700232 private Intent mAppMarketIntent = null;
233
Adam Cohended9f8d2010-11-03 13:25:16 -0700234 // Related to the auto-advancing of widgets
235 private final int ADVANCE_MSG = 1;
236 private final int mAdvanceInterval = 20000;
237 private final int mAdvanceStagger = 250;
238 private long mAutoAdvanceSentTime;
239 private long mAutoAdvanceTimeLeft = -1;
240 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
241 new HashMap<View, AppWidgetProviderInfo>();
242
Winson Chung400438b2011-01-16 17:53:48 -0800243 // Determines how long to wait after a rotation before restoring the screen orientation to
244 // match the sensor state.
245 private final int mRestoreScreenOrientationDelay = 500;
246
Michael Jurka4ef207b2010-11-29 17:05:45 -0800247 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700248 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
249 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
250 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800251
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700252 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohened66b2b2012-01-23 17:28:51 -0800253 PendingAddWidgetInfo mWidgetBeingConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700254
Winson Chung46353de2012-02-16 14:05:10 -0800255 // We only want to get the SharedPreferences once since it does an FS stat each time we get
256 // it from the context.
257 private SharedPreferences mSharedPrefs;
258
Winson Chungf0c6ae02012-03-21 16:10:31 -0700259 // Holds the page that we need to animate to, and the icon views that we need to animate up
260 // when we scroll to that page on resume.
261 private int mNewShortcutAnimatePage = -1;
262 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700263
Michael Jurkaddd62e92011-02-16 17:49:14 -0800264 private BubbleTextView mWaitingForResume;
265
Michael Jurkac1f5d262011-09-30 19:32:27 -0700266 private Runnable mBuildLayersRunnable = new Runnable() {
267 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700268 if (mWorkspace != null) {
269 mWorkspace.buildPageHardwareLayers();
270 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700271 }
272 };
273
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800274 private static ArrayList<PendingAddArguments> sPendingAddList
275 = new ArrayList<PendingAddArguments>();
276
277 private static class PendingAddArguments {
278 int requestCode;
279 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700280 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800281 int screen;
282 int cellX;
283 int cellY;
284 }
285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 @Override
287 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700288 if (DEBUG_STRICT_MODE) {
289 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
290 .detectDiskReads()
291 .detectDiskWrites()
292 .detectNetwork() // or .detectAll() for all detectable problems
293 .penaltyLog()
294 .build());
295 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
296 .detectLeakedSqlLiteObjects()
297 .detectLeakedClosableObjects()
298 .penaltyLog()
299 .penaltyDeath()
300 .build());
301 }
302
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800304 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700305 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
306 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800307 mModel = app.setLauncher(this);
308 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400309 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800310 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700311
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700312 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700313 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
314 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700315
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800316 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200317 android.os.Debug.startMethodTracing(
318 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800319 }
320
321 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800322 setContentView(R.layout.launcher);
323 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700324 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800325
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800326 registerContentObservers();
327
Joe Onorato7c312c12009-08-13 21:36:53 -0700328 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700329
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800330 mSavedState = savedInstanceState;
331 restoreState(mSavedState);
332
Winson Chunga12a2502010-12-20 14:41:35 -0800333 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700334 if (mAppsCustomizeContent != null) {
335 mAppsCustomizeContent.onPackagesUpdated();
336 }
Winson Chunga12a2502010-12-20 14:41:35 -0800337
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800338 if (PROFILE_STARTUP) {
339 android.os.Debug.stopMethodTracing();
340 }
341
342 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700343 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 }
345
Michael Jurkac57b7a82011-08-09 22:02:20 -0700346 if (!mModel.isAllAppsLoaded()) {
347 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
348 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
349 }
350
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 // For handling default keys
352 mDefaultKeySsb = new SpannableStringBuilder();
353 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800354
355 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
356 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800357
Winson Chungc51db6a2011-10-05 11:44:49 -0700358 boolean searchVisible = false;
359 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800360 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700361 int coi = getCurrentOrientationIndexForGlobalIcons();
362 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
363 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700364 updateAppMarketIcon();
365 searchVisible = updateGlobalSearchIcon();
366 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700367 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700368 if (sGlobalSearchIcon[coi] != null) {
369 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700370 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700371 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700372 if (sVoiceSearchIcon[coi] != null) {
373 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700374 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700375 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700376 if (sAppMarketIcon[coi] != null) {
377 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800378 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700379 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700380
Adam Cohen23a4d302011-12-01 17:26:44 -0800381 final String forceEnableRotation =
382 SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
383
Adam Cohen446e9402011-09-15 18:21:21 -0700384 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohen23a4d302011-12-01 17:26:44 -0800385 if (LauncherApplication.isScreenLarge() || "true".equalsIgnoreCase(forceEnableRotation)) {
Adam Cohen446e9402011-09-15 18:21:21 -0700386 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
387 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 }
Romain Guycbb89e42009-06-08 15:52:54 -0700389
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700391 if (sLocaleConfiguration == null) {
392 new AsyncTask<Void, Void, LocaleConfiguration>() {
393 @Override
394 protected LocaleConfiguration doInBackground(Void... unused) {
395 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
396 readConfiguration(Launcher.this, localeConfiguration);
397 return localeConfiguration;
398 }
399
400 @Override
401 protected void onPostExecute(LocaleConfiguration result) {
402 sLocaleConfiguration = result;
403 checkForLocaleChange(); // recursive, but now with a locale configuration
404 }
405 }.execute();
406 return;
407 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700408
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800409 final Configuration configuration = getResources().getConfiguration();
410
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700411 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800412 final String locale = configuration.locale.toString();
413
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700414 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 final int mcc = configuration.mcc;
416
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700417 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800418 final int mnc = configuration.mnc;
419
Romain Guy84f296c2009-11-04 15:00:44 -0800420 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800421
Romain Guy84f296c2009-11-04 15:00:44 -0800422 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700423 sLocaleConfiguration.locale = locale;
424 sLocaleConfiguration.mcc = mcc;
425 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700426
Joe Onorato0589f0f2010-02-08 13:44:00 -0800427 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700428
429 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
430 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700431 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700432 public void run() {
433 writeConfiguration(Launcher.this, localeConfiguration);
434 }
435 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700436 }
437 }
438
439 private static class LocaleConfiguration {
440 public String locale;
441 public int mcc = -1;
442 public int mnc = -1;
443 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700444
Romain Guy98d01652009-06-30 16:21:04 -0700445 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
446 DataInputStream in = null;
447 try {
448 in = new DataInputStream(context.openFileInput(PREFERENCES));
449 configuration.locale = in.readUTF();
450 configuration.mcc = in.readInt();
451 configuration.mnc = in.readInt();
452 } catch (FileNotFoundException e) {
453 // Ignore
454 } catch (IOException e) {
455 // Ignore
456 } finally {
457 if (in != null) {
458 try {
459 in.close();
460 } catch (IOException e) {
461 // Ignore
462 }
463 }
464 }
465 }
466
467 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
468 DataOutputStream out = null;
469 try {
470 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
471 out.writeUTF(configuration.locale);
472 out.writeInt(configuration.mcc);
473 out.writeInt(configuration.mnc);
474 out.flush();
475 } catch (FileNotFoundException e) {
476 // Ignore
477 } catch (IOException e) {
478 //noinspection ResultOfMethodCallIgnored
479 context.getFileStreamPath(PREFERENCES).delete();
480 } finally {
481 if (out != null) {
482 try {
483 out.close();
484 } catch (IOException e) {
485 // Ignore
486 }
487 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 }
489 }
490
Adam Cohen716b51e2011-06-30 12:09:54 -0700491 public DragLayer getDragLayer() {
492 return mDragLayer;
493 }
494
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800495 static int getScreen() {
496 synchronized (sLock) {
497 return sScreen;
498 }
499 }
500
501 static void setScreen(int screen) {
502 synchronized (sLock) {
503 sScreen = screen;
504 }
505 }
506
Winson Chung557d6ed2011-07-08 15:34:52 -0700507 /**
508 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
509 * a configuration step, this allows the proper animations to run after other transitions.
510 */
511 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700512 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800513 switch (args.requestCode) {
514 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700515 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
516 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800517 break;
518 case REQUEST_PICK_SHORTCUT:
519 processShortcut(args.intent);
520 break;
521 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700522 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
523 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700524 result = true;
525 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800526 case REQUEST_PICK_APPWIDGET:
527 addAppWidgetFromPick(args.intent);
528 break;
529 case REQUEST_CREATE_APPWIDGET:
530 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800531 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700532 result = true;
533 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800534 case REQUEST_PICK_WALLPAPER:
535 // We just wanted the activity result here so we can clear mWaitingForResult
536 break;
537 }
Michael Jurka27614d22012-04-02 06:40:22 -0700538 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
539 // if you turned the screen off and then back while in All Apps, Launcher would not
540 // return to the workspace. Clearing mAddInfo.container here fixes this issue
541 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700542 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800543 }
544
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800545 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800546 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700547 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800548 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
549 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700550 mWaitingForResult = false;
551
Adam Cohened66b2b2012-01-23 17:28:51 -0800552 // We have special handling for widgets
553 if (isWidgetDrop) {
554 int appWidgetId = data != null ?
555 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
556 completeTwoStageWidgetDrop(resultCode, appWidgetId);
557 return;
558 }
559
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800560 // The pattern used here is that a user PICKs a specific application,
561 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700562
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800563 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
564 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700565 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800566 final PendingAddArguments args = new PendingAddArguments();
567 args.requestCode = requestCode;
568 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700569 args.container = mPendingAddInfo.container;
570 args.screen = mPendingAddInfo.screen;
571 args.cellX = mPendingAddInfo.cellX;
572 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800573 if (isWorkspaceLocked()) {
574 sPendingAddList.add(args);
575 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700576 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800579 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700580 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800581 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
582 null);
583 }
584
585 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
586 CellLayout cellLayout = (CellLayout) mWorkspace.getChildAt(mWidgetBeingConfigured.screen);
587 Runnable onCompleteRunnable = null;
588 int animationType = 0;
589
590 if (resultCode == RESULT_OK) {
591 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
592 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
593 mWidgetBeingConfigured.info);
594 mWidgetBeingConfigured.boundWidget = layout;
595 onCompleteRunnable = new Runnable() {
596 @Override
597 public void run() {
598 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
599 mPendingAddInfo.screen, layout, null);
600 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
601 null);
602 }
603 };
604 } else if (resultCode == RESULT_CANCELED) {
605 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
606 onCompleteRunnable = new Runnable() {
607 @Override
608 public void run() {
609 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
610 null);
611 }
612 };
613 }
Adam Cohend41fbf52012-02-16 23:53:59 -0800614 mWorkspace.animateWidgetDrop(mWidgetBeingConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800615 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Adam Cohend41fbf52012-02-16 23:53:59 -0800616 animationType, mWidgetBeingConfigured.boundWidget, true);
Adam Cohen1b36dc32012-02-13 19:27:37 -0800617 mWidgetBeingConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 }
619
620 @Override
621 protected void onResume() {
622 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700623
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800624 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700625 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400626 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700627 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400628 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700629 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800630 }
Winson Chunge4e50662012-01-23 14:45:13 -0800631
632 // Reset the pressed state of icons that were locked in the press state while activities
633 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800634 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800635 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800636 mWaitingForResume.setStayPressed(false);
637 }
Winson Chunge4e50662012-01-23 14:45:13 -0800638 if (mAppsCustomizeContent != null) {
639 // Resets the previous all apps icon press state
640 mAppsCustomizeContent.resetDrawableState();
641 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 }
643
644 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700645 protected void onPause() {
646 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700647 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800648 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700649 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700650 }
Romain Guycbb89e42009-06-08 15:52:54 -0700651
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700652 @Override
653 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400654 // Flag the loader to stop early before switching
655 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700656 if (mAppsCustomizeContent != null) {
657 mAppsCustomizeContent.surrender();
658 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800659 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700660 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700661
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800662 // We can't hide the IME if it was forced open. So don't bother
663 /*
664 @Override
665 public void onWindowFocusChanged(boolean hasFocus) {
666 super.onWindowFocusChanged(hasFocus);
667
668 if (hasFocus) {
669 final InputMethodManager inputManager = (InputMethodManager)
670 getSystemService(Context.INPUT_METHOD_SERVICE);
671 WindowManager.LayoutParams lp = getWindow().getAttributes();
672 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
673 android.os.Handler()) {
674 protected void onReceiveResult(int resultCode, Bundle resultData) {
675 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
676 }
677 });
678 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
679 }
680 }
681 */
682
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800683 private boolean acceptFilter() {
684 final InputMethodManager inputManager = (InputMethodManager)
685 getSystemService(Context.INPUT_METHOD_SERVICE);
686 return !inputManager.isFullscreenMode();
687 }
688
689 @Override
690 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700691 final int uniChar = event.getUnicodeChar();
692 final boolean handled = super.onKeyDown(keyCode, event);
693 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
694 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
696 keyCode, event);
697 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700698 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700699 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700700 // showSearchDialog()
701 // If there are multiple keystrokes before the search dialog takes focus,
702 // onSearchRequested() will be called for every keystroke,
703 // but it is idempotent, so it's fine.
704 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800705 }
706 }
707
Joe Onorato8a9625e2010-01-28 15:55:35 -0800708 // Eat the long press event so the keyboard doesn't come up.
709 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
710 return true;
711 }
712
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 return handled;
714 }
715
Karl Rosaen138a0412009-04-23 19:00:21 -0700716 private String getTypedText() {
717 return mDefaultKeySsb.toString();
718 }
719
720 private void clearTypedText() {
721 mDefaultKeySsb.clear();
722 mDefaultKeySsb.clearSpans();
723 Selection.setSelection(mDefaultKeySsb, 0);
724 }
725
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800726 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700727 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
728 * State
729 */
730 private static State intToState(int stateOrdinal) {
731 State state = State.WORKSPACE;
732 final State[] stateValues = State.values();
733 for (int i = 0; i < stateValues.length; i++) {
734 if (stateValues[i].ordinal() == stateOrdinal) {
735 state = stateValues[i];
736 break;
737 }
738 }
739 return state;
740 }
741
742 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800743 * Restores the previous state, if it exists.
744 *
745 * @param savedState The previous state.
746 */
747 private void restoreState(Bundle savedState) {
748 if (savedState == null) {
749 return;
750 }
751
Michael Jurka883f55b2010-10-21 15:47:14 -0700752 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700753 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800754 showAllApps(false);
755 }
756
Winson Chungf0c6ae02012-03-21 16:10:31 -0700757 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800758 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700759 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
761
Winson Chung3d503fb2011-07-13 17:25:49 -0700762 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
763 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700764
Winson Chung3d503fb2011-07-13 17:25:49 -0700765 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
766 mPendingAddInfo.container = pendingAddContainer;
767 mPendingAddInfo.screen = pendingAddScreen;
768 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
769 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800770 mRestoring = true;
771 }
772
773 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
774 if (renameFolder) {
775 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700776 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800777 mRestoring = true;
778 }
Winson Chunga12a2502010-12-20 14:41:35 -0800779
Winson Chung785d2eb2011-04-14 16:08:02 -0700780
781 // Restore the AppsCustomize tab
782 if (mAppsCustomizeTabHost != null) {
783 String curTab = savedState.getString("apps_customize_currentTab");
784 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700785 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700786 mAppsCustomizeContent.setContentType(
787 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
788 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700789 mAppsCustomizeContent.loadAssociatedPages(
790 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700791 }
792
Winson Chung5afbf7b2011-07-25 11:53:08 -0700793 int currentIndex = savedState.getInt("apps_customize_currentIndex");
794 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700795 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 }
797
798 /**
799 * Finds all the views we need and configure them properly.
800 */
801 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700802 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400803
Winson Chung4c98d922011-05-31 16:50:48 -0700804 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
805 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800806 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
807 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808
Winson Chung4c98d922011-05-31 16:50:48 -0700809 // Setup the drag layer
810 mDragLayer.setup(this, dragController);
811
Winson Chung3d503fb2011-07-13 17:25:49 -0700812 // Setup the hotseat
813 mHotseat = (Hotseat) findViewById(R.id.hotseat);
814 if (mHotseat != null) {
815 mHotseat.setup(this);
816 }
817
Winson Chung4c98d922011-05-31 16:50:48 -0700818 // Setup the workspace
819 mWorkspace.setHapticFeedbackEnabled(false);
820 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700821 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700822 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700823
Winson Chungf0ea4d32011-06-06 14:27:16 -0700824 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700825 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700826
Winson Chungf0ea4d32011-06-06 14:27:16 -0700827 // Setup AppsCustomize
828 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
829 findViewById(R.id.apps_customize_pane);
830 mAppsCustomizeContent = (AppsCustomizePagedView)
831 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700832 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700833 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400834
Michael Jurka5130e402011-10-13 04:55:35 -0700835 // Get the all apps button
836 mAllAppsButton = findViewById(R.id.all_apps_button);
837 if (mAllAppsButton != null) {
838 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
839 @Override
840 public boolean onTouch(View v, MotionEvent event) {
841 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
842 onTouchDownAllAppsButton(v);
843 }
844 return false;
845 }
846 });
847 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700848 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700849 dragController.setDragScoller(mWorkspace);
850 dragController.setScrollView(mDragLayer);
851 dragController.setMoveTarget(mWorkspace);
852 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700853 if (mSearchDropTargetBar != null) {
854 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800855 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 }
857
858 /**
859 * Creates a view representing a shortcut.
860 *
861 * @param info The data structure describing the shortcut.
862 *
863 * @return A View inflated from R.layout.application.
864 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800865 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700867 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 }
869
870 /**
871 * Creates a view representing a shortcut inflated from the specified resource.
872 *
873 * @param layoutResId The id of the XML layout used to create the shortcut.
874 * @param parent The group the shortcut belongs to.
875 * @param info The data structure describing the shortcut.
876 *
877 * @return A View inflated from layoutResId.
878 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800879 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800880 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
881 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 return favorite;
884 }
885
886 /**
887 * Add an application shortcut to the workspace.
888 *
889 * @param data The intent describing the application.
890 * @param cellInfo The position on screen where to create the shortcut.
891 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700892 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700893 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700894 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700895
Adam Cohenfbba09b2011-07-18 21:43:05 -0700896 // First we check if we already know the exact location where we want to add this item.
897 if (cellX >= 0 && cellY >= 0) {
898 cellXY[0] = cellX;
899 cellXY[1] = cellY;
900 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700901 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700902 return;
903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800905 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800906
Romain Guy73b979d2009-06-09 12:57:21 -0700907 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800908 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800910 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700911 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700912 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800913 } else {
914 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800915 }
916 }
Romain Guycbb89e42009-06-08 15:52:54 -0700917
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800918 /**
919 * Add a shortcut to the workspace.
920 *
921 * @param data The intent describing the shortcut.
922 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800923 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700924 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
925 int cellY) {
926 int[] cellXY = mTmpAddItemCellCoordinates;
927 int[] touchXY = mPendingAddInfo.dropPos;
928 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -0700929
Michael Jurkad3ef3062010-11-23 16:23:58 -0800930 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -0700931
Adam Cohen558baaf2011-08-15 15:22:57 -0700932 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -0800933 if (info == null) {
934 return;
935 }
Adam Cohen558baaf2011-08-15 15:22:57 -0700936 final View view = createShortcut(info);
937
Adam Cohenfbba09b2011-07-18 21:43:05 -0700938 // First we check if we already know the exact location where we want to add this item.
939 if (cellX >= 0 && cellY >= 0) {
940 cellXY[0] = cellX;
941 cellXY[1] = cellY;
942 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -0700943
944 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -0800945 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -0700946 true, null,null)) {
947 return;
948 }
949 DragObject dragObject = new DragObject();
950 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -0800951 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
952 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -0700953 return;
954 }
Adam Cohenfbba09b2011-07-18 21:43:05 -0700955 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -0800956 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -0700957 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800958 foundCellSpan = (result != null);
959 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -0700960 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -0800961 }
962
963 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -0700964 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700965 return;
966 }
967
Adam Cohen558baaf2011-08-15 15:22:57 -0700968 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969
970 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -0700971 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
972 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 }
974 }
975
Winson Chunga5c96362012-04-12 14:04:41 -0700976 int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
Adam Cohen0cf2a7c2011-11-08 15:07:01 -0800977 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -0700978 // We want to account for the extra amount of padding that we are adding to the widget
979 // to ensure that it gets the full amount of space that it has requested
980 int requiredWidth = minWidth + padding.left + padding.right;
981 int requiredHeight = minHeight + padding.top + padding.bottom;
982 return CellLayout.rectToCell(getResources(), requiredWidth, requiredHeight, null);
983 }
984
Winson Chunga5c96362012-04-12 14:04:41 -0700985 int[] getSpanForWidget(AppWidgetProviderInfo info) {
986 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -0700987 }
988
Winson Chunga5c96362012-04-12 14:04:41 -0700989 int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
990 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -0700991 }
992
Winson Chunga5c96362012-04-12 14:04:41 -0700993 int[] getSpanForWidget(PendingAddWidgetInfo info) {
994 return getSpanForWidget(info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -0700995 }
996
Winson Chunga5c96362012-04-12 14:04:41 -0700997 int[] getMinSpanForWidget(PendingAddWidgetInfo info) {
Adam Cohend41fbf52012-02-16 23:53:59 -0800998 return getSpanForWidget(info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -0700999 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001000 }
1001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001003 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001005 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001006 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001008 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1009 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1010 if (appWidgetInfo == null) {
1011 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1012 }
Romain Guycbb89e42009-06-08 15:52:54 -07001013
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001014 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001015 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001016
Winson Chunga5c96362012-04-12 14:04:41 -07001017 int[] minSpanXY = getMinSpanForWidget(appWidgetInfo);
1018 int[] spanXY = getSpanForWidget(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001019
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001021 // We have saved the position to which the widget was dragged-- this really only matters
1022 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001023 int[] cellXY = mTmpAddItemCellCoordinates;
1024 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001025 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001026 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001027 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1028 cellXY[0] = mPendingAddInfo.cellX;
1029 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001030 spanXY[0] = mPendingAddInfo.spanX;
1031 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001032 foundCellSpan = true;
1033 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001034 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001035 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001036 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1037 spanXY[1], cellXY, finalSpan);
1038 spanXY[0] = finalSpan[0];
1039 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001040 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001041 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001042 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001043 }
1044
Michael Jurka0280c3b2010-09-17 15:00:07 -07001045 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001046 if (appWidgetId != -1) {
1047 // Deleting an app widget ID is a void call but writes to disk before returning
1048 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001049 new Thread("deleteAppWidgetId") {
1050 public void run() {
1051 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1052 }
1053 }.start();
1054 }
Winson Chung93eef082012-03-23 15:59:27 -07001055 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001056 return;
1057 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001058
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001059 // Build Launcher-specific widget info and save to database
Michael Jurkac9d95c52011-08-29 14:03:34 -07001060 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001061 launcherInfo.spanX = spanXY[0];
1062 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001063 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1064 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001065
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001066 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001067 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068
1069 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001070 if (hostView == null) {
1071 // Perform actual inflation because we're live
1072 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1073 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1074 } else {
1075 // The AppWidgetHostView has already been inflated and instantiated
1076 launcherInfo.hostView = hostView;
1077 }
Romain Guycbb89e42009-06-08 15:52:54 -07001078
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001080 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung3d503fb2011-07-13 17:25:49 -07001081 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001082 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001083
1084 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001085 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001086 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 }
Romain Guycbb89e42009-06-08 15:52:54 -07001088
Adam Cohended9f8d2010-11-03 13:25:16 -07001089 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1090 @Override
1091 public void onReceive(Context context, Intent intent) {
1092 final String action = intent.getAction();
1093 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1094 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001095 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001096 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001097
Winson Chungc100e8e2011-08-09 16:02:43 -07001098 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001099 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001100 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1101 mAppsCustomizeTabHost.reset();
1102 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001103 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001104 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1105 mUserPresent = true;
1106 updateRunning();
1107 }
1108 }
1109 };
1110
1111 @Override
1112 public void onAttachedToWindow() {
1113 super.onAttachedToWindow();
1114
1115 // Listen for broadcasts related to user-presence
1116 final IntentFilter filter = new IntentFilter();
1117 filter.addAction(Intent.ACTION_SCREEN_OFF);
1118 filter.addAction(Intent.ACTION_USER_PRESENT);
1119 registerReceiver(mReceiver, filter);
1120
Adam Cohend113e0c2010-11-11 10:48:05 -08001121 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001122 mVisible = true;
1123 }
1124
1125 @Override
1126 public void onDetachedFromWindow() {
1127 super.onDetachedFromWindow();
1128 mVisible = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001129 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001130
Adam Cohend113e0c2010-11-11 10:48:05 -08001131 if (mAttached) {
1132 unregisterReceiver(mReceiver);
1133 mAttached = false;
1134 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001135 updateRunning();
1136 }
1137
1138 public void onWindowVisibilityChanged(int visibility) {
1139 mVisible = visibility == View.VISIBLE;
1140 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001141 // The following code used to be in onResume, but it turns out onResume is called when
1142 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1143 // is a more appropriate event to handle
1144 if (mVisible) {
1145 mAppsCustomizeTabHost.onWindowVisible();
1146 if (!mWorkspaceLoading) {
1147 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001148 // We want to let Launcher draw itself at least once before we force it to build
1149 // layers on all the workspace pages, so that transitioning to Launcher from other
1150 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1151 // a true draw so we wait until the second preDraw call to be safe
1152 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001153 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001154 // We delay the layer building a bit in order to give
1155 // other message processing a time to run. In particular
1156 // this avoids a delay in hiding the IME if it was
1157 // currently shown, because doing that may involve
1158 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001159 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurka6ee21d22012-02-21 18:20:27 -08001160 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001161 return true;
1162 }
1163 });
1164 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001165 // When Launcher comes back to foreground, a different Activity might be responsible for
1166 // the app market intent, so refresh the icon
1167 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001168 clearTypedText();
1169 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001170 }
1171
1172 private void sendAdvanceMessage(long delay) {
1173 mHandler.removeMessages(ADVANCE_MSG);
1174 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1175 mHandler.sendMessageDelayed(msg, delay);
1176 mAutoAdvanceSentTime = System.currentTimeMillis();
1177 }
1178
1179 private void updateRunning() {
1180 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1181 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1182 mAutoAdvanceRunning = autoAdvanceRunning;
1183 if (autoAdvanceRunning) {
1184 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1185 sendAdvanceMessage(delay);
1186 } else {
1187 if (!mWidgetsToAdvance.isEmpty()) {
1188 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1189 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1190 }
1191 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001192 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001193 }
1194 }
1195 }
1196
1197 private final Handler mHandler = new Handler() {
1198 @Override
1199 public void handleMessage(Message msg) {
1200 if (msg.what == ADVANCE_MSG) {
1201 int i = 0;
1202 for (View key: mWidgetsToAdvance.keySet()) {
1203 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1204 final int delay = mAdvanceStagger * i;
1205 if (v instanceof Advanceable) {
1206 postDelayed(new Runnable() {
1207 public void run() {
1208 ((Advanceable) v).advance();
1209 }
1210 }, delay);
1211 }
1212 i++;
1213 }
1214 sendAdvanceMessage(mAdvanceInterval);
1215 }
1216 }
1217 };
1218
1219 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001220 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001221 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1222 if (v instanceof Advanceable) {
1223 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001224 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001225 updateRunning();
1226 }
1227 }
1228
1229 void removeWidgetToAutoAdvance(View hostView) {
1230 if (mWidgetsToAdvance.containsKey(hostView)) {
1231 mWidgetsToAdvance.remove(hostView);
1232 updateRunning();
1233 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001234 }
1235
Joe Onorato9c1289c2009-08-17 11:03:03 -04001236 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001237 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001238 launcherInfo.hostView = null;
1239 }
1240
Winson Chung93eef082012-03-23 15:59:27 -07001241 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1242 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1243 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001244 }
1245
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001246 public LauncherAppWidgetHost getAppWidgetHost() {
1247 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 }
Romain Guycbb89e42009-06-08 15:52:54 -07001249
Winson Chunga9abd0e2010-10-27 17:18:37 -07001250 public LauncherModel getModel() {
1251 return mModel;
1252 }
1253
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001254 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001255 getWindow().closeAllPanels();
1256
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001257 // Whatever we were doing is hereby canceled.
1258 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001259 }
1260
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 @Override
1262 protected void onNewIntent(Intent intent) {
1263 super.onNewIntent(intent);
1264
1265 // Close the menu
1266 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001267 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001268 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001269
Joe Onorato14f122b2009-11-19 14:06:36 -08001270 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1271 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001272
Adam Cohenac56cff2011-09-28 20:45:37 -07001273 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001274 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001275 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001276 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1277 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001278 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001279 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001280
1281 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001282 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001283 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001284
Joe Onorato3a8820b2009-11-10 15:06:42 -08001285 final View v = getWindow().peekDecorView();
1286 if (v != null && v.getWindowToken() != null) {
1287 InputMethodManager imm = (InputMethodManager)getSystemService(
1288 INPUT_METHOD_SERVICE);
1289 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001290 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001291
Michael Jurka35aa14d2011-07-07 17:01:08 -07001292 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001293 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001294 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001295 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 }
1297 }
1298
1299 @Override
1300 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1301 // Do not call super here
1302 mSavedInstanceState = savedInstanceState;
1303 }
1304
1305 @Override
1306 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001307 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001308 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309
Michael Jurka883f55b2010-10-21 15:47:14 -07001310 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001311 // We close any open folder since it will not be re-opened, and we need to make sure
1312 // this state is reflected.
1313 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314
Winson Chung3d503fb2011-07-13 17:25:49 -07001315 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1316 mWaitingForResult) {
1317 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1318 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1319 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1320 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001321 }
1322
1323 if (mFolderInfo != null && mWaitingForResult) {
1324 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1325 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1326 }
Michael Jurka946ad472010-07-09 18:05:18 -07001327
Winson Chung785d2eb2011-04-14 16:08:02 -07001328 // Save the current AppsCustomize tab
1329 if (mAppsCustomizeTabHost != null) {
1330 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1331 if (currentTabTag != null) {
1332 outState.putString("apps_customize_currentTab", currentTabTag);
1333 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001334 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1335 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001336 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
1338
1339 @Override
1340 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001341 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001342
Winson Chunge7a03942011-08-05 15:05:12 -07001343 // Remove all pending runnables
1344 mHandler.removeMessages(ADVANCE_MSG);
1345 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001346 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001347
Winson Chungcd2b0142011-06-08 16:02:26 -07001348 // Stop callbacks from LauncherModel
1349 LauncherApplication app = ((LauncherApplication) getApplication());
1350 mModel.stopLoader();
1351 app.setLauncher(null);
1352
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001353 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001354 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001355 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001356 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001358 mAppWidgetHost = null;
1359
1360 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361
1362 TextKeyListener.getInstance().release();
1363
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364
Winson Chung3d503fb2011-07-13 17:25:49 -07001365 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001366
1367 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001368 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001369
1370 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1371 mWorkspace.removeAllViews();
1372 mWorkspace = null;
1373 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001374
1375 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 }
1377
Adam Cohena9cf38f2011-05-02 15:36:58 -07001378 public DragController getDragController() {
1379 return mDragController;
1380 }
1381
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 @Override
1383 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001384 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 super.startActivityForResult(intent, requestCode);
1386 }
1387
Winson Chung70d72102011-08-12 11:24:35 -07001388 /**
1389 * Indicates that we want global search for this activity by setting the globalSearch
1390 * argument for {@link #startSearch} to true.
1391 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001393 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001395
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001396 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001397
Karl Rosaen138a0412009-04-23 19:00:21 -07001398 if (initialQuery == null) {
1399 // Use any text typed in the launcher as the initial query
1400 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001401 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402 if (appSearchData == null) {
1403 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001404 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001405 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001406 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001407
Karl Rosaen138a0412009-04-23 19:00:21 -07001408 final SearchManager searchManager =
1409 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001410 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001411 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 }
1413
Winson Chung70d72102011-08-12 11:24:35 -07001414 @Override
1415 public boolean onCreateOptionsMenu(Menu menu) {
1416 if (isWorkspaceLocked()) {
1417 return false;
1418 }
1419
1420 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001421
1422 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1423 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1424 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001425 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1426 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1427 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001428 String helpUrl = getString(R.string.help_url);
1429 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001430 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1431 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1432
Winson Chung70d72102011-08-12 11:24:35 -07001433 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1434 .setIcon(android.R.drawable.ic_menu_gallery)
1435 .setAlphabeticShortcut('W');
1436 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1437 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001438 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001439 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001440 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1441 .setIcon(android.R.drawable.ic_menu_preferences)
1442 .setIntent(settings)
1443 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001444 if (!helpUrl.isEmpty()) {
1445 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1446 .setIcon(android.R.drawable.ic_menu_help)
1447 .setIntent(help)
1448 .setAlphabeticShortcut('H');
1449 }
Winson Chung70d72102011-08-12 11:24:35 -07001450 return true;
1451 }
1452
1453 @Override
1454 public boolean onPrepareOptionsMenu(Menu menu) {
1455 super.onPrepareOptionsMenu(menu);
1456
1457 if (mAppsCustomizeTabHost.isTransitioning()) {
1458 return false;
1459 }
1460 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1461 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1462
1463 return true;
1464 }
1465
1466 @Override
1467 public boolean onOptionsItemSelected(MenuItem item) {
1468 switch (item.getItemId()) {
1469 case MENU_WALLPAPER_SETTINGS:
1470 startWallpaper();
1471 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001472 }
1473
1474 return super.onOptionsItemSelected(item);
1475 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001477 @Override
1478 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001479 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001480 // Use a custom animation for launching search
1481 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001482 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001483 }
1484
Joe Onorato9c1289c2009-08-17 11:03:03 -04001485 public boolean isWorkspaceLocked() {
1486 return mWorkspaceLoading || mWaitingForResult;
1487 }
1488
Michael Jurka0280c3b2010-09-17 15:00:07 -07001489 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001490 mPendingAddInfo.container = ItemInfo.NO_ID;
1491 mPendingAddInfo.screen = -1;
1492 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1493 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001494 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001495 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001496 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001497
Michael Jurkaaf442092010-06-10 17:01:57 -07001498 void addAppWidgetFromPick(Intent data) {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001499 // TODO: catch bad widget exception when sent
1500 int appWidgetId = data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Michael Jurkaaf442092010-06-10 17:01:57 -07001501 // TODO: Is this log message meaningful?
1502 if (LOGD) Log.d(TAG, "dumping extras content=" + data.getExtras());
Winson Chung55cef262010-10-28 14:14:18 -07001503 addAppWidgetImpl(appWidgetId, null);
Michael Jurkaaf442092010-06-10 17:01:57 -07001504 }
1505
Adam Cohened66b2b2012-01-23 17:28:51 -08001506 void addAppWidgetImpl(final int appWidgetId, final PendingAddWidgetInfo info) {
1507 final AppWidgetProviderInfo appWidget = info.info;
Bjorn Bringert7984c942009-12-09 15:38:25 +00001508 if (appWidget.configure != null) {
1509 // Launch over to configure widget, if needed
1510 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
1511 intent.setComponent(appWidget.configure);
1512 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Winson Chung55cef262010-10-28 14:14:18 -07001513 if (info != null) {
Winson Chung68846fd2010-10-29 11:00:27 -07001514 if (info.mimeType != null && !info.mimeType.isEmpty()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001515 intent.putExtra(InstallWidgetReceiver.
1516 EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE, info.mimeType);
Winson Chung68846fd2010-10-29 11:00:27 -07001517
1518 final String mimeType = info.mimeType;
1519 final ClipData clipData = (ClipData) info.configurationData;
1520 final ClipDescription clipDesc = clipData.getDescription();
1521 for (int i = 0; i < clipDesc.getMimeTypeCount(); ++i) {
1522 if (clipDesc.getMimeType(i).equals(mimeType)) {
Dianne Hackborn0d5aad72011-01-17 15:28:58 -08001523 final ClipData.Item item = clipData.getItemAt(i);
Winson Chung68846fd2010-10-29 11:00:27 -07001524 final CharSequence stringData = item.getText();
1525 final Uri uriData = item.getUri();
1526 final Intent intentData = item.getIntent();
Adam Cohened66b2b2012-01-23 17:28:51 -08001527 final String key = InstallWidgetReceiver.
1528 EXTRA_APPWIDGET_CONFIGURATION_DATA;
Winson Chung68846fd2010-10-29 11:00:27 -07001529 if (uriData != null) {
1530 intent.putExtra(key, uriData);
1531 } else if (intentData != null) {
1532 intent.putExtra(key, intentData);
1533 } else if (stringData != null) {
1534 intent.putExtra(key, stringData);
1535 }
1536 break;
1537 }
1538 }
1539 }
Winson Chung55cef262010-10-28 14:14:18 -07001540 }
Romain Guy8e633c52010-03-04 12:51:36 -08001541 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
Adam Cohened66b2b2012-01-23 17:28:51 -08001542 mWidgetBeingConfigured = info;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001543 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001544 // Otherwise just add it
Adam Cohened66b2b2012-01-23 17:28:51 -08001545 completeAddAppWidget(appWidgetId, info.container, info.screen, info.boundWidget, appWidget);
Winson Chung557d6ed2011-07-08 15:34:52 -07001546 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001547 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001548 }
1549 }
Romain Guycbb89e42009-06-08 15:52:54 -07001550
Adam Cohenfbba09b2011-07-18 21:43:05 -07001551 /**
1552 * Process a shortcut drop.
1553 *
1554 * @param componentName The name of the component
1555 * @param screen The screen where it should be added
1556 * @param cell The cell it should be added to, optional
1557 * @param position The location on the screen where it was dropped, optional
1558 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001559 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1560 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001561 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001562 mPendingAddInfo.container = container;
1563 mPendingAddInfo.screen = screen;
1564 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001565
1566 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001567 mPendingAddInfo.cellX = cell[0];
1568 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001569 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001570
1571 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1572 createShortcutIntent.setComponent(componentName);
1573 processShortcut(createShortcutIntent);
1574 }
1575
Adam Cohenfbba09b2011-07-18 21:43:05 -07001576 /**
1577 * Process a widget drop.
1578 *
1579 * @param info The PendingAppWidgetInfo of the widget being added.
1580 * @param screen The screen where it should be added
1581 * @param cell The cell it should be added to, optional
1582 * @param position The location on the screen where it was dropped, optional
1583 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001584 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001585 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001586 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001587 mPendingAddInfo.container = info.container = container;
1588 mPendingAddInfo.screen = info.screen = screen;
1589 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001590 mPendingAddInfo.minSpanX = info.minSpanX;
1591 mPendingAddInfo.minSpanY = info.minSpanY;
1592
Adam Cohenfbba09b2011-07-18 21:43:05 -07001593 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001594 mPendingAddInfo.cellX = cell[0];
1595 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001596 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001597 if (span != null) {
1598 mPendingAddInfo.spanX = span[0];
1599 mPendingAddInfo.spanY = span[1];
1600 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001601
Adam Cohened66b2b2012-01-23 17:28:51 -08001602 AppWidgetHostView hostView = info.boundWidget;
1603 int appWidgetId;
1604 if (hostView != null) {
1605 appWidgetId = hostView.getAppWidgetId();
1606 } else {
1607 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
1608 AppWidgetManager.getInstance(this).bindAppWidgetId(appWidgetId, info.componentName);
1609 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001610 addAppWidgetImpl(appWidgetId, info);
1611 }
1612
Joe Onoratodeb98af2010-02-19 14:59:39 -08001613 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001614 // Handle case where user selected "Applications"
1615 String applicationName = getResources().getString(R.string.group_applications);
1616 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001617
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001618 if (applicationName != null && applicationName.equals(shortcutName)) {
1619 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1620 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001621
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001622 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1623 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001624 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001625 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001626 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001627 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001628 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001629 }
1630
Winson Chung24ab2f12010-09-16 14:10:47 -07001631 void processWallpaper(Intent intent) {
1632 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1633 }
1634
Winson Chung3d503fb2011-07-13 17:25:49 -07001635 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1636 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001637 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 folderInfo.title = getText(R.string.folder_name);
1639
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001641 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1642 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001643 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644
1645 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001646 FolderIcon newFolder =
1647 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1648 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1649 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001650 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 }
Romain Guycbb89e42009-06-08 15:52:54 -07001652
Joe Onorato9c1289c2009-08-17 11:03:03 -04001653 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001654 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001655 }
1656
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001657 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001658 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001659 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001660 Intent chooser = Intent.createChooser(pickWallpaper,
1661 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001662 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1663 // Removed in Eclair MR1
1664// WallpaperManager wm = (WallpaperManager)
1665// getSystemService(Context.WALLPAPER_SERVICE);
1666// WallpaperInfo wi = wm.getWallpaperInfo();
1667// if (wi != null && wi.getSettingsActivity() != null) {
1668// LabeledIntent li = new LabeledIntent(getPackageName(),
1669// R.string.configure_wallpaper, 0);
1670// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1671// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1672// }
Mike Clerona0618e42009-10-22 13:55:21 -07001673 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001674 }
1675
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001676 /**
1677 * Registers various content observers. The current implementation registers
1678 * only a favorites observer to keep track of the favorites applications.
1679 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001680 private void registerContentObservers() {
1681 ContentResolver resolver = getContentResolver();
1682 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1683 true, mWidgetObserver);
1684 }
1685
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 @Override
1687 public boolean dispatchKeyEvent(KeyEvent event) {
1688 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1689 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001690 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001691 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001692 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001693 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001694 dumpState();
1695 return true;
1696 }
1697 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001698 }
1699 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1700 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001701 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 return true;
1703 }
1704 }
1705
1706 return super.dispatchKeyEvent(event);
1707 }
1708
Joe Onorato88ec0992009-11-19 13:16:06 -08001709 @Override
1710 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001711 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001712 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001713 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001714 Folder openFolder = mWorkspace.getOpenFolder();
1715 if (openFolder.isEditingName()) {
1716 openFolder.dismissEditingName();
1717 } else {
1718 closeFolder();
1719 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001720 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001721 mWorkspace.exitWidgetResizeMode();
1722
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001723 // Back button is a no-op here, but give at least some feedback for the button press
1724 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001725 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001726 }
1727
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001728 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001729 * Re-listen when widgets are reset.
1730 */
1731 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001732 if (mAppWidgetHost != null) {
1733 mAppWidgetHost.startListening();
1734 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001735 }
1736
1737 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001738 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1739 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001741 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001742 if (mModel != null) {
1743 mModel.unbindWorkspaceItems();
1744 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001745 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001746
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 /**
1748 * Launches the intent referred by the clicked shortcut.
1749 *
1750 * @param v The view representing the clicked shortcut.
1751 */
1752 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001753 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1754 // view has detached (it's possible for this to happen if the view is removed mid touch).
1755 if (v.getWindowToken() == null) {
1756 return;
1757 }
1758
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001759 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001760 return;
1761 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001762
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001763 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001764 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001766 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001767 int[] pos = new int[2];
1768 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001769 intent.setSourceBounds(new Rect(pos[0], pos[1],
1770 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001771
1772 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001773
1774 if (success && v instanceof BubbleTextView) {
1775 mWaitingForResume = (BubbleTextView) v;
1776 mWaitingForResume.setStayPressed(true);
1777 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001778 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001779 if (v instanceof FolderIcon) {
1780 FolderIcon fi = (FolderIcon) v;
1781 handleFolderClick(fi);
1782 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001783 } else if (v == mAllAppsButton) {
1784 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001785 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001786 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001787 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001788 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 }
1790 }
1791
Michael Jurka0e260592010-06-30 17:07:39 -07001792 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001793 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001794 // clicking anywhere on the workspace causes the customization drawer to slide down
1795 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001796 return false;
1797 }
1798
Michael Jurkaaf442092010-06-10 17:01:57 -07001799 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001800 * Event handler for the search button
1801 *
1802 * @param v The view that was clicked.
1803 */
1804 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001805 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1806
Amith Yamasania135ba82011-08-09 17:42:01 -07001807 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001808 }
1809
1810 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001811 * Event handler for the voice button
1812 *
1813 * @param v The view that was clicked.
1814 */
1815 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001816 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001817
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001818 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001819 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001820 startActivity(intent);
1821 }
1822
1823 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001824 * Event handler for the "grid" button that appears on the home screen, which
1825 * enters all apps mode.
1826 *
1827 * @param v The view that was clicked.
1828 */
1829 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001830 showAllApps(true);
1831 }
1832
1833 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001834 // Provide the same haptic feedback that the system offers for virtual keys.
1835 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001836 }
1837
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001838 public void onClickAppMarketButton(View v) {
1839 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001840 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001841 } else {
1842 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001843 }
1844 }
1845
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001846 void startApplicationDetailsActivity(ComponentName componentName) {
1847 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001848 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1849 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001850 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001851 startActivity(intent);
1852 }
1853
Patrick Dubroy5539af72010-09-07 15:22:01 -07001854 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1855 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1856 // System applications cannot be installed. For now, show a toast explaining that.
1857 // We may give them the option of disabling apps this way.
1858 int messageId = R.string.uninstall_system_app_text;
1859 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1860 } else {
1861 String packageName = appInfo.componentName.getPackageName();
1862 String className = appInfo.componentName.getClassName();
1863 Intent intent = new Intent(
1864 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001865 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1866 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001867 startActivity(intent);
1868 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001869 }
1870
Winson Chungc7450e32012-04-17 17:34:08 -07001871 boolean startActivitySafely(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001872 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001873
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001874 try {
Winson Chungc7450e32012-04-17 17:34:08 -07001875 if (v != null) {
1876 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1877 v.getMeasuredWidth(), v.getMeasuredHeight());
1878
1879 startActivity(intent, opts.toBundle());
1880 } else {
1881 startActivity(intent);
1882 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001883 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 } catch (ActivityNotFoundException e) {
1885 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001886 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 } catch (SecurityException e) {
1888 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001889 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001891 "or use the exported attribute for this activity. "
1892 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001893 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001894 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001895 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001896
Romain Guy8e633c52010-03-04 12:51:36 -08001897 void startActivityForResultSafely(Intent intent, int requestCode) {
1898 try {
1899 startActivityForResult(intent, requestCode);
1900 } catch (ActivityNotFoundException e) {
1901 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1902 } catch (SecurityException e) {
1903 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1904 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1905 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1906 "or use the exported attribute for this activity.", e);
1907 }
1908 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909
Adam Cohena9cf38f2011-05-02 15:36:58 -07001910 private void handleFolderClick(FolderIcon folderIcon) {
1911 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001912 Folder openFolder = mWorkspace.getFolderForTag(info);
1913
1914 // If the folder info reports that the associated folder is open, then verify that
1915 // it is actually opened. There have been a few instances where this gets out of sync.
1916 if (info.opened && openFolder == null) {
1917 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1918 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1919 info.opened = false;
1920 }
1921
Adam Cohena9cf38f2011-05-02 15:36:58 -07001922 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 // Close any open folder
1924 closeFolder();
1925 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001926 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001927 } else {
1928 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001929 int folderScreen;
1930 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001931 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001932 // .. and close it
1933 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001934 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001935 // Close any folder open on the current screen
1936 closeFolder();
1937 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001938 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 }
1940 }
1941 }
1942 }
1943
Adam Cohen2801caf2011-05-13 20:57:39 -07001944 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001945 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001946 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1947 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1948 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1949
Adam Cohenc51934b2011-07-26 21:07:43 -07001950 FolderInfo info = (FolderInfo) fi.getTag();
1951 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1952 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001953 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1954 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001955 }
1956
Adam Cohen2801caf2011-05-13 20:57:39 -07001957 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1958 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1959 oa.start();
1960 }
1961
1962 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001963 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001964 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1965 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1966 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1967
Adam Cohenc51934b2011-07-26 21:07:43 -07001968 FolderInfo info = (FolderInfo) fi.getTag();
1969 CellLayout cl = null;
1970 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1971 cl = (CellLayout) fi.getParent().getParent();
1972 }
1973
1974 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001975 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1976 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001977 oa.addListener(new AnimatorListenerAdapter() {
1978 @Override
1979 public void onAnimationEnd(Animator animation) {
1980 if (layout != null) {
1981 layout.clearFolderLeaveBehind();
1982 }
1983 }
1984 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001985 oa.start();
1986 }
1987
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001989 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001990 * is animated relative to the specified View. If the View is null, no animation
1991 * is played.
1992 *
1993 * @param folderInfo The FolderInfo describing the folder to open.
1994 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001995 public void openFolder(FolderIcon folderIcon) {
1996 Folder folder = folderIcon.mFolder;
1997 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998
Adam Cohen2801caf2011-05-13 20:57:39 -07001999 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002000 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001
Adam Cohen4554ee12011-08-03 16:13:21 -07002002 // Just verify that the folder hasn't already been added to the DragLayer.
2003 // There was a one-off crash where the folder had a parent already.
2004 if (folder.getParent() == null) {
2005 mDragLayer.addView(folder);
2006 mDragController.addDropTarget((DropTarget) folder);
2007 } else {
2008 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2009 folder.getParent() + ").");
2010 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002011 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002012 }
2013
2014 public void closeFolder() {
2015 Folder folder = mWorkspace.getOpenFolder();
2016 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002017 if (folder.isEditingName()) {
2018 folder.dismissEditingName();
2019 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002020 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002021
2022 // Dismiss the folder cling
2023 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002024 }
2025 }
2026
2027 void closeFolder(Folder folder) {
2028 folder.getInfo().opened = false;
2029
2030 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2031 if (parent != null) {
2032 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2033 shrinkAndFadeInFolderIcon(fi);
2034 }
2035 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 }
2037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002039
Patrick Dubroye708c522011-03-01 16:03:43 -08002040 if (mState != State.WORKSPACE) {
2041 return false;
2042 }
2043
Joe Onorato9c1289c2009-08-17 11:03:03 -04002044 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 return false;
2046 }
2047
2048 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002049 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 }
2051
Michael Jurka0280c3b2010-09-17 15:00:07 -07002052 resetAddInfo();
2053 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002055 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 return true;
2057 }
2058
Winson Chung3d503fb2011-07-13 17:25:49 -07002059 // The hotseat touch handling does not go through Workspace, and we always allow long press
2060 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002061 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002062 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2063 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002064 if (itemUnderLongClick == null) {
2065 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002066 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2067 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002068 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002070 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002072 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 }
2074 }
2075 }
2076 return true;
2077 }
2078
Winson Chung3d503fb2011-07-13 17:25:49 -07002079 boolean isHotseatLayout(View layout) {
2080 return mHotseat != null && layout != null &&
2081 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2082 }
2083 Hotseat getHotseat() {
2084 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002085 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002086 SearchDropTargetBar getSearchBar() {
2087 return mSearchDropTargetBar;
2088 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002089
Winson Chung3d503fb2011-07-13 17:25:49 -07002090 /**
2091 * Returns the CellLayout of the specified container at the specified screen.
2092 */
2093 CellLayout getCellLayout(long container, int screen) {
2094 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2095 if (mHotseat != null) {
2096 return mHotseat.getLayout();
2097 } else {
2098 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002099 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002100 } else {
2101 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002102 }
2103 }
2104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 Workspace getWorkspace() {
2106 return mWorkspace;
2107 }
2108
Daniel Sandler843e8602010-06-07 14:59:01 -04002109 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2110 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002111 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002112 }
2113
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002114 public boolean isAllAppsButtonRank(int rank) {
2115 return mHotseat.isAllAppsButtonRank(rank);
2116 }
2117
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002118 // AllAppsView.Watcher
2119 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002120 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002121 mWorkspace.setVisibility(View.GONE);
2122 }
2123 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002124
2125 /**
2126 * Helper method for the cameraZoomIn/cameraZoomOut animations
2127 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002128 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002129 * @param scaleFactor The scale factor used for the zoom
2130 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002131 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002132 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002133 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002134 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002135
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002136 void updateWallpaperVisibility(boolean visible) {
2137 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2138 int curflags = getWindow().getAttributes().flags
2139 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2140 if (wpflags != curflags) {
2141 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2142 }
2143 }
2144
Michael Jurkabed61d22012-02-14 22:51:29 -08002145 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2146 if (v instanceof LauncherTransitionable) {
2147 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2148 }
Winson Chung70442722012-02-10 15:43:22 -08002149
2150 // Update the workspace transition step as well
2151 dispatchOnLauncherTransitionStep(v, 0f);
2152 }
2153
2154 private void dispatchOnLauncherTransitionStep(View v, float t) {
2155 if (v instanceof LauncherTransitionable) {
2156 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2157 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002158 }
2159
2160 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2161 if (v instanceof LauncherTransitionable) {
2162 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2163 }
Winson Chung70442722012-02-10 15:43:22 -08002164
2165 // Update the workspace transition step as well
2166 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002167 }
2168
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002169 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002170 * Things to test when changing the following seven functions.
2171 * - Home from workspace
2172 * - from center screen
2173 * - from other screens
2174 * - Home from all apps
2175 * - from center screen
2176 * - from other screens
2177 * - Back from all apps
2178 * - from center screen
2179 * - from other screens
2180 * - Launch app from workspace and quit
2181 * - with back
2182 * - with home
2183 * - Launch app from all apps and quit
2184 * - with back
2185 * - with home
2186 * - Go to a screen that's not the default, then all
2187 * apps, and launch and app, and go back
2188 * - with back
2189 * -with home
2190 * - On workspace, long press power and go back
2191 * - with back
2192 * - with home
2193 * - On all apps, long press power and go back
2194 * - with back
2195 * - with home
2196 * - On workspace, power off
2197 * - On all apps, power off
2198 * - Launch an app and turn off the screen while in that app
2199 * - Go back with home key
2200 * - Go back with back key TODO: make this not go to workspace
2201 * - From all apps
2202 * - From workspace
2203 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2204 * - From all apps
2205 * - From the center workspace
2206 * - From another workspace
2207 */
2208
2209 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002210 * Zoom the camera out from the workspace to reveal 'toView'.
2211 * Assumes that the view to show is anchored at either the very top or very bottom
2212 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002213 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002214 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002215 if (mStateAnimation != null) {
2216 mStateAnimation.cancel();
2217 mStateAnimation = null;
2218 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002219 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002220
Winson Chungf0ea4d32011-06-06 14:27:16 -07002221 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2222 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2223 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002224 final View fromView = mWorkspace;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002225 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002226 final int startDelay =
2227 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002228
Michael Jurkab3e22d92011-10-31 15:58:33 -07002229 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002230
Michael Jurkad74c9842011-07-10 12:44:21 -07002231 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002232 Animator workspaceAnim =
2233 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002234
2235 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002236 toView.setScaleX(scale);
2237 toView.setScaleY(scale);
2238 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2239 scaleAnim.
2240 scaleX(1f).scaleY(1f).
2241 setDuration(duration).
2242 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002243
Winson Chungf0ea4d32011-06-06 14:27:16 -07002244 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002245 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002246 final ObjectAnimator alphaAnim = ObjectAnimator
2247 .ofFloat(toView, "alpha", 0f, 1f)
2248 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002249 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002250 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2251 @Override
2252 public void onAnimationUpdate(ValueAnimator animation) {
2253 float t = (Float) animation.getAnimatedValue();
2254 dispatchOnLauncherTransitionStep(fromView, t);
2255 dispatchOnLauncherTransitionStep(toView, t);
2256 }
2257 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002258
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002259 // toView should appear right at the end of the workspace shrink
2260 // animation
2261 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002262 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002263 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002264
2265 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002266 boolean animationCancelled = false;
2267
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002268 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002269 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002270 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002271 // Prepare the position
2272 toView.setTranslationX(0.0f);
2273 toView.setTranslationY(0.0f);
2274 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002275 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002276 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002277 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002278 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002279 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2280 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002281
2282 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2283 // Hide the workspace scrollbar
2284 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002285 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002286 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002287 if (!animationCancelled) {
2288 updateWallpaperVisibility(false);
2289 }
2290 }
2291
Adam Cohencff6af82011-09-13 14:51:53 -07002292 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002293 public void onAnimationCancel(Animator animation) {
2294 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002295 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002296 });
2297
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002298 if (workspaceAnim != null) {
2299 mStateAnimation.play(workspaceAnim);
2300 }
Michael Jurka1899a362011-11-03 13:50:45 -07002301
2302 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002303 final ViewTreeObserver observer;
2304
Michael Jurkabed61d22012-02-14 22:51:29 -08002305 dispatchOnLauncherTransitionStart(fromView, animated, false);
2306 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002307
2308 // If any of the objects being animated haven't been measured/laid out
2309 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002310 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002311 (mWorkspace.getMeasuredWidth() == 0) ||
2312 (toView.getMeasuredWidth() == 0)) {
2313 observer = mWorkspace.getViewTreeObserver();
2314 delayAnim = true;
2315 } else {
2316 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002317 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002318
2319 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002320 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002321 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2322 public void onGlobalLayout() {
2323 mWorkspace.post(new Runnable() {
2324 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002325 // Check that mStateAnimation hasn't changed while
2326 // we waited for a layout pass
2327 if (mStateAnimation == stateAnimation) {
2328 // Need to update pivots for zoom if layout changed
2329 setPivotsForZoom(toView, scale);
2330 mStateAnimation.start();
2331 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002332 }
2333 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002334 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002335 }
2336 };
2337 observer.addOnGlobalLayoutListener(delayedStart);
2338 } else {
2339 setPivotsForZoom(toView, scale);
Michael Jurka1899a362011-11-03 13:50:45 -07002340 mStateAnimation.start();
2341 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002342 } else {
2343 toView.setTranslationX(0.0f);
2344 toView.setTranslationY(0.0f);
2345 toView.setScaleX(1.0f);
2346 toView.setScaleY(1.0f);
2347 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002348 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002349
Michael Jurkabed61d22012-02-14 22:51:29 -08002350 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2351 // Hide the workspace scrollbar
2352 mWorkspace.hideScrollingIndicator(true);
2353 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002354 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002355 dispatchOnLauncherTransitionStart(fromView, animated, false);
2356 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2357 dispatchOnLauncherTransitionStart(toView, animated, false);
2358 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002359 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002360 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002361 }
2362
2363 /**
2364 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002365 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002366 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002367 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002368 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2369 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002370
Michael Jurkab3e22d92011-10-31 15:58:33 -07002371 if (mStateAnimation != null) {
2372 mStateAnimation.cancel();
2373 mStateAnimation = null;
2374 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002375 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002376
Winson Chungf0ea4d32011-06-06 14:27:16 -07002377 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002378 final int fadeOutDuration =
2379 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002380 final float scaleFactor = (float)
2381 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2382 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002383 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002384 Animator workspaceAnim = null;
2385
2386 if (toState == State.WORKSPACE) {
2387 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2388 workspaceAnim = mWorkspace.getChangeStateAnimation(
2389 Workspace.State.NORMAL, animated, stagger);
2390 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2391 workspaceAnim = mWorkspace.getChangeStateAnimation(
2392 Workspace.State.SPRING_LOADED, animated);
2393 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002394
Michael Jurkab3e22d92011-10-31 15:58:33 -07002395 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002396 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002397 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002398 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002399 final LauncherViewPropertyAnimator scaleAnim =
2400 new LauncherViewPropertyAnimator(fromView);
2401 scaleAnim.
2402 scaleX(scaleFactor).scaleY(scaleFactor).
2403 setDuration(duration).
2404 setInterpolator(new Workspace.ZoomInInterpolator());
2405
2406 final ObjectAnimator alphaAnim = ObjectAnimator
2407 .ofFloat(fromView, "alpha", 1f, 0f)
2408 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002409 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002410 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2411 @Override
2412 public void onAnimationUpdate(ValueAnimator animation) {
2413 float t = 1f - (Float) animation.getAnimatedValue();
2414 dispatchOnLauncherTransitionStep(fromView, t);
2415 dispatchOnLauncherTransitionStep(toView, t);
2416 }
2417 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002418
Michael Jurkabed61d22012-02-14 22:51:29 -08002419 mStateAnimation = new AnimatorSet();
2420
2421 dispatchOnLauncherTransitionStart(fromView, animated, true);
2422 dispatchOnLauncherTransitionStart(toView, animated, true);
2423
2424 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002425 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002426 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002427 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002428 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002429 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2430 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurka430e8a52011-08-08 15:52:14 -07002431 mWorkspace.hideScrollingIndicator(false);
Adam Cohened66b2b2012-01-23 17:28:51 -08002432 if (onCompleteRunnable != null) {
2433 onCompleteRunnable.run();
2434 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002435 }
2436 });
2437
Winson Chungf0ea4d32011-06-06 14:27:16 -07002438 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002439 if (workspaceAnim != null) {
2440 mStateAnimation.play(workspaceAnim);
2441 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002442 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002443 } else {
2444 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002445 dispatchOnLauncherTransitionStart(fromView, animated, true);
2446 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2447 dispatchOnLauncherTransitionStart(toView, animated, true);
2448 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002449 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002450 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002451 }
2452
Michael Jurkae326f182011-11-21 14:05:46 -08002453 @Override
2454 public void onTrimMemory(int level) {
2455 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002456 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002457 mAppsCustomizeTabHost.onTrimMemory();
2458 }
2459 }
2460
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002461 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002462 showWorkspace(animated, null);
2463 }
2464
2465 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002466 if (mState != State.WORKSPACE) {
2467 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002468 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002469
2470 // Show the search bar and hotseat
2471 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002472 // We only need to animate in the dock divider if we're going from spring loaded mode
2473 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002474
2475 // Set focus to the AppsCustomize button
2476 if (mAllAppsButton != null) {
2477 mAllAppsButton.requestFocus();
2478 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002479 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002480
Michael Jurkab737ee62011-11-15 15:57:22 -08002481 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002482
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002483 // Change the state *after* we've called all the transition code
2484 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002485
Winson Chung5fb63472011-02-02 17:03:37 -08002486 // Resume the auto-advance of widgets
2487 mUserPresent = true;
2488 updateRunning();
2489
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002490 // send an accessibility event to announce the context change
2491 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002492 }
2493
Michael Jurkab3e22d92011-10-31 15:58:33 -07002494 void showAllApps(boolean animated) {
2495 if (mState != State.WORKSPACE) return;
2496
2497 showAppsCustomizeHelper(animated, false);
2498 mAppsCustomizeTabHost.requestFocus();
2499
2500 // Hide the search bar and hotseat
2501 mSearchDropTargetBar.hideSearchBar(animated);
2502
2503 // Change the state *after* we've called all the transition code
2504 mState = State.APPS_CUSTOMIZE;
2505
2506 // Pause the auto-advance of widgets until we are out of AllApps
2507 mUserPresent = false;
2508 updateRunning();
2509 closeFolder();
2510
2511 // Send an accessibility event to announce the context change
2512 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2513 }
2514
Adam Cohen7777d962011-08-18 18:58:38 -07002515 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002516 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002517 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002518 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002519 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002520 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002521 }
Adam Cohen7777d962011-08-18 18:58:38 -07002522
Adam Cohened66b2b2012-01-23 17:28:51 -08002523 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2524 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002525 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2526
Winson Chunge7a03942011-08-05 15:05:12 -07002527 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002528 @Override
2529 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002530 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002531 // Before we show workspace, hide all apps again because
2532 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2533 // clean up our state transition functions
2534 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002535 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002536 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002537 } else {
2538 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002539 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002540 }
2541 }, (extendedDelay ?
2542 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2543 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2544 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002545
Michael Jurkad3ef3062010-11-23 16:23:58 -08002546 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002547 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002548 final boolean animated = true;
2549 final boolean springLoaded = true;
2550 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002551 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002552 }
2553 // Otherwise, we are not in spring loaded mode, so don't do anything.
2554 }
2555
Michael Jurkab737ee62011-11-15 15:57:22 -08002556 void hideDockDivider() {
2557 if (mQsbDivider != null && mDockDivider != null) {
2558 mQsbDivider.setVisibility(View.INVISIBLE);
2559 mDockDivider.setVisibility(View.INVISIBLE);
2560 }
2561 }
2562
2563 void showDockDivider(boolean animated) {
2564 if (mQsbDivider != null && mDockDivider != null) {
2565 mQsbDivider.setVisibility(View.VISIBLE);
2566 mDockDivider.setVisibility(View.VISIBLE);
2567 if (mDividerAnimator != null) {
2568 mDividerAnimator.cancel();
2569 mQsbDivider.setAlpha(1f);
2570 mDockDivider.setAlpha(1f);
2571 mDividerAnimator = null;
2572 }
2573 if (animated) {
2574 mDividerAnimator = new AnimatorSet();
2575 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2576 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2577 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2578 mDividerAnimator.start();
2579 }
2580 }
2581 }
2582
Michael Jurkab3e22d92011-10-31 15:58:33 -07002583 void lockAllApps() {
2584 // TODO
2585 }
2586
2587 void unlockAllApps() {
2588 // TODO
2589 }
2590
Adam Cohenfc53cd22011-07-20 15:45:11 -07002591 public boolean isAllAppsCustomizeOpen() {
2592 return mState == State.APPS_CUSTOMIZE;
2593 }
2594
Winson Chungf0ea4d32011-06-06 14:27:16 -07002595 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002596 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002597 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002598 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002599 if (!LauncherApplication.isScreenLarge()) {
2600 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002601 if (mHotseat.getAlpha() != 1f) {
2602 int duration = mSearchDropTargetBar.getTransitionInDuration();
2603 mHotseat.animate().alpha(1f).setDuration(duration);
2604 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002605 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002606 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002607 }
2608 }
2609 }
2610
2611 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002612 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002613 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002614 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002615 if (!LauncherApplication.isScreenLarge()) {
2616 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002617 if (mHotseat.getAlpha() != 0f) {
2618 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2619 mHotseat.animate().alpha(0f).setDuration(duration);
2620 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002621 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002622 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002623 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002624 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002625 }
2626
Patrick Dubroy5f445422011-02-18 14:35:21 -08002627 /**
2628 * Add an item from all apps or customize onto the given workspace screen.
2629 * If layout is null, add to the current screen.
2630 */
2631 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002632 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002633 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002634 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002635 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002636
Winson Chungdff8ebb2011-09-08 17:25:31 -07002637 /** Maps the current orientation to an index for referencing orientation correct global icons */
2638 private int getCurrentOrientationIndexForGlobalIcons() {
2639 // default - 0, landscape - 1
2640 switch (getResources().getConfiguration().orientation) {
2641 case Configuration.ORIENTATION_LANDSCAPE:
2642 return 1;
2643 default:
2644 return 0;
2645 }
2646 }
2647
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002648 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002649 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002650 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002651 // Look for the toolbar icon specified in the activity meta-data
2652 Bundle metaData = packageManager.getActivityInfo(
2653 activityName, PackageManager.GET_META_DATA).metaData;
2654 if (metaData != null) {
2655 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2656 if (iconResId != 0) {
2657 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002658 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002659 }
2660 }
2661 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002662 // This can happen if the activity defines an invalid drawable
2663 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2664 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002665 } catch (Resources.NotFoundException nfe) {
2666 // This can happen if the activity defines an invalid drawable
2667 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2668 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002669 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002670 return null;
2671 }
2672
2673 // if successful in getting icon, return it; otherwise, set button to use default drawable
2674 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2675 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002676 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002677 Resources r = getResources();
2678 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2679 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002680
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002681 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002682 // If we were unable to find the icon via the meta-data, use a generic one
2683 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002684 toolbarIcon = r.getDrawable(fallbackDrawableId);
2685 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002686 if (button != null) {
2687 button.setCompoundDrawables(toolbarIcon, null, null, null);
2688 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002689 return null;
2690 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002691 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002692 if (button != null) {
2693 button.setCompoundDrawables(toolbarIcon, null, null, null);
2694 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002695 return toolbarIcon.getConstantState();
2696 }
2697 }
2698
2699 // if successful in getting icon, return it; otherwise, set button to use default drawable
2700 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2701 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2702 ImageView button = (ImageView) findViewById(buttonId);
2703 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2704
Michael Jurka19e0fc52011-07-22 18:00:21 -07002705 if (button != null) {
2706 // If we were unable to find the icon via the meta-data, use a
2707 // generic one
2708 if (toolbarIcon == null) {
2709 button.setImageResource(fallbackDrawableId);
2710 } else {
2711 button.setImageDrawable(toolbarIcon);
2712 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002713 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002714
2715 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2716
Michael Jurka0423dcf2010-10-05 14:56:18 -07002717 }
2718
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002719 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2720 TextView button = (TextView) findViewById(buttonId);
2721 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2722 }
2723
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002724 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002725 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002726 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002727 }
2728
Winson Chungbb185bd2011-11-21 12:31:42 -08002729 private void invalidatePressedFocusedStates(View container, View button) {
2730 if (container instanceof HolographicLinearLayout) {
2731 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2732 layout.invalidatePressedFocusedStates();
2733 } else if (button instanceof HolographicImageView) {
2734 HolographicImageView view = (HolographicImageView) button;
2735 view.invalidatePressedFocusedStates();
2736 }
2737 }
2738
Winson Chungc51db6a2011-10-05 11:44:49 -07002739 private boolean updateGlobalSearchIcon() {
2740 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002741 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2742 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002743 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002744 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002745
Winson Chung1cad91e2011-05-25 17:41:01 -07002746 final SearchManager searchManager =
2747 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2748 ComponentName activityName = searchManager.getGlobalSearchActivity();
2749 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002750 int coi = getCurrentOrientationIndexForGlobalIcons();
2751 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002752 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002753 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002754 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2755 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002756 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002757 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002758 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002759 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002760 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002761 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2762 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2763 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002764 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002765 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002766 }
2767 }
2768
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002769 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002770 final View searchButtonContainer = findViewById(R.id.search_button_container);
2771 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002772 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002773 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002774 }
2775
Winson Chungc51db6a2011-10-05 11:44:49 -07002776 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002777 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002778 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002779 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002780
Winson Chungc51db6a2011-10-05 11:44:49 -07002781 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002782 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2783 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002784 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002785 int coi = getCurrentOrientationIndexForGlobalIcons();
2786 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002787 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002788 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002789 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002790 voiceButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002791 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002792 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002793 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002794 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002795 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002796 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002797 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002798 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002799 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002800
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002801 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002802 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2803 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002804 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002805 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002806 }
2807
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002808 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002809 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002810 */
2811 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002812 final View marketButton = findViewById(R.id.market_button);
2813 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2814 // Find the app market activity by resolving an intent.
2815 // (If multiple app markets are installed, it will return the ResolverActivity.)
2816 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002817 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002818 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002819 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002820 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002821 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002822 marketButton.setVisibility(View.VISIBLE);
2823 } else {
2824 // We should hide and disable the view so that we don't try and restore the visibility
2825 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2826 marketButton.setVisibility(View.GONE);
2827 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002828 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002829 }
2830
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002831 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002832 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002833 }
2834
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002835 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002836 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002837 */
2838 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2839 @Override
2840 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002841 closeSystemDialogs();
2842 }
2843 }
2844
2845 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002846 * Receives notifications whenever the appwidgets are reset.
2847 */
2848 private class AppWidgetResetObserver extends ContentObserver {
2849 public AppWidgetResetObserver() {
2850 super(new Handler());
2851 }
2852
2853 @Override
2854 public void onChange(boolean selfChange) {
2855 onAppWidgetReset();
2856 }
2857 }
2858
2859 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002860 * If the activity is currently paused, signal that we need to re-run the loader
2861 * in onResume.
2862 *
2863 * This needs to be called from incoming places where resources might have been loaded
2864 * while we are paused. That is becaues the Configuration might be wrong
2865 * when we're not running, and if it comes back to what it was when we
2866 * were paused, we are not restarted.
2867 *
2868 * Implementation of the method from LauncherModel.Callbacks.
2869 *
2870 * @return true if we are currently paused. The caller might be able to
2871 * skip some work in that case since we will come back again.
2872 */
2873 public boolean setLoadOnResume() {
2874 if (mPaused) {
2875 Log.i(TAG, "setLoadOnResume");
2876 mOnResumeNeedsLoad = true;
2877 return true;
2878 } else {
2879 return false;
2880 }
2881 }
2882
2883 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002884 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002885 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002886 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002887 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002888 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002889 } else {
2890 return SCREEN_COUNT / 2;
2891 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002892 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002893
Joe Onorato9c1289c2009-08-17 11:03:03 -04002894 /**
2895 * Refreshes the shortcuts shown on the workspace.
2896 *
2897 * Implementation of the method from LauncherModel.Callbacks.
2898 */
2899 public void startBinding() {
2900 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002901
Winson Chungf0c6ae02012-03-21 16:10:31 -07002902 mNewShortcutAnimatePage = -1;
2903 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07002904 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002905 int count = workspace.getChildCount();
2906 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002907 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002908 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2909 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002910 }
Michael Jurka05bf6442011-11-30 20:28:53 -08002911 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07002912 if (mHotseat != null) {
2913 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002914 }
2915 }
2916
2917 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002918 * Bind the items start-end from the list.
2919 *
2920 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002921 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002922 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002923 setLoadOnResume();
2924
Winson Chungf0c6ae02012-03-21 16:10:31 -07002925 // Get the list of added shortcuts and intersect them with the set of shortcuts here
2926 Set<String> newApps = new HashSet<String>();
2927 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
2928
2929 Workspace workspace = mWorkspace;
2930 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002931 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002932
2933 // Short circuit if we are loading dock items for a configuration which has no dock
2934 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2935 mHotseat == null) {
2936 continue;
2937 }
2938
Joe Onorato9c1289c2009-08-17 11:03:03 -04002939 switch (item.itemType) {
2940 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2941 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07002942 ShortcutInfo info = (ShortcutInfo) item;
2943 String uri = info.intent.toUri(0).toString();
2944 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07002945 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2946 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07002947 if (newApps.contains(uri)) {
2948 newApps.remove(uri);
2949
2950 // Prepare the view to be animated up
2951 shortcut.setAlpha(0f);
2952 shortcut.setScaleX(0f);
2953 shortcut.setScaleY(0f);
2954 mNewShortcutAnimatePage = item.screen;
2955 if (!mNewShortcutAnimateViews.contains(shortcut)) {
2956 mNewShortcutAnimateViews.add(shortcut);
2957 }
2958 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002959 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002960 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002961 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002962 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002963 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002964 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2965 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002966 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002967 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002969
Joe Onorato9c1289c2009-08-17 11:03:03 -04002970 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 }
2972
Joe Onorato9c1289c2009-08-17 11:03:03 -04002973 /**
2974 * Implementation of the method from LauncherModel.Callbacks.
2975 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002976 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002977 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002978 sFolders.clear();
2979 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002980 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002981
2982 /**
2983 * Add the views for a widget to the workspace.
2984 *
2985 * Implementation of the method from LauncherModel.Callbacks.
2986 */
2987 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002988 setLoadOnResume();
2989
Daniel Sandler843e8602010-06-07 14:59:01 -04002990 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2991 if (DEBUG_WIDGETS) {
2992 Log.d(TAG, "bindAppWidget: " + item);
2993 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002994 final Workspace workspace = mWorkspace;
2995
2996 final int appWidgetId = item.appWidgetId;
2997 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002998 if (DEBUG_WIDGETS) {
2999 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3000 }
3001
Joe Onorato9c1289c2009-08-17 11:03:03 -04003002 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3003
Joe Onorato9c1289c2009-08-17 11:03:03 -04003004 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
3005 item.hostView.setTag(item);
3006
Winson Chung3d503fb2011-07-13 17:25:49 -07003007 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003008 item.cellY, item.spanX, item.spanY, false);
3009
Adam Cohended9f8d2010-11-03 13:25:16 -07003010 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3011
Joe Onorato9c1289c2009-08-17 11:03:03 -04003012 workspace.requestLayout();
3013
Daniel Sandler843e8602010-06-07 14:59:01 -04003014 if (DEBUG_WIDGETS) {
3015 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3016 + (SystemClock.uptimeMillis()-start) + "ms");
3017 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003018 }
3019
3020 /**
3021 * Callback saying that there aren't any more items to bind.
3022 *
3023 * Implementation of the method from LauncherModel.Callbacks.
3024 */
3025 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003026 setLoadOnResume();
3027
Joe Onorato9c1289c2009-08-17 11:03:03 -04003028 if (mSavedState != null) {
3029 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003030 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003031 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003032 mSavedState = null;
3033 }
3034
3035 if (mSavedInstanceState != null) {
3036 super.onRestoreInstanceState(mSavedInstanceState);
3037 mSavedInstanceState = null;
3038 }
3039
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003040 // If we received the result of any pending adds while the loader was running (e.g. the
3041 // widget configuration forced an orientation change), process them now.
3042 for (int i = 0; i < sPendingAddList.size(); i++) {
3043 completeAdd(sPendingAddList.get(i));
3044 }
3045 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003046
Winson Chungc51db6a2011-10-05 11:44:49 -07003047 // Update the market app icon as necessary (the other icons will be managed in response to
3048 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003049 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003050
Winson Chungf0c6ae02012-03-21 16:10:31 -07003051 // Animate up any icons as necessary
3052 if (mVisible || mWorkspaceLoading) {
3053 Runnable newAppsRunnable = new Runnable() {
3054 @Override
3055 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003056 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003057 }
3058 };
Winson Chunga2413752012-04-03 14:22:34 -07003059
3060 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3061 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3062 if (canRunNewAppsAnimation()) {
3063 // If the user has not interacted recently, then either snap to the new page to show
3064 // the new-apps animation or just run them if they are to appear on the current page
3065 if (willSnapPage) {
3066 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3067 } else {
3068 runNewAppsAnimation(false);
3069 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003070 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003071 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003072 // are on another page (or just normally if they are added to the current page)
3073 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003074 }
3075 }
3076
3077 mWorkspaceLoading = false;
3078 }
3079
Winson Chunga2413752012-04-03 14:22:34 -07003080 private boolean canRunNewAppsAnimation() {
3081 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3082 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3083 }
3084
Winson Chungf0c6ae02012-03-21 16:10:31 -07003085 /**
3086 * Runs a new animation that scales up icons that were added while Launcher was in the
3087 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003088 *
3089 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003090 */
Winson Chunga2413752012-04-03 14:22:34 -07003091 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003092 AnimatorSet anim = new AnimatorSet();
3093 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003094
3095 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003096 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3097 @Override
3098 public int compare(View a, View b) {
3099 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3100 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3101 int cellCountX = LauncherModel.getCellCountX();
3102 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3103 }
3104 });
Winson Chunga2413752012-04-03 14:22:34 -07003105
3106 // Animate each of the views in place (or show them immediately if requested)
3107 if (immediate) {
3108 for (View v : mNewShortcutAnimateViews) {
3109 v.setAlpha(1f);
3110 v.setScaleX(1f);
3111 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003112 }
Winson Chunga2413752012-04-03 14:22:34 -07003113 } else {
3114 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3115 View v = mNewShortcutAnimateViews.get(i);
3116 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3117 PropertyValuesHolder.ofFloat("alpha", 1f),
3118 PropertyValuesHolder.ofFloat("scaleX", 1f),
3119 PropertyValuesHolder.ofFloat("scaleY", 1f));
3120 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3121 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3122 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3123 bounceAnims.add(bounceAnim);
3124 }
3125 anim.playTogether(bounceAnims);
3126 anim.addListener(new AnimatorListenerAdapter() {
3127 @Override
3128 public void onAnimationEnd(Animator animation) {
3129 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3130 }
3131 });
3132 anim.start();
3133 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003134
3135 // Clean up
3136 mNewShortcutAnimatePage = -1;
3137 mNewShortcutAnimateViews.clear();
3138 new Thread("clearNewAppsThread") {
3139 public void run() {
3140 mSharedPrefs.edit()
3141 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3142 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3143 .commit();
3144 }
3145 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003146 }
3147
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003148 @Override
3149 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003150 boolean searchVisible = updateGlobalSearchIcon();
3151 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3152 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003153 }
3154
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003155 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003156 * Add the icons for all apps.
3157 *
3158 * Implementation of the method from LauncherModel.Callbacks.
3159 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003160 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3161 // Remove the progress bar entirely; we could also make it GONE
3162 // but better to remove it since we know it's not going to be used
3163 View progressBar = mAppsCustomizeTabHost.
3164 findViewById(R.id.apps_customize_progress_bar);
3165 if (progressBar != null) {
3166 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003167 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003168 // We just post the call to setApps so the user sees the progress bar
3169 // disappear-- otherwise, it just looks like the progress bar froze
3170 // which doesn't look great
3171 mAppsCustomizeTabHost.post(new Runnable() {
3172 public void run() {
3173 if (mAppsCustomizeContent != null) {
3174 mAppsCustomizeContent.setApps(apps);
3175 }
3176 }
3177 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003178 }
3179
3180 /**
3181 * A package was installed.
3182 *
3183 * Implementation of the method from LauncherModel.Callbacks.
3184 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003185 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003186 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003187
Winson Chung785d2eb2011-04-14 16:08:02 -07003188 if (mAppsCustomizeContent != null) {
3189 mAppsCustomizeContent.addApps(apps);
3190 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003191 }
3192
3193 /**
3194 * A package was updated.
3195 *
3196 * Implementation of the method from LauncherModel.Callbacks.
3197 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003198 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003199 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003200 if (mWorkspace != null) {
3201 mWorkspace.updateShortcuts(apps);
3202 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003203
Winson Chung785d2eb2011-04-14 16:08:02 -07003204 if (mAppsCustomizeContent != null) {
3205 mAppsCustomizeContent.updateApps(apps);
3206 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003207 }
3208
3209 /**
3210 * A package was uninstalled.
3211 *
3212 * Implementation of the method from LauncherModel.Callbacks.
3213 */
Joe Onorato36115782010-06-17 13:28:48 -04003214 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003215 if (permanent) {
3216 mWorkspace.removeItems(apps);
3217 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003218
Winson Chung785d2eb2011-04-14 16:08:02 -07003219 if (mAppsCustomizeContent != null) {
3220 mAppsCustomizeContent.removeApps(apps);
3221 }
Winson Chunga1820962011-10-03 16:31:06 -07003222
3223 // Notify the drag controller
3224 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003225 }
Joe Onoratobe386092009-11-17 17:32:16 -08003226
3227 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003228 * A number of packages were updated.
3229 */
3230 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003231 if (mAppsCustomizeContent != null) {
3232 mAppsCustomizeContent.onPackagesUpdated();
3233 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003234 }
3235
Winson Chung400438b2011-01-16 17:53:48 -08003236 private int mapConfigurationOriActivityInfoOri(int configOri) {
3237 final Display d = getWindowManager().getDefaultDisplay();
3238 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3239 switch (d.getRotation()) {
3240 case Surface.ROTATION_0:
3241 case Surface.ROTATION_180:
3242 // We are currently in the same basic orientation as the natural orientation
3243 naturalOri = configOri;
3244 break;
3245 case Surface.ROTATION_90:
3246 case Surface.ROTATION_270:
3247 // We are currently in the other basic orientation to the natural orientation
3248 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3249 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3250 break;
3251 }
3252
3253 int[] oriMap = {
3254 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3255 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3256 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3257 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3258 };
3259 // Since the map starts at portrait, we need to offset if this device's natural orientation
3260 // is landscape.
3261 int indexOffset = 0;
3262 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3263 indexOffset = 1;
3264 }
3265 return oriMap[(d.getRotation() + indexOffset) % 4];
3266 }
Adam Cohen446e9402011-09-15 18:21:21 -07003267
3268 public void lockScreenOrientationOnLargeUI() {
3269 if (LauncherApplication.isScreenLarge()) {
3270 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3271 .getConfiguration().orientation));
3272 }
Winson Chung400438b2011-01-16 17:53:48 -08003273 }
Adam Cohen446e9402011-09-15 18:21:21 -07003274 public void unlockScreenOrientationOnLargeUI() {
3275 if (LauncherApplication.isScreenLarge()) {
3276 mHandler.postDelayed(new Runnable() {
3277 public void run() {
3278 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3279 }
3280 }, mRestoreScreenOrientationDelay);
3281 }
Winson Chung400438b2011-01-16 17:53:48 -08003282 }
3283
Winson Chung82f55532011-08-09 14:14:23 -07003284 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003285 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003286 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003287 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003288
Winson Chung7d7541e2011-09-16 20:14:36 -07003289 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003290 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003291 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3292 Cling cling = (Cling) findViewById(clingId);
3293 if (cling != null) {
3294 cling.init(this, positionData);
3295 cling.setVisibility(View.VISIBLE);
3296 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3297 if (animate) {
3298 cling.buildLayer();
3299 cling.setAlpha(0f);
3300 cling.animate()
3301 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003302 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003303 .setDuration(SHOW_CLING_DURATION)
3304 .setStartDelay(delay)
3305 .start();
3306 } else {
3307 cling.setAlpha(1f);
3308 }
3309 }
3310 return cling;
3311 }
3312 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003313 if (cling != null) {
3314 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003315 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003316 anim.addListener(new AnimatorListenerAdapter() {
3317 public void onAnimationEnd(Animator animation) {
3318 cling.setVisibility(View.GONE);
3319 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003320 // We should update the shared preferences on a background thread
3321 new Thread("dismissClingThread") {
3322 public void run() {
3323 SharedPreferences.Editor editor = mSharedPrefs.edit();
3324 editor.putBoolean(flag, true);
3325 editor.commit();
3326 }
3327 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003328 };
3329 });
3330 anim.start();
3331 }
3332 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003333 private void removeCling(int id) {
3334 final View cling = findViewById(id);
3335 if (cling != null) {
3336 final ViewGroup parent = (ViewGroup) cling.getParent();
3337 parent.post(new Runnable() {
3338 @Override
3339 public void run() {
3340 parent.removeView(cling);
3341 }
3342 });
3343 }
3344 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003345 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003346 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003347 if (isClingsEnabled() &&
3348 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003349 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003350 } else {
3351 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003352 }
3353 }
3354 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003355 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003356 if (isClingsEnabled() &&
3357 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003358 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003359 } else {
3360 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003361 }
3362 }
3363 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003364 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003365 if (isClingsEnabled() &&
3366 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3367 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003368 } else {
3369 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003370 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003371 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003372 }
3373 public boolean isFolderClingVisible() {
3374 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003375 if (cling != null) {
3376 return cling.getVisibility() == View.VISIBLE;
3377 }
3378 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003379 }
Winson Chung82f55532011-08-09 14:14:23 -07003380 public void dismissWorkspaceCling(View v) {
3381 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003382 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003383 }
3384 public void dismissAllAppsCling(View v) {
3385 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003386 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3387 }
3388 public void dismissFolderCling(View v) {
3389 Cling cling = (Cling) findViewById(R.id.folder_cling);
3390 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003391 }
3392
Winson Chung80baf5a2010-08-09 16:03:15 -07003393 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003394 * Prints out out state for debugging.
3395 */
3396 public void dumpState() {
3397 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003398 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003399 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3400 Log.d(TAG, "mRestoring=" + mRestoring);
3401 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3402 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003403 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003404 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003405
Winson Chung785d2eb2011-04-14 16:08:02 -07003406 if (mAppsCustomizeContent != null) {
3407 mAppsCustomizeContent.dumpState();
3408 }
Joe Onoratobe386092009-11-17 17:32:16 -08003409 Log.d(TAG, "END launcher2 dump state");
3410 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003411
3412 @Override
3413 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3414 super.dump(prefix, fd, writer, args);
3415 writer.println(" ");
3416 writer.println("Debug logs: ");
3417 for (int i = 0; i < sDumpLogs.size(); i++) {
3418 writer.println(" " + sDumpLogs.get(i));
3419 }
3420 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003421}
Michael Jurka2763be32011-02-24 11:19:57 -08003422
Michael Jurkaabded662011-03-04 12:06:57 -08003423interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003424 View getContent();
Michael Jurkabed61d22012-02-14 22:51:29 -08003425 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003426 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003427 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003428}