blob: ae2ca3b479da416bf398455efa50ce90dbd3dda2 [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
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
53import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040056import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070057import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080058import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020059import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080060import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070061import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070062import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040063import android.os.SystemClock;
Joe Onoratobe386092009-11-17 17:32:16 -080064import android.os.SystemProperties;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070070import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080071import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080072import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.view.KeyEvent;
74import android.view.LayoutInflater;
75import android.view.Menu;
76import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070077import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080078import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080079import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080080import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070081import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080082import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080083import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070084import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080085import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070086import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070087import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080088import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080089import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070090import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070091import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070092import android.widget.TextView;
93import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070094
Adam Cohendf2cc412011-04-27 16:56:57 -070095import com.android.common.Search;
96import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070097import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080098
Adam Cohenc0dcf592011-06-01 15:30:43 -070099import java.io.DataInputStream;
100import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700101import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.FileNotFoundException;
103import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700104import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700106import java.util.Collection;
107import java.util.Collections;
108import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700109import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700110import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700111import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700112import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700113
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800114/**
115 * Default launcher application.
116 */
Michael Jurka946ad472010-07-09 18:05:18 -0700117public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700118 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
119 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800120 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700121 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800122
Joe Onorato9c1289c2009-08-17 11:03:03 -0400123 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400124 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700125 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700126
Winson Chung70d72102011-08-12 11:24:35 -0700127 private static final int MENU_GROUP_WALLPAPER = 1;
128 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
129 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700130 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
131 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132
133 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700134 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800135 private static final int REQUEST_PICK_APPLICATION = 6;
136 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700137 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700138 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Michael Jurka8b805b12012-04-18 14:23:14 -0700140 private static final int REQUEST_BIND_APPWIDGET = 11;
141
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
143
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800144 static final int SCREEN_COUNT = 5;
145 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146
Romain Guy98d01652009-06-30 16:21:04 -0700147 private static final String PREFERENCES = "launcher.preferences";
Adam Cohen23a4d302011-12-01 17:26:44 -0800148 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher.force_enable_rotation";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149
Winson Chung2672ff92012-05-04 16:22:30 -0700150 // The Intent extra that defines whether to ignore the launch animation
151 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
152 "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 // Type: int
155 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700156 // Type: int
157 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700159 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
160 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
162 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700163 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700165 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: boolean
167 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
168 // Type: long
169 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700170 // Type: int
171 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
172 // Type: int
173 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
174 // Type: parcelable
175 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700177 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700178 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
179 "com.android.launcher.toolbar_search_icon";
180 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
181 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700182
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700183 /** The different states that Launcher can be in. */
Winson Chung36e6c5b2012-07-19 14:53:05 -0700184 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700185 private State mState = State.WORKSPACE;
186 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800187 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700188
Joe Onorato9c1289c2009-08-17 11:03:03 -0400189 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700190 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
191 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700192 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700193 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800196 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
Winson Chunga2413752012-04-03 14:22:34 -0700198 // How long to wait before the new-shortcut animation automatically pans the workspace
199 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
200
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800201 private final BroadcastReceiver mCloseSystemDialogsReceiver
202 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800203 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
204
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205 private LayoutInflater mInflater;
206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800208 private View mQsbDivider;
209 private View mDockDivider;
210 private DragLayer mDragLayer;
211 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700212
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700213 private AppWidgetManager mAppWidgetManager;
214 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700215
Michael Jurkac9d95c52011-08-29 14:03:34 -0700216 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700217 private AppWidgetProviderInfo mPendingAddWidgetInfo;
218
Michael Jurka0280c3b2010-09-17 15:00:07 -0700219 private int[] mTmpAddItemCellCoordinates = new int[2];
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 private FolderInfo mFolderInfo;
222
Winson Chung3d503fb2011-07-13 17:25:49 -0700223 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800224 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700225
Winson Chungc51db6a2011-10-05 11:44:49 -0700226 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700227 private AppsCustomizeTabHost mAppsCustomizeTabHost;
228 private AppsCustomizePagedView mAppsCustomizeContent;
229 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231 private Bundle mSavedState;
Winson Chung36e6c5b2012-07-19 14:53:05 -0700232 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
233 // scroll issues (because the workspace may not have been measured yet) and extra work.
234 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
235 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236
237 private SpannableStringBuilder mDefaultKeySsb = null;
238
Joe Onorato9c1289c2009-08-17 11:03:03 -0400239 private boolean mWorkspaceLoading = true;
240
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800241 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242 private boolean mRestoring;
243 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700244 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245
Winson Chung36e6c5b2012-07-19 14:53:05 -0700246 // Keep track of whether the user has left launcher
247 private static boolean sPausedFromUserAction = false;
248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private Bundle mSavedInstanceState;
250
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800252 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800253 private boolean mUserPresent = true;
254 private boolean mVisible = false;
255 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400256
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700257 private static LocaleConfiguration sLocaleConfiguration = null;
258
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700259 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700260
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700261 private Intent mAppMarketIntent = null;
262
Adam Cohended9f8d2010-11-03 13:25:16 -0700263 // Related to the auto-advancing of widgets
264 private final int ADVANCE_MSG = 1;
265 private final int mAdvanceInterval = 20000;
266 private final int mAdvanceStagger = 250;
267 private long mAutoAdvanceSentTime;
268 private long mAutoAdvanceTimeLeft = -1;
269 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
270 new HashMap<View, AppWidgetProviderInfo>();
271
Winson Chung400438b2011-01-16 17:53:48 -0800272 // Determines how long to wait after a rotation before restoring the screen orientation to
273 // match the sensor state.
274 private final int mRestoreScreenOrientationDelay = 500;
275
Michael Jurka4ef207b2010-11-29 17:05:45 -0800276 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700277 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
278 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
279 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800280
Adam Cohen1462de32012-07-24 22:34:36 -0700281 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
282
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700283 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
284
Winson Chung46353de2012-02-16 14:05:10 -0800285 // We only want to get the SharedPreferences once since it does an FS stat each time we get
286 // it from the context.
287 private SharedPreferences mSharedPrefs;
288
Winson Chungf0c6ae02012-03-21 16:10:31 -0700289 // Holds the page that we need to animate to, and the icon views that we need to animate up
290 // when we scroll to that page on resume.
291 private int mNewShortcutAnimatePage = -1;
292 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700293 private ImageView mFolderIconImageView;
294 private Bitmap mFolderIconBitmap;
295 private Canvas mFolderIconCanvas;
296 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700297
Michael Jurkaddd62e92011-02-16 17:49:14 -0800298 private BubbleTextView mWaitingForResume;
299
Michael Jurkac1f5d262011-09-30 19:32:27 -0700300 private Runnable mBuildLayersRunnable = new Runnable() {
301 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700302 if (mWorkspace != null) {
303 mWorkspace.buildPageHardwareLayers();
304 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700305 }
306 };
307
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800308 private static ArrayList<PendingAddArguments> sPendingAddList
309 = new ArrayList<PendingAddArguments>();
310
311 private static class PendingAddArguments {
312 int requestCode;
313 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700314 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800315 int screen;
316 int cellX;
317 int cellY;
318 }
319
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800320 @Override
321 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700322 if (DEBUG_STRICT_MODE) {
323 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
324 .detectDiskReads()
325 .detectDiskWrites()
326 .detectNetwork() // or .detectAll() for all detectable problems
327 .penaltyLog()
328 .build());
329 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
330 .detectLeakedSqlLiteObjects()
331 .detectLeakedClosableObjects()
332 .penaltyLog()
333 .penaltyDeath()
334 .build());
335 }
336
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800338 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700339 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
340 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800341 mModel = app.setLauncher(this);
342 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400343 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700345
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700346 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700347 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
348 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700349
Adam Cohend7d37b12012-07-18 16:15:08 -0700350 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
351 // this also ensures that any synchronous binding below doesn't re-trigger another
352 // LauncherModel load.
353 mPaused = false;
354
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800355 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200356 android.os.Debug.startMethodTracing(
357 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800358 }
359
360 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 setContentView(R.layout.launcher);
362 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700363 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800365 registerContentObservers();
366
Joe Onorato7c312c12009-08-13 21:36:53 -0700367 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700368
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800369 mSavedState = savedInstanceState;
370 restoreState(mSavedState);
371
Winson Chunga12a2502010-12-20 14:41:35 -0800372 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700373 if (mAppsCustomizeContent != null) {
374 mAppsCustomizeContent.onPackagesUpdated();
375 }
Winson Chunga12a2502010-12-20 14:41:35 -0800376
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800377 if (PROFILE_STARTUP) {
378 android.os.Debug.stopMethodTracing();
379 }
380
381 if (!mRestoring) {
Winson Chung36e6c5b2012-07-19 14:53:05 -0700382 if (sPausedFromUserAction) {
383 // If the user leaves launcher, then we should just load items asynchronously when
384 // they return.
385 mModel.startLoader(true, -1);
386 } else {
387 // We only load the page synchronously if the user rotates (or triggers a
388 // configuration change) while launcher is in the foreground
389 mModel.startLoader(true, mWorkspace.getCurrentPage());
390 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800391 }
392
Michael Jurkac57b7a82011-08-09 22:02:20 -0700393 if (!mModel.isAllAppsLoaded()) {
394 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
395 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
396 }
397
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800398 // For handling default keys
399 mDefaultKeySsb = new SpannableStringBuilder();
400 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800401
402 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
403 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800404
Adam Cohenf9426d52012-06-04 17:26:21 -0700405 updateGlobalIcons();
406
407 // On large interfaces, we want the screen to auto-rotate based on the current orientation
408 unlockScreenOrientation(true);
409 }
410
Winson Chung36e6c5b2012-07-19 14:53:05 -0700411 protected void onUserLeaveHint() {
412 super.onUserLeaveHint();
413 sPausedFromUserAction = true;
414 }
415
Adam Cohenf9426d52012-06-04 17:26:21 -0700416 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700417 boolean searchVisible = false;
418 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800419 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700420 int coi = getCurrentOrientationIndexForGlobalIcons();
421 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
422 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700423 updateAppMarketIcon();
424 searchVisible = updateGlobalSearchIcon();
425 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700426 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700427 if (sGlobalSearchIcon[coi] != null) {
428 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700429 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700430 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700431 if (sVoiceSearchIcon[coi] != null) {
432 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700433 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700434 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700435 if (sAppMarketIcon[coi] != null) {
436 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800437 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700438 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 }
Romain Guycbb89e42009-06-08 15:52:54 -0700440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700442 if (sLocaleConfiguration == null) {
443 new AsyncTask<Void, Void, LocaleConfiguration>() {
444 @Override
445 protected LocaleConfiguration doInBackground(Void... unused) {
446 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
447 readConfiguration(Launcher.this, localeConfiguration);
448 return localeConfiguration;
449 }
450
451 @Override
452 protected void onPostExecute(LocaleConfiguration result) {
453 sLocaleConfiguration = result;
454 checkForLocaleChange(); // recursive, but now with a locale configuration
455 }
456 }.execute();
457 return;
458 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 final Configuration configuration = getResources().getConfiguration();
461
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700462 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 final String locale = configuration.locale.toString();
464
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700465 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 final int mcc = configuration.mcc;
467
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700468 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 final int mnc = configuration.mnc;
470
Romain Guy84f296c2009-11-04 15:00:44 -0800471 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472
Romain Guy84f296c2009-11-04 15:00:44 -0800473 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700474 sLocaleConfiguration.locale = locale;
475 sLocaleConfiguration.mcc = mcc;
476 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700477
Joe Onorato0589f0f2010-02-08 13:44:00 -0800478 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700479
480 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
481 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700482 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700483 public void run() {
484 writeConfiguration(Launcher.this, localeConfiguration);
485 }
486 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700487 }
488 }
489
490 private static class LocaleConfiguration {
491 public String locale;
492 public int mcc = -1;
493 public int mnc = -1;
494 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700495
Romain Guy98d01652009-06-30 16:21:04 -0700496 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
497 DataInputStream in = null;
498 try {
499 in = new DataInputStream(context.openFileInput(PREFERENCES));
500 configuration.locale = in.readUTF();
501 configuration.mcc = in.readInt();
502 configuration.mnc = in.readInt();
503 } catch (FileNotFoundException e) {
504 // Ignore
505 } catch (IOException e) {
506 // Ignore
507 } finally {
508 if (in != null) {
509 try {
510 in.close();
511 } catch (IOException e) {
512 // Ignore
513 }
514 }
515 }
516 }
517
518 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
519 DataOutputStream out = null;
520 try {
521 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
522 out.writeUTF(configuration.locale);
523 out.writeInt(configuration.mcc);
524 out.writeInt(configuration.mnc);
525 out.flush();
526 } catch (FileNotFoundException e) {
527 // Ignore
528 } catch (IOException e) {
529 //noinspection ResultOfMethodCallIgnored
530 context.getFileStreamPath(PREFERENCES).delete();
531 } finally {
532 if (out != null) {
533 try {
534 out.close();
535 } catch (IOException e) {
536 // Ignore
537 }
538 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800539 }
540 }
541
Adam Cohen716b51e2011-06-30 12:09:54 -0700542 public DragLayer getDragLayer() {
543 return mDragLayer;
544 }
545
Winson Chung36a62fe2012-05-06 18:04:42 -0700546 boolean isDraggingEnabled() {
547 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
548 // that is subsequently removed from the workspace in startBinding().
549 return !mModel.isLoadingWorkspace();
550 }
551
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 static int getScreen() {
553 synchronized (sLock) {
554 return sScreen;
555 }
556 }
557
558 static void setScreen(int screen) {
559 synchronized (sLock) {
560 sScreen = screen;
561 }
562 }
563
Winson Chung557d6ed2011-07-08 15:34:52 -0700564 /**
565 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
566 * a configuration step, this allows the proper animations to run after other transitions.
567 */
568 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700569 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800570 switch (args.requestCode) {
571 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700572 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
573 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800574 break;
575 case REQUEST_PICK_SHORTCUT:
576 processShortcut(args.intent);
577 break;
578 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700579 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
580 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700581 result = true;
582 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800583 case REQUEST_CREATE_APPWIDGET:
584 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800585 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700586 result = true;
587 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800588 case REQUEST_PICK_WALLPAPER:
589 // We just wanted the activity result here so we can clear mWaitingForResult
590 break;
591 }
Michael Jurka27614d22012-04-02 06:40:22 -0700592 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
593 // if you turned the screen off and then back while in All Apps, Launcher would not
594 // return to the workspace. Clearing mAddInfo.container here fixes this issue
595 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700596 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800597 }
598
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800599 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700600 protected void onActivityResult(
601 final int requestCode, final int resultCode, final Intent data) {
602 if (requestCode == REQUEST_BIND_APPWIDGET) {
603 int appWidgetId = data != null ?
604 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
605 if (resultCode == RESULT_CANCELED) {
606 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
607 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700608 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700609 }
610 return;
611 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700612 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800613 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
614 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700615 mWaitingForResult = false;
616
Adam Cohened66b2b2012-01-23 17:28:51 -0800617 // We have special handling for widgets
618 if (isWidgetDrop) {
619 int appWidgetId = data != null ?
620 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700621 if (appWidgetId < 0) {
622 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
623 "widget configuration activity.");
624 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
625 } else {
626 completeTwoStageWidgetDrop(resultCode, appWidgetId);
627 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800628 return;
629 }
630
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800631 // The pattern used here is that a user PICKs a specific application,
632 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700633
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800634 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
635 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700636 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800637 final PendingAddArguments args = new PendingAddArguments();
638 args.requestCode = requestCode;
639 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700640 args.container = mPendingAddInfo.container;
641 args.screen = mPendingAddInfo.screen;
642 args.cellX = mPendingAddInfo.cellX;
643 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800644 if (isWorkspaceLocked()) {
645 sPendingAddList.add(args);
646 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700647 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800649 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800650 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700651 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800652 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
653 null);
654 }
655
656 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700657 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700658 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800659 Runnable onCompleteRunnable = null;
660 int animationType = 0;
661
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700662 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800663 if (resultCode == RESULT_OK) {
664 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
665 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700666 mPendingAddWidgetInfo);
667 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800668 onCompleteRunnable = new Runnable() {
669 @Override
670 public void run() {
671 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
672 mPendingAddInfo.screen, layout, null);
673 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
674 null);
675 }
676 };
677 } else if (resultCode == RESULT_CANCELED) {
678 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
679 onCompleteRunnable = new Runnable() {
680 @Override
681 public void run() {
682 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
683 null);
684 }
685 };
686 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700687 if (mDragLayer.getAnimatedView() != null) {
688 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
689 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
690 animationType, boundWidget, true);
691 } else {
692 // The animated view may be null in the case of a rotation during widget configuration
693 onCompleteRunnable.run();
694 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 }
696
697 @Override
698 protected void onResume() {
699 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700700
Winson Chung36e6c5b2012-07-19 14:53:05 -0700701 // Restore the previous launcher state
702 if (mOnResumeState == State.WORKSPACE) {
703 showWorkspace(false);
704 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
705 showAllApps(false);
706 }
707 mOnResumeState = State.NONE;
708
Winson Chungde0fb8f2012-05-08 14:37:08 -0700709 // Process any items that were added while Launcher was away
710 InstallShortcutReceiver.flushInstallQueue(this);
711
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800712 mPaused = false;
Winson Chung36e6c5b2012-07-19 14:53:05 -0700713 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700714 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400715 mWorkspaceLoading = true;
Adam Cohend7d37b12012-07-18 16:15:08 -0700716 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400717 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700718 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800719 }
Winson Chunge4e50662012-01-23 14:45:13 -0800720
721 // Reset the pressed state of icons that were locked in the press state while activities
722 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800723 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800724 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800725 mWaitingForResume.setStayPressed(false);
726 }
Winson Chunge4e50662012-01-23 14:45:13 -0800727 if (mAppsCustomizeContent != null) {
728 // Resets the previous all apps icon press state
729 mAppsCustomizeContent.resetDrawableState();
730 }
Adam Cohen06dff352012-06-01 17:17:08 -0700731 // It is possible that widgets can receive updates while launcher is not in the foreground.
732 // Consequently, the widgets will be inflated in the orientation of the foreground activity
733 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
734 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700735 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700736
737 // Again, as with the above scenario, it's possible that one or more of the global icons
738 // were updated in the wrong orientation.
739 updateGlobalIcons();
Adam Cohen06dff352012-06-01 17:17:08 -0700740 }
741
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800742 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700743 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700744 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
745 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
746 // when Launcher resumes and we are still in AllApps.
747 updateWallpaperVisibility(true);
748
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700749 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700750 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800751 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700752 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700753 }
Romain Guycbb89e42009-06-08 15:52:54 -0700754
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700755 @Override
756 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400757 // Flag the loader to stop early before switching
758 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700759 if (mAppsCustomizeContent != null) {
760 mAppsCustomizeContent.surrender();
761 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800762 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700763 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700764
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800765 // We can't hide the IME if it was forced open. So don't bother
766 /*
767 @Override
768 public void onWindowFocusChanged(boolean hasFocus) {
769 super.onWindowFocusChanged(hasFocus);
770
771 if (hasFocus) {
772 final InputMethodManager inputManager = (InputMethodManager)
773 getSystemService(Context.INPUT_METHOD_SERVICE);
774 WindowManager.LayoutParams lp = getWindow().getAttributes();
775 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
776 android.os.Handler()) {
777 protected void onReceiveResult(int resultCode, Bundle resultData) {
778 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
779 }
780 });
781 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
782 }
783 }
784 */
785
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800786 private boolean acceptFilter() {
787 final InputMethodManager inputManager = (InputMethodManager)
788 getSystemService(Context.INPUT_METHOD_SERVICE);
789 return !inputManager.isFullscreenMode();
790 }
791
792 @Override
793 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700794 final int uniChar = event.getUnicodeChar();
795 final boolean handled = super.onKeyDown(keyCode, event);
796 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
797 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800798 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
799 keyCode, event);
800 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700801 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700802 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700803 // showSearchDialog()
804 // If there are multiple keystrokes before the search dialog takes focus,
805 // onSearchRequested() will be called for every keystroke,
806 // but it is idempotent, so it's fine.
807 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 }
809 }
810
Joe Onorato8a9625e2010-01-28 15:55:35 -0800811 // Eat the long press event so the keyboard doesn't come up.
812 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
813 return true;
814 }
815
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800816 return handled;
817 }
818
Karl Rosaen138a0412009-04-23 19:00:21 -0700819 private String getTypedText() {
820 return mDefaultKeySsb.toString();
821 }
822
823 private void clearTypedText() {
824 mDefaultKeySsb.clear();
825 mDefaultKeySsb.clearSpans();
826 Selection.setSelection(mDefaultKeySsb, 0);
827 }
828
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800829 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700830 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
831 * State
832 */
833 private static State intToState(int stateOrdinal) {
834 State state = State.WORKSPACE;
835 final State[] stateValues = State.values();
836 for (int i = 0; i < stateValues.length; i++) {
837 if (stateValues[i].ordinal() == stateOrdinal) {
838 state = stateValues[i];
839 break;
840 }
841 }
842 return state;
843 }
844
845 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 * Restores the previous state, if it exists.
847 *
848 * @param savedState The previous state.
849 */
850 private void restoreState(Bundle savedState) {
851 if (savedState == null) {
852 return;
853 }
854
Michael Jurka883f55b2010-10-21 15:47:14 -0700855 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700856 if (state == State.APPS_CUSTOMIZE) {
Winson Chung36e6c5b2012-07-19 14:53:05 -0700857 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800858 }
859
Winson Chungf0c6ae02012-03-21 16:10:31 -0700860 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700862 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800863 }
864
Winson Chung3d503fb2011-07-13 17:25:49 -0700865 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
866 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700867
Winson Chung3d503fb2011-07-13 17:25:49 -0700868 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
869 mPendingAddInfo.container = pendingAddContainer;
870 mPendingAddInfo.screen = pendingAddScreen;
871 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
872 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700873 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
874 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
875 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700876 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800877 mRestoring = true;
878 }
879
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700880
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800881 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
882 if (renameFolder) {
883 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700884 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 mRestoring = true;
886 }
Winson Chunga12a2502010-12-20 14:41:35 -0800887
Winson Chung785d2eb2011-04-14 16:08:02 -0700888
889 // Restore the AppsCustomize tab
890 if (mAppsCustomizeTabHost != null) {
891 String curTab = savedState.getString("apps_customize_currentTab");
892 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700893 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700894 mAppsCustomizeContent.setContentType(
895 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
896 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700897 mAppsCustomizeContent.loadAssociatedPages(
898 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700899 }
900
Winson Chung5afbf7b2011-07-25 11:53:08 -0700901 int currentIndex = savedState.getInt("apps_customize_currentIndex");
902 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700903 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 }
905
906 /**
907 * Finds all the views we need and configure them properly.
908 */
909 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700910 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400911
Winson Chung4c98d922011-05-31 16:50:48 -0700912 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
913 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800914 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
915 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800916
Winson Chung4c98d922011-05-31 16:50:48 -0700917 // Setup the drag layer
918 mDragLayer.setup(this, dragController);
919
Winson Chung3d503fb2011-07-13 17:25:49 -0700920 // Setup the hotseat
921 mHotseat = (Hotseat) findViewById(R.id.hotseat);
922 if (mHotseat != null) {
923 mHotseat.setup(this);
924 }
925
Winson Chung4c98d922011-05-31 16:50:48 -0700926 // Setup the workspace
927 mWorkspace.setHapticFeedbackEnabled(false);
928 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700929 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700930 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700931
Winson Chungf0ea4d32011-06-06 14:27:16 -0700932 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700933 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700934
Winson Chungf0ea4d32011-06-06 14:27:16 -0700935 // Setup AppsCustomize
936 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
937 findViewById(R.id.apps_customize_pane);
938 mAppsCustomizeContent = (AppsCustomizePagedView)
939 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700940 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700941 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400942
Michael Jurka5130e402011-10-13 04:55:35 -0700943 // Get the all apps button
944 mAllAppsButton = findViewById(R.id.all_apps_button);
945 if (mAllAppsButton != null) {
946 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
947 @Override
948 public boolean onTouch(View v, MotionEvent event) {
949 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
950 onTouchDownAllAppsButton(v);
951 }
952 return false;
953 }
954 });
955 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700956 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700957 dragController.setDragScoller(mWorkspace);
958 dragController.setScrollView(mDragLayer);
959 dragController.setMoveTarget(mWorkspace);
960 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700961 if (mSearchDropTargetBar != null) {
962 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800963 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 }
965
966 /**
967 * Creates a view representing a shortcut.
968 *
969 * @param info The data structure describing the shortcut.
970 *
971 * @return A View inflated from R.layout.application.
972 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800973 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700975 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 }
977
978 /**
979 * Creates a view representing a shortcut inflated from the specified resource.
980 *
981 * @param layoutResId The id of the XML layout used to create the shortcut.
982 * @param parent The group the shortcut belongs to.
983 * @param info The data structure describing the shortcut.
984 *
985 * @return A View inflated from layoutResId.
986 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800987 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800988 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
989 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991 return favorite;
992 }
993
994 /**
995 * Add an application shortcut to the workspace.
996 *
997 * @param data The intent describing the application.
998 * @param cellInfo The position on screen where to create the shortcut.
999 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001000 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001001 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001002 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001003
Adam Cohenfbba09b2011-07-18 21:43:05 -07001004 // First we check if we already know the exact location where we want to add this item.
1005 if (cellX >= 0 && cellY >= 0) {
1006 cellXY[0] = cellX;
1007 cellXY[1] = cellY;
1008 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001009 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001010 return;
1011 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001012
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001013 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001014
Romain Guy73b979d2009-06-09 12:57:21 -07001015 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001016 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001017 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001018 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001019 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001020 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001021 } else {
1022 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 }
1024 }
Romain Guycbb89e42009-06-08 15:52:54 -07001025
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 /**
1027 * Add a shortcut to the workspace.
1028 *
1029 * @param data The intent describing the shortcut.
1030 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001032 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1033 int cellY) {
1034 int[] cellXY = mTmpAddItemCellCoordinates;
1035 int[] touchXY = mPendingAddInfo.dropPos;
1036 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001037
Michael Jurkad3ef3062010-11-23 16:23:58 -08001038 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001039
Adam Cohen558baaf2011-08-15 15:22:57 -07001040 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001041 if (info == null) {
1042 return;
1043 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001044 final View view = createShortcut(info);
1045
Adam Cohenfbba09b2011-07-18 21:43:05 -07001046 // First we check if we already know the exact location where we want to add this item.
1047 if (cellX >= 0 && cellY >= 0) {
1048 cellXY[0] = cellX;
1049 cellXY[1] = cellY;
1050 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001051
1052 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001053 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001054 true, null,null)) {
1055 return;
1056 }
1057 DragObject dragObject = new DragObject();
1058 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001059 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1060 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001061 return;
1062 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001063 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001064 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001065 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001066 foundCellSpan = (result != null);
1067 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001068 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001069 }
1070
1071 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001072 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001073 return;
1074 }
1075
Adam Cohen558baaf2011-08-15 15:22:57 -07001076 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001077
1078 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001079 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1080 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001081 }
1082 }
1083
Adam Cohen2f093b62012-04-30 18:59:53 -07001084 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1085 int minHeight) {
1086 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001087 // We want to account for the extra amount of padding that we are adding to the widget
1088 // to ensure that it gets the full amount of space that it has requested
1089 int requiredWidth = minWidth + padding.left + padding.right;
1090 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001091 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001092 }
1093
Adam Cohen2f093b62012-04-30 18:59:53 -07001094 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1095 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001096 }
1097
Adam Cohen2f093b62012-04-30 18:59:53 -07001098 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1099 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001100 }
1101
Adam Cohen2f093b62012-04-30 18:59:53 -07001102 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1103 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001104 }
1105
Adam Cohen2f093b62012-04-30 18:59:53 -07001106 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1107 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001108 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001109 }
1110
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001112 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001114 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001115 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001117 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1118 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1119 if (appWidgetInfo == null) {
1120 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1121 }
Romain Guycbb89e42009-06-08 15:52:54 -07001122
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001123 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001124 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001125
Adam Cohen2f093b62012-04-30 18:59:53 -07001126 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1127 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001128
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001129 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001130 // We have saved the position to which the widget was dragged-- this really only matters
1131 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001132 int[] cellXY = mTmpAddItemCellCoordinates;
1133 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001134 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001135 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001136 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1137 cellXY[0] = mPendingAddInfo.cellX;
1138 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001139 spanXY[0] = mPendingAddInfo.spanX;
1140 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001141 foundCellSpan = true;
1142 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001143 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001144 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001145 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1146 spanXY[1], cellXY, finalSpan);
1147 spanXY[0] = finalSpan[0];
1148 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001149 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001150 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001151 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001152 }
1153
Michael Jurka0280c3b2010-09-17 15:00:07 -07001154 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001155 if (appWidgetId != -1) {
1156 // Deleting an app widget ID is a void call but writes to disk before returning
1157 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001158 new Thread("deleteAppWidgetId") {
1159 public void run() {
1160 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1161 }
1162 }.start();
1163 }
Winson Chung93eef082012-03-23 15:59:27 -07001164 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001165 return;
1166 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001168 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001169 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1170 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001171 launcherInfo.spanX = spanXY[0];
1172 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001173 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1174 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001175
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001177 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178
1179 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001180 if (hostView == null) {
1181 // Perform actual inflation because we're live
1182 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1183 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1184 } else {
1185 // The AppWidgetHostView has already been inflated and instantiated
1186 launcherInfo.hostView = hostView;
1187 }
Romain Guycbb89e42009-06-08 15:52:54 -07001188
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001189 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001190 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung211bac32012-05-15 13:43:57 -07001191 launcherInfo.notifyWidgetSizeChanged(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001192 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001193 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001194
1195 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001197 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001198 }
Romain Guycbb89e42009-06-08 15:52:54 -07001199
Adam Cohended9f8d2010-11-03 13:25:16 -07001200 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1201 @Override
1202 public void onReceive(Context context, Intent intent) {
1203 final String action = intent.getAction();
1204 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1205 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001206 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001207 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001208
Winson Chungc100e8e2011-08-09 16:02:43 -07001209 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001210 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001211 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1212 mAppsCustomizeTabHost.reset();
1213 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001214 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001215 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1216 mUserPresent = true;
1217 updateRunning();
1218 }
1219 }
1220 };
1221
1222 @Override
1223 public void onAttachedToWindow() {
1224 super.onAttachedToWindow();
1225
1226 // Listen for broadcasts related to user-presence
1227 final IntentFilter filter = new IntentFilter();
1228 filter.addAction(Intent.ACTION_SCREEN_OFF);
1229 filter.addAction(Intent.ACTION_USER_PRESENT);
1230 registerReceiver(mReceiver, filter);
1231
Adam Cohend113e0c2010-11-11 10:48:05 -08001232 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001233 mVisible = true;
1234 }
1235
1236 @Override
1237 public void onDetachedFromWindow() {
1238 super.onDetachedFromWindow();
1239 mVisible = false;
1240
Adam Cohend113e0c2010-11-11 10:48:05 -08001241 if (mAttached) {
1242 unregisterReceiver(mReceiver);
1243 mAttached = false;
1244 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001245 updateRunning();
1246 }
1247
1248 public void onWindowVisibilityChanged(int visibility) {
1249 mVisible = visibility == View.VISIBLE;
1250 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001251 // The following code used to be in onResume, but it turns out onResume is called when
1252 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1253 // is a more appropriate event to handle
1254 if (mVisible) {
1255 mAppsCustomizeTabHost.onWindowVisible();
1256 if (!mWorkspaceLoading) {
1257 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001258 // We want to let Launcher draw itself at least once before we force it to build
1259 // layers on all the workspace pages, so that transitioning to Launcher from other
1260 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1261 // a true draw so we wait until the second preDraw call to be safe
1262 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001263 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001264 // We delay the layer building a bit in order to give
1265 // other message processing a time to run. In particular
1266 // this avoids a delay in hiding the IME if it was
1267 // currently shown, because doing that may involve
1268 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001269 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001270
Michael Jurka6ee21d22012-02-21 18:20:27 -08001271 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001272 return true;
1273 }
1274 });
1275 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001276 // When Launcher comes back to foreground, a different Activity might be responsible for
1277 // the app market intent, so refresh the icon
1278 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001279 clearTypedText();
1280 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001281 }
1282
1283 private void sendAdvanceMessage(long delay) {
1284 mHandler.removeMessages(ADVANCE_MSG);
1285 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1286 mHandler.sendMessageDelayed(msg, delay);
1287 mAutoAdvanceSentTime = System.currentTimeMillis();
1288 }
1289
1290 private void updateRunning() {
1291 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1292 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1293 mAutoAdvanceRunning = autoAdvanceRunning;
1294 if (autoAdvanceRunning) {
1295 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1296 sendAdvanceMessage(delay);
1297 } else {
1298 if (!mWidgetsToAdvance.isEmpty()) {
1299 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1300 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1301 }
1302 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001303 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001304 }
1305 }
1306 }
1307
1308 private final Handler mHandler = new Handler() {
1309 @Override
1310 public void handleMessage(Message msg) {
1311 if (msg.what == ADVANCE_MSG) {
1312 int i = 0;
1313 for (View key: mWidgetsToAdvance.keySet()) {
1314 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1315 final int delay = mAdvanceStagger * i;
1316 if (v instanceof Advanceable) {
1317 postDelayed(new Runnable() {
1318 public void run() {
1319 ((Advanceable) v).advance();
1320 }
1321 }, delay);
1322 }
1323 i++;
1324 }
1325 sendAdvanceMessage(mAdvanceInterval);
1326 }
1327 }
1328 };
1329
1330 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001331 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001332 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1333 if (v instanceof Advanceable) {
1334 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001335 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001336 updateRunning();
1337 }
1338 }
1339
1340 void removeWidgetToAutoAdvance(View hostView) {
1341 if (mWidgetsToAdvance.containsKey(hostView)) {
1342 mWidgetsToAdvance.remove(hostView);
1343 updateRunning();
1344 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001345 }
1346
Joe Onorato9c1289c2009-08-17 11:03:03 -04001347 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001348 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001349 launcherInfo.hostView = null;
1350 }
1351
Winson Chung93eef082012-03-23 15:59:27 -07001352 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1353 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1354 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001355 }
1356
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001357 public LauncherAppWidgetHost getAppWidgetHost() {
1358 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 }
Romain Guycbb89e42009-06-08 15:52:54 -07001360
Winson Chunga9abd0e2010-10-27 17:18:37 -07001361 public LauncherModel getModel() {
1362 return mModel;
1363 }
1364
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001365 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001366 getWindow().closeAllPanels();
1367
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001368 // Whatever we were doing is hereby canceled.
1369 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001370 }
1371
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001372 @Override
1373 protected void onNewIntent(Intent intent) {
1374 super.onNewIntent(intent);
1375
1376 // Close the menu
1377 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001378 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001379 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001380
Joe Onorato14f122b2009-11-19 14:06:36 -08001381 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1382 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001383
Adam Cohenac56cff2011-09-28 20:45:37 -07001384 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001385 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001386 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001387 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1388 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001389 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001390 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001391
1392 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001393 exitSpringLoadedDragMode();
Winson Chung36e6c5b2012-07-19 14:53:05 -07001394
1395 // If we are already on home, then just animate back to the workspace, otherwise, just
1396 // wait until onResume to set the state back to Workspace
1397 if (alreadyOnHome) {
1398 showWorkspace(true);
1399 } else {
1400 mOnResumeState = State.WORKSPACE;
1401 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001402
Joe Onorato3a8820b2009-11-10 15:06:42 -08001403 final View v = getWindow().peekDecorView();
1404 if (v != null && v.getWindowToken() != null) {
1405 InputMethodManager imm = (InputMethodManager)getSystemService(
1406 INPUT_METHOD_SERVICE);
1407 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001409
Michael Jurka35aa14d2011-07-07 17:01:08 -07001410 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001411 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001412 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001413 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
1415 }
1416
1417 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001418 public void onRestoreInstanceState(Bundle state) {
1419 super.onRestoreInstanceState(state);
1420 for (int page: mSynchronouslyBoundPages) {
1421 mWorkspace.restoreInstanceStateForChild(page);
1422 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001423 }
1424
1425 @Override
1426 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001427 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001428 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429
Michael Jurka883f55b2010-10-21 15:47:14 -07001430 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001431 // We close any open folder since it will not be re-opened, and we need to make sure
1432 // this state is reflected.
1433 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434
Winson Chung3d503fb2011-07-13 17:25:49 -07001435 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1436 mWaitingForResult) {
1437 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1438 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1439 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1440 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001441 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1442 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1443 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001444 }
1445
1446 if (mFolderInfo != null && mWaitingForResult) {
1447 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1448 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1449 }
Michael Jurka946ad472010-07-09 18:05:18 -07001450
Winson Chung785d2eb2011-04-14 16:08:02 -07001451 // Save the current AppsCustomize tab
1452 if (mAppsCustomizeTabHost != null) {
1453 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1454 if (currentTabTag != null) {
1455 outState.putString("apps_customize_currentTab", currentTabTag);
1456 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001457 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1458 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001459 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460 }
1461
1462 @Override
1463 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001464 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001465
Winson Chunge7a03942011-08-05 15:05:12 -07001466 // Remove all pending runnables
1467 mHandler.removeMessages(ADVANCE_MSG);
1468 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001469 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001470
Winson Chungcd2b0142011-06-08 16:02:26 -07001471 // Stop callbacks from LauncherModel
1472 LauncherApplication app = ((LauncherApplication) getApplication());
1473 mModel.stopLoader();
1474 app.setLauncher(null);
1475
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001477 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001479 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001480 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001481 mAppWidgetHost = null;
1482
1483 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484
1485 TextKeyListener.getInstance().release();
1486
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487
Winson Chung3d503fb2011-07-13 17:25:49 -07001488 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001489
1490 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001491 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001492
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001493 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001494 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1495 mWorkspace.removeAllViews();
1496 mWorkspace = null;
1497 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001498
1499 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 }
1501
Adam Cohena9cf38f2011-05-02 15:36:58 -07001502 public DragController getDragController() {
1503 return mDragController;
1504 }
1505
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 @Override
1507 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001508 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 super.startActivityForResult(intent, requestCode);
1510 }
1511
Winson Chung70d72102011-08-12 11:24:35 -07001512 /**
1513 * Indicates that we want global search for this activity by setting the globalSearch
1514 * argument for {@link #startSearch} to true.
1515 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001517 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001518 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001519
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001520 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001521
Karl Rosaen138a0412009-04-23 19:00:21 -07001522 if (initialQuery == null) {
1523 // Use any text typed in the launcher as the initial query
1524 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001525 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 if (appSearchData == null) {
1527 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001528 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001530 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001531
Karl Rosaen138a0412009-04-23 19:00:21 -07001532 final SearchManager searchManager =
1533 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001534 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001535 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 }
1537
Winson Chung70d72102011-08-12 11:24:35 -07001538 @Override
1539 public boolean onCreateOptionsMenu(Menu menu) {
1540 if (isWorkspaceLocked()) {
1541 return false;
1542 }
1543
1544 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001545
1546 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1547 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1548 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001549 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1550 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1551 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001552 String helpUrl = getString(R.string.help_url);
1553 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001554 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1555 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1556
Winson Chung70d72102011-08-12 11:24:35 -07001557 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1558 .setIcon(android.R.drawable.ic_menu_gallery)
1559 .setAlphabeticShortcut('W');
1560 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1561 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001562 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001563 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001564 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1565 .setIcon(android.R.drawable.ic_menu_preferences)
1566 .setIntent(settings)
1567 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001568 if (!helpUrl.isEmpty()) {
1569 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1570 .setIcon(android.R.drawable.ic_menu_help)
1571 .setIntent(help)
1572 .setAlphabeticShortcut('H');
1573 }
Winson Chung70d72102011-08-12 11:24:35 -07001574 return true;
1575 }
1576
1577 @Override
1578 public boolean onPrepareOptionsMenu(Menu menu) {
1579 super.onPrepareOptionsMenu(menu);
1580
1581 if (mAppsCustomizeTabHost.isTransitioning()) {
1582 return false;
1583 }
1584 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1585 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1586
1587 return true;
1588 }
1589
1590 @Override
1591 public boolean onOptionsItemSelected(MenuItem item) {
1592 switch (item.getItemId()) {
1593 case MENU_WALLPAPER_SETTINGS:
1594 startWallpaper();
1595 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001596 }
1597
1598 return super.onOptionsItemSelected(item);
1599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001601 @Override
1602 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001603 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001604 // Use a custom animation for launching search
1605 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001606 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001607 }
1608
Joe Onorato9c1289c2009-08-17 11:03:03 -04001609 public boolean isWorkspaceLocked() {
1610 return mWorkspaceLoading || mWaitingForResult;
1611 }
1612
Michael Jurka0280c3b2010-09-17 15:00:07 -07001613 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001614 mPendingAddInfo.container = ItemInfo.NO_ID;
1615 mPendingAddInfo.screen = -1;
1616 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1617 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001618 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001619 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001620 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001621
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001622 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1623 AppWidgetProviderInfo appWidgetInfo) {
1624 if (appWidgetInfo.configure != null) {
1625 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001626
Bjorn Bringert7984c942009-12-09 15:38:25 +00001627 // Launch over to configure widget, if needed
1628 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001629 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001630 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001631 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001633 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001634 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1635 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001636 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001637 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 }
1639 }
Romain Guycbb89e42009-06-08 15:52:54 -07001640
Adam Cohenfbba09b2011-07-18 21:43:05 -07001641 /**
1642 * Process a shortcut drop.
1643 *
1644 * @param componentName The name of the component
1645 * @param screen The screen where it should be added
1646 * @param cell The cell it should be added to, optional
1647 * @param position The location on the screen where it was dropped, optional
1648 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001649 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1650 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001651 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001652 mPendingAddInfo.container = container;
1653 mPendingAddInfo.screen = screen;
1654 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001655
1656 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001657 mPendingAddInfo.cellX = cell[0];
1658 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001659 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001660
1661 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1662 createShortcutIntent.setComponent(componentName);
1663 processShortcut(createShortcutIntent);
1664 }
1665
Adam Cohenfbba09b2011-07-18 21:43:05 -07001666 /**
1667 * Process a widget drop.
1668 *
1669 * @param info The PendingAppWidgetInfo of the widget being added.
1670 * @param screen The screen where it should be added
1671 * @param cell The cell it should be added to, optional
1672 * @param position The location on the screen where it was dropped, optional
1673 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001674 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001675 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001676 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001677 mPendingAddInfo.container = info.container = container;
1678 mPendingAddInfo.screen = info.screen = screen;
1679 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001680 mPendingAddInfo.minSpanX = info.minSpanX;
1681 mPendingAddInfo.minSpanY = info.minSpanY;
1682
Adam Cohenfbba09b2011-07-18 21:43:05 -07001683 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001684 mPendingAddInfo.cellX = cell[0];
1685 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001686 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001687 if (span != null) {
1688 mPendingAddInfo.spanX = span[0];
1689 mPendingAddInfo.spanY = span[1];
1690 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001691
Adam Cohened66b2b2012-01-23 17:28:51 -08001692 AppWidgetHostView hostView = info.boundWidget;
1693 int appWidgetId;
1694 if (hostView != null) {
1695 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001696 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001697 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001698 // In this case, we either need to start an activity to get permission to bind
1699 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001700 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001701 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001702 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001703 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001704 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001705 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1706 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1707 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1708 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1709 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001710 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001711 }
1712
Joe Onoratodeb98af2010-02-19 14:59:39 -08001713 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001714 // Handle case where user selected "Applications"
1715 String applicationName = getResources().getString(R.string.group_applications);
1716 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001717
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001718 if (applicationName != null && applicationName.equals(shortcutName)) {
1719 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1720 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001721
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001722 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1723 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001724 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001725 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001726 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001727 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001728 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 }
1730
Winson Chung24ab2f12010-09-16 14:10:47 -07001731 void processWallpaper(Intent intent) {
1732 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1733 }
1734
Winson Chung3d503fb2011-07-13 17:25:49 -07001735 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1736 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001737 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 folderInfo.title = getText(R.string.folder_name);
1739
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001740 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001741 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1742 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001743 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001744
1745 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001746 FolderIcon newFolder =
1747 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1748 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1749 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001750 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 }
Romain Guycbb89e42009-06-08 15:52:54 -07001752
Joe Onorato9c1289c2009-08-17 11:03:03 -04001753 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001754 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001755 }
1756
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001758 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001760 Intent chooser = Intent.createChooser(pickWallpaper,
1761 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001762 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1763 // Removed in Eclair MR1
1764// WallpaperManager wm = (WallpaperManager)
1765// getSystemService(Context.WALLPAPER_SERVICE);
1766// WallpaperInfo wi = wm.getWallpaperInfo();
1767// if (wi != null && wi.getSettingsActivity() != null) {
1768// LabeledIntent li = new LabeledIntent(getPackageName(),
1769// R.string.configure_wallpaper, 0);
1770// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1771// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1772// }
Mike Clerona0618e42009-10-22 13:55:21 -07001773 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001774 }
1775
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001776 /**
1777 * Registers various content observers. The current implementation registers
1778 * only a favorites observer to keep track of the favorites applications.
1779 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001780 private void registerContentObservers() {
1781 ContentResolver resolver = getContentResolver();
1782 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1783 true, mWidgetObserver);
1784 }
1785
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001786 @Override
1787 public boolean dispatchKeyEvent(KeyEvent event) {
1788 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1789 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001790 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001791 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001792 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001793 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001794 dumpState();
1795 return true;
1796 }
1797 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001798 }
1799 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1800 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001801 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 return true;
1803 }
1804 }
1805
1806 return super.dispatchKeyEvent(event);
1807 }
1808
Joe Onorato88ec0992009-11-19 13:16:06 -08001809 @Override
1810 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001811 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001812 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001813 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001814 Folder openFolder = mWorkspace.getOpenFolder();
1815 if (openFolder.isEditingName()) {
1816 openFolder.dismissEditingName();
1817 } else {
1818 closeFolder();
1819 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001820 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001821 mWorkspace.exitWidgetResizeMode();
1822
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001823 // Back button is a no-op here, but give at least some feedback for the button press
1824 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001825 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001826 }
1827
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001828 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001829 * Re-listen when widgets are reset.
1830 */
1831 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001832 if (mAppWidgetHost != null) {
1833 mAppWidgetHost.startListening();
1834 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001835 }
1836
1837 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001838 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1839 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001840 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001841 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001842 if (mModel != null) {
1843 mModel.unbindWorkspaceItems();
1844 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001846
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001847 /**
1848 * Launches the intent referred by the clicked shortcut.
1849 *
1850 * @param v The view representing the clicked shortcut.
1851 */
1852 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001853 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1854 // view has detached (it's possible for this to happen if the view is removed mid touch).
1855 if (v.getWindowToken() == null) {
1856 return;
1857 }
1858
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001859 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001860 return;
1861 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001862
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001864 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001866 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001867 int[] pos = new int[2];
1868 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001869 intent.setSourceBounds(new Rect(pos[0], pos[1],
1870 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001871
1872 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001873
1874 if (success && v instanceof BubbleTextView) {
1875 mWaitingForResume = (BubbleTextView) v;
1876 mWaitingForResume.setStayPressed(true);
1877 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001879 if (v instanceof FolderIcon) {
1880 FolderIcon fi = (FolderIcon) v;
1881 handleFolderClick(fi);
1882 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001883 } else if (v == mAllAppsButton) {
1884 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001885 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001886 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001887 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001888 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001889 }
1890 }
1891
Michael Jurka0e260592010-06-30 17:07:39 -07001892 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001893 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001894 // clicking anywhere on the workspace causes the customization drawer to slide down
1895 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001896 return false;
1897 }
1898
Michael Jurkaaf442092010-06-10 17:01:57 -07001899 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001900 * Event handler for the search button
1901 *
1902 * @param v The view that was clicked.
1903 */
1904 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001905 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1906
Amith Yamasania135ba82011-08-09 17:42:01 -07001907 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001908 }
1909
1910 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001911 * Event handler for the voice button
1912 *
1913 * @param v The view that was clicked.
1914 */
1915 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001916 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001917
Michael Jurkaae65ee32012-04-30 11:45:54 -07001918 try {
1919 final SearchManager searchManager =
1920 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1921 ComponentName activityName = searchManager.getGlobalSearchActivity();
1922 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001923 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001924 if (activityName != null) {
1925 intent.setPackage(activityName.getPackageName());
1926 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001927 startActivity(null, intent, "onClickVoiceButton");
Winson Chung01b0b632012-05-22 10:18:14 -07001928 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001929 } catch (ActivityNotFoundException e) {
1930 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001931 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001932 startActivitySafely(null, intent, "onClickVoiceButton");
1933 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001934 }
1935
1936 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001937 * Event handler for the "grid" button that appears on the home screen, which
1938 * enters all apps mode.
1939 *
1940 * @param v The view that was clicked.
1941 */
1942 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001943 showAllApps(true);
1944 }
1945
1946 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001947 // Provide the same haptic feedback that the system offers for virtual keys.
1948 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001949 }
1950
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001951 public void onClickAppMarketButton(View v) {
1952 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001953 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001954 } else {
1955 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001956 }
1957 }
1958
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001959 void startApplicationDetailsActivity(ComponentName componentName) {
1960 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001961 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1962 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001963 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001964 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001965 }
1966
Patrick Dubroy5539af72010-09-07 15:22:01 -07001967 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1968 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1969 // System applications cannot be installed. For now, show a toast explaining that.
1970 // We may give them the option of disabling apps this way.
1971 int messageId = R.string.uninstall_system_app_text;
1972 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1973 } else {
1974 String packageName = appInfo.componentName.getPackageName();
1975 String className = appInfo.componentName.getClassName();
1976 Intent intent = new Intent(
1977 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001978 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1979 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001980 startActivity(intent);
1981 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001982 }
1983
Michael Jurka86a720e2012-05-09 11:23:23 -07001984 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001986
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001987 try {
Winson Chung2672ff92012-05-04 16:22:30 -07001988 // Only launch using the new animation if the shortcut has not opted out (this is a
1989 // private contract between launcher and may be ignored in the future).
1990 boolean useLaunchAnimation = (v != null) &&
1991 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1992 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07001993 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1994 v.getMeasuredWidth(), v.getMeasuredHeight());
1995
1996 startActivity(intent, opts.toBundle());
1997 } else {
1998 startActivity(intent);
1999 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002000 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001 } catch (SecurityException e) {
2002 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002003 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002005 "or use the exported attribute for this activity. "
2006 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002008 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002010
Michael Jurka86a720e2012-05-09 11:23:23 -07002011 boolean startActivitySafely(View v, Intent intent, Object tag) {
2012 boolean success = false;
2013 try {
2014 success = startActivity(v, intent, tag);
2015 } catch (ActivityNotFoundException e) {
2016 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2017 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2018 }
2019 return success;
2020 }
2021
Romain Guy8e633c52010-03-04 12:51:36 -08002022 void startActivityForResultSafely(Intent intent, int requestCode) {
2023 try {
2024 startActivityForResult(intent, requestCode);
2025 } catch (ActivityNotFoundException e) {
2026 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2027 } catch (SecurityException e) {
2028 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2029 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2030 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2031 "or use the exported attribute for this activity.", e);
2032 }
2033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034
Adam Cohena9cf38f2011-05-02 15:36:58 -07002035 private void handleFolderClick(FolderIcon folderIcon) {
2036 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07002037 Folder openFolder = mWorkspace.getFolderForTag(info);
2038
2039 // If the folder info reports that the associated folder is open, then verify that
2040 // it is actually opened. There have been a few instances where this gets out of sync.
2041 if (info.opened && openFolder == null) {
2042 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2043 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2044 info.opened = false;
2045 }
2046
Adam Cohena9cf38f2011-05-02 15:36:58 -07002047 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048 // Close any open folder
2049 closeFolder();
2050 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002051 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 } else {
2053 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054 int folderScreen;
2055 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002056 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002057 // .. and close it
2058 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002059 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 // Close any folder open on the current screen
2061 closeFolder();
2062 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002063 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 }
2065 }
2066 }
2067 }
2068
Adam Cohen268c4752012-06-06 17:47:33 -07002069 /**
2070 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2071 * in the DragLayer in the exact absolute location of the original FolderIcon.
2072 */
2073 private void copyFolderIconToImage(FolderIcon fi) {
2074 final int width = fi.getMeasuredWidth();
2075 final int height = fi.getMeasuredHeight();
2076
2077 // Lazy load ImageView, Bitmap and Canvas
2078 if (mFolderIconImageView == null) {
2079 mFolderIconImageView = new ImageView(this);
2080 }
2081 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2082 mFolderIconBitmap.getHeight() != height) {
2083 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2084 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2085 }
2086
2087 DragLayer.LayoutParams lp;
2088 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2089 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2090 } else {
2091 lp = new DragLayer.LayoutParams(width, height);
2092 }
2093
2094 mDragLayer.getViewRectRelativeToSelf(fi, mRectForFolderAnimation);
2095 lp.customPosition = true;
2096 lp.x = mRectForFolderAnimation.left;
2097 lp.y = mRectForFolderAnimation.top;
Adam Cohen8ec23032012-06-08 14:46:22 -07002098 lp.width = width;
2099 lp.height = height;
Adam Cohen268c4752012-06-06 17:47:33 -07002100
2101 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2102 fi.draw(mFolderIconCanvas);
2103 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohen8ec23032012-06-08 14:46:22 -07002104 if (fi.mFolder != null) {
2105 mFolderIconImageView.setPivotX(fi.mFolder.getPivotXForIconAnimation());
2106 mFolderIconImageView.setPivotY(fi.mFolder.getPivotYForIconAnimation());
2107 }
Adam Cohen268c4752012-06-06 17:47:33 -07002108 // Just in case this image view is still in the drag layer from a previous animation,
2109 // we remove it and re-add it.
2110 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2111 mDragLayer.removeView(mFolderIconImageView);
2112 }
2113 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohen8ec23032012-06-08 14:46:22 -07002114 if (fi.mFolder != null) {
2115 fi.mFolder.bringToFront();
2116 }
Adam Cohen268c4752012-06-06 17:47:33 -07002117 }
2118
Adam Cohen2801caf2011-05-13 20:57:39 -07002119 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002120 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002121 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2122 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2123 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2124
Adam Cohenc51934b2011-07-26 21:07:43 -07002125 FolderInfo info = (FolderInfo) fi.getTag();
2126 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2127 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002128 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2129 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002130 }
2131
Adam Cohen268c4752012-06-06 17:47:33 -07002132 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2133 copyFolderIconToImage(fi);
2134 fi.setVisibility(View.INVISIBLE);
2135
2136 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(mFolderIconImageView, alpha,
2137 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002138 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2139 oa.start();
2140 }
2141
Adam Cohen268c4752012-06-06 17:47:33 -07002142 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002143 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002144 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2145 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2146 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2147
Adam Cohen268c4752012-06-06 17:47:33 -07002148 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002149
Adam Cohen268c4752012-06-06 17:47:33 -07002150 // We remove and re-draw the FolderIcon in-case it has changed
2151 mDragLayer.removeView(mFolderIconImageView);
2152 copyFolderIconToImage(fi);
Adam Cohen268c4752012-06-06 17:47:33 -07002153 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(mFolderIconImageView, alpha,
2154 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002155 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002156 oa.addListener(new AnimatorListenerAdapter() {
2157 @Override
2158 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002159 if (cl != null) {
2160 cl.clearFolderLeaveBehind();
2161 // Remove the ImageView copy of the FolderIcon and make the original visible.
2162 mDragLayer.removeView(mFolderIconImageView);
2163 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002164 }
2165 }
2166 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002167 oa.start();
2168 }
2169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002171 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 * is animated relative to the specified View. If the View is null, no animation
2173 * is played.
2174 *
2175 * @param folderInfo The FolderInfo describing the folder to open.
2176 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002177 public void openFolder(FolderIcon folderIcon) {
2178 Folder folder = folderIcon.mFolder;
2179 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180
Adam Cohena9cf38f2011-05-02 15:36:58 -07002181 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002182
Adam Cohen4554ee12011-08-03 16:13:21 -07002183 // Just verify that the folder hasn't already been added to the DragLayer.
2184 // There was a one-off crash where the folder had a parent already.
2185 if (folder.getParent() == null) {
2186 mDragLayer.addView(folder);
2187 mDragController.addDropTarget((DropTarget) folder);
2188 } else {
2189 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2190 folder.getParent() + ").");
2191 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002192 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002193 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002194 }
2195
2196 public void closeFolder() {
2197 Folder folder = mWorkspace.getOpenFolder();
2198 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002199 if (folder.isEditingName()) {
2200 folder.dismissEditingName();
2201 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002202 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002203
2204 // Dismiss the folder cling
2205 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002206 }
2207 }
2208
2209 void closeFolder(Folder folder) {
2210 folder.getInfo().opened = false;
2211
2212 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2213 if (parent != null) {
2214 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2215 shrinkAndFadeInFolderIcon(fi);
2216 }
2217 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 }
2219
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002220 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002221 if (!isDraggingEnabled()) return false;
2222 if (isWorkspaceLocked()) return false;
2223 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224
2225 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002226 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002227 }
2228
Michael Jurka0280c3b2010-09-17 15:00:07 -07002229 resetAddInfo();
2230 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002232 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 return true;
2234 }
2235
Winson Chung3d503fb2011-07-13 17:25:49 -07002236 // The hotseat touch handling does not go through Workspace, and we always allow long press
2237 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002238 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002239 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2240 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002241 if (itemUnderLongClick == null) {
2242 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002243 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2244 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002245 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002247 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002249 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002250 }
2251 }
2252 }
2253 return true;
2254 }
2255
Winson Chung3d503fb2011-07-13 17:25:49 -07002256 boolean isHotseatLayout(View layout) {
2257 return mHotseat != null && layout != null &&
2258 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2259 }
2260 Hotseat getHotseat() {
2261 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002262 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002263 SearchDropTargetBar getSearchBar() {
2264 return mSearchDropTargetBar;
2265 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002266
Winson Chung3d503fb2011-07-13 17:25:49 -07002267 /**
2268 * Returns the CellLayout of the specified container at the specified screen.
2269 */
2270 CellLayout getCellLayout(long container, int screen) {
2271 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2272 if (mHotseat != null) {
2273 return mHotseat.getLayout();
2274 } else {
2275 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002276 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002277 } else {
2278 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002279 }
2280 }
2281
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002282 Workspace getWorkspace() {
2283 return mWorkspace;
2284 }
2285
Daniel Sandler843e8602010-06-07 14:59:01 -04002286 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2287 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002288 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002289 }
2290
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002291 public boolean isAllAppsButtonRank(int rank) {
2292 return mHotseat.isAllAppsButtonRank(rank);
2293 }
2294
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002295 // AllAppsView.Watcher
2296 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002297 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002298 mWorkspace.setVisibility(View.GONE);
2299 }
2300 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002301
2302 /**
2303 * Helper method for the cameraZoomIn/cameraZoomOut animations
2304 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002305 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002306 * @param scaleFactor The scale factor used for the zoom
2307 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002308 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002309 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002310 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002311 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002312
Winson Chung18f41f82012-05-09 13:28:10 -07002313 void disableWallpaperIfInAllApps() {
2314 // Only disable it if we are in all apps
2315 if (mState == State.APPS_CUSTOMIZE) {
2316 if (mAppsCustomizeTabHost != null &&
2317 !mAppsCustomizeTabHost.isTransitioning()) {
2318 updateWallpaperVisibility(false);
2319 }
2320 }
2321 }
2322
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002323 void updateWallpaperVisibility(boolean visible) {
2324 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2325 int curflags = getWindow().getAttributes().flags
2326 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2327 if (wpflags != curflags) {
2328 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2329 }
2330 }
2331
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002332 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2333 if (v instanceof LauncherTransitionable) {
2334 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2335 }
2336 }
2337
Michael Jurkabed61d22012-02-14 22:51:29 -08002338 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2339 if (v instanceof LauncherTransitionable) {
2340 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2341 }
Winson Chung70442722012-02-10 15:43:22 -08002342
2343 // Update the workspace transition step as well
2344 dispatchOnLauncherTransitionStep(v, 0f);
2345 }
2346
2347 private void dispatchOnLauncherTransitionStep(View v, float t) {
2348 if (v instanceof LauncherTransitionable) {
2349 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2350 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002351 }
2352
2353 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2354 if (v instanceof LauncherTransitionable) {
2355 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2356 }
Winson Chung70442722012-02-10 15:43:22 -08002357
2358 // Update the workspace transition step as well
2359 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002360 }
2361
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002362 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002363 * Things to test when changing the following seven functions.
2364 * - Home from workspace
2365 * - from center screen
2366 * - from other screens
2367 * - Home from all apps
2368 * - from center screen
2369 * - from other screens
2370 * - Back from all apps
2371 * - from center screen
2372 * - from other screens
2373 * - Launch app from workspace and quit
2374 * - with back
2375 * - with home
2376 * - Launch app from all apps and quit
2377 * - with back
2378 * - with home
2379 * - Go to a screen that's not the default, then all
2380 * apps, and launch and app, and go back
2381 * - with back
2382 * -with home
2383 * - On workspace, long press power and go back
2384 * - with back
2385 * - with home
2386 * - On all apps, long press power and go back
2387 * - with back
2388 * - with home
2389 * - On workspace, power off
2390 * - On all apps, power off
2391 * - Launch an app and turn off the screen while in that app
2392 * - Go back with home key
2393 * - Go back with back key TODO: make this not go to workspace
2394 * - From all apps
2395 * - From workspace
2396 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2397 * - From all apps
2398 * - From the center workspace
2399 * - From another workspace
2400 */
2401
2402 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002403 * Zoom the camera out from the workspace to reveal 'toView'.
2404 * Assumes that the view to show is anchored at either the very top or very bottom
2405 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002406 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002407 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002408 if (mStateAnimation != null) {
2409 mStateAnimation.cancel();
2410 mStateAnimation = null;
2411 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002412 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002413
Winson Chungf0ea4d32011-06-06 14:27:16 -07002414 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2415 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2416 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002417 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002418 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002419 final int startDelay =
2420 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002421
Michael Jurkab3e22d92011-10-31 15:58:33 -07002422 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002423
Michael Jurkad74c9842011-07-10 12:44:21 -07002424 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002425 Animator workspaceAnim =
2426 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002427
2428 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002429 toView.setScaleX(scale);
2430 toView.setScaleY(scale);
2431 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2432 scaleAnim.
2433 scaleX(1f).scaleY(1f).
2434 setDuration(duration).
2435 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002436
Winson Chungf0ea4d32011-06-06 14:27:16 -07002437 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002438 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002439 final ObjectAnimator alphaAnim = ObjectAnimator
2440 .ofFloat(toView, "alpha", 0f, 1f)
2441 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002442 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002443 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2444 @Override
2445 public void onAnimationUpdate(ValueAnimator animation) {
2446 float t = (Float) animation.getAnimatedValue();
2447 dispatchOnLauncherTransitionStep(fromView, t);
2448 dispatchOnLauncherTransitionStep(toView, t);
2449 }
2450 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002451
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002452 // toView should appear right at the end of the workspace shrink
2453 // animation
2454 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002455 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002456 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002457
2458 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002459 boolean animationCancelled = false;
2460
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002461 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002462 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002463 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002464 // Prepare the position
2465 toView.setTranslationX(0.0f);
2466 toView.setTranslationY(0.0f);
2467 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002468 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002469 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002470 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002471 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002472 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2473 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002474
2475 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2476 // Hide the workspace scrollbar
2477 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002478 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002479 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002480 if (!animationCancelled) {
2481 updateWallpaperVisibility(false);
2482 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002483
2484 // Hide the search bar
2485 mSearchDropTargetBar.hideSearchBar(false);
Adam Cohenf4ddea32011-09-12 13:46:42 -07002486 }
2487
Adam Cohencff6af82011-09-13 14:51:53 -07002488 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002489 public void onAnimationCancel(Animator animation) {
2490 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002491 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002492 });
2493
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002494 if (workspaceAnim != null) {
2495 mStateAnimation.play(workspaceAnim);
2496 }
Michael Jurka1899a362011-11-03 13:50:45 -07002497
2498 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002499 final ViewTreeObserver observer;
2500
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002501 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2502 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002503
2504 // If any of the objects being animated haven't been measured/laid out
2505 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002506 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002507 (mWorkspace.getMeasuredWidth() == 0) ||
2508 (toView.getMeasuredWidth() == 0)) {
2509 observer = mWorkspace.getViewTreeObserver();
2510 delayAnim = true;
2511 } else {
2512 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002513 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002514
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002515 final AnimatorSet stateAnimation = mStateAnimation;
2516 final Runnable startAnimRunnable = new Runnable() {
2517 public void run() {
2518 // Check that mStateAnimation hasn't changed while
2519 // we waited for a layout/draw pass
2520 if (mStateAnimation != stateAnimation)
2521 return;
2522 setPivotsForZoom(toView, scale);
2523 dispatchOnLauncherTransitionStart(fromView, animated, false);
2524 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka382aa182012-06-11 15:42:07 -07002525 toView.post(new Runnable() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002526 public void run() {
2527 // Check that mStateAnimation hasn't changed while
2528 // we waited for a layout/draw pass
2529 if (mStateAnimation != stateAnimation)
2530 return;
2531 mStateAnimation.start();
2532 }
2533 });
2534 }
2535 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002536 if (delayAnim) {
2537 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2538 public void onGlobalLayout() {
Michael Jurka382aa182012-06-11 15:42:07 -07002539 toView.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002540 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002541 }
2542 };
2543 observer.addOnGlobalLayoutListener(delayedStart);
2544 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002545 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002546 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002547 } else {
2548 toView.setTranslationX(0.0f);
2549 toView.setTranslationY(0.0f);
2550 toView.setScaleX(1.0f);
2551 toView.setScaleY(1.0f);
2552 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002553 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002554
Michael Jurkabed61d22012-02-14 22:51:29 -08002555 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2556 // Hide the workspace scrollbar
2557 mWorkspace.hideScrollingIndicator(true);
2558 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002559
2560 // Hide the search bar
2561 mSearchDropTargetBar.hideSearchBar(false);
Michael Jurkaabded662011-03-04 12:06:57 -08002562 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002563 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002564 dispatchOnLauncherTransitionStart(fromView, animated, false);
2565 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002566 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002567 dispatchOnLauncherTransitionStart(toView, animated, false);
2568 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002569 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002570 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002571 }
2572
2573 /**
2574 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002575 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002576 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002577 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002578 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2579 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002580
Michael Jurkab3e22d92011-10-31 15:58:33 -07002581 if (mStateAnimation != null) {
2582 mStateAnimation.cancel();
2583 mStateAnimation = null;
2584 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002585 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002586
Winson Chungf0ea4d32011-06-06 14:27:16 -07002587 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002588 final int fadeOutDuration =
2589 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002590 final float scaleFactor = (float)
2591 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2592 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002593 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002594 Animator workspaceAnim = null;
2595
2596 if (toState == State.WORKSPACE) {
2597 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2598 workspaceAnim = mWorkspace.getChangeStateAnimation(
2599 Workspace.State.NORMAL, animated, stagger);
2600 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2601 workspaceAnim = mWorkspace.getChangeStateAnimation(
2602 Workspace.State.SPRING_LOADED, animated);
2603 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002604
Michael Jurkab3e22d92011-10-31 15:58:33 -07002605 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002606 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002607 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002608 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002609 final LauncherViewPropertyAnimator scaleAnim =
2610 new LauncherViewPropertyAnimator(fromView);
2611 scaleAnim.
2612 scaleX(scaleFactor).scaleY(scaleFactor).
2613 setDuration(duration).
2614 setInterpolator(new Workspace.ZoomInInterpolator());
2615
2616 final ObjectAnimator alphaAnim = ObjectAnimator
2617 .ofFloat(fromView, "alpha", 1f, 0f)
2618 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002619 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002620 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2621 @Override
2622 public void onAnimationUpdate(ValueAnimator animation) {
2623 float t = 1f - (Float) animation.getAnimatedValue();
2624 dispatchOnLauncherTransitionStep(fromView, t);
2625 dispatchOnLauncherTransitionStep(toView, t);
2626 }
2627 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002628
Michael Jurkabed61d22012-02-14 22:51:29 -08002629 mStateAnimation = new AnimatorSet();
2630
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002631 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2632 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002633
2634 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002635 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002636 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002637 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002638 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002639 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2640 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002641 if (mWorkspace != null) {
2642 mWorkspace.hideScrollingIndicator(false);
2643 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002644 if (onCompleteRunnable != null) {
2645 onCompleteRunnable.run();
2646 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002647 }
2648 });
2649
Winson Chungf0ea4d32011-06-06 14:27:16 -07002650 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002651 if (workspaceAnim != null) {
2652 mStateAnimation.play(workspaceAnim);
2653 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002654 dispatchOnLauncherTransitionStart(fromView, animated, true);
2655 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002656 final Animator stateAnimation = mStateAnimation;
2657 mWorkspace.post(new Runnable() {
2658 public void run() {
2659 if (stateAnimation != mStateAnimation)
2660 return;
2661 mStateAnimation.start();
2662 }
2663 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002664 } else {
2665 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002666 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002667 dispatchOnLauncherTransitionStart(fromView, animated, true);
2668 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002669 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002670 dispatchOnLauncherTransitionStart(toView, animated, true);
2671 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002672 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002673 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002674 }
2675
Michael Jurkae326f182011-11-21 14:05:46 -08002676 @Override
2677 public void onTrimMemory(int level) {
2678 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002679 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002680 mAppsCustomizeTabHost.onTrimMemory();
2681 }
2682 }
2683
Winson Chung18f41f82012-05-09 13:28:10 -07002684 @Override
2685 public void onWindowFocusChanged(boolean hasFocus) {
2686 if (!hasFocus) {
2687 // When another window occludes launcher (like the notification shade, or recents),
2688 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2689 updateWallpaperVisibility(true);
2690 } else {
2691 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002692 mWorkspace.postDelayed(new Runnable() {
2693 @Override
2694 public void run() {
2695 disableWallpaperIfInAllApps();
2696 }
2697 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002698 }
2699 }
2700
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002701 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002702 showWorkspace(animated, null);
2703 }
2704
2705 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002706 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002707 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002708 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002709 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002710
Winson Chungc7d2b602012-05-16 17:02:20 -07002711 // Show the search bar (only animate if we were showing the drop target bar in spring
2712 // loaded mode)
2713 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2714
Michael Jurkab737ee62011-11-15 15:57:22 -08002715 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002716 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002717
2718 // Set focus to the AppsCustomize button
2719 if (mAllAppsButton != null) {
2720 mAllAppsButton.requestFocus();
2721 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002722 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002723
Michael Jurkab737ee62011-11-15 15:57:22 -08002724 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002725
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002726 // Change the state *after* we've called all the transition code
2727 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002728
Winson Chung5fb63472011-02-02 17:03:37 -08002729 // Resume the auto-advance of widgets
2730 mUserPresent = true;
2731 updateRunning();
2732
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002733 // send an accessibility event to announce the context change
2734 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002735 }
2736
Michael Jurkab3e22d92011-10-31 15:58:33 -07002737 void showAllApps(boolean animated) {
2738 if (mState != State.WORKSPACE) return;
2739
2740 showAppsCustomizeHelper(animated, false);
2741 mAppsCustomizeTabHost.requestFocus();
2742
Michael Jurkab3e22d92011-10-31 15:58:33 -07002743 // Change the state *after* we've called all the transition code
2744 mState = State.APPS_CUSTOMIZE;
2745
2746 // Pause the auto-advance of widgets until we are out of AllApps
2747 mUserPresent = false;
2748 updateRunning();
2749 closeFolder();
2750
2751 // Send an accessibility event to announce the context change
2752 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2753 }
2754
Adam Cohen7777d962011-08-18 18:58:38 -07002755 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002756 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002757 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002758 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002759 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002760 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002761 }
Adam Cohen7777d962011-08-18 18:58:38 -07002762
Adam Cohened66b2b2012-01-23 17:28:51 -08002763 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2764 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002765 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2766
Winson Chunge7a03942011-08-05 15:05:12 -07002767 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002768 @Override
2769 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002770 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002771 // Before we show workspace, hide all apps again because
2772 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2773 // clean up our state transition functions
2774 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002775 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002776 } else {
2777 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002778 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002779 }
2780 }, (extendedDelay ?
2781 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2782 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2783 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002784
Michael Jurkad3ef3062010-11-23 16:23:58 -08002785 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002786 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002787 final boolean animated = true;
2788 final boolean springLoaded = true;
2789 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002790 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002791 }
2792 // Otherwise, we are not in spring loaded mode, so don't do anything.
2793 }
2794
Michael Jurkab737ee62011-11-15 15:57:22 -08002795 void hideDockDivider() {
2796 if (mQsbDivider != null && mDockDivider != null) {
2797 mQsbDivider.setVisibility(View.INVISIBLE);
2798 mDockDivider.setVisibility(View.INVISIBLE);
2799 }
2800 }
2801
2802 void showDockDivider(boolean animated) {
2803 if (mQsbDivider != null && mDockDivider != null) {
2804 mQsbDivider.setVisibility(View.VISIBLE);
2805 mDockDivider.setVisibility(View.VISIBLE);
2806 if (mDividerAnimator != null) {
2807 mDividerAnimator.cancel();
2808 mQsbDivider.setAlpha(1f);
2809 mDockDivider.setAlpha(1f);
2810 mDividerAnimator = null;
2811 }
2812 if (animated) {
2813 mDividerAnimator = new AnimatorSet();
2814 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2815 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2816 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2817 mDividerAnimator.start();
2818 }
2819 }
2820 }
2821
Michael Jurkab3e22d92011-10-31 15:58:33 -07002822 void lockAllApps() {
2823 // TODO
2824 }
2825
2826 void unlockAllApps() {
2827 // TODO
2828 }
2829
Adam Cohenfc53cd22011-07-20 15:45:11 -07002830 public boolean isAllAppsCustomizeOpen() {
2831 return mState == State.APPS_CUSTOMIZE;
2832 }
2833
Winson Chungf0ea4d32011-06-06 14:27:16 -07002834 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002835 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002836 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002837 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002838 if (!LauncherApplication.isScreenLarge()) {
2839 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002840 if (mHotseat.getAlpha() != 1f) {
2841 int duration = mSearchDropTargetBar.getTransitionInDuration();
2842 mHotseat.animate().alpha(1f).setDuration(duration);
2843 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002844 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002845 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002846 }
2847 }
2848 }
2849
2850 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002851 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002852 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002853 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002854 if (!LauncherApplication.isScreenLarge()) {
2855 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002856 if (mHotseat.getAlpha() != 0f) {
2857 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2858 mHotseat.animate().alpha(0f).setDuration(duration);
2859 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002860 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002861 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002862 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002863 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002864 }
2865
Patrick Dubroy5f445422011-02-18 14:35:21 -08002866 /**
2867 * Add an item from all apps or customize onto the given workspace screen.
2868 * If layout is null, add to the current screen.
2869 */
2870 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002871 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002872 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002873 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002874 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002875
Winson Chungdff8ebb2011-09-08 17:25:31 -07002876 /** Maps the current orientation to an index for referencing orientation correct global icons */
2877 private int getCurrentOrientationIndexForGlobalIcons() {
2878 // default - 0, landscape - 1
2879 switch (getResources().getConfiguration().orientation) {
2880 case Configuration.ORIENTATION_LANDSCAPE:
2881 return 1;
2882 default:
2883 return 0;
2884 }
2885 }
2886
Michael Jurkaae65ee32012-04-30 11:45:54 -07002887 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002888 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002889 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002890 // Look for the toolbar icon specified in the activity meta-data
2891 Bundle metaData = packageManager.getActivityInfo(
2892 activityName, PackageManager.GET_META_DATA).metaData;
2893 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002894 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002895 if (iconResId != 0) {
2896 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002897 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002898 }
2899 }
2900 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002901 // This can happen if the activity defines an invalid drawable
2902 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2903 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002904 } catch (Resources.NotFoundException nfe) {
2905 // This can happen if the activity defines an invalid drawable
2906 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2907 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002908 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002909 return null;
2910 }
2911
2912 // if successful in getting icon, return it; otherwise, set button to use default drawable
2913 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002914 int buttonId, ComponentName activityName, int fallbackDrawableId,
2915 String toolbarResourceName) {
2916 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002917 Resources r = getResources();
2918 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2919 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002920
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002921 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002922 // If we were unable to find the icon via the meta-data, use a generic one
2923 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002924 toolbarIcon = r.getDrawable(fallbackDrawableId);
2925 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002926 if (button != null) {
2927 button.setCompoundDrawables(toolbarIcon, null, null, null);
2928 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002929 return null;
2930 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002931 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002932 if (button != null) {
2933 button.setCompoundDrawables(toolbarIcon, null, null, null);
2934 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002935 return toolbarIcon.getConstantState();
2936 }
2937 }
2938
2939 // if successful in getting icon, return it; otherwise, set button to use default drawable
2940 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002941 int buttonId, ComponentName activityName, int fallbackDrawableId,
2942 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002943 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002944 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002945
Michael Jurka19e0fc52011-07-22 18:00:21 -07002946 if (button != null) {
2947 // If we were unable to find the icon via the meta-data, use a
2948 // generic one
2949 if (toolbarIcon == null) {
2950 button.setImageResource(fallbackDrawableId);
2951 } else {
2952 button.setImageDrawable(toolbarIcon);
2953 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002954 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002955
2956 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2957
Michael Jurka0423dcf2010-10-05 14:56:18 -07002958 }
2959
Winson Chung1b884092012-06-08 17:13:02 -07002960 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002961 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07002962 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002963 }
2964
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002965 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002966 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002967 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002968 }
2969
Winson Chungbb185bd2011-11-21 12:31:42 -08002970 private void invalidatePressedFocusedStates(View container, View button) {
2971 if (container instanceof HolographicLinearLayout) {
2972 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2973 layout.invalidatePressedFocusedStates();
2974 } else if (button instanceof HolographicImageView) {
2975 HolographicImageView view = (HolographicImageView) button;
2976 view.invalidatePressedFocusedStates();
2977 }
2978 }
2979
Winson Chungc51db6a2011-10-05 11:44:49 -07002980 private boolean updateGlobalSearchIcon() {
2981 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002982 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2983 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002984 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002985 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002986 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002987
Winson Chung1cad91e2011-05-25 17:41:01 -07002988 final SearchManager searchManager =
2989 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2990 ComponentName activityName = searchManager.getGlobalSearchActivity();
2991 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002992 int coi = getCurrentOrientationIndexForGlobalIcons();
2993 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002994 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2995 TOOLBAR_SEARCH_ICON_METADATA_NAME);
2996 if (sGlobalSearchIcon[coi] == null) {
2997 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2998 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2999 TOOLBAR_ICON_METADATA_NAME);
3000 }
3001
Winson Chung649723c2011-07-06 20:41:23 -07003002 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003003 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3004 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003005 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003006 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003007 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003008 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07003009 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003010 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3011 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3012 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003013 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003014 if (voiceButtonProxy != null) {
3015 voiceButtonProxy.setVisibility(View.GONE);
3016 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003017 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003018 }
3019 }
3020
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003021 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003022 final View searchButtonContainer = findViewById(R.id.search_button_container);
3023 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003024 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003025 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003026 }
3027
Winson Chungc51db6a2011-10-05 11:44:49 -07003028 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07003029 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07003030 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003031 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003032 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003033
Winson Chungc51db6a2011-10-05 11:44:49 -07003034 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003035 final SearchManager searchManager =
3036 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3037 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3038
3039 ComponentName activityName = null;
3040 if (globalSearchActivity != null) {
3041 // Check if the global search activity handles voice search
3042 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3043 intent.setPackage(globalSearchActivity.getPackageName());
3044 activityName = intent.resolveActivity(getPackageManager());
3045 }
3046
3047 if (activityName == null) {
3048 // Fallback: check if an activity other than the global search activity
3049 // resolves this
3050 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3051 activityName = intent.resolveActivity(getPackageManager());
3052 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003053 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003054 int coi = getCurrentOrientationIndexForGlobalIcons();
3055 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003056 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3057 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3058 if (sVoiceSearchIcon[coi] == null) {
3059 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3060 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3061 TOOLBAR_ICON_METADATA_NAME);
3062 }
Winson Chung649723c2011-07-06 20:41:23 -07003063 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003064 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003065 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003066 if (voiceButtonProxy != null) {
3067 voiceButtonProxy.setVisibility(View.VISIBLE);
3068 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003069 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003070 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003071 } else {
Winson Chung649723c2011-07-06 20:41:23 -07003072 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003073 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003074 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003075 if (voiceButtonProxy != null) {
3076 voiceButtonProxy.setVisibility(View.GONE);
3077 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003078 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003079 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003080 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003081
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003082 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003083 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3084 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003085 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003086 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003087 }
3088
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003089 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003090 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003091 */
3092 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003093 final View marketButton = findViewById(R.id.market_button);
3094 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3095 // Find the app market activity by resolving an intent.
3096 // (If multiple app markets are installed, it will return the ResolverActivity.)
3097 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003098 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003099 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003100 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003101 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003102 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3103 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003104 marketButton.setVisibility(View.VISIBLE);
3105 } else {
3106 // We should hide and disable the view so that we don't try and restore the visibility
3107 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3108 marketButton.setVisibility(View.GONE);
3109 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003110 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003111 }
3112
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003113 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003114 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3115 Resources r = getResources();
3116 Drawable marketIconDrawable = d.newDrawable(r);
3117 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3118 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3119 marketIconDrawable.setBounds(0, 0, w, h);
3120
3121 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003122 }
3123
Michael Jurkad7c28052012-04-27 15:43:36 -07003124 @Override
3125 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3126 boolean result = super.dispatchPopulateAccessibilityEvent(event);
3127 final List<CharSequence> text = event.getText();
3128 text.clear();
3129 text.add(getString(R.string.home));
3130 return result;
3131 }
3132
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003133 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003134 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003135 */
3136 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3137 @Override
3138 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003139 closeSystemDialogs();
3140 }
3141 }
3142
3143 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003144 * Receives notifications whenever the appwidgets are reset.
3145 */
3146 private class AppWidgetResetObserver extends ContentObserver {
3147 public AppWidgetResetObserver() {
3148 super(new Handler());
3149 }
3150
3151 @Override
3152 public void onChange(boolean selfChange) {
3153 onAppWidgetReset();
3154 }
3155 }
3156
3157 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003158 * If the activity is currently paused, signal that we need to re-run the loader
3159 * in onResume.
3160 *
3161 * This needs to be called from incoming places where resources might have been loaded
3162 * while we are paused. That is becaues the Configuration might be wrong
3163 * when we're not running, and if it comes back to what it was when we
3164 * were paused, we are not restarted.
3165 *
3166 * Implementation of the method from LauncherModel.Callbacks.
3167 *
3168 * @return true if we are currently paused. The caller might be able to
3169 * skip some work in that case since we will come back again.
3170 */
3171 public boolean setLoadOnResume() {
3172 if (mPaused) {
3173 Log.i(TAG, "setLoadOnResume");
3174 mOnResumeNeedsLoad = true;
3175 return true;
3176 } else {
3177 return false;
3178 }
3179 }
3180
3181 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003182 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003183 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003184 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003185 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003186 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003187 } else {
3188 return SCREEN_COUNT / 2;
3189 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003190 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003191
Joe Onorato9c1289c2009-08-17 11:03:03 -04003192 /**
3193 * Refreshes the shortcuts shown on the workspace.
3194 *
3195 * Implementation of the method from LauncherModel.Callbacks.
3196 */
3197 public void startBinding() {
3198 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003199
Winson Chungf0c6ae02012-03-21 16:10:31 -07003200 mNewShortcutAnimatePage = -1;
3201 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003202 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003203 int count = workspace.getChildCount();
3204 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003205 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003206 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3207 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003208 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003209 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003210 if (mHotseat != null) {
3211 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003212 }
3213 }
3214
3215 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003216 * Bind the items start-end from the list.
3217 *
3218 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003219 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003220 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003221 setLoadOnResume();
3222
Winson Chungf0c6ae02012-03-21 16:10:31 -07003223 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3224 Set<String> newApps = new HashSet<String>();
3225 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3226
3227 Workspace workspace = mWorkspace;
3228 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003229 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003230
3231 // Short circuit if we are loading dock items for a configuration which has no dock
3232 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3233 mHotseat == null) {
3234 continue;
3235 }
3236
Joe Onorato9c1289c2009-08-17 11:03:03 -04003237 switch (item.itemType) {
3238 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3239 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003240 ShortcutInfo info = (ShortcutInfo) item;
3241 String uri = info.intent.toUri(0).toString();
3242 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003243 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3244 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003245 boolean animateIconUp = false;
3246 synchronized (newApps) {
3247 if (newApps.contains(uri)) {
3248 animateIconUp = newApps.remove(uri);
3249 }
3250 }
3251 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003252 // Prepare the view to be animated up
3253 shortcut.setAlpha(0f);
3254 shortcut.setScaleX(0f);
3255 shortcut.setScaleY(0f);
3256 mNewShortcutAnimatePage = item.screen;
3257 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3258 mNewShortcutAnimateViews.add(shortcut);
3259 }
3260 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003261 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003262 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003263 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003264 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003265 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003266 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3267 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003268 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003269 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003270 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003271
Joe Onorato9c1289c2009-08-17 11:03:03 -04003272 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003273 }
3274
Joe Onorato9c1289c2009-08-17 11:03:03 -04003275 /**
3276 * Implementation of the method from LauncherModel.Callbacks.
3277 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003278 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003279 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003280 sFolders.clear();
3281 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003282 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003283
3284 /**
3285 * Add the views for a widget to the workspace.
3286 *
3287 * Implementation of the method from LauncherModel.Callbacks.
3288 */
3289 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003290 setLoadOnResume();
3291
Daniel Sandler843e8602010-06-07 14:59:01 -04003292 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3293 if (DEBUG_WIDGETS) {
3294 Log.d(TAG, "bindAppWidget: " + item);
3295 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003296 final Workspace workspace = mWorkspace;
3297
3298 final int appWidgetId = item.appWidgetId;
3299 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003300 if (DEBUG_WIDGETS) {
3301 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3302 }
3303
Joe Onorato9c1289c2009-08-17 11:03:03 -04003304 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3305
Joe Onorato9c1289c2009-08-17 11:03:03 -04003306 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003307 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003308
Winson Chung3d503fb2011-07-13 17:25:49 -07003309 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003310 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003311 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3312
Joe Onorato9c1289c2009-08-17 11:03:03 -04003313 workspace.requestLayout();
3314
Daniel Sandler843e8602010-06-07 14:59:01 -04003315 if (DEBUG_WIDGETS) {
3316 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3317 + (SystemClock.uptimeMillis()-start) + "ms");
3318 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003319 }
3320
Adam Cohen1462de32012-07-24 22:34:36 -07003321 public void onPageBoundSynchronously(int page) {
3322 mSynchronouslyBoundPages.add(page);
3323 }
3324
Joe Onorato9c1289c2009-08-17 11:03:03 -04003325 /**
3326 * Callback saying that there aren't any more items to bind.
3327 *
3328 * Implementation of the method from LauncherModel.Callbacks.
3329 */
3330 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003331 setLoadOnResume();
3332
Joe Onorato9c1289c2009-08-17 11:03:03 -04003333 if (mSavedState != null) {
3334 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003335 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003337 mSavedState = null;
3338 }
3339
Adam Cohen1462de32012-07-24 22:34:36 -07003340 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003341
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003342 // If we received the result of any pending adds while the loader was running (e.g. the
3343 // widget configuration forced an orientation change), process them now.
3344 for (int i = 0; i < sPendingAddList.size(); i++) {
3345 completeAdd(sPendingAddList.get(i));
3346 }
3347 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003348
Winson Chungc51db6a2011-10-05 11:44:49 -07003349 // Update the market app icon as necessary (the other icons will be managed in response to
3350 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003351 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003352
Winson Chungf0c6ae02012-03-21 16:10:31 -07003353 // Animate up any icons as necessary
3354 if (mVisible || mWorkspaceLoading) {
3355 Runnable newAppsRunnable = new Runnable() {
3356 @Override
3357 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003358 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003359 }
3360 };
Winson Chunga2413752012-04-03 14:22:34 -07003361
3362 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3363 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3364 if (canRunNewAppsAnimation()) {
3365 // If the user has not interacted recently, then either snap to the new page to show
3366 // the new-apps animation or just run them if they are to appear on the current page
3367 if (willSnapPage) {
3368 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3369 } else {
3370 runNewAppsAnimation(false);
3371 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003372 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003373 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003374 // are on another page (or just normally if they are added to the current page)
3375 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003376 }
3377 }
3378
3379 mWorkspaceLoading = false;
3380 }
3381
Winson Chunga2413752012-04-03 14:22:34 -07003382 private boolean canRunNewAppsAnimation() {
3383 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3384 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3385 }
3386
Winson Chungf0c6ae02012-03-21 16:10:31 -07003387 /**
3388 * Runs a new animation that scales up icons that were added while Launcher was in the
3389 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003390 *
3391 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003392 */
Winson Chunga2413752012-04-03 14:22:34 -07003393 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003394 AnimatorSet anim = new AnimatorSet();
3395 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003396
3397 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003398 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3399 @Override
3400 public int compare(View a, View b) {
3401 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3402 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3403 int cellCountX = LauncherModel.getCellCountX();
3404 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3405 }
3406 });
Winson Chunga2413752012-04-03 14:22:34 -07003407
3408 // Animate each of the views in place (or show them immediately if requested)
3409 if (immediate) {
3410 for (View v : mNewShortcutAnimateViews) {
3411 v.setAlpha(1f);
3412 v.setScaleX(1f);
3413 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003414 }
Winson Chunga2413752012-04-03 14:22:34 -07003415 } else {
3416 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3417 View v = mNewShortcutAnimateViews.get(i);
3418 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3419 PropertyValuesHolder.ofFloat("alpha", 1f),
3420 PropertyValuesHolder.ofFloat("scaleX", 1f),
3421 PropertyValuesHolder.ofFloat("scaleY", 1f));
3422 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3423 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3424 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3425 bounceAnims.add(bounceAnim);
3426 }
3427 anim.playTogether(bounceAnims);
3428 anim.addListener(new AnimatorListenerAdapter() {
3429 @Override
3430 public void onAnimationEnd(Animator animation) {
3431 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3432 }
3433 });
3434 anim.start();
3435 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003436
3437 // Clean up
3438 mNewShortcutAnimatePage = -1;
3439 mNewShortcutAnimateViews.clear();
3440 new Thread("clearNewAppsThread") {
3441 public void run() {
3442 mSharedPrefs.edit()
3443 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3444 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3445 .commit();
3446 }
3447 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003448 }
3449
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003450 @Override
3451 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003452 boolean searchVisible = updateGlobalSearchIcon();
3453 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3454 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003455 }
3456
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003457 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003458 * Add the icons for all apps.
3459 *
3460 * Implementation of the method from LauncherModel.Callbacks.
3461 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003462 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3463 // Remove the progress bar entirely; we could also make it GONE
3464 // but better to remove it since we know it's not going to be used
3465 View progressBar = mAppsCustomizeTabHost.
3466 findViewById(R.id.apps_customize_progress_bar);
3467 if (progressBar != null) {
3468 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003469 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003470 // We just post the call to setApps so the user sees the progress bar
3471 // disappear-- otherwise, it just looks like the progress bar froze
3472 // which doesn't look great
3473 mAppsCustomizeTabHost.post(new Runnable() {
3474 public void run() {
3475 if (mAppsCustomizeContent != null) {
3476 mAppsCustomizeContent.setApps(apps);
3477 }
3478 }
3479 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003480 }
3481
3482 /**
3483 * A package was installed.
3484 *
3485 * Implementation of the method from LauncherModel.Callbacks.
3486 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003487 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003488 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003489
Winson Chung785d2eb2011-04-14 16:08:02 -07003490 if (mAppsCustomizeContent != null) {
3491 mAppsCustomizeContent.addApps(apps);
3492 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003493 }
3494
3495 /**
3496 * A package was updated.
3497 *
3498 * Implementation of the method from LauncherModel.Callbacks.
3499 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003500 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003501 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003502 if (mWorkspace != null) {
3503 mWorkspace.updateShortcuts(apps);
3504 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003505
Winson Chung785d2eb2011-04-14 16:08:02 -07003506 if (mAppsCustomizeContent != null) {
3507 mAppsCustomizeContent.updateApps(apps);
3508 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003509 }
3510
3511 /**
3512 * A package was uninstalled.
3513 *
3514 * Implementation of the method from LauncherModel.Callbacks.
3515 */
Joe Onorato36115782010-06-17 13:28:48 -04003516 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003517 if (permanent) {
3518 mWorkspace.removeItems(apps);
3519 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003520
Winson Chung785d2eb2011-04-14 16:08:02 -07003521 if (mAppsCustomizeContent != null) {
3522 mAppsCustomizeContent.removeApps(apps);
3523 }
Winson Chunga1820962011-10-03 16:31:06 -07003524
3525 // Notify the drag controller
3526 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003527 }
Joe Onoratobe386092009-11-17 17:32:16 -08003528
3529 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003530 * A number of packages were updated.
3531 */
3532 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003533 if (mAppsCustomizeContent != null) {
3534 mAppsCustomizeContent.onPackagesUpdated();
3535 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003536 }
3537
Winson Chung400438b2011-01-16 17:53:48 -08003538 private int mapConfigurationOriActivityInfoOri(int configOri) {
3539 final Display d = getWindowManager().getDefaultDisplay();
3540 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3541 switch (d.getRotation()) {
3542 case Surface.ROTATION_0:
3543 case Surface.ROTATION_180:
3544 // We are currently in the same basic orientation as the natural orientation
3545 naturalOri = configOri;
3546 break;
3547 case Surface.ROTATION_90:
3548 case Surface.ROTATION_270:
3549 // We are currently in the other basic orientation to the natural orientation
3550 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3551 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3552 break;
3553 }
3554
3555 int[] oriMap = {
3556 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3557 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3558 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3559 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3560 };
3561 // Since the map starts at portrait, we need to offset if this device's natural orientation
3562 // is landscape.
3563 int indexOffset = 0;
3564 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3565 indexOffset = 1;
3566 }
3567 return oriMap[(d.getRotation() + indexOffset) % 4];
3568 }
Adam Cohen446e9402011-09-15 18:21:21 -07003569
Winson Chung4b919f82012-05-01 10:44:08 -07003570 public boolean isRotationEnabled() {
3571 boolean forceEnableRotation = "true".equalsIgnoreCase(SystemProperties.get(
3572 FORCE_ENABLE_ROTATION_PROPERTY, "false"));
3573 boolean enableRotation = forceEnableRotation ||
3574 getResources().getBoolean(R.bool.allow_rotation);
3575 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003576 }
Winson Chung4b919f82012-05-01 10:44:08 -07003577 public void lockScreenOrientation() {
3578 if (isRotationEnabled()) {
3579 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3580 .getConfiguration().orientation));
3581 }
3582 }
3583 public void unlockScreenOrientation(boolean immediate) {
3584 if (isRotationEnabled()) {
3585 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003586 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003587 } else {
3588 mHandler.postDelayed(new Runnable() {
3589 public void run() {
3590 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3591 }
3592 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003593 }
Winson Chung4b919f82012-05-01 10:44:08 -07003594 }
Winson Chung400438b2011-01-16 17:53:48 -08003595 }
3596
Winson Chung82f55532011-08-09 14:14:23 -07003597 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003598 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003599 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003600 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003601
Winson Chung7d7541e2011-09-16 20:14:36 -07003602 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003603 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003604 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3605 Cling cling = (Cling) findViewById(clingId);
3606 if (cling != null) {
3607 cling.init(this, positionData);
3608 cling.setVisibility(View.VISIBLE);
3609 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Svetoslav Ganov55d225d2012-05-22 15:19:14 -07003610 cling.requestAccessibilityFocus();
Winson Chung7d7541e2011-09-16 20:14:36 -07003611 if (animate) {
3612 cling.buildLayer();
3613 cling.setAlpha(0f);
3614 cling.animate()
3615 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003616 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003617 .setDuration(SHOW_CLING_DURATION)
3618 .setStartDelay(delay)
3619 .start();
3620 } else {
3621 cling.setAlpha(1f);
3622 }
3623 }
3624 return cling;
3625 }
3626 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003627 if (cling != null) {
3628 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003629 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003630 anim.addListener(new AnimatorListenerAdapter() {
3631 public void onAnimationEnd(Animator animation) {
3632 cling.setVisibility(View.GONE);
3633 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003634 // We should update the shared preferences on a background thread
3635 new Thread("dismissClingThread") {
3636 public void run() {
3637 SharedPreferences.Editor editor = mSharedPrefs.edit();
3638 editor.putBoolean(flag, true);
3639 editor.commit();
3640 }
3641 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003642 };
3643 });
3644 anim.start();
3645 }
3646 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003647 private void removeCling(int id) {
3648 final View cling = findViewById(id);
3649 if (cling != null) {
3650 final ViewGroup parent = (ViewGroup) cling.getParent();
3651 parent.post(new Runnable() {
3652 @Override
3653 public void run() {
3654 parent.removeView(cling);
3655 }
3656 });
3657 }
3658 }
Michael Jurka974c3862012-05-22 22:00:31 -07003659
3660 private boolean skipCustomClingIfNoAccounts() {
3661 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3662 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3663 if (customCling) {
3664 AccountManager am = AccountManager.get(this);
3665 Account[] accounts = am.getAccountsByType("com.google");
3666 return accounts.length == 0;
3667 }
3668 return false;
3669 }
3670
Winson Chung7d7541e2011-09-16 20:14:36 -07003671 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003672 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003673 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003674 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3675 !skipCustomClingIfNoAccounts() ) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003676 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003677 } else {
3678 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003679 }
3680 }
3681 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003682 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003683 if (isClingsEnabled() &&
3684 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003685 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003686 } else {
3687 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003688 }
3689 }
3690 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003691 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003692 if (isClingsEnabled() &&
3693 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3694 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003695 } else {
3696 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003697 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003698 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003699 }
3700 public boolean isFolderClingVisible() {
3701 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003702 if (cling != null) {
3703 return cling.getVisibility() == View.VISIBLE;
3704 }
3705 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003706 }
Winson Chung82f55532011-08-09 14:14:23 -07003707 public void dismissWorkspaceCling(View v) {
3708 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003709 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003710 }
3711 public void dismissAllAppsCling(View v) {
3712 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003713 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3714 }
3715 public void dismissFolderCling(View v) {
3716 Cling cling = (Cling) findViewById(R.id.folder_cling);
3717 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003718 }
3719
Winson Chung80baf5a2010-08-09 16:03:15 -07003720 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003721 * Prints out out state for debugging.
3722 */
3723 public void dumpState() {
3724 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003725 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003726 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3727 Log.d(TAG, "mRestoring=" + mRestoring);
3728 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3729 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003730 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003731 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003732
Winson Chung785d2eb2011-04-14 16:08:02 -07003733 if (mAppsCustomizeContent != null) {
3734 mAppsCustomizeContent.dumpState();
3735 }
Joe Onoratobe386092009-11-17 17:32:16 -08003736 Log.d(TAG, "END launcher2 dump state");
3737 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003738
3739 @Override
3740 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3741 super.dump(prefix, fd, writer, args);
3742 writer.println(" ");
3743 writer.println("Debug logs: ");
3744 for (int i = 0; i < sDumpLogs.size(); i++) {
3745 writer.println(" " + sDumpLogs.get(i));
3746 }
3747 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003748}
Michael Jurka2763be32011-02-24 11:19:57 -08003749
Michael Jurkaabded662011-03-04 12:06:57 -08003750interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003751 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003752 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003753 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003754 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003755 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003756}