blob: 9784f572b89c5c0da982c76aa29a8ebf8cff83f6 [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,
Winson Chungcd810732012-06-18 16:45:43 -0700120 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
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700357 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
358 // this also ensures that any synchronous binding below doesn't re-trigger another
359 // LauncherModel load.
360 mPaused = false;
361
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800362 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200363 android.os.Debug.startMethodTracing(
364 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800365 }
366
367 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800368 setContentView(R.layout.launcher);
369 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700370 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800371
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800372 registerContentObservers();
373
Joe Onorato7c312c12009-08-13 21:36:53 -0700374 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700375
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800376 mSavedState = savedInstanceState;
377 restoreState(mSavedState);
378
Winson Chunga12a2502010-12-20 14:41:35 -0800379 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700380 if (mAppsCustomizeContent != null) {
381 mAppsCustomizeContent.onPackagesUpdated();
382 }
Winson Chunga12a2502010-12-20 14:41:35 -0800383
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384 if (PROFILE_STARTUP) {
385 android.os.Debug.stopMethodTracing();
386 }
387
388 if (!mRestoring) {
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700389 mModel.startLoader(true, mWorkspace.getCurrentPage());
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800390 }
391
Michael Jurkac57b7a82011-08-09 22:02:20 -0700392 if (!mModel.isAllAppsLoaded()) {
393 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
394 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
395 }
396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 // For handling default keys
398 mDefaultKeySsb = new SpannableStringBuilder();
399 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800400
401 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
402 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800403
Adam Cohenf9426d52012-06-04 17:26:21 -0700404 updateGlobalIcons();
405
406 // On large interfaces, we want the screen to auto-rotate based on the current orientation
407 unlockScreenOrientation(true);
408 }
409
410 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700411 boolean searchVisible = false;
412 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800413 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700414 int coi = getCurrentOrientationIndexForGlobalIcons();
415 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
416 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700417 updateAppMarketIcon();
418 searchVisible = updateGlobalSearchIcon();
419 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700420 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700421 if (sGlobalSearchIcon[coi] != null) {
422 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700423 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700424 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700425 if (sVoiceSearchIcon[coi] != null) {
426 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700427 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700428 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700429 if (sAppMarketIcon[coi] != null) {
430 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800431 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700432 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 }
Romain Guycbb89e42009-06-08 15:52:54 -0700434
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800435 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700436 if (sLocaleConfiguration == null) {
437 new AsyncTask<Void, Void, LocaleConfiguration>() {
438 @Override
439 protected LocaleConfiguration doInBackground(Void... unused) {
440 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
441 readConfiguration(Launcher.this, localeConfiguration);
442 return localeConfiguration;
443 }
444
445 @Override
446 protected void onPostExecute(LocaleConfiguration result) {
447 sLocaleConfiguration = result;
448 checkForLocaleChange(); // recursive, but now with a locale configuration
449 }
450 }.execute();
451 return;
452 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 final Configuration configuration = getResources().getConfiguration();
455
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700456 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 final String locale = configuration.locale.toString();
458
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700459 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 final int mcc = configuration.mcc;
461
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700462 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 final int mnc = configuration.mnc;
464
Romain Guy84f296c2009-11-04 15:00:44 -0800465 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466
Romain Guy84f296c2009-11-04 15:00:44 -0800467 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700468 sLocaleConfiguration.locale = locale;
469 sLocaleConfiguration.mcc = mcc;
470 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700471
Joe Onorato0589f0f2010-02-08 13:44:00 -0800472 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700473
474 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
475 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700476 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700477 public void run() {
478 writeConfiguration(Launcher.this, localeConfiguration);
479 }
480 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700481 }
482 }
483
484 private static class LocaleConfiguration {
485 public String locale;
486 public int mcc = -1;
487 public int mnc = -1;
488 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700489
Romain Guy98d01652009-06-30 16:21:04 -0700490 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
491 DataInputStream in = null;
492 try {
493 in = new DataInputStream(context.openFileInput(PREFERENCES));
494 configuration.locale = in.readUTF();
495 configuration.mcc = in.readInt();
496 configuration.mnc = in.readInt();
497 } catch (FileNotFoundException e) {
498 // Ignore
499 } catch (IOException e) {
500 // Ignore
501 } finally {
502 if (in != null) {
503 try {
504 in.close();
505 } catch (IOException e) {
506 // Ignore
507 }
508 }
509 }
510 }
511
512 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
513 DataOutputStream out = null;
514 try {
515 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
516 out.writeUTF(configuration.locale);
517 out.writeInt(configuration.mcc);
518 out.writeInt(configuration.mnc);
519 out.flush();
520 } catch (FileNotFoundException e) {
521 // Ignore
522 } catch (IOException e) {
523 //noinspection ResultOfMethodCallIgnored
524 context.getFileStreamPath(PREFERENCES).delete();
525 } finally {
526 if (out != null) {
527 try {
528 out.close();
529 } catch (IOException e) {
530 // Ignore
531 }
532 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800533 }
534 }
535
Adam Cohen716b51e2011-06-30 12:09:54 -0700536 public DragLayer getDragLayer() {
537 return mDragLayer;
538 }
539
Winson Chung36a62fe2012-05-06 18:04:42 -0700540 boolean isDraggingEnabled() {
541 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
542 // that is subsequently removed from the workspace in startBinding().
543 return !mModel.isLoadingWorkspace();
544 }
545
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800546 static int getScreen() {
547 synchronized (sLock) {
548 return sScreen;
549 }
550 }
551
552 static void setScreen(int screen) {
553 synchronized (sLock) {
554 sScreen = screen;
555 }
556 }
557
Winson Chung557d6ed2011-07-08 15:34:52 -0700558 /**
559 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
560 * a configuration step, this allows the proper animations to run after other transitions.
561 */
562 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700563 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800564 switch (args.requestCode) {
565 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700566 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
567 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800568 break;
569 case REQUEST_PICK_SHORTCUT:
570 processShortcut(args.intent);
571 break;
572 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700573 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
574 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700575 result = true;
576 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800577 case REQUEST_CREATE_APPWIDGET:
578 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800579 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700580 result = true;
581 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800582 case REQUEST_PICK_WALLPAPER:
583 // We just wanted the activity result here so we can clear mWaitingForResult
584 break;
585 }
Michael Jurka27614d22012-04-02 06:40:22 -0700586 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
587 // if you turned the screen off and then back while in All Apps, Launcher would not
588 // return to the workspace. Clearing mAddInfo.container here fixes this issue
589 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700590 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800591 }
592
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700594 protected void onActivityResult(
595 final int requestCode, final int resultCode, final Intent data) {
596 if (requestCode == REQUEST_BIND_APPWIDGET) {
597 int appWidgetId = data != null ?
598 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
599 if (resultCode == RESULT_CANCELED) {
600 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
601 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700602 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700603 }
604 return;
605 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700606 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800607 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
608 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700609 mWaitingForResult = false;
610
Adam Cohened66b2b2012-01-23 17:28:51 -0800611 // We have special handling for widgets
612 if (isWidgetDrop) {
613 int appWidgetId = data != null ?
614 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700615 if (appWidgetId < 0) {
616 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
617 "widget configuration activity.");
618 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
619 } else {
620 completeTwoStageWidgetDrop(resultCode, appWidgetId);
621 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800622 return;
623 }
624
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800625 // The pattern used here is that a user PICKs a specific application,
626 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700627
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800628 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
629 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700630 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800631 final PendingAddArguments args = new PendingAddArguments();
632 args.requestCode = requestCode;
633 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700634 args.container = mPendingAddInfo.container;
635 args.screen = mPendingAddInfo.screen;
636 args.cellX = mPendingAddInfo.cellX;
637 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800638 if (isWorkspaceLocked()) {
639 sPendingAddList.add(args);
640 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700641 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800642 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800643 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800644 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700645 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800646 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
647 null);
648 }
649
650 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700651 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700652 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800653 Runnable onCompleteRunnable = null;
654 int animationType = 0;
655
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700656 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800657 if (resultCode == RESULT_OK) {
658 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
659 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700660 mPendingAddWidgetInfo);
661 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800662 onCompleteRunnable = new Runnable() {
663 @Override
664 public void run() {
665 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
666 mPendingAddInfo.screen, layout, null);
667 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
668 null);
669 }
670 };
671 } else if (resultCode == RESULT_CANCELED) {
672 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
673 onCompleteRunnable = new Runnable() {
674 @Override
675 public void run() {
676 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
677 null);
678 }
679 };
680 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700681 if (mDragLayer.getAnimatedView() != null) {
682 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
683 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
684 animationType, boundWidget, true);
685 } else {
686 // The animated view may be null in the case of a rotation during widget configuration
687 onCompleteRunnable.run();
688 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 }
690
691 @Override
692 protected void onResume() {
693 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700694
Winson Chungde0fb8f2012-05-08 14:37:08 -0700695 // Process any items that were added while Launcher was away
696 InstallShortcutReceiver.flushInstallQueue(this);
697
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800698 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700699 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400700 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700701 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400702 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700703 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800704 }
Winson Chunge4e50662012-01-23 14:45:13 -0800705
706 // Reset the pressed state of icons that were locked in the press state while activities
707 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800708 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800709 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800710 mWaitingForResume.setStayPressed(false);
711 }
Winson Chunge4e50662012-01-23 14:45:13 -0800712 if (mAppsCustomizeContent != null) {
713 // Resets the previous all apps icon press state
714 mAppsCustomizeContent.resetDrawableState();
715 }
Adam Cohen06dff352012-06-01 17:17:08 -0700716 // It is possible that widgets can receive updates while launcher is not in the foreground.
717 // Consequently, the widgets will be inflated in the orientation of the foreground activity
718 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
719 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700720 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700721
722 // Again, as with the above scenario, it's possible that one or more of the global icons
723 // were updated in the wrong orientation.
724 updateGlobalIcons();
Adam Cohen06dff352012-06-01 17:17:08 -0700725 }
726
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800727 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700728 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700729 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
730 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
731 // when Launcher resumes and we are still in AllApps.
732 updateWallpaperVisibility(true);
733
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700734 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700735 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800736 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700737 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700738 }
Romain Guycbb89e42009-06-08 15:52:54 -0700739
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700740 @Override
741 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400742 // Flag the loader to stop early before switching
743 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700744 if (mAppsCustomizeContent != null) {
745 mAppsCustomizeContent.surrender();
746 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800747 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700748 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700749
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800750 // We can't hide the IME if it was forced open. So don't bother
751 /*
752 @Override
753 public void onWindowFocusChanged(boolean hasFocus) {
754 super.onWindowFocusChanged(hasFocus);
755
756 if (hasFocus) {
757 final InputMethodManager inputManager = (InputMethodManager)
758 getSystemService(Context.INPUT_METHOD_SERVICE);
759 WindowManager.LayoutParams lp = getWindow().getAttributes();
760 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
761 android.os.Handler()) {
762 protected void onReceiveResult(int resultCode, Bundle resultData) {
763 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
764 }
765 });
766 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
767 }
768 }
769 */
770
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800771 private boolean acceptFilter() {
772 final InputMethodManager inputManager = (InputMethodManager)
773 getSystemService(Context.INPUT_METHOD_SERVICE);
774 return !inputManager.isFullscreenMode();
775 }
776
777 @Override
778 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700779 final int uniChar = event.getUnicodeChar();
780 final boolean handled = super.onKeyDown(keyCode, event);
781 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
782 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800783 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
784 keyCode, event);
785 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700786 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700787 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700788 // showSearchDialog()
789 // If there are multiple keystrokes before the search dialog takes focus,
790 // onSearchRequested() will be called for every keystroke,
791 // but it is idempotent, so it's fine.
792 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800793 }
794 }
795
Joe Onorato8a9625e2010-01-28 15:55:35 -0800796 // Eat the long press event so the keyboard doesn't come up.
797 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
798 return true;
799 }
800
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800801 return handled;
802 }
803
Karl Rosaen138a0412009-04-23 19:00:21 -0700804 private String getTypedText() {
805 return mDefaultKeySsb.toString();
806 }
807
808 private void clearTypedText() {
809 mDefaultKeySsb.clear();
810 mDefaultKeySsb.clearSpans();
811 Selection.setSelection(mDefaultKeySsb, 0);
812 }
813
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800814 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700815 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
816 * State
817 */
818 private static State intToState(int stateOrdinal) {
819 State state = State.WORKSPACE;
820 final State[] stateValues = State.values();
821 for (int i = 0; i < stateValues.length; i++) {
822 if (stateValues[i].ordinal() == stateOrdinal) {
823 state = stateValues[i];
824 break;
825 }
826 }
827 return state;
828 }
829
830 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800831 * Restores the previous state, if it exists.
832 *
833 * @param savedState The previous state.
834 */
835 private void restoreState(Bundle savedState) {
836 if (savedState == null) {
837 return;
838 }
839
Michael Jurka883f55b2010-10-21 15:47:14 -0700840 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700841 if (state == State.APPS_CUSTOMIZE) {
Joe Onorato3a8820b2009-11-10 15:06:42 -0800842 showAllApps(false);
843 }
844
Winson Chungf0c6ae02012-03-21 16:10:31 -0700845 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700847 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 }
849
Winson Chung3d503fb2011-07-13 17:25:49 -0700850 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
851 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700852
Winson Chung3d503fb2011-07-13 17:25:49 -0700853 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
854 mPendingAddInfo.container = pendingAddContainer;
855 mPendingAddInfo.screen = pendingAddScreen;
856 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
857 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700858 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
859 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
860 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700861 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800862 mRestoring = true;
863 }
864
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700865
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800866 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
867 if (renameFolder) {
868 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700869 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800870 mRestoring = true;
871 }
Winson Chunga12a2502010-12-20 14:41:35 -0800872
Winson Chung785d2eb2011-04-14 16:08:02 -0700873
874 // Restore the AppsCustomize tab
875 if (mAppsCustomizeTabHost != null) {
876 String curTab = savedState.getString("apps_customize_currentTab");
877 if (curTab != null) {
Winson Chungf0ea4d32011-06-06 14:27:16 -0700878 // We set this directly so that there is no delay before the tab is set
Winson Chung785d2eb2011-04-14 16:08:02 -0700879 mAppsCustomizeContent.setContentType(
880 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
881 mAppsCustomizeTabHost.setCurrentTabByTag(curTab);
Winson Chungf314b0e2011-08-16 11:54:27 -0700882 mAppsCustomizeContent.loadAssociatedPages(
883 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700884 }
885
Winson Chung5afbf7b2011-07-25 11:53:08 -0700886 int currentIndex = savedState.getInt("apps_customize_currentIndex");
887 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700888 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800889 }
890
891 /**
892 * Finds all the views we need and configure them properly.
893 */
894 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700895 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400896
Winson Chung4c98d922011-05-31 16:50:48 -0700897 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
898 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800899 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
900 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901
Winson Chung4c98d922011-05-31 16:50:48 -0700902 // Setup the drag layer
903 mDragLayer.setup(this, dragController);
904
Winson Chung3d503fb2011-07-13 17:25:49 -0700905 // Setup the hotseat
906 mHotseat = (Hotseat) findViewById(R.id.hotseat);
907 if (mHotseat != null) {
908 mHotseat.setup(this);
909 }
910
Winson Chung4c98d922011-05-31 16:50:48 -0700911 // Setup the workspace
912 mWorkspace.setHapticFeedbackEnabled(false);
913 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700914 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700915 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700916
Winson Chungf0ea4d32011-06-06 14:27:16 -0700917 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700918 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700919
Winson Chungf0ea4d32011-06-06 14:27:16 -0700920 // Setup AppsCustomize
921 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
922 findViewById(R.id.apps_customize_pane);
923 mAppsCustomizeContent = (AppsCustomizePagedView)
924 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
925 mAppsCustomizeContent.setup(this, dragController);
Daniel Sandlerc9b18772010-04-22 14:37:59 -0400926
Michael Jurka5130e402011-10-13 04:55:35 -0700927 // Get the all apps button
928 mAllAppsButton = findViewById(R.id.all_apps_button);
929 if (mAllAppsButton != null) {
930 mAllAppsButton.setOnTouchListener(new View.OnTouchListener() {
931 @Override
932 public boolean onTouch(View v, MotionEvent event) {
933 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
934 onTouchDownAllAppsButton(v);
935 }
936 return false;
937 }
938 });
939 }
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700941 dragController.setDragScoller(mWorkspace);
942 dragController.setScrollView(mDragLayer);
943 dragController.setMoveTarget(mWorkspace);
944 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700945 if (mSearchDropTargetBar != null) {
946 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800947 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800948 }
949
950 /**
951 * Creates a view representing a shortcut.
952 *
953 * @param info The data structure describing the shortcut.
954 *
955 * @return A View inflated from R.layout.application.
956 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800957 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700959 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 }
961
962 /**
963 * Creates a view representing a shortcut inflated from the specified resource.
964 *
965 * @param layoutResId The id of the XML layout used to create the shortcut.
966 * @param parent The group the shortcut belongs to.
967 * @param info The data structure describing the shortcut.
968 *
969 * @return A View inflated from layoutResId.
970 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800971 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800972 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
973 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 return favorite;
976 }
977
978 /**
979 * Add an application shortcut to the workspace.
980 *
981 * @param data The intent describing the application.
982 * @param cellInfo The position on screen where to create the shortcut.
983 */
Winson Chung3d503fb2011-07-13 17:25:49 -0700984 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -0700985 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -0700986 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700987
Adam Cohenfbba09b2011-07-18 21:43:05 -0700988 // First we check if we already know the exact location where we want to add this item.
989 if (cellX >= 0 && cellY >= 0) {
990 cellXY[0] = cellX;
991 cellXY[1] = cellY;
992 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -0700993 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -0700994 return;
995 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800997 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800998
Romain Guy73b979d2009-06-09 12:57:21 -0700999 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001000 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001002 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001003 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001004 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001005 } else {
1006 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001007 }
1008 }
Romain Guycbb89e42009-06-08 15:52:54 -07001009
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001010 /**
1011 * Add a shortcut to the workspace.
1012 *
1013 * @param data The intent describing the shortcut.
1014 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001016 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1017 int cellY) {
1018 int[] cellXY = mTmpAddItemCellCoordinates;
1019 int[] touchXY = mPendingAddInfo.dropPos;
1020 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001021
Michael Jurkad3ef3062010-11-23 16:23:58 -08001022 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001023
Adam Cohen558baaf2011-08-15 15:22:57 -07001024 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001025 if (info == null) {
1026 return;
1027 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001028 final View view = createShortcut(info);
1029
Adam Cohenfbba09b2011-07-18 21:43:05 -07001030 // First we check if we already know the exact location where we want to add this item.
1031 if (cellX >= 0 && cellY >= 0) {
1032 cellXY[0] = cellX;
1033 cellXY[1] = cellY;
1034 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001035
1036 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001037 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001038 true, null,null)) {
1039 return;
1040 }
1041 DragObject dragObject = new DragObject();
1042 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001043 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1044 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001045 return;
1046 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001047 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001048 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001049 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001050 foundCellSpan = (result != null);
1051 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001052 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001053 }
1054
1055 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001056 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001057 return;
1058 }
1059
Adam Cohen558baaf2011-08-15 15:22:57 -07001060 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001061
1062 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001063 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1064 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001065 }
1066 }
1067
Adam Cohen2f093b62012-04-30 18:59:53 -07001068 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1069 int minHeight) {
1070 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001071 // We want to account for the extra amount of padding that we are adding to the widget
1072 // to ensure that it gets the full amount of space that it has requested
1073 int requiredWidth = minWidth + padding.left + padding.right;
1074 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001075 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001076 }
1077
Adam Cohen2f093b62012-04-30 18:59:53 -07001078 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1079 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001080 }
1081
Adam Cohen2f093b62012-04-30 18:59:53 -07001082 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1083 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001084 }
1085
Adam Cohen2f093b62012-04-30 18:59:53 -07001086 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1087 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001088 }
1089
Adam Cohen2f093b62012-04-30 18:59:53 -07001090 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1091 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001092 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001093 }
1094
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001095 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001096 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001098 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001099 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001100 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001101 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1102 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1103 if (appWidgetInfo == null) {
1104 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1105 }
Romain Guycbb89e42009-06-08 15:52:54 -07001106
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001107 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001108 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001109
Adam Cohen2f093b62012-04-30 18:59:53 -07001110 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1111 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001112
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001113 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001114 // We have saved the position to which the widget was dragged-- this really only matters
1115 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001116 int[] cellXY = mTmpAddItemCellCoordinates;
1117 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001118 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001119 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001120 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1121 cellXY[0] = mPendingAddInfo.cellX;
1122 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001123 spanXY[0] = mPendingAddInfo.spanX;
1124 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001125 foundCellSpan = true;
1126 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001127 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001128 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001129 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1130 spanXY[1], cellXY, finalSpan);
1131 spanXY[0] = finalSpan[0];
1132 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001133 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001134 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001135 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001136 }
1137
Michael Jurka0280c3b2010-09-17 15:00:07 -07001138 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001139 if (appWidgetId != -1) {
1140 // Deleting an app widget ID is a void call but writes to disk before returning
1141 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001142 new Thread("deleteAppWidgetId") {
1143 public void run() {
1144 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1145 }
1146 }.start();
1147 }
Winson Chung93eef082012-03-23 15:59:27 -07001148 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001149 return;
1150 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001151
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001152 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001153 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1154 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001155 launcherInfo.spanX = spanXY[0];
1156 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001157 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1158 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001159
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001161 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162
1163 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001164 if (hostView == null) {
1165 // Perform actual inflation because we're live
1166 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1167 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1168 } else {
1169 // The AppWidgetHostView has already been inflated and instantiated
1170 launcherInfo.hostView = hostView;
1171 }
Romain Guycbb89e42009-06-08 15:52:54 -07001172
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001174 launcherInfo.hostView.setVisibility(View.VISIBLE);
Winson Chung211bac32012-05-15 13:43:57 -07001175 launcherInfo.notifyWidgetSizeChanged(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001176 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001177 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001178
1179 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001181 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 }
Romain Guycbb89e42009-06-08 15:52:54 -07001183
Adam Cohended9f8d2010-11-03 13:25:16 -07001184 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1185 @Override
1186 public void onReceive(Context context, Intent intent) {
1187 final String action = intent.getAction();
1188 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1189 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001190 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001191 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001192
Winson Chungc100e8e2011-08-09 16:02:43 -07001193 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001194 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001195 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1196 mAppsCustomizeTabHost.reset();
1197 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001198 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001199 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1200 mUserPresent = true;
1201 updateRunning();
1202 }
1203 }
1204 };
1205
1206 @Override
1207 public void onAttachedToWindow() {
1208 super.onAttachedToWindow();
1209
1210 // Listen for broadcasts related to user-presence
1211 final IntentFilter filter = new IntentFilter();
1212 filter.addAction(Intent.ACTION_SCREEN_OFF);
1213 filter.addAction(Intent.ACTION_USER_PRESENT);
1214 registerReceiver(mReceiver, filter);
1215
Adam Cohend113e0c2010-11-11 10:48:05 -08001216 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001217 mVisible = true;
1218 }
1219
1220 @Override
1221 public void onDetachedFromWindow() {
1222 super.onDetachedFromWindow();
1223 mVisible = false;
1224
Adam Cohend113e0c2010-11-11 10:48:05 -08001225 if (mAttached) {
1226 unregisterReceiver(mReceiver);
1227 mAttached = false;
1228 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001229 updateRunning();
1230 }
1231
1232 public void onWindowVisibilityChanged(int visibility) {
1233 mVisible = visibility == View.VISIBLE;
1234 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001235 // The following code used to be in onResume, but it turns out onResume is called when
1236 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1237 // is a more appropriate event to handle
1238 if (mVisible) {
1239 mAppsCustomizeTabHost.onWindowVisible();
1240 if (!mWorkspaceLoading) {
1241 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001242 // We want to let Launcher draw itself at least once before we force it to build
1243 // layers on all the workspace pages, so that transitioning to Launcher from other
1244 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1245 // a true draw so we wait until the second preDraw call to be safe
1246 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001247 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001248 // We delay the layer building a bit in order to give
1249 // other message processing a time to run. In particular
1250 // this avoids a delay in hiding the IME if it was
1251 // currently shown, because doing that may involve
1252 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001253 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001254
Michael Jurka6ee21d22012-02-21 18:20:27 -08001255 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001256 return true;
1257 }
1258 });
1259 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001260 // When Launcher comes back to foreground, a different Activity might be responsible for
1261 // the app market intent, so refresh the icon
1262 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001263 clearTypedText();
1264 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001265 }
1266
1267 private void sendAdvanceMessage(long delay) {
1268 mHandler.removeMessages(ADVANCE_MSG);
1269 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1270 mHandler.sendMessageDelayed(msg, delay);
1271 mAutoAdvanceSentTime = System.currentTimeMillis();
1272 }
1273
1274 private void updateRunning() {
1275 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1276 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1277 mAutoAdvanceRunning = autoAdvanceRunning;
1278 if (autoAdvanceRunning) {
1279 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1280 sendAdvanceMessage(delay);
1281 } else {
1282 if (!mWidgetsToAdvance.isEmpty()) {
1283 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1284 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1285 }
1286 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001287 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001288 }
1289 }
1290 }
1291
1292 private final Handler mHandler = new Handler() {
1293 @Override
1294 public void handleMessage(Message msg) {
1295 if (msg.what == ADVANCE_MSG) {
1296 int i = 0;
1297 for (View key: mWidgetsToAdvance.keySet()) {
1298 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1299 final int delay = mAdvanceStagger * i;
1300 if (v instanceof Advanceable) {
1301 postDelayed(new Runnable() {
1302 public void run() {
1303 ((Advanceable) v).advance();
1304 }
1305 }, delay);
1306 }
1307 i++;
1308 }
1309 sendAdvanceMessage(mAdvanceInterval);
1310 }
1311 }
1312 };
1313
1314 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001315 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001316 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1317 if (v instanceof Advanceable) {
1318 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001319 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001320 updateRunning();
1321 }
1322 }
1323
1324 void removeWidgetToAutoAdvance(View hostView) {
1325 if (mWidgetsToAdvance.containsKey(hostView)) {
1326 mWidgetsToAdvance.remove(hostView);
1327 updateRunning();
1328 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001329 }
1330
Joe Onorato9c1289c2009-08-17 11:03:03 -04001331 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001332 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001333 launcherInfo.hostView = null;
1334 }
1335
Winson Chung93eef082012-03-23 15:59:27 -07001336 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1337 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1338 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001339 }
1340
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001341 public LauncherAppWidgetHost getAppWidgetHost() {
1342 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001343 }
Romain Guycbb89e42009-06-08 15:52:54 -07001344
Winson Chunga9abd0e2010-10-27 17:18:37 -07001345 public LauncherModel getModel() {
1346 return mModel;
1347 }
1348
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001349 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001350 getWindow().closeAllPanels();
1351
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001352 // Whatever we were doing is hereby canceled.
1353 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001354 }
1355
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 @Override
1357 protected void onNewIntent(Intent intent) {
1358 super.onNewIntent(intent);
1359
1360 // Close the menu
1361 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001362 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001363 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001364
Joe Onorato14f122b2009-11-19 14:06:36 -08001365 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1366 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001367
Adam Cohenac56cff2011-09-28 20:45:37 -07001368 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001369 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001370 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001371 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1372 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001373 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001374 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001375
1376 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001377 exitSpringLoadedDragMode();
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001378 showWorkspace(alreadyOnHome);
Romain Guy1dd3a072009-07-16 13:21:01 -07001379
Joe Onorato3a8820b2009-11-10 15:06:42 -08001380 final View v = getWindow().peekDecorView();
1381 if (v != null && v.getWindowToken() != null) {
1382 InputMethodManager imm = (InputMethodManager)getSystemService(
1383 INPUT_METHOD_SERVICE);
1384 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001386
Michael Jurka35aa14d2011-07-07 17:01:08 -07001387 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001388 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001389 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001390 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001391 }
1392 }
1393
1394 @Override
1395 protected void onRestoreInstanceState(Bundle savedInstanceState) {
1396 // Do not call super here
1397 mSavedInstanceState = savedInstanceState;
1398 }
1399
1400 @Override
1401 protected void onSaveInstanceState(Bundle outState) {
Michael Jurka0142d492010-08-25 17:46:15 -07001402 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getCurrentPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001403 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404
Michael Jurka883f55b2010-10-21 15:47:14 -07001405 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001406 // We close any open folder since it will not be re-opened, and we need to make sure
1407 // this state is reflected.
1408 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001409
Winson Chung3d503fb2011-07-13 17:25:49 -07001410 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1411 mWaitingForResult) {
1412 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1413 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1414 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1415 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001416 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1417 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1418 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 }
1420
1421 if (mFolderInfo != null && mWaitingForResult) {
1422 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1423 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1424 }
Michael Jurka946ad472010-07-09 18:05:18 -07001425
Winson Chung785d2eb2011-04-14 16:08:02 -07001426 // Save the current AppsCustomize tab
1427 if (mAppsCustomizeTabHost != null) {
1428 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1429 if (currentTabTag != null) {
1430 outState.putString("apps_customize_currentTab", currentTabTag);
1431 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001432 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1433 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001434 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001435 }
1436
1437 @Override
1438 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001440
Winson Chunge7a03942011-08-05 15:05:12 -07001441 // Remove all pending runnables
1442 mHandler.removeMessages(ADVANCE_MSG);
1443 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001444 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001445
Winson Chungcd2b0142011-06-08 16:02:26 -07001446 // Stop callbacks from LauncherModel
1447 LauncherApplication app = ((LauncherApplication) getApplication());
1448 mModel.stopLoader();
1449 app.setLauncher(null);
1450
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001452 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001453 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001454 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001456 mAppWidgetHost = null;
1457
1458 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459
1460 TextKeyListener.getInstance().release();
1461
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462
Winson Chung3d503fb2011-07-13 17:25:49 -07001463 unbindWorkspaceAndHotseatItems();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001464
1465 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001466 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001467
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001468 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001469 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1470 mWorkspace.removeAllViews();
1471 mWorkspace = null;
1472 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001473
Michael Jurka2ecf9952012-06-18 12:52:28 -07001474 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001475 }
1476
Adam Cohena9cf38f2011-05-02 15:36:58 -07001477 public DragController getDragController() {
1478 return mDragController;
1479 }
1480
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 @Override
1482 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001483 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 super.startActivityForResult(intent, requestCode);
1485 }
1486
Winson Chung70d72102011-08-12 11:24:35 -07001487 /**
1488 * Indicates that we want global search for this activity by setting the globalSearch
1489 * argument for {@link #startSearch} to true.
1490 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001492 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001494
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001495 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001496
Karl Rosaen138a0412009-04-23 19:00:21 -07001497 if (initialQuery == null) {
1498 // Use any text typed in the launcher as the initial query
1499 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001500 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 if (appSearchData == null) {
1502 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001503 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 }
Mathew Inwoodcf7f63b2011-10-13 11:31:38 +01001505 Rect sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
Romain Guycbb89e42009-06-08 15:52:54 -07001506
Michael Jurkaa33411c2012-06-14 16:18:21 -07001507 startGlobalSearch(initialQuery, selectInitialQuery,
1508 appSearchData, sourceBounds);
1509 }
1510
1511 /**
1512 * Starts the global search activity. This code is a copied from SearchManager
1513 */
1514 public void startGlobalSearch(String initialQuery,
1515 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001516 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001517 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1518 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1519 if (globalSearchActivity == null) {
1520 Log.w(TAG, "No global search activity found.");
1521 return;
1522 }
1523 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1524 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1525 intent.setComponent(globalSearchActivity);
1526 // Make sure that we have a Bundle to put source in
1527 if (appSearchData == null) {
1528 appSearchData = new Bundle();
1529 } else {
1530 appSearchData = new Bundle(appSearchData);
1531 }
1532 // Set source to package name of app that starts global search, if not set already.
1533 if (!appSearchData.containsKey("source")) {
1534 appSearchData.putString("source", getPackageName());
1535 }
1536 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1537 if (!TextUtils.isEmpty(initialQuery)) {
1538 intent.putExtra(SearchManager.QUERY, initialQuery);
1539 }
1540 if (selectInitialQuery) {
1541 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1542 }
1543 intent.setSourceBounds(sourceBounds);
1544 try {
1545 startActivity(intent);
1546 } catch (ActivityNotFoundException ex) {
1547 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1548 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549 }
1550
Winson Chung70d72102011-08-12 11:24:35 -07001551 @Override
1552 public boolean onCreateOptionsMenu(Menu menu) {
1553 if (isWorkspaceLocked()) {
1554 return false;
1555 }
1556
1557 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001558
1559 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1560 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1561 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001562 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1563 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1564 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001565 String helpUrl = getString(R.string.help_url);
1566 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001567 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1568 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1569
Winson Chung70d72102011-08-12 11:24:35 -07001570 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1571 .setIcon(android.R.drawable.ic_menu_gallery)
1572 .setAlphabeticShortcut('W');
1573 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1574 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001575 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001576 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001577 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1578 .setIcon(android.R.drawable.ic_menu_preferences)
1579 .setIntent(settings)
1580 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001581 if (!helpUrl.isEmpty()) {
1582 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1583 .setIcon(android.R.drawable.ic_menu_help)
1584 .setIntent(help)
1585 .setAlphabeticShortcut('H');
1586 }
Winson Chung70d72102011-08-12 11:24:35 -07001587 return true;
1588 }
1589
1590 @Override
1591 public boolean onPrepareOptionsMenu(Menu menu) {
1592 super.onPrepareOptionsMenu(menu);
1593
1594 if (mAppsCustomizeTabHost.isTransitioning()) {
1595 return false;
1596 }
1597 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1598 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1599
1600 return true;
1601 }
1602
1603 @Override
1604 public boolean onOptionsItemSelected(MenuItem item) {
1605 switch (item.getItemId()) {
1606 case MENU_WALLPAPER_SETTINGS:
1607 startWallpaper();
1608 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001609 }
1610
1611 return super.onOptionsItemSelected(item);
1612 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001614 @Override
1615 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001616 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001617 // Use a custom animation for launching search
1618 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Karl Rosaen138a0412009-04-23 19:00:21 -07001619 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001620 }
1621
Joe Onorato9c1289c2009-08-17 11:03:03 -04001622 public boolean isWorkspaceLocked() {
1623 return mWorkspaceLoading || mWaitingForResult;
1624 }
1625
Michael Jurka0280c3b2010-09-17 15:00:07 -07001626 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001627 mPendingAddInfo.container = ItemInfo.NO_ID;
1628 mPendingAddInfo.screen = -1;
1629 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1630 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001631 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001632 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001633 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001634
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001635 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1636 AppWidgetProviderInfo appWidgetInfo) {
1637 if (appWidgetInfo.configure != null) {
1638 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001639
Bjorn Bringert7984c942009-12-09 15:38:25 +00001640 // Launch over to configure widget, if needed
1641 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001642 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001643 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001644 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001645 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001646 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001647 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1648 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001649 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001650 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651 }
1652 }
Romain Guycbb89e42009-06-08 15:52:54 -07001653
Adam Cohenfbba09b2011-07-18 21:43:05 -07001654 /**
1655 * Process a shortcut drop.
1656 *
1657 * @param componentName The name of the component
1658 * @param screen The screen where it should be added
1659 * @param cell The cell it should be added to, optional
1660 * @param position The location on the screen where it was dropped, optional
1661 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001662 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1663 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001664 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001665 mPendingAddInfo.container = container;
1666 mPendingAddInfo.screen = screen;
1667 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001668
1669 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001670 mPendingAddInfo.cellX = cell[0];
1671 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001672 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001673
1674 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1675 createShortcutIntent.setComponent(componentName);
1676 processShortcut(createShortcutIntent);
1677 }
1678
Adam Cohenfbba09b2011-07-18 21:43:05 -07001679 /**
1680 * Process a widget drop.
1681 *
1682 * @param info The PendingAppWidgetInfo of the widget being added.
1683 * @param screen The screen where it should be added
1684 * @param cell The cell it should be added to, optional
1685 * @param position The location on the screen where it was dropped, optional
1686 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001687 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001688 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001689 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001690 mPendingAddInfo.container = info.container = container;
1691 mPendingAddInfo.screen = info.screen = screen;
1692 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001693 mPendingAddInfo.minSpanX = info.minSpanX;
1694 mPendingAddInfo.minSpanY = info.minSpanY;
1695
Adam Cohenfbba09b2011-07-18 21:43:05 -07001696 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001697 mPendingAddInfo.cellX = cell[0];
1698 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001699 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001700 if (span != null) {
1701 mPendingAddInfo.spanX = span[0];
1702 mPendingAddInfo.spanY = span[1];
1703 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001704
Adam Cohened66b2b2012-01-23 17:28:51 -08001705 AppWidgetHostView hostView = info.boundWidget;
1706 int appWidgetId;
1707 if (hostView != null) {
1708 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001709 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001710 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001711 // In this case, we either need to start an activity to get permission to bind
1712 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001713 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Michael Jurka8b805b12012-04-18 14:23:14 -07001714 if (mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId, info.componentName)) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001715 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001716 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001717 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001718 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1719 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1720 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
1721 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1722 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001723 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001724 }
1725
Joe Onoratodeb98af2010-02-19 14:59:39 -08001726 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001727 // Handle case where user selected "Applications"
1728 String applicationName = getResources().getString(R.string.group_applications);
1729 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001730
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001731 if (applicationName != null && applicationName.equals(shortcutName)) {
1732 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1733 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001734
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001735 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1736 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001737 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001738 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001739 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001740 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001741 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001742 }
1743
Winson Chung24ab2f12010-09-16 14:10:47 -07001744 void processWallpaper(Intent intent) {
1745 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1746 }
1747
Winson Chung3d503fb2011-07-13 17:25:49 -07001748 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1749 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001750 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001751 folderInfo.title = getText(R.string.folder_name);
1752
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001753 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001754 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1755 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001756 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001757
1758 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001759 FolderIcon newFolder =
1760 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1761 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1762 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001763 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001764 }
Romain Guycbb89e42009-06-08 15:52:54 -07001765
Joe Onorato9c1289c2009-08-17 11:03:03 -04001766 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001767 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001768 }
1769
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001771 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001772 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001773 Intent chooser = Intent.createChooser(pickWallpaper,
1774 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001775 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1776 // Removed in Eclair MR1
1777// WallpaperManager wm = (WallpaperManager)
1778// getSystemService(Context.WALLPAPER_SERVICE);
1779// WallpaperInfo wi = wm.getWallpaperInfo();
1780// if (wi != null && wi.getSettingsActivity() != null) {
1781// LabeledIntent li = new LabeledIntent(getPackageName(),
1782// R.string.configure_wallpaper, 0);
1783// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1784// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1785// }
Mike Clerona0618e42009-10-22 13:55:21 -07001786 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001787 }
1788
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001789 /**
1790 * Registers various content observers. The current implementation registers
1791 * only a favorites observer to keep track of the favorites applications.
1792 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001793 private void registerContentObservers() {
1794 ContentResolver resolver = getContentResolver();
1795 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1796 true, mWidgetObserver);
1797 }
1798
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001799 @Override
1800 public boolean dispatchKeyEvent(KeyEvent event) {
1801 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1802 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001804 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001805 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001806 if (doesFileExist(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001807 dumpState();
1808 return true;
1809 }
1810 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001811 }
1812 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1813 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001814 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001815 return true;
1816 }
1817 }
1818
1819 return super.dispatchKeyEvent(event);
1820 }
1821
Joe Onorato88ec0992009-11-19 13:16:06 -08001822 @Override
1823 public void onBackPressed() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07001824 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001825 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001826 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001827 Folder openFolder = mWorkspace.getOpenFolder();
1828 if (openFolder.isEditingName()) {
1829 openFolder.dismissEditingName();
1830 } else {
1831 closeFolder();
1832 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001833 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001834 mWorkspace.exitWidgetResizeMode();
1835
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001836 // Back button is a no-op here, but give at least some feedback for the button press
1837 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001838 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001839 }
1840
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001841 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001842 * Re-listen when widgets are reset.
1843 */
1844 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001845 if (mAppWidgetHost != null) {
1846 mAppWidgetHost.startListening();
1847 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001848 }
1849
1850 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04001851 * Go through the and disconnect any of the callbacks in the drawables and the views or we
1852 * leak the previous Home screen on orientation change.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001854 private void unbindWorkspaceAndHotseatItems() {
Winson Chung603bcb92011-09-02 11:45:39 -07001855 if (mModel != null) {
1856 mModel.unbindWorkspaceItems();
1857 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 }
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001859
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001860 /**
1861 * Launches the intent referred by the clicked shortcut.
1862 *
1863 * @param v The view representing the clicked shortcut.
1864 */
1865 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001866 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1867 // view has detached (it's possible for this to happen if the view is removed mid touch).
1868 if (v.getWindowToken() == null) {
1869 return;
1870 }
1871
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001872 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001873 return;
1874 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001875
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001876 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001877 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001879 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001880 int[] pos = new int[2];
1881 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001882 intent.setSourceBounds(new Rect(pos[0], pos[1],
1883 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001884
1885 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001886
1887 if (success && v instanceof BubbleTextView) {
1888 mWaitingForResume = (BubbleTextView) v;
1889 mWaitingForResume.setStayPressed(true);
1890 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001892 if (v instanceof FolderIcon) {
1893 FolderIcon fi = (FolderIcon) v;
1894 handleFolderClick(fi);
1895 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001896 } else if (v == mAllAppsButton) {
1897 if (mState == State.APPS_CUSTOMIZE) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001898 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001899 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001900 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001901 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001902 }
1903 }
1904
Michael Jurka0e260592010-06-30 17:07:39 -07001905 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001906 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001907 // clicking anywhere on the workspace causes the customization drawer to slide down
1908 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001909 return false;
1910 }
1911
Michael Jurkaaf442092010-06-10 17:01:57 -07001912 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001913 * Event handler for the search button
1914 *
1915 * @param v The view that was clicked.
1916 */
1917 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001918 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1919
Amith Yamasania135ba82011-08-09 17:42:01 -07001920 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001921 }
1922
1923 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001924 * Event handler for the voice button
1925 *
1926 * @param v The view that was clicked.
1927 */
1928 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001929 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001930
Michael Jurkaae65ee32012-04-30 11:45:54 -07001931 try {
1932 final SearchManager searchManager =
1933 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1934 ComponentName activityName = searchManager.getGlobalSearchActivity();
1935 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001936 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001937 if (activityName != null) {
1938 intent.setPackage(activityName.getPackageName());
1939 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001940 startActivity(null, intent, "onClickVoiceButton");
Winson Chung01b0b632012-05-22 10:18:14 -07001941 overridePendingTransition(R.anim.fade_in_fast, R.anim.fade_out_fast);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001942 } catch (ActivityNotFoundException e) {
1943 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001944 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001945 startActivitySafely(null, intent, "onClickVoiceButton");
1946 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001947 }
1948
1949 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001950 * Event handler for the "grid" button that appears on the home screen, which
1951 * enters all apps mode.
1952 *
1953 * @param v The view that was clicked.
1954 */
1955 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001956 showAllApps(true);
1957 }
1958
1959 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001960 // Provide the same haptic feedback that the system offers for virtual keys.
1961 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001962 }
1963
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001964 public void onClickAppMarketButton(View v) {
1965 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07001966 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07001967 } else {
1968 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07001969 }
1970 }
1971
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001972 void startApplicationDetailsActivity(ComponentName componentName) {
1973 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001974 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
1975 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001976 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07001977 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07001978 }
1979
Patrick Dubroy5539af72010-09-07 15:22:01 -07001980 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
1981 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
1982 // System applications cannot be installed. For now, show a toast explaining that.
1983 // We may give them the option of disabling apps this way.
1984 int messageId = R.string.uninstall_system_app_text;
1985 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
1986 } else {
1987 String packageName = appInfo.componentName.getPackageName();
1988 String className = appInfo.componentName.getClassName();
1989 Intent intent = new Intent(
1990 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001991 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
1992 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07001993 startActivity(intent);
1994 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07001995 }
1996
Michael Jurka86a720e2012-05-09 11:23:23 -07001997 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07001999
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002001 // Only launch using the new animation if the shortcut has not opted out (this is a
2002 // private contract between launcher and may be ignored in the future).
2003 boolean useLaunchAnimation = (v != null) &&
2004 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2005 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002006 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2007 v.getMeasuredWidth(), v.getMeasuredHeight());
2008
2009 startActivity(intent, opts.toBundle());
2010 } else {
2011 startActivity(intent);
2012 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002013 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002014 } catch (SecurityException e) {
2015 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002016 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002018 "or use the exported attribute for this activity. "
2019 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002021 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002023
Michael Jurka86a720e2012-05-09 11:23:23 -07002024 boolean startActivitySafely(View v, Intent intent, Object tag) {
2025 boolean success = false;
2026 try {
2027 success = startActivity(v, intent, tag);
2028 } catch (ActivityNotFoundException e) {
2029 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2030 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2031 }
2032 return success;
2033 }
2034
Romain Guy8e633c52010-03-04 12:51:36 -08002035 void startActivityForResultSafely(Intent intent, int requestCode) {
2036 try {
2037 startActivityForResult(intent, requestCode);
2038 } catch (ActivityNotFoundException e) {
2039 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2040 } catch (SecurityException e) {
2041 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2042 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2043 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2044 "or use the exported attribute for this activity.", e);
2045 }
2046 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047
Adam Cohena9cf38f2011-05-02 15:36:58 -07002048 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002049 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002050 Folder openFolder = mWorkspace.getFolderForTag(info);
2051
2052 // If the folder info reports that the associated folder is open, then verify that
2053 // it is actually opened. There have been a few instances where this gets out of sync.
2054 if (info.opened && openFolder == null) {
2055 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2056 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2057 info.opened = false;
2058 }
2059
Adam Cohenfb91f302012-06-11 15:45:18 -07002060 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 // Close any open folder
2062 closeFolder();
2063 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002064 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002065 } else {
2066 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 int folderScreen;
2068 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002069 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 // .. and close it
2071 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002072 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 // Close any folder open on the current screen
2074 closeFolder();
2075 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002076 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002077 }
2078 }
2079 }
2080 }
2081
Adam Cohen268c4752012-06-06 17:47:33 -07002082 /**
2083 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2084 * in the DragLayer in the exact absolute location of the original FolderIcon.
2085 */
2086 private void copyFolderIconToImage(FolderIcon fi) {
2087 final int width = fi.getMeasuredWidth();
2088 final int height = fi.getMeasuredHeight();
2089
2090 // Lazy load ImageView, Bitmap and Canvas
2091 if (mFolderIconImageView == null) {
2092 mFolderIconImageView = new ImageView(this);
2093 }
2094 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2095 mFolderIconBitmap.getHeight() != height) {
2096 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2097 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2098 }
2099
2100 DragLayer.LayoutParams lp;
2101 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2102 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2103 } else {
2104 lp = new DragLayer.LayoutParams(width, height);
2105 }
2106
2107 mDragLayer.getViewRectRelativeToSelf(fi, mRectForFolderAnimation);
2108 lp.customPosition = true;
2109 lp.x = mRectForFolderAnimation.left;
2110 lp.y = mRectForFolderAnimation.top;
Adam Cohen8ec23032012-06-08 14:46:22 -07002111 lp.width = width;
2112 lp.height = height;
Adam Cohen268c4752012-06-06 17:47:33 -07002113
2114 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2115 fi.draw(mFolderIconCanvas);
2116 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002117 if (fi.getFolder() != null) {
2118 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2119 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002120 }
Adam Cohen268c4752012-06-06 17:47:33 -07002121 // Just in case this image view is still in the drag layer from a previous animation,
2122 // we remove it and re-add it.
2123 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2124 mDragLayer.removeView(mFolderIconImageView);
2125 }
2126 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002127 if (fi.getFolder() != null) {
2128 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002129 }
Adam Cohen268c4752012-06-06 17:47:33 -07002130 }
2131
Adam Cohen2801caf2011-05-13 20:57:39 -07002132 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002133 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002134 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2135 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2136 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2137
Adam Cohenc51934b2011-07-26 21:07:43 -07002138 FolderInfo info = (FolderInfo) fi.getTag();
2139 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2140 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002141 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2142 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002143 }
2144
Adam Cohen268c4752012-06-06 17:47:33 -07002145 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2146 copyFolderIconToImage(fi);
2147 fi.setVisibility(View.INVISIBLE);
2148
Michael Jurka2ecf9952012-06-18 12:52:28 -07002149 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002150 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002151 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2152 oa.start();
2153 }
2154
Adam Cohen268c4752012-06-06 17:47:33 -07002155 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002156 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002157 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2158 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2159 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2160
Adam Cohen268c4752012-06-06 17:47:33 -07002161 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002162
Adam Cohen268c4752012-06-06 17:47:33 -07002163 // We remove and re-draw the FolderIcon in-case it has changed
2164 mDragLayer.removeView(mFolderIconImageView);
2165 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002166 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002167 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002168 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002169 oa.addListener(new AnimatorListenerAdapter() {
2170 @Override
2171 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002172 if (cl != null) {
2173 cl.clearFolderLeaveBehind();
2174 // Remove the ImageView copy of the FolderIcon and make the original visible.
2175 mDragLayer.removeView(mFolderIconImageView);
2176 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002177 }
2178 }
2179 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002180 oa.start();
2181 }
2182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002183 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002184 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002185 * is animated relative to the specified View. If the View is null, no animation
2186 * is played.
2187 *
2188 * @param folderInfo The FolderInfo describing the folder to open.
2189 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002190 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002191 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002192 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193
Adam Cohena9cf38f2011-05-02 15:36:58 -07002194 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195
Adam Cohen4554ee12011-08-03 16:13:21 -07002196 // Just verify that the folder hasn't already been added to the DragLayer.
2197 // There was a one-off crash where the folder had a parent already.
2198 if (folder.getParent() == null) {
2199 mDragLayer.addView(folder);
2200 mDragController.addDropTarget((DropTarget) folder);
2201 } else {
2202 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2203 folder.getParent() + ").");
2204 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002205 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002206 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002207 }
2208
2209 public void closeFolder() {
2210 Folder folder = mWorkspace.getOpenFolder();
2211 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002212 if (folder.isEditingName()) {
2213 folder.dismissEditingName();
2214 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002215 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002216
2217 // Dismiss the folder cling
2218 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002219 }
2220 }
2221
2222 void closeFolder(Folder folder) {
2223 folder.getInfo().opened = false;
2224
2225 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2226 if (parent != null) {
2227 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2228 shrinkAndFadeInFolderIcon(fi);
2229 }
2230 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231 }
2232
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002234 if (!isDraggingEnabled()) return false;
2235 if (isWorkspaceLocked()) return false;
2236 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002237
2238 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002239 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240 }
2241
Michael Jurka0280c3b2010-09-17 15:00:07 -07002242 resetAddInfo();
2243 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002245 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002246 return true;
2247 }
2248
Winson Chung3d503fb2011-07-13 17:25:49 -07002249 // The hotseat touch handling does not go through Workspace, and we always allow long press
2250 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002251 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002252 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2253 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002254 if (itemUnderLongClick == null) {
2255 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2257 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002258 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002259 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002260 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002261 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002262 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002263 }
2264 }
2265 }
2266 return true;
2267 }
2268
Winson Chung3d503fb2011-07-13 17:25:49 -07002269 boolean isHotseatLayout(View layout) {
2270 return mHotseat != null && layout != null &&
2271 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2272 }
2273 Hotseat getHotseat() {
2274 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002275 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002276 SearchDropTargetBar getSearchBar() {
2277 return mSearchDropTargetBar;
2278 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002279
Winson Chung3d503fb2011-07-13 17:25:49 -07002280 /**
2281 * Returns the CellLayout of the specified container at the specified screen.
2282 */
2283 CellLayout getCellLayout(long container, int screen) {
2284 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2285 if (mHotseat != null) {
2286 return mHotseat.getLayout();
2287 } else {
2288 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002289 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002290 } else {
2291 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002292 }
2293 }
2294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002295 Workspace getWorkspace() {
2296 return mWorkspace;
2297 }
2298
Daniel Sandler843e8602010-06-07 14:59:01 -04002299 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2300 public boolean isAllAppsVisible() {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002301 return (mState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002302 }
2303
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002304 public boolean isAllAppsButtonRank(int rank) {
2305 return mHotseat.isAllAppsButtonRank(rank);
2306 }
2307
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002308 /**
2309 * Helper method for the cameraZoomIn/cameraZoomOut animations
2310 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002311 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002312 * @param scaleFactor The scale factor used for the zoom
2313 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002314 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002315 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002316 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002317 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002318
Winson Chung18f41f82012-05-09 13:28:10 -07002319 void disableWallpaperIfInAllApps() {
2320 // Only disable it if we are in all apps
2321 if (mState == State.APPS_CUSTOMIZE) {
2322 if (mAppsCustomizeTabHost != null &&
2323 !mAppsCustomizeTabHost.isTransitioning()) {
2324 updateWallpaperVisibility(false);
2325 }
2326 }
2327 }
2328
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002329 void updateWallpaperVisibility(boolean visible) {
2330 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2331 int curflags = getWindow().getAttributes().flags
2332 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2333 if (wpflags != curflags) {
2334 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2335 }
2336 }
2337
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002338 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2339 if (v instanceof LauncherTransitionable) {
2340 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2341 }
2342 }
2343
Michael Jurkabed61d22012-02-14 22:51:29 -08002344 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2345 if (v instanceof LauncherTransitionable) {
2346 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2347 }
Winson Chung70442722012-02-10 15:43:22 -08002348
2349 // Update the workspace transition step as well
2350 dispatchOnLauncherTransitionStep(v, 0f);
2351 }
2352
2353 private void dispatchOnLauncherTransitionStep(View v, float t) {
2354 if (v instanceof LauncherTransitionable) {
2355 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2356 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002357 }
2358
2359 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2360 if (v instanceof LauncherTransitionable) {
2361 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2362 }
Winson Chung70442722012-02-10 15:43:22 -08002363
2364 // Update the workspace transition step as well
2365 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002366 }
2367
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002368 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002369 * Things to test when changing the following seven functions.
2370 * - Home from workspace
2371 * - from center screen
2372 * - from other screens
2373 * - Home from all apps
2374 * - from center screen
2375 * - from other screens
2376 * - Back from all apps
2377 * - from center screen
2378 * - from other screens
2379 * - Launch app from workspace and quit
2380 * - with back
2381 * - with home
2382 * - Launch app from all apps and quit
2383 * - with back
2384 * - with home
2385 * - Go to a screen that's not the default, then all
2386 * apps, and launch and app, and go back
2387 * - with back
2388 * -with home
2389 * - On workspace, long press power and go back
2390 * - with back
2391 * - with home
2392 * - On all apps, long press power and go back
2393 * - with back
2394 * - with home
2395 * - On workspace, power off
2396 * - On all apps, power off
2397 * - Launch an app and turn off the screen while in that app
2398 * - Go back with home key
2399 * - Go back with back key TODO: make this not go to workspace
2400 * - From all apps
2401 * - From workspace
2402 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2403 * - From all apps
2404 * - From the center workspace
2405 * - From another workspace
2406 */
2407
2408 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002409 * Zoom the camera out from the workspace to reveal 'toView'.
2410 * Assumes that the view to show is anchored at either the very top or very bottom
2411 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002412 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002413 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002414 if (mStateAnimation != null) {
2415 mStateAnimation.cancel();
2416 mStateAnimation = null;
2417 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002418 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002419
Winson Chungf0ea4d32011-06-06 14:27:16 -07002420 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2421 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2422 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002423 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002424 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002425 final int startDelay =
2426 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002427
Michael Jurkab3e22d92011-10-31 15:58:33 -07002428 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002429
Michael Jurkad74c9842011-07-10 12:44:21 -07002430 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002431 Animator workspaceAnim =
2432 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002433
2434 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002435 toView.setScaleX(scale);
2436 toView.setScaleY(scale);
2437 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2438 scaleAnim.
2439 scaleX(1f).scaleY(1f).
2440 setDuration(duration).
2441 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002442
Winson Chungf0ea4d32011-06-06 14:27:16 -07002443 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002444 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002445 final ObjectAnimator alphaAnim = ObjectAnimator
2446 .ofFloat(toView, "alpha", 0f, 1f)
2447 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002448 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002449 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2450 @Override
2451 public void onAnimationUpdate(ValueAnimator animation) {
2452 float t = (Float) animation.getAnimatedValue();
2453 dispatchOnLauncherTransitionStep(fromView, t);
2454 dispatchOnLauncherTransitionStep(toView, t);
2455 }
2456 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002457
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002458 // toView should appear right at the end of the workspace shrink
2459 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002460 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002461 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002462 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002463
2464 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002465 boolean animationCancelled = false;
2466
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002467 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002468 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002469 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002470 // Prepare the position
2471 toView.setTranslationX(0.0f);
2472 toView.setTranslationY(0.0f);
2473 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002474 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002475 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002476 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002477 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002478 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2479 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002480
2481 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2482 // Hide the workspace scrollbar
2483 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002484 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002485 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002486 if (!animationCancelled) {
2487 updateWallpaperVisibility(false);
2488 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002489
2490 // Hide the search bar
2491 mSearchDropTargetBar.hideSearchBar(false);
Adam Cohenf4ddea32011-09-12 13:46:42 -07002492 }
2493
Adam Cohencff6af82011-09-13 14:51:53 -07002494 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002495 public void onAnimationCancel(Animator animation) {
2496 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002497 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002498 });
2499
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002500 if (workspaceAnim != null) {
2501 mStateAnimation.play(workspaceAnim);
2502 }
Michael Jurka1899a362011-11-03 13:50:45 -07002503
2504 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002505 final ViewTreeObserver observer;
2506
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002507 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2508 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002509
2510 // If any of the objects being animated haven't been measured/laid out
2511 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002512 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002513 (mWorkspace.getMeasuredWidth() == 0) ||
2514 (toView.getMeasuredWidth() == 0)) {
2515 observer = mWorkspace.getViewTreeObserver();
2516 delayAnim = true;
2517 } else {
2518 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002519 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002520
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002521 final AnimatorSet stateAnimation = mStateAnimation;
2522 final Runnable startAnimRunnable = new Runnable() {
2523 public void run() {
2524 // Check that mStateAnimation hasn't changed while
2525 // we waited for a layout/draw pass
2526 if (mStateAnimation != stateAnimation)
2527 return;
2528 setPivotsForZoom(toView, scale);
2529 dispatchOnLauncherTransitionStart(fromView, animated, false);
2530 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka382aa182012-06-11 15:42:07 -07002531 toView.post(new Runnable() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002532 public void run() {
2533 // Check that mStateAnimation hasn't changed while
2534 // we waited for a layout/draw pass
2535 if (mStateAnimation != stateAnimation)
2536 return;
2537 mStateAnimation.start();
2538 }
2539 });
2540 }
2541 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002542 if (delayAnim) {
2543 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2544 public void onGlobalLayout() {
Michael Jurka382aa182012-06-11 15:42:07 -07002545 toView.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002546 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002547 }
2548 };
2549 observer.addOnGlobalLayoutListener(delayedStart);
2550 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002551 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002552 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002553 } else {
2554 toView.setTranslationX(0.0f);
2555 toView.setTranslationY(0.0f);
2556 toView.setScaleX(1.0f);
2557 toView.setScaleY(1.0f);
2558 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002559 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002560
Michael Jurkabed61d22012-02-14 22:51:29 -08002561 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2562 // Hide the workspace scrollbar
2563 mWorkspace.hideScrollingIndicator(true);
2564 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002565
2566 // Hide the search bar
2567 mSearchDropTargetBar.hideSearchBar(false);
Michael Jurkaabded662011-03-04 12:06:57 -08002568 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002569 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002570 dispatchOnLauncherTransitionStart(fromView, animated, false);
2571 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002572 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002573 dispatchOnLauncherTransitionStart(toView, animated, false);
2574 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002575 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002576 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002577 }
2578
2579 /**
2580 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002581 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002582 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002583 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002584 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2585 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002586
Michael Jurkab3e22d92011-10-31 15:58:33 -07002587 if (mStateAnimation != null) {
2588 mStateAnimation.cancel();
2589 mStateAnimation = null;
2590 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002591 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002592
Winson Chungf0ea4d32011-06-06 14:27:16 -07002593 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002594 final int fadeOutDuration =
2595 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002596 final float scaleFactor = (float)
2597 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2598 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002599 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002600 Animator workspaceAnim = null;
2601
2602 if (toState == State.WORKSPACE) {
2603 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2604 workspaceAnim = mWorkspace.getChangeStateAnimation(
2605 Workspace.State.NORMAL, animated, stagger);
2606 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2607 workspaceAnim = mWorkspace.getChangeStateAnimation(
2608 Workspace.State.SPRING_LOADED, animated);
2609 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002610
Michael Jurkab3e22d92011-10-31 15:58:33 -07002611 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002612 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002613 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002614 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002615 final LauncherViewPropertyAnimator scaleAnim =
2616 new LauncherViewPropertyAnimator(fromView);
2617 scaleAnim.
2618 scaleX(scaleFactor).scaleY(scaleFactor).
2619 setDuration(duration).
2620 setInterpolator(new Workspace.ZoomInInterpolator());
2621
2622 final ObjectAnimator alphaAnim = ObjectAnimator
2623 .ofFloat(fromView, "alpha", 1f, 0f)
2624 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002625 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002626 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2627 @Override
2628 public void onAnimationUpdate(ValueAnimator animation) {
2629 float t = 1f - (Float) animation.getAnimatedValue();
2630 dispatchOnLauncherTransitionStep(fromView, t);
2631 dispatchOnLauncherTransitionStep(toView, t);
2632 }
2633 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002634
Michael Jurka2ecf9952012-06-18 12:52:28 -07002635 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002636
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002637 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2638 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002639
2640 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002641 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002642 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002643 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002644 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002645 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2646 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002647 if (mWorkspace != null) {
2648 mWorkspace.hideScrollingIndicator(false);
2649 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002650 if (onCompleteRunnable != null) {
2651 onCompleteRunnable.run();
2652 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002653 }
2654 });
2655
Winson Chungf0ea4d32011-06-06 14:27:16 -07002656 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002657 if (workspaceAnim != null) {
2658 mStateAnimation.play(workspaceAnim);
2659 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002660 dispatchOnLauncherTransitionStart(fromView, animated, true);
2661 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002662 final Animator stateAnimation = mStateAnimation;
2663 mWorkspace.post(new Runnable() {
2664 public void run() {
2665 if (stateAnimation != mStateAnimation)
2666 return;
2667 mStateAnimation.start();
2668 }
2669 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002670 } else {
2671 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002672 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002673 dispatchOnLauncherTransitionStart(fromView, animated, true);
2674 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002675 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002676 dispatchOnLauncherTransitionStart(toView, animated, true);
2677 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002678 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002679 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002680 }
2681
Michael Jurkae326f182011-11-21 14:05:46 -08002682 @Override
2683 public void onTrimMemory(int level) {
2684 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002685 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002686 mAppsCustomizeTabHost.onTrimMemory();
2687 }
2688 }
2689
Winson Chung18f41f82012-05-09 13:28:10 -07002690 @Override
2691 public void onWindowFocusChanged(boolean hasFocus) {
2692 if (!hasFocus) {
2693 // When another window occludes launcher (like the notification shade, or recents),
2694 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2695 updateWallpaperVisibility(true);
2696 } else {
2697 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002698 mWorkspace.postDelayed(new Runnable() {
2699 @Override
2700 public void run() {
2701 disableWallpaperIfInAllApps();
2702 }
2703 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002704 }
2705 }
2706
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002707 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002708 showWorkspace(animated, null);
2709 }
2710
2711 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002712 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002713 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002714 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002715 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002716
Winson Chungc7d2b602012-05-16 17:02:20 -07002717 // Show the search bar (only animate if we were showing the drop target bar in spring
2718 // loaded mode)
2719 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2720
Michael Jurkab737ee62011-11-15 15:57:22 -08002721 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002722 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002723
2724 // Set focus to the AppsCustomize button
2725 if (mAllAppsButton != null) {
2726 mAllAppsButton.requestFocus();
2727 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002728 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002729
Michael Jurkab737ee62011-11-15 15:57:22 -08002730 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002731
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002732 // Change the state *after* we've called all the transition code
2733 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002734
Winson Chung5fb63472011-02-02 17:03:37 -08002735 // Resume the auto-advance of widgets
2736 mUserPresent = true;
2737 updateRunning();
2738
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002739 // send an accessibility event to announce the context change
2740 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002741 }
2742
Michael Jurkab3e22d92011-10-31 15:58:33 -07002743 void showAllApps(boolean animated) {
2744 if (mState != State.WORKSPACE) return;
2745
2746 showAppsCustomizeHelper(animated, false);
2747 mAppsCustomizeTabHost.requestFocus();
2748
Michael Jurkab3e22d92011-10-31 15:58:33 -07002749 // Change the state *after* we've called all the transition code
2750 mState = State.APPS_CUSTOMIZE;
2751
2752 // Pause the auto-advance of widgets until we are out of AllApps
2753 mUserPresent = false;
2754 updateRunning();
2755 closeFolder();
2756
2757 // Send an accessibility event to announce the context change
2758 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2759 }
2760
Adam Cohen7777d962011-08-18 18:58:38 -07002761 void enterSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002762 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002763 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002764 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002765 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002766 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002767 }
Adam Cohen7777d962011-08-18 18:58:38 -07002768
Adam Cohened66b2b2012-01-23 17:28:51 -08002769 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2770 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002771 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2772
Winson Chunge7a03942011-08-05 15:05:12 -07002773 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002774 @Override
2775 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002776 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002777 // Before we show workspace, hide all apps again because
2778 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2779 // clean up our state transition functions
2780 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002781 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002782 } else {
2783 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002784 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002785 }
2786 }, (extendedDelay ?
2787 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2788 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2789 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002790
Michael Jurkad3ef3062010-11-23 16:23:58 -08002791 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002792 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002793 final boolean animated = true;
2794 final boolean springLoaded = true;
2795 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002796 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002797 }
2798 // Otherwise, we are not in spring loaded mode, so don't do anything.
2799 }
2800
Michael Jurkab737ee62011-11-15 15:57:22 -08002801 void hideDockDivider() {
2802 if (mQsbDivider != null && mDockDivider != null) {
2803 mQsbDivider.setVisibility(View.INVISIBLE);
2804 mDockDivider.setVisibility(View.INVISIBLE);
2805 }
2806 }
2807
2808 void showDockDivider(boolean animated) {
2809 if (mQsbDivider != null && mDockDivider != null) {
2810 mQsbDivider.setVisibility(View.VISIBLE);
2811 mDockDivider.setVisibility(View.VISIBLE);
2812 if (mDividerAnimator != null) {
2813 mDividerAnimator.cancel();
2814 mQsbDivider.setAlpha(1f);
2815 mDockDivider.setAlpha(1f);
2816 mDividerAnimator = null;
2817 }
2818 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002819 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2820 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2821 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Michael Jurkab737ee62011-11-15 15:57:22 -08002822 mDividerAnimator.setDuration(mSearchDropTargetBar.getTransitionInDuration());
2823 mDividerAnimator.start();
2824 }
2825 }
2826 }
2827
Michael Jurkab3e22d92011-10-31 15:58:33 -07002828 void lockAllApps() {
2829 // TODO
2830 }
2831
2832 void unlockAllApps() {
2833 // TODO
2834 }
2835
Adam Cohenfc53cd22011-07-20 15:45:11 -07002836 public boolean isAllAppsCustomizeOpen() {
2837 return mState == State.APPS_CUSTOMIZE;
2838 }
2839
Winson Chungf0ea4d32011-06-06 14:27:16 -07002840 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002841 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002842 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002843 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002844 if (!LauncherApplication.isScreenLarge()) {
2845 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002846 if (mHotseat.getAlpha() != 1f) {
2847 int duration = mSearchDropTargetBar.getTransitionInDuration();
2848 mHotseat.animate().alpha(1f).setDuration(duration);
2849 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002850 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002851 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002852 }
2853 }
2854 }
2855
2856 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002857 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002858 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002859 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002860 if (!LauncherApplication.isScreenLarge()) {
2861 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002862 if (mHotseat.getAlpha() != 0f) {
2863 int duration = mSearchDropTargetBar.getTransitionOutDuration();
2864 mHotseat.animate().alpha(0f).setDuration(duration);
2865 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002866 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002867 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002868 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002869 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002870 }
2871
Patrick Dubroy5f445422011-02-18 14:35:21 -08002872 /**
2873 * Add an item from all apps or customize onto the given workspace screen.
2874 * If layout is null, add to the current screen.
2875 */
2876 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002877 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002878 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002879 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002880 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002881
Winson Chungdff8ebb2011-09-08 17:25:31 -07002882 /** Maps the current orientation to an index for referencing orientation correct global icons */
2883 private int getCurrentOrientationIndexForGlobalIcons() {
2884 // default - 0, landscape - 1
2885 switch (getResources().getConfiguration().orientation) {
2886 case Configuration.ORIENTATION_LANDSCAPE:
2887 return 1;
2888 default:
2889 return 0;
2890 }
2891 }
2892
Michael Jurkaae65ee32012-04-30 11:45:54 -07002893 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002894 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002895 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002896 // Look for the toolbar icon specified in the activity meta-data
2897 Bundle metaData = packageManager.getActivityInfo(
2898 activityName, PackageManager.GET_META_DATA).metaData;
2899 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002900 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002901 if (iconResId != 0) {
2902 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002903 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002904 }
2905 }
2906 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002907 // This can happen if the activity defines an invalid drawable
2908 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2909 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002910 } catch (Resources.NotFoundException nfe) {
2911 // This can happen if the activity defines an invalid drawable
2912 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2913 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002914 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002915 return null;
2916 }
2917
2918 // if successful in getting icon, return it; otherwise, set button to use default drawable
2919 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002920 int buttonId, ComponentName activityName, int fallbackDrawableId,
2921 String toolbarResourceName) {
2922 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002923 Resources r = getResources();
2924 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2925 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002926
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002927 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002928 // If we were unable to find the icon via the meta-data, use a generic one
2929 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002930 toolbarIcon = r.getDrawable(fallbackDrawableId);
2931 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002932 if (button != null) {
2933 button.setCompoundDrawables(toolbarIcon, null, null, null);
2934 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002935 return null;
2936 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002937 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002938 if (button != null) {
2939 button.setCompoundDrawables(toolbarIcon, null, null, null);
2940 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002941 return toolbarIcon.getConstantState();
2942 }
2943 }
2944
2945 // if successful in getting icon, return it; otherwise, set button to use default drawable
2946 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002947 int buttonId, ComponentName activityName, int fallbackDrawableId,
2948 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002949 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002950 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002951
Michael Jurka19e0fc52011-07-22 18:00:21 -07002952 if (button != null) {
2953 // If we were unable to find the icon via the meta-data, use a
2954 // generic one
2955 if (toolbarIcon == null) {
2956 button.setImageResource(fallbackDrawableId);
2957 } else {
2958 button.setImageDrawable(toolbarIcon);
2959 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07002960 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07002961
2962 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
2963
Michael Jurka0423dcf2010-10-05 14:56:18 -07002964 }
2965
Winson Chung1b884092012-06-08 17:13:02 -07002966 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002967 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07002968 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002969 }
2970
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002971 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08002972 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08002973 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08002974 }
2975
Winson Chungbb185bd2011-11-21 12:31:42 -08002976 private void invalidatePressedFocusedStates(View container, View button) {
2977 if (container instanceof HolographicLinearLayout) {
2978 HolographicLinearLayout layout = (HolographicLinearLayout) container;
2979 layout.invalidatePressedFocusedStates();
2980 } else if (button instanceof HolographicImageView) {
2981 HolographicImageView view = (HolographicImageView) button;
2982 view.invalidatePressedFocusedStates();
2983 }
2984 }
2985
Winson Chungc51db6a2011-10-05 11:44:49 -07002986 private boolean updateGlobalSearchIcon() {
2987 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07002988 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
2989 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07002990 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07002991 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07002992 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07002993
Winson Chung1cad91e2011-05-25 17:41:01 -07002994 final SearchManager searchManager =
2995 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2996 ComponentName activityName = searchManager.getGlobalSearchActivity();
2997 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07002998 int coi = getCurrentOrientationIndexForGlobalIcons();
2999 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003000 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3001 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3002 if (sGlobalSearchIcon[coi] == null) {
3003 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3004 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3005 TOOLBAR_ICON_METADATA_NAME);
3006 }
3007
Winson Chung649723c2011-07-06 20:41:23 -07003008 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003009 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3010 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003011 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003012 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003013 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003014 // We disable both search and voice search when there is no global search provider
Winson Chung649723c2011-07-06 20:41:23 -07003015 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003016 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3017 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3018 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003019 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003020 if (voiceButtonProxy != null) {
3021 voiceButtonProxy.setVisibility(View.GONE);
3022 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003023 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003024 }
3025 }
3026
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003027 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003028 final View searchButtonContainer = findViewById(R.id.search_button_container);
3029 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003030 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003031 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003032 }
3033
Winson Chungc51db6a2011-10-05 11:44:49 -07003034 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chung1cad91e2011-05-25 17:41:01 -07003035 final View searchDivider = findViewById(R.id.search_divider);
Winson Chungc51db6a2011-10-05 11:44:49 -07003036 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003037 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003038 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003039
Winson Chungc51db6a2011-10-05 11:44:49 -07003040 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003041 final SearchManager searchManager =
3042 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3043 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3044
3045 ComponentName activityName = null;
3046 if (globalSearchActivity != null) {
3047 // Check if the global search activity handles voice search
3048 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3049 intent.setPackage(globalSearchActivity.getPackageName());
3050 activityName = intent.resolveActivity(getPackageManager());
3051 }
3052
3053 if (activityName == null) {
3054 // Fallback: check if an activity other than the global search activity
3055 // resolves this
3056 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3057 activityName = intent.resolveActivity(getPackageManager());
3058 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003059 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003060 int coi = getCurrentOrientationIndexForGlobalIcons();
3061 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003062 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3063 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3064 if (sVoiceSearchIcon[coi] == null) {
3065 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3066 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3067 TOOLBAR_ICON_METADATA_NAME);
3068 }
Winson Chung649723c2011-07-06 20:41:23 -07003069 if (searchDivider != null) searchDivider.setVisibility(View.VISIBLE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003070 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003071 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003072 if (voiceButtonProxy != null) {
3073 voiceButtonProxy.setVisibility(View.VISIBLE);
3074 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003075 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003076 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003077 } else {
Winson Chung649723c2011-07-06 20:41:23 -07003078 if (searchDivider != null) searchDivider.setVisibility(View.GONE);
Winson Chungc51db6a2011-10-05 11:44:49 -07003079 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003080 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003081 if (voiceButtonProxy != null) {
3082 voiceButtonProxy.setVisibility(View.GONE);
3083 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003084 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003085 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003086 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003087
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003088 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003089 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3090 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003091 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003092 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003093 }
3094
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003095 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003096 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003097 */
3098 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003099 final View marketButton = findViewById(R.id.market_button);
3100 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3101 // Find the app market activity by resolving an intent.
3102 // (If multiple app markets are installed, it will return the ResolverActivity.)
3103 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003104 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003105 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003106 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003107 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003108 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3109 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003110 marketButton.setVisibility(View.VISIBLE);
3111 } else {
3112 // We should hide and disable the view so that we don't try and restore the visibility
3113 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3114 marketButton.setVisibility(View.GONE);
3115 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003116 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003117 }
3118
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003119 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003120 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3121 Resources r = getResources();
3122 Drawable marketIconDrawable = d.newDrawable(r);
3123 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3124 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3125 marketIconDrawable.setBounds(0, 0, w, h);
3126
3127 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003128 }
3129
Michael Jurkad7c28052012-04-27 15:43:36 -07003130 @Override
3131 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3132 boolean result = super.dispatchPopulateAccessibilityEvent(event);
3133 final List<CharSequence> text = event.getText();
3134 text.clear();
3135 text.add(getString(R.string.home));
3136 return result;
3137 }
3138
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003139 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003140 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003141 */
3142 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3143 @Override
3144 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003145 closeSystemDialogs();
3146 }
3147 }
3148
3149 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003150 * Receives notifications whenever the appwidgets are reset.
3151 */
3152 private class AppWidgetResetObserver extends ContentObserver {
3153 public AppWidgetResetObserver() {
3154 super(new Handler());
3155 }
3156
3157 @Override
3158 public void onChange(boolean selfChange) {
3159 onAppWidgetReset();
3160 }
3161 }
3162
3163 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003164 * If the activity is currently paused, signal that we need to re-run the loader
3165 * in onResume.
3166 *
3167 * This needs to be called from incoming places where resources might have been loaded
3168 * while we are paused. That is becaues the Configuration might be wrong
3169 * when we're not running, and if it comes back to what it was when we
3170 * were paused, we are not restarted.
3171 *
3172 * Implementation of the method from LauncherModel.Callbacks.
3173 *
3174 * @return true if we are currently paused. The caller might be able to
3175 * skip some work in that case since we will come back again.
3176 */
3177 public boolean setLoadOnResume() {
3178 if (mPaused) {
3179 Log.i(TAG, "setLoadOnResume");
3180 mOnResumeNeedsLoad = true;
3181 return true;
3182 } else {
3183 return false;
3184 }
3185 }
3186
3187 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003188 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003189 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003190 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003191 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003192 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003193 } else {
3194 return SCREEN_COUNT / 2;
3195 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003196 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003197
Joe Onorato9c1289c2009-08-17 11:03:03 -04003198 /**
3199 * Refreshes the shortcuts shown on the workspace.
3200 *
3201 * Implementation of the method from LauncherModel.Callbacks.
3202 */
3203 public void startBinding() {
3204 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003205
Winson Chungf0c6ae02012-03-21 16:10:31 -07003206 mNewShortcutAnimatePage = -1;
3207 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003208 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003209 int count = workspace.getChildCount();
3210 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003211 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003212 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3213 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003214 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003215 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003216 if (mHotseat != null) {
3217 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003218 }
3219 }
3220
3221 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003222 * Bind the items start-end from the list.
3223 *
3224 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003226 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003227 setLoadOnResume();
3228
Winson Chungf0c6ae02012-03-21 16:10:31 -07003229 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3230 Set<String> newApps = new HashSet<String>();
3231 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3232
3233 Workspace workspace = mWorkspace;
3234 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003235 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003236
3237 // Short circuit if we are loading dock items for a configuration which has no dock
3238 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3239 mHotseat == null) {
3240 continue;
3241 }
3242
Joe Onorato9c1289c2009-08-17 11:03:03 -04003243 switch (item.itemType) {
3244 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3245 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003246 ShortcutInfo info = (ShortcutInfo) item;
3247 String uri = info.intent.toUri(0).toString();
3248 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003249 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3250 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003251 boolean animateIconUp = false;
3252 synchronized (newApps) {
3253 if (newApps.contains(uri)) {
3254 animateIconUp = newApps.remove(uri);
3255 }
3256 }
3257 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003258 // Prepare the view to be animated up
3259 shortcut.setAlpha(0f);
3260 shortcut.setScaleX(0f);
3261 shortcut.setScaleY(0f);
3262 mNewShortcutAnimatePage = item.screen;
3263 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3264 mNewShortcutAnimateViews.add(shortcut);
3265 }
3266 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003267 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003268 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003269 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003270 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003271 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003272 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3273 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003274 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003275 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003276 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003277
Joe Onorato9c1289c2009-08-17 11:03:03 -04003278 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003279 }
3280
Joe Onorato9c1289c2009-08-17 11:03:03 -04003281 /**
3282 * Implementation of the method from LauncherModel.Callbacks.
3283 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003284 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003285 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003286 sFolders.clear();
3287 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003288 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003289
3290 /**
3291 * Add the views for a widget to the workspace.
3292 *
3293 * Implementation of the method from LauncherModel.Callbacks.
3294 */
3295 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003296 setLoadOnResume();
3297
Daniel Sandler843e8602010-06-07 14:59:01 -04003298 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3299 if (DEBUG_WIDGETS) {
3300 Log.d(TAG, "bindAppWidget: " + item);
3301 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003302 final Workspace workspace = mWorkspace;
3303
3304 final int appWidgetId = item.appWidgetId;
3305 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003306 if (DEBUG_WIDGETS) {
3307 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3308 }
3309
Joe Onorato9c1289c2009-08-17 11:03:03 -04003310 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3311
Joe Onorato9c1289c2009-08-17 11:03:03 -04003312 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003313 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003314
Winson Chung3d503fb2011-07-13 17:25:49 -07003315 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003316 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003317 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3318
Joe Onorato9c1289c2009-08-17 11:03:03 -04003319 workspace.requestLayout();
3320
Daniel Sandler843e8602010-06-07 14:59:01 -04003321 if (DEBUG_WIDGETS) {
3322 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3323 + (SystemClock.uptimeMillis()-start) + "ms");
3324 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003325 }
3326
3327 /**
3328 * Callback saying that there aren't any more items to bind.
3329 *
3330 * Implementation of the method from LauncherModel.Callbacks.
3331 */
3332 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003333 setLoadOnResume();
3334
Joe Onorato9c1289c2009-08-17 11:03:03 -04003335 if (mSavedState != null) {
3336 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003337 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003339 mSavedState = null;
3340 }
3341
3342 if (mSavedInstanceState != null) {
3343 super.onRestoreInstanceState(mSavedInstanceState);
3344 mSavedInstanceState = null;
3345 }
3346
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003347 // If we received the result of any pending adds while the loader was running (e.g. the
3348 // widget configuration forced an orientation change), process them now.
3349 for (int i = 0; i < sPendingAddList.size(); i++) {
3350 completeAdd(sPendingAddList.get(i));
3351 }
3352 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003353
Winson Chungc51db6a2011-10-05 11:44:49 -07003354 // Update the market app icon as necessary (the other icons will be managed in response to
3355 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003356 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003357
Winson Chungf0c6ae02012-03-21 16:10:31 -07003358 // Animate up any icons as necessary
3359 if (mVisible || mWorkspaceLoading) {
3360 Runnable newAppsRunnable = new Runnable() {
3361 @Override
3362 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003363 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003364 }
3365 };
Winson Chunga2413752012-04-03 14:22:34 -07003366
3367 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3368 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3369 if (canRunNewAppsAnimation()) {
3370 // If the user has not interacted recently, then either snap to the new page to show
3371 // the new-apps animation or just run them if they are to appear on the current page
3372 if (willSnapPage) {
3373 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3374 } else {
3375 runNewAppsAnimation(false);
3376 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003377 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003378 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003379 // are on another page (or just normally if they are added to the current page)
3380 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003381 }
3382 }
3383
3384 mWorkspaceLoading = false;
3385 }
3386
Winson Chunga2413752012-04-03 14:22:34 -07003387 private boolean canRunNewAppsAnimation() {
3388 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3389 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3390 }
3391
Winson Chungf0c6ae02012-03-21 16:10:31 -07003392 /**
3393 * Runs a new animation that scales up icons that were added while Launcher was in the
3394 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003395 *
3396 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003397 */
Winson Chunga2413752012-04-03 14:22:34 -07003398 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003399 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003400 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003401
3402 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003403 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3404 @Override
3405 public int compare(View a, View b) {
3406 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3407 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3408 int cellCountX = LauncherModel.getCellCountX();
3409 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3410 }
3411 });
Winson Chunga2413752012-04-03 14:22:34 -07003412
3413 // Animate each of the views in place (or show them immediately if requested)
3414 if (immediate) {
3415 for (View v : mNewShortcutAnimateViews) {
3416 v.setAlpha(1f);
3417 v.setScaleX(1f);
3418 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003419 }
Winson Chunga2413752012-04-03 14:22:34 -07003420 } else {
3421 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3422 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003423 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003424 PropertyValuesHolder.ofFloat("alpha", 1f),
3425 PropertyValuesHolder.ofFloat("scaleX", 1f),
3426 PropertyValuesHolder.ofFloat("scaleY", 1f));
3427 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3428 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3429 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3430 bounceAnims.add(bounceAnim);
3431 }
3432 anim.playTogether(bounceAnims);
3433 anim.addListener(new AnimatorListenerAdapter() {
3434 @Override
3435 public void onAnimationEnd(Animator animation) {
3436 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3437 }
3438 });
3439 anim.start();
3440 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003441
3442 // Clean up
3443 mNewShortcutAnimatePage = -1;
3444 mNewShortcutAnimateViews.clear();
3445 new Thread("clearNewAppsThread") {
3446 public void run() {
3447 mSharedPrefs.edit()
3448 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3449 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3450 .commit();
3451 }
3452 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003453 }
3454
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003455 @Override
3456 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003457 boolean searchVisible = updateGlobalSearchIcon();
3458 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
3459 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003460 }
3461
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003462 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003463 * Add the icons for all apps.
3464 *
3465 * Implementation of the method from LauncherModel.Callbacks.
3466 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003467 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
3468 // Remove the progress bar entirely; we could also make it GONE
3469 // but better to remove it since we know it's not going to be used
3470 View progressBar = mAppsCustomizeTabHost.
3471 findViewById(R.id.apps_customize_progress_bar);
3472 if (progressBar != null) {
3473 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chung785d2eb2011-04-14 16:08:02 -07003474 }
Michael Jurkac57b7a82011-08-09 22:02:20 -07003475 // We just post the call to setApps so the user sees the progress bar
3476 // disappear-- otherwise, it just looks like the progress bar froze
3477 // which doesn't look great
3478 mAppsCustomizeTabHost.post(new Runnable() {
3479 public void run() {
3480 if (mAppsCustomizeContent != null) {
3481 mAppsCustomizeContent.setApps(apps);
3482 }
3483 }
3484 });
Joe Onorato9c1289c2009-08-17 11:03:03 -04003485 }
3486
3487 /**
3488 * A package was installed.
3489 *
3490 * Implementation of the method from LauncherModel.Callbacks.
3491 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003492 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003493 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003494
Winson Chung785d2eb2011-04-14 16:08:02 -07003495 if (mAppsCustomizeContent != null) {
3496 mAppsCustomizeContent.addApps(apps);
3497 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003498 }
3499
3500 /**
3501 * A package was updated.
3502 *
3503 * Implementation of the method from LauncherModel.Callbacks.
3504 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003505 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003506 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003507 if (mWorkspace != null) {
3508 mWorkspace.updateShortcuts(apps);
3509 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003510
Winson Chung785d2eb2011-04-14 16:08:02 -07003511 if (mAppsCustomizeContent != null) {
3512 mAppsCustomizeContent.updateApps(apps);
3513 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003514 }
3515
3516 /**
3517 * A package was uninstalled.
3518 *
3519 * Implementation of the method from LauncherModel.Callbacks.
3520 */
Winson Chungcd810732012-06-18 16:45:43 -07003521 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003522 if (permanent) {
Winson Chungcd810732012-06-18 16:45:43 -07003523 mWorkspace.removeItems(packageNames);
Joe Onorato36115782010-06-17 13:28:48 -04003524 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003525
Winson Chung785d2eb2011-04-14 16:08:02 -07003526 if (mAppsCustomizeContent != null) {
Winson Chungcd810732012-06-18 16:45:43 -07003527 mAppsCustomizeContent.removeApps(packageNames);
Winson Chung785d2eb2011-04-14 16:08:02 -07003528 }
Winson Chunga1820962011-10-03 16:31:06 -07003529
3530 // Notify the drag controller
Winson Chungcd810732012-06-18 16:45:43 -07003531 mDragController.onAppsRemoved(packageNames, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003532 }
Joe Onoratobe386092009-11-17 17:32:16 -08003533
3534 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003535 * A number of packages were updated.
3536 */
3537 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003538 if (mAppsCustomizeContent != null) {
3539 mAppsCustomizeContent.onPackagesUpdated();
3540 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003541 }
3542
Winson Chung400438b2011-01-16 17:53:48 -08003543 private int mapConfigurationOriActivityInfoOri(int configOri) {
3544 final Display d = getWindowManager().getDefaultDisplay();
3545 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3546 switch (d.getRotation()) {
3547 case Surface.ROTATION_0:
3548 case Surface.ROTATION_180:
3549 // We are currently in the same basic orientation as the natural orientation
3550 naturalOri = configOri;
3551 break;
3552 case Surface.ROTATION_90:
3553 case Surface.ROTATION_270:
3554 // We are currently in the other basic orientation to the natural orientation
3555 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3556 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3557 break;
3558 }
3559
3560 int[] oriMap = {
3561 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3562 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3563 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3564 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3565 };
3566 // Since the map starts at portrait, we need to offset if this device's natural orientation
3567 // is landscape.
3568 int indexOffset = 0;
3569 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3570 indexOffset = 1;
3571 }
3572 return oriMap[(d.getRotation() + indexOffset) % 4];
3573 }
Adam Cohen446e9402011-09-15 18:21:21 -07003574
Winson Chung4b919f82012-05-01 10:44:08 -07003575 public boolean isRotationEnabled() {
Michael Jurka9bc8eba2012-05-21 20:36:44 -07003576 boolean forceEnableRotation = doesFileExist(FORCE_ENABLE_ROTATION_PROPERTY);
Winson Chung4b919f82012-05-01 10:44:08 -07003577 boolean enableRotation = forceEnableRotation ||
3578 getResources().getBoolean(R.bool.allow_rotation);
3579 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003580 }
Winson Chung4b919f82012-05-01 10:44:08 -07003581 public void lockScreenOrientation() {
3582 if (isRotationEnabled()) {
3583 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3584 .getConfiguration().orientation));
3585 }
3586 }
3587 public void unlockScreenOrientation(boolean immediate) {
3588 if (isRotationEnabled()) {
3589 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003590 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003591 } else {
3592 mHandler.postDelayed(new Runnable() {
3593 public void run() {
3594 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3595 }
3596 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003597 }
Winson Chung4b919f82012-05-01 10:44:08 -07003598 }
Winson Chung400438b2011-01-16 17:53:48 -08003599 }
3600
Winson Chung82f55532011-08-09 14:14:23 -07003601 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003602 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003603 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003604 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003605
Winson Chung7d7541e2011-09-16 20:14:36 -07003606 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003607 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003608 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3609 Cling cling = (Cling) findViewById(clingId);
3610 if (cling != null) {
3611 cling.init(this, positionData);
3612 cling.setVisibility(View.VISIBLE);
3613 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3614 if (animate) {
3615 cling.buildLayer();
3616 cling.setAlpha(0f);
3617 cling.animate()
3618 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003619 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003620 .setDuration(SHOW_CLING_DURATION)
3621 .setStartDelay(delay)
3622 .start();
3623 } else {
3624 cling.setAlpha(1f);
3625 }
3626 }
3627 return cling;
3628 }
3629 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003630 if (cling != null) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003631 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003632 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003633 anim.addListener(new AnimatorListenerAdapter() {
3634 public void onAnimationEnd(Animator animation) {
3635 cling.setVisibility(View.GONE);
3636 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003637 // We should update the shared preferences on a background thread
3638 new Thread("dismissClingThread") {
3639 public void run() {
3640 SharedPreferences.Editor editor = mSharedPrefs.edit();
3641 editor.putBoolean(flag, true);
3642 editor.commit();
3643 }
3644 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003645 };
3646 });
3647 anim.start();
3648 }
3649 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003650 private void removeCling(int id) {
3651 final View cling = findViewById(id);
3652 if (cling != null) {
3653 final ViewGroup parent = (ViewGroup) cling.getParent();
3654 parent.post(new Runnable() {
3655 @Override
3656 public void run() {
3657 parent.removeView(cling);
3658 }
3659 });
3660 }
3661 }
Michael Jurka974c3862012-05-22 22:00:31 -07003662
3663 private boolean skipCustomClingIfNoAccounts() {
3664 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3665 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3666 if (customCling) {
3667 AccountManager am = AccountManager.get(this);
3668 Account[] accounts = am.getAccountsByType("com.google");
3669 return accounts.length == 0;
3670 }
3671 return false;
3672 }
3673
Winson Chung7d7541e2011-09-16 20:14:36 -07003674 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003675 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003676 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003677 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3678 !skipCustomClingIfNoAccounts() ) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003679 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003680 } else {
3681 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003682 }
3683 }
3684 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003685 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003686 if (isClingsEnabled() &&
3687 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003688 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003689 } else {
3690 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003691 }
3692 }
3693 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003694 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003695 if (isClingsEnabled() &&
3696 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3697 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003698 } else {
3699 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003700 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003701 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003702 }
3703 public boolean isFolderClingVisible() {
3704 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003705 if (cling != null) {
3706 return cling.getVisibility() == View.VISIBLE;
3707 }
3708 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003709 }
Winson Chung82f55532011-08-09 14:14:23 -07003710 public void dismissWorkspaceCling(View v) {
3711 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003712 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003713 }
3714 public void dismissAllAppsCling(View v) {
3715 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003716 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3717 }
3718 public void dismissFolderCling(View v) {
3719 Cling cling = (Cling) findViewById(R.id.folder_cling);
3720 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003721 }
3722
Winson Chung80baf5a2010-08-09 16:03:15 -07003723 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003724 * Prints out out state for debugging.
3725 */
3726 public void dumpState() {
3727 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003728 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003729 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3730 Log.d(TAG, "mRestoring=" + mRestoring);
3731 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3732 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003733 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003734 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003735
Winson Chung785d2eb2011-04-14 16:08:02 -07003736 if (mAppsCustomizeContent != null) {
3737 mAppsCustomizeContent.dumpState();
3738 }
Joe Onoratobe386092009-11-17 17:32:16 -08003739 Log.d(TAG, "END launcher2 dump state");
3740 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003741
3742 @Override
3743 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3744 super.dump(prefix, fd, writer, args);
3745 writer.println(" ");
3746 writer.println("Debug logs: ");
3747 for (int i = 0; i < sDumpLogs.size(); i++) {
3748 writer.println(" " + sDumpLogs.get(i));
3749 }
3750 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003751
3752 public static void dumpDebugLogsToConsole() {
3753 Log.d(TAG, "");
3754 Log.d(TAG, "*********************");
3755 Log.d(TAG, "Launcher debug logs: ");
3756 for (int i = 0; i < sDumpLogs.size(); i++) {
3757 Log.d(TAG, " " + sDumpLogs.get(i));
3758 }
3759 Log.d(TAG, "*********************");
3760 Log.d(TAG, "");
3761 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003762}
Michael Jurka2763be32011-02-24 11:19:57 -08003763
Michael Jurkaabded662011-03-04 12:06:57 -08003764interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003765 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003766 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003767 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003768 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003769 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003770}