blob: 700f9211e22d2dd4d56e9001f232c9379523440d [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;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070058import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080072import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080073import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.KeyEvent;
75import android.view.LayoutInflater;
76import android.view.Menu;
77import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070078import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080079import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080081import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070082import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080083import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080084import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070085import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080086import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070087import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070088import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080089import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070091import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070092import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070093import android.widget.TextView;
94import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070095
Adam Cohendf2cc412011-04-27 16:56:57 -070096import com.android.common.Search;
97import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070098import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080099
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.io.DataInputStream;
101import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700102import java.io.FileDescriptor;
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700103import java.io.FileInputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700104import java.io.FileNotFoundException;
105import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700106import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700108import java.util.Collection;
109import java.util.Collections;
110import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700112import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700113import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700114import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116/**
117 * Default launcher application.
118 */
Michael Jurka946ad472010-07-09 18:05:18 -0700119public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700120 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700121 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800122 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700123 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400126 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700127 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700128
Winson Chung70d72102011-08-12 11:24:35 -0700129 private static final int MENU_GROUP_WALLPAPER = 1;
130 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
131 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700132 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
133 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurka8b805b12012-04-18 14:23:14 -0700142 private static final int REQUEST_BIND_APPWIDGET = 11;
143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
145
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800146 static final int SCREEN_COUNT = 5;
147 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Romain Guy98d01652009-06-30 16:21:04 -0700149 private static final String PREFERENCES = "launcher.preferences";
Michael Jurkaf3bade62012-06-12 11:00:21 -0700150 static final String FORCE_ENABLE_ROTATION_PROPERTY = "debug.force_enable_rotation";
151 static final String DUMP_STATE_PROPERTY = "debug.dumpstate";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Winson Chung2672ff92012-05-04 16:22:30 -0700153 // The Intent extra that defines whether to ignore the launch animation
154 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
155 "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: int
158 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700159 // Type: int
160 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700162 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
163 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
165 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700166 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700168 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: boolean
170 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
171 // Type: long
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700173 // Type: int
174 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
175 // Type: int
176 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
177 // Type: parcelable
178 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700180 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700181 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
182 "com.android.launcher.toolbar_search_icon";
183 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
184 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700185
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700186 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700187 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700188 private State mState = State.WORKSPACE;
189 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800190 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700191
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700193 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
194 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700195 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700196 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800199 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Winson Chunga2413752012-04-03 14:22:34 -0700201 // How long to wait before the new-shortcut animation automatically pans the workspace
202 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
203
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800204 private final BroadcastReceiver mCloseSystemDialogsReceiver
205 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800206 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private LayoutInflater mInflater;
209
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800211 private View mQsbDivider;
212 private View mDockDivider;
213 private DragLayer mDragLayer;
214 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700215
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700216 private AppWidgetManager mAppWidgetManager;
217 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700218
Michael Jurkac9d95c52011-08-29 14:03:34 -0700219 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700220 private AppWidgetProviderInfo mPendingAddWidgetInfo;
221
Michael Jurka0280c3b2010-09-17 15:00:07 -0700222 private int[] mTmpAddItemCellCoordinates = new int[2];
223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 private FolderInfo mFolderInfo;
225
Winson Chung3d503fb2011-07-13 17:25:49 -0700226 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800227 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700228
Winson Chungc51db6a2011-10-05 11:44:49 -0700229 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700230 private AppsCustomizeTabHost mAppsCustomizeTabHost;
231 private AppsCustomizePagedView mAppsCustomizeContent;
232 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700235 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
236 // scroll issues (because the workspace may not have been measured yet) and extra work.
237 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
238 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239
240 private SpannableStringBuilder mDefaultKeySsb = null;
241
Joe Onorato9c1289c2009-08-17 11:03:03 -0400242 private boolean mWorkspaceLoading = true;
243
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800244 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 private boolean mRestoring;
246 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700247 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248
Winson Chung4a2afa32012-07-19 14:53:05 -0700249 // Keep track of whether the user has left launcher
250 private static boolean sPausedFromUserAction = false;
251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 private Bundle mSavedInstanceState;
253
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800255 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800256 private boolean mUserPresent = true;
257 private boolean mVisible = false;
258 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400259
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700260 private static LocaleConfiguration sLocaleConfiguration = null;
261
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700262 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700263
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700264 private Intent mAppMarketIntent = null;
265
Adam Cohended9f8d2010-11-03 13:25:16 -0700266 // Related to the auto-advancing of widgets
267 private final int ADVANCE_MSG = 1;
268 private final int mAdvanceInterval = 20000;
269 private final int mAdvanceStagger = 250;
270 private long mAutoAdvanceSentTime;
271 private long mAutoAdvanceTimeLeft = -1;
272 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
273 new HashMap<View, AppWidgetProviderInfo>();
274
Winson Chung400438b2011-01-16 17:53:48 -0800275 // Determines how long to wait after a rotation before restoring the screen orientation to
276 // match the sensor state.
277 private final int mRestoreScreenOrientationDelay = 500;
278
Michael Jurka4ef207b2010-11-29 17:05:45 -0800279 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700280 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
281 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
282 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800283
Adam Cohen1462de32012-07-24 22:34:36 -0700284 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
285
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700286 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
287
Winson Chung46353de2012-02-16 14:05:10 -0800288 // We only want to get the SharedPreferences once since it does an FS stat each time we get
289 // it from the context.
290 private SharedPreferences mSharedPrefs;
291
Winson Chungf0c6ae02012-03-21 16:10:31 -0700292 // Holds the page that we need to animate to, and the icon views that we need to animate up
293 // when we scroll to that page on resume.
294 private int mNewShortcutAnimatePage = -1;
295 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700296 private ImageView mFolderIconImageView;
297 private Bitmap mFolderIconBitmap;
298 private Canvas mFolderIconCanvas;
299 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700300
Michael Jurkaddd62e92011-02-16 17:49:14 -0800301 private BubbleTextView mWaitingForResume;
302
Michael Jurka22143132012-10-04 17:42:38 +0200303 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
304 = new HideFromAccessibilityHelper();
305
Michael Jurkac1f5d262011-09-30 19:32:27 -0700306 private Runnable mBuildLayersRunnable = new Runnable() {
307 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700308 if (mWorkspace != null) {
309 mWorkspace.buildPageHardwareLayers();
310 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700311 }
312 };
313
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800314 private static ArrayList<PendingAddArguments> sPendingAddList
315 = new ArrayList<PendingAddArguments>();
316
317 private static class PendingAddArguments {
318 int requestCode;
319 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700320 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800321 int screen;
322 int cellX;
323 int cellY;
324 }
325
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700326
327 private boolean doesFileExist(String filename) {
328 FileInputStream fis = null;
329 try {
330 fis = openFileInput(filename);
331 fis.close();
332 return true;
333 } catch (java.io.FileNotFoundException e) {
334 return false;
335 } catch (java.io.IOException e) {
336 return true;
337 }
338 }
339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 @Override
341 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700342 if (DEBUG_STRICT_MODE) {
343 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
344 .detectDiskReads()
345 .detectDiskWrites()
346 .detectNetwork() // or .detectAll() for all detectable problems
347 .penaltyLog()
348 .build());
349 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
350 .detectLeakedSqlLiteObjects()
351 .detectLeakedClosableObjects()
352 .penaltyLog()
353 .penaltyDeath()
354 .build());
355 }
356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800358 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700359 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
360 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800361 mModel = app.setLauncher(this);
362 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400363 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700365
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700366 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700367 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
368 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700369
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700370 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
371 // this also ensures that any synchronous binding below doesn't re-trigger another
372 // LauncherModel load.
373 mPaused = false;
374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200376 android.os.Debug.startMethodTracing(
377 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 }
379
380 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 setContentView(R.layout.launcher);
382 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700383 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800385 registerContentObservers();
386
Joe Onorato7c312c12009-08-13 21:36:53 -0700387 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 mSavedState = savedInstanceState;
390 restoreState(mSavedState);
391
Winson Chunga12a2502010-12-20 14:41:35 -0800392 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700393 if (mAppsCustomizeContent != null) {
394 mAppsCustomizeContent.onPackagesUpdated();
395 }
Winson Chunga12a2502010-12-20 14:41:35 -0800396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 if (PROFILE_STARTUP) {
398 android.os.Debug.stopMethodTracing();
399 }
400
401 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700402 if (sPausedFromUserAction) {
403 // If the user leaves launcher, then we should just load items asynchronously when
404 // they return.
405 mModel.startLoader(true, -1);
406 } else {
407 // We only load the page synchronously if the user rotates (or triggers a
408 // configuration change) while launcher is in the foreground
409 mModel.startLoader(true, mWorkspace.getCurrentPage());
410 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
412
Michael Jurkac57b7a82011-08-09 22:02:20 -0700413 if (!mModel.isAllAppsLoaded()) {
414 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
415 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
416 }
417
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800418 // For handling default keys
419 mDefaultKeySsb = new SpannableStringBuilder();
420 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800421
422 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
423 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800424
Adam Cohenf9426d52012-06-04 17:26:21 -0700425 updateGlobalIcons();
426
427 // On large interfaces, we want the screen to auto-rotate based on the current orientation
428 unlockScreenOrientation(true);
429 }
430
Winson Chung4a2afa32012-07-19 14:53:05 -0700431 protected void onUserLeaveHint() {
432 super.onUserLeaveHint();
433 sPausedFromUserAction = true;
434 }
435
Adam Cohenf9426d52012-06-04 17:26:21 -0700436 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700437 boolean searchVisible = false;
438 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800439 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700440 int coi = getCurrentOrientationIndexForGlobalIcons();
441 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
442 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700443 updateAppMarketIcon();
444 searchVisible = updateGlobalSearchIcon();
445 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700446 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700447 if (sGlobalSearchIcon[coi] != null) {
448 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700449 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700450 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700451 if (sVoiceSearchIcon[coi] != null) {
452 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700453 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700454 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700455 if (sAppMarketIcon[coi] != null) {
456 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800457 }
Winson Chungadf0c182012-08-23 14:59:07 -0700458 if (mSearchDropTargetBar != null) {
459 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 }
Romain Guycbb89e42009-06-08 15:52:54 -0700462
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700464 if (sLocaleConfiguration == null) {
465 new AsyncTask<Void, Void, LocaleConfiguration>() {
466 @Override
467 protected LocaleConfiguration doInBackground(Void... unused) {
468 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
469 readConfiguration(Launcher.this, localeConfiguration);
470 return localeConfiguration;
471 }
472
473 @Override
474 protected void onPostExecute(LocaleConfiguration result) {
475 sLocaleConfiguration = result;
476 checkForLocaleChange(); // recursive, but now with a locale configuration
477 }
478 }.execute();
479 return;
480 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700481
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 final Configuration configuration = getResources().getConfiguration();
483
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700484 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 final String locale = configuration.locale.toString();
486
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700487 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 final int mcc = configuration.mcc;
489
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700490 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491 final int mnc = configuration.mnc;
492
Romain Guy84f296c2009-11-04 15:00:44 -0800493 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494
Romain Guy84f296c2009-11-04 15:00:44 -0800495 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700496 sLocaleConfiguration.locale = locale;
497 sLocaleConfiguration.mcc = mcc;
498 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700499
Joe Onorato0589f0f2010-02-08 13:44:00 -0800500 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700501
502 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
503 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700504 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700505 public void run() {
506 writeConfiguration(Launcher.this, localeConfiguration);
507 }
508 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700509 }
510 }
511
512 private static class LocaleConfiguration {
513 public String locale;
514 public int mcc = -1;
515 public int mnc = -1;
516 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700517
Romain Guy98d01652009-06-30 16:21:04 -0700518 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
519 DataInputStream in = null;
520 try {
521 in = new DataInputStream(context.openFileInput(PREFERENCES));
522 configuration.locale = in.readUTF();
523 configuration.mcc = in.readInt();
524 configuration.mnc = in.readInt();
525 } catch (FileNotFoundException e) {
526 // Ignore
527 } catch (IOException e) {
528 // Ignore
529 } finally {
530 if (in != null) {
531 try {
532 in.close();
533 } catch (IOException e) {
534 // Ignore
535 }
536 }
537 }
538 }
539
540 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
541 DataOutputStream out = null;
542 try {
543 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
544 out.writeUTF(configuration.locale);
545 out.writeInt(configuration.mcc);
546 out.writeInt(configuration.mnc);
547 out.flush();
548 } catch (FileNotFoundException e) {
549 // Ignore
550 } catch (IOException e) {
551 //noinspection ResultOfMethodCallIgnored
552 context.getFileStreamPath(PREFERENCES).delete();
553 } finally {
554 if (out != null) {
555 try {
556 out.close();
557 } catch (IOException e) {
558 // Ignore
559 }
560 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 }
562 }
563
Adam Cohen716b51e2011-06-30 12:09:54 -0700564 public DragLayer getDragLayer() {
565 return mDragLayer;
566 }
567
Winson Chung36a62fe2012-05-06 18:04:42 -0700568 boolean isDraggingEnabled() {
569 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
570 // that is subsequently removed from the workspace in startBinding().
571 return !mModel.isLoadingWorkspace();
572 }
573
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 static int getScreen() {
575 synchronized (sLock) {
576 return sScreen;
577 }
578 }
579
580 static void setScreen(int screen) {
581 synchronized (sLock) {
582 sScreen = screen;
583 }
584 }
585
Winson Chung557d6ed2011-07-08 15:34:52 -0700586 /**
587 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
588 * a configuration step, this allows the proper animations to run after other transitions.
589 */
590 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700591 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800592 switch (args.requestCode) {
593 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700594 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
595 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800596 break;
597 case REQUEST_PICK_SHORTCUT:
598 processShortcut(args.intent);
599 break;
600 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700601 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
602 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700603 result = true;
604 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800605 case REQUEST_CREATE_APPWIDGET:
606 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800607 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700608 result = true;
609 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800610 case REQUEST_PICK_WALLPAPER:
611 // We just wanted the activity result here so we can clear mWaitingForResult
612 break;
613 }
Michael Jurka27614d22012-04-02 06:40:22 -0700614 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
615 // if you turned the screen off and then back while in All Apps, Launcher would not
616 // return to the workspace. Clearing mAddInfo.container here fixes this issue
617 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700618 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800619 }
620
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700622 protected void onActivityResult(
623 final int requestCode, final int resultCode, final Intent data) {
624 if (requestCode == REQUEST_BIND_APPWIDGET) {
625 int appWidgetId = data != null ?
626 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
627 if (resultCode == RESULT_CANCELED) {
628 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
629 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700630 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700631 }
632 return;
633 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700634 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800635 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
636 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700637 mWaitingForResult = false;
638
Adam Cohened66b2b2012-01-23 17:28:51 -0800639 // We have special handling for widgets
640 if (isWidgetDrop) {
641 int appWidgetId = data != null ?
642 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700643 if (appWidgetId < 0) {
644 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
645 "widget configuration activity.");
646 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
647 } else {
648 completeTwoStageWidgetDrop(resultCode, appWidgetId);
649 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800650 return;
651 }
652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 // The pattern used here is that a user PICKs a specific application,
654 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700655
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
657 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700658 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800659 final PendingAddArguments args = new PendingAddArguments();
660 args.requestCode = requestCode;
661 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700662 args.container = mPendingAddInfo.container;
663 args.screen = mPendingAddInfo.screen;
664 args.cellX = mPendingAddInfo.cellX;
665 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800666 if (isWorkspaceLocked()) {
667 sPendingAddList.add(args);
668 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700669 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800672 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700673 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800674 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
675 null);
676 }
677
678 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700679 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700680 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800681 Runnable onCompleteRunnable = null;
682 int animationType = 0;
683
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700684 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800685 if (resultCode == RESULT_OK) {
686 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
687 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700688 mPendingAddWidgetInfo);
689 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800690 onCompleteRunnable = new Runnable() {
691 @Override
692 public void run() {
693 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
694 mPendingAddInfo.screen, layout, null);
695 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
696 null);
697 }
698 };
699 } else if (resultCode == RESULT_CANCELED) {
700 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
701 onCompleteRunnable = new Runnable() {
702 @Override
703 public void run() {
704 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
705 null);
706 }
707 };
708 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700709 if (mDragLayer.getAnimatedView() != null) {
710 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
711 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
712 animationType, boundWidget, true);
713 } else {
714 // The animated view may be null in the case of a rotation during widget configuration
715 onCompleteRunnable.run();
716 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 }
718
719 @Override
720 protected void onResume() {
721 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700722
Winson Chung4a2afa32012-07-19 14:53:05 -0700723 // Restore the previous launcher state
724 if (mOnResumeState == State.WORKSPACE) {
725 showWorkspace(false);
726 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
727 showAllApps(false);
728 }
729 mOnResumeState = State.NONE;
730
Winson Chungde0fb8f2012-05-08 14:37:08 -0700731 // Process any items that were added while Launcher was away
732 InstallShortcutReceiver.flushInstallQueue(this);
733
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800734 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700735 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700736 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700738 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700740 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 }
Winson Chunge4e50662012-01-23 14:45:13 -0800742
743 // Reset the pressed state of icons that were locked in the press state while activities
744 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800745 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800746 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800747 mWaitingForResume.setStayPressed(false);
748 }
Winson Chunge4e50662012-01-23 14:45:13 -0800749 if (mAppsCustomizeContent != null) {
750 // Resets the previous all apps icon press state
751 mAppsCustomizeContent.resetDrawableState();
752 }
Adam Cohen06dff352012-06-01 17:17:08 -0700753 // It is possible that widgets can receive updates while launcher is not in the foreground.
754 // Consequently, the widgets will be inflated in the orientation of the foreground activity
755 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
756 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700757 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700758
759 // Again, as with the above scenario, it's possible that one or more of the global icons
760 // were updated in the wrong orientation.
761 updateGlobalIcons();
Adam Cohen06dff352012-06-01 17:17:08 -0700762 }
763
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700765 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700766 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
767 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
768 // when Launcher resumes and we are still in AllApps.
769 updateWallpaperVisibility(true);
770
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700771 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700772 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800773 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700774 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700775 }
Romain Guycbb89e42009-06-08 15:52:54 -0700776
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700777 @Override
778 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400779 // Flag the loader to stop early before switching
780 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700781 if (mAppsCustomizeContent != null) {
782 mAppsCustomizeContent.surrender();
783 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800784 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700785 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700786
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800787 // We can't hide the IME if it was forced open. So don't bother
788 /*
789 @Override
790 public void onWindowFocusChanged(boolean hasFocus) {
791 super.onWindowFocusChanged(hasFocus);
792
793 if (hasFocus) {
794 final InputMethodManager inputManager = (InputMethodManager)
795 getSystemService(Context.INPUT_METHOD_SERVICE);
796 WindowManager.LayoutParams lp = getWindow().getAttributes();
797 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
798 android.os.Handler()) {
799 protected void onReceiveResult(int resultCode, Bundle resultData) {
800 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
801 }
802 });
803 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
804 }
805 }
806 */
807
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 private boolean acceptFilter() {
809 final InputMethodManager inputManager = (InputMethodManager)
810 getSystemService(Context.INPUT_METHOD_SERVICE);
811 return !inputManager.isFullscreenMode();
812 }
813
814 @Override
815 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700816 final int uniChar = event.getUnicodeChar();
817 final boolean handled = super.onKeyDown(keyCode, event);
818 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
819 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
821 keyCode, event);
822 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700823 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700824 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700825 // showSearchDialog()
826 // If there are multiple keystrokes before the search dialog takes focus,
827 // onSearchRequested() will be called for every keystroke,
828 // but it is idempotent, so it's fine.
829 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
831 }
832
Joe Onorato8a9625e2010-01-28 15:55:35 -0800833 // Eat the long press event so the keyboard doesn't come up.
834 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
835 return true;
836 }
837
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 return handled;
839 }
840
Karl Rosaen138a0412009-04-23 19:00:21 -0700841 private String getTypedText() {
842 return mDefaultKeySsb.toString();
843 }
844
845 private void clearTypedText() {
846 mDefaultKeySsb.clear();
847 mDefaultKeySsb.clearSpans();
848 Selection.setSelection(mDefaultKeySsb, 0);
849 }
850
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700852 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
853 * State
854 */
855 private static State intToState(int stateOrdinal) {
856 State state = State.WORKSPACE;
857 final State[] stateValues = State.values();
858 for (int i = 0; i < stateValues.length; i++) {
859 if (stateValues[i].ordinal() == stateOrdinal) {
860 state = stateValues[i];
861 break;
862 }
863 }
864 return state;
865 }
866
867 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 * Restores the previous state, if it exists.
869 *
870 * @param savedState The previous state.
871 */
872 private void restoreState(Bundle savedState) {
873 if (savedState == null) {
874 return;
875 }
876
Michael Jurka883f55b2010-10-21 15:47:14 -0700877 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700878 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700879 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800880 }
881
Winson Chungf0c6ae02012-03-21 16:10:31 -0700882 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700884 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886
Winson Chung3d503fb2011-07-13 17:25:49 -0700887 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
888 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700889
Winson Chung3d503fb2011-07-13 17:25:49 -0700890 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
891 mPendingAddInfo.container = pendingAddContainer;
892 mPendingAddInfo.screen = pendingAddScreen;
893 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
894 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700895 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
896 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
897 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700898 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 mRestoring = true;
900 }
901
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700902
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
904 if (renameFolder) {
905 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700906 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 mRestoring = true;
908 }
Winson Chunga12a2502010-12-20 14:41:35 -0800909
Winson Chung785d2eb2011-04-14 16:08:02 -0700910
911 // Restore the AppsCustomize tab
912 if (mAppsCustomizeTabHost != null) {
913 String curTab = savedState.getString("apps_customize_currentTab");
914 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700915 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700916 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700917 mAppsCustomizeContent.loadAssociatedPages(
918 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700919 }
920
Winson Chung5afbf7b2011-07-25 11:53:08 -0700921 int currentIndex = savedState.getInt("apps_customize_currentIndex");
922 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 }
925
926 /**
927 * Finds all the views we need and configure them properly.
928 */
929 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700930 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400931
Winson Chung4c98d922011-05-31 16:50:48 -0700932 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
933 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800934 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
935 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936
Winson Chung4c98d922011-05-31 16:50:48 -0700937 // Setup the drag layer
938 mDragLayer.setup(this, dragController);
939
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 // Setup the hotseat
941 mHotseat = (Hotseat) findViewById(R.id.hotseat);
942 if (mHotseat != null) {
943 mHotseat.setup(this);
944 }
945
Winson Chung4c98d922011-05-31 16:50:48 -0700946 // Setup the workspace
947 mWorkspace.setHapticFeedbackEnabled(false);
948 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700949 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700950 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700951
Winson Chungf0ea4d32011-06-06 14:27:16 -0700952 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700953 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700954
Winson Chungf0ea4d32011-06-06 14:27:16 -0700955 // Setup AppsCustomize
956 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
957 findViewById(R.id.apps_customize_pane);
958 mAppsCustomizeContent = (AppsCustomizePagedView)
959 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
960 mAppsCustomizeContent.setup(this, dragController);
Winson Chung0e721a42012-08-02 17:40:30 -0700961
Winson Chung3d503fb2011-07-13 17:25:49 -0700962 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700963 dragController.setDragScoller(mWorkspace);
964 dragController.setScrollView(mDragLayer);
965 dragController.setMoveTarget(mWorkspace);
966 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700967 if (mSearchDropTargetBar != null) {
968 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800969 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 }
971
972 /**
973 * Creates a view representing a shortcut.
974 *
975 * @param info The data structure describing the shortcut.
976 *
977 * @return A View inflated from R.layout.application.
978 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800979 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700981 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 }
983
984 /**
985 * Creates a view representing a shortcut inflated from the specified resource.
986 *
987 * @param layoutResId The id of the XML layout used to create the shortcut.
988 * @param parent The group the shortcut belongs to.
989 * @param info The data structure describing the shortcut.
990 *
991 * @return A View inflated from layoutResId.
992 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800993 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800994 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
995 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 return favorite;
998 }
999
1000 /**
1001 * Add an application shortcut to the workspace.
1002 *
1003 * @param data The intent describing the application.
1004 * @param cellInfo The position on screen where to create the shortcut.
1005 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001006 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001007 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001008 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001009
Adam Cohenfbba09b2011-07-18 21:43:05 -07001010 // First we check if we already know the exact location where we want to add this item.
1011 if (cellX >= 0 && cellY >= 0) {
1012 cellXY[0] = cellX;
1013 cellXY[1] = cellY;
1014 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001015 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001016 return;
1017 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001019 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001020
Romain Guy73b979d2009-06-09 12:57:21 -07001021 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001022 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001024 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001025 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001026 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001027 } else {
1028 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 }
1030 }
Romain Guycbb89e42009-06-08 15:52:54 -07001031
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 /**
1033 * Add a shortcut to the workspace.
1034 *
1035 * @param data The intent describing the shortcut.
1036 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001038 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1039 int cellY) {
1040 int[] cellXY = mTmpAddItemCellCoordinates;
1041 int[] touchXY = mPendingAddInfo.dropPos;
1042 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001043
Michael Jurkad3ef3062010-11-23 16:23:58 -08001044 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001045
Adam Cohen558baaf2011-08-15 15:22:57 -07001046 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001047 if (info == null) {
1048 return;
1049 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001050 final View view = createShortcut(info);
1051
Adam Cohenfbba09b2011-07-18 21:43:05 -07001052 // First we check if we already know the exact location where we want to add this item.
1053 if (cellX >= 0 && cellY >= 0) {
1054 cellXY[0] = cellX;
1055 cellXY[1] = cellY;
1056 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001057
1058 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001059 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001060 true, null,null)) {
1061 return;
1062 }
1063 DragObject dragObject = new DragObject();
1064 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001065 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1066 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001067 return;
1068 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001069 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001070 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001071 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001072 foundCellSpan = (result != null);
1073 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001074 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001075 }
1076
1077 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001078 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001079 return;
1080 }
1081
Adam Cohen558baaf2011-08-15 15:22:57 -07001082 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083
1084 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001085 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1086 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 }
1088 }
1089
Adam Cohen2f093b62012-04-30 18:59:53 -07001090 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1091 int minHeight) {
1092 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001093 // We want to account for the extra amount of padding that we are adding to the widget
1094 // to ensure that it gets the full amount of space that it has requested
1095 int requiredWidth = minWidth + padding.left + padding.right;
1096 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001097 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001098 }
1099
Adam Cohen2f093b62012-04-30 18:59:53 -07001100 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1101 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001102 }
1103
Adam Cohen2f093b62012-04-30 18:59:53 -07001104 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1105 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001106 }
1107
Adam Cohen2f093b62012-04-30 18:59:53 -07001108 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1109 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001110 }
1111
Adam Cohen2f093b62012-04-30 18:59:53 -07001112 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1113 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001114 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001115 }
1116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001118 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001120 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001121 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001123 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1124 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1125 if (appWidgetInfo == null) {
1126 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1127 }
Romain Guycbb89e42009-06-08 15:52:54 -07001128
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001129 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001130 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001131
Adam Cohen2f093b62012-04-30 18:59:53 -07001132 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1133 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001136 // We have saved the position to which the widget was dragged-- this really only matters
1137 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001138 int[] cellXY = mTmpAddItemCellCoordinates;
1139 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001140 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001141 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001142 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1143 cellXY[0] = mPendingAddInfo.cellX;
1144 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001145 spanXY[0] = mPendingAddInfo.spanX;
1146 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001147 foundCellSpan = true;
1148 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001149 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001150 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001151 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1152 spanXY[1], cellXY, finalSpan);
1153 spanXY[0] = finalSpan[0];
1154 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001155 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001156 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001157 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001158 }
1159
Michael Jurka0280c3b2010-09-17 15:00:07 -07001160 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001161 if (appWidgetId != -1) {
1162 // Deleting an app widget ID is a void call but writes to disk before returning
1163 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001164 new Thread("deleteAppWidgetId") {
1165 public void run() {
1166 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1167 }
1168 }.start();
1169 }
Winson Chung93eef082012-03-23 15:59:27 -07001170 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001171 return;
1172 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001174 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001175 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1176 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001177 launcherInfo.spanX = spanXY[0];
1178 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001179 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1180 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001183 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184
1185 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001186 if (hostView == null) {
1187 // Perform actual inflation because we're live
1188 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1189 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1190 } else {
1191 // The AppWidgetHostView has already been inflated and instantiated
1192 launcherInfo.hostView = hostView;
1193 }
Romain Guycbb89e42009-06-08 15:52:54 -07001194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001196 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001197 launcherInfo.notifyWidgetSizeChanged(this);
1198
Winson Chung3d503fb2011-07-13 17:25:49 -07001199 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001200 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001201
1202 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001204 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 }
Romain Guycbb89e42009-06-08 15:52:54 -07001206
Adam Cohended9f8d2010-11-03 13:25:16 -07001207 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1208 @Override
1209 public void onReceive(Context context, Intent intent) {
1210 final String action = intent.getAction();
1211 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1212 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001213 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001214 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001215
Winson Chungc100e8e2011-08-09 16:02:43 -07001216 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001217 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001218 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1219 mAppsCustomizeTabHost.reset();
1220 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001221 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001222 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1223 mUserPresent = true;
1224 updateRunning();
1225 }
1226 }
1227 };
1228
1229 @Override
1230 public void onAttachedToWindow() {
1231 super.onAttachedToWindow();
1232
1233 // Listen for broadcasts related to user-presence
1234 final IntentFilter filter = new IntentFilter();
1235 filter.addAction(Intent.ACTION_SCREEN_OFF);
1236 filter.addAction(Intent.ACTION_USER_PRESENT);
1237 registerReceiver(mReceiver, filter);
1238
Adam Cohend113e0c2010-11-11 10:48:05 -08001239 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001240 mVisible = true;
1241 }
1242
1243 @Override
1244 public void onDetachedFromWindow() {
1245 super.onDetachedFromWindow();
1246 mVisible = false;
1247
Adam Cohend113e0c2010-11-11 10:48:05 -08001248 if (mAttached) {
1249 unregisterReceiver(mReceiver);
1250 mAttached = false;
1251 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001252 updateRunning();
1253 }
1254
1255 public void onWindowVisibilityChanged(int visibility) {
1256 mVisible = visibility == View.VISIBLE;
1257 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001258 // The following code used to be in onResume, but it turns out onResume is called when
1259 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1260 // is a more appropriate event to handle
1261 if (mVisible) {
1262 mAppsCustomizeTabHost.onWindowVisible();
1263 if (!mWorkspaceLoading) {
1264 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001265 // We want to let Launcher draw itself at least once before we force it to build
1266 // layers on all the workspace pages, so that transitioning to Launcher from other
1267 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1268 // a true draw so we wait until the second preDraw call to be safe
1269 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001270 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001271 // We delay the layer building a bit in order to give
1272 // other message processing a time to run. In particular
1273 // this avoids a delay in hiding the IME if it was
1274 // currently shown, because doing that may involve
1275 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001276 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001277
Michael Jurka6ee21d22012-02-21 18:20:27 -08001278 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001279 return true;
1280 }
1281 });
1282 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001283 // When Launcher comes back to foreground, a different Activity might be responsible for
1284 // the app market intent, so refresh the icon
1285 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001286 clearTypedText();
1287 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001288 }
1289
1290 private void sendAdvanceMessage(long delay) {
1291 mHandler.removeMessages(ADVANCE_MSG);
1292 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1293 mHandler.sendMessageDelayed(msg, delay);
1294 mAutoAdvanceSentTime = System.currentTimeMillis();
1295 }
1296
1297 private void updateRunning() {
1298 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1299 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1300 mAutoAdvanceRunning = autoAdvanceRunning;
1301 if (autoAdvanceRunning) {
1302 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1303 sendAdvanceMessage(delay);
1304 } else {
1305 if (!mWidgetsToAdvance.isEmpty()) {
1306 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1307 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1308 }
1309 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001310 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001311 }
1312 }
1313 }
1314
1315 private final Handler mHandler = new Handler() {
1316 @Override
1317 public void handleMessage(Message msg) {
1318 if (msg.what == ADVANCE_MSG) {
1319 int i = 0;
1320 for (View key: mWidgetsToAdvance.keySet()) {
1321 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1322 final int delay = mAdvanceStagger * i;
1323 if (v instanceof Advanceable) {
1324 postDelayed(new Runnable() {
1325 public void run() {
1326 ((Advanceable) v).advance();
1327 }
1328 }, delay);
1329 }
1330 i++;
1331 }
1332 sendAdvanceMessage(mAdvanceInterval);
1333 }
1334 }
1335 };
1336
1337 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001338 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001339 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1340 if (v instanceof Advanceable) {
1341 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001342 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001343 updateRunning();
1344 }
1345 }
1346
1347 void removeWidgetToAutoAdvance(View hostView) {
1348 if (mWidgetsToAdvance.containsKey(hostView)) {
1349 mWidgetsToAdvance.remove(hostView);
1350 updateRunning();
1351 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001352 }
1353
Joe Onorato9c1289c2009-08-17 11:03:03 -04001354 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001355 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001356 launcherInfo.hostView = null;
1357 }
1358
Winson Chung93eef082012-03-23 15:59:27 -07001359 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1360 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1361 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001362 }
1363
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001364 public LauncherAppWidgetHost getAppWidgetHost() {
1365 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 }
Romain Guycbb89e42009-06-08 15:52:54 -07001367
Winson Chunga9abd0e2010-10-27 17:18:37 -07001368 public LauncherModel getModel() {
1369 return mModel;
1370 }
1371
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001372 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001373 getWindow().closeAllPanels();
1374
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001375 // Whatever we were doing is hereby canceled.
1376 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001377 }
1378
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 @Override
1380 protected void onNewIntent(Intent intent) {
1381 super.onNewIntent(intent);
1382
1383 // Close the menu
1384 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001385 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001386 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001387
Joe Onorato14f122b2009-11-19 14:06:36 -08001388 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1389 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001390
Adam Cohenac56cff2011-09-28 20:45:37 -07001391 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001392 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001393 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001394 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1395 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001396 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001397 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001398
1399 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001400 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001401
1402 // If we are already on home, then just animate back to the workspace, otherwise, just
1403 // wait until onResume to set the state back to Workspace
1404 if (alreadyOnHome) {
1405 showWorkspace(true);
1406 } else {
1407 mOnResumeState = State.WORKSPACE;
1408 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001409
Joe Onorato3a8820b2009-11-10 15:06:42 -08001410 final View v = getWindow().peekDecorView();
1411 if (v != null && v.getWindowToken() != null) {
1412 InputMethodManager imm = (InputMethodManager)getSystemService(
1413 INPUT_METHOD_SERVICE);
1414 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001416
Michael Jurka35aa14d2011-07-07 17:01:08 -07001417 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001418 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001419 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001420 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001421 }
1422 }
1423
1424 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001425 public void onRestoreInstanceState(Bundle state) {
1426 super.onRestoreInstanceState(state);
1427 for (int page: mSynchronouslyBoundPages) {
1428 mWorkspace.restoreInstanceStateForChild(page);
1429 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001430 }
1431
1432 @Override
1433 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001434 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001435 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436
Michael Jurka883f55b2010-10-21 15:47:14 -07001437 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001438 // We close any open folder since it will not be re-opened, and we need to make sure
1439 // this state is reflected.
1440 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441
Winson Chung3d503fb2011-07-13 17:25:49 -07001442 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1443 mWaitingForResult) {
1444 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1445 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1446 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1447 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001448 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1449 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1450 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001451 }
1452
1453 if (mFolderInfo != null && mWaitingForResult) {
1454 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1455 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1456 }
Michael Jurka946ad472010-07-09 18:05:18 -07001457
Winson Chung785d2eb2011-04-14 16:08:02 -07001458 // Save the current AppsCustomize tab
1459 if (mAppsCustomizeTabHost != null) {
1460 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1461 if (currentTabTag != null) {
1462 outState.putString("apps_customize_currentTab", currentTabTag);
1463 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001464 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1465 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001466 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001467 }
1468
1469 @Override
1470 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001472
Winson Chunge7a03942011-08-05 15:05:12 -07001473 // Remove all pending runnables
1474 mHandler.removeMessages(ADVANCE_MSG);
1475 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001476 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001477
Winson Chungcd2b0142011-06-08 16:02:26 -07001478 // Stop callbacks from LauncherModel
1479 LauncherApplication app = ((LauncherApplication) getApplication());
1480 mModel.stopLoader();
1481 app.setLauncher(null);
1482
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001484 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001486 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001488 mAppWidgetHost = null;
1489
1490 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001491
1492 TextKeyListener.getInstance().release();
1493
Winson Chung81b52252012-08-27 15:34:29 -07001494 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1495 // to prevent leaking Launcher activities on orientation change.
1496 if (mModel != null) {
1497 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1498 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001499
1500 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001501 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001502
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001503 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001504 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1505 mWorkspace.removeAllViews();
1506 mWorkspace = null;
1507 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001508
Michael Jurka2ecf9952012-06-18 12:52:28 -07001509 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 }
1511
Adam Cohena9cf38f2011-05-02 15:36:58 -07001512 public DragController getDragController() {
1513 return mDragController;
1514 }
1515
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 @Override
1517 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001518 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001519 super.startActivityForResult(intent, requestCode);
1520 }
1521
Winson Chung70d72102011-08-12 11:24:35 -07001522 /**
1523 * Indicates that we want global search for this activity by setting the globalSearch
1524 * argument for {@link #startSearch} to true.
1525 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001527 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001529
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001530 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001531
Karl Rosaen138a0412009-04-23 19:00:21 -07001532 if (initialQuery == null) {
1533 // Use any text typed in the launcher as the initial query
1534 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001535 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 if (appSearchData == null) {
1537 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001538 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 }
Winson Chungadf0c182012-08-23 14:59:07 -07001540 Rect sourceBounds = new Rect();
1541 if (mSearchDropTargetBar != null) {
1542 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1543 }
Romain Guycbb89e42009-06-08 15:52:54 -07001544
Michael Jurkaa33411c2012-06-14 16:18:21 -07001545 startGlobalSearch(initialQuery, selectInitialQuery,
1546 appSearchData, sourceBounds);
1547 }
1548
1549 /**
1550 * Starts the global search activity. This code is a copied from SearchManager
1551 */
1552 public void startGlobalSearch(String initialQuery,
1553 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001554 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001555 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1556 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1557 if (globalSearchActivity == null) {
1558 Log.w(TAG, "No global search activity found.");
1559 return;
1560 }
1561 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1562 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1563 intent.setComponent(globalSearchActivity);
1564 // Make sure that we have a Bundle to put source in
1565 if (appSearchData == null) {
1566 appSearchData = new Bundle();
1567 } else {
1568 appSearchData = new Bundle(appSearchData);
1569 }
1570 // Set source to package name of app that starts global search, if not set already.
1571 if (!appSearchData.containsKey("source")) {
1572 appSearchData.putString("source", getPackageName());
1573 }
1574 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1575 if (!TextUtils.isEmpty(initialQuery)) {
1576 intent.putExtra(SearchManager.QUERY, initialQuery);
1577 }
1578 if (selectInitialQuery) {
1579 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1580 }
1581 intent.setSourceBounds(sourceBounds);
1582 try {
1583 startActivity(intent);
1584 } catch (ActivityNotFoundException ex) {
1585 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1586 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 }
1588
Winson Chung70d72102011-08-12 11:24:35 -07001589 @Override
1590 public boolean onCreateOptionsMenu(Menu menu) {
1591 if (isWorkspaceLocked()) {
1592 return false;
1593 }
1594
1595 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001596
1597 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1598 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1599 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001600 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1601 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1602 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001603 String helpUrl = getString(R.string.help_url);
1604 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001605 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1606 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1607
Winson Chung70d72102011-08-12 11:24:35 -07001608 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1609 .setIcon(android.R.drawable.ic_menu_gallery)
1610 .setAlphabeticShortcut('W');
1611 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1612 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001613 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001614 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001615 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1616 .setIcon(android.R.drawable.ic_menu_preferences)
1617 .setIntent(settings)
1618 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001619 if (!helpUrl.isEmpty()) {
1620 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1621 .setIcon(android.R.drawable.ic_menu_help)
1622 .setIntent(help)
1623 .setAlphabeticShortcut('H');
1624 }
Winson Chung70d72102011-08-12 11:24:35 -07001625 return true;
1626 }
1627
1628 @Override
1629 public boolean onPrepareOptionsMenu(Menu menu) {
1630 super.onPrepareOptionsMenu(menu);
1631
1632 if (mAppsCustomizeTabHost.isTransitioning()) {
1633 return false;
1634 }
1635 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1636 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1637
1638 return true;
1639 }
1640
1641 @Override
1642 public boolean onOptionsItemSelected(MenuItem item) {
1643 switch (item.getItemId()) {
1644 case MENU_WALLPAPER_SETTINGS:
1645 startWallpaper();
1646 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001647 }
1648
1649 return super.onOptionsItemSelected(item);
1650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001651
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001652 @Override
1653 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001654 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001655 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001656 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001657 }
1658
Joe Onorato9c1289c2009-08-17 11:03:03 -04001659 public boolean isWorkspaceLocked() {
1660 return mWorkspaceLoading || mWaitingForResult;
1661 }
1662
Michael Jurka0280c3b2010-09-17 15:00:07 -07001663 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001664 mPendingAddInfo.container = ItemInfo.NO_ID;
1665 mPendingAddInfo.screen = -1;
1666 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1667 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001668 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001669 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001670 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001671
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001672 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1673 AppWidgetProviderInfo appWidgetInfo) {
1674 if (appWidgetInfo.configure != null) {
1675 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001676
Bjorn Bringert7984c942009-12-09 15:38:25 +00001677 // Launch over to configure widget, if needed
1678 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001679 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001680 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001681 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001682 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001683 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001684 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1685 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001686 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001687 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688 }
1689 }
Romain Guycbb89e42009-06-08 15:52:54 -07001690
Adam Cohenfbba09b2011-07-18 21:43:05 -07001691 /**
1692 * Process a shortcut drop.
1693 *
1694 * @param componentName The name of the component
1695 * @param screen The screen where it should be added
1696 * @param cell The cell it should be added to, optional
1697 * @param position The location on the screen where it was dropped, optional
1698 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001699 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1700 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001701 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001702 mPendingAddInfo.container = container;
1703 mPendingAddInfo.screen = screen;
1704 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001705
1706 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001707 mPendingAddInfo.cellX = cell[0];
1708 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001709 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001710
1711 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1712 createShortcutIntent.setComponent(componentName);
1713 processShortcut(createShortcutIntent);
1714 }
1715
Adam Cohenfbba09b2011-07-18 21:43:05 -07001716 /**
1717 * Process a widget drop.
1718 *
1719 * @param info The PendingAppWidgetInfo of the widget being added.
1720 * @param screen The screen where it should be added
1721 * @param cell The cell it should be added to, optional
1722 * @param position The location on the screen where it was dropped, optional
1723 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001724 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001725 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001726 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001727 mPendingAddInfo.container = info.container = container;
1728 mPendingAddInfo.screen = info.screen = screen;
1729 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001730 mPendingAddInfo.minSpanX = info.minSpanX;
1731 mPendingAddInfo.minSpanY = info.minSpanY;
1732
Adam Cohenfbba09b2011-07-18 21:43:05 -07001733 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001734 mPendingAddInfo.cellX = cell[0];
1735 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001736 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001737 if (span != null) {
1738 mPendingAddInfo.spanX = span[0];
1739 mPendingAddInfo.spanY = span[1];
1740 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001741
Adam Cohened66b2b2012-01-23 17:28:51 -08001742 AppWidgetHostView hostView = info.boundWidget;
1743 int appWidgetId;
1744 if (hostView != null) {
1745 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001746 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001747 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001748 // In this case, we either need to start an activity to get permission to bind
1749 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001750 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001751 Bundle options = info.bindOptions;
1752
1753 boolean success = false;
1754 if (options != null) {
1755 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1756 info.componentName, options);
1757 } else {
1758 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1759 info.componentName);
1760 }
1761 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001762 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001763 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001764 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001765 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1766 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1767 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001768 // TODO: we need to make sure that this accounts for the options bundle.
1769 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001770 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1771 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001772 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001773 }
1774
Joe Onoratodeb98af2010-02-19 14:59:39 -08001775 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001776 // Handle case where user selected "Applications"
1777 String applicationName = getResources().getString(R.string.group_applications);
1778 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001779
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001780 if (applicationName != null && applicationName.equals(shortcutName)) {
1781 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1782 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001783
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001784 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1785 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001786 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001787 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001788 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001789 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001790 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001791 }
1792
Winson Chung24ab2f12010-09-16 14:10:47 -07001793 void processWallpaper(Intent intent) {
1794 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1795 }
1796
Winson Chung3d503fb2011-07-13 17:25:49 -07001797 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1798 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001799 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 folderInfo.title = getText(R.string.folder_name);
1801
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001802 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001803 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1804 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001805 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806
1807 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001808 FolderIcon newFolder =
1809 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1810 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1811 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001812 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001813 }
Romain Guycbb89e42009-06-08 15:52:54 -07001814
Joe Onorato9c1289c2009-08-17 11:03:03 -04001815 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001816 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001817 }
1818
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001820 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001821 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001822 Intent chooser = Intent.createChooser(pickWallpaper,
1823 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001824 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1825 // Removed in Eclair MR1
1826// WallpaperManager wm = (WallpaperManager)
1827// getSystemService(Context.WALLPAPER_SERVICE);
1828// WallpaperInfo wi = wm.getWallpaperInfo();
1829// if (wi != null && wi.getSettingsActivity() != null) {
1830// LabeledIntent li = new LabeledIntent(getPackageName(),
1831// R.string.configure_wallpaper, 0);
1832// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1833// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1834// }
Mike Clerona0618e42009-10-22 13:55:21 -07001835 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001836 }
1837
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001838 /**
1839 * Registers various content observers. The current implementation registers
1840 * only a favorites observer to keep track of the favorites applications.
1841 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001842 private void registerContentObservers() {
1843 ContentResolver resolver = getContentResolver();
1844 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1845 true, mWidgetObserver);
1846 }
1847
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001848 @Override
1849 public boolean dispatchKeyEvent(KeyEvent event) {
1850 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1851 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001852 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001853 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001854 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001855 if (doesFileExist(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001856 dumpState();
1857 return true;
1858 }
1859 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001860 }
1861 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1862 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001863 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 return true;
1865 }
1866 }
1867
1868 return super.dispatchKeyEvent(event);
1869 }
1870
Joe Onorato88ec0992009-11-19 13:16:06 -08001871 @Override
1872 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001873 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001874 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001875 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001876 Folder openFolder = mWorkspace.getOpenFolder();
1877 if (openFolder.isEditingName()) {
1878 openFolder.dismissEditingName();
1879 } else {
1880 closeFolder();
1881 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001882 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001883 mWorkspace.exitWidgetResizeMode();
1884
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001885 // Back button is a no-op here, but give at least some feedback for the button press
1886 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001887 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001888 }
1889
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001890 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001891 * Re-listen when widgets are reset.
1892 */
1893 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001894 if (mAppWidgetHost != null) {
1895 mAppWidgetHost.startListening();
1896 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001897 }
1898
1899 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 * Launches the intent referred by the clicked shortcut.
1901 *
1902 * @param v The view representing the clicked shortcut.
1903 */
1904 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001905 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1906 // view has detached (it's possible for this to happen if the view is removed mid touch).
1907 if (v.getWindowToken() == null) {
1908 return;
1909 }
1910
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001911 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001912 return;
1913 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001914
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001916 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001917 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001918 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001919 int[] pos = new int[2];
1920 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001921 intent.setSourceBounds(new Rect(pos[0], pos[1],
1922 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001923
1924 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001925
1926 if (success && v instanceof BubbleTextView) {
1927 mWaitingForResume = (BubbleTextView) v;
1928 mWaitingForResume.setStayPressed(true);
1929 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001931 if (v instanceof FolderIcon) {
1932 FolderIcon fi = (FolderIcon) v;
1933 handleFolderClick(fi);
1934 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001935 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001936 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001937 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001938 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001939 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001940 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001941 }
1942 }
1943
Michael Jurka0e260592010-06-30 17:07:39 -07001944 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001945 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001946 // clicking anywhere on the workspace causes the customization drawer to slide down
1947 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001948 return false;
1949 }
1950
Michael Jurkaaf442092010-06-10 17:01:57 -07001951 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001952 * Event handler for the search button
1953 *
1954 * @param v The view that was clicked.
1955 */
1956 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001957 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1958
Amith Yamasania135ba82011-08-09 17:42:01 -07001959 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001960 }
1961
1962 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001963 * Event handler for the voice button
1964 *
1965 * @param v The view that was clicked.
1966 */
1967 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001968 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001969
Michael Jurkaae65ee32012-04-30 11:45:54 -07001970 try {
1971 final SearchManager searchManager =
1972 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1973 ComponentName activityName = searchManager.getGlobalSearchActivity();
1974 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001975 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001976 if (activityName != null) {
1977 intent.setPackage(activityName.getPackageName());
1978 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001979 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07001980 } catch (ActivityNotFoundException e) {
1981 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001982 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001983 startActivitySafely(null, intent, "onClickVoiceButton");
1984 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001985 }
1986
1987 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001988 * Event handler for the "grid" button that appears on the home screen, which
1989 * enters all apps mode.
1990 *
1991 * @param v The view that was clicked.
1992 */
1993 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001994 showAllApps(true);
1995 }
1996
1997 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001998 // Provide the same haptic feedback that the system offers for virtual keys.
1999 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002000 }
2001
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002002 public void onClickAppMarketButton(View v) {
2003 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002004 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002005 } else {
2006 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002007 }
2008 }
2009
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002010 void startApplicationDetailsActivity(ComponentName componentName) {
2011 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002012 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2013 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002014 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002015 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002016 }
2017
Patrick Dubroy5539af72010-09-07 15:22:01 -07002018 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2019 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2020 // System applications cannot be installed. For now, show a toast explaining that.
2021 // We may give them the option of disabling apps this way.
2022 int messageId = R.string.uninstall_system_app_text;
2023 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2024 } else {
2025 String packageName = appInfo.componentName.getPackageName();
2026 String className = appInfo.componentName.getClassName();
2027 Intent intent = new Intent(
2028 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002029 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2030 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002031 startActivity(intent);
2032 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002033 }
2034
Michael Jurka86a720e2012-05-09 11:23:23 -07002035 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002039 // Only launch using the new animation if the shortcut has not opted out (this is a
2040 // private contract between launcher and may be ignored in the future).
2041 boolean useLaunchAnimation = (v != null) &&
2042 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2043 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002044 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2045 v.getMeasuredWidth(), v.getMeasuredHeight());
2046
2047 startActivity(intent, opts.toBundle());
2048 } else {
2049 startActivity(intent);
2050 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002051 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002052 } catch (SecurityException e) {
2053 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002054 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002056 "or use the exported attribute for this activity. "
2057 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002059 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002061
Michael Jurka86a720e2012-05-09 11:23:23 -07002062 boolean startActivitySafely(View v, Intent intent, Object tag) {
2063 boolean success = false;
2064 try {
2065 success = startActivity(v, intent, tag);
2066 } catch (ActivityNotFoundException e) {
2067 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2068 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2069 }
2070 return success;
2071 }
2072
Romain Guy8e633c52010-03-04 12:51:36 -08002073 void startActivityForResultSafely(Intent intent, int requestCode) {
2074 try {
2075 startActivityForResult(intent, requestCode);
2076 } catch (ActivityNotFoundException e) {
2077 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2078 } catch (SecurityException e) {
2079 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2080 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2081 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2082 "or use the exported attribute for this activity.", e);
2083 }
2084 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085
Adam Cohena9cf38f2011-05-02 15:36:58 -07002086 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002087 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002088 Folder openFolder = mWorkspace.getFolderForTag(info);
2089
2090 // If the folder info reports that the associated folder is open, then verify that
2091 // it is actually opened. There have been a few instances where this gets out of sync.
2092 if (info.opened && openFolder == null) {
2093 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2094 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2095 info.opened = false;
2096 }
2097
Adam Cohenfb91f302012-06-11 15:45:18 -07002098 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 // Close any open folder
2100 closeFolder();
2101 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002102 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 } else {
2104 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 int folderScreen;
2106 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002107 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 // .. and close it
2109 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002110 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 // Close any folder open on the current screen
2112 closeFolder();
2113 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002114 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002115 }
2116 }
2117 }
2118 }
2119
Adam Cohen268c4752012-06-06 17:47:33 -07002120 /**
2121 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2122 * in the DragLayer in the exact absolute location of the original FolderIcon.
2123 */
2124 private void copyFolderIconToImage(FolderIcon fi) {
2125 final int width = fi.getMeasuredWidth();
2126 final int height = fi.getMeasuredHeight();
2127
2128 // Lazy load ImageView, Bitmap and Canvas
2129 if (mFolderIconImageView == null) {
2130 mFolderIconImageView = new ImageView(this);
2131 }
2132 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2133 mFolderIconBitmap.getHeight() != height) {
2134 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2135 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2136 }
2137
2138 DragLayer.LayoutParams lp;
2139 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2140 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2141 } else {
2142 lp = new DragLayer.LayoutParams(width, height);
2143 }
2144
Adam Cohen307fe232012-08-16 17:55:58 -07002145 // The layout from which the folder is being opened may be scaled, adjust the starting
2146 // view size by this scale factor.
2147 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002148 lp.customPosition = true;
2149 lp.x = mRectForFolderAnimation.left;
2150 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002151 lp.width = (int) (scale * width);
2152 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002153
2154 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2155 fi.draw(mFolderIconCanvas);
2156 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002157 if (fi.getFolder() != null) {
2158 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2159 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002160 }
Adam Cohen268c4752012-06-06 17:47:33 -07002161 // Just in case this image view is still in the drag layer from a previous animation,
2162 // we remove it and re-add it.
2163 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2164 mDragLayer.removeView(mFolderIconImageView);
2165 }
2166 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002167 if (fi.getFolder() != null) {
2168 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002169 }
Adam Cohen268c4752012-06-06 17:47:33 -07002170 }
2171
Adam Cohen2801caf2011-05-13 20:57:39 -07002172 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002173 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002174 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2175 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2176 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2177
Adam Cohenc51934b2011-07-26 21:07:43 -07002178 FolderInfo info = (FolderInfo) fi.getTag();
2179 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2180 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002181 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2182 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002183 }
2184
Adam Cohen268c4752012-06-06 17:47:33 -07002185 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2186 copyFolderIconToImage(fi);
2187 fi.setVisibility(View.INVISIBLE);
2188
Michael Jurka2ecf9952012-06-18 12:52:28 -07002189 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002190 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002191 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2192 oa.start();
2193 }
2194
Adam Cohen268c4752012-06-06 17:47:33 -07002195 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002196 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002197 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2198 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2199 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2200
Adam Cohen268c4752012-06-06 17:47:33 -07002201 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002202
Adam Cohen268c4752012-06-06 17:47:33 -07002203 // We remove and re-draw the FolderIcon in-case it has changed
2204 mDragLayer.removeView(mFolderIconImageView);
2205 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002206 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002207 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002208 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002209 oa.addListener(new AnimatorListenerAdapter() {
2210 @Override
2211 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002212 if (cl != null) {
2213 cl.clearFolderLeaveBehind();
2214 // Remove the ImageView copy of the FolderIcon and make the original visible.
2215 mDragLayer.removeView(mFolderIconImageView);
2216 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002217 }
2218 }
2219 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002220 oa.start();
2221 }
2222
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002224 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002225 * is animated relative to the specified View. If the View is null, no animation
2226 * is played.
2227 *
2228 * @param folderInfo The FolderInfo describing the folder to open.
2229 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002230 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002231 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002232 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233
Adam Cohena9cf38f2011-05-02 15:36:58 -07002234 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002235
Adam Cohen4554ee12011-08-03 16:13:21 -07002236 // Just verify that the folder hasn't already been added to the DragLayer.
2237 // There was a one-off crash where the folder had a parent already.
2238 if (folder.getParent() == null) {
2239 mDragLayer.addView(folder);
2240 mDragController.addDropTarget((DropTarget) folder);
2241 } else {
2242 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2243 folder.getParent() + ").");
2244 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002245 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002246 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002247 }
2248
2249 public void closeFolder() {
2250 Folder folder = mWorkspace.getOpenFolder();
2251 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002252 if (folder.isEditingName()) {
2253 folder.dismissEditingName();
2254 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002255 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002256
2257 // Dismiss the folder cling
2258 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002259 }
2260 }
2261
2262 void closeFolder(Folder folder) {
2263 folder.getInfo().opened = false;
2264
2265 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2266 if (parent != null) {
2267 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2268 shrinkAndFadeInFolderIcon(fi);
2269 }
2270 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 }
2272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002274 if (!isDraggingEnabled()) return false;
2275 if (isWorkspaceLocked()) return false;
2276 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002277
2278 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002279 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002280 }
2281
Michael Jurka0280c3b2010-09-17 15:00:07 -07002282 resetAddInfo();
2283 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002285 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002286 return true;
2287 }
2288
Winson Chung3d503fb2011-07-13 17:25:49 -07002289 // The hotseat touch handling does not go through Workspace, and we always allow long press
2290 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002291 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002292 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2293 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002294 if (itemUnderLongClick == null) {
2295 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002296 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2297 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002298 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002300 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002302 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 }
2304 }
2305 }
2306 return true;
2307 }
2308
Winson Chung3d503fb2011-07-13 17:25:49 -07002309 boolean isHotseatLayout(View layout) {
2310 return mHotseat != null && layout != null &&
2311 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2312 }
2313 Hotseat getHotseat() {
2314 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002315 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002316 SearchDropTargetBar getSearchBar() {
2317 return mSearchDropTargetBar;
2318 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002319
Winson Chung3d503fb2011-07-13 17:25:49 -07002320 /**
2321 * Returns the CellLayout of the specified container at the specified screen.
2322 */
2323 CellLayout getCellLayout(long container, int screen) {
2324 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2325 if (mHotseat != null) {
2326 return mHotseat.getLayout();
2327 } else {
2328 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002329 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002330 } else {
2331 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002332 }
2333 }
2334
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002335 Workspace getWorkspace() {
2336 return mWorkspace;
2337 }
2338
Daniel Sandler843e8602010-06-07 14:59:01 -04002339 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2340 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002341 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002342 }
2343
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002344 public boolean isAllAppsButtonRank(int rank) {
2345 return mHotseat.isAllAppsButtonRank(rank);
2346 }
2347
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002348 /**
2349 * Helper method for the cameraZoomIn/cameraZoomOut animations
2350 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002351 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002352 * @param scaleFactor The scale factor used for the zoom
2353 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002354 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002355 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002356 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002357 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002358
Winson Chung18f41f82012-05-09 13:28:10 -07002359 void disableWallpaperIfInAllApps() {
2360 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002361 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002362 if (mAppsCustomizeTabHost != null &&
2363 !mAppsCustomizeTabHost.isTransitioning()) {
2364 updateWallpaperVisibility(false);
2365 }
2366 }
2367 }
2368
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002369 void updateWallpaperVisibility(boolean visible) {
2370 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2371 int curflags = getWindow().getAttributes().flags
2372 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2373 if (wpflags != curflags) {
2374 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2375 }
2376 }
2377
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002378 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2379 if (v instanceof LauncherTransitionable) {
2380 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2381 }
2382 }
2383
Michael Jurkabed61d22012-02-14 22:51:29 -08002384 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2385 if (v instanceof LauncherTransitionable) {
2386 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2387 }
Winson Chung70442722012-02-10 15:43:22 -08002388
2389 // Update the workspace transition step as well
2390 dispatchOnLauncherTransitionStep(v, 0f);
2391 }
2392
2393 private void dispatchOnLauncherTransitionStep(View v, float t) {
2394 if (v instanceof LauncherTransitionable) {
2395 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2396 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002397 }
2398
2399 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2400 if (v instanceof LauncherTransitionable) {
2401 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2402 }
Winson Chung70442722012-02-10 15:43:22 -08002403
2404 // Update the workspace transition step as well
2405 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002406 }
2407
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002408 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002409 * Things to test when changing the following seven functions.
2410 * - Home from workspace
2411 * - from center screen
2412 * - from other screens
2413 * - Home from all apps
2414 * - from center screen
2415 * - from other screens
2416 * - Back from all apps
2417 * - from center screen
2418 * - from other screens
2419 * - Launch app from workspace and quit
2420 * - with back
2421 * - with home
2422 * - Launch app from all apps and quit
2423 * - with back
2424 * - with home
2425 * - Go to a screen that's not the default, then all
2426 * apps, and launch and app, and go back
2427 * - with back
2428 * -with home
2429 * - On workspace, long press power and go back
2430 * - with back
2431 * - with home
2432 * - On all apps, long press power and go back
2433 * - with back
2434 * - with home
2435 * - On workspace, power off
2436 * - On all apps, power off
2437 * - Launch an app and turn off the screen while in that app
2438 * - Go back with home key
2439 * - Go back with back key TODO: make this not go to workspace
2440 * - From all apps
2441 * - From workspace
2442 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2443 * - From all apps
2444 * - From the center workspace
2445 * - From another workspace
2446 */
2447
2448 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002449 * Zoom the camera out from the workspace to reveal 'toView'.
2450 * Assumes that the view to show is anchored at either the very top or very bottom
2451 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002452 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002453 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002454 if (mStateAnimation != null) {
2455 mStateAnimation.cancel();
2456 mStateAnimation = null;
2457 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002458 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002459
Winson Chungf0ea4d32011-06-06 14:27:16 -07002460 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2461 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2462 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002463 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002464 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002465 final int startDelay =
2466 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002467
Michael Jurkab3e22d92011-10-31 15:58:33 -07002468 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002469
Michael Jurkad74c9842011-07-10 12:44:21 -07002470 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002471 Animator workspaceAnim =
2472 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002473
2474 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002475 toView.setScaleX(scale);
2476 toView.setScaleY(scale);
2477 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2478 scaleAnim.
2479 scaleX(1f).scaleY(1f).
2480 setDuration(duration).
2481 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002482
Winson Chungf0ea4d32011-06-06 14:27:16 -07002483 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002484 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002485 final ObjectAnimator alphaAnim = ObjectAnimator
2486 .ofFloat(toView, "alpha", 0f, 1f)
2487 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002488 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002489 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2490 @Override
2491 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002492 if (animation == null) {
2493 throw new RuntimeException("animation is null");
2494 }
Winson Chung70442722012-02-10 15:43:22 -08002495 float t = (Float) animation.getAnimatedValue();
2496 dispatchOnLauncherTransitionStep(fromView, t);
2497 dispatchOnLauncherTransitionStep(toView, t);
2498 }
2499 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002500
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002501 // toView should appear right at the end of the workspace shrink
2502 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002503 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002504 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002505 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002506
2507 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002508 boolean animationCancelled = false;
2509
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002510 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002511 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002512 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002513 // Prepare the position
2514 toView.setTranslationX(0.0f);
2515 toView.setTranslationY(0.0f);
2516 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002517 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002518 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002519 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002520 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002521 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2522 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002523
Michael Jurkafa7969f2012-09-21 16:39:14 -07002524 if (mWorkspace != null && !springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002525 // Hide the workspace scrollbar
2526 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002527 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002528 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002529 if (!animationCancelled) {
2530 updateWallpaperVisibility(false);
2531 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002532
2533 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002534 if (mSearchDropTargetBar != null) {
2535 mSearchDropTargetBar.hideSearchBar(false);
2536 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002537 }
2538
Adam Cohencff6af82011-09-13 14:51:53 -07002539 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002540 public void onAnimationCancel(Animator animation) {
2541 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002542 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002543 });
2544
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002545 if (workspaceAnim != null) {
2546 mStateAnimation.play(workspaceAnim);
2547 }
Michael Jurka1899a362011-11-03 13:50:45 -07002548
2549 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002550 final ViewTreeObserver observer;
2551
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002552 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2553 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002554
2555 // If any of the objects being animated haven't been measured/laid out
2556 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002557 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002558 (mWorkspace.getMeasuredWidth() == 0) ||
2559 (toView.getMeasuredWidth() == 0)) {
2560 observer = mWorkspace.getViewTreeObserver();
2561 delayAnim = true;
2562 } else {
2563 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002564 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002565
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002566 final AnimatorSet stateAnimation = mStateAnimation;
2567 final Runnable startAnimRunnable = new Runnable() {
2568 public void run() {
2569 // Check that mStateAnimation hasn't changed while
2570 // we waited for a layout/draw pass
2571 if (mStateAnimation != stateAnimation)
2572 return;
2573 setPivotsForZoom(toView, scale);
2574 dispatchOnLauncherTransitionStart(fromView, animated, false);
2575 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka382aa182012-06-11 15:42:07 -07002576 toView.post(new Runnable() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002577 public void run() {
2578 // Check that mStateAnimation hasn't changed while
2579 // we waited for a layout/draw pass
2580 if (mStateAnimation != stateAnimation)
2581 return;
2582 mStateAnimation.start();
2583 }
2584 });
2585 }
2586 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002587 if (delayAnim) {
2588 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2589 public void onGlobalLayout() {
Michael Jurka382aa182012-06-11 15:42:07 -07002590 toView.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002591 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002592 }
2593 };
2594 observer.addOnGlobalLayoutListener(delayedStart);
2595 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002596 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002597 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002598 } else {
2599 toView.setTranslationX(0.0f);
2600 toView.setTranslationY(0.0f);
2601 toView.setScaleX(1.0f);
2602 toView.setScaleY(1.0f);
2603 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002604 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002605
Michael Jurkabed61d22012-02-14 22:51:29 -08002606 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2607 // Hide the workspace scrollbar
2608 mWorkspace.hideScrollingIndicator(true);
2609 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002610
2611 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002612 if (mSearchDropTargetBar != null) {
2613 mSearchDropTargetBar.hideSearchBar(false);
2614 }
Michael Jurkaabded662011-03-04 12:06:57 -08002615 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002616 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002617 dispatchOnLauncherTransitionStart(fromView, animated, false);
2618 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002619 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002620 dispatchOnLauncherTransitionStart(toView, animated, false);
2621 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002622 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002623 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002624 }
2625
2626 /**
2627 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002628 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002629 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002630 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002631 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2632 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002633
Michael Jurkab3e22d92011-10-31 15:58:33 -07002634 if (mStateAnimation != null) {
2635 mStateAnimation.cancel();
2636 mStateAnimation = null;
2637 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002638 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002639
Winson Chungf0ea4d32011-06-06 14:27:16 -07002640 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002641 final int fadeOutDuration =
2642 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002643 final float scaleFactor = (float)
2644 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2645 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002646 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002647 Animator workspaceAnim = null;
2648
2649 if (toState == State.WORKSPACE) {
2650 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2651 workspaceAnim = mWorkspace.getChangeStateAnimation(
2652 Workspace.State.NORMAL, animated, stagger);
2653 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2654 workspaceAnim = mWorkspace.getChangeStateAnimation(
2655 Workspace.State.SPRING_LOADED, animated);
2656 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002657
Michael Jurkab3e22d92011-10-31 15:58:33 -07002658 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002659 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002660 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002661 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002662 final LauncherViewPropertyAnimator scaleAnim =
2663 new LauncherViewPropertyAnimator(fromView);
2664 scaleAnim.
2665 scaleX(scaleFactor).scaleY(scaleFactor).
2666 setDuration(duration).
2667 setInterpolator(new Workspace.ZoomInInterpolator());
2668
2669 final ObjectAnimator alphaAnim = ObjectAnimator
2670 .ofFloat(fromView, "alpha", 1f, 0f)
2671 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002672 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002673 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2674 @Override
2675 public void onAnimationUpdate(ValueAnimator animation) {
2676 float t = 1f - (Float) animation.getAnimatedValue();
2677 dispatchOnLauncherTransitionStep(fromView, t);
2678 dispatchOnLauncherTransitionStep(toView, t);
2679 }
2680 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002681
Michael Jurka2ecf9952012-06-18 12:52:28 -07002682 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002683
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002684 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2685 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002686
2687 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002688 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002689 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002690 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002691 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002692 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2693 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002694 if (mWorkspace != null) {
2695 mWorkspace.hideScrollingIndicator(false);
2696 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002697 if (onCompleteRunnable != null) {
2698 onCompleteRunnable.run();
2699 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002700 }
2701 });
2702
Winson Chungf0ea4d32011-06-06 14:27:16 -07002703 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002704 if (workspaceAnim != null) {
2705 mStateAnimation.play(workspaceAnim);
2706 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002707 dispatchOnLauncherTransitionStart(fromView, animated, true);
2708 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002709 final Animator stateAnimation = mStateAnimation;
2710 mWorkspace.post(new Runnable() {
2711 public void run() {
2712 if (stateAnimation != mStateAnimation)
2713 return;
2714 mStateAnimation.start();
2715 }
2716 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002717 } else {
2718 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002719 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002720 dispatchOnLauncherTransitionStart(fromView, animated, true);
2721 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002722 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002723 dispatchOnLauncherTransitionStart(toView, animated, true);
2724 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002725 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002726 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002727 }
2728
Michael Jurkae326f182011-11-21 14:05:46 -08002729 @Override
2730 public void onTrimMemory(int level) {
2731 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002732 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002733 mAppsCustomizeTabHost.onTrimMemory();
2734 }
2735 }
2736
Winson Chung18f41f82012-05-09 13:28:10 -07002737 @Override
2738 public void onWindowFocusChanged(boolean hasFocus) {
2739 if (!hasFocus) {
2740 // When another window occludes launcher (like the notification shade, or recents),
2741 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2742 updateWallpaperVisibility(true);
2743 } else {
2744 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002745 mWorkspace.postDelayed(new Runnable() {
2746 @Override
2747 public void run() {
2748 disableWallpaperIfInAllApps();
2749 }
2750 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002751 }
2752 }
2753
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002754 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002755 showWorkspace(animated, null);
2756 }
2757
2758 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002759 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002760 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002761 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002762 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002763
Winson Chungc7d2b602012-05-16 17:02:20 -07002764 // Show the search bar (only animate if we were showing the drop target bar in spring
2765 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002766 if (mSearchDropTargetBar != null) {
2767 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2768 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002769
Michael Jurkab737ee62011-11-15 15:57:22 -08002770 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002771 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002772
2773 // Set focus to the AppsCustomize button
2774 if (mAllAppsButton != null) {
2775 mAllAppsButton.requestFocus();
2776 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002777 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002778
Michael Jurkab737ee62011-11-15 15:57:22 -08002779 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002780
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002781 // Change the state *after* we've called all the transition code
2782 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002783
Winson Chung5fb63472011-02-02 17:03:37 -08002784 // Resume the auto-advance of widgets
2785 mUserPresent = true;
2786 updateRunning();
2787
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002788 // send an accessibility event to announce the context change
2789 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002790 }
2791
Michael Jurkab3e22d92011-10-31 15:58:33 -07002792 void showAllApps(boolean animated) {
2793 if (mState != State.WORKSPACE) return;
2794
2795 showAppsCustomizeHelper(animated, false);
2796 mAppsCustomizeTabHost.requestFocus();
2797
Michael Jurkab3e22d92011-10-31 15:58:33 -07002798 // Change the state *after* we've called all the transition code
2799 mState = State.APPS_CUSTOMIZE;
2800
2801 // Pause the auto-advance of widgets until we are out of AllApps
2802 mUserPresent = false;
2803 updateRunning();
2804 closeFolder();
2805
2806 // Send an accessibility event to announce the context change
2807 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2808 }
2809
Adam Cohen7777d962011-08-18 18:58:38 -07002810 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002811 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002812 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002813 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002814 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002815 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002816 }
Adam Cohen7777d962011-08-18 18:58:38 -07002817
Adam Cohened66b2b2012-01-23 17:28:51 -08002818 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2819 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002820 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2821
Winson Chunge7a03942011-08-05 15:05:12 -07002822 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002823 @Override
2824 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002825 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002826 // Before we show workspace, hide all apps again because
2827 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2828 // clean up our state transition functions
2829 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002830 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002831 } else {
2832 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002833 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002834 }
2835 }, (extendedDelay ?
2836 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2837 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2838 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002839
Michael Jurkad3ef3062010-11-23 16:23:58 -08002840 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002841 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002842 final boolean animated = true;
2843 final boolean springLoaded = true;
2844 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002845 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002846 }
2847 // Otherwise, we are not in spring loaded mode, so don't do anything.
2848 }
2849
Michael Jurkab737ee62011-11-15 15:57:22 -08002850 void hideDockDivider() {
2851 if (mQsbDivider != null && mDockDivider != null) {
2852 mQsbDivider.setVisibility(View.INVISIBLE);
2853 mDockDivider.setVisibility(View.INVISIBLE);
2854 }
2855 }
2856
2857 void showDockDivider(boolean animated) {
2858 if (mQsbDivider != null && mDockDivider != null) {
2859 mQsbDivider.setVisibility(View.VISIBLE);
2860 mDockDivider.setVisibility(View.VISIBLE);
2861 if (mDividerAnimator != null) {
2862 mDividerAnimator.cancel();
2863 mQsbDivider.setAlpha(1f);
2864 mDockDivider.setAlpha(1f);
2865 mDividerAnimator = null;
2866 }
2867 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002868 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2869 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2870 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07002871 int duration = 0;
2872 if (mSearchDropTargetBar != null) {
2873 duration = mSearchDropTargetBar.getTransitionInDuration();
2874 }
2875 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08002876 mDividerAnimator.start();
2877 }
2878 }
2879 }
2880
Michael Jurkab3e22d92011-10-31 15:58:33 -07002881 void lockAllApps() {
2882 // TODO
2883 }
2884
2885 void unlockAllApps() {
2886 // TODO
2887 }
2888
Winson Chungf0ea4d32011-06-06 14:27:16 -07002889 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002890 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002891 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002892 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002893 if (!LauncherApplication.isScreenLarge()) {
2894 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002895 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002896 int duration = 0;
2897 if (mSearchDropTargetBar != null) {
2898 duration = mSearchDropTargetBar.getTransitionInDuration();
2899 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002900 mHotseat.animate().alpha(1f).setDuration(duration);
2901 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002902 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002903 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002904 }
2905 }
2906 }
2907
2908 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002909 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002910 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002911 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002912 if (!LauncherApplication.isScreenLarge()) {
2913 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002914 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002915 int duration = 0;
2916 if (mSearchDropTargetBar != null) {
2917 duration = mSearchDropTargetBar.getTransitionOutDuration();
2918 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002919 mHotseat.animate().alpha(0f).setDuration(duration);
2920 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002921 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002922 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002923 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002924 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002925 }
2926
Patrick Dubroy5f445422011-02-18 14:35:21 -08002927 /**
2928 * Add an item from all apps or customize onto the given workspace screen.
2929 * If layout is null, add to the current screen.
2930 */
2931 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002932 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002933 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002934 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002935 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002936
Winson Chungdff8ebb2011-09-08 17:25:31 -07002937 /** Maps the current orientation to an index for referencing orientation correct global icons */
2938 private int getCurrentOrientationIndexForGlobalIcons() {
2939 // default - 0, landscape - 1
2940 switch (getResources().getConfiguration().orientation) {
2941 case Configuration.ORIENTATION_LANDSCAPE:
2942 return 1;
2943 default:
2944 return 0;
2945 }
2946 }
2947
Michael Jurkaae65ee32012-04-30 11:45:54 -07002948 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002949 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002950 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002951 // Look for the toolbar icon specified in the activity meta-data
2952 Bundle metaData = packageManager.getActivityInfo(
2953 activityName, PackageManager.GET_META_DATA).metaData;
2954 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002955 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002956 if (iconResId != 0) {
2957 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002958 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002959 }
2960 }
2961 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002962 // This can happen if the activity defines an invalid drawable
2963 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2964 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002965 } catch (Resources.NotFoundException nfe) {
2966 // This can happen if the activity defines an invalid drawable
2967 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2968 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002969 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002970 return null;
2971 }
2972
2973 // if successful in getting icon, return it; otherwise, set button to use default drawable
2974 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002975 int buttonId, ComponentName activityName, int fallbackDrawableId,
2976 String toolbarResourceName) {
2977 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002978 Resources r = getResources();
2979 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2980 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002981
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002982 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002983 // If we were unable to find the icon via the meta-data, use a generic one
2984 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002985 toolbarIcon = r.getDrawable(fallbackDrawableId);
2986 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002987 if (button != null) {
2988 button.setCompoundDrawables(toolbarIcon, null, null, null);
2989 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002990 return null;
2991 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002992 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002993 if (button != null) {
2994 button.setCompoundDrawables(toolbarIcon, null, null, null);
2995 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002996 return toolbarIcon.getConstantState();
2997 }
2998 }
2999
3000 // if successful in getting icon, return it; otherwise, set button to use default drawable
3001 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003002 int buttonId, ComponentName activityName, int fallbackDrawableId,
3003 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003004 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003005 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003006
Michael Jurka19e0fc52011-07-22 18:00:21 -07003007 if (button != null) {
3008 // If we were unable to find the icon via the meta-data, use a
3009 // generic one
3010 if (toolbarIcon == null) {
3011 button.setImageResource(fallbackDrawableId);
3012 } else {
3013 button.setImageDrawable(toolbarIcon);
3014 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003015 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003016
3017 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3018
Michael Jurka0423dcf2010-10-05 14:56:18 -07003019 }
3020
Winson Chung1b884092012-06-08 17:13:02 -07003021 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003022 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003023 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003024 }
3025
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003026 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003027 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003028 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003029 }
3030
Winson Chungbb185bd2011-11-21 12:31:42 -08003031 private void invalidatePressedFocusedStates(View container, View button) {
3032 if (container instanceof HolographicLinearLayout) {
3033 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3034 layout.invalidatePressedFocusedStates();
3035 } else if (button instanceof HolographicImageView) {
3036 HolographicImageView view = (HolographicImageView) button;
3037 view.invalidatePressedFocusedStates();
3038 }
3039 }
3040
Winson Chungc51db6a2011-10-05 11:44:49 -07003041 private boolean updateGlobalSearchIcon() {
3042 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003043 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003044 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003045 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003046 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003047
Winson Chung1cad91e2011-05-25 17:41:01 -07003048 final SearchManager searchManager =
3049 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3050 ComponentName activityName = searchManager.getGlobalSearchActivity();
3051 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003052 int coi = getCurrentOrientationIndexForGlobalIcons();
3053 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003054 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3055 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3056 if (sGlobalSearchIcon[coi] == null) {
3057 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3058 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3059 TOOLBAR_ICON_METADATA_NAME);
3060 }
3061
Winson Chungc51db6a2011-10-05 11:44:49 -07003062 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3063 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003064 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003065 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003066 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003067 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003068 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3069 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3070 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003071 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003072 if (voiceButtonProxy != null) {
3073 voiceButtonProxy.setVisibility(View.GONE);
3074 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003075 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003076 }
3077 }
3078
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003079 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003080 final View searchButtonContainer = findViewById(R.id.search_button_container);
3081 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003082 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003083 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003084 }
3085
Winson Chungc51db6a2011-10-05 11:44:49 -07003086 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003087 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003088 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003089 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003090
Winson Chungc51db6a2011-10-05 11:44:49 -07003091 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003092 final SearchManager searchManager =
3093 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3094 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3095
3096 ComponentName activityName = null;
3097 if (globalSearchActivity != null) {
3098 // Check if the global search activity handles voice search
3099 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3100 intent.setPackage(globalSearchActivity.getPackageName());
3101 activityName = intent.resolveActivity(getPackageManager());
3102 }
3103
3104 if (activityName == null) {
3105 // Fallback: check if an activity other than the global search activity
3106 // resolves this
3107 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3108 activityName = intent.resolveActivity(getPackageManager());
3109 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003110 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003111 int coi = getCurrentOrientationIndexForGlobalIcons();
3112 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003113 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3114 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3115 if (sVoiceSearchIcon[coi] == null) {
3116 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3117 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3118 TOOLBAR_ICON_METADATA_NAME);
3119 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003120 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003121 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003122 if (voiceButtonProxy != null) {
3123 voiceButtonProxy.setVisibility(View.VISIBLE);
3124 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003125 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003126 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003127 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003128 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003129 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003130 if (voiceButtonProxy != null) {
3131 voiceButtonProxy.setVisibility(View.GONE);
3132 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003133 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003134 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003135 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003136
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003137 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003138 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3139 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003140 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003141 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003142 }
3143
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003144 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003145 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003146 */
3147 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003148 final View marketButton = findViewById(R.id.market_button);
3149 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3150 // Find the app market activity by resolving an intent.
3151 // (If multiple app markets are installed, it will return the ResolverActivity.)
3152 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003153 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003154 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003155 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003156 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003157 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3158 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003159 marketButton.setVisibility(View.VISIBLE);
3160 } else {
3161 // We should hide and disable the view so that we don't try and restore the visibility
3162 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3163 marketButton.setVisibility(View.GONE);
3164 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003165 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003166 }
3167
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003168 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003169 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3170 Resources r = getResources();
3171 Drawable marketIconDrawable = d.newDrawable(r);
3172 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3173 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3174 marketIconDrawable.setBounds(0, 0, w, h);
3175
3176 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003177 }
3178
Michael Jurkad7c28052012-04-27 15:43:36 -07003179 @Override
3180 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3181 boolean result = super.dispatchPopulateAccessibilityEvent(event);
3182 final List<CharSequence> text = event.getText();
3183 text.clear();
3184 text.add(getString(R.string.home));
3185 return result;
3186 }
3187
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003188 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003189 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003190 */
3191 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3192 @Override
3193 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003194 closeSystemDialogs();
3195 }
3196 }
3197
3198 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003199 * Receives notifications whenever the appwidgets are reset.
3200 */
3201 private class AppWidgetResetObserver extends ContentObserver {
3202 public AppWidgetResetObserver() {
3203 super(new Handler());
3204 }
3205
3206 @Override
3207 public void onChange(boolean selfChange) {
3208 onAppWidgetReset();
3209 }
3210 }
3211
3212 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003213 * If the activity is currently paused, signal that we need to re-run the loader
3214 * in onResume.
3215 *
3216 * This needs to be called from incoming places where resources might have been loaded
3217 * while we are paused. That is becaues the Configuration might be wrong
3218 * when we're not running, and if it comes back to what it was when we
3219 * were paused, we are not restarted.
3220 *
3221 * Implementation of the method from LauncherModel.Callbacks.
3222 *
3223 * @return true if we are currently paused. The caller might be able to
3224 * skip some work in that case since we will come back again.
3225 */
3226 public boolean setLoadOnResume() {
3227 if (mPaused) {
3228 Log.i(TAG, "setLoadOnResume");
3229 mOnResumeNeedsLoad = true;
3230 return true;
3231 } else {
3232 return false;
3233 }
3234 }
3235
3236 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003237 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003239 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003240 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003241 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003242 } else {
3243 return SCREEN_COUNT / 2;
3244 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003245 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003246
Joe Onorato9c1289c2009-08-17 11:03:03 -04003247 /**
3248 * Refreshes the shortcuts shown on the workspace.
3249 *
3250 * Implementation of the method from LauncherModel.Callbacks.
3251 */
3252 public void startBinding() {
3253 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003254
Winson Chungf0c6ae02012-03-21 16:10:31 -07003255 mNewShortcutAnimatePage = -1;
3256 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003257 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003258 int count = workspace.getChildCount();
3259 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003260 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003261 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3262 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003263 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003264 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003265 if (mHotseat != null) {
3266 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003267 }
3268 }
3269
3270 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003271 * Bind the items start-end from the list.
3272 *
3273 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003274 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003275 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003276 setLoadOnResume();
3277
Winson Chungf0c6ae02012-03-21 16:10:31 -07003278 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3279 Set<String> newApps = new HashSet<String>();
3280 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3281
3282 Workspace workspace = mWorkspace;
3283 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003284 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003285
3286 // Short circuit if we are loading dock items for a configuration which has no dock
3287 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3288 mHotseat == null) {
3289 continue;
3290 }
3291
Joe Onorato9c1289c2009-08-17 11:03:03 -04003292 switch (item.itemType) {
3293 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3294 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003295 ShortcutInfo info = (ShortcutInfo) item;
3296 String uri = info.intent.toUri(0).toString();
3297 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003298 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3299 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003300 boolean animateIconUp = false;
3301 synchronized (newApps) {
3302 if (newApps.contains(uri)) {
3303 animateIconUp = newApps.remove(uri);
3304 }
3305 }
3306 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003307 // Prepare the view to be animated up
3308 shortcut.setAlpha(0f);
3309 shortcut.setScaleX(0f);
3310 shortcut.setScaleY(0f);
3311 mNewShortcutAnimatePage = item.screen;
3312 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3313 mNewShortcutAnimateViews.add(shortcut);
3314 }
3315 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003316 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003317 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003318 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003319 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003320 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003321 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3322 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003323 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003324 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003325 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003326
Joe Onorato9c1289c2009-08-17 11:03:03 -04003327 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003328 }
3329
Joe Onorato9c1289c2009-08-17 11:03:03 -04003330 /**
3331 * Implementation of the method from LauncherModel.Callbacks.
3332 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003333 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003334 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003335 sFolders.clear();
3336 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003337 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003338
3339 /**
3340 * Add the views for a widget to the workspace.
3341 *
3342 * Implementation of the method from LauncherModel.Callbacks.
3343 */
3344 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003345 setLoadOnResume();
3346
Daniel Sandler843e8602010-06-07 14:59:01 -04003347 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3348 if (DEBUG_WIDGETS) {
3349 Log.d(TAG, "bindAppWidget: " + item);
3350 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003351 final Workspace workspace = mWorkspace;
3352
3353 final int appWidgetId = item.appWidgetId;
3354 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003355 if (DEBUG_WIDGETS) {
3356 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3357 }
3358
Joe Onorato9c1289c2009-08-17 11:03:03 -04003359 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3360
Joe Onorato9c1289c2009-08-17 11:03:03 -04003361 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003362 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003363
Winson Chung3d503fb2011-07-13 17:25:49 -07003364 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003365 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003366 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3367
Joe Onorato9c1289c2009-08-17 11:03:03 -04003368 workspace.requestLayout();
3369
Daniel Sandler843e8602010-06-07 14:59:01 -04003370 if (DEBUG_WIDGETS) {
3371 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3372 + (SystemClock.uptimeMillis()-start) + "ms");
3373 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003374 }
3375
Adam Cohen1462de32012-07-24 22:34:36 -07003376 public void onPageBoundSynchronously(int page) {
3377 mSynchronouslyBoundPages.add(page);
3378 }
3379
Joe Onorato9c1289c2009-08-17 11:03:03 -04003380 /**
3381 * Callback saying that there aren't any more items to bind.
3382 *
3383 * Implementation of the method from LauncherModel.Callbacks.
3384 */
3385 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003386 setLoadOnResume();
3387
Joe Onorato9c1289c2009-08-17 11:03:03 -04003388 if (mSavedState != null) {
3389 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003390 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003391 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003392 mSavedState = null;
3393 }
3394
Adam Cohen1462de32012-07-24 22:34:36 -07003395 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003396
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003397 // If we received the result of any pending adds while the loader was running (e.g. the
3398 // widget configuration forced an orientation change), process them now.
3399 for (int i = 0; i < sPendingAddList.size(); i++) {
3400 completeAdd(sPendingAddList.get(i));
3401 }
3402 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003403
Winson Chungc51db6a2011-10-05 11:44:49 -07003404 // Update the market app icon as necessary (the other icons will be managed in response to
3405 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003406 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003407
Winson Chungf0c6ae02012-03-21 16:10:31 -07003408 // Animate up any icons as necessary
3409 if (mVisible || mWorkspaceLoading) {
3410 Runnable newAppsRunnable = new Runnable() {
3411 @Override
3412 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003413 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003414 }
3415 };
Winson Chunga2413752012-04-03 14:22:34 -07003416
3417 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3418 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3419 if (canRunNewAppsAnimation()) {
3420 // If the user has not interacted recently, then either snap to the new page to show
3421 // the new-apps animation or just run them if they are to appear on the current page
3422 if (willSnapPage) {
3423 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3424 } else {
3425 runNewAppsAnimation(false);
3426 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003427 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003428 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003429 // are on another page (or just normally if they are added to the current page)
3430 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003431 }
3432 }
3433
3434 mWorkspaceLoading = false;
3435 }
3436
Winson Chunga2413752012-04-03 14:22:34 -07003437 private boolean canRunNewAppsAnimation() {
3438 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3439 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3440 }
3441
Winson Chungf0c6ae02012-03-21 16:10:31 -07003442 /**
3443 * Runs a new animation that scales up icons that were added while Launcher was in the
3444 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003445 *
3446 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003447 */
Winson Chunga2413752012-04-03 14:22:34 -07003448 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003449 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003450 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003451
3452 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003453 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3454 @Override
3455 public int compare(View a, View b) {
3456 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3457 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3458 int cellCountX = LauncherModel.getCellCountX();
3459 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3460 }
3461 });
Winson Chunga2413752012-04-03 14:22:34 -07003462
3463 // Animate each of the views in place (or show them immediately if requested)
3464 if (immediate) {
3465 for (View v : mNewShortcutAnimateViews) {
3466 v.setAlpha(1f);
3467 v.setScaleX(1f);
3468 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003469 }
Winson Chunga2413752012-04-03 14:22:34 -07003470 } else {
3471 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3472 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003473 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003474 PropertyValuesHolder.ofFloat("alpha", 1f),
3475 PropertyValuesHolder.ofFloat("scaleX", 1f),
3476 PropertyValuesHolder.ofFloat("scaleY", 1f));
3477 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3478 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3479 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3480 bounceAnims.add(bounceAnim);
3481 }
3482 anim.playTogether(bounceAnims);
3483 anim.addListener(new AnimatorListenerAdapter() {
3484 @Override
3485 public void onAnimationEnd(Animator animation) {
3486 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3487 }
3488 });
3489 anim.start();
3490 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003491
3492 // Clean up
3493 mNewShortcutAnimatePage = -1;
3494 mNewShortcutAnimateViews.clear();
3495 new Thread("clearNewAppsThread") {
3496 public void run() {
3497 mSharedPrefs.edit()
3498 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3499 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3500 .commit();
3501 }
3502 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003503 }
3504
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003505 @Override
3506 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003507 boolean searchVisible = updateGlobalSearchIcon();
3508 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003509 if (mSearchDropTargetBar != null) {
3510 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3511 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003512 }
3513
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003514 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003515 * Add the icons for all apps.
3516 *
3517 * Implementation of the method from LauncherModel.Callbacks.
3518 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003519 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003520 Runnable setAllAppsRunnable = new Runnable() {
3521 public void run() {
3522 if (mAppsCustomizeContent != null) {
3523 mAppsCustomizeContent.setApps(apps);
3524 }
3525 }
3526 };
3527
Michael Jurkac57b7a82011-08-09 22:02:20 -07003528 // Remove the progress bar entirely; we could also make it GONE
3529 // but better to remove it since we know it's not going to be used
3530 View progressBar = mAppsCustomizeTabHost.
3531 findViewById(R.id.apps_customize_progress_bar);
3532 if (progressBar != null) {
3533 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003534
3535 // We just post the call to setApps so the user sees the progress bar
3536 // disappear-- otherwise, it just looks like the progress bar froze
3537 // which doesn't look great
3538 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3539 } else {
3540 // If we did not initialize the spinner in onCreate, then we can directly set the
3541 // list of applications without waiting for any progress bars views to be hidden.
3542 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003543 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003544 }
3545
3546 /**
3547 * A package was installed.
3548 *
3549 * Implementation of the method from LauncherModel.Callbacks.
3550 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003551 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003552 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003553
Winson Chung785d2eb2011-04-14 16:08:02 -07003554 if (mAppsCustomizeContent != null) {
3555 mAppsCustomizeContent.addApps(apps);
3556 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003557 }
3558
3559 /**
3560 * A package was updated.
3561 *
3562 * Implementation of the method from LauncherModel.Callbacks.
3563 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003564 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003565 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003566 if (mWorkspace != null) {
3567 mWorkspace.updateShortcuts(apps);
3568 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003569
Winson Chung785d2eb2011-04-14 16:08:02 -07003570 if (mAppsCustomizeContent != null) {
3571 mAppsCustomizeContent.updateApps(apps);
3572 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003573 }
3574
3575 /**
3576 * A package was uninstalled.
3577 *
3578 * Implementation of the method from LauncherModel.Callbacks.
3579 */
Winson Chungcd810732012-06-18 16:45:43 -07003580 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003581 if (permanent) {
Winson Chungcd810732012-06-18 16:45:43 -07003582 mWorkspace.removeItems(packageNames);
Joe Onorato36115782010-06-17 13:28:48 -04003583 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003584
Winson Chung785d2eb2011-04-14 16:08:02 -07003585 if (mAppsCustomizeContent != null) {
Winson Chungcd810732012-06-18 16:45:43 -07003586 mAppsCustomizeContent.removeApps(packageNames);
Winson Chung785d2eb2011-04-14 16:08:02 -07003587 }
Winson Chunga1820962011-10-03 16:31:06 -07003588
3589 // Notify the drag controller
Winson Chungcd810732012-06-18 16:45:43 -07003590 mDragController.onAppsRemoved(packageNames, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003591 }
Joe Onoratobe386092009-11-17 17:32:16 -08003592
3593 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003594 * A number of packages were updated.
3595 */
3596 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003597 if (mAppsCustomizeContent != null) {
3598 mAppsCustomizeContent.onPackagesUpdated();
3599 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003600 }
3601
Winson Chung400438b2011-01-16 17:53:48 -08003602 private int mapConfigurationOriActivityInfoOri(int configOri) {
3603 final Display d = getWindowManager().getDefaultDisplay();
3604 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3605 switch (d.getRotation()) {
3606 case Surface.ROTATION_0:
3607 case Surface.ROTATION_180:
3608 // We are currently in the same basic orientation as the natural orientation
3609 naturalOri = configOri;
3610 break;
3611 case Surface.ROTATION_90:
3612 case Surface.ROTATION_270:
3613 // We are currently in the other basic orientation to the natural orientation
3614 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3615 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3616 break;
3617 }
3618
3619 int[] oriMap = {
3620 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3621 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3622 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3623 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3624 };
3625 // Since the map starts at portrait, we need to offset if this device's natural orientation
3626 // is landscape.
3627 int indexOffset = 0;
3628 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3629 indexOffset = 1;
3630 }
3631 return oriMap[(d.getRotation() + indexOffset) % 4];
3632 }
Adam Cohen446e9402011-09-15 18:21:21 -07003633
Winson Chung4b919f82012-05-01 10:44:08 -07003634 public boolean isRotationEnabled() {
Michael Jurka9bc8eba2012-05-21 20:36:44 -07003635 boolean forceEnableRotation = doesFileExist(FORCE_ENABLE_ROTATION_PROPERTY);
Winson Chung4b919f82012-05-01 10:44:08 -07003636 boolean enableRotation = forceEnableRotation ||
3637 getResources().getBoolean(R.bool.allow_rotation);
3638 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003639 }
Winson Chung4b919f82012-05-01 10:44:08 -07003640 public void lockScreenOrientation() {
3641 if (isRotationEnabled()) {
3642 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3643 .getConfiguration().orientation));
3644 }
3645 }
3646 public void unlockScreenOrientation(boolean immediate) {
3647 if (isRotationEnabled()) {
3648 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003649 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003650 } else {
3651 mHandler.postDelayed(new Runnable() {
3652 public void run() {
3653 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3654 }
3655 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003656 }
Winson Chung4b919f82012-05-01 10:44:08 -07003657 }
Winson Chung400438b2011-01-16 17:53:48 -08003658 }
3659
Winson Chung82f55532011-08-09 14:14:23 -07003660 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003661 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003662 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003663 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003664
Winson Chung7d7541e2011-09-16 20:14:36 -07003665 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003666 }
Michael Jurka22143132012-10-04 17:42:38 +02003667
Winson Chung7d7541e2011-09-16 20:14:36 -07003668 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003669 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003670 if (cling != null) {
3671 cling.init(this, positionData);
3672 cling.setVisibility(View.VISIBLE);
3673 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3674 if (animate) {
3675 cling.buildLayer();
3676 cling.setAlpha(0f);
3677 cling.animate()
3678 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003679 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003680 .setDuration(SHOW_CLING_DURATION)
3681 .setStartDelay(delay)
3682 .start();
3683 } else {
3684 cling.setAlpha(1f);
3685 }
Michael Jurka22143132012-10-04 17:42:38 +02003686 cling.setFocusableInTouchMode(true);
3687 cling.post(new Runnable() {
3688 public void run() {
3689 cling.setFocusable(true);
3690 cling.requestFocus();
3691 }
3692 });
3693 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3694 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003695 }
3696 return cling;
3697 }
Michael Jurka22143132012-10-04 17:42:38 +02003698
Winson Chung7d7541e2011-09-16 20:14:36 -07003699 private void dismissCling(final Cling cling, final String flag, int duration) {
Michael Jurka22143132012-10-04 17:42:38 +02003700 if (cling != null && cling.getVisibility() == View.VISIBLE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003701 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003702 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003703 anim.addListener(new AnimatorListenerAdapter() {
3704 public void onAnimationEnd(Animator animation) {
3705 cling.setVisibility(View.GONE);
3706 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003707 // We should update the shared preferences on a background thread
3708 new Thread("dismissClingThread") {
3709 public void run() {
3710 SharedPreferences.Editor editor = mSharedPrefs.edit();
3711 editor.putBoolean(flag, true);
3712 editor.commit();
3713 }
3714 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003715 };
3716 });
3717 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02003718 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07003719 }
3720 }
Michael Jurka22143132012-10-04 17:42:38 +02003721
Winson Chung9d9d74f2011-09-19 11:49:12 -07003722 private void removeCling(int id) {
3723 final View cling = findViewById(id);
3724 if (cling != null) {
3725 final ViewGroup parent = (ViewGroup) cling.getParent();
3726 parent.post(new Runnable() {
3727 @Override
3728 public void run() {
3729 parent.removeView(cling);
3730 }
3731 });
Michael Jurka22143132012-10-04 17:42:38 +02003732 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003733 }
3734 }
Michael Jurka974c3862012-05-22 22:00:31 -07003735
3736 private boolean skipCustomClingIfNoAccounts() {
3737 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3738 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3739 if (customCling) {
3740 AccountManager am = AccountManager.get(this);
3741 Account[] accounts = am.getAccountsByType("com.google");
3742 return accounts.length == 0;
3743 }
3744 return false;
3745 }
3746
Winson Chung7d7541e2011-09-16 20:14:36 -07003747 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003748 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003749 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003750 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3751 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02003752 // If we're not using the default workspace layout, replace workspace cling
3753 // with a custom workspace cling (usually specified in an overlay)
3754 // For now, only do this on tablets
3755 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02003756 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02003757 // Use a custom cling
3758 View cling = findViewById(R.id.workspace_cling);
3759 ViewGroup clingParent = (ViewGroup) cling.getParent();
3760 int clingIndex = clingParent.indexOfChild(cling);
3761 clingParent.removeViewAt(clingIndex);
3762 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
3763 clingParent.addView(customCling, clingIndex);
3764 customCling.setId(R.id.workspace_cling);
3765 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003766 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003767 } else {
3768 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003769 }
3770 }
3771 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003772 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003773 if (isClingsEnabled() &&
3774 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003775 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003776 } else {
3777 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003778 }
3779 }
3780 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003781 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003782 if (isClingsEnabled() &&
3783 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3784 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003785 } else {
3786 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003787 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003788 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003789 }
3790 public boolean isFolderClingVisible() {
3791 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003792 if (cling != null) {
3793 return cling.getVisibility() == View.VISIBLE;
3794 }
3795 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003796 }
Winson Chung82f55532011-08-09 14:14:23 -07003797 public void dismissWorkspaceCling(View v) {
3798 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003799 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003800 }
3801 public void dismissAllAppsCling(View v) {
3802 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003803 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3804 }
3805 public void dismissFolderCling(View v) {
3806 Cling cling = (Cling) findViewById(R.id.folder_cling);
3807 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003808 }
3809
Winson Chung80baf5a2010-08-09 16:03:15 -07003810 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003811 * Prints out out state for debugging.
3812 */
3813 public void dumpState() {
3814 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003815 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003816 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3817 Log.d(TAG, "mRestoring=" + mRestoring);
3818 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3819 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003820 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003821 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003822
Winson Chung785d2eb2011-04-14 16:08:02 -07003823 if (mAppsCustomizeContent != null) {
3824 mAppsCustomizeContent.dumpState();
3825 }
Joe Onoratobe386092009-11-17 17:32:16 -08003826 Log.d(TAG, "END launcher2 dump state");
3827 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003828
3829 @Override
3830 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3831 super.dump(prefix, fd, writer, args);
3832 writer.println(" ");
3833 writer.println("Debug logs: ");
3834 for (int i = 0; i < sDumpLogs.size(); i++) {
3835 writer.println(" " + sDumpLogs.get(i));
3836 }
3837 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003838
3839 public static void dumpDebugLogsToConsole() {
3840 Log.d(TAG, "");
3841 Log.d(TAG, "*********************");
3842 Log.d(TAG, "Launcher debug logs: ");
3843 for (int i = 0; i < sDumpLogs.size(); i++) {
3844 Log.d(TAG, " " + sDumpLogs.get(i));
3845 }
3846 Log.d(TAG, "*********************");
3847 Log.d(TAG, "");
3848 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003849}
Michael Jurka2763be32011-02-24 11:19:57 -08003850
Michael Jurkaabded662011-03-04 12:06:57 -08003851interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003852 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003853 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003854 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003855 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003856 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003857}