blob: 4906e8153e62df498d4876660b4911bb76fa46bb [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;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070064import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080065import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080066import android.text.Selection;
67import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070068import android.text.TextUtils;
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;
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700102import java.io.FileInputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.FileNotFoundException;
104import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.Collection;
108import java.util.Collections;
109import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700111import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700112import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700113import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115/**
116 * Default launcher application.
117 */
Michael Jurka946ad472010-07-09 18:05:18 -0700118public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700119 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
120 AllAppsView.Watcher, View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800121 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Joe Onorato9c1289c2009-08-17 11:03:03 -0400124 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400125 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700126 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700127
Winson Chung70d72102011-08-12 11:24:35 -0700128 private static final int MENU_GROUP_WALLPAPER = 1;
129 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
130 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700131 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
132 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133
134 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700135 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136 private static final int REQUEST_PICK_APPLICATION = 6;
137 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700138 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700139 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Michael Jurka8b805b12012-04-18 14:23:14 -0700141 private static final int REQUEST_BIND_APPWIDGET = 11;
142
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
144
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800145 static final int SCREEN_COUNT = 5;
146 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147
Romain Guy98d01652009-06-30 16:21:04 -0700148 private static final String PREFERENCES = "launcher.preferences";
Michael Jurkaf3bade62012-06-12 11:00:21 -0700149 static final String FORCE_ENABLE_ROTATION_PROPERTY = "debug.force_enable_rotation";
150 static final String DUMP_STATE_PROPERTY = "debug.dumpstate";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151
Winson Chung2672ff92012-05-04 16:22:30 -0700152 // The Intent extra that defines whether to ignore the launch animation
153 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
154 "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
155
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156 // Type: int
157 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700158 // Type: int
159 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700161 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
162 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
164 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700165 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700167 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168 // Type: boolean
169 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
170 // Type: long
171 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700172 // Type: int
173 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
174 // Type: int
175 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
176 // Type: parcelable
177 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700179 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700180 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
181 "com.android.launcher.toolbar_search_icon";
182 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
183 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700184
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700185 /** The different states that Launcher can be in. */
Winson Chungf0ea4d32011-06-06 14:27:16 -0700186 private enum State { WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700187 private State mState = State.WORKSPACE;
188 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800189 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700190
Joe Onorato9c1289c2009-08-17 11:03:03 -0400191 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700192 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
193 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700194 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700195 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800196
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800198 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800199
Winson Chunga2413752012-04-03 14:22:34 -0700200 // How long to wait before the new-shortcut animation automatically pans the workspace
201 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
202
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800203 private final BroadcastReceiver mCloseSystemDialogsReceiver
204 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800205 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
206
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207 private LayoutInflater mInflater;
208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800210 private View mQsbDivider;
211 private View mDockDivider;
212 private DragLayer mDragLayer;
213 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700214
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700215 private AppWidgetManager mAppWidgetManager;
216 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700217
Michael Jurkac9d95c52011-08-29 14:03:34 -0700218 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700219 private AppWidgetProviderInfo mPendingAddWidgetInfo;
220
Michael Jurka0280c3b2010-09-17 15:00:07 -0700221 private int[] mTmpAddItemCellCoordinates = new int[2];
222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private FolderInfo mFolderInfo;
224
Winson Chung3d503fb2011-07-13 17:25:49 -0700225 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800226 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700227
Winson Chungc51db6a2011-10-05 11:44:49 -0700228 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700229 private AppsCustomizeTabHost mAppsCustomizeTabHost;
230 private AppsCustomizePagedView mAppsCustomizeContent;
231 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233 private Bundle mSavedState;
234
235 private SpannableStringBuilder mDefaultKeySsb = null;
236
Joe Onorato9c1289c2009-08-17 11:03:03 -0400237 private boolean mWorkspaceLoading = true;
238
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800239 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 private boolean mRestoring;
241 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700242 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800243
244 private Bundle mSavedInstanceState;
245
Joe Onorato9c1289c2009-08-17 11:03:03 -0400246 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800247 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800248 private boolean mUserPresent = true;
249 private boolean mVisible = false;
250 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400251
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700252 private static LocaleConfiguration sLocaleConfiguration = null;
253
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700254 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700255
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700256 private Intent mAppMarketIntent = null;
257
Adam Cohended9f8d2010-11-03 13:25:16 -0700258 // Related to the auto-advancing of widgets
259 private final int ADVANCE_MSG = 1;
260 private final int mAdvanceInterval = 20000;
261 private final int mAdvanceStagger = 250;
262 private long mAutoAdvanceSentTime;
263 private long mAutoAdvanceTimeLeft = -1;
264 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
265 new HashMap<View, AppWidgetProviderInfo>();
266
Winson Chung400438b2011-01-16 17:53:48 -0800267 // Determines how long to wait after a rotation before restoring the screen orientation to
268 // match the sensor state.
269 private final int mRestoreScreenOrientationDelay = 500;
270
Michael Jurka4ef207b2010-11-29 17:05:45 -0800271 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700272 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
273 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
274 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800275
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700276 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
277
Winson Chung46353de2012-02-16 14:05:10 -0800278 // We only want to get the SharedPreferences once since it does an FS stat each time we get
279 // it from the context.
280 private SharedPreferences mSharedPrefs;
281
Winson Chungf0c6ae02012-03-21 16:10:31 -0700282 // Holds the page that we need to animate to, and the icon views that we need to animate up
283 // when we scroll to that page on resume.
284 private int mNewShortcutAnimatePage = -1;
285 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700286 private ImageView mFolderIconImageView;
287 private Bitmap mFolderIconBitmap;
288 private Canvas mFolderIconCanvas;
289 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700290
Michael Jurkaddd62e92011-02-16 17:49:14 -0800291 private BubbleTextView mWaitingForResume;
292
Michael Jurkac1f5d262011-09-30 19:32:27 -0700293 private Runnable mBuildLayersRunnable = new Runnable() {
294 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700295 if (mWorkspace != null) {
296 mWorkspace.buildPageHardwareLayers();
297 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700298 }
299 };
300
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800301 private static ArrayList<PendingAddArguments> sPendingAddList
302 = new ArrayList<PendingAddArguments>();
303
304 private static class PendingAddArguments {
305 int requestCode;
306 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700307 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800308 int screen;
309 int cellX;
310 int cellY;
311 }
312
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700313
314 private boolean doesFileExist(String filename) {
315 FileInputStream fis = null;
316 try {
317 fis = openFileInput(filename);
318 fis.close();
319 return true;
320 } catch (java.io.FileNotFoundException e) {
321 return false;
322 } catch (java.io.IOException e) {
323 return true;
324 }
325 }
326
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800327 @Override
328 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700329 if (DEBUG_STRICT_MODE) {
330 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
331 .detectDiskReads()
332 .detectDiskWrites()
333 .detectNetwork() // or .detectAll() for all detectable problems
334 .penaltyLog()
335 .build());
336 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
337 .detectLeakedSqlLiteObjects()
338 .detectLeakedClosableObjects()
339 .penaltyLog()
340 .penaltyDeath()
341 .build());
342 }
343
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800344 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800345 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700346 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
347 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800348 mModel = app.setLauncher(this);
349 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400350 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800351 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700352
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700353 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700354 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
355 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200358 android.os.Debug.startMethodTracing(
359 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800360 }
361
362 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800363 setContentView(R.layout.launcher);
364 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700365 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800366
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800367 registerContentObservers();
368
Joe Onorato7c312c12009-08-13 21:36:53 -0700369 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700370
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371 mSavedState = savedInstanceState;
372 restoreState(mSavedState);
373
Winson Chunga12a2502010-12-20 14:41:35 -0800374 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700375 if (mAppsCustomizeContent != null) {
376 mAppsCustomizeContent.onPackagesUpdated();
377 }
Winson Chunga12a2502010-12-20 14:41:35 -0800378
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800379 if (PROFILE_STARTUP) {
380 android.os.Debug.stopMethodTracing();
381 }
382
383 if (!mRestoring) {
Winson Chungf0c6ae02012-03-21 16:10:31 -0700384 mModel.startLoader(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800385 }
386
Michael Jurkac57b7a82011-08-09 22:02:20 -0700387 if (!mModel.isAllAppsLoaded()) {
388 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
389 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
390 }
391
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800392 // For handling default keys
393 mDefaultKeySsb = new SpannableStringBuilder();
394 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800395
396 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
397 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800398
Adam Cohenf9426d52012-06-04 17:26:21 -0700399 updateGlobalIcons();
400
401 // On large interfaces, we want the screen to auto-rotate based on the current orientation
402 unlockScreenOrientation(true);
403 }
404
405 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700406 boolean searchVisible = false;
407 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800408 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700409 int coi = getCurrentOrientationIndexForGlobalIcons();
410 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
411 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700412 updateAppMarketIcon();
413 searchVisible = updateGlobalSearchIcon();
414 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700415 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700416 if (sGlobalSearchIcon[coi] != null) {
417 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700418 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700419 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700420 if (sVoiceSearchIcon[coi] != null) {
421 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700422 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700423 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700424 if (sAppMarketIcon[coi] != null) {
425 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800426 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700427 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 }
Romain Guycbb89e42009-06-08 15:52:54 -0700429
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700431 if (sLocaleConfiguration == null) {
432 new AsyncTask<Void, Void, LocaleConfiguration>() {
433 @Override
434 protected LocaleConfiguration doInBackground(Void... unused) {
435 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
436 readConfiguration(Launcher.this, localeConfiguration);
437 return localeConfiguration;
438 }
439
440 @Override
441 protected void onPostExecute(LocaleConfiguration result) {
442 sLocaleConfiguration = result;
443 checkForLocaleChange(); // recursive, but now with a locale configuration
444 }
445 }.execute();
446 return;
447 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700448
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 final Configuration configuration = getResources().getConfiguration();
450
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700451 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 final String locale = configuration.locale.toString();
453
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700454 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800455 final int mcc = configuration.mcc;
456
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700457 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 final int mnc = configuration.mnc;
459
Romain Guy84f296c2009-11-04 15:00:44 -0800460 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461
Romain Guy84f296c2009-11-04 15:00:44 -0800462 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700463 sLocaleConfiguration.locale = locale;
464 sLocaleConfiguration.mcc = mcc;
465 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700466
Joe Onorato0589f0f2010-02-08 13:44:00 -0800467 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700468
469 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
470 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700471 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700472 public void run() {
473 writeConfiguration(Launcher.this, localeConfiguration);
474 }
475 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700476 }
477 }
478
479 private static class LocaleConfiguration {
480 public String locale;
481 public int mcc = -1;
482 public int mnc = -1;
483 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700484
Romain Guy98d01652009-06-30 16:21:04 -0700485 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
486 DataInputStream in = null;
487 try {
488 in = new DataInputStream(context.openFileInput(PREFERENCES));
489 configuration.locale = in.readUTF();
490 configuration.mcc = in.readInt();
491 configuration.mnc = in.readInt();
492 } catch (FileNotFoundException e) {
493 // Ignore
494 } catch (IOException e) {
495 // Ignore
496 } finally {
497 if (in != null) {
498 try {
499 in.close();
500 } catch (IOException e) {
501 // Ignore
502 }
503 }
504 }
505 }
506
507 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
508 DataOutputStream out = null;
509 try {
510 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
511 out.writeUTF(configuration.locale);
512 out.writeInt(configuration.mcc);
513 out.writeInt(configuration.mnc);
514 out.flush();
515 } catch (FileNotFoundException e) {
516 // Ignore
517 } catch (IOException e) {
518 //noinspection ResultOfMethodCallIgnored
519 context.getFileStreamPath(PREFERENCES).delete();
520 } finally {
521 if (out != null) {
522 try {
523 out.close();
524 } catch (IOException e) {
525 // Ignore
526 }
527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800528 }
529 }
530
Adam Cohen716b51e2011-06-30 12:09:54 -0700531 public DragLayer getDragLayer() {
532 return mDragLayer;
533 }
534
Winson Chung36a62fe2012-05-06 18:04:42 -0700535 boolean isDraggingEnabled() {
536 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
537 // that is subsequently removed from the workspace in startBinding().
538 return !mModel.isLoadingWorkspace();
539 }
540
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800541 static int getScreen() {
542 synchronized (sLock) {
543 return sScreen;
544 }
545 }
546
547 static void setScreen(int screen) {
548 synchronized (sLock) {
549 sScreen = screen;
550 }
551 }
552
Winson Chung557d6ed2011-07-08 15:34:52 -0700553 /**
554 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
555 * a configuration step, this allows the proper animations to run after other transitions.
556 */
557 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700558 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800559 switch (args.requestCode) {
560 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700561 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
562 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800563 break;
564 case REQUEST_PICK_SHORTCUT:
565 processShortcut(args.intent);
566 break;
567 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700568 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
569 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700570 result = true;
571 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800572 case REQUEST_CREATE_APPWIDGET:
573 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800574 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700575 result = true;
576 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800577 case REQUEST_PICK_WALLPAPER:
578 // We just wanted the activity result here so we can clear mWaitingForResult
579 break;
580 }
Michael Jurka27614d22012-04-02 06:40:22 -0700581 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
582 // if you turned the screen off and then back while in All Apps, Launcher would not
583 // return to the workspace. Clearing mAddInfo.container here fixes this issue
584 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700585 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800586 }
587
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800588 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700589 protected void onActivityResult(
590 final int requestCode, final int resultCode, final Intent data) {
591 if (requestCode == REQUEST_BIND_APPWIDGET) {
592 int appWidgetId = data != null ?
593 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
594 if (resultCode == RESULT_CANCELED) {
595 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
596 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700597 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700598 }
599 return;
600 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700601 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800602 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
603 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700604 mWaitingForResult = false;
605
Adam Cohened66b2b2012-01-23 17:28:51 -0800606 // We have special handling for widgets
607 if (isWidgetDrop) {
608 int appWidgetId = data != null ?
609 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700610 if (appWidgetId < 0) {
611 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
612 "widget configuration activity.");
613 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
614 } else {
615 completeTwoStageWidgetDrop(resultCode, appWidgetId);
616 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800617 return;
618 }
619
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 // The pattern used here is that a user PICKs a specific application,
621 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700622
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
624 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700625 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800626 final PendingAddArguments args = new PendingAddArguments();
627 args.requestCode = requestCode;
628 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700629 args.container = mPendingAddInfo.container;
630 args.screen = mPendingAddInfo.screen;
631 args.cellX = mPendingAddInfo.cellX;
632 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800633 if (isWorkspaceLocked()) {
634 sPendingAddList.add(args);
635 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700636 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800638 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800639 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700640 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800641 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
642 null);
643 }
644
645 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700646 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700647 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800648 Runnable onCompleteRunnable = null;
649 int animationType = 0;
650
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700651 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800652 if (resultCode == RESULT_OK) {
653 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
654 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700655 mPendingAddWidgetInfo);
656 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800657 onCompleteRunnable = new Runnable() {
658 @Override
659 public void run() {
660 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
661 mPendingAddInfo.screen, layout, null);
662 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
663 null);
664 }
665 };
666 } else if (resultCode == RESULT_CANCELED) {
667 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
668 onCompleteRunnable = new Runnable() {
669 @Override
670 public void run() {
671 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
672 null);
673 }
674 };
675 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700676 if (mDragLayer.getAnimatedView() != null) {
677 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
678 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
679 animationType, boundWidget, true);
680 } else {
681 // The animated view may be null in the case of a rotation during widget configuration
682 onCompleteRunnable.run();
683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
685
686 @Override
687 protected void onResume() {
688 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700689
Winson Chungde0fb8f2012-05-08 14:37:08 -0700690 // Process any items that were added while Launcher was away
691 InstallShortcutReceiver.flushInstallQueue(this);
692
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800693 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700694 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400695 mWorkspaceLoading = true;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700696 mModel.startLoader(true);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400697 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700698 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 }
Winson Chunge4e50662012-01-23 14:45:13 -0800700
701 // Reset the pressed state of icons that were locked in the press state while activities
702 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800703 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800704 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800705 mWaitingForResume.setStayPressed(false);
706 }
Winson Chunge4e50662012-01-23 14:45:13 -0800707 if (mAppsCustomizeContent != null) {
708 // Resets the previous all apps icon press state
709 mAppsCustomizeContent.resetDrawableState();
710 }
Adam Cohen06dff352012-06-01 17:17:08 -0700711 // It is possible that widgets can receive updates while launcher is not in the foreground.
712 // Consequently, the widgets will be inflated in the orientation of the foreground activity
713 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
714 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700715 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700716
717 // Again, as with the above scenario, it's possible that one or more of the global icons
718 // were updated in the wrong orientation.
719 updateGlobalIcons();
Adam Cohen06dff352012-06-01 17:17:08 -0700720 }
721
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800722 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700723 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700724 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
725 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
726 // when Launcher resumes and we are still in AllApps.
727 updateWallpaperVisibility(true);
728
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700729 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700730 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800731 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700732 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700733 }
Romain Guycbb89e42009-06-08 15:52:54 -0700734
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700735 @Override
736 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 // Flag the loader to stop early before switching
738 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700739 if (mAppsCustomizeContent != null) {
740 mAppsCustomizeContent.surrender();
741 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800742 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700743 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700744
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800745 // We can't hide the IME if it was forced open. So don't bother
746 /*
747 @Override
748 public void onWindowFocusChanged(boolean hasFocus) {
749 super.onWindowFocusChanged(hasFocus);
750
751 if (hasFocus) {
752 final InputMethodManager inputManager = (InputMethodManager)
753 getSystemService(Context.INPUT_METHOD_SERVICE);
754 WindowManager.LayoutParams lp = getWindow().getAttributes();
755 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
756 android.os.Handler()) {
757 protected void onReceiveResult(int resultCode, Bundle resultData) {
758 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
759 }
760 });
761 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
762 }
763 }
764 */
765
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800766 private boolean acceptFilter() {
767 final InputMethodManager inputManager = (InputMethodManager)
768 getSystemService(Context.INPUT_METHOD_SERVICE);
769 return !inputManager.isFullscreenMode();
770 }
771
772 @Override
773 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700774 final int uniChar = event.getUnicodeChar();
775 final boolean handled = super.onKeyDown(keyCode, event);
776 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
777 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800778 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
779 keyCode, event);
780 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700781 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700782 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700783 // showSearchDialog()
784 // If there are multiple keystrokes before the search dialog takes focus,
785 // onSearchRequested() will be called for every keystroke,
786 // but it is idempotent, so it's fine.
787 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800788 }
789 }
790
Joe Onorato8a9625e2010-01-28 15:55:35 -0800791 // Eat the long press event so the keyboard doesn't come up.
792 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
793 return true;
794 }
795
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800796 return handled;
797 }
798
Karl Rosaen138a0412009-04-23 19:00:21 -0700799 private String getTypedText() {
800 return mDefaultKeySsb.toString();
801 }
802
803 private void clearTypedText() {
804 mDefaultKeySsb.clear();
805 mDefaultKeySsb.clearSpans();
806 Selection.setSelection(mDefaultKeySsb, 0);
807 }
808
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800809 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700810 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
811 * State
812 */
813 private static State intToState(int stateOrdinal) {
814 State state = State.WORKSPACE;
815 final State[] stateValues = State.values();
816 for (int i = 0; i < stateValues.length; i++) {
817 if (stateValues[i].ordinal() == stateOrdinal) {
818 state = stateValues[i];
819 break;
820 }
821 }
822 return state;
823 }
824
825 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800826 * Restores the previous state, if it exists.
827 *
828 * @param savedState The previous state.
829 */
830 private void restoreState(Bundle savedState) {
831 if (savedState == null) {
832 return;
833 }
834
Michael Jurka883f55b2010-10-21 15:47:14 -0700835 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700836 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800837 showAllApps(false);
838 }
839
Winson Chungf0c6ae02012-03-21 16:10:31 -0700840 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800841 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700842 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 }
844
Winson Chung3d503fb2011-07-13 17:25:49 -0700845 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
846 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700847
Winson Chung3d503fb2011-07-13 17:25:49 -0700848 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
849 mPendingAddInfo.container = pendingAddContainer;
850 mPendingAddInfo.screen = pendingAddScreen;
851 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
852 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700853 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
854 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
855 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700856 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 mRestoring = true;
858 }
859
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700860
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
862 if (renameFolder) {
863 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700864 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 mRestoring = true;
866 }
Winson Chunga12a2502010-12-20 14:41:35 -0800867
Winson Chung785d2eb2011-04-14 16:08:02 -0700868
869 // Restore the AppsCustomize tab
870 if (mAppsCustomizeTabHost != null) {
871 String curTab = savedState.getString("apps_customize_currentTab");
872 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700873 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700874 mAppsCustomizeContent.setContentType(
875 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
876 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700877 mAppsCustomizeContent.loadAssociatedPages(
878 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700879 }
880
Winson Chung5afbf7b2011-07-25 11:53:08 -0700881 int currentIndex = savedState.getInt("apps_customize_currentIndex");
882 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700883 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800884 }
885
886 /**
887 * Finds all the views we need and configure them properly.
888 */
889 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700890 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400891
Winson Chung4c98d922011-05-31 16:50:48 -0700892 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
893 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800894 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
895 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896
Winson Chung4c98d922011-05-31 16:50:48 -0700897 // Setup the drag layer
898 mDragLayer.setup(this, dragController);
899
Winson Chung3d503fb2011-07-13 17:25:49 -0700900 // Setup the hotseat
901 mHotseat = (Hotseat) findViewById(R.id.hotseat);
902 if (mHotseat != null) {
903 mHotseat.setup(this);
904 }
905
Winson Chung4c98d922011-05-31 16:50:48 -0700906 // Setup the workspace
907 mWorkspace.setHapticFeedbackEnabled(false);
908 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700909 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700910 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700911
Winson Chungf0ea4d32011-06-06 14:27:16 -0700912 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700913 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700914
Winson Chungf0ea4d32011-06-06 14:27:16 -0700915 // Setup AppsCustomize
916 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
917 findViewById(R.id.apps_customize_pane);
918 mAppsCustomizeContent = (AppsCustomizePagedView)
919 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
920 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400921
Michael Jurka5130e402011-10-13 04:55:35 -0700922 // Get the all apps button
923 mAllAppsButton = findViewById(R.id.all_apps_button);
924 if (mAllAppsButton != null) {
925 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
926 @Override
927 public boolean onTouch(View v, MotionEvent event) {
928 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
929 onTouchDownAllAppsButton(v);
930 }
931 return false;
932 }
933 });
934 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700935 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700936 dragController.setDragScoller(mWorkspace);
937 dragController.setScrollView(mDragLayer);
938 dragController.setMoveTarget(mWorkspace);
939 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700940 if (mSearchDropTargetBar != null) {
941 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800942 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 }
944
945 /**
946 * Creates a view representing a shortcut.
947 *
948 * @param info The data structure describing the shortcut.
949 *
950 * @return A View inflated from R.layout.application.
951 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800952 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800953 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700954 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 }
956
957 /**
958 * Creates a view representing a shortcut inflated from the specified resource.
959 *
960 * @param layoutResId The id of the XML layout used to create the shortcut.
961 * @param parent The group the shortcut belongs to.
962 * @param info The data structure describing the shortcut.
963 *
964 * @return A View inflated from layoutResId.
965 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800966 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800967 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
968 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800969 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 return favorite;
971 }
972
973 /**
974 * Add an application shortcut to the workspace.
975 *
976 * @param data The intent describing the application.
977 * @param cellInfo The position on screen where to create the shortcut.
978 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700979 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700980 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700981 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700982
Adam Cohenfbba09b2011-07-18 21:43:05 -0700983 // First we check if we already know the exact location where we want to add this item.
984 if (cellX >= 0 && cellY >= 0) {
985 cellXY[0] = cellX;
986 cellXY[1] = cellY;
987 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700988 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700989 return;
990 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800991
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800992 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800993
Romain Guy73b979d2009-06-09 12:57:21 -0700994 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -0800995 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800997 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -0700998 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -0700999 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001000 } else {
1001 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001002 }
1003 }
Romain Guycbb89e42009-06-08 15:52:54 -07001004
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 /**
1006 * Add a shortcut to the workspace.
1007 *
1008 * @param data The intent describing the shortcut.
1009 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001011 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1012 int cellY) {
1013 int[] cellXY = mTmpAddItemCellCoordinates;
1014 int[] touchXY = mPendingAddInfo.dropPos;
1015 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001016
Michael Jurkad3ef3062010-11-23 16:23:58 -08001017 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001018
Adam Cohen558baaf2011-08-15 15:22:57 -07001019 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001020 if (info == null) {
1021 return;
1022 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001023 final View view = createShortcut(info);
1024
Adam Cohenfbba09b2011-07-18 21:43:05 -07001025 // First we check if we already know the exact location where we want to add this item.
1026 if (cellX >= 0 && cellY >= 0) {
1027 cellXY[0] = cellX;
1028 cellXY[1] = cellY;
1029 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001030
1031 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001032 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001033 true, null,null)) {
1034 return;
1035 }
1036 DragObject dragObject = new DragObject();
1037 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001038 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1039 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001040 return;
1041 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001042 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001043 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001044 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001045 foundCellSpan = (result != null);
1046 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001047 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001048 }
1049
1050 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001051 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001052 return;
1053 }
1054
Adam Cohen558baaf2011-08-15 15:22:57 -07001055 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001056
1057 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001058 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1059 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 }
1061 }
1062
Adam Cohen2f093b62012-04-30 18:59:53 -07001063 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1064 int minHeight) {
1065 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001066 // We want to account for the extra amount of padding that we are adding to the widget
1067 // to ensure that it gets the full amount of space that it has requested
1068 int requiredWidth = minWidth + padding.left + padding.right;
1069 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001070 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001071 }
1072
Adam Cohen2f093b62012-04-30 18:59:53 -07001073 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1074 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001075 }
1076
Adam Cohen2f093b62012-04-30 18:59:53 -07001077 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1078 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001079 }
1080
Adam Cohen2f093b62012-04-30 18:59:53 -07001081 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1082 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001083 }
1084
Adam Cohen2f093b62012-04-30 18:59:53 -07001085 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1086 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001087 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001088 }
1089
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001090 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001091 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001092 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001093 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001094 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001096 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1097 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1098 if (appWidgetInfo == null) {
1099 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1100 }
Romain Guycbb89e42009-06-08 15:52:54 -07001101
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001102 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001103 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001104
Adam Cohen2f093b62012-04-30 18:59:53 -07001105 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1106 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001107
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001108 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001109 // We have saved the position to which the widget was dragged-- this really only matters
1110 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001111 int[] cellXY = mTmpAddItemCellCoordinates;
1112 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001113 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001114 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001115 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1116 cellXY[0] = mPendingAddInfo.cellX;
1117 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001118 spanXY[0] = mPendingAddInfo.spanX;
1119 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001120 foundCellSpan = true;
1121 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001122 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001123 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001124 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1125 spanXY[1], cellXY, finalSpan);
1126 spanXY[0] = finalSpan[0];
1127 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001128 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001129 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001130 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001131 }
1132
Michael Jurka0280c3b2010-09-17 15:00:07 -07001133 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001134 if (appWidgetId != -1) {
1135 // Deleting an app widget ID is a void call but writes to disk before returning
1136 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001137 new Thread("deleteAppWidgetId") {
1138 public void run() {
1139 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1140 }
1141 }.start();
1142 }
Winson Chung93eef082012-03-23 15:59:27 -07001143 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001144 return;
1145 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001146
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001147 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001148 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1149 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001150 launcherInfo.spanX = spanXY[0];
1151 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001152 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1153 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001154
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001155 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001156 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001157
1158 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001159 if (hostView == null) {
1160 // Perform actual inflation because we're live
1161 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1162 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1163 } else {
1164 // The AppWidgetHostView has already been inflated and instantiated
1165 launcherInfo.hostView = hostView;
1166 }
Romain Guycbb89e42009-06-08 15:52:54 -07001167
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001168 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001169 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung211bac32012-05-15 13:43:57 -07001170 launcherInfo.notifyWidgetSizeChanged(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001171 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001172 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001173
1174 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001175 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001176 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001177 }
Romain Guycbb89e42009-06-08 15:52:54 -07001178
Adam Cohended9f8d2010-11-03 13:25:16 -07001179 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1180 @Override
1181 public void onReceive(Context context, Intent intent) {
1182 final String action = intent.getAction();
1183 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1184 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001185 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001186 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001187
Winson Chungc100e8e2011-08-09 16:02:43 -07001188 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001189 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001190 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1191 mAppsCustomizeTabHost.reset();
1192 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001193 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001194 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1195 mUserPresent = true;
1196 updateRunning();
1197 }
1198 }
1199 };
1200
1201 @Override
1202 public void onAttachedToWindow() {
1203 super.onAttachedToWindow();
1204
1205 // Listen for broadcasts related to user-presence
1206 final IntentFilter filter = new IntentFilter();
1207 filter.addAction(Intent.ACTION_SCREEN_OFF);
1208 filter.addAction(Intent.ACTION_USER_PRESENT);
1209 registerReceiver(mReceiver, filter);
1210
Adam Cohend113e0c2010-11-11 10:48:05 -08001211 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001212 mVisible = true;
1213 }
1214
1215 @Override
1216 public void onDetachedFromWindow() {
1217 super.onDetachedFromWindow();
1218 mVisible = false;
1219
Adam Cohend113e0c2010-11-11 10:48:05 -08001220 if (mAttached) {
1221 unregisterReceiver(mReceiver);
1222 mAttached = false;
1223 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001224 updateRunning();
1225 }
1226
1227 public void onWindowVisibilityChanged(int visibility) {
1228 mVisible = visibility == View.VISIBLE;
1229 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001230 // The following code used to be in onResume, but it turns out onResume is called when
1231 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1232 // is a more appropriate event to handle
1233 if (mVisible) {
1234 mAppsCustomizeTabHost.onWindowVisible();
1235 if (!mWorkspaceLoading) {
1236 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001237 // We want to let Launcher draw itself at least once before we force it to build
1238 // layers on all the workspace pages, so that transitioning to Launcher from other
1239 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1240 // a true draw so we wait until the second preDraw call to be safe
1241 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001242 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001243 // We delay the layer building a bit in order to give
1244 // other message processing a time to run. In particular
1245 // this avoids a delay in hiding the IME if it was
1246 // currently shown, because doing that may involve
1247 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001248 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001249
Michael Jurka6ee21d22012-02-21 18:20:27 -08001250 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001251 return true;
1252 }
1253 });
1254 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001255 // When Launcher comes back to foreground, a different Activity might be responsible for
1256 // the app market intent, so refresh the icon
1257 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001258 clearTypedText();
1259 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001260 }
1261
1262 private void sendAdvanceMessage(long delay) {
1263 mHandler.removeMessages(ADVANCE_MSG);
1264 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1265 mHandler.sendMessageDelayed(msg, delay);
1266 mAutoAdvanceSentTime = System.currentTimeMillis();
1267 }
1268
1269 private void updateRunning() {
1270 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1271 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1272 mAutoAdvanceRunning = autoAdvanceRunning;
1273 if (autoAdvanceRunning) {
1274 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1275 sendAdvanceMessage(delay);
1276 } else {
1277 if (!mWidgetsToAdvance.isEmpty()) {
1278 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1279 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1280 }
1281 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001282 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001283 }
1284 }
1285 }
1286
1287 private final Handler mHandler = new Handler() {
1288 @Override
1289 public void handleMessage(Message msg) {
1290 if (msg.what == ADVANCE_MSG) {
1291 int i = 0;
1292 for (View key: mWidgetsToAdvance.keySet()) {
1293 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1294 final int delay = mAdvanceStagger * i;
1295 if (v instanceof Advanceable) {
1296 postDelayed(new Runnable() {
1297 public void run() {
1298 ((Advanceable) v).advance();
1299 }
1300 }, delay);
1301 }
1302 i++;
1303 }
1304 sendAdvanceMessage(mAdvanceInterval);
1305 }
1306 }
1307 };
1308
1309 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001310 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001311 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1312 if (v instanceof Advanceable) {
1313 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001314 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001315 updateRunning();
1316 }
1317 }
1318
1319 void removeWidgetToAutoAdvance(View hostView) {
1320 if (mWidgetsToAdvance.containsKey(hostView)) {
1321 mWidgetsToAdvance.remove(hostView);
1322 updateRunning();
1323 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001324 }
1325
Joe Onorato9c1289c2009-08-17 11:03:03 -04001326 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001327 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001328 launcherInfo.hostView = null;
1329 }
1330
Winson Chung93eef082012-03-23 15:59:27 -07001331 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1332 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1333 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001334 }
1335
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001336 public LauncherAppWidgetHost getAppWidgetHost() {
1337 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 }
Romain Guycbb89e42009-06-08 15:52:54 -07001339
Winson Chunga9abd0e2010-10-27 17:18:37 -07001340 public LauncherModel getModel() {
1341 return mModel;
1342 }
1343
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001344 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001345 getWindow().closeAllPanels();
1346
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001347 // Whatever we were doing is hereby canceled.
1348 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001349 }
1350
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 @Override
1352 protected void onNewIntent(Intent intent) {
1353 super.onNewIntent(intent);
1354
1355 // Close the menu
1356 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001357 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001358 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001359
Joe Onorato14f122b2009-11-19 14:06:36 -08001360 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1361 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001362
Adam Cohenac56cff2011-09-28 20:45:37 -07001363 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001364 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001365 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001366 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1367 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001368 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001369 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001370
1371 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001372 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001373 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001374
Joe Onorato3a8820b2009-11-10 15:06:42 -08001375 final View v = getWindow().peekDecorView();
1376 if (v != null && v.getWindowToken() != null) {
1377 InputMethodManager imm = (InputMethodManager)getSystemService(
1378 INPUT_METHOD_SERVICE);
1379 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001381
Michael Jurka35aa14d2011-07-07 17:01:08 -07001382 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001383 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001384 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001385 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001386 }
1387 }
1388
1389 @Override
1390 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1391 // Do not call super here
1392 mSavedInstanceState = savedInstanceState;
1393 }
1394
1395 @Override
1396 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001397 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001398 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399
Michael Jurka883f55b2010-10-21 15:47:14 -07001400 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001401 // We close any open folder since it will not be re-opened, and we need to make sure
1402 // this state is reflected.
1403 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404
Winson Chung3d503fb2011-07-13 17:25:49 -07001405 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1406 mWaitingForResult) {
1407 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1408 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1409 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1410 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001411 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1412 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1413 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001414 }
1415
1416 if (mFolderInfo != null && mWaitingForResult) {
1417 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1418 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1419 }
Michael Jurka946ad472010-07-09 18:05:18 -07001420
Winson Chung785d2eb2011-04-14 16:08:02 -07001421 // Save the current AppsCustomize tab
1422 if (mAppsCustomizeTabHost != null) {
1423 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1424 if (currentTabTag != null) {
1425 outState.putString("apps_customize_currentTab", currentTabTag);
1426 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001427 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1428 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001429 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 }
1431
1432 @Override
1433 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001435
Winson Chunge7a03942011-08-05 15:05:12 -07001436 // Remove all pending runnables
1437 mHandler.removeMessages(ADVANCE_MSG);
1438 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001439 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001440
Winson Chungcd2b0142011-06-08 16:02:26 -07001441 // Stop callbacks from LauncherModel
1442 LauncherApplication app = ((LauncherApplication) getApplication());
1443 mModel.stopLoader();
1444 app.setLauncher(null);
1445
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001446 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001447 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001448 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001449 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001450 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001451 mAppWidgetHost = null;
1452
1453 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454
1455 TextKeyListener.getInstance().release();
1456
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457
Winson Chung3d503fb2011-07-13 17:25:49 -07001458 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001459
1460 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001461 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001462
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001463 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001464 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1465 mWorkspace.removeAllViews();
1466 mWorkspace = null;
1467 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001468
Michael Jurka2ecf9952012-06-18 12:52:28 -07001469 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 }
1471
Adam Cohena9cf38f2011-05-02 15:36:58 -07001472 public DragController getDragController() {
1473 return mDragController;
1474 }
1475
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 @Override
1477 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001478 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 super.startActivityForResult(intent, requestCode);
1480 }
1481
Winson Chung70d72102011-08-12 11:24:35 -07001482 /**
1483 * Indicates that we want global search for this activity by setting the globalSearch
1484 * argument for {@link #startSearch} to true.
1485 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001487 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001489
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001490 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001491
Karl Rosaen138a0412009-04-23 19:00:21 -07001492 if (initialQuery == null) {
1493 // Use any text typed in the launcher as the initial query
1494 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001495 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001496 if (appSearchData == null) {
1497 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001498 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001500 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001501
Michael Jurkaa33411c2012-06-14 16:18:21 -07001502 startGlobalSearch(initialQuery, selectInitialQuery,
1503 appSearchData, sourceBounds);
1504 }
1505
1506 /**
1507 * Starts the global search activity. This code is a copied from SearchManager
1508 */
1509 public void startGlobalSearch(String initialQuery,
1510 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001511 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001512 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1513 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1514 if (globalSearchActivity == null) {
1515 Log.w(TAG, "No global search activity found.");
1516 return;
1517 }
1518 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1519 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1520 intent.setComponent(globalSearchActivity);
1521 // Make sure that we have a Bundle to put source in
1522 if (appSearchData == null) {
1523 appSearchData = new Bundle();
1524 } else {
1525 appSearchData = new Bundle(appSearchData);
1526 }
1527 // Set source to package name of app that starts global search, if not set already.
1528 if (!appSearchData.containsKey("source")) {
1529 appSearchData.putString("source", getPackageName());
1530 }
1531 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1532 if (!TextUtils.isEmpty(initialQuery)) {
1533 intent.putExtra(SearchManager.QUERY, initialQuery);
1534 }
1535 if (selectInitialQuery) {
1536 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1537 }
1538 intent.setSourceBounds(sourceBounds);
1539 try {
1540 startActivity(intent);
1541 } catch (ActivityNotFoundException ex) {
1542 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1543 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 }
1545
Winson Chung70d72102011-08-12 11:24:35 -07001546 @Override
1547 public boolean onCreateOptionsMenu(Menu menu) {
1548 if (isWorkspaceLocked()) {
1549 return false;
1550 }
1551
1552 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001553
1554 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1555 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1556 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001557 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1558 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1559 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001560 String helpUrl = getString(R.string.help_url);
1561 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001562 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1563 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1564
Winson Chung70d72102011-08-12 11:24:35 -07001565 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1566 .setIcon(android.R.drawable.ic_menu_gallery)
1567 .setAlphabeticShortcut('W');
1568 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1569 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001570 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001571 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001572 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1573 .setIcon(android.R.drawable.ic_menu_preferences)
1574 .setIntent(settings)
1575 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001576 if (!helpUrl.isEmpty()) {
1577 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1578 .setIcon(android.R.drawable.ic_menu_help)
1579 .setIntent(help)
1580 .setAlphabeticShortcut('H');
1581 }
Winson Chung70d72102011-08-12 11:24:35 -07001582 return true;
1583 }
1584
1585 @Override
1586 public boolean onPrepareOptionsMenu(Menu menu) {
1587 super.onPrepareOptionsMenu(menu);
1588
1589 if (mAppsCustomizeTabHost.isTransitioning()) {
1590 return false;
1591 }
1592 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1593 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1594
1595 return true;
1596 }
1597
1598 @Override
1599 public boolean onOptionsItemSelected(MenuItem item) {
1600 switch (item.getItemId()) {
1601 case MENU_WALLPAPER_SETTINGS:
1602 startWallpaper();
1603 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001604 }
1605
1606 return super.onOptionsItemSelected(item);
1607 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001608
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001609 @Override
1610 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001611 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001612 // Use a custom animation for launching search
1613 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001614 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001615 }
1616
Joe Onorato9c1289c2009-08-17 11:03:03 -04001617 public boolean isWorkspaceLocked() {
1618 return mWorkspaceLoading || mWaitingForResult;
1619 }
1620
Michael Jurka0280c3b2010-09-17 15:00:07 -07001621 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001622 mPendingAddInfo.container = ItemInfo.NO_ID;
1623 mPendingAddInfo.screen = -1;
1624 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1625 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001626 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001627 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001628 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001629
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001630 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1631 AppWidgetProviderInfo appWidgetInfo) {
1632 if (appWidgetInfo.configure != null) {
1633 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001634
Bjorn Bringert7984c942009-12-09 15:38:25 +00001635 // Launch over to configure widget, if needed
1636 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001637 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001638 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001639 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001641 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001642 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1643 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001644 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001645 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 }
1647 }
Romain Guycbb89e42009-06-08 15:52:54 -07001648
Adam Cohenfbba09b2011-07-18 21:43:05 -07001649 /**
1650 * Process a shortcut drop.
1651 *
1652 * @param componentName The name of the component
1653 * @param screen The screen where it should be added
1654 * @param cell The cell it should be added to, optional
1655 * @param position The location on the screen where it was dropped, optional
1656 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001657 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1658 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001659 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001660 mPendingAddInfo.container = container;
1661 mPendingAddInfo.screen = screen;
1662 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001663
1664 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001665 mPendingAddInfo.cellX = cell[0];
1666 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001667 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001668
1669 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1670 createShortcutIntent.setComponent(componentName);
1671 processShortcut(createShortcutIntent);
1672 }
1673
Adam Cohenfbba09b2011-07-18 21:43:05 -07001674 /**
1675 * Process a widget drop.
1676 *
1677 * @param info The PendingAppWidgetInfo of the widget being added.
1678 * @param screen The screen where it should be added
1679 * @param cell The cell it should be added to, optional
1680 * @param position The location on the screen where it was dropped, optional
1681 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001682 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001683 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001684 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001685 mPendingAddInfo.container = info.container = container;
1686 mPendingAddInfo.screen = info.screen = screen;
1687 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001688 mPendingAddInfo.minSpanX = info.minSpanX;
1689 mPendingAddInfo.minSpanY = info.minSpanY;
1690
Adam Cohenfbba09b2011-07-18 21:43:05 -07001691 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001692 mPendingAddInfo.cellX = cell[0];
1693 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001694 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001695 if (span != null) {
1696 mPendingAddInfo.spanX = span[0];
1697 mPendingAddInfo.spanY = span[1];
1698 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001699
Adam Cohened66b2b2012-01-23 17:28:51 -08001700 AppWidgetHostView hostView = info.boundWidget;
1701 int appWidgetId;
1702 if (hostView != null) {
1703 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001704 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001705 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001706 // In this case, we either need to start an activity to get permission to bind
1707 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001708 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001709 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001710 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001711 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001712 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001713 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1714 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1715 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1716 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1717 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001718 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001719 }
1720
Joe Onoratodeb98af2010-02-19 14:59:39 -08001721 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001722 // Handle case where user selected "Applications"
1723 String applicationName = getResources().getString(R.string.group_applications);
1724 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001725
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001726 if (applicationName != null && applicationName.equals(shortcutName)) {
1727 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1728 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001729
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001730 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1731 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001732 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001733 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001734 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001735 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001736 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001737 }
1738
Winson Chung24ab2f12010-09-16 14:10:47 -07001739 void processWallpaper(Intent intent) {
1740 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1741 }
1742
Winson Chung3d503fb2011-07-13 17:25:49 -07001743 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1744 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001745 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001746 folderInfo.title = getText(R.string.folder_name);
1747
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001748 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001749 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1750 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001751 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752
1753 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001754 FolderIcon newFolder =
1755 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1756 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1757 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001758 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001759 }
Romain Guycbb89e42009-06-08 15:52:54 -07001760
Joe Onorato9c1289c2009-08-17 11:03:03 -04001761 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001762 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001763 }
1764
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001765 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001766 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001767 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001768 Intent chooser = Intent.createChooser(pickWallpaper,
1769 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001770 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1771 // Removed in Eclair MR1
1772// WallpaperManager wm = (WallpaperManager)
1773// getSystemService(Context.WALLPAPER_SERVICE);
1774// WallpaperInfo wi = wm.getWallpaperInfo();
1775// if (wi != null && wi.getSettingsActivity() != null) {
1776// LabeledIntent li = new LabeledIntent(getPackageName(),
1777// R.string.configure_wallpaper, 0);
1778// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1779// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1780// }
Mike Clerona0618e42009-10-22 13:55:21 -07001781 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001782 }
1783
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001784 /**
1785 * Registers various content observers. The current implementation registers
1786 * only a favorites observer to keep track of the favorites applications.
1787 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001788 private void registerContentObservers() {
1789 ContentResolver resolver = getContentResolver();
1790 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1791 true, mWidgetObserver);
1792 }
1793
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001794 @Override
1795 public boolean dispatchKeyEvent(KeyEvent event) {
1796 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1797 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001799 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001800 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001801 if (doesFileExist(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001802 dumpState();
1803 return true;
1804 }
1805 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001806 }
1807 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1808 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001809 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001810 return true;
1811 }
1812 }
1813
1814 return super.dispatchKeyEvent(event);
1815 }
1816
Joe Onorato88ec0992009-11-19 13:16:06 -08001817 @Override
1818 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001819 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001820 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001821 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001822 Folder openFolder = mWorkspace.getOpenFolder();
1823 if (openFolder.isEditingName()) {
1824 openFolder.dismissEditingName();
1825 } else {
1826 closeFolder();
1827 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001828 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001829 mWorkspace.exitWidgetResizeMode();
1830
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001831 // Back button is a no-op here, but give at least some feedback for the button press
1832 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001833 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001834 }
1835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001837 * Re-listen when widgets are reset.
1838 */
1839 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001840 if (mAppWidgetHost != null) {
1841 mAppWidgetHost.startListening();
1842 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001843 }
1844
1845 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001846 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1847 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001849 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001850 if (mModel != null) {
1851 mModel.unbindWorkspaceItems();
1852 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001854
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 /**
1856 * Launches the intent referred by the clicked shortcut.
1857 *
1858 * @param v The view representing the clicked shortcut.
1859 */
1860 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001861 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1862 // view has detached (it's possible for this to happen if the view is removed mid touch).
1863 if (v.getWindowToken() == null) {
1864 return;
1865 }
1866
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001867 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001868 return;
1869 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001870
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001872 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001873 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001874 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001875 int[] pos = new int[2];
1876 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001877 intent.setSourceBounds(new Rect(pos[0], pos[1],
1878 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001879
1880 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001881
1882 if (success && v instanceof BubbleTextView) {
1883 mWaitingForResume = (BubbleTextView) v;
1884 mWaitingForResume.setStayPressed(true);
1885 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001886 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001887 if (v instanceof FolderIcon) {
1888 FolderIcon fi = (FolderIcon) v;
1889 handleFolderClick(fi);
1890 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001891 } else if (v == mAllAppsButton) {
1892 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001893 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001894 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001895 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001896 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 }
1898 }
1899
Michael Jurka0e260592010-06-30 17:07:39 -07001900 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001901 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001902 // clicking anywhere on the workspace causes the customization drawer to slide down
1903 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001904 return false;
1905 }
1906
Michael Jurkaaf442092010-06-10 17:01:57 -07001907 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001908 * Event handler for the search button
1909 *
1910 * @param v The view that was clicked.
1911 */
1912 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001913 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1914
Amith Yamasania135ba82011-08-09 17:42:01 -07001915 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001916 }
1917
1918 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001919 * Event handler for the voice button
1920 *
1921 * @param v The view that was clicked.
1922 */
1923 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001924 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001925
Michael Jurkaae65ee32012-04-30 11:45:54 -07001926 try {
1927 final SearchManager searchManager =
1928 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1929 ComponentName activityName = searchManager.getGlobalSearchActivity();
1930 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001931 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001932 if (activityName != null) {
1933 intent.setPackage(activityName.getPackageName());
1934 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001935 startActivity(null, intent, "onClickVoiceButton");
Winson Chung01b0b632012-05-22 10:18:14 -07001936 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001937 } catch (ActivityNotFoundException e) {
1938 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001939 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001940 startActivitySafely(null, intent, "onClickVoiceButton");
1941 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001942 }
1943
1944 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001945 * Event handler for the "grid" button that appears on the home screen, which
1946 * enters all apps mode.
1947 *
1948 * @param v The view that was clicked.
1949 */
1950 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001951 showAllApps(true);
1952 }
1953
1954 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001955 // Provide the same haptic feedback that the system offers for virtual keys.
1956 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001957 }
1958
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001959 public void onClickAppMarketButton(View v) {
1960 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001961 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001962 } else {
1963 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001964 }
1965 }
1966
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001967 void startApplicationDetailsActivity(ComponentName componentName) {
1968 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001969 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1970 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001971 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001972 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001973 }
1974
Patrick Dubroy5539af72010-09-07 15:22:01 -07001975 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1976 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1977 // System applications cannot be installed. For now, show a toast explaining that.
1978 // We may give them the option of disabling apps this way.
1979 int messageId = R.string.uninstall_system_app_text;
1980 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1981 } else {
1982 String packageName = appInfo.componentName.getPackageName();
1983 String className = appInfo.componentName.getClassName();
1984 Intent intent = new Intent(
1985 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001986 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1987 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001988 startActivity(intent);
1989 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001990 }
1991
Michael Jurka86a720e2012-05-09 11:23:23 -07001992 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001994
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 try {
Winson Chung2672ff92012-05-04 16:22:30 -07001996 // Only launch using the new animation if the shortcut has not opted out (this is a
1997 // private contract between launcher and may be ignored in the future).
1998 boolean useLaunchAnimation = (v != null) &&
1999 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2000 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002001 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2002 v.getMeasuredWidth(), v.getMeasuredHeight());
2003
2004 startActivity(intent, opts.toBundle());
2005 } else {
2006 startActivity(intent);
2007 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002008 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002009 } catch (SecurityException e) {
2010 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002011 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002012 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002013 "or use the exported attribute for this activity. "
2014 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002015 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002016 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002018
Michael Jurka86a720e2012-05-09 11:23:23 -07002019 boolean startActivitySafely(View v, Intent intent, Object tag) {
2020 boolean success = false;
2021 try {
2022 success = startActivity(v, intent, tag);
2023 } catch (ActivityNotFoundException e) {
2024 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2025 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2026 }
2027 return success;
2028 }
2029
Romain Guy8e633c52010-03-04 12:51:36 -08002030 void startActivityForResultSafely(Intent intent, int requestCode) {
2031 try {
2032 startActivityForResult(intent, requestCode);
2033 } catch (ActivityNotFoundException e) {
2034 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2035 } catch (SecurityException e) {
2036 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2037 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2038 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2039 "or use the exported attribute for this activity.", e);
2040 }
2041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042
Adam Cohena9cf38f2011-05-02 15:36:58 -07002043 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002044 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002045 Folder openFolder = mWorkspace.getFolderForTag(info);
2046
2047 // If the folder info reports that the associated folder is open, then verify that
2048 // it is actually opened. There have been a few instances where this gets out of sync.
2049 if (info.opened && openFolder == null) {
2050 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2051 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2052 info.opened = false;
2053 }
2054
Adam Cohenfb91f302012-06-11 15:45:18 -07002055 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 // Close any open folder
2057 closeFolder();
2058 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002059 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 } else {
2061 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002062 int folderScreen;
2063 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002064 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 // .. and close it
2066 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002067 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068 // Close any folder open on the current screen
2069 closeFolder();
2070 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002071 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 }
2073 }
2074 }
2075 }
2076
Adam Cohen268c4752012-06-06 17:47:33 -07002077 /**
2078 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2079 * in the DragLayer in the exact absolute location of the original FolderIcon.
2080 */
2081 private void copyFolderIconToImage(FolderIcon fi) {
2082 final int width = fi.getMeasuredWidth();
2083 final int height = fi.getMeasuredHeight();
2084
2085 // Lazy load ImageView, Bitmap and Canvas
2086 if (mFolderIconImageView == null) {
2087 mFolderIconImageView = new ImageView(this);
2088 }
2089 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2090 mFolderIconBitmap.getHeight() != height) {
2091 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2092 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2093 }
2094
2095 DragLayer.LayoutParams lp;
2096 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2097 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2098 } else {
2099 lp = new DragLayer.LayoutParams(width, height);
2100 }
2101
2102 mDragLayer.getViewRectRelativeToSelf(fi, mRectForFolderAnimation);
2103 lp.customPosition = true;
2104 lp.x = mRectForFolderAnimation.left;
2105 lp.y = mRectForFolderAnimation.top;
Adam Cohen8ec23032012-06-08 14:46:22 -07002106 lp.width = width;
2107 lp.height = height;
Adam Cohen268c4752012-06-06 17:47:33 -07002108
2109 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2110 fi.draw(mFolderIconCanvas);
2111 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002112 if (fi.getFolder() != null) {
2113 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2114 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002115 }
Adam Cohen268c4752012-06-06 17:47:33 -07002116 // Just in case this image view is still in the drag layer from a previous animation,
2117 // we remove it and re-add it.
2118 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2119 mDragLayer.removeView(mFolderIconImageView);
2120 }
2121 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002122 if (fi.getFolder() != null) {
2123 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002124 }
Adam Cohen268c4752012-06-06 17:47:33 -07002125 }
2126
Adam Cohen2801caf2011-05-13 20:57:39 -07002127 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002128 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002129 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2130 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2131 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2132
Adam Cohenc51934b2011-07-26 21:07:43 -07002133 FolderInfo info = (FolderInfo) fi.getTag();
2134 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2135 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002136 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2137 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002138 }
2139
Adam Cohen268c4752012-06-06 17:47:33 -07002140 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2141 copyFolderIconToImage(fi);
2142 fi.setVisibility(View.INVISIBLE);
2143
Michael Jurka2ecf9952012-06-18 12:52:28 -07002144 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002145 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002146 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2147 oa.start();
2148 }
2149
Adam Cohen268c4752012-06-06 17:47:33 -07002150 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002151 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002152 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2153 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2154 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2155
Adam Cohen268c4752012-06-06 17:47:33 -07002156 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002157
Adam Cohen268c4752012-06-06 17:47:33 -07002158 // We remove and re-draw the FolderIcon in-case it has changed
2159 mDragLayer.removeView(mFolderIconImageView);
2160 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002161 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002162 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002163 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002164 oa.addListener(new AnimatorListenerAdapter() {
2165 @Override
2166 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002167 if (cl != null) {
2168 cl.clearFolderLeaveBehind();
2169 // Remove the ImageView copy of the FolderIcon and make the original visible.
2170 mDragLayer.removeView(mFolderIconImageView);
2171 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002172 }
2173 }
2174 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002175 oa.start();
2176 }
2177
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002178 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002179 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002180 * is animated relative to the specified View. If the View is null, no animation
2181 * is played.
2182 *
2183 * @param folderInfo The FolderInfo describing the folder to open.
2184 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002185 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002186 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002187 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188
Adam Cohena9cf38f2011-05-02 15:36:58 -07002189 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190
Adam Cohen4554ee12011-08-03 16:13:21 -07002191 // Just verify that the folder hasn't already been added to the DragLayer.
2192 // There was a one-off crash where the folder had a parent already.
2193 if (folder.getParent() == null) {
2194 mDragLayer.addView(folder);
2195 mDragController.addDropTarget((DropTarget) folder);
2196 } else {
2197 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2198 folder.getParent() + ").");
2199 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002200 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002201 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002202 }
2203
2204 public void closeFolder() {
2205 Folder folder = mWorkspace.getOpenFolder();
2206 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002207 if (folder.isEditingName()) {
2208 folder.dismissEditingName();
2209 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002210 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002211
2212 // Dismiss the folder cling
2213 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002214 }
2215 }
2216
2217 void closeFolder(Folder folder) {
2218 folder.getInfo().opened = false;
2219
2220 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2221 if (parent != null) {
2222 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2223 shrinkAndFadeInFolderIcon(fi);
2224 }
2225 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002226 }
2227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002228 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002229 if (!isDraggingEnabled()) return false;
2230 if (isWorkspaceLocked()) return false;
2231 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002232
2233 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002234 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235 }
2236
Michael Jurka0280c3b2010-09-17 15:00:07 -07002237 resetAddInfo();
2238 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002239 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002240 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002241 return true;
2242 }
2243
Winson Chung3d503fb2011-07-13 17:25:49 -07002244 // The hotseat touch handling does not go through Workspace, and we always allow long press
2245 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002246 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002247 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2248 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002249 if (itemUnderLongClick == null) {
2250 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002251 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2252 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002253 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002254 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002255 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002256 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002257 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002258 }
2259 }
2260 }
2261 return true;
2262 }
2263
Winson Chung3d503fb2011-07-13 17:25:49 -07002264 boolean isHotseatLayout(View layout) {
2265 return mHotseat != null && layout != null &&
2266 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2267 }
2268 Hotseat getHotseat() {
2269 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002270 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002271 SearchDropTargetBar getSearchBar() {
2272 return mSearchDropTargetBar;
2273 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002274
Winson Chung3d503fb2011-07-13 17:25:49 -07002275 /**
2276 * Returns the CellLayout of the specified container at the specified screen.
2277 */
2278 CellLayout getCellLayout(long container, int screen) {
2279 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2280 if (mHotseat != null) {
2281 return mHotseat.getLayout();
2282 } else {
2283 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002284 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002285 } else {
2286 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002287 }
2288 }
2289
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002290 Workspace getWorkspace() {
2291 return mWorkspace;
2292 }
2293
Daniel Sandler843e8602010-06-07 14:59:01 -04002294 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2295 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002296 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002297 }
2298
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002299 public boolean isAllAppsButtonRank(int rank) {
2300 return mHotseat.isAllAppsButtonRank(rank);
2301 }
2302
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002303 // AllAppsView.Watcher
2304 public void zoomed(float zoom) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002305 if (zoom == 1.0f) {
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002306 mWorkspace.setVisibility(View.GONE);
2307 }
2308 }
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002309
2310 /**
2311 * Helper method for the cameraZoomIn/cameraZoomOut animations
2312 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002313 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002314 * @param scaleFactor The scale factor used for the zoom
2315 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002316 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002317 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002318 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002319 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002320
Winson Chung18f41f82012-05-09 13:28:10 -07002321 void disableWallpaperIfInAllApps() {
2322 // Only disable it if we are in all apps
2323 if (mState == State.APPS_CUSTOMIZE) {
2324 if (mAppsCustomizeTabHost != null &&
2325 !mAppsCustomizeTabHost.isTransitioning()) {
2326 updateWallpaperVisibility(false);
2327 }
2328 }
2329 }
2330
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002331 void updateWallpaperVisibility(boolean visible) {
2332 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2333 int curflags = getWindow().getAttributes().flags
2334 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2335 if (wpflags != curflags) {
2336 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2337 }
2338 }
2339
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002340 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2341 if (v instanceof LauncherTransitionable) {
2342 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2343 }
2344 }
2345
Michael Jurkabed61d22012-02-14 22:51:29 -08002346 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2347 if (v instanceof LauncherTransitionable) {
2348 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2349 }
Winson Chung70442722012-02-10 15:43:22 -08002350
2351 // Update the workspace transition step as well
2352 dispatchOnLauncherTransitionStep(v, 0f);
2353 }
2354
2355 private void dispatchOnLauncherTransitionStep(View v, float t) {
2356 if (v instanceof LauncherTransitionable) {
2357 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2358 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002359 }
2360
2361 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2362 if (v instanceof LauncherTransitionable) {
2363 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2364 }
Winson Chung70442722012-02-10 15:43:22 -08002365
2366 // Update the workspace transition step as well
2367 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002368 }
2369
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002370 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002371 * Things to test when changing the following seven functions.
2372 * - Home from workspace
2373 * - from center screen
2374 * - from other screens
2375 * - Home from all apps
2376 * - from center screen
2377 * - from other screens
2378 * - Back from all apps
2379 * - from center screen
2380 * - from other screens
2381 * - Launch app from workspace and quit
2382 * - with back
2383 * - with home
2384 * - Launch app from all apps and quit
2385 * - with back
2386 * - with home
2387 * - Go to a screen that's not the default, then all
2388 * apps, and launch and app, and go back
2389 * - with back
2390 * -with home
2391 * - On workspace, long press power and go back
2392 * - with back
2393 * - with home
2394 * - On all apps, long press power and go back
2395 * - with back
2396 * - with home
2397 * - On workspace, power off
2398 * - On all apps, power off
2399 * - Launch an app and turn off the screen while in that app
2400 * - Go back with home key
2401 * - Go back with back key TODO: make this not go to workspace
2402 * - From all apps
2403 * - From workspace
2404 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2405 * - From all apps
2406 * - From the center workspace
2407 * - From another workspace
2408 */
2409
2410 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002411 * Zoom the camera out from the workspace to reveal 'toView'.
2412 * Assumes that the view to show is anchored at either the very top or very bottom
2413 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002414 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002415 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002416 if (mStateAnimation != null) {
2417 mStateAnimation.cancel();
2418 mStateAnimation = null;
2419 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002420 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002421
Winson Chungf0ea4d32011-06-06 14:27:16 -07002422 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2423 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2424 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002425 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002426 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002427 final int startDelay =
2428 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002429
Michael Jurkab3e22d92011-10-31 15:58:33 -07002430 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002431
Michael Jurkad74c9842011-07-10 12:44:21 -07002432 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002433 Animator workspaceAnim =
2434 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002435
2436 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002437 toView.setScaleX(scale);
2438 toView.setScaleY(scale);
2439 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2440 scaleAnim.
2441 scaleX(1f).scaleY(1f).
2442 setDuration(duration).
2443 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002444
Winson Chungf0ea4d32011-06-06 14:27:16 -07002445 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002446 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002447 final ObjectAnimator alphaAnim = ObjectAnimator
2448 .ofFloat(toView, "alpha", 0f, 1f)
2449 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002450 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002451 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2452 @Override
2453 public void onAnimationUpdate(ValueAnimator animation) {
2454 float t = (Float) animation.getAnimatedValue();
2455 dispatchOnLauncherTransitionStep(fromView, t);
2456 dispatchOnLauncherTransitionStep(toView, t);
2457 }
2458 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002459
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002460 // toView should appear right at the end of the workspace shrink
2461 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002462 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002463 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002464 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002465
2466 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002467 boolean animationCancelled = false;
2468
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002469 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002470 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002471 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002472 // Prepare the position
2473 toView.setTranslationX(0.0f);
2474 toView.setTranslationY(0.0f);
2475 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002476 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002477 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002478 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002479 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002480 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2481 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002482
2483 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2484 // Hide the workspace scrollbar
2485 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002486 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002487 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002488 if (!animationCancelled) {
2489 updateWallpaperVisibility(false);
2490 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002491
2492 // Hide the search bar
2493 mSearchDropTargetBar.hideSearchBar(false);
Adam Cohenf4ddea32011-09-12 13:46:42 -07002494 }
2495
Adam Cohencff6af82011-09-13 14:51:53 -07002496 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002497 public void onAnimationCancel(Animator animation) {
2498 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002499 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002500 });
2501
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002502 if (workspaceAnim != null) {
2503 mStateAnimation.play(workspaceAnim);
2504 }
Michael Jurka1899a362011-11-03 13:50:45 -07002505
2506 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002507 final ViewTreeObserver observer;
2508
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002509 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2510 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002511
2512 // If any of the objects being animated haven't been measured/laid out
2513 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002514 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002515 (mWorkspace.getMeasuredWidth() == 0) ||
2516 (toView.getMeasuredWidth() == 0)) {
2517 observer = mWorkspace.getViewTreeObserver();
2518 delayAnim = true;
2519 } else {
2520 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002521 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002522
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002523 final AnimatorSet stateAnimation = mStateAnimation;
2524 final Runnable startAnimRunnable = new Runnable() {
2525 public void run() {
2526 // Check that mStateAnimation hasn't changed while
2527 // we waited for a layout/draw pass
2528 if (mStateAnimation != stateAnimation)
2529 return;
2530 setPivotsForZoom(toView, scale);
2531 dispatchOnLauncherTransitionStart(fromView, animated, false);
2532 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka382aa182012-06-11 15:42:07 -07002533 toView.post(new Runnable() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002534 public void run() {
2535 // Check that mStateAnimation hasn't changed while
2536 // we waited for a layout/draw pass
2537 if (mStateAnimation != stateAnimation)
2538 return;
2539 mStateAnimation.start();
2540 }
2541 });
2542 }
2543 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002544 if (delayAnim) {
2545 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2546 public void onGlobalLayout() {
Michael Jurka382aa182012-06-11 15:42:07 -07002547 toView.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002548 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002549 }
2550 };
2551 observer.addOnGlobalLayoutListener(delayedStart);
2552 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002553 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002554 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002555 } else {
2556 toView.setTranslationX(0.0f);
2557 toView.setTranslationY(0.0f);
2558 toView.setScaleX(1.0f);
2559 toView.setScaleY(1.0f);
2560 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002561 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002562
Michael Jurkabed61d22012-02-14 22:51:29 -08002563 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2564 // Hide the workspace scrollbar
2565 mWorkspace.hideScrollingIndicator(true);
2566 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002567
2568 // Hide the search bar
2569 mSearchDropTargetBar.hideSearchBar(false);
Michael Jurkaabded662011-03-04 12:06:57 -08002570 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002571 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002572 dispatchOnLauncherTransitionStart(fromView, animated, false);
2573 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002574 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002575 dispatchOnLauncherTransitionStart(toView, animated, false);
2576 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002577 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002578 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002579 }
2580
2581 /**
2582 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002583 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002584 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002585 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002586 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2587 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002588
Michael Jurkab3e22d92011-10-31 15:58:33 -07002589 if (mStateAnimation != null) {
2590 mStateAnimation.cancel();
2591 mStateAnimation = null;
2592 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002593 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002594
Winson Chungf0ea4d32011-06-06 14:27:16 -07002595 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002596 final int fadeOutDuration =
2597 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002598 final float scaleFactor = (float)
2599 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2600 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002601 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002602 Animator workspaceAnim = null;
2603
2604 if (toState == State.WORKSPACE) {
2605 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2606 workspaceAnim = mWorkspace.getChangeStateAnimation(
2607 Workspace.State.NORMAL, animated, stagger);
2608 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2609 workspaceAnim = mWorkspace.getChangeStateAnimation(
2610 Workspace.State.SPRING_LOADED, animated);
2611 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002612
Michael Jurkab3e22d92011-10-31 15:58:33 -07002613 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002614 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002615 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002616 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002617 final LauncherViewPropertyAnimator scaleAnim =
2618 new LauncherViewPropertyAnimator(fromView);
2619 scaleAnim.
2620 scaleX(scaleFactor).scaleY(scaleFactor).
2621 setDuration(duration).
2622 setInterpolator(new Workspace.ZoomInInterpolator());
2623
2624 final ObjectAnimator alphaAnim = ObjectAnimator
2625 .ofFloat(fromView, "alpha", 1f, 0f)
2626 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002627 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002628 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2629 @Override
2630 public void onAnimationUpdate(ValueAnimator animation) {
2631 float t = 1f - (Float) animation.getAnimatedValue();
2632 dispatchOnLauncherTransitionStep(fromView, t);
2633 dispatchOnLauncherTransitionStep(toView, t);
2634 }
2635 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002636
Michael Jurka2ecf9952012-06-18 12:52:28 -07002637 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002638
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002639 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2640 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002641
2642 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002643 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002644 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002645 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002646 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002647 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2648 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002649 if (mWorkspace != null) {
2650 mWorkspace.hideScrollingIndicator(false);
2651 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002652 if (onCompleteRunnable != null) {
2653 onCompleteRunnable.run();
2654 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002655 }
2656 });
2657
Winson Chungf0ea4d32011-06-06 14:27:16 -07002658 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002659 if (workspaceAnim != null) {
2660 mStateAnimation.play(workspaceAnim);
2661 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002662 dispatchOnLauncherTransitionStart(fromView, animated, true);
2663 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002664 final Animator stateAnimation = mStateAnimation;
2665 mWorkspace.post(new Runnable() {
2666 public void run() {
2667 if (stateAnimation != mStateAnimation)
2668 return;
2669 mStateAnimation.start();
2670 }
2671 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002672 } else {
2673 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002674 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002675 dispatchOnLauncherTransitionStart(fromView, animated, true);
2676 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002677 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002678 dispatchOnLauncherTransitionStart(toView, animated, true);
2679 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002680 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002681 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002682 }
2683
Michael Jurkae326f182011-11-21 14:05:46 -08002684 @Override
2685 public void onTrimMemory(int level) {
2686 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002687 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002688 mAppsCustomizeTabHost.onTrimMemory();
2689 }
2690 }
2691
Winson Chung18f41f82012-05-09 13:28:10 -07002692 @Override
2693 public void onWindowFocusChanged(boolean hasFocus) {
2694 if (!hasFocus) {
2695 // When another window occludes launcher (like the notification shade, or recents),
2696 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2697 updateWallpaperVisibility(true);
2698 } else {
2699 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002700 mWorkspace.postDelayed(new Runnable() {
2701 @Override
2702 public void run() {
2703 disableWallpaperIfInAllApps();
2704 }
2705 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002706 }
2707 }
2708
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002709 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002710 showWorkspace(animated, null);
2711 }
2712
2713 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002714 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002715 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002716 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002717 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002718
Winson Chungc7d2b602012-05-16 17:02:20 -07002719 // Show the search bar (only animate if we were showing the drop target bar in spring
2720 // loaded mode)
2721 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2722
Michael Jurkab737ee62011-11-15 15:57:22 -08002723 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002724 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002725
2726 // Set focus to the AppsCustomize button
2727 if (mAllAppsButton != null) {
2728 mAllAppsButton.requestFocus();
2729 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002730 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002731
Michael Jurkab737ee62011-11-15 15:57:22 -08002732 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002733
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002734 // Change the state *after* we've called all the transition code
2735 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002736
Winson Chung5fb63472011-02-02 17:03:37 -08002737 // Resume the auto-advance of widgets
2738 mUserPresent = true;
2739 updateRunning();
2740
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002741 // send an accessibility event to announce the context change
2742 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002743 }
2744
Michael Jurkab3e22d92011-10-31 15:58:33 -07002745 void showAllApps(boolean animated) {
2746 if (mState != State.WORKSPACE) return;
2747
2748 showAppsCustomizeHelper(animated, false);
2749 mAppsCustomizeTabHost.requestFocus();
2750
Michael Jurkab3e22d92011-10-31 15:58:33 -07002751 // Change the state *after* we've called all the transition code
2752 mState = State.APPS_CUSTOMIZE;
2753
2754 // Pause the auto-advance of widgets until we are out of AllApps
2755 mUserPresent = false;
2756 updateRunning();
2757 closeFolder();
2758
2759 // Send an accessibility event to announce the context change
2760 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2761 }
2762
Adam Cohen7777d962011-08-18 18:58:38 -07002763 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002764 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002765 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002766 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002767 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002768 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002769 }
Adam Cohen7777d962011-08-18 18:58:38 -07002770
Adam Cohened66b2b2012-01-23 17:28:51 -08002771 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2772 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002773 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2774
Winson Chunge7a03942011-08-05 15:05:12 -07002775 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002776 @Override
2777 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002778 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002779 // Before we show workspace, hide all apps again because
2780 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2781 // clean up our state transition functions
2782 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002783 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002784 } else {
2785 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002786 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002787 }
2788 }, (extendedDelay ?
2789 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2790 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2791 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002792
Michael Jurkad3ef3062010-11-23 16:23:58 -08002793 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002794 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002795 final boolean animated = true;
2796 final boolean springLoaded = true;
2797 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002798 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002799 }
2800 // Otherwise, we are not in spring loaded mode, so don't do anything.
2801 }
2802
Michael Jurkab737ee62011-11-15 15:57:22 -08002803 void hideDockDivider() {
2804 if (mQsbDivider != null && mDockDivider != null) {
2805 mQsbDivider.setVisibility(View.INVISIBLE);
2806 mDockDivider.setVisibility(View.INVISIBLE);
2807 }
2808 }
2809
2810 void showDockDivider(boolean animated) {
2811 if (mQsbDivider != null && mDockDivider != null) {
2812 mQsbDivider.setVisibility(View.VISIBLE);
2813 mDockDivider.setVisibility(View.VISIBLE);
2814 if (mDividerAnimator != null) {
2815 mDividerAnimator.cancel();
2816 mQsbDivider.setAlpha(1f);
2817 mDockDivider.setAlpha(1f);
2818 mDividerAnimator = null;
2819 }
2820 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002821 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2822 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2823 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Michael Jurkab737ee62011-11-15 15:57:22 -08002824 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2825 mDividerAnimator.start();
2826 }
2827 }
2828 }
2829
Michael Jurkab3e22d92011-10-31 15:58:33 -07002830 void lockAllApps() {
2831 // TODO
2832 }
2833
2834 void unlockAllApps() {
2835 // TODO
2836 }
2837
Adam Cohenfc53cd22011-07-20 15:45:11 -07002838 public boolean isAllAppsCustomizeOpen() {
2839 return mState == State.APPS_CUSTOMIZE;
2840 }
2841
Winson Chungf0ea4d32011-06-06 14:27:16 -07002842 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002843 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002844 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002845 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002846 if (!LauncherApplication.isScreenLarge()) {
2847 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002848 if (mHotseat.getAlpha() != 1f) {
2849 int duration = mSearchDropTargetBar.getTransitionInDuration();
2850 mHotseat.animate().alpha(1f).setDuration(duration);
2851 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002852 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002853 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002854 }
2855 }
2856 }
2857
2858 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002859 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002860 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002861 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002862 if (!LauncherApplication.isScreenLarge()) {
2863 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002864 if (mHotseat.getAlpha() != 0f) {
2865 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2866 mHotseat.animate().alpha(0f).setDuration(duration);
2867 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002868 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002869 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002870 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002871 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002872 }
2873
Patrick Dubroy5f445422011-02-18 14:35:21 -08002874 /**
2875 * Add an item from all apps or customize onto the given workspace screen.
2876 * If layout is null, add to the current screen.
2877 */
2878 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002879 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002880 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002881 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002882 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002883
Winson Chungdff8ebb2011-09-08 17:25:31 -07002884 /** Maps the current orientation to an index for referencing orientation correct global icons */
2885 private int getCurrentOrientationIndexForGlobalIcons() {
2886 // default - 0, landscape - 1
2887 switch (getResources().getConfiguration().orientation) {
2888 case Configuration.ORIENTATION_LANDSCAPE:
2889 return 1;
2890 default:
2891 return 0;
2892 }
2893 }
2894
Michael Jurkaae65ee32012-04-30 11:45:54 -07002895 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002896 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002897 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002898 // Look for the toolbar icon specified in the activity meta-data
2899 Bundle metaData = packageManager.getActivityInfo(
2900 activityName, PackageManager.GET_META_DATA).metaData;
2901 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002902 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002903 if (iconResId != 0) {
2904 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002905 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002906 }
2907 }
2908 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002909 // This can happen if the activity defines an invalid drawable
2910 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2911 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002912 } catch (Resources.NotFoundException nfe) {
2913 // This can happen if the activity defines an invalid drawable
2914 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2915 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002916 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002917 return null;
2918 }
2919
2920 // if successful in getting icon, return it; otherwise, set button to use default drawable
2921 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002922 int buttonId, ComponentName activityName, int fallbackDrawableId,
2923 String toolbarResourceName) {
2924 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002925 Resources r = getResources();
2926 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2927 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002928
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002929 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002930 // If we were unable to find the icon via the meta-data, use a generic one
2931 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002932 toolbarIcon = r.getDrawable(fallbackDrawableId);
2933 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002934 if (button != null) {
2935 button.setCompoundDrawables(toolbarIcon, null, null, null);
2936 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002937 return null;
2938 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002939 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002940 if (button != null) {
2941 button.setCompoundDrawables(toolbarIcon, null, null, null);
2942 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002943 return toolbarIcon.getConstantState();
2944 }
2945 }
2946
2947 // if successful in getting icon, return it; otherwise, set button to use default drawable
2948 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002949 int buttonId, ComponentName activityName, int fallbackDrawableId,
2950 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002951 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002952 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002953
Michael Jurka19e0fc52011-07-22 18:00:21 -07002954 if (button != null) {
2955 // If we were unable to find the icon via the meta-data, use a
2956 // generic one
2957 if (toolbarIcon == null) {
2958 button.setImageResource(fallbackDrawableId);
2959 } else {
2960 button.setImageDrawable(toolbarIcon);
2961 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002962 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002963
2964 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2965
Michael Jurka0423dcf2010-10-05 14:56:18 -07002966 }
2967
Winson Chung1b884092012-06-08 17:13:02 -07002968 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002969 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07002970 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002971 }
2972
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002973 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002974 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002975 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002976 }
2977
Winson Chungbb185bd2011-11-21 12:31:42 -08002978 private void invalidatePressedFocusedStates(View container, View button) {
2979 if (container instanceof HolographicLinearLayout) {
2980 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2981 layout.invalidatePressedFocusedStates();
2982 } else if (button instanceof HolographicImageView) {
2983 HolographicImageView view = (HolographicImageView) button;
2984 view.invalidatePressedFocusedStates();
2985 }
2986 }
2987
Winson Chungc51db6a2011-10-05 11:44:49 -07002988 private boolean updateGlobalSearchIcon() {
2989 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002990 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2991 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002992 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002993 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002994 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002995
Winson Chung1cad91e2011-05-25 17:41:01 -07002996 final SearchManager searchManager =
2997 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2998 ComponentName activityName = searchManager.getGlobalSearchActivity();
2999 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003000 int coi = getCurrentOrientationIndexForGlobalIcons();
3001 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003002 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3003 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3004 if (sGlobalSearchIcon[coi] == null) {
3005 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3006 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3007 TOOLBAR_ICON_METADATA_NAME);
3008 }
3009
Winson Chung649723c2011-07-06 20:41:23 -07003010 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003011 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3012 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003013 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003014 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003015 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003016 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07003017 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003018 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3019 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3020 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003021 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003022 if (voiceButtonProxy != null) {
3023 voiceButtonProxy.setVisibility(View.GONE);
3024 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003025 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003026 }
3027 }
3028
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003029 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003030 final View searchButtonContainer = findViewById(R.id.search_button_container);
3031 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003032 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003033 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003034 }
3035
Winson Chungc51db6a2011-10-05 11:44:49 -07003036 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07003037 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07003038 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003039 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003040 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003041
Winson Chungc51db6a2011-10-05 11:44:49 -07003042 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003043 final SearchManager searchManager =
3044 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3045 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3046
3047 ComponentName activityName = null;
3048 if (globalSearchActivity != null) {
3049 // Check if the global search activity handles voice search
3050 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3051 intent.setPackage(globalSearchActivity.getPackageName());
3052 activityName = intent.resolveActivity(getPackageManager());
3053 }
3054
3055 if (activityName == null) {
3056 // Fallback: check if an activity other than the global search activity
3057 // resolves this
3058 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3059 activityName = intent.resolveActivity(getPackageManager());
3060 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003061 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003062 int coi = getCurrentOrientationIndexForGlobalIcons();
3063 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003064 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3065 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3066 if (sVoiceSearchIcon[coi] == null) {
3067 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3068 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3069 TOOLBAR_ICON_METADATA_NAME);
3070 }
Winson Chung649723c2011-07-06 20:41:23 -07003071 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003072 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003073 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003074 if (voiceButtonProxy != null) {
3075 voiceButtonProxy.setVisibility(View.VISIBLE);
3076 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003077 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003078 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003079 } else {
Winson Chung649723c2011-07-06 20:41:23 -07003080 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003081 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003082 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003083 if (voiceButtonProxy != null) {
3084 voiceButtonProxy.setVisibility(View.GONE);
3085 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003086 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003087 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003088 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003089
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003090 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003091 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3092 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003093 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003094 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003095 }
3096
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003097 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003098 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003099 */
3100 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003101 final View marketButton = findViewById(R.id.market_button);
3102 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3103 // Find the app market activity by resolving an intent.
3104 // (If multiple app markets are installed, it will return the ResolverActivity.)
3105 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003106 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003107 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003108 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003109 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003110 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3111 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003112 marketButton.setVisibility(View.VISIBLE);
3113 } else {
3114 // We should hide and disable the view so that we don't try and restore the visibility
3115 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3116 marketButton.setVisibility(View.GONE);
3117 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003118 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003119 }
3120
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003121 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003122 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3123 Resources r = getResources();
3124 Drawable marketIconDrawable = d.newDrawable(r);
3125 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3126 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3127 marketIconDrawable.setBounds(0, 0, w, h);
3128
3129 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003130 }
3131
Michael Jurkad7c28052012-04-27 15:43:36 -07003132 @Override
3133 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3134 boolean result = super.dispatchPopulateAccessibilityEvent(event);
3135 final List<CharSequence> text = event.getText();
3136 text.clear();
3137 text.add(getString(R.string.home));
3138 return result;
3139 }
3140
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003141 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003142 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003143 */
3144 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3145 @Override
3146 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003147 closeSystemDialogs();
3148 }
3149 }
3150
3151 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003152 * Receives notifications whenever the appwidgets are reset.
3153 */
3154 private class AppWidgetResetObserver extends ContentObserver {
3155 public AppWidgetResetObserver() {
3156 super(new Handler());
3157 }
3158
3159 @Override
3160 public void onChange(boolean selfChange) {
3161 onAppWidgetReset();
3162 }
3163 }
3164
3165 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003166 * If the activity is currently paused, signal that we need to re-run the loader
3167 * in onResume.
3168 *
3169 * This needs to be called from incoming places where resources might have been loaded
3170 * while we are paused. That is becaues the Configuration might be wrong
3171 * when we're not running, and if it comes back to what it was when we
3172 * were paused, we are not restarted.
3173 *
3174 * Implementation of the method from LauncherModel.Callbacks.
3175 *
3176 * @return true if we are currently paused. The caller might be able to
3177 * skip some work in that case since we will come back again.
3178 */
3179 public boolean setLoadOnResume() {
3180 if (mPaused) {
3181 Log.i(TAG, "setLoadOnResume");
3182 mOnResumeNeedsLoad = true;
3183 return true;
3184 } else {
3185 return false;
3186 }
3187 }
3188
3189 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003190 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003191 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003192 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003193 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003194 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003195 } else {
3196 return SCREEN_COUNT / 2;
3197 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003198 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003199
Joe Onorato9c1289c2009-08-17 11:03:03 -04003200 /**
3201 * Refreshes the shortcuts shown on the workspace.
3202 *
3203 * Implementation of the method from LauncherModel.Callbacks.
3204 */
3205 public void startBinding() {
3206 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003207
Winson Chungf0c6ae02012-03-21 16:10:31 -07003208 mNewShortcutAnimatePage = -1;
3209 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003210 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003211 int count = workspace.getChildCount();
3212 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003213 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003214 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3215 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003216 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003217 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003218 if (mHotseat != null) {
3219 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003220 }
3221 }
3222
3223 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003224 * Bind the items start-end from the list.
3225 *
3226 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003227 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003228 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003229 setLoadOnResume();
3230
Winson Chungf0c6ae02012-03-21 16:10:31 -07003231 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3232 Set<String> newApps = new HashSet<String>();
3233 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3234
3235 Workspace workspace = mWorkspace;
3236 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003237 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003238
3239 // Short circuit if we are loading dock items for a configuration which has no dock
3240 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3241 mHotseat == null) {
3242 continue;
3243 }
3244
Joe Onorato9c1289c2009-08-17 11:03:03 -04003245 switch (item.itemType) {
3246 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3247 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003248 ShortcutInfo info = (ShortcutInfo) item;
3249 String uri = info.intent.toUri(0).toString();
3250 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003251 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3252 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003253 boolean animateIconUp = false;
3254 synchronized (newApps) {
3255 if (newApps.contains(uri)) {
3256 animateIconUp = newApps.remove(uri);
3257 }
3258 }
3259 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003260 // Prepare the view to be animated up
3261 shortcut.setAlpha(0f);
3262 shortcut.setScaleX(0f);
3263 shortcut.setScaleY(0f);
3264 mNewShortcutAnimatePage = item.screen;
3265 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3266 mNewShortcutAnimateViews.add(shortcut);
3267 }
3268 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003269 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003270 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003271 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003272 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003273 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003274 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3275 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003276 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003277 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003278 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003279
Joe Onorato9c1289c2009-08-17 11:03:03 -04003280 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003281 }
3282
Joe Onorato9c1289c2009-08-17 11:03:03 -04003283 /**
3284 * Implementation of the method from LauncherModel.Callbacks.
3285 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003286 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003287 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003288 sFolders.clear();
3289 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003290 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003291
3292 /**
3293 * Add the views for a widget to the workspace.
3294 *
3295 * Implementation of the method from LauncherModel.Callbacks.
3296 */
3297 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003298 setLoadOnResume();
3299
Daniel Sandler843e8602010-06-07 14:59:01 -04003300 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3301 if (DEBUG_WIDGETS) {
3302 Log.d(TAG, "bindAppWidget: " + item);
3303 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003304 final Workspace workspace = mWorkspace;
3305
3306 final int appWidgetId = item.appWidgetId;
3307 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003308 if (DEBUG_WIDGETS) {
3309 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3310 }
3311
Joe Onorato9c1289c2009-08-17 11:03:03 -04003312 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3313
Joe Onorato9c1289c2009-08-17 11:03:03 -04003314 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003315 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003316
Winson Chung3d503fb2011-07-13 17:25:49 -07003317 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003318 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003319 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3320
Joe Onorato9c1289c2009-08-17 11:03:03 -04003321 workspace.requestLayout();
3322
Daniel Sandler843e8602010-06-07 14:59:01 -04003323 if (DEBUG_WIDGETS) {
3324 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3325 + (SystemClock.uptimeMillis()-start) + "ms");
3326 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003327 }
3328
3329 /**
3330 * Callback saying that there aren't any more items to bind.
3331 *
3332 * Implementation of the method from LauncherModel.Callbacks.
3333 */
3334 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003335 setLoadOnResume();
3336
Joe Onorato9c1289c2009-08-17 11:03:03 -04003337 if (mSavedState != null) {
3338 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003339 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003340 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003341 mSavedState = null;
3342 }
3343
3344 if (mSavedInstanceState != null) {
3345 super.onRestoreInstanceState(mSavedInstanceState);
3346 mSavedInstanceState = null;
3347 }
3348
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003349 // If we received the result of any pending adds while the loader was running (e.g. the
3350 // widget configuration forced an orientation change), process them now.
3351 for (int i = 0; i < sPendingAddList.size(); i++) {
3352 completeAdd(sPendingAddList.get(i));
3353 }
3354 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003355
Winson Chungc51db6a2011-10-05 11:44:49 -07003356 // Update the market app icon as necessary (the other icons will be managed in response to
3357 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003358 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003359
Winson Chungf0c6ae02012-03-21 16:10:31 -07003360 // Animate up any icons as necessary
3361 if (mVisible || mWorkspaceLoading) {
3362 Runnable newAppsRunnable = new Runnable() {
3363 @Override
3364 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003365 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003366 }
3367 };
Winson Chunga2413752012-04-03 14:22:34 -07003368
3369 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3370 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3371 if (canRunNewAppsAnimation()) {
3372 // If the user has not interacted recently, then either snap to the new page to show
3373 // the new-apps animation or just run them if they are to appear on the current page
3374 if (willSnapPage) {
3375 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3376 } else {
3377 runNewAppsAnimation(false);
3378 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003379 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003380 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003381 // are on another page (or just normally if they are added to the current page)
3382 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003383 }
3384 }
3385
3386 mWorkspaceLoading = false;
3387 }
3388
Winson Chunga2413752012-04-03 14:22:34 -07003389 private boolean canRunNewAppsAnimation() {
3390 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3391 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3392 }
3393
Winson Chungf0c6ae02012-03-21 16:10:31 -07003394 /**
3395 * Runs a new animation that scales up icons that were added while Launcher was in the
3396 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003397 *
3398 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003399 */
Winson Chunga2413752012-04-03 14:22:34 -07003400 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003401 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003402 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003403
3404 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003405 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3406 @Override
3407 public int compare(View a, View b) {
3408 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3409 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3410 int cellCountX = LauncherModel.getCellCountX();
3411 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3412 }
3413 });
Winson Chunga2413752012-04-03 14:22:34 -07003414
3415 // Animate each of the views in place (or show them immediately if requested)
3416 if (immediate) {
3417 for (View v : mNewShortcutAnimateViews) {
3418 v.setAlpha(1f);
3419 v.setScaleX(1f);
3420 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003421 }
Winson Chunga2413752012-04-03 14:22:34 -07003422 } else {
3423 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3424 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003425 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003426 PropertyValuesHolder.ofFloat("alpha", 1f),
3427 PropertyValuesHolder.ofFloat("scaleX", 1f),
3428 PropertyValuesHolder.ofFloat("scaleY", 1f));
3429 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3430 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3431 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3432 bounceAnims.add(bounceAnim);
3433 }
3434 anim.playTogether(bounceAnims);
3435 anim.addListener(new AnimatorListenerAdapter() {
3436 @Override
3437 public void onAnimationEnd(Animator animation) {
3438 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3439 }
3440 });
3441 anim.start();
3442 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003443
3444 // Clean up
3445 mNewShortcutAnimatePage = -1;
3446 mNewShortcutAnimateViews.clear();
3447 new Thread("clearNewAppsThread") {
3448 public void run() {
3449 mSharedPrefs.edit()
3450 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3451 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3452 .commit();
3453 }
3454 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003455 }
3456
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003457 @Override
3458 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003459 boolean searchVisible = updateGlobalSearchIcon();
3460 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3461 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003462 }
3463
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003464 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003465 * Add the icons for all apps.
3466 *
3467 * Implementation of the method from LauncherModel.Callbacks.
3468 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003469 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3470 // Remove the progress bar entirely; we could also make it GONE
3471 // but better to remove it since we know it's not going to be used
3472 View progressBar = mAppsCustomizeTabHost.
3473 findViewById(R.id.apps_customize_progress_bar);
3474 if (progressBar != null) {
3475 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003476 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003477 // We just post the call to setApps so the user sees the progress bar
3478 // disappear-- otherwise, it just looks like the progress bar froze
3479 // which doesn't look great
3480 mAppsCustomizeTabHost.post(new Runnable() {
3481 public void run() {
3482 if (mAppsCustomizeContent != null) {
3483 mAppsCustomizeContent.setApps(apps);
3484 }
3485 }
3486 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003487 }
3488
3489 /**
3490 * A package was installed.
3491 *
3492 * Implementation of the method from LauncherModel.Callbacks.
3493 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003494 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003495 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003496
Winson Chung785d2eb2011-04-14 16:08:02 -07003497 if (mAppsCustomizeContent != null) {
3498 mAppsCustomizeContent.addApps(apps);
3499 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003500 }
3501
3502 /**
3503 * A package was updated.
3504 *
3505 * Implementation of the method from LauncherModel.Callbacks.
3506 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003507 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003508 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003509 if (mWorkspace != null) {
3510 mWorkspace.updateShortcuts(apps);
3511 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003512
Winson Chung785d2eb2011-04-14 16:08:02 -07003513 if (mAppsCustomizeContent != null) {
3514 mAppsCustomizeContent.updateApps(apps);
3515 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003516 }
3517
3518 /**
3519 * A package was uninstalled.
3520 *
3521 * Implementation of the method from LauncherModel.Callbacks.
3522 */
Joe Onorato36115782010-06-17 13:28:48 -04003523 public void bindAppsRemoved(ArrayList<ApplicationInfo> apps, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003524 if (permanent) {
3525 mWorkspace.removeItems(apps);
3526 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003527
Winson Chung785d2eb2011-04-14 16:08:02 -07003528 if (mAppsCustomizeContent != null) {
3529 mAppsCustomizeContent.removeApps(apps);
3530 }
Winson Chunga1820962011-10-03 16:31:06 -07003531
3532 // Notify the drag controller
3533 mDragController.onAppsRemoved(apps, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003534 }
Joe Onoratobe386092009-11-17 17:32:16 -08003535
3536 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003537 * A number of packages were updated.
3538 */
3539 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003540 if (mAppsCustomizeContent != null) {
3541 mAppsCustomizeContent.onPackagesUpdated();
3542 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003543 }
3544
Winson Chung400438b2011-01-16 17:53:48 -08003545 private int mapConfigurationOriActivityInfoOri(int configOri) {
3546 final Display d = getWindowManager().getDefaultDisplay();
3547 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3548 switch (d.getRotation()) {
3549 case Surface.ROTATION_0:
3550 case Surface.ROTATION_180:
3551 // We are currently in the same basic orientation as the natural orientation
3552 naturalOri = configOri;
3553 break;
3554 case Surface.ROTATION_90:
3555 case Surface.ROTATION_270:
3556 // We are currently in the other basic orientation to the natural orientation
3557 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3558 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3559 break;
3560 }
3561
3562 int[] oriMap = {
3563 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3564 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3565 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3566 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3567 };
3568 // Since the map starts at portrait, we need to offset if this device's natural orientation
3569 // is landscape.
3570 int indexOffset = 0;
3571 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3572 indexOffset = 1;
3573 }
3574 return oriMap[(d.getRotation() + indexOffset) % 4];
3575 }
Adam Cohen446e9402011-09-15 18:21:21 -07003576
Winson Chung4b919f82012-05-01 10:44:08 -07003577 public boolean isRotationEnabled() {
Michael Jurka9bc8eba2012-05-21 20:36:44 -07003578 boolean forceEnableRotation = doesFileExist(FORCE_ENABLE_ROTATION_PROPERTY);
Winson Chung4b919f82012-05-01 10:44:08 -07003579 boolean enableRotation = forceEnableRotation ||
3580 getResources().getBoolean(R.bool.allow_rotation);
3581 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003582 }
Winson Chung4b919f82012-05-01 10:44:08 -07003583 public void lockScreenOrientation() {
3584 if (isRotationEnabled()) {
3585 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3586 .getConfiguration().orientation));
3587 }
3588 }
3589 public void unlockScreenOrientation(boolean immediate) {
3590 if (isRotationEnabled()) {
3591 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003592 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003593 } else {
3594 mHandler.postDelayed(new Runnable() {
3595 public void run() {
3596 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3597 }
3598 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003599 }
Winson Chung4b919f82012-05-01 10:44:08 -07003600 }
Winson Chung400438b2011-01-16 17:53:48 -08003601 }
3602
Winson Chung82f55532011-08-09 14:14:23 -07003603 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003604 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003605 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003606 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003607
Winson Chung7d7541e2011-09-16 20:14:36 -07003608 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003609 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003610 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3611 Cling cling = (Cling) findViewById(clingId);
3612 if (cling != null) {
3613 cling.init(this, positionData);
3614 cling.setVisibility(View.VISIBLE);
3615 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3616 if (animate) {
3617 cling.buildLayer();
3618 cling.setAlpha(0f);
3619 cling.animate()
3620 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003621 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003622 .setDuration(SHOW_CLING_DURATION)
3623 .setStartDelay(delay)
3624 .start();
3625 } else {
3626 cling.setAlpha(1f);
3627 }
3628 }
3629 return cling;
3630 }
3631 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003632 if (cling != null) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003633 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003634 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003635 anim.addListener(new AnimatorListenerAdapter() {
3636 public void onAnimationEnd(Animator animation) {
3637 cling.setVisibility(View.GONE);
3638 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003639 // We should update the shared preferences on a background thread
3640 new Thread("dismissClingThread") {
3641 public void run() {
3642 SharedPreferences.Editor editor = mSharedPrefs.edit();
3643 editor.putBoolean(flag, true);
3644 editor.commit();
3645 }
3646 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003647 };
3648 });
3649 anim.start();
3650 }
3651 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003652 private void removeCling(int id) {
3653 final View cling = findViewById(id);
3654 if (cling != null) {
3655 final ViewGroup parent = (ViewGroup) cling.getParent();
3656 parent.post(new Runnable() {
3657 @Override
3658 public void run() {
3659 parent.removeView(cling);
3660 }
3661 });
3662 }
3663 }
Michael Jurka974c3862012-05-22 22:00:31 -07003664
3665 private boolean skipCustomClingIfNoAccounts() {
3666 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3667 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3668 if (customCling) {
3669 AccountManager am = AccountManager.get(this);
3670 Account[] accounts = am.getAccountsByType("com.google");
3671 return accounts.length == 0;
3672 }
3673 return false;
3674 }
3675
Winson Chung7d7541e2011-09-16 20:14:36 -07003676 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003677 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003678 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003679 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3680 !skipCustomClingIfNoAccounts() ) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003681 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003682 } else {
3683 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003684 }
3685 }
3686 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003687 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003688 if (isClingsEnabled() &&
3689 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003690 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003691 } else {
3692 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003693 }
3694 }
3695 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003696 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003697 if (isClingsEnabled() &&
3698 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3699 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003700 } else {
3701 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003702 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003703 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003704 }
3705 public boolean isFolderClingVisible() {
3706 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003707 if (cling != null) {
3708 return cling.getVisibility() == View.VISIBLE;
3709 }
3710 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003711 }
Winson Chung82f55532011-08-09 14:14:23 -07003712 public void dismissWorkspaceCling(View v) {
3713 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003714 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003715 }
3716 public void dismissAllAppsCling(View v) {
3717 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003718 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3719 }
3720 public void dismissFolderCling(View v) {
3721 Cling cling = (Cling) findViewById(R.id.folder_cling);
3722 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003723 }
3724
Winson Chung80baf5a2010-08-09 16:03:15 -07003725 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003726 * Prints out out state for debugging.
3727 */
3728 public void dumpState() {
3729 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003730 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003731 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3732 Log.d(TAG, "mRestoring=" + mRestoring);
3733 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3734 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003735 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003736 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003737
Winson Chung785d2eb2011-04-14 16:08:02 -07003738 if (mAppsCustomizeContent != null) {
3739 mAppsCustomizeContent.dumpState();
3740 }
Joe Onoratobe386092009-11-17 17:32:16 -08003741 Log.d(TAG, "END launcher2 dump state");
3742 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003743
3744 @Override
3745 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3746 super.dump(prefix, fd, writer, args);
3747 writer.println(" ");
3748 writer.println("Debug logs: ");
3749 for (int i = 0; i < sDumpLogs.size(); i++) {
3750 writer.println(" " + sDumpLogs.get(i));
3751 }
3752 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003753}
Michael Jurka2763be32011-02-24 11:19:57 -08003754
Michael Jurkaabded662011-03-04 12:06:57 -08003755interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003756 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003757 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003758 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003759 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003760 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003761}