blob: 38085e0296ea9ef1b9912a6c75af5a34b4619063 [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) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700374 mAppsCustomizeContent.onPackagesUpdated(true);
Winson Chung785d2eb2011-04-14 16:08:02 -0700375 }
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 Chungc93e5ae2012-07-23 20:48:26 -0700893 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700894 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700895 mAppsCustomizeContent.loadAssociatedPages(
896 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700897 }
898
Winson Chung5afbf7b2011-07-25 11:53:08 -0700899 int currentIndex = savedState.getInt("apps_customize_currentIndex");
900 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700901 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800902 }
903
904 /**
905 * Finds all the views we need and configure them properly.
906 */
907 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700908 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400909
Winson Chung4c98d922011-05-31 16:50:48 -0700910 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
911 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800912 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
913 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800914
Winson Chung4c98d922011-05-31 16:50:48 -0700915 // Setup the drag layer
916 mDragLayer.setup(this, dragController);
917
Winson Chung3d503fb2011-07-13 17:25:49 -0700918 // Setup the hotseat
919 mHotseat = (Hotseat) findViewById(R.id.hotseat);
920 if (mHotseat != null) {
921 mHotseat.setup(this);
922 }
923
Winson Chung4c98d922011-05-31 16:50:48 -0700924 // Setup the workspace
925 mWorkspace.setHapticFeedbackEnabled(false);
926 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700927 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700928 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700929
Winson Chungf0ea4d32011-06-06 14:27:16 -0700930 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700931 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700932
Winson Chungf0ea4d32011-06-06 14:27:16 -0700933 // Setup AppsCustomize
934 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
935 findViewById(R.id.apps_customize_pane);
936 mAppsCustomizeContent = (AppsCustomizePagedView)
937 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
Winson Chungc7450e32012-04-17 17:34:08 -0700938 mAppsCustomizeTabHost.setup(this);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700939 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400940
Michael Jurka5130e402011-10-13 04:55:35 -0700941 // Get the all apps button
942 mAllAppsButton = findViewById(R.id.all_apps_button);
943 if (mAllAppsButton != null) {
944 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
945 @Override
946 public boolean onTouch(View v, MotionEvent event) {
947 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
948 onTouchDownAllAppsButton(v);
949 }
950 return false;
951 }
952 });
953 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700954 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700955 dragController.setDragScoller(mWorkspace);
956 dragController.setScrollView(mDragLayer);
957 dragController.setMoveTarget(mWorkspace);
958 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700959 if (mSearchDropTargetBar != null) {
960 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800962 }
963
964 /**
965 * Creates a view representing a shortcut.
966 *
967 * @param info The data structure describing the shortcut.
968 *
969 * @return A View inflated from R.layout.application.
970 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800971 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800972 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700973 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 }
975
976 /**
977 * Creates a view representing a shortcut inflated from the specified resource.
978 *
979 * @param layoutResId The id of the XML layout used to create the shortcut.
980 * @param parent The group the shortcut belongs to.
981 * @param info The data structure describing the shortcut.
982 *
983 * @return A View inflated from layoutResId.
984 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800985 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800986 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
987 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800989 return favorite;
990 }
991
992 /**
993 * Add an application shortcut to the workspace.
994 *
995 * @param data The intent describing the application.
996 * @param cellInfo The position on screen where to create the shortcut.
997 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700998 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700999 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001000 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001001
Adam Cohenfbba09b2011-07-18 21:43:05 -07001002 // First we check if we already know the exact location where we want to add this item.
1003 if (cellX >= 0 && cellY >= 0) {
1004 cellXY[0] = cellX;
1005 cellXY[1] = cellY;
1006 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001007 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001008 return;
1009 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001011 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001012
Romain Guy73b979d2009-06-09 12:57:21 -07001013 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001014 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001016 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001017 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001018 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001019 } else {
1020 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001021 }
1022 }
Romain Guycbb89e42009-06-08 15:52:54 -07001023
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001024 /**
1025 * Add a shortcut to the workspace.
1026 *
1027 * @param data The intent describing the shortcut.
1028 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001030 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1031 int cellY) {
1032 int[] cellXY = mTmpAddItemCellCoordinates;
1033 int[] touchXY = mPendingAddInfo.dropPos;
1034 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001035
Michael Jurkad3ef3062010-11-23 16:23:58 -08001036 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001037
Adam Cohen558baaf2011-08-15 15:22:57 -07001038 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001039 if (info == null) {
1040 return;
1041 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001042 final View view = createShortcut(info);
1043
Adam Cohenfbba09b2011-07-18 21:43:05 -07001044 // First we check if we already know the exact location where we want to add this item.
1045 if (cellX >= 0 && cellY >= 0) {
1046 cellXY[0] = cellX;
1047 cellXY[1] = cellY;
1048 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001049
1050 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001051 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001052 true, null,null)) {
1053 return;
1054 }
1055 DragObject dragObject = new DragObject();
1056 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001057 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1058 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001059 return;
1060 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001061 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001062 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001063 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001064 foundCellSpan = (result != null);
1065 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001066 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001067 }
1068
1069 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001070 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001071 return;
1072 }
1073
Adam Cohen558baaf2011-08-15 15:22:57 -07001074 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001075
1076 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001077 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1078 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001079 }
1080 }
1081
Adam Cohen2f093b62012-04-30 18:59:53 -07001082 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1083 int minHeight) {
1084 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001085 // We want to account for the extra amount of padding that we are adding to the widget
1086 // to ensure that it gets the full amount of space that it has requested
1087 int requiredWidth = minWidth + padding.left + padding.right;
1088 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001089 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001090 }
1091
Adam Cohen2f093b62012-04-30 18:59:53 -07001092 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1093 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001094 }
1095
Adam Cohen2f093b62012-04-30 18:59:53 -07001096 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1097 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001098 }
1099
Adam Cohen2f093b62012-04-30 18:59:53 -07001100 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1101 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001102 }
1103
Adam Cohen2f093b62012-04-30 18:59:53 -07001104 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1105 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001106 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001107 }
1108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001110 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001112 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001113 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001115 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1116 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1117 if (appWidgetInfo == null) {
1118 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1119 }
Romain Guycbb89e42009-06-08 15:52:54 -07001120
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001121 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001122 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001123
Adam Cohen2f093b62012-04-30 18:59:53 -07001124 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1125 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001126
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001127 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001128 // We have saved the position to which the widget was dragged-- this really only matters
1129 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001130 int[] cellXY = mTmpAddItemCellCoordinates;
1131 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001132 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001133 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001134 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1135 cellXY[0] = mPendingAddInfo.cellX;
1136 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001137 spanXY[0] = mPendingAddInfo.spanX;
1138 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001139 foundCellSpan = true;
1140 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001141 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001142 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001143 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1144 spanXY[1], cellXY, finalSpan);
1145 spanXY[0] = finalSpan[0];
1146 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001147 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001148 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001149 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001150 }
1151
Michael Jurka0280c3b2010-09-17 15:00:07 -07001152 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001153 if (appWidgetId != -1) {
1154 // Deleting an app widget ID is a void call but writes to disk before returning
1155 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001156 new Thread("deleteAppWidgetId") {
1157 public void run() {
1158 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1159 }
1160 }.start();
1161 }
Winson Chung93eef082012-03-23 15:59:27 -07001162 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001163 return;
1164 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001165
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001166 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001167 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1168 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001169 launcherInfo.spanX = spanXY[0];
1170 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001171 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1172 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001173
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001174 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001175 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001176
1177 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001178 if (hostView == null) {
1179 // Perform actual inflation because we're live
1180 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1181 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1182 } else {
1183 // The AppWidgetHostView has already been inflated and instantiated
1184 launcherInfo.hostView = hostView;
1185 }
Romain Guycbb89e42009-06-08 15:52:54 -07001186
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001187 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001188 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung211bac32012-05-15 13:43:57 -07001189 launcherInfo.notifyWidgetSizeChanged(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001190 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001191 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001192
1193 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001194 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001195 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001196 }
Romain Guycbb89e42009-06-08 15:52:54 -07001197
Adam Cohended9f8d2010-11-03 13:25:16 -07001198 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1199 @Override
1200 public void onReceive(Context context, Intent intent) {
1201 final String action = intent.getAction();
1202 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1203 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001204 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001205 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001206
Winson Chungc100e8e2011-08-09 16:02:43 -07001207 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001208 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001209 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1210 mAppsCustomizeTabHost.reset();
1211 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001212 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001213 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1214 mUserPresent = true;
1215 updateRunning();
1216 }
1217 }
1218 };
1219
1220 @Override
1221 public void onAttachedToWindow() {
1222 super.onAttachedToWindow();
1223
1224 // Listen for broadcasts related to user-presence
1225 final IntentFilter filter = new IntentFilter();
1226 filter.addAction(Intent.ACTION_SCREEN_OFF);
1227 filter.addAction(Intent.ACTION_USER_PRESENT);
1228 registerReceiver(mReceiver, filter);
1229
Adam Cohend113e0c2010-11-11 10:48:05 -08001230 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001231 mVisible = true;
1232 }
1233
1234 @Override
1235 public void onDetachedFromWindow() {
1236 super.onDetachedFromWindow();
1237 mVisible = false;
1238
Adam Cohend113e0c2010-11-11 10:48:05 -08001239 if (mAttached) {
1240 unregisterReceiver(mReceiver);
1241 mAttached = false;
1242 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001243 updateRunning();
1244 }
1245
1246 public void onWindowVisibilityChanged(int visibility) {
1247 mVisible = visibility == View.VISIBLE;
1248 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001249 // The following code used to be in onResume, but it turns out onResume is called when
1250 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1251 // is a more appropriate event to handle
1252 if (mVisible) {
1253 mAppsCustomizeTabHost.onWindowVisible();
1254 if (!mWorkspaceLoading) {
1255 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001256 // We want to let Launcher draw itself at least once before we force it to build
1257 // layers on all the workspace pages, so that transitioning to Launcher from other
1258 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1259 // a true draw so we wait until the second preDraw call to be safe
1260 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001261 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001262 // We delay the layer building a bit in order to give
1263 // other message processing a time to run. In particular
1264 // this avoids a delay in hiding the IME if it was
1265 // currently shown, because doing that may involve
1266 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001267 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001268
Michael Jurka6ee21d22012-02-21 18:20:27 -08001269 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001270 return true;
1271 }
1272 });
1273 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001274 // When Launcher comes back to foreground, a different Activity might be responsible for
1275 // the app market intent, so refresh the icon
1276 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001277 clearTypedText();
1278 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001279 }
1280
1281 private void sendAdvanceMessage(long delay) {
1282 mHandler.removeMessages(ADVANCE_MSG);
1283 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1284 mHandler.sendMessageDelayed(msg, delay);
1285 mAutoAdvanceSentTime = System.currentTimeMillis();
1286 }
1287
1288 private void updateRunning() {
1289 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1290 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1291 mAutoAdvanceRunning = autoAdvanceRunning;
1292 if (autoAdvanceRunning) {
1293 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1294 sendAdvanceMessage(delay);
1295 } else {
1296 if (!mWidgetsToAdvance.isEmpty()) {
1297 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1298 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1299 }
1300 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001301 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001302 }
1303 }
1304 }
1305
1306 private final Handler mHandler = new Handler() {
1307 @Override
1308 public void handleMessage(Message msg) {
1309 if (msg.what == ADVANCE_MSG) {
1310 int i = 0;
1311 for (View key: mWidgetsToAdvance.keySet()) {
1312 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1313 final int delay = mAdvanceStagger * i;
1314 if (v instanceof Advanceable) {
1315 postDelayed(new Runnable() {
1316 public void run() {
1317 ((Advanceable) v).advance();
1318 }
1319 }, delay);
1320 }
1321 i++;
1322 }
1323 sendAdvanceMessage(mAdvanceInterval);
1324 }
1325 }
1326 };
1327
1328 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001329 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001330 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1331 if (v instanceof Advanceable) {
1332 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001333 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001334 updateRunning();
1335 }
1336 }
1337
1338 void removeWidgetToAutoAdvance(View hostView) {
1339 if (mWidgetsToAdvance.containsKey(hostView)) {
1340 mWidgetsToAdvance.remove(hostView);
1341 updateRunning();
1342 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001343 }
1344
Joe Onorato9c1289c2009-08-17 11:03:03 -04001345 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001346 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001347 launcherInfo.hostView = null;
1348 }
1349
Winson Chung93eef082012-03-23 15:59:27 -07001350 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1351 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1352 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001353 }
1354
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001355 public LauncherAppWidgetHost getAppWidgetHost() {
1356 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001357 }
Romain Guycbb89e42009-06-08 15:52:54 -07001358
Winson Chunga9abd0e2010-10-27 17:18:37 -07001359 public LauncherModel getModel() {
1360 return mModel;
1361 }
1362
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001363 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001364 getWindow().closeAllPanels();
1365
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001366 // Whatever we were doing is hereby canceled.
1367 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001368 }
1369
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001370 @Override
1371 protected void onNewIntent(Intent intent) {
1372 super.onNewIntent(intent);
1373
1374 // Close the menu
1375 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001376 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001377 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001378
Joe Onorato14f122b2009-11-19 14:06:36 -08001379 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1380 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001381
Adam Cohenac56cff2011-09-28 20:45:37 -07001382 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001383 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001384 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001385 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1386 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001387 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001388 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001389
1390 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001391 exitSpringLoadedDragMode();
Winson Chung36e6c5b2012-07-19 14:53:05 -07001392
1393 // If we are already on home, then just animate back to the workspace, otherwise, just
1394 // wait until onResume to set the state back to Workspace
1395 if (alreadyOnHome) {
1396 showWorkspace(true);
1397 } else {
1398 mOnResumeState = State.WORKSPACE;
1399 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001400
Joe Onorato3a8820b2009-11-10 15:06:42 -08001401 final View v = getWindow().peekDecorView();
1402 if (v != null && v.getWindowToken() != null) {
1403 InputMethodManager imm = (InputMethodManager)getSystemService(
1404 INPUT_METHOD_SERVICE);
1405 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001407
Michael Jurka35aa14d2011-07-07 17:01:08 -07001408 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001409 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001410 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001411 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 }
1413 }
1414
1415 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001416 public void onRestoreInstanceState(Bundle state) {
1417 super.onRestoreInstanceState(state);
1418 for (int page: mSynchronouslyBoundPages) {
1419 mWorkspace.restoreInstanceStateForChild(page);
1420 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
1422
1423 @Override
1424 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001425 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001426 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001427
Michael Jurka883f55b2010-10-21 15:47:14 -07001428 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001429 // We close any open folder since it will not be re-opened, and we need to make sure
1430 // this state is reflected.
1431 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432
Winson Chung3d503fb2011-07-13 17:25:49 -07001433 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1434 mWaitingForResult) {
1435 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1436 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1437 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1438 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001439 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1440 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1441 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 }
1443
1444 if (mFolderInfo != null && mWaitingForResult) {
1445 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1446 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1447 }
Michael Jurka946ad472010-07-09 18:05:18 -07001448
Winson Chung785d2eb2011-04-14 16:08:02 -07001449 // Save the current AppsCustomize tab
1450 if (mAppsCustomizeTabHost != null) {
1451 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1452 if (currentTabTag != null) {
1453 outState.putString("apps_customize_currentTab", currentTabTag);
1454 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001455 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1456 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 }
1459
1460 @Override
1461 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001463
Winson Chunge7a03942011-08-05 15:05:12 -07001464 // Remove all pending runnables
1465 mHandler.removeMessages(ADVANCE_MSG);
1466 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001467 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001468
Winson Chungcd2b0142011-06-08 16:02:26 -07001469 // Stop callbacks from LauncherModel
1470 LauncherApplication app = ((LauncherApplication) getApplication());
1471 mModel.stopLoader();
1472 app.setLauncher(null);
1473
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001474 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001475 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001477 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001478 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001479 mAppWidgetHost = null;
1480
1481 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001482
1483 TextKeyListener.getInstance().release();
1484
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485
Winson Chung3d503fb2011-07-13 17:25:49 -07001486 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001487
1488 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001489 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001490
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001491 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001492 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1493 mWorkspace.removeAllViews();
1494 mWorkspace = null;
1495 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001496
1497 ValueAnimator.clearAllAnimations();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001498 }
1499
Adam Cohena9cf38f2011-05-02 15:36:58 -07001500 public DragController getDragController() {
1501 return mDragController;
1502 }
1503
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 @Override
1505 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001506 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 super.startActivityForResult(intent, requestCode);
1508 }
1509
Winson Chung70d72102011-08-12 11:24:35 -07001510 /**
1511 * Indicates that we want global search for this activity by setting the globalSearch
1512 * argument for {@link #startSearch} to true.
1513 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001515 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001517
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001518 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001519
Karl Rosaen138a0412009-04-23 19:00:21 -07001520 if (initialQuery == null) {
1521 // Use any text typed in the launcher as the initial query
1522 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001523 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 if (appSearchData == null) {
1525 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001526 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001528 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001529
Karl Rosaen138a0412009-04-23 19:00:21 -07001530 final SearchManager searchManager =
1531 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
Karl Rosaen138a0412009-04-23 19:00:21 -07001532 searchManager.startSearch(initialQuery, selectInitialQuery, getComponentName(),
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001533 appSearchData, globalSearch, sourceBounds);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 }
1535
Winson Chung70d72102011-08-12 11:24:35 -07001536 @Override
1537 public boolean onCreateOptionsMenu(Menu menu) {
1538 if (isWorkspaceLocked()) {
1539 return false;
1540 }
1541
1542 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001543
1544 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1545 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1546 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001547 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1548 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1549 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001550 String helpUrl = getString(R.string.help_url);
1551 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001552 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1553 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1554
Winson Chung70d72102011-08-12 11:24:35 -07001555 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1556 .setIcon(android.R.drawable.ic_menu_gallery)
1557 .setAlphabeticShortcut('W');
1558 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1559 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001560 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001561 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001562 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1563 .setIcon(android.R.drawable.ic_menu_preferences)
1564 .setIntent(settings)
1565 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001566 if (!helpUrl.isEmpty()) {
1567 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1568 .setIcon(android.R.drawable.ic_menu_help)
1569 .setIntent(help)
1570 .setAlphabeticShortcut('H');
1571 }
Winson Chung70d72102011-08-12 11:24:35 -07001572 return true;
1573 }
1574
1575 @Override
1576 public boolean onPrepareOptionsMenu(Menu menu) {
1577 super.onPrepareOptionsMenu(menu);
1578
1579 if (mAppsCustomizeTabHost.isTransitioning()) {
1580 return false;
1581 }
1582 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1583 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1584
1585 return true;
1586 }
1587
1588 @Override
1589 public boolean onOptionsItemSelected(MenuItem item) {
1590 switch (item.getItemId()) {
1591 case MENU_WALLPAPER_SETTINGS:
1592 startWallpaper();
1593 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001594 }
1595
1596 return super.onOptionsItemSelected(item);
1597 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001599 @Override
1600 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001601 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001602 // Use a custom animation for launching search
1603 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001604 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001605 }
1606
Joe Onorato9c1289c2009-08-17 11:03:03 -04001607 public boolean isWorkspaceLocked() {
1608 return mWorkspaceLoading || mWaitingForResult;
1609 }
1610
Michael Jurka0280c3b2010-09-17 15:00:07 -07001611 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001612 mPendingAddInfo.container = ItemInfo.NO_ID;
1613 mPendingAddInfo.screen = -1;
1614 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1615 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001616 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001617 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001618 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001619
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001620 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1621 AppWidgetProviderInfo appWidgetInfo) {
1622 if (appWidgetInfo.configure != null) {
1623 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001624
Bjorn Bringert7984c942009-12-09 15:38:25 +00001625 // Launch over to configure widget, if needed
1626 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001627 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001628 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001629 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001631 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001632 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1633 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001634 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001635 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001636 }
1637 }
Romain Guycbb89e42009-06-08 15:52:54 -07001638
Adam Cohenfbba09b2011-07-18 21:43:05 -07001639 /**
1640 * Process a shortcut drop.
1641 *
1642 * @param componentName The name of the component
1643 * @param screen The screen where it should be added
1644 * @param cell The cell it should be added to, optional
1645 * @param position The location on the screen where it was dropped, optional
1646 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001647 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1648 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001649 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001650 mPendingAddInfo.container = container;
1651 mPendingAddInfo.screen = screen;
1652 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001653
1654 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001655 mPendingAddInfo.cellX = cell[0];
1656 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001657 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001658
1659 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1660 createShortcutIntent.setComponent(componentName);
1661 processShortcut(createShortcutIntent);
1662 }
1663
Adam Cohenfbba09b2011-07-18 21:43:05 -07001664 /**
1665 * Process a widget drop.
1666 *
1667 * @param info The PendingAppWidgetInfo of the widget being added.
1668 * @param screen The screen where it should be added
1669 * @param cell The cell it should be added to, optional
1670 * @param position The location on the screen where it was dropped, optional
1671 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001672 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001673 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001674 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001675 mPendingAddInfo.container = info.container = container;
1676 mPendingAddInfo.screen = info.screen = screen;
1677 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001678 mPendingAddInfo.minSpanX = info.minSpanX;
1679 mPendingAddInfo.minSpanY = info.minSpanY;
1680
Adam Cohenfbba09b2011-07-18 21:43:05 -07001681 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001682 mPendingAddInfo.cellX = cell[0];
1683 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001684 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001685 if (span != null) {
1686 mPendingAddInfo.spanX = span[0];
1687 mPendingAddInfo.spanY = span[1];
1688 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001689
Adam Cohened66b2b2012-01-23 17:28:51 -08001690 AppWidgetHostView hostView = info.boundWidget;
1691 int appWidgetId;
1692 if (hostView != null) {
1693 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001694 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001695 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001696 // In this case, we either need to start an activity to get permission to bind
1697 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001698 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001699 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001700 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001701 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001702 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001703 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1704 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1705 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1706 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1707 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001708 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001709 }
1710
Joe Onoratodeb98af2010-02-19 14:59:39 -08001711 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001712 // Handle case where user selected "Applications"
1713 String applicationName = getResources().getString(R.string.group_applications);
1714 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001715
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001716 if (applicationName != null && applicationName.equals(shortcutName)) {
1717 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1718 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001719
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001720 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1721 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001722 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001723 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001724 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001725 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001726 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 }
1728
Winson Chung24ab2f12010-09-16 14:10:47 -07001729 void processWallpaper(Intent intent) {
1730 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1731 }
1732
Winson Chung3d503fb2011-07-13 17:25:49 -07001733 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1734 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001735 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001736 folderInfo.title = getText(R.string.folder_name);
1737
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001738 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001739 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1740 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001741 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742
1743 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001744 FolderIcon newFolder =
1745 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1746 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1747 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001748 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 }
Romain Guycbb89e42009-06-08 15:52:54 -07001750
Joe Onorato9c1289c2009-08-17 11:03:03 -04001751 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001752 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001753 }
1754
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001755 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001756 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001758 Intent chooser = Intent.createChooser(pickWallpaper,
1759 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001760 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1761 // Removed in Eclair MR1
1762// WallpaperManager wm = (WallpaperManager)
1763// getSystemService(Context.WALLPAPER_SERVICE);
1764// WallpaperInfo wi = wm.getWallpaperInfo();
1765// if (wi != null && wi.getSettingsActivity() != null) {
1766// LabeledIntent li = new LabeledIntent(getPackageName(),
1767// R.string.configure_wallpaper, 0);
1768// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1769// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1770// }
Mike Clerona0618e42009-10-22 13:55:21 -07001771 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 }
1773
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001774 /**
1775 * Registers various content observers. The current implementation registers
1776 * only a favorites observer to keep track of the favorites applications.
1777 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001778 private void registerContentObservers() {
1779 ContentResolver resolver = getContentResolver();
1780 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1781 true, mWidgetObserver);
1782 }
1783
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001784 @Override
1785 public boolean dispatchKeyEvent(KeyEvent event) {
1786 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1787 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001788 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001789 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001790 case KeyEvent.KEYCODE_VOLUME_DOWN:
Jason Samseb5615d2009-11-30 11:50:10 -08001791 if (SystemProperties.getInt("debug.launcher2.dumpstate", 0) != 0) {
Joe Onoratobe386092009-11-17 17:32:16 -08001792 dumpState();
1793 return true;
1794 }
1795 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001796 }
1797 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1798 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001799 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 return true;
1801 }
1802 }
1803
1804 return super.dispatchKeyEvent(event);
1805 }
1806
Joe Onorato88ec0992009-11-19 13:16:06 -08001807 @Override
1808 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001809 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001810 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001811 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001812 Folder openFolder = mWorkspace.getOpenFolder();
1813 if (openFolder.isEditingName()) {
1814 openFolder.dismissEditingName();
1815 } else {
1816 closeFolder();
1817 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001818 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001819 mWorkspace.exitWidgetResizeMode();
1820
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001821 // Back button is a no-op here, but give at least some feedback for the button press
1822 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001823 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001824 }
1825
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001826 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001827 * Re-listen when widgets are reset.
1828 */
1829 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001830 if (mAppWidgetHost != null) {
1831 mAppWidgetHost.startListening();
1832 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001833 }
1834
1835 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001836 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1837 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001838 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001839 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001840 if (mModel != null) {
1841 mModel.unbindWorkspaceItems();
1842 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001843 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001844
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001845 /**
1846 * Launches the intent referred by the clicked shortcut.
1847 *
1848 * @param v The view representing the clicked shortcut.
1849 */
1850 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001851 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1852 // view has detached (it's possible for this to happen if the view is removed mid touch).
1853 if (v.getWindowToken() == null) {
1854 return;
1855 }
1856
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001857 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001858 return;
1859 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001860
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001862 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001863 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001864 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001865 int[] pos = new int[2];
1866 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001867 intent.setSourceBounds(new Rect(pos[0], pos[1],
1868 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001869
1870 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001871
1872 if (success && v instanceof BubbleTextView) {
1873 mWaitingForResume = (BubbleTextView) v;
1874 mWaitingForResume.setStayPressed(true);
1875 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001877 if (v instanceof FolderIcon) {
1878 FolderIcon fi = (FolderIcon) v;
1879 handleFolderClick(fi);
1880 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001881 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001882 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001883 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001884 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001885 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001886 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001887 }
1888 }
1889
Michael Jurka0e260592010-06-30 17:07:39 -07001890 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001891 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001892 // clicking anywhere on the workspace causes the customization drawer to slide down
1893 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001894 return false;
1895 }
1896
Michael Jurkaaf442092010-06-10 17:01:57 -07001897 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001898 * Event handler for the search button
1899 *
1900 * @param v The view that was clicked.
1901 */
1902 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001903 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1904
Amith Yamasania135ba82011-08-09 17:42:01 -07001905 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001906 }
1907
1908 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001909 * Event handler for the voice button
1910 *
1911 * @param v The view that was clicked.
1912 */
1913 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001914 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001915
Michael Jurkaae65ee32012-04-30 11:45:54 -07001916 try {
1917 final SearchManager searchManager =
1918 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1919 ComponentName activityName = searchManager.getGlobalSearchActivity();
1920 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001921 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001922 if (activityName != null) {
1923 intent.setPackage(activityName.getPackageName());
1924 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001925 startActivity(null, intent, "onClickVoiceButton");
Winson Chung01b0b632012-05-22 10:18:14 -07001926 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001927 } catch (ActivityNotFoundException e) {
1928 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001929 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001930 startActivitySafely(null, intent, "onClickVoiceButton");
1931 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001932 }
1933
1934 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001935 * Event handler for the "grid" button that appears on the home screen, which
1936 * enters all apps mode.
1937 *
1938 * @param v The view that was clicked.
1939 */
1940 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001941 showAllApps(true);
1942 }
1943
1944 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001945 // Provide the same haptic feedback that the system offers for virtual keys.
1946 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001947 }
1948
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001949 public void onClickAppMarketButton(View v) {
1950 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001951 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001952 } else {
1953 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001954 }
1955 }
1956
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001957 void startApplicationDetailsActivity(ComponentName componentName) {
1958 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001959 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1960 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001961 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001962 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001963 }
1964
Patrick Dubroy5539af72010-09-07 15:22:01 -07001965 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1966 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1967 // System applications cannot be installed. For now, show a toast explaining that.
1968 // We may give them the option of disabling apps this way.
1969 int messageId = R.string.uninstall_system_app_text;
1970 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1971 } else {
1972 String packageName = appInfo.componentName.getPackageName();
1973 String className = appInfo.componentName.getClassName();
1974 Intent intent = new Intent(
1975 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001976 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1977 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001978 startActivity(intent);
1979 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001980 }
1981
Michael Jurka86a720e2012-05-09 11:23:23 -07001982 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001984
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 try {
Winson Chung2672ff92012-05-04 16:22:30 -07001986 // Only launch using the new animation if the shortcut has not opted out (this is a
1987 // private contract between launcher and may be ignored in the future).
1988 boolean useLaunchAnimation = (v != null) &&
1989 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
1990 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07001991 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
1992 v.getMeasuredWidth(), v.getMeasuredHeight());
1993
1994 startActivity(intent, opts.toBundle());
1995 } else {
1996 startActivity(intent);
1997 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08001998 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 } catch (SecurityException e) {
2000 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002001 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002003 "or use the exported attribute for this activity. "
2004 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002005 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002006 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002008
Michael Jurka86a720e2012-05-09 11:23:23 -07002009 boolean startActivitySafely(View v, Intent intent, Object tag) {
2010 boolean success = false;
2011 try {
2012 success = startActivity(v, intent, tag);
2013 } catch (ActivityNotFoundException e) {
2014 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2015 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2016 }
2017 return success;
2018 }
2019
Romain Guy8e633c52010-03-04 12:51:36 -08002020 void startActivityForResultSafely(Intent intent, int requestCode) {
2021 try {
2022 startActivityForResult(intent, requestCode);
2023 } catch (ActivityNotFoundException e) {
2024 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2025 } catch (SecurityException e) {
2026 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2027 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2028 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2029 "or use the exported attribute for this activity.", e);
2030 }
2031 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032
Adam Cohena9cf38f2011-05-02 15:36:58 -07002033 private void handleFolderClick(FolderIcon folderIcon) {
2034 final FolderInfo info = folderIcon.mInfo;
Adam Cohen3c81a382011-08-31 22:25:51 -07002035 Folder openFolder = mWorkspace.getFolderForTag(info);
2036
2037 // If the folder info reports that the associated folder is open, then verify that
2038 // it is actually opened. There have been a few instances where this gets out of sync.
2039 if (info.opened && openFolder == null) {
2040 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2041 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2042 info.opened = false;
2043 }
2044
Adam Cohena9cf38f2011-05-02 15:36:58 -07002045 if (!info.opened) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 // Close any open folder
2047 closeFolder();
2048 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002049 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 } else {
2051 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 int folderScreen;
2053 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002054 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 // .. and close it
2056 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002057 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 // Close any folder open on the current screen
2059 closeFolder();
2060 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002061 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 }
2063 }
2064 }
2065 }
2066
Adam Cohen268c4752012-06-06 17:47:33 -07002067 /**
2068 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2069 * in the DragLayer in the exact absolute location of the original FolderIcon.
2070 */
2071 private void copyFolderIconToImage(FolderIcon fi) {
2072 final int width = fi.getMeasuredWidth();
2073 final int height = fi.getMeasuredHeight();
2074
2075 // Lazy load ImageView, Bitmap and Canvas
2076 if (mFolderIconImageView == null) {
2077 mFolderIconImageView = new ImageView(this);
2078 }
2079 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2080 mFolderIconBitmap.getHeight() != height) {
2081 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2082 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2083 }
2084
2085 DragLayer.LayoutParams lp;
2086 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2087 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2088 } else {
2089 lp = new DragLayer.LayoutParams(width, height);
2090 }
2091
2092 mDragLayer.getViewRectRelativeToSelf(fi, mRectForFolderAnimation);
2093 lp.customPosition = true;
2094 lp.x = mRectForFolderAnimation.left;
2095 lp.y = mRectForFolderAnimation.top;
Adam Cohen8ec23032012-06-08 14:46:22 -07002096 lp.width = width;
2097 lp.height = height;
Adam Cohen268c4752012-06-06 17:47:33 -07002098
2099 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2100 fi.draw(mFolderIconCanvas);
2101 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohen8ec23032012-06-08 14:46:22 -07002102 if (fi.mFolder != null) {
2103 mFolderIconImageView.setPivotX(fi.mFolder.getPivotXForIconAnimation());
2104 mFolderIconImageView.setPivotY(fi.mFolder.getPivotYForIconAnimation());
2105 }
Adam Cohen268c4752012-06-06 17:47:33 -07002106 // Just in case this image view is still in the drag layer from a previous animation,
2107 // we remove it and re-add it.
2108 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2109 mDragLayer.removeView(mFolderIconImageView);
2110 }
2111 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohen8ec23032012-06-08 14:46:22 -07002112 if (fi.mFolder != null) {
2113 fi.mFolder.bringToFront();
2114 }
Adam Cohen268c4752012-06-06 17:47:33 -07002115 }
2116
Adam Cohen2801caf2011-05-13 20:57:39 -07002117 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002118 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002119 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2120 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2121 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2122
Adam Cohenc51934b2011-07-26 21:07:43 -07002123 FolderInfo info = (FolderInfo) fi.getTag();
2124 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2125 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002126 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2127 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002128 }
2129
Adam Cohen268c4752012-06-06 17:47:33 -07002130 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2131 copyFolderIconToImage(fi);
2132 fi.setVisibility(View.INVISIBLE);
2133
2134 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(mFolderIconImageView, alpha,
2135 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002136 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2137 oa.start();
2138 }
2139
Adam Cohen268c4752012-06-06 17:47:33 -07002140 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002141 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002142 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2143 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2144 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2145
Adam Cohen268c4752012-06-06 17:47:33 -07002146 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002147
Adam Cohen268c4752012-06-06 17:47:33 -07002148 // We remove and re-draw the FolderIcon in-case it has changed
2149 mDragLayer.removeView(mFolderIconImageView);
2150 copyFolderIconToImage(fi);
Adam Cohen268c4752012-06-06 17:47:33 -07002151 ObjectAnimator oa = ObjectAnimator.ofPropertyValuesHolder(mFolderIconImageView, alpha,
2152 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002153 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002154 oa.addListener(new AnimatorListenerAdapter() {
2155 @Override
2156 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002157 if (cl != null) {
2158 cl.clearFolderLeaveBehind();
2159 // Remove the ImageView copy of the FolderIcon and make the original visible.
2160 mDragLayer.removeView(mFolderIconImageView);
2161 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002162 }
2163 }
2164 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002165 oa.start();
2166 }
2167
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002168 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002169 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 * is animated relative to the specified View. If the View is null, no animation
2171 * is played.
2172 *
2173 * @param folderInfo The FolderInfo describing the folder to open.
2174 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002175 public void openFolder(FolderIcon folderIcon) {
2176 Folder folder = folderIcon.mFolder;
2177 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178
Adam Cohena9cf38f2011-05-02 15:36:58 -07002179 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180
Adam Cohen4554ee12011-08-03 16:13:21 -07002181 // Just verify that the folder hasn't already been added to the DragLayer.
2182 // There was a one-off crash where the folder had a parent already.
2183 if (folder.getParent() == null) {
2184 mDragLayer.addView(folder);
2185 mDragController.addDropTarget((DropTarget) folder);
2186 } else {
2187 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2188 folder.getParent() + ").");
2189 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002190 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002191 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002192 }
2193
2194 public void closeFolder() {
2195 Folder folder = mWorkspace.getOpenFolder();
2196 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002197 if (folder.isEditingName()) {
2198 folder.dismissEditingName();
2199 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002200 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002201
2202 // Dismiss the folder cling
2203 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002204 }
2205 }
2206
2207 void closeFolder(Folder folder) {
2208 folder.getInfo().opened = false;
2209
2210 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2211 if (parent != null) {
2212 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2213 shrinkAndFadeInFolderIcon(fi);
2214 }
2215 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002216 }
2217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002218 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002219 if (!isDraggingEnabled()) return false;
2220 if (isWorkspaceLocked()) return false;
2221 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002222
2223 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002224 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225 }
2226
Michael Jurka0280c3b2010-09-17 15:00:07 -07002227 resetAddInfo();
2228 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002229 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002230 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 return true;
2232 }
2233
Winson Chung3d503fb2011-07-13 17:25:49 -07002234 // The hotseat touch handling does not go through Workspace, and we always allow long press
2235 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002236 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002237 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2238 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002239 if (itemUnderLongClick == null) {
2240 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002241 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2242 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002243 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002245 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002247 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002248 }
2249 }
2250 }
2251 return true;
2252 }
2253
Winson Chung3d503fb2011-07-13 17:25:49 -07002254 boolean isHotseatLayout(View layout) {
2255 return mHotseat != null && layout != null &&
2256 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2257 }
2258 Hotseat getHotseat() {
2259 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002260 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002261 SearchDropTargetBar getSearchBar() {
2262 return mSearchDropTargetBar;
2263 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002264
Winson Chung3d503fb2011-07-13 17:25:49 -07002265 /**
2266 * Returns the CellLayout of the specified container at the specified screen.
2267 */
2268 CellLayout getCellLayout(long container, int screen) {
2269 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2270 if (mHotseat != null) {
2271 return mHotseat.getLayout();
2272 } else {
2273 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002274 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002275 } else {
2276 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002277 }
2278 }
2279
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002280 Workspace getWorkspace() {
2281 return mWorkspace;
2282 }
2283
Daniel Sandler843e8602010-06-07 14:59:01 -04002284 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2285 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002286 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002287 }
2288
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002289 public boolean isAllAppsButtonRank(int rank) {
2290 return mHotseat.isAllAppsButtonRank(rank);
2291 }
2292
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002293 // AllAppsView.Watcher
2294 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002295 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002296 mWorkspace.setVisibility(View.GONE);
2297 }
2298 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002299
2300 /**
2301 * Helper method for the cameraZoomIn/cameraZoomOut animations
2302 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002303 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002304 * @param scaleFactor The scale factor used for the zoom
2305 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002306 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002307 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002308 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002309 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002310
Winson Chung18f41f82012-05-09 13:28:10 -07002311 void disableWallpaperIfInAllApps() {
2312 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002313 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002314 if (mAppsCustomizeTabHost != null &&
2315 !mAppsCustomizeTabHost.isTransitioning()) {
2316 updateWallpaperVisibility(false);
2317 }
2318 }
2319 }
2320
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002321 void updateWallpaperVisibility(boolean visible) {
2322 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2323 int curflags = getWindow().getAttributes().flags
2324 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2325 if (wpflags != curflags) {
2326 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2327 }
2328 }
2329
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002330 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2331 if (v instanceof LauncherTransitionable) {
2332 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2333 }
2334 }
2335
Michael Jurkabed61d22012-02-14 22:51:29 -08002336 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2337 if (v instanceof LauncherTransitionable) {
2338 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2339 }
Winson Chung70442722012-02-10 15:43:22 -08002340
2341 // Update the workspace transition step as well
2342 dispatchOnLauncherTransitionStep(v, 0f);
2343 }
2344
2345 private void dispatchOnLauncherTransitionStep(View v, float t) {
2346 if (v instanceof LauncherTransitionable) {
2347 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2348 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002349 }
2350
2351 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2352 if (v instanceof LauncherTransitionable) {
2353 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2354 }
Winson Chung70442722012-02-10 15:43:22 -08002355
2356 // Update the workspace transition step as well
2357 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002358 }
2359
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002360 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002361 * Things to test when changing the following seven functions.
2362 * - Home from workspace
2363 * - from center screen
2364 * - from other screens
2365 * - Home from all apps
2366 * - from center screen
2367 * - from other screens
2368 * - Back from all apps
2369 * - from center screen
2370 * - from other screens
2371 * - Launch app from workspace and quit
2372 * - with back
2373 * - with home
2374 * - Launch app from all apps and quit
2375 * - with back
2376 * - with home
2377 * - Go to a screen that's not the default, then all
2378 * apps, and launch and app, and go back
2379 * - with back
2380 * -with home
2381 * - On workspace, long press power and go back
2382 * - with back
2383 * - with home
2384 * - On all apps, long press power and go back
2385 * - with back
2386 * - with home
2387 * - On workspace, power off
2388 * - On all apps, power off
2389 * - Launch an app and turn off the screen while in that app
2390 * - Go back with home key
2391 * - Go back with back key TODO: make this not go to workspace
2392 * - From all apps
2393 * - From workspace
2394 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2395 * - From all apps
2396 * - From the center workspace
2397 * - From another workspace
2398 */
2399
2400 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002401 * Zoom the camera out from the workspace to reveal 'toView'.
2402 * Assumes that the view to show is anchored at either the very top or very bottom
2403 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002404 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002405 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002406 if (mStateAnimation != null) {
2407 mStateAnimation.cancel();
2408 mStateAnimation = null;
2409 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002410 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002411
Winson Chungf0ea4d32011-06-06 14:27:16 -07002412 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2413 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2414 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002415 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002416 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002417 final int startDelay =
2418 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002419
Michael Jurkab3e22d92011-10-31 15:58:33 -07002420 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002421
Michael Jurkad74c9842011-07-10 12:44:21 -07002422 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002423 Animator workspaceAnim =
2424 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002425
2426 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002427 toView.setScaleX(scale);
2428 toView.setScaleY(scale);
2429 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2430 scaleAnim.
2431 scaleX(1f).scaleY(1f).
2432 setDuration(duration).
2433 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002434
Winson Chungf0ea4d32011-06-06 14:27:16 -07002435 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002436 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002437 final ObjectAnimator alphaAnim = ObjectAnimator
2438 .ofFloat(toView, "alpha", 0f, 1f)
2439 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002440 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002441 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2442 @Override
2443 public void onAnimationUpdate(ValueAnimator animation) {
2444 float t = (Float) animation.getAnimatedValue();
2445 dispatchOnLauncherTransitionStep(fromView, t);
2446 dispatchOnLauncherTransitionStep(toView, t);
2447 }
2448 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002449
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002450 // toView should appear right at the end of the workspace shrink
2451 // animation
2452 mStateAnimation = new AnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002453 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002454 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002455
2456 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002457 boolean animationCancelled = false;
2458
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002459 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002460 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002461 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002462 // Prepare the position
2463 toView.setTranslationX(0.0f);
2464 toView.setTranslationY(0.0f);
2465 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002466 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002467 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002468 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002469 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002470 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2471 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002472
2473 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2474 // Hide the workspace scrollbar
2475 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002476 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002477 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002478 if (!animationCancelled) {
2479 updateWallpaperVisibility(false);
2480 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002481
2482 // Hide the search bar
2483 mSearchDropTargetBar.hideSearchBar(false);
Adam Cohenf4ddea32011-09-12 13:46:42 -07002484 }
2485
Adam Cohencff6af82011-09-13 14:51:53 -07002486 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002487 public void onAnimationCancel(Animator animation) {
2488 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002489 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002490 });
2491
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002492 if (workspaceAnim != null) {
2493 mStateAnimation.play(workspaceAnim);
2494 }
Michael Jurka1899a362011-11-03 13:50:45 -07002495
2496 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002497 final ViewTreeObserver observer;
2498
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002499 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2500 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002501
2502 // If any of the objects being animated haven't been measured/laid out
2503 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002504 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002505 (mWorkspace.getMeasuredWidth() == 0) ||
2506 (toView.getMeasuredWidth() == 0)) {
2507 observer = mWorkspace.getViewTreeObserver();
2508 delayAnim = true;
2509 } else {
2510 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002511 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002512
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002513 final AnimatorSet stateAnimation = mStateAnimation;
2514 final Runnable startAnimRunnable = new Runnable() {
2515 public void run() {
2516 // Check that mStateAnimation hasn't changed while
2517 // we waited for a layout/draw pass
2518 if (mStateAnimation != stateAnimation)
2519 return;
2520 setPivotsForZoom(toView, scale);
2521 dispatchOnLauncherTransitionStart(fromView, animated, false);
2522 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka382aa182012-06-11 15:42:07 -07002523 toView.post(new Runnable() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002524 public void run() {
2525 // Check that mStateAnimation hasn't changed while
2526 // we waited for a layout/draw pass
2527 if (mStateAnimation != stateAnimation)
2528 return;
2529 mStateAnimation.start();
2530 }
2531 });
2532 }
2533 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002534 if (delayAnim) {
2535 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2536 public void onGlobalLayout() {
Michael Jurka382aa182012-06-11 15:42:07 -07002537 toView.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002538 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002539 }
2540 };
2541 observer.addOnGlobalLayoutListener(delayedStart);
2542 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002543 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002544 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002545 } else {
2546 toView.setTranslationX(0.0f);
2547 toView.setTranslationY(0.0f);
2548 toView.setScaleX(1.0f);
2549 toView.setScaleY(1.0f);
2550 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002551 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002552
Michael Jurkabed61d22012-02-14 22:51:29 -08002553 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2554 // Hide the workspace scrollbar
2555 mWorkspace.hideScrollingIndicator(true);
2556 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002557
2558 // Hide the search bar
2559 mSearchDropTargetBar.hideSearchBar(false);
Michael Jurkaabded662011-03-04 12:06:57 -08002560 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002561 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002562 dispatchOnLauncherTransitionStart(fromView, animated, false);
2563 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002564 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002565 dispatchOnLauncherTransitionStart(toView, animated, false);
2566 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002567 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002568 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002569 }
2570
2571 /**
2572 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002573 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002574 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002575 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002576 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2577 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002578
Michael Jurkab3e22d92011-10-31 15:58:33 -07002579 if (mStateAnimation != null) {
2580 mStateAnimation.cancel();
2581 mStateAnimation = null;
2582 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002583 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002584
Winson Chungf0ea4d32011-06-06 14:27:16 -07002585 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002586 final int fadeOutDuration =
2587 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002588 final float scaleFactor = (float)
2589 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2590 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002591 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002592 Animator workspaceAnim = null;
2593
2594 if (toState == State.WORKSPACE) {
2595 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2596 workspaceAnim = mWorkspace.getChangeStateAnimation(
2597 Workspace.State.NORMAL, animated, stagger);
2598 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2599 workspaceAnim = mWorkspace.getChangeStateAnimation(
2600 Workspace.State.SPRING_LOADED, animated);
2601 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002602
Michael Jurkab3e22d92011-10-31 15:58:33 -07002603 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002604 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002605 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002606 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002607 final LauncherViewPropertyAnimator scaleAnim =
2608 new LauncherViewPropertyAnimator(fromView);
2609 scaleAnim.
2610 scaleX(scaleFactor).scaleY(scaleFactor).
2611 setDuration(duration).
2612 setInterpolator(new Workspace.ZoomInInterpolator());
2613
2614 final ObjectAnimator alphaAnim = ObjectAnimator
2615 .ofFloat(fromView, "alpha", 1f, 0f)
2616 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002617 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002618 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2619 @Override
2620 public void onAnimationUpdate(ValueAnimator animation) {
2621 float t = 1f - (Float) animation.getAnimatedValue();
2622 dispatchOnLauncherTransitionStep(fromView, t);
2623 dispatchOnLauncherTransitionStep(toView, t);
2624 }
2625 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002626
Michael Jurkabed61d22012-02-14 22:51:29 -08002627 mStateAnimation = new AnimatorSet();
2628
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002629 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2630 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002631
2632 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002633 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002634 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002635 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002636 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002637 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2638 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002639 if (mWorkspace != null) {
2640 mWorkspace.hideScrollingIndicator(false);
2641 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002642 if (onCompleteRunnable != null) {
2643 onCompleteRunnable.run();
2644 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002645 }
2646 });
2647
Winson Chungf0ea4d32011-06-06 14:27:16 -07002648 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002649 if (workspaceAnim != null) {
2650 mStateAnimation.play(workspaceAnim);
2651 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002652 dispatchOnLauncherTransitionStart(fromView, animated, true);
2653 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002654 final Animator stateAnimation = mStateAnimation;
2655 mWorkspace.post(new Runnable() {
2656 public void run() {
2657 if (stateAnimation != mStateAnimation)
2658 return;
2659 mStateAnimation.start();
2660 }
2661 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002662 } else {
2663 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002664 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002665 dispatchOnLauncherTransitionStart(fromView, animated, true);
2666 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002667 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002668 dispatchOnLauncherTransitionStart(toView, animated, true);
2669 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002670 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002671 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002672 }
2673
Michael Jurkae326f182011-11-21 14:05:46 -08002674 @Override
2675 public void onTrimMemory(int level) {
2676 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002677 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002678 mAppsCustomizeTabHost.onTrimMemory();
2679 }
2680 }
2681
Winson Chung18f41f82012-05-09 13:28:10 -07002682 @Override
2683 public void onWindowFocusChanged(boolean hasFocus) {
2684 if (!hasFocus) {
2685 // When another window occludes launcher (like the notification shade, or recents),
2686 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2687 updateWallpaperVisibility(true);
2688 } else {
2689 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002690 mWorkspace.postDelayed(new Runnable() {
2691 @Override
2692 public void run() {
2693 disableWallpaperIfInAllApps();
2694 }
2695 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002696 }
2697 }
2698
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002699 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002700 showWorkspace(animated, null);
2701 }
2702
2703 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002704 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002705 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002706 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002707 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002708
Winson Chungc7d2b602012-05-16 17:02:20 -07002709 // Show the search bar (only animate if we were showing the drop target bar in spring
2710 // loaded mode)
2711 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2712
Michael Jurkab737ee62011-11-15 15:57:22 -08002713 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002714 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002715
2716 // Set focus to the AppsCustomize button
2717 if (mAllAppsButton != null) {
2718 mAllAppsButton.requestFocus();
2719 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002720 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002721
Michael Jurkab737ee62011-11-15 15:57:22 -08002722 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002723
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002724 // Change the state *after* we've called all the transition code
2725 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002726
Winson Chung5fb63472011-02-02 17:03:37 -08002727 // Resume the auto-advance of widgets
2728 mUserPresent = true;
2729 updateRunning();
2730
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002731 // send an accessibility event to announce the context change
2732 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002733 }
2734
Michael Jurkab3e22d92011-10-31 15:58:33 -07002735 void showAllApps(boolean animated) {
2736 if (mState != State.WORKSPACE) return;
2737
2738 showAppsCustomizeHelper(animated, false);
2739 mAppsCustomizeTabHost.requestFocus();
2740
Michael Jurkab3e22d92011-10-31 15:58:33 -07002741 // Change the state *after* we've called all the transition code
2742 mState = State.APPS_CUSTOMIZE;
2743
2744 // Pause the auto-advance of widgets until we are out of AllApps
2745 mUserPresent = false;
2746 updateRunning();
2747 closeFolder();
2748
2749 // Send an accessibility event to announce the context change
2750 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2751 }
2752
Adam Cohen7777d962011-08-18 18:58:38 -07002753 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002754 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002755 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002756 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002757 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002758 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002759 }
Adam Cohen7777d962011-08-18 18:58:38 -07002760
Adam Cohened66b2b2012-01-23 17:28:51 -08002761 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2762 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002763 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2764
Winson Chunge7a03942011-08-05 15:05:12 -07002765 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002766 @Override
2767 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002768 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002769 // Before we show workspace, hide all apps again because
2770 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2771 // clean up our state transition functions
2772 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002773 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002774 } else {
2775 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002776 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002777 }
2778 }, (extendedDelay ?
2779 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2780 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2781 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002782
Michael Jurkad3ef3062010-11-23 16:23:58 -08002783 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002784 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002785 final boolean animated = true;
2786 final boolean springLoaded = true;
2787 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002788 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002789 }
2790 // Otherwise, we are not in spring loaded mode, so don't do anything.
2791 }
2792
Michael Jurkab737ee62011-11-15 15:57:22 -08002793 void hideDockDivider() {
2794 if (mQsbDivider != null && mDockDivider != null) {
2795 mQsbDivider.setVisibility(View.INVISIBLE);
2796 mDockDivider.setVisibility(View.INVISIBLE);
2797 }
2798 }
2799
2800 void showDockDivider(boolean animated) {
2801 if (mQsbDivider != null && mDockDivider != null) {
2802 mQsbDivider.setVisibility(View.VISIBLE);
2803 mDockDivider.setVisibility(View.VISIBLE);
2804 if (mDividerAnimator != null) {
2805 mDividerAnimator.cancel();
2806 mQsbDivider.setAlpha(1f);
2807 mDockDivider.setAlpha(1f);
2808 mDividerAnimator = null;
2809 }
2810 if (animated) {
2811 mDividerAnimator = new AnimatorSet();
2812 mDividerAnimator.playTogether(ObjectAnimator.ofFloat(mQsbDivider, "alpha", 1f),
2813 ObjectAnimator.ofFloat(mDockDivider, "alpha", 1f));
2814 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2815 mDividerAnimator.start();
2816 }
2817 }
2818 }
2819
Michael Jurkab3e22d92011-10-31 15:58:33 -07002820 void lockAllApps() {
2821 // TODO
2822 }
2823
2824 void unlockAllApps() {
2825 // TODO
2826 }
2827
Winson Chungf0ea4d32011-06-06 14:27:16 -07002828 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002829 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002830 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002831 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002832 if (!LauncherApplication.isScreenLarge()) {
2833 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002834 if (mHotseat.getAlpha() != 1f) {
2835 int duration = mSearchDropTargetBar.getTransitionInDuration();
2836 mHotseat.animate().alpha(1f).setDuration(duration);
2837 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002838 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002839 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002840 }
2841 }
2842 }
2843
2844 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002845 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002846 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002847 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002848 if (!LauncherApplication.isScreenLarge()) {
2849 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002850 if (mHotseat.getAlpha() != 0f) {
2851 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2852 mHotseat.animate().alpha(0f).setDuration(duration);
2853 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002854 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002855 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002856 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002857 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002858 }
2859
Patrick Dubroy5f445422011-02-18 14:35:21 -08002860 /**
2861 * Add an item from all apps or customize onto the given workspace screen.
2862 * If layout is null, add to the current screen.
2863 */
2864 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002865 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002866 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002867 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002868 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002869
Winson Chungdff8ebb2011-09-08 17:25:31 -07002870 /** Maps the current orientation to an index for referencing orientation correct global icons */
2871 private int getCurrentOrientationIndexForGlobalIcons() {
2872 // default - 0, landscape - 1
2873 switch (getResources().getConfiguration().orientation) {
2874 case Configuration.ORIENTATION_LANDSCAPE:
2875 return 1;
2876 default:
2877 return 0;
2878 }
2879 }
2880
Michael Jurkaae65ee32012-04-30 11:45:54 -07002881 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002882 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002883 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002884 // Look for the toolbar icon specified in the activity meta-data
2885 Bundle metaData = packageManager.getActivityInfo(
2886 activityName, PackageManager.GET_META_DATA).metaData;
2887 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002888 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002889 if (iconResId != 0) {
2890 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002891 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002892 }
2893 }
2894 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002895 // This can happen if the activity defines an invalid drawable
2896 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2897 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002898 } catch (Resources.NotFoundException nfe) {
2899 // This can happen if the activity defines an invalid drawable
2900 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2901 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002902 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002903 return null;
2904 }
2905
2906 // if successful in getting icon, return it; otherwise, set button to use default drawable
2907 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002908 int buttonId, ComponentName activityName, int fallbackDrawableId,
2909 String toolbarResourceName) {
2910 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002911 Resources r = getResources();
2912 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2913 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002914
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002915 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002916 // If we were unable to find the icon via the meta-data, use a generic one
2917 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002918 toolbarIcon = r.getDrawable(fallbackDrawableId);
2919 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002920 if (button != null) {
2921 button.setCompoundDrawables(toolbarIcon, null, null, null);
2922 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002923 return null;
2924 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002925 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 toolbarIcon.getConstantState();
2930 }
2931 }
2932
2933 // if successful in getting icon, return it; otherwise, set button to use default drawable
2934 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002935 int buttonId, ComponentName activityName, int fallbackDrawableId,
2936 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002937 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002938 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002939
Michael Jurka19e0fc52011-07-22 18:00:21 -07002940 if (button != null) {
2941 // If we were unable to find the icon via the meta-data, use a
2942 // generic one
2943 if (toolbarIcon == null) {
2944 button.setImageResource(fallbackDrawableId);
2945 } else {
2946 button.setImageDrawable(toolbarIcon);
2947 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002948 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002949
2950 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2951
Michael Jurka0423dcf2010-10-05 14:56:18 -07002952 }
2953
Winson Chung1b884092012-06-08 17:13:02 -07002954 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002955 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07002956 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002957 }
2958
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002959 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002960 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002961 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002962 }
2963
Winson Chungbb185bd2011-11-21 12:31:42 -08002964 private void invalidatePressedFocusedStates(View container, View button) {
2965 if (container instanceof HolographicLinearLayout) {
2966 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2967 layout.invalidatePressedFocusedStates();
2968 } else if (button instanceof HolographicImageView) {
2969 HolographicImageView view = (HolographicImageView) button;
2970 view.invalidatePressedFocusedStates();
2971 }
2972 }
2973
Winson Chungc51db6a2011-10-05 11:44:49 -07002974 private boolean updateGlobalSearchIcon() {
2975 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002976 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2977 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002978 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002979 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002980 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002981
Winson Chung1cad91e2011-05-25 17:41:01 -07002982 final SearchManager searchManager =
2983 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2984 ComponentName activityName = searchManager.getGlobalSearchActivity();
2985 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002986 int coi = getCurrentOrientationIndexForGlobalIcons();
2987 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002988 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2989 TOOLBAR_SEARCH_ICON_METADATA_NAME);
2990 if (sGlobalSearchIcon[coi] == null) {
2991 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
2992 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
2993 TOOLBAR_ICON_METADATA_NAME);
2994 }
2995
Winson Chung649723c2011-07-06 20:41:23 -07002996 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07002997 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
2998 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08002999 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003000 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003001 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003002 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07003003 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003004 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3005 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3006 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003007 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003008 if (voiceButtonProxy != null) {
3009 voiceButtonProxy.setVisibility(View.GONE);
3010 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003011 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003012 }
3013 }
3014
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003015 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003016 final View searchButtonContainer = findViewById(R.id.search_button_container);
3017 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003018 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003019 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003020 }
3021
Winson Chungc51db6a2011-10-05 11:44:49 -07003022 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07003023 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07003024 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003025 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003026 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003027
Winson Chungc51db6a2011-10-05 11:44:49 -07003028 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003029 final SearchManager searchManager =
3030 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3031 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3032
3033 ComponentName activityName = null;
3034 if (globalSearchActivity != null) {
3035 // Check if the global search activity handles voice search
3036 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3037 intent.setPackage(globalSearchActivity.getPackageName());
3038 activityName = intent.resolveActivity(getPackageManager());
3039 }
3040
3041 if (activityName == null) {
3042 // Fallback: check if an activity other than the global search activity
3043 // resolves this
3044 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3045 activityName = intent.resolveActivity(getPackageManager());
3046 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003047 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003048 int coi = getCurrentOrientationIndexForGlobalIcons();
3049 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003050 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3051 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3052 if (sVoiceSearchIcon[coi] == null) {
3053 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3054 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3055 TOOLBAR_ICON_METADATA_NAME);
3056 }
Winson Chung649723c2011-07-06 20:41:23 -07003057 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003058 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003059 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003060 if (voiceButtonProxy != null) {
3061 voiceButtonProxy.setVisibility(View.VISIBLE);
3062 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003063 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003064 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003065 } else {
Winson Chung649723c2011-07-06 20:41:23 -07003066 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003067 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003068 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003069 if (voiceButtonProxy != null) {
3070 voiceButtonProxy.setVisibility(View.GONE);
3071 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003072 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003073 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003074 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003075
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003076 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003077 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3078 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003079 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003080 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003081 }
3082
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003083 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003084 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003085 */
3086 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003087 final View marketButton = findViewById(R.id.market_button);
3088 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3089 // Find the app market activity by resolving an intent.
3090 // (If multiple app markets are installed, it will return the ResolverActivity.)
3091 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003092 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003093 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003094 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003095 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003096 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3097 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003098 marketButton.setVisibility(View.VISIBLE);
3099 } else {
3100 // We should hide and disable the view so that we don't try and restore the visibility
3101 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3102 marketButton.setVisibility(View.GONE);
3103 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003104 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003105 }
3106
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003107 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003108 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3109 Resources r = getResources();
3110 Drawable marketIconDrawable = d.newDrawable(r);
3111 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3112 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3113 marketIconDrawable.setBounds(0, 0, w, h);
3114
3115 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003116 }
3117
Michael Jurkad7c28052012-04-27 15:43:36 -07003118 @Override
3119 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3120 boolean result = super.dispatchPopulateAccessibilityEvent(event);
3121 final List<CharSequence> text = event.getText();
3122 text.clear();
3123 text.add(getString(R.string.home));
3124 return result;
3125 }
3126
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003127 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003128 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003129 */
3130 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3131 @Override
3132 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003133 closeSystemDialogs();
3134 }
3135 }
3136
3137 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003138 * Receives notifications whenever the appwidgets are reset.
3139 */
3140 private class AppWidgetResetObserver extends ContentObserver {
3141 public AppWidgetResetObserver() {
3142 super(new Handler());
3143 }
3144
3145 @Override
3146 public void onChange(boolean selfChange) {
3147 onAppWidgetReset();
3148 }
3149 }
3150
3151 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003152 * If the activity is currently paused, signal that we need to re-run the loader
3153 * in onResume.
3154 *
3155 * This needs to be called from incoming places where resources might have been loaded
3156 * while we are paused. That is becaues the Configuration might be wrong
3157 * when we're not running, and if it comes back to what it was when we
3158 * were paused, we are not restarted.
3159 *
3160 * Implementation of the method from LauncherModel.Callbacks.
3161 *
3162 * @return true if we are currently paused. The caller might be able to
3163 * skip some work in that case since we will come back again.
3164 */
3165 public boolean setLoadOnResume() {
3166 if (mPaused) {
3167 Log.i(TAG, "setLoadOnResume");
3168 mOnResumeNeedsLoad = true;
3169 return true;
3170 } else {
3171 return false;
3172 }
3173 }
3174
3175 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003176 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003177 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003178 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003179 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003180 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003181 } else {
3182 return SCREEN_COUNT / 2;
3183 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003184 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003185
Joe Onorato9c1289c2009-08-17 11:03:03 -04003186 /**
3187 * Refreshes the shortcuts shown on the workspace.
3188 *
3189 * Implementation of the method from LauncherModel.Callbacks.
3190 */
3191 public void startBinding() {
3192 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003193
Winson Chungf0c6ae02012-03-21 16:10:31 -07003194 mNewShortcutAnimatePage = -1;
3195 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003196 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003197 int count = workspace.getChildCount();
3198 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003199 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003200 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3201 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003202 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003203 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003204 if (mHotseat != null) {
3205 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003206 }
3207 }
3208
3209 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003210 * Bind the items start-end from the list.
3211 *
3212 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003213 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003214 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003215 setLoadOnResume();
3216
Winson Chungf0c6ae02012-03-21 16:10:31 -07003217 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3218 Set<String> newApps = new HashSet<String>();
3219 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3220
3221 Workspace workspace = mWorkspace;
3222 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003223 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003224
3225 // Short circuit if we are loading dock items for a configuration which has no dock
3226 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3227 mHotseat == null) {
3228 continue;
3229 }
3230
Joe Onorato9c1289c2009-08-17 11:03:03 -04003231 switch (item.itemType) {
3232 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3233 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003234 ShortcutInfo info = (ShortcutInfo) item;
3235 String uri = info.intent.toUri(0).toString();
3236 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003237 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3238 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003239 boolean animateIconUp = false;
3240 synchronized (newApps) {
3241 if (newApps.contains(uri)) {
3242 animateIconUp = newApps.remove(uri);
3243 }
3244 }
3245 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003246 // Prepare the view to be animated up
3247 shortcut.setAlpha(0f);
3248 shortcut.setScaleX(0f);
3249 shortcut.setScaleY(0f);
3250 mNewShortcutAnimatePage = item.screen;
3251 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3252 mNewShortcutAnimateViews.add(shortcut);
3253 }
3254 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003255 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003256 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003257 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003258 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003259 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003260 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3261 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003262 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003263 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003264 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003265
Joe Onorato9c1289c2009-08-17 11:03:03 -04003266 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003267 }
3268
Joe Onorato9c1289c2009-08-17 11:03:03 -04003269 /**
3270 * Implementation of the method from LauncherModel.Callbacks.
3271 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003272 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003273 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003274 sFolders.clear();
3275 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003276 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003277
3278 /**
3279 * Add the views for a widget to the workspace.
3280 *
3281 * Implementation of the method from LauncherModel.Callbacks.
3282 */
3283 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003284 setLoadOnResume();
3285
Daniel Sandler843e8602010-06-07 14:59:01 -04003286 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3287 if (DEBUG_WIDGETS) {
3288 Log.d(TAG, "bindAppWidget: " + item);
3289 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003290 final Workspace workspace = mWorkspace;
3291
3292 final int appWidgetId = item.appWidgetId;
3293 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003294 if (DEBUG_WIDGETS) {
3295 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3296 }
3297
Joe Onorato9c1289c2009-08-17 11:03:03 -04003298 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3299
Joe Onorato9c1289c2009-08-17 11:03:03 -04003300 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003301 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003302
Winson Chung3d503fb2011-07-13 17:25:49 -07003303 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003304 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003305 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3306
Joe Onorato9c1289c2009-08-17 11:03:03 -04003307 workspace.requestLayout();
3308
Daniel Sandler843e8602010-06-07 14:59:01 -04003309 if (DEBUG_WIDGETS) {
3310 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3311 + (SystemClock.uptimeMillis()-start) + "ms");
3312 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003313 }
3314
Adam Cohen1462de32012-07-24 22:34:36 -07003315 public void onPageBoundSynchronously(int page) {
3316 mSynchronouslyBoundPages.add(page);
3317 }
3318
Joe Onorato9c1289c2009-08-17 11:03:03 -04003319 /**
3320 * Callback saying that there aren't any more items to bind.
3321 *
3322 * Implementation of the method from LauncherModel.Callbacks.
3323 */
3324 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003325 setLoadOnResume();
3326
Joe Onorato9c1289c2009-08-17 11:03:03 -04003327 if (mSavedState != null) {
3328 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003329 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003330 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003331 mSavedState = null;
3332 }
3333
Adam Cohen1462de32012-07-24 22:34:36 -07003334 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003335
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003336 // If we received the result of any pending adds while the loader was running (e.g. the
3337 // widget configuration forced an orientation change), process them now.
3338 for (int i = 0; i < sPendingAddList.size(); i++) {
3339 completeAdd(sPendingAddList.get(i));
3340 }
3341 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003342
Winson Chungc51db6a2011-10-05 11:44:49 -07003343 // Update the market app icon as necessary (the other icons will be managed in response to
3344 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003345 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003346
Winson Chungf0c6ae02012-03-21 16:10:31 -07003347 // Animate up any icons as necessary
3348 if (mVisible || mWorkspaceLoading) {
3349 Runnable newAppsRunnable = new Runnable() {
3350 @Override
3351 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003352 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003353 }
3354 };
Winson Chunga2413752012-04-03 14:22:34 -07003355
3356 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3357 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3358 if (canRunNewAppsAnimation()) {
3359 // If the user has not interacted recently, then either snap to the new page to show
3360 // the new-apps animation or just run them if they are to appear on the current page
3361 if (willSnapPage) {
3362 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3363 } else {
3364 runNewAppsAnimation(false);
3365 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003366 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003367 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003368 // are on another page (or just normally if they are added to the current page)
3369 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003370 }
3371 }
3372
3373 mWorkspaceLoading = false;
3374 }
3375
Winson Chunga2413752012-04-03 14:22:34 -07003376 private boolean canRunNewAppsAnimation() {
3377 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3378 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3379 }
3380
Winson Chungf0c6ae02012-03-21 16:10:31 -07003381 /**
3382 * Runs a new animation that scales up icons that were added while Launcher was in the
3383 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003384 *
3385 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003386 */
Winson Chunga2413752012-04-03 14:22:34 -07003387 private void runNewAppsAnimation(boolean immediate) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003388 AnimatorSet anim = new AnimatorSet();
3389 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003390
3391 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003392 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3393 @Override
3394 public int compare(View a, View b) {
3395 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3396 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3397 int cellCountX = LauncherModel.getCellCountX();
3398 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3399 }
3400 });
Winson Chunga2413752012-04-03 14:22:34 -07003401
3402 // Animate each of the views in place (or show them immediately if requested)
3403 if (immediate) {
3404 for (View v : mNewShortcutAnimateViews) {
3405 v.setAlpha(1f);
3406 v.setScaleX(1f);
3407 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003408 }
Winson Chunga2413752012-04-03 14:22:34 -07003409 } else {
3410 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3411 View v = mNewShortcutAnimateViews.get(i);
3412 ValueAnimator bounceAnim = ObjectAnimator.ofPropertyValuesHolder(v,
3413 PropertyValuesHolder.ofFloat("alpha", 1f),
3414 PropertyValuesHolder.ofFloat("scaleX", 1f),
3415 PropertyValuesHolder.ofFloat("scaleY", 1f));
3416 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3417 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3418 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3419 bounceAnims.add(bounceAnim);
3420 }
3421 anim.playTogether(bounceAnims);
3422 anim.addListener(new AnimatorListenerAdapter() {
3423 @Override
3424 public void onAnimationEnd(Animator animation) {
3425 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3426 }
3427 });
3428 anim.start();
3429 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003430
3431 // Clean up
3432 mNewShortcutAnimatePage = -1;
3433 mNewShortcutAnimateViews.clear();
3434 new Thread("clearNewAppsThread") {
3435 public void run() {
3436 mSharedPrefs.edit()
3437 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3438 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3439 .commit();
3440 }
3441 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003442 }
3443
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003444 @Override
3445 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003446 boolean searchVisible = updateGlobalSearchIcon();
3447 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3448 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003449 }
3450
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003451 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003452 * Add the icons for all apps.
3453 *
3454 * Implementation of the method from LauncherModel.Callbacks.
3455 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003456 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003457 Runnable setAllAppsRunnable = new Runnable() {
3458 public void run() {
3459 if (mAppsCustomizeContent != null) {
3460 mAppsCustomizeContent.setApps(apps);
3461 }
3462 }
3463 };
3464
Michael Jurkac57b7a82011-08-09 22:02:20 -07003465 // Remove the progress bar entirely; we could also make it GONE
3466 // but better to remove it since we know it's not going to be used
3467 View progressBar = mAppsCustomizeTabHost.
3468 findViewById(R.id.apps_customize_progress_bar);
3469 if (progressBar != null) {
3470 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003471
3472 // We just post the call to setApps so the user sees the progress bar
3473 // disappear-- otherwise, it just looks like the progress bar froze
3474 // which doesn't look great
3475 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3476 } else {
3477 // If we did not initialize the spinner in onCreate, then we can directly set the
3478 // list of applications without waiting for any progress bars views to be hidden.
3479 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003480 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003481 }
3482
3483 /**
3484 * A package was installed.
3485 *
3486 * Implementation of the method from LauncherModel.Callbacks.
3487 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003488 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003489 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003490
Winson Chung785d2eb2011-04-14 16:08:02 -07003491 if (mAppsCustomizeContent != null) {
3492 mAppsCustomizeContent.addApps(apps);
3493 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003494 }
3495
3496 /**
3497 * A package was updated.
3498 *
3499 * Implementation of the method from LauncherModel.Callbacks.
3500 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003501 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003502 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003503 if (mWorkspace != null) {
3504 mWorkspace.updateShortcuts(apps);
3505 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003506
Winson Chung785d2eb2011-04-14 16:08:02 -07003507 if (mAppsCustomizeContent != null) {
3508 mAppsCustomizeContent.updateApps(apps);
3509 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003510 }
3511
3512 /**
3513 * A package was uninstalled.
3514 *
3515 * Implementation of the method from LauncherModel.Callbacks.
3516 */
Joe Onorato36115782010-06-17 13:28:48 -04003517 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003518 if (permanent) {
3519 mWorkspace.removeItems(apps);
3520 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003521
Winson Chung785d2eb2011-04-14 16:08:02 -07003522 if (mAppsCustomizeContent != null) {
3523 mAppsCustomizeContent.removeApps(apps);
3524 }
Winson Chunga1820962011-10-03 16:31:06 -07003525
3526 // Notify the drag controller
3527 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003528 }
Joe Onoratobe386092009-11-17 17:32:16 -08003529
3530 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003531 * A number of packages were updated.
3532 */
3533 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003534 if (mAppsCustomizeContent != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003535 mAppsCustomizeContent.onPackagesUpdated(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07003536 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003537 }
3538
Winson Chung400438b2011-01-16 17:53:48 -08003539 private int mapConfigurationOriActivityInfoOri(int configOri) {
3540 final Display d = getWindowManager().getDefaultDisplay();
3541 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3542 switch (d.getRotation()) {
3543 case Surface.ROTATION_0:
3544 case Surface.ROTATION_180:
3545 // We are currently in the same basic orientation as the natural orientation
3546 naturalOri = configOri;
3547 break;
3548 case Surface.ROTATION_90:
3549 case Surface.ROTATION_270:
3550 // We are currently in the other basic orientation to the natural orientation
3551 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3552 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3553 break;
3554 }
3555
3556 int[] oriMap = {
3557 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3558 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3559 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3560 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3561 };
3562 // Since the map starts at portrait, we need to offset if this device's natural orientation
3563 // is landscape.
3564 int indexOffset = 0;
3565 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3566 indexOffset = 1;
3567 }
3568 return oriMap[(d.getRotation() + indexOffset) % 4];
3569 }
Adam Cohen446e9402011-09-15 18:21:21 -07003570
Winson Chung4b919f82012-05-01 10:44:08 -07003571 public boolean isRotationEnabled() {
3572 boolean forceEnableRotation = "true".equalsIgnoreCase(SystemProperties.get(
3573 FORCE_ENABLE_ROTATION_PROPERTY, "false"));
3574 boolean enableRotation = forceEnableRotation ||
3575 getResources().getBoolean(R.bool.allow_rotation);
3576 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003577 }
Winson Chung4b919f82012-05-01 10:44:08 -07003578 public void lockScreenOrientation() {
3579 if (isRotationEnabled()) {
3580 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3581 .getConfiguration().orientation));
3582 }
3583 }
3584 public void unlockScreenOrientation(boolean immediate) {
3585 if (isRotationEnabled()) {
3586 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003587 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003588 } else {
3589 mHandler.postDelayed(new Runnable() {
3590 public void run() {
3591 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3592 }
3593 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003594 }
Winson Chung4b919f82012-05-01 10:44:08 -07003595 }
Winson Chung400438b2011-01-16 17:53:48 -08003596 }
3597
Winson Chung82f55532011-08-09 14:14:23 -07003598 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003599 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003600 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003601 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003602
Winson Chung7d7541e2011-09-16 20:14:36 -07003603 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003604 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003605 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3606 Cling cling = (Cling) findViewById(clingId);
3607 if (cling != null) {
3608 cling.init(this, positionData);
3609 cling.setVisibility(View.VISIBLE);
3610 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Svetoslav Ganov55d225d2012-05-22 15:19:14 -07003611 cling.requestAccessibilityFocus();
Winson Chung7d7541e2011-09-16 20:14:36 -07003612 if (animate) {
3613 cling.buildLayer();
3614 cling.setAlpha(0f);
3615 cling.animate()
3616 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003617 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003618 .setDuration(SHOW_CLING_DURATION)
3619 .setStartDelay(delay)
3620 .start();
3621 } else {
3622 cling.setAlpha(1f);
3623 }
3624 }
3625 return cling;
3626 }
3627 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003628 if (cling != null) {
3629 ObjectAnimator anim = ObjectAnimator.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003630 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003631 anim.addListener(new AnimatorListenerAdapter() {
3632 public void onAnimationEnd(Animator animation) {
3633 cling.setVisibility(View.GONE);
3634 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003635 // We should update the shared preferences on a background thread
3636 new Thread("dismissClingThread") {
3637 public void run() {
3638 SharedPreferences.Editor editor = mSharedPrefs.edit();
3639 editor.putBoolean(flag, true);
3640 editor.commit();
3641 }
3642 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003643 };
3644 });
3645 anim.start();
3646 }
3647 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003648 private void removeCling(int id) {
3649 final View cling = findViewById(id);
3650 if (cling != null) {
3651 final ViewGroup parent = (ViewGroup) cling.getParent();
3652 parent.post(new Runnable() {
3653 @Override
3654 public void run() {
3655 parent.removeView(cling);
3656 }
3657 });
3658 }
3659 }
Michael Jurka974c3862012-05-22 22:00:31 -07003660
3661 private boolean skipCustomClingIfNoAccounts() {
3662 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3663 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3664 if (customCling) {
3665 AccountManager am = AccountManager.get(this);
3666 Account[] accounts = am.getAccountsByType("com.google");
3667 return accounts.length == 0;
3668 }
3669 return false;
3670 }
3671
Winson Chung7d7541e2011-09-16 20:14:36 -07003672 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003673 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003674 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003675 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3676 !skipCustomClingIfNoAccounts() ) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003677 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003678 } else {
3679 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003680 }
3681 }
3682 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003683 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003684 if (isClingsEnabled() &&
3685 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003686 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003687 } else {
3688 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003689 }
3690 }
3691 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003692 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003693 if (isClingsEnabled() &&
3694 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3695 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003696 } else {
3697 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003698 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003699 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003700 }
3701 public boolean isFolderClingVisible() {
3702 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003703 if (cling != null) {
3704 return cling.getVisibility() == View.VISIBLE;
3705 }
3706 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003707 }
Winson Chung82f55532011-08-09 14:14:23 -07003708 public void dismissWorkspaceCling(View v) {
3709 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003710 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003711 }
3712 public void dismissAllAppsCling(View v) {
3713 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003714 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3715 }
3716 public void dismissFolderCling(View v) {
3717 Cling cling = (Cling) findViewById(R.id.folder_cling);
3718 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003719 }
3720
Winson Chung80baf5a2010-08-09 16:03:15 -07003721 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003722 * Prints out out state for debugging.
3723 */
3724 public void dumpState() {
3725 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003726 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003727 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3728 Log.d(TAG, "mRestoring=" + mRestoring);
3729 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3730 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003731 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003732 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003733
Winson Chung785d2eb2011-04-14 16:08:02 -07003734 if (mAppsCustomizeContent != null) {
3735 mAppsCustomizeContent.dumpState();
3736 }
Joe Onoratobe386092009-11-17 17:32:16 -08003737 Log.d(TAG, "END launcher2 dump state");
3738 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003739
3740 @Override
3741 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3742 super.dump(prefix, fd, writer, args);
3743 writer.println(" ");
3744 writer.println("Debug logs: ");
3745 for (int i = 0; i < sDumpLogs.size(); i++) {
3746 writer.println(" " + sDumpLogs.get(i));
3747 }
3748 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003749}
Michael Jurka2763be32011-02-24 11:19:57 -08003750
Michael Jurkaabded662011-03-04 12:06:57 -08003751interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003752 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003753 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003754 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003755 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003756 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003757}