blob: 71fd278562f54615f2e0ef73ab4188dec8f8803b [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080029import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080030import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070031import android.appwidget.AppWidgetManager;
32import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080033import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080034import android.content.BroadcastReceiver;
Winson Chung68846fd2010-10-29 11:00:27 -070035import android.content.ClipData;
36import android.content.ClipDescription;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070042import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070043import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070044import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080045import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070046import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080047import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070048import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080049import android.database.ContentObserver;
Michael Jurkaaf442092010-06-10 17:01:57 -070050import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070051import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040052import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070053import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080054import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020055import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080056import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070057import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070058import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040059import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080060import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070061import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080062import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.text.Selection;
64import android.text.SpannableStringBuilder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080065import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070066import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080067import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080068import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.view.KeyEvent;
70import android.view.LayoutInflater;
71import android.view.Menu;
72import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070073import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080074import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080075import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080076import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070077import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080078import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080079import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070080import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080081import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070082import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070083import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080084import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080085import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070086import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070087import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070088import android.widget.TextView;
89import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070090
Adam Cohendf2cc412011-04-27 16:56:57 -070091import com.android.common.Search;
92import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070093import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080094
Adam Cohenc0dcf592011-06-01 15:30:43 -070095import java.io.DataInputStream;
96import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -070097import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -070098import java.io.FileNotFoundException;
99import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700100import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700101import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700102import java.util.Collection;
103import java.util.Collections;
104import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700106import java.util.HashSet;
107import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800109/**
110 * Default launcher application.
111 */
Michael Jurka946ad472010-07-09 18:05:18 -0700112public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700113 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
114 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800115 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700116 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117
Joe Onorato9c1289c2009-08-17 11:03:03 -0400118 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400119 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700120 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700121
Winson Chung70d72102011-08-12 11:24:35 -0700122 private static final int MENU_GROUP_WALLPAPER = 1;
123 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
124 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700125 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
126 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800127
128 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700129 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130 private static final int REQUEST_PICK_APPLICATION = 6;
131 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700132 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700133 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
136
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800137 static final int SCREEN_COUNT = 5;
138 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Romain Guy98d01652009-06-30 16:21:04 -0700140 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800141 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
143 // Type: int
144 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700145 // Type: int
146 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700148 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
149 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
151 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700152 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700154 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 // Type: boolean
156 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
157 // Type: long
158 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
159
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700160 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
161
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700162 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700163 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700164 private State mState = State.WORKSPACE;
165 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800166 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700167
Joe Onorato9c1289c2009-08-17 11:03:03 -0400168 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700169 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
170 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700171 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700172 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800175 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Winson Chunga2413752012-04-03 14:22:34 -0700177 // How long to wait before the new-shortcut animation automatically pans the workspace
178 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
179
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800180 private final BroadcastReceiver mCloseSystemDialogsReceiver
181 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800182 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 private LayoutInflater mInflater;
185
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800187 private View mQsbDivider;
188 private View mDockDivider;
189 private DragLayer mDragLayer;
190 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700191
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700192 private AppWidgetManager mAppWidgetManager;
193 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700194
Michael Jurkac9d95c52011-08-29 14:03:34 -0700195 private ItemInfo mPendingAddInfo = new ItemInfo();
Michael Jurka0280c3b2010-09-17 15:00:07 -0700196 private int[] mTmpAddItemCellCoordinates = new int[2];
197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private FolderInfo mFolderInfo;
199
Winson Chung3d503fb2011-07-13 17:25:49 -0700200 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800201 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700202
Winson Chungc51db6a2011-10-05 11:44:49 -0700203 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700204 private AppsCustomizeTabHost mAppsCustomizeTabHost;
205 private AppsCustomizePagedView mAppsCustomizeContent;
206 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private Bundle mSavedState;
209
210 private SpannableStringBuilder mDefaultKeySsb = null;
211
Joe Onorato9c1289c2009-08-17 11:03:03 -0400212 private boolean mWorkspaceLoading = true;
213
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800214 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800215 private boolean mRestoring;
216 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700217 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800218
219 private Bundle mSavedInstanceState;
220
Joe Onorato9c1289c2009-08-17 11:03:03 -0400221 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800222 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800223 private boolean mUserPresent = true;
224 private boolean mVisible = false;
225 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400226
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700227 private static LocaleConfiguration sLocaleConfiguration = null;
228
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700229 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700230
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700231 private Intent mAppMarketIntent = null;
232
Adam Cohended9f8d2010-11-03 13:25:16 -0700233 // Related to the auto-advancing of widgets
234 private final int ADVANCE_MSG = 1;
235 private final int mAdvanceInterval = 20000;
236 private final int mAdvanceStagger = 250;
237 private long mAutoAdvanceSentTime;
238 private long mAutoAdvanceTimeLeft = -1;
239 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
240 new HashMap<View, AppWidgetProviderInfo>();
241
Winson Chung400438b2011-01-16 17:53:48 -0800242 // Determines how long to wait after a rotation before restoring the screen orientation to
243 // match the sensor state.
244 private final int mRestoreScreenOrientationDelay = 500;
245
Michael Jurka4ef207b2010-11-29 17:05:45 -0800246 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700247 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
248 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
249 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800250
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700251 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohened66b2b2012-01-23 17:28:51 -0800252 PendingAddWidgetInfo mWidgetBeingConfigured = null;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700253
Winson Chung46353de2012-02-16 14:05:10 -0800254 // We only want to get the SharedPreferences once since it does an FS stat each time we get
255 // it from the context.
256 private SharedPreferences mSharedPrefs;
257
Winson Chungf0c6ae02012-03-21 16:10:31 -0700258 // Holds the page that we need to animate to, and the icon views that we need to animate up
259 // when we scroll to that page on resume.
260 private int mNewShortcutAnimatePage = -1;
261 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen2801caf2011-05-13 20:57:39 -0700262
Michael Jurkaddd62e92011-02-16 17:49:14 -0800263 private BubbleTextView mWaitingForResume;
264
Michael Jurkac1f5d262011-09-30 19:32:27 -0700265 private Runnable mBuildLayersRunnable = new Runnable() {
266 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700267 if (mWorkspace != null) {
268 mWorkspace.buildPageHardwareLayers();
269 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700270 }
271 };
272
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800273 private static ArrayList<PendingAddArguments> sPendingAddList
274 = new ArrayList<PendingAddArguments>();
275
276 private static class PendingAddArguments {
277 int requestCode;
278 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700279 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800280 int screen;
281 int cellX;
282 int cellY;
283 }
284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 @Override
286 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700287 if (DEBUG_STRICT_MODE) {
288 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
289 .detectDiskReads()
290 .detectDiskWrites()
291 .detectNetwork() // or .detectAll() for all detectable problems
292 .penaltyLog()
293 .build());
294 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
295 .detectLeakedSqlLiteObjects()
296 .detectLeakedClosableObjects()
297 .penaltyLog()
298 .penaltyDeath()
299 .build());
300 }
301
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800302 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800303 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700304 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
305 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800306 mModel = app.setLauncher(this);
307 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400308 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800309 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700310
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700311 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700312 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
313 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700314
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800315 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200316 android.os.Debug.startMethodTracing(
317 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800318 }
319
320 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800321 setContentView(R.layout.launcher);
322 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700323 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800324
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800325 registerContentObservers();
326
Joe Onorato7c312c12009-08-13 21:36:53 -0700327 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700328
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800329 mSavedState = savedInstanceState;
330 restoreState(mSavedState);
331
Winson Chunga12a2502010-12-20 14:41:35 -0800332 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700333 if (mAppsCustomizeContent != null) {
334 mAppsCustomizeContent.onPackagesUpdated();
335 }
Winson Chunga12a2502010-12-20 14:41:35 -0800336
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 if (PROFILE_STARTUP) {
338 android.os.Debug.stopMethodTracing();
339 }
340
341 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700342 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800343 }
344
Michael Jurkac57b7a82011-08-09 22:02:20 -0700345 if (!mModel.isAllAppsLoaded()) {
346 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
347 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
348 }
349
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800350 // For handling default keys
351 mDefaultKeySsb = new SpannableStringBuilder();
352 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800353
354 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
355 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800356
Winson Chungc51db6a2011-10-05 11:44:49 -0700357 boolean searchVisible = false;
358 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800359 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700360 int coi = getCurrentOrientationIndexForGlobalIcons();
361 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
362 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700363 updateAppMarketIcon();
364 searchVisible = updateGlobalSearchIcon();
365 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700366 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700367 if (sGlobalSearchIcon[coi] != null) {
368 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700369 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700370 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700371 if (sVoiceSearchIcon[coi] != null) {
372 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700373 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700374 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700375 if (sAppMarketIcon[coi] != null) {
376 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800377 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700378 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Adam Cohen446e9402011-09-15 18:21:21 -0700379
Adam Cohen23a4d302011-12-01 17:26:44 -0800380 final String forceEnableRotation =
381 SystemProperties.get(FORCE_ENABLE_ROTATION_PROPERTY, "false");
382
Adam Cohencb3f98e2012-04-24 14:24:35 -0700383 boolean enableRotation = getResources().getBoolean(R.bool.allow_rotation);
384
Adam Cohen446e9402011-09-15 18:21:21 -0700385 // On large interfaces, we want the screen to auto-rotate based on the current orientation
Adam Cohencb3f98e2012-04-24 14:24:35 -0700386 if (enableRotation || "true".equalsIgnoreCase(forceEnableRotation)) {
Adam Cohen446e9402011-09-15 18:21:21 -0700387 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
388 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 }
Romain Guycbb89e42009-06-08 15:52:54 -0700390
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700392 if (sLocaleConfiguration == null) {
393 new AsyncTask<Void, Void, LocaleConfiguration>() {
394 @Override
395 protected LocaleConfiguration doInBackground(Void... unused) {
396 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
397 readConfiguration(Launcher.this, localeConfiguration);
398 return localeConfiguration;
399 }
400
401 @Override
402 protected void onPostExecute(LocaleConfiguration result) {
403 sLocaleConfiguration = result;
404 checkForLocaleChange(); // recursive, but now with a locale configuration
405 }
406 }.execute();
407 return;
408 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 final Configuration configuration = getResources().getConfiguration();
411
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700412 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 final String locale = configuration.locale.toString();
414
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700415 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 final int mcc = configuration.mcc;
417
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700418 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800419 final int mnc = configuration.mnc;
420
Romain Guy84f296c2009-11-04 15:00:44 -0800421 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800422
Romain Guy84f296c2009-11-04 15:00:44 -0800423 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700424 sLocaleConfiguration.locale = locale;
425 sLocaleConfiguration.mcc = mcc;
426 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700427
Joe Onorato0589f0f2010-02-08 13:44:00 -0800428 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700429
430 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
431 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700432 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700433 public void run() {
434 writeConfiguration(Launcher.this, localeConfiguration);
435 }
436 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700437 }
438 }
439
440 private static class LocaleConfiguration {
441 public String locale;
442 public int mcc = -1;
443 public int mnc = -1;
444 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700445
Romain Guy98d01652009-06-30 16:21:04 -0700446 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
447 DataInputStream in = null;
448 try {
449 in = new DataInputStream(context.openFileInput(PREFERENCES));
450 configuration.locale = in.readUTF();
451 configuration.mcc = in.readInt();
452 configuration.mnc = in.readInt();
453 } catch (FileNotFoundException e) {
454 // Ignore
455 } catch (IOException e) {
456 // Ignore
457 } finally {
458 if (in != null) {
459 try {
460 in.close();
461 } catch (IOException e) {
462 // Ignore
463 }
464 }
465 }
466 }
467
468 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
469 DataOutputStream out = null;
470 try {
471 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
472 out.writeUTF(configuration.locale);
473 out.writeInt(configuration.mcc);
474 out.writeInt(configuration.mnc);
475 out.flush();
476 } catch (FileNotFoundException e) {
477 // Ignore
478 } catch (IOException e) {
479 //noinspection ResultOfMethodCallIgnored
480 context.getFileStreamPath(PREFERENCES).delete();
481 } finally {
482 if (out != null) {
483 try {
484 out.close();
485 } catch (IOException e) {
486 // Ignore
487 }
488 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 }
490 }
491
Adam Cohen716b51e2011-06-30 12:09:54 -0700492 public DragLayer getDragLayer() {
493 return mDragLayer;
494 }
495
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496 static int getScreen() {
497 synchronized (sLock) {
498 return sScreen;
499 }
500 }
501
502 static void setScreen(int screen) {
503 synchronized (sLock) {
504 sScreen = screen;
505 }
506 }
507
Winson Chung557d6ed2011-07-08 15:34:52 -0700508 /**
509 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
510 * a configuration step, this allows the proper animations to run after other transitions.
511 */
512 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700513 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800514 switch (args.requestCode) {
515 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700516 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
517 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800518 break;
519 case REQUEST_PICK_SHORTCUT:
520 processShortcut(args.intent);
521 break;
522 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700523 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
524 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700525 result = true;
526 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800527 case REQUEST_PICK_APPWIDGET:
528 addAppWidgetFromPick(args.intent);
529 break;
530 case REQUEST_CREATE_APPWIDGET:
531 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800532 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700533 result = true;
534 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800535 case REQUEST_PICK_WALLPAPER:
536 // We just wanted the activity result here so we can clear mWaitingForResult
537 break;
538 }
Michael Jurka27614d22012-04-02 06:40:22 -0700539 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
540 // if you turned the screen off and then back while in All Apps, Launcher would not
541 // return to the workspace. Clearing mAddInfo.container here fixes this issue
542 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700543 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800544 }
545
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 @Override
Adam Cohened66b2b2012-01-23 17:28:51 -0800547 protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
Winson Chung557d6ed2011-07-08 15:34:52 -0700548 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800549 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
550 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700551 mWaitingForResult = false;
552
Adam Cohened66b2b2012-01-23 17:28:51 -0800553 // We have special handling for widgets
554 if (isWidgetDrop) {
555 int appWidgetId = data != null ?
556 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
557 completeTwoStageWidgetDrop(resultCode, appWidgetId);
558 return;
559 }
560
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 // The pattern used here is that a user PICKs a specific application,
562 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700563
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800564 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
565 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700566 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800567 final PendingAddArguments args = new PendingAddArguments();
568 args.requestCode = requestCode;
569 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700570 args.container = mPendingAddInfo.container;
571 args.screen = mPendingAddInfo.screen;
572 args.cellX = mPendingAddInfo.cellX;
573 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800574 if (isWorkspaceLocked()) {
575 sPendingAddList.add(args);
576 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700577 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800580 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700581 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800582 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
583 null);
584 }
585
586 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
587 CellLayout cellLayout = (CellLayout) mWorkspace.getChildAt(mWidgetBeingConfigured.screen);
588 Runnable onCompleteRunnable = null;
589 int animationType = 0;
590
591 if (resultCode == RESULT_OK) {
592 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
593 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
594 mWidgetBeingConfigured.info);
595 mWidgetBeingConfigured.boundWidget = layout;
596 onCompleteRunnable = new Runnable() {
597 @Override
598 public void run() {
599 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
600 mPendingAddInfo.screen, layout, null);
601 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
602 null);
603 }
604 };
605 } else if (resultCode == RESULT_CANCELED) {
606 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
607 onCompleteRunnable = new Runnable() {
608 @Override
609 public void run() {
610 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
611 null);
612 }
613 };
614 }
Adam Cohend41fbf52012-02-16 23:53:59 -0800615 mWorkspace.animateWidgetDrop(mWidgetBeingConfigured, cellLayout,
Adam Cohened66b2b2012-01-23 17:28:51 -0800616 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
Adam Cohend41fbf52012-02-16 23:53:59 -0800617 animationType, mWidgetBeingConfigured.boundWidget, true);
Adam Cohen1b36dc32012-02-13 19:27:37 -0800618 mWidgetBeingConfigured = null;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800619 }
620
621 @Override
622 protected void onResume() {
623 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700624
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800625 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700626 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400627 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700628 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400629 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700630 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 }
Winson Chunge4e50662012-01-23 14:45:13 -0800632
633 // Reset the pressed state of icons that were locked in the press state while activities
634 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800635 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800636 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800637 mWaitingForResume.setStayPressed(false);
638 }
Winson Chunge4e50662012-01-23 14:45:13 -0800639 if (mAppsCustomizeContent != null) {
640 // Resets the previous all apps icon press state
641 mAppsCustomizeContent.resetDrawableState();
642 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 }
644
645 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700646 protected void onPause() {
647 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700648 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800649 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700650 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700651 }
Romain Guycbb89e42009-06-08 15:52:54 -0700652
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700653 @Override
654 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400655 // Flag the loader to stop early before switching
656 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700657 if (mAppsCustomizeContent != null) {
658 mAppsCustomizeContent.surrender();
659 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800660 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700661 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700662
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800663 // We can't hide the IME if it was forced open. So don't bother
664 /*
665 @Override
666 public void onWindowFocusChanged(boolean hasFocus) {
667 super.onWindowFocusChanged(hasFocus);
668
669 if (hasFocus) {
670 final InputMethodManager inputManager = (InputMethodManager)
671 getSystemService(Context.INPUT_METHOD_SERVICE);
672 WindowManager.LayoutParams lp = getWindow().getAttributes();
673 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
674 android.os.Handler()) {
675 protected void onReceiveResult(int resultCode, Bundle resultData) {
676 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
677 }
678 });
679 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
680 }
681 }
682 */
683
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 private boolean acceptFilter() {
685 final InputMethodManager inputManager = (InputMethodManager)
686 getSystemService(Context.INPUT_METHOD_SERVICE);
687 return !inputManager.isFullscreenMode();
688 }
689
690 @Override
691 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700692 final int uniChar = event.getUnicodeChar();
693 final boolean handled = super.onKeyDown(keyCode, event);
694 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
695 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
697 keyCode, event);
698 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700699 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700700 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700701 // showSearchDialog()
702 // If there are multiple keystrokes before the search dialog takes focus,
703 // onSearchRequested() will be called for every keystroke,
704 // but it is idempotent, so it's fine.
705 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800706 }
707 }
708
Joe Onorato8a9625e2010-01-28 15:55:35 -0800709 // Eat the long press event so the keyboard doesn't come up.
710 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
711 return true;
712 }
713
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 return handled;
715 }
716
Karl Rosaen138a0412009-04-23 19:00:21 -0700717 private String getTypedText() {
718 return mDefaultKeySsb.toString();
719 }
720
721 private void clearTypedText() {
722 mDefaultKeySsb.clear();
723 mDefaultKeySsb.clearSpans();
724 Selection.setSelection(mDefaultKeySsb, 0);
725 }
726
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700728 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
729 * State
730 */
731 private static State intToState(int stateOrdinal) {
732 State state = State.WORKSPACE;
733 final State[] stateValues = State.values();
734 for (int i = 0; i < stateValues.length; i++) {
735 if (stateValues[i].ordinal() == stateOrdinal) {
736 state = stateValues[i];
737 break;
738 }
739 }
740 return state;
741 }
742
743 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800744 * Restores the previous state, if it exists.
745 *
746 * @param savedState The previous state.
747 */
748 private void restoreState(Bundle savedState) {
749 if (savedState == null) {
750 return;
751 }
752
Michael Jurka883f55b2010-10-21 15:47:14 -0700753 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700754 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800755 showAllApps(false);
756 }
757
Winson Chungf0c6ae02012-03-21 16:10:31 -0700758 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700760 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 }
762
Winson Chung3d503fb2011-07-13 17:25:49 -0700763 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
764 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700765
Winson Chung3d503fb2011-07-13 17:25:49 -0700766 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
767 mPendingAddInfo.container = pendingAddContainer;
768 mPendingAddInfo.screen = pendingAddScreen;
769 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
770 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 mRestoring = true;
772 }
773
774 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
775 if (renameFolder) {
776 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700777 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 mRestoring = true;
779 }
Winson Chunga12a2502010-12-20 14:41:35 -0800780
Winson Chung785d2eb2011-04-14 16:08:02 -0700781
782 // Restore the AppsCustomize tab
783 if (mAppsCustomizeTabHost != null) {
784 String curTab = savedState.getString("apps_customize_currentTab");
785 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700786 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700787 mAppsCustomizeContent.setContentType(
788 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
789 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700790 mAppsCustomizeContent.loadAssociatedPages(
791 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700792 }
793
Winson Chung5afbf7b2011-07-25 11:53:08 -0700794 int currentIndex = savedState.getInt("apps_customize_currentIndex");
795 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700796 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800797 }
798
799 /**
800 * Finds all the views we need and configure them properly.
801 */
802 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700803 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400804
Winson Chung4c98d922011-05-31 16:50:48 -0700805 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
806 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800807 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
808 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809
Winson Chung4c98d922011-05-31 16:50:48 -0700810 // Setup the drag layer
811 mDragLayer.setup(this, dragController);
812
Winson Chung3d503fb2011-07-13 17:25:49 -0700813 // Setup the hotseat
814 mHotseat = (Hotseat) findViewById(R.id.hotseat);
815 if (mHotseat != null) {
816 mHotseat.setup(this);
817 }
818
Winson Chung4c98d922011-05-31 16:50:48 -0700819 // Setup the workspace
820 mWorkspace.setHapticFeedbackEnabled(false);
821 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700822 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700823 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700824
Winson Chungf0ea4d32011-06-06 14:27:16 -0700825 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700826 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700827
Winson Chungf0ea4d32011-06-06 14:27:16 -0700828 // Setup AppsCustomize
829 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
830 findViewById(R.id.apps_customize_pane);
831 mAppsCustomizeContent = (AppsCustomizePagedView)
832 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
833 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()));
Michael Jurkaddd62e92011-02-16 17:49:14 -08001771 boolean success = startActivitySafely(intent, tag);
1772
1773 if (success && v instanceof BubbleTextView) {
1774 mWaitingForResume = (BubbleTextView) v;
1775 mWaitingForResume.setStayPressed(true);
1776 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001777 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001778 if (v instanceof FolderIcon) {
1779 FolderIcon fi = (FolderIcon) v;
1780 handleFolderClick(fi);
1781 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001782 } else if (v == mAllAppsButton) {
1783 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001784 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001785 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001786 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001787 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 }
1789 }
1790
Michael Jurka0e260592010-06-30 17:07:39 -07001791 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001792 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001793 // clicking anywhere on the workspace causes the customization drawer to slide down
1794 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001795 return false;
1796 }
1797
Michael Jurkaaf442092010-06-10 17:01:57 -07001798 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001799 * Event handler for the search button
1800 *
1801 * @param v The view that was clicked.
1802 */
1803 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001804 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1805
Amith Yamasania135ba82011-08-09 17:42:01 -07001806 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001807 }
1808
1809 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001810 * Event handler for the voice button
1811 *
1812 * @param v The view that was clicked.
1813 */
1814 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001815 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001816
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001817 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001818 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001819 startActivity(intent);
1820 }
1821
1822 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001823 * Event handler for the "grid" button that appears on the home screen, which
1824 * enters all apps mode.
1825 *
1826 * @param v The view that was clicked.
1827 */
1828 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001829 showAllApps(true);
1830 }
1831
1832 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001833 // Provide the same haptic feedback that the system offers for virtual keys.
1834 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001835 }
1836
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001837 public void onClickAppMarketButton(View v) {
1838 if (mAppMarketIntent != null) {
1839 startActivitySafely(mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001840 } else {
1841 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001842 }
1843 }
1844
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001845 void startApplicationDetailsActivity(ComponentName componentName) {
1846 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001847 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1848 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001849 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001850 startActivity(intent);
1851 }
1852
Patrick Dubroy5539af72010-09-07 15:22:01 -07001853 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1854 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1855 // System applications cannot be installed. For now, show a toast explaining that.
1856 // We may give them the option of disabling apps this way.
1857 int messageId = R.string.uninstall_system_app_text;
1858 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1859 } else {
1860 String packageName = appInfo.componentName.getPackageName();
1861 String className = appInfo.componentName.getClassName();
1862 Intent intent = new Intent(
1863 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001864 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1865 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001866 startActivity(intent);
1867 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001868 }
1869
Michael Jurkaddd62e92011-02-16 17:49:14 -08001870 boolean startActivitySafely(Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1872 try {
1873 startActivity(intent);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001874 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001875 } catch (ActivityNotFoundException e) {
1876 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Daniel Sandlerc9b18772010-04-22 14:37:59 -04001877 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 } catch (SecurityException e) {
1879 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001880 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001881 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07001882 "or use the exported attribute for this activity. "
1883 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001884 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001885 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 }
Winson Chungaafa03c2010-06-11 17:34:16 -07001887
Romain Guy8e633c52010-03-04 12:51:36 -08001888 void startActivityForResultSafely(Intent intent, int requestCode) {
1889 try {
1890 startActivityForResult(intent, requestCode);
1891 } catch (ActivityNotFoundException e) {
1892 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1893 } catch (SecurityException e) {
1894 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
1895 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
1896 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
1897 "or use the exported attribute for this activity.", e);
1898 }
1899 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900
Adam Cohena9cf38f2011-05-02 15:36:58 -07001901 private void handleFolderClick(FolderIcon folderIcon) {
1902 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07001903 Folder openFolder = mWorkspace.getFolderForTag(info);
1904
1905 // If the folder info reports that the associated folder is open, then verify that
1906 // it is actually opened. There have been a few instances where this gets out of sync.
1907 if (info.opened && openFolder == null) {
1908 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
1909 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
1910 info.opened = false;
1911 }
1912
Adam Cohena9cf38f2011-05-02 15:36:58 -07001913 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001914 // Close any open folder
1915 closeFolder();
1916 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07001917 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 } else {
1919 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001920 int folderScreen;
1921 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07001922 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001923 // .. and close it
1924 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07001925 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001926 // Close any folder open on the current screen
1927 closeFolder();
1928 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07001929 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 }
1931 }
1932 }
1933 }
1934
Adam Cohen2801caf2011-05-13 20:57:39 -07001935 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001936 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001937 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
1938 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
1939 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
1940
Adam Cohenc51934b2011-07-26 21:07:43 -07001941 FolderInfo info = (FolderInfo) fi.getTag();
1942 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1943 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07001944 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
1945 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07001946 }
1947
Adam Cohen2801caf2011-05-13 20:57:39 -07001948 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1949 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
1950 oa.start();
1951 }
1952
1953 private void shrinkAndFadeInFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001954 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07001955 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
1956 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
1957 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
1958
Adam Cohenc51934b2011-07-26 21:07:43 -07001959 FolderInfo info = (FolderInfo) fi.getTag();
1960 CellLayout cl = null;
1961 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
1962 cl = (CellLayout) fi.getParent().getParent();
1963 }
1964
1965 final CellLayout layout = cl;
Adam Cohen2801caf2011-05-13 20:57:39 -07001966 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(fi, alpha, scaleX, scaleY);
1967 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07001968 oa.addListener(new AnimatorListenerAdapter() {
1969 @Override
1970 public void onAnimationEnd(Animator animation) {
1971 if (layout != null) {
1972 layout.clearFolderLeaveBehind();
1973 }
1974 }
1975 });
Adam Cohen2801caf2011-05-13 20:57:39 -07001976 oa.start();
1977 }
1978
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 /**
Michael Jurka774bd372010-10-22 13:40:50 -07001980 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981 * is animated relative to the specified View. If the View is null, no animation
1982 * is played.
1983 *
1984 * @param folderInfo The FolderInfo describing the folder to open.
1985 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07001986 public void openFolder(FolderIcon folderIcon) {
1987 Folder folder = folderIcon.mFolder;
1988 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989
Adam Cohen2801caf2011-05-13 20:57:39 -07001990 growAndFadeOutFolderIcon(folderIcon);
Adam Cohena9cf38f2011-05-02 15:36:58 -07001991 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992
Adam Cohen4554ee12011-08-03 16:13:21 -07001993 // Just verify that the folder hasn't already been added to the DragLayer.
1994 // There was a one-off crash where the folder had a parent already.
1995 if (folder.getParent() == null) {
1996 mDragLayer.addView(folder);
1997 mDragController.addDropTarget((DropTarget) folder);
1998 } else {
1999 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2000 folder.getParent() + ").");
2001 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002002 folder.animateOpen();
Adam Cohen4554ee12011-08-03 16:13:21 -07002003 }
2004
2005 public void closeFolder() {
2006 Folder folder = mWorkspace.getOpenFolder();
2007 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002008 if (folder.isEditingName()) {
2009 folder.dismissEditingName();
2010 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002011 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002012
2013 // Dismiss the folder cling
2014 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002015 }
2016 }
2017
2018 void closeFolder(Folder folder) {
2019 folder.getInfo().opened = false;
2020
2021 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2022 if (parent != null) {
2023 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2024 shrinkAndFadeInFolderIcon(fi);
2025 }
2026 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027 }
2028
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 public boolean onLongClick(View v) {
Winson Chungeecf02d2012-03-02 17:14:58 -08002030
Patrick Dubroye708c522011-03-01 16:03:43 -08002031 if (mState != State.WORKSPACE) {
2032 return false;
2033 }
2034
Joe Onorato9c1289c2009-08-17 11:03:03 -04002035 if (isWorkspaceLocked()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 return false;
2037 }
2038
2039 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002040 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 }
2042
Michael Jurka0280c3b2010-09-17 15:00:07 -07002043 resetAddInfo();
2044 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002046 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 return true;
2048 }
2049
Winson Chung3d503fb2011-07-13 17:25:49 -07002050 // The hotseat touch handling does not go through Workspace, and we always allow long press
2051 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002052 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002053 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2054 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002055 if (itemUnderLongClick == null) {
2056 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002057 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2058 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002059 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002061 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002063 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 }
2065 }
2066 }
2067 return true;
2068 }
2069
Winson Chung3d503fb2011-07-13 17:25:49 -07002070 boolean isHotseatLayout(View layout) {
2071 return mHotseat != null && layout != null &&
2072 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2073 }
2074 Hotseat getHotseat() {
2075 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002076 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002077 SearchDropTargetBar getSearchBar() {
2078 return mSearchDropTargetBar;
2079 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002080
Winson Chung3d503fb2011-07-13 17:25:49 -07002081 /**
2082 * Returns the CellLayout of the specified container at the specified screen.
2083 */
2084 CellLayout getCellLayout(long container, int screen) {
2085 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2086 if (mHotseat != null) {
2087 return mHotseat.getLayout();
2088 } else {
2089 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002090 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002091 } else {
2092 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002093 }
2094 }
2095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 Workspace getWorkspace() {
2097 return mWorkspace;
2098 }
2099
Daniel Sandler843e8602010-06-07 14:59:01 -04002100 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2101 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002102 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002103 }
2104
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002105 public boolean isAllAppsButtonRank(int rank) {
2106 return mHotseat.isAllAppsButtonRank(rank);
2107 }
2108
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002109 // AllAppsView.Watcher
2110 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002111 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002112 mWorkspace.setVisibility(View.GONE);
2113 }
2114 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002115
2116 /**
2117 * Helper method for the cameraZoomIn/cameraZoomOut animations
2118 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002119 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002120 * @param scaleFactor The scale factor used for the zoom
2121 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002122 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002123 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002124 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002125 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002126
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002127 void updateWallpaperVisibility(boolean visible) {
2128 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2129 int curflags = getWindow().getAttributes().flags
2130 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2131 if (wpflags != curflags) {
2132 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2133 }
2134 }
2135
Michael Jurkabed61d22012-02-14 22:51:29 -08002136 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2137 if (v instanceof LauncherTransitionable) {
2138 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2139 }
Winson Chung70442722012-02-10 15:43:22 -08002140
2141 // Update the workspace transition step as well
2142 dispatchOnLauncherTransitionStep(v, 0f);
2143 }
2144
2145 private void dispatchOnLauncherTransitionStep(View v, float t) {
2146 if (v instanceof LauncherTransitionable) {
2147 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2148 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002149 }
2150
2151 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2152 if (v instanceof LauncherTransitionable) {
2153 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2154 }
Winson Chung70442722012-02-10 15:43:22 -08002155
2156 // Update the workspace transition step as well
2157 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002158 }
2159
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002160 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002161 * Things to test when changing the following seven functions.
2162 * - Home from workspace
2163 * - from center screen
2164 * - from other screens
2165 * - Home from all apps
2166 * - from center screen
2167 * - from other screens
2168 * - Back from all apps
2169 * - from center screen
2170 * - from other screens
2171 * - Launch app from workspace and quit
2172 * - with back
2173 * - with home
2174 * - Launch app from all apps and quit
2175 * - with back
2176 * - with home
2177 * - Go to a screen that's not the default, then all
2178 * apps, and launch and app, and go back
2179 * - with back
2180 * -with home
2181 * - On workspace, long press power and go back
2182 * - with back
2183 * - with home
2184 * - On all apps, long press power and go back
2185 * - with back
2186 * - with home
2187 * - On workspace, power off
2188 * - On all apps, power off
2189 * - Launch an app and turn off the screen while in that app
2190 * - Go back with home key
2191 * - Go back with back key TODO: make this not go to workspace
2192 * - From all apps
2193 * - From workspace
2194 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2195 * - From all apps
2196 * - From the center workspace
2197 * - From another workspace
2198 */
2199
2200 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002201 * Zoom the camera out from the workspace to reveal 'toView'.
2202 * Assumes that the view to show is anchored at either the very top or very bottom
2203 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002204 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002205 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002206 if (mStateAnimation != null) {
2207 mStateAnimation.cancel();
2208 mStateAnimation = null;
2209 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002210 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002211
Winson Chungf0ea4d32011-06-06 14:27:16 -07002212 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2213 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2214 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002215 final View fromView = mWorkspace;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002216 final View toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002217 final int startDelay =
2218 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002219
Michael Jurkab3e22d92011-10-31 15:58:33 -07002220 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002221
Michael Jurkad74c9842011-07-10 12:44:21 -07002222 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002223 Animator workspaceAnim =
2224 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002225
2226 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002227 toView.setScaleX(scale);
2228 toView.setScaleY(scale);
2229 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2230 scaleAnim.
2231 scaleX(1f).scaleY(1f).
2232 setDuration(duration).
2233 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002234
Winson Chungf0ea4d32011-06-06 14:27:16 -07002235 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002236 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002237 final ObjectAnimator alphaAnim = ObjectAnimator
2238 .ofFloat(toView, "alpha", 0f, 1f)
2239 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002240 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002241 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2242 @Override
2243 public void onAnimationUpdate(ValueAnimator animation) {
2244 float t = (Float) animation.getAnimatedValue();
2245 dispatchOnLauncherTransitionStep(fromView, t);
2246 dispatchOnLauncherTransitionStep(toView, t);
2247 }
2248 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002249
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002250 // toView should appear right at the end of the workspace shrink
2251 // animation
2252 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002253 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002254 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002255
2256 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002257 boolean animationCancelled = false;
2258
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002259 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002260 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002261 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002262 // Prepare the position
2263 toView.setTranslationX(0.0f);
2264 toView.setTranslationY(0.0f);
2265 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002266 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002267 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002268 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002269 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002270 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2271 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002272
2273 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2274 // Hide the workspace scrollbar
2275 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002276 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002277 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002278 if (!animationCancelled) {
2279 updateWallpaperVisibility(false);
2280 }
2281 }
2282
Adam Cohencff6af82011-09-13 14:51:53 -07002283 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002284 public void onAnimationCancel(Animator animation) {
2285 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002286 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002287 });
2288
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002289 if (workspaceAnim != null) {
2290 mStateAnimation.play(workspaceAnim);
2291 }
Michael Jurka1899a362011-11-03 13:50:45 -07002292
2293 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002294 final ViewTreeObserver observer;
2295
Michael Jurkabed61d22012-02-14 22:51:29 -08002296 dispatchOnLauncherTransitionStart(fromView, animated, false);
2297 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002298
2299 // If any of the objects being animated haven't been measured/laid out
2300 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002301 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002302 (mWorkspace.getMeasuredWidth() == 0) ||
2303 (toView.getMeasuredWidth() == 0)) {
2304 observer = mWorkspace.getViewTreeObserver();
2305 delayAnim = true;
2306 } else {
2307 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002308 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002309
2310 if (delayAnim) {
Michael Jurka49779a12012-01-16 04:10:08 -08002311 final AnimatorSet stateAnimation = mStateAnimation;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002312 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2313 public void onGlobalLayout() {
2314 mWorkspace.post(new Runnable() {
2315 public void run() {
Michael Jurka49779a12012-01-16 04:10:08 -08002316 // Check that mStateAnimation hasn't changed while
2317 // we waited for a layout pass
2318 if (mStateAnimation == stateAnimation) {
2319 // Need to update pivots for zoom if layout changed
2320 setPivotsForZoom(toView, scale);
2321 mStateAnimation.start();
2322 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002323 }
2324 });
Michael Jurka3a9fced2012-04-13 14:44:29 -07002325 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002326 }
2327 };
2328 observer.addOnGlobalLayoutListener(delayedStart);
2329 } else {
2330 setPivotsForZoom(toView, scale);
Michael Jurka1899a362011-11-03 13:50:45 -07002331 mStateAnimation.start();
2332 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002333 } else {
2334 toView.setTranslationX(0.0f);
2335 toView.setTranslationY(0.0f);
2336 toView.setScaleX(1.0f);
2337 toView.setScaleY(1.0f);
2338 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002339 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002340
Michael Jurkabed61d22012-02-14 22:51:29 -08002341 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2342 // Hide the workspace scrollbar
2343 mWorkspace.hideScrollingIndicator(true);
2344 hideDockDivider();
Michael Jurkaabded662011-03-04 12:06:57 -08002345 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002346 dispatchOnLauncherTransitionStart(fromView, animated, false);
2347 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2348 dispatchOnLauncherTransitionStart(toView, animated, false);
2349 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002350 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002351 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002352 }
2353
2354 /**
2355 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002356 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002357 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002358 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002359 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2360 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002361
Michael Jurkab3e22d92011-10-31 15:58:33 -07002362 if (mStateAnimation != null) {
2363 mStateAnimation.cancel();
2364 mStateAnimation = null;
2365 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002366 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002367
Winson Chungf0ea4d32011-06-06 14:27:16 -07002368 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002369 final int fadeOutDuration =
2370 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002371 final float scaleFactor = (float)
2372 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2373 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002374 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002375 Animator workspaceAnim = null;
2376
2377 if (toState == State.WORKSPACE) {
2378 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2379 workspaceAnim = mWorkspace.getChangeStateAnimation(
2380 Workspace.State.NORMAL, animated, stagger);
2381 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2382 workspaceAnim = mWorkspace.getChangeStateAnimation(
2383 Workspace.State.SPRING_LOADED, animated);
2384 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002385
Michael Jurkab3e22d92011-10-31 15:58:33 -07002386 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002387 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002388 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002389 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002390 final LauncherViewPropertyAnimator scaleAnim =
2391 new LauncherViewPropertyAnimator(fromView);
2392 scaleAnim.
2393 scaleX(scaleFactor).scaleY(scaleFactor).
2394 setDuration(duration).
2395 setInterpolator(new Workspace.ZoomInInterpolator());
2396
2397 final ObjectAnimator alphaAnim = ObjectAnimator
2398 .ofFloat(fromView, "alpha", 1f, 0f)
2399 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002400 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002401 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2402 @Override
2403 public void onAnimationUpdate(ValueAnimator animation) {
2404 float t = 1f - (Float) animation.getAnimatedValue();
2405 dispatchOnLauncherTransitionStep(fromView, t);
2406 dispatchOnLauncherTransitionStep(toView, t);
2407 }
2408 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002409
Michael Jurkabed61d22012-02-14 22:51:29 -08002410 mStateAnimation = new AnimatorSet();
2411
2412 dispatchOnLauncherTransitionStart(fromView, animated, true);
2413 dispatchOnLauncherTransitionStart(toView, animated, true);
2414
2415 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002416 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002417 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002418 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002419 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002420 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2421 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurka430e8a52011-08-08 15:52:14 -07002422 mWorkspace.hideScrollingIndicator(false);
Adam Cohened66b2b2012-01-23 17:28:51 -08002423 if (onCompleteRunnable != null) {
2424 onCompleteRunnable.run();
2425 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002426 }
2427 });
2428
Winson Chungf0ea4d32011-06-06 14:27:16 -07002429 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002430 if (workspaceAnim != null) {
2431 mStateAnimation.play(workspaceAnim);
2432 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002433 mStateAnimation.start();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002434 } else {
2435 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002436 dispatchOnLauncherTransitionStart(fromView, animated, true);
2437 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2438 dispatchOnLauncherTransitionStart(toView, animated, true);
2439 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002440 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002441 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002442 }
2443
Michael Jurkae326f182011-11-21 14:05:46 -08002444 @Override
2445 public void onTrimMemory(int level) {
2446 super.onTrimMemory(level);
Dianne Hackborn4f560ce2012-03-06 19:07:47 -08002447 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
Michael Jurkae326f182011-11-21 14:05:46 -08002448 mAppsCustomizeTabHost.onTrimMemory();
2449 }
2450 }
2451
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002452 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002453 showWorkspace(animated, null);
2454 }
2455
2456 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002457 if (mState != State.WORKSPACE) {
2458 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002459 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002460
2461 // Show the search bar and hotseat
2462 mSearchDropTargetBar.showSearchBar(animated);
Michael Jurkab737ee62011-11-15 15:57:22 -08002463 // We only need to animate in the dock divider if we're going from spring loaded mode
2464 showDockDivider(animated && mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002465
2466 // Set focus to the AppsCustomize button
2467 if (mAllAppsButton != null) {
2468 mAllAppsButton.requestFocus();
2469 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002470 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002471
Michael Jurkab737ee62011-11-15 15:57:22 -08002472 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002473
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002474 // Change the state *after* we've called all the transition code
2475 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002476
Winson Chung5fb63472011-02-02 17:03:37 -08002477 // Resume the auto-advance of widgets
2478 mUserPresent = true;
2479 updateRunning();
2480
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002481 // send an accessibility event to announce the context change
2482 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002483 }
2484
Michael Jurkab3e22d92011-10-31 15:58:33 -07002485 void showAllApps(boolean animated) {
2486 if (mState != State.WORKSPACE) return;
2487
2488 showAppsCustomizeHelper(animated, false);
2489 mAppsCustomizeTabHost.requestFocus();
2490
2491 // Hide the search bar and hotseat
2492 mSearchDropTargetBar.hideSearchBar(animated);
2493
2494 // Change the state *after* we've called all the transition code
2495 mState = State.APPS_CUSTOMIZE;
2496
2497 // Pause the auto-advance of widgets until we are out of AllApps
2498 mUserPresent = false;
2499 updateRunning();
2500 closeFolder();
2501
2502 // Send an accessibility event to announce the context change
2503 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2504 }
2505
Adam Cohen7777d962011-08-18 18:58:38 -07002506 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002507 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002508 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002509 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002510 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002511 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002512 }
Adam Cohen7777d962011-08-18 18:58:38 -07002513
Adam Cohened66b2b2012-01-23 17:28:51 -08002514 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2515 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002516 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2517
Winson Chunge7a03942011-08-05 15:05:12 -07002518 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002519 @Override
2520 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002521 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002522 // Before we show workspace, hide all apps again because
2523 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2524 // clean up our state transition functions
2525 mAppsCustomizeTabHost.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002526 mSearchDropTargetBar.showSearchBar(true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002527 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002528 } else {
2529 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002530 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002531 }
2532 }, (extendedDelay ?
2533 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2534 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2535 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002536
Michael Jurkad3ef3062010-11-23 16:23:58 -08002537 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002538 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002539 final boolean animated = true;
2540 final boolean springLoaded = true;
2541 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002542 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002543 }
2544 // Otherwise, we are not in spring loaded mode, so don't do anything.
2545 }
2546
Michael Jurkab737ee62011-11-15 15:57:22 -08002547 void hideDockDivider() {
2548 if (mQsbDivider != null && mDockDivider != null) {
2549 mQsbDivider.setVisibility(View.INVISIBLE);
2550 mDockDivider.setVisibility(View.INVISIBLE);
2551 }
2552 }
2553
2554 void showDockDivider(boolean animated) {
2555 if (mQsbDivider != null && mDockDivider != null) {
2556 mQsbDivider.setVisibility(View.VISIBLE);
2557 mDockDivider.setVisibility(View.VISIBLE);
2558 if (mDividerAnimator != null) {
2559 mDividerAnimator.cancel();
2560 mQsbDivider.setAlpha(1f);
2561 mDockDivider.setAlpha(1f);
2562 mDividerAnimator = null;
2563 }
2564 if (animated) {
2565 mDividerAnimator = new AnimatorSet();
2566 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2567 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2568 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2569 mDividerAnimator.start();
2570 }
2571 }
2572 }
2573
Michael Jurkab3e22d92011-10-31 15:58:33 -07002574 void lockAllApps() {
2575 // TODO
2576 }
2577
2578 void unlockAllApps() {
2579 // TODO
2580 }
2581
Adam Cohenfc53cd22011-07-20 15:45:11 -07002582 public boolean isAllAppsCustomizeOpen() {
2583 return mState == State.APPS_CUSTOMIZE;
2584 }
2585
Winson Chungf0ea4d32011-06-06 14:27:16 -07002586 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002587 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002588 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002589 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002590 if (!LauncherApplication.isScreenLarge()) {
2591 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002592 if (mHotseat.getAlpha() != 1f) {
2593 int duration = mSearchDropTargetBar.getTransitionInDuration();
2594 mHotseat.animate().alpha(1f).setDuration(duration);
2595 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002596 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002597 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002598 }
2599 }
2600 }
2601
2602 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002603 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002604 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002605 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002606 if (!LauncherApplication.isScreenLarge()) {
2607 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002608 if (mHotseat.getAlpha() != 0f) {
2609 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2610 mHotseat.animate().alpha(0f).setDuration(duration);
2611 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002612 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002613 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002614 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002615 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002616 }
2617
Patrick Dubroy5f445422011-02-18 14:35:21 -08002618 /**
2619 * Add an item from all apps or customize onto the given workspace screen.
2620 * If layout is null, add to the current screen.
2621 */
2622 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002623 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002624 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002625 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002626 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002627
Winson Chungdff8ebb2011-09-08 17:25:31 -07002628 /** Maps the current orientation to an index for referencing orientation correct global icons */
2629 private int getCurrentOrientationIndexForGlobalIcons() {
2630 // default - 0, landscape - 1
2631 switch (getResources().getConfiguration().orientation) {
2632 case Configuration.ORIENTATION_LANDSCAPE:
2633 return 1;
2634 default:
2635 return 0;
2636 }
2637 }
2638
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002639 private Drawable getExternalPackageToolbarIcon(ComponentName activityName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002640 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002641 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002642 // Look for the toolbar icon specified in the activity meta-data
2643 Bundle metaData = packageManager.getActivityInfo(
2644 activityName, PackageManager.GET_META_DATA).metaData;
2645 if (metaData != null) {
2646 int iconResId = metaData.getInt(TOOLBAR_ICON_METADATA_NAME);
2647 if (iconResId != 0) {
2648 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002649 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002650 }
2651 }
2652 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002653 // This can happen if the activity defines an invalid drawable
2654 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2655 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002656 } catch (Resources.NotFoundException nfe) {
2657 // This can happen if the activity defines an invalid drawable
2658 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2659 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002660 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002661 return null;
2662 }
2663
2664 // if successful in getting icon, return it; otherwise, set button to use default drawable
2665 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
2666 int buttonId, ComponentName activityName, int fallbackDrawableId) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002667 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002668 Resources r = getResources();
2669 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2670 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002671
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002672 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002673 // If we were unable to find the icon via the meta-data, use a generic one
2674 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002675 toolbarIcon = r.getDrawable(fallbackDrawableId);
2676 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002677 if (button != null) {
2678 button.setCompoundDrawables(toolbarIcon, null, null, null);
2679 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002680 return null;
2681 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002682 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002683 if (button != null) {
2684 button.setCompoundDrawables(toolbarIcon, null, null, null);
2685 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002686 return toolbarIcon.getConstantState();
2687 }
2688 }
2689
2690 // if successful in getting icon, return it; otherwise, set button to use default drawable
2691 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
2692 int buttonId, ComponentName activityName, int fallbackDrawableId) {
2693 ImageView button = (ImageView) findViewById(buttonId);
2694 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName);
2695
Michael Jurka19e0fc52011-07-22 18:00:21 -07002696 if (button != null) {
2697 // If we were unable to find the icon via the meta-data, use a
2698 // generic one
2699 if (toolbarIcon == null) {
2700 button.setImageResource(fallbackDrawableId);
2701 } else {
2702 button.setImageDrawable(toolbarIcon);
2703 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002704 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002705
2706 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2707
Michael Jurka0423dcf2010-10-05 14:56:18 -07002708 }
2709
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002710 private void updateTextButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
2711 TextView button = (TextView) findViewById(buttonId);
2712 button.setCompoundDrawables(d.newDrawable(getResources()), null, null, null);
2713 }
2714
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002715 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002716 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002717 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002718 }
2719
Winson Chungbb185bd2011-11-21 12:31:42 -08002720 private void invalidatePressedFocusedStates(View container, View button) {
2721 if (container instanceof HolographicLinearLayout) {
2722 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2723 layout.invalidatePressedFocusedStates();
2724 } else if (button instanceof HolographicImageView) {
2725 HolographicImageView view = (HolographicImageView) button;
2726 view.invalidatePressedFocusedStates();
2727 }
2728 }
2729
Winson Chungc51db6a2011-10-05 11:44:49 -07002730 private boolean updateGlobalSearchIcon() {
2731 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002732 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2733 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002734 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002735 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002736
Winson Chung1cad91e2011-05-25 17:41:01 -07002737 final SearchManager searchManager =
2738 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2739 ComponentName activityName = searchManager.getGlobalSearchActivity();
2740 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002741 int coi = getCurrentOrientationIndexForGlobalIcons();
2742 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002743 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002744 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002745 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2746 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002747 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002748 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002749 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002750 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07002751 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002752 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
2753 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
2754 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002755 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002756 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002757 }
2758 }
2759
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002760 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002761 final View searchButtonContainer = findViewById(R.id.search_button_container);
2762 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002763 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002764 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002765 }
2766
Winson Chungc51db6a2011-10-05 11:44:49 -07002767 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07002768 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002769 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002770 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07002771
Winson Chungc51db6a2011-10-05 11:44:49 -07002772 // We only show/update the voice search icon if the search icon is enabled as well
Winson Chung1cad91e2011-05-25 17:41:01 -07002773 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
2774 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chungc51db6a2011-10-05 11:44:49 -07002775 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002776 int coi = getCurrentOrientationIndexForGlobalIcons();
2777 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Winson Chungc7aef8c2011-09-15 18:53:04 -07002778 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo);
Winson Chung649723c2011-07-06 20:41:23 -07002779 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002780 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002781 voiceButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002782 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07002783 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07002784 } else {
Winson Chung649723c2011-07-06 20:41:23 -07002785 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002786 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07002787 voiceButton.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002788 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002789 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002790 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002791
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002792 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002793 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
2794 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002795 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08002796 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002797 }
2798
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002799 /**
Winson Chungeb66b142011-06-16 13:14:22 -07002800 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07002801 */
2802 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07002803 final View marketButton = findViewById(R.id.market_button);
2804 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
2805 // Find the app market activity by resolving an intent.
2806 // (If multiple app markets are installed, it will return the ResolverActivity.)
2807 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07002808 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002809 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07002810 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07002811 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Winson Chung967289b2011-06-30 18:09:30 -07002812 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo);
Winson Chung785d2eb2011-04-14 16:08:02 -07002813 marketButton.setVisibility(View.VISIBLE);
2814 } else {
2815 // We should hide and disable the view so that we don't try and restore the visibility
2816 // of it when we swap between drag & normal states from IconDropTarget subclasses.
2817 marketButton.setVisibility(View.GONE);
2818 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002819 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002820 }
2821
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002822 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002823 updateTextButtonWithDrawable(R.id.market_button, d);
Michael Jurka4ef207b2010-11-29 17:05:45 -08002824 }
2825
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002826 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07002827 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002828 */
2829 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
2830 @Override
2831 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002832 closeSystemDialogs();
2833 }
2834 }
2835
2836 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002837 * Receives notifications whenever the appwidgets are reset.
2838 */
2839 private class AppWidgetResetObserver extends ContentObserver {
2840 public AppWidgetResetObserver() {
2841 super(new Handler());
2842 }
2843
2844 @Override
2845 public void onChange(boolean selfChange) {
2846 onAppWidgetReset();
2847 }
2848 }
2849
2850 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002851 * If the activity is currently paused, signal that we need to re-run the loader
2852 * in onResume.
2853 *
2854 * This needs to be called from incoming places where resources might have been loaded
2855 * while we are paused. That is becaues the Configuration might be wrong
2856 * when we're not running, and if it comes back to what it was when we
2857 * were paused, we are not restarted.
2858 *
2859 * Implementation of the method from LauncherModel.Callbacks.
2860 *
2861 * @return true if we are currently paused. The caller might be able to
2862 * skip some work in that case since we will come back again.
2863 */
2864 public boolean setLoadOnResume() {
2865 if (mPaused) {
2866 Log.i(TAG, "setLoadOnResume");
2867 mOnResumeNeedsLoad = true;
2868 return true;
2869 } else {
2870 return false;
2871 }
2872 }
2873
2874 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002875 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002876 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002877 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07002878 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002879 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07002880 } else {
2881 return SCREEN_COUNT / 2;
2882 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002883 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07002884
Joe Onorato9c1289c2009-08-17 11:03:03 -04002885 /**
2886 * Refreshes the shortcuts shown on the workspace.
2887 *
2888 * Implementation of the method from LauncherModel.Callbacks.
2889 */
2890 public void startBinding() {
2891 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07002892
Winson Chungf0c6ae02012-03-21 16:10:31 -07002893 mNewShortcutAnimatePage = -1;
2894 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07002895 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002896 int count = workspace.getChildCount();
2897 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04002898 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08002899 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
2900 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04002901 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08002902 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07002903 if (mHotseat != null) {
2904 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002905 }
2906 }
2907
2908 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04002909 * Bind the items start-end from the list.
2910 *
2911 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002912 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04002913 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002914 setLoadOnResume();
2915
Winson Chungf0c6ae02012-03-21 16:10:31 -07002916 // Get the list of added shortcuts and intersect them with the set of shortcuts here
2917 Set<String> newApps = new HashSet<String>();
2918 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
2919
2920 Workspace workspace = mWorkspace;
2921 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04002922 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07002923
2924 // Short circuit if we are loading dock items for a configuration which has no dock
2925 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
2926 mHotseat == null) {
2927 continue;
2928 }
2929
Joe Onorato9c1289c2009-08-17 11:03:03 -04002930 switch (item.itemType) {
2931 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
2932 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07002933 ShortcutInfo info = (ShortcutInfo) item;
2934 String uri = info.intent.toUri(0).toString();
2935 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07002936 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
2937 item.cellY, 1, 1, false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07002938 if (newApps.contains(uri)) {
2939 newApps.remove(uri);
2940
2941 // Prepare the view to be animated up
2942 shortcut.setAlpha(0f);
2943 shortcut.setScaleX(0f);
2944 shortcut.setScaleY(0f);
2945 mNewShortcutAnimatePage = item.screen;
2946 if (!mNewShortcutAnimateViews.contains(shortcut)) {
2947 mNewShortcutAnimateViews.add(shortcut);
2948 }
2949 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002950 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07002951 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07002952 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07002953 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07002954 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07002955 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
2956 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002957 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04002958 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002959 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07002960
Joe Onorato9c1289c2009-08-17 11:03:03 -04002961 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002962 }
2963
Joe Onorato9c1289c2009-08-17 11:03:03 -04002964 /**
2965 * Implementation of the method from LauncherModel.Callbacks.
2966 */
Joe Onoratoad72e172009-11-06 16:25:04 -05002967 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002968 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002969 sFolders.clear();
2970 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002972
2973 /**
2974 * Add the views for a widget to the workspace.
2975 *
2976 * Implementation of the method from LauncherModel.Callbacks.
2977 */
2978 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07002979 setLoadOnResume();
2980
Daniel Sandler843e8602010-06-07 14:59:01 -04002981 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
2982 if (DEBUG_WIDGETS) {
2983 Log.d(TAG, "bindAppWidget: " + item);
2984 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04002985 final Workspace workspace = mWorkspace;
2986
2987 final int appWidgetId = item.appWidgetId;
2988 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04002989 if (DEBUG_WIDGETS) {
2990 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
2991 }
2992
Joe Onorato9c1289c2009-08-17 11:03:03 -04002993 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
2994
Joe Onorato9c1289c2009-08-17 11:03:03 -04002995 item.hostView.setAppWidget(appWidgetId, appWidgetInfo);
2996 item.hostView.setTag(item);
2997
Winson Chung3d503fb2011-07-13 17:25:49 -07002998 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04002999 item.cellY, item.spanX, item.spanY, false);
3000
Adam Cohended9f8d2010-11-03 13:25:16 -07003001 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3002
Joe Onorato9c1289c2009-08-17 11:03:03 -04003003 workspace.requestLayout();
3004
Daniel Sandler843e8602010-06-07 14:59:01 -04003005 if (DEBUG_WIDGETS) {
3006 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3007 + (SystemClock.uptimeMillis()-start) + "ms");
3008 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003009 }
3010
3011 /**
3012 * Callback saying that there aren't any more items to bind.
3013 *
3014 * Implementation of the method from LauncherModel.Callbacks.
3015 */
3016 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003017 setLoadOnResume();
3018
Joe Onorato9c1289c2009-08-17 11:03:03 -04003019 if (mSavedState != null) {
3020 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003021 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003022 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003023 mSavedState = null;
3024 }
3025
3026 if (mSavedInstanceState != null) {
3027 super.onRestoreInstanceState(mSavedInstanceState);
3028 mSavedInstanceState = null;
3029 }
3030
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003031 // If we received the result of any pending adds while the loader was running (e.g. the
3032 // widget configuration forced an orientation change), process them now.
3033 for (int i = 0; i < sPendingAddList.size(); i++) {
3034 completeAdd(sPendingAddList.get(i));
3035 }
3036 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003037
Winson Chungc51db6a2011-10-05 11:44:49 -07003038 // Update the market app icon as necessary (the other icons will be managed in response to
3039 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003040 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003041
Winson Chungf0c6ae02012-03-21 16:10:31 -07003042 // Animate up any icons as necessary
3043 if (mVisible || mWorkspaceLoading) {
3044 Runnable newAppsRunnable = new Runnable() {
3045 @Override
3046 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003047 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003048 }
3049 };
Winson Chunga2413752012-04-03 14:22:34 -07003050
3051 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3052 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3053 if (canRunNewAppsAnimation()) {
3054 // If the user has not interacted recently, then either snap to the new page to show
3055 // the new-apps animation or just run them if they are to appear on the current page
3056 if (willSnapPage) {
3057 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3058 } else {
3059 runNewAppsAnimation(false);
3060 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003061 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003062 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003063 // are on another page (or just normally if they are added to the current page)
3064 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003065 }
3066 }
3067
3068 mWorkspaceLoading = false;
3069 }
3070
Winson Chunga2413752012-04-03 14:22:34 -07003071 private boolean canRunNewAppsAnimation() {
3072 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3073 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3074 }
3075
Winson Chungf0c6ae02012-03-21 16:10:31 -07003076 /**
3077 * Runs a new animation that scales up icons that were added while Launcher was in the
3078 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003079 *
3080 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003081 */
Winson Chunga2413752012-04-03 14:22:34 -07003082 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003083 AnimatorSet anim = new AnimatorSet();
3084 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003085
3086 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003087 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3088 @Override
3089 public int compare(View a, View b) {
3090 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3091 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3092 int cellCountX = LauncherModel.getCellCountX();
3093 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3094 }
3095 });
Winson Chunga2413752012-04-03 14:22:34 -07003096
3097 // Animate each of the views in place (or show them immediately if requested)
3098 if (immediate) {
3099 for (View v : mNewShortcutAnimateViews) {
3100 v.setAlpha(1f);
3101 v.setScaleX(1f);
3102 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003103 }
Winson Chunga2413752012-04-03 14:22:34 -07003104 } else {
3105 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3106 View v = mNewShortcutAnimateViews.get(i);
3107 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3108 PropertyValuesHolder.ofFloat("alpha", 1f),
3109 PropertyValuesHolder.ofFloat("scaleX", 1f),
3110 PropertyValuesHolder.ofFloat("scaleY", 1f));
3111 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3112 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3113 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3114 bounceAnims.add(bounceAnim);
3115 }
3116 anim.playTogether(bounceAnims);
3117 anim.addListener(new AnimatorListenerAdapter() {
3118 @Override
3119 public void onAnimationEnd(Animator animation) {
3120 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3121 }
3122 });
3123 anim.start();
3124 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003125
3126 // Clean up
3127 mNewShortcutAnimatePage = -1;
3128 mNewShortcutAnimateViews.clear();
3129 new Thread("clearNewAppsThread") {
3130 public void run() {
3131 mSharedPrefs.edit()
3132 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3133 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3134 .commit();
3135 }
3136 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003137 }
3138
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003139 @Override
3140 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003141 boolean searchVisible = updateGlobalSearchIcon();
3142 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3143 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003144 }
3145
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003146 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003147 * Add the icons for all apps.
3148 *
3149 * Implementation of the method from LauncherModel.Callbacks.
3150 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003151 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3152 // Remove the progress bar entirely; we could also make it GONE
3153 // but better to remove it since we know it's not going to be used
3154 View progressBar = mAppsCustomizeTabHost.
3155 findViewById(R.id.apps_customize_progress_bar);
3156 if (progressBar != null) {
3157 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003158 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003159 // We just post the call to setApps so the user sees the progress bar
3160 // disappear-- otherwise, it just looks like the progress bar froze
3161 // which doesn't look great
3162 mAppsCustomizeTabHost.post(new Runnable() {
3163 public void run() {
3164 if (mAppsCustomizeContent != null) {
3165 mAppsCustomizeContent.setApps(apps);
3166 }
3167 }
3168 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003169 }
3170
3171 /**
3172 * A package was installed.
3173 *
3174 * Implementation of the method from LauncherModel.Callbacks.
3175 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003176 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003177 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003178
Winson Chung785d2eb2011-04-14 16:08:02 -07003179 if (mAppsCustomizeContent != null) {
3180 mAppsCustomizeContent.addApps(apps);
3181 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003182 }
3183
3184 /**
3185 * A package was updated.
3186 *
3187 * Implementation of the method from LauncherModel.Callbacks.
3188 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003189 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003190 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003191 if (mWorkspace != null) {
3192 mWorkspace.updateShortcuts(apps);
3193 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003194
Winson Chung785d2eb2011-04-14 16:08:02 -07003195 if (mAppsCustomizeContent != null) {
3196 mAppsCustomizeContent.updateApps(apps);
3197 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003198 }
3199
3200 /**
3201 * A package was uninstalled.
3202 *
3203 * Implementation of the method from LauncherModel.Callbacks.
3204 */
Joe Onorato36115782010-06-17 13:28:48 -04003205 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003206 if (permanent) {
3207 mWorkspace.removeItems(apps);
3208 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003209
Winson Chung785d2eb2011-04-14 16:08:02 -07003210 if (mAppsCustomizeContent != null) {
3211 mAppsCustomizeContent.removeApps(apps);
3212 }
Winson Chunga1820962011-10-03 16:31:06 -07003213
3214 // Notify the drag controller
3215 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003216 }
Joe Onoratobe386092009-11-17 17:32:16 -08003217
3218 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003219 * A number of packages were updated.
3220 */
3221 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003222 if (mAppsCustomizeContent != null) {
3223 mAppsCustomizeContent.onPackagesUpdated();
3224 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003225 }
3226
Winson Chung400438b2011-01-16 17:53:48 -08003227 private int mapConfigurationOriActivityInfoOri(int configOri) {
3228 final Display d = getWindowManager().getDefaultDisplay();
3229 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3230 switch (d.getRotation()) {
3231 case Surface.ROTATION_0:
3232 case Surface.ROTATION_180:
3233 // We are currently in the same basic orientation as the natural orientation
3234 naturalOri = configOri;
3235 break;
3236 case Surface.ROTATION_90:
3237 case Surface.ROTATION_270:
3238 // We are currently in the other basic orientation to the natural orientation
3239 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3240 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3241 break;
3242 }
3243
3244 int[] oriMap = {
3245 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3246 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3247 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3248 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3249 };
3250 // Since the map starts at portrait, we need to offset if this device's natural orientation
3251 // is landscape.
3252 int indexOffset = 0;
3253 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3254 indexOffset = 1;
3255 }
3256 return oriMap[(d.getRotation() + indexOffset) % 4];
3257 }
Adam Cohen446e9402011-09-15 18:21:21 -07003258
3259 public void lockScreenOrientationOnLargeUI() {
3260 if (LauncherApplication.isScreenLarge()) {
3261 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3262 .getConfiguration().orientation));
3263 }
Winson Chung400438b2011-01-16 17:53:48 -08003264 }
Adam Cohen446e9402011-09-15 18:21:21 -07003265 public void unlockScreenOrientationOnLargeUI() {
3266 if (LauncherApplication.isScreenLarge()) {
3267 mHandler.postDelayed(new Runnable() {
3268 public void run() {
3269 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3270 }
3271 }, mRestoreScreenOrientationDelay);
3272 }
Winson Chung400438b2011-01-16 17:53:48 -08003273 }
3274
Winson Chung82f55532011-08-09 14:14:23 -07003275 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003276 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003277 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003278 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003279
Winson Chung7d7541e2011-09-16 20:14:36 -07003280 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003281 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003282 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3283 Cling cling = (Cling) findViewById(clingId);
3284 if (cling != null) {
3285 cling.init(this, positionData);
3286 cling.setVisibility(View.VISIBLE);
3287 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3288 if (animate) {
3289 cling.buildLayer();
3290 cling.setAlpha(0f);
3291 cling.animate()
3292 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003293 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003294 .setDuration(SHOW_CLING_DURATION)
3295 .setStartDelay(delay)
3296 .start();
3297 } else {
3298 cling.setAlpha(1f);
3299 }
3300 }
3301 return cling;
3302 }
3303 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003304 if (cling != null) {
3305 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003306 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003307 anim.addListener(new AnimatorListenerAdapter() {
3308 public void onAnimationEnd(Animator animation) {
3309 cling.setVisibility(View.GONE);
3310 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003311 // We should update the shared preferences on a background thread
3312 new Thread("dismissClingThread") {
3313 public void run() {
3314 SharedPreferences.Editor editor = mSharedPrefs.edit();
3315 editor.putBoolean(flag, true);
3316 editor.commit();
3317 }
3318 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003319 };
3320 });
3321 anim.start();
3322 }
3323 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003324 private void removeCling(int id) {
3325 final View cling = findViewById(id);
3326 if (cling != null) {
3327 final ViewGroup parent = (ViewGroup) cling.getParent();
3328 parent.post(new Runnable() {
3329 @Override
3330 public void run() {
3331 parent.removeView(cling);
3332 }
3333 });
3334 }
3335 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003336 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003337 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003338 if (isClingsEnabled() &&
3339 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003340 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003341 } else {
3342 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003343 }
3344 }
3345 public void showFirstRunAllAppsCling(int[] position) {
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.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003349 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003350 } else {
3351 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003352 }
3353 }
3354 public Cling showFirstRunFoldersCling() {
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.FOLDER_CLING_DISMISSED_KEY, false)) {
3358 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003359 } else {
3360 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003361 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003362 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003363 }
3364 public boolean isFolderClingVisible() {
3365 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003366 if (cling != null) {
3367 return cling.getVisibility() == View.VISIBLE;
3368 }
3369 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003370 }
Winson Chung82f55532011-08-09 14:14:23 -07003371 public void dismissWorkspaceCling(View v) {
3372 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003373 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003374 }
3375 public void dismissAllAppsCling(View v) {
3376 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003377 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3378 }
3379 public void dismissFolderCling(View v) {
3380 Cling cling = (Cling) findViewById(R.id.folder_cling);
3381 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003382 }
3383
Winson Chung80baf5a2010-08-09 16:03:15 -07003384 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003385 * Prints out out state for debugging.
3386 */
3387 public void dumpState() {
3388 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003389 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003390 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3391 Log.d(TAG, "mRestoring=" + mRestoring);
3392 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3393 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003394 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003395 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003396
Winson Chung785d2eb2011-04-14 16:08:02 -07003397 if (mAppsCustomizeContent != null) {
3398 mAppsCustomizeContent.dumpState();
3399 }
Joe Onoratobe386092009-11-17 17:32:16 -08003400 Log.d(TAG, "END launcher2 dump state");
3401 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003402
3403 @Override
3404 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3405 super.dump(prefix, fd, writer, args);
3406 writer.println(" ");
3407 writer.println("Debug logs: ");
3408 for (int i = 0; i < sDumpLogs.size(); i++) {
3409 writer.println(" " + sDumpLogs.get(i));
3410 }
3411 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003412}
Michael Jurka2763be32011-02-24 11:19:57 -08003413
Michael Jurkaabded662011-03-04 12:06:57 -08003414interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003415 View getContent();
Michael Jurkabed61d22012-02-14 22:51:29 -08003416 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003417 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003418 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003419}