blob: 3fdf5c3ce40219e12f30fbdef6433d51a31f3fc4 [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 Jurkac1f5d262011-09-30 19:32:27 -0700303 private Runnable mBuildLayersRunnable = new Runnable() {
304 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700305 if (mWorkspace != null) {
306 mWorkspace.buildPageHardwareLayers();
307 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700308 }
309 };
310
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800311 private static ArrayList<PendingAddArguments> sPendingAddList
312 = new ArrayList<PendingAddArguments>();
313
314 private static class PendingAddArguments {
315 int requestCode;
316 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700317 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800318 int screen;
319 int cellX;
320 int cellY;
321 }
322
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700323
324 private boolean doesFileExist(String filename) {
325 FileInputStream fis = null;
326 try {
327 fis = openFileInput(filename);
328 fis.close();
329 return true;
330 } catch (java.io.FileNotFoundException e) {
331 return false;
332 } catch (java.io.IOException e) {
333 return true;
334 }
335 }
336
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800337 @Override
338 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700339 if (DEBUG_STRICT_MODE) {
340 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
341 .detectDiskReads()
342 .detectDiskWrites()
343 .detectNetwork() // or .detectAll() for all detectable problems
344 .penaltyLog()
345 .build());
346 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
347 .detectLeakedSqlLiteObjects()
348 .detectLeakedClosableObjects()
349 .penaltyLog()
350 .penaltyDeath()
351 .build());
352 }
353
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800354 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800355 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700356 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
357 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800358 mModel = app.setLauncher(this);
359 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400360 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800361 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700362
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700363 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700364 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
365 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700366
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700367 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
368 // this also ensures that any synchronous binding below doesn't re-trigger another
369 // LauncherModel load.
370 mPaused = false;
371
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800372 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200373 android.os.Debug.startMethodTracing(
374 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 }
376
377 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 setContentView(R.layout.launcher);
379 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700380 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800382 registerContentObservers();
383
Joe Onorato7c312c12009-08-13 21:36:53 -0700384 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700385
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 mSavedState = savedInstanceState;
387 restoreState(mSavedState);
388
Winson Chunga12a2502010-12-20 14:41:35 -0800389 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700390 if (mAppsCustomizeContent != null) {
391 mAppsCustomizeContent.onPackagesUpdated();
392 }
Winson Chunga12a2502010-12-20 14:41:35 -0800393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 if (PROFILE_STARTUP) {
395 android.os.Debug.stopMethodTracing();
396 }
397
398 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700399 if (sPausedFromUserAction) {
400 // If the user leaves launcher, then we should just load items asynchronously when
401 // they return.
402 mModel.startLoader(true, -1);
403 } else {
404 // We only load the page synchronously if the user rotates (or triggers a
405 // configuration change) while launcher is in the foreground
406 mModel.startLoader(true, mWorkspace.getCurrentPage());
407 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 }
409
Michael Jurkac57b7a82011-08-09 22:02:20 -0700410 if (!mModel.isAllAppsLoaded()) {
411 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
412 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
413 }
414
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 // For handling default keys
416 mDefaultKeySsb = new SpannableStringBuilder();
417 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800418
419 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
420 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800421
Adam Cohenf9426d52012-06-04 17:26:21 -0700422 updateGlobalIcons();
423
424 // On large interfaces, we want the screen to auto-rotate based on the current orientation
425 unlockScreenOrientation(true);
426 }
427
Winson Chung4a2afa32012-07-19 14:53:05 -0700428 protected void onUserLeaveHint() {
429 super.onUserLeaveHint();
430 sPausedFromUserAction = true;
431 }
432
Adam Cohenf9426d52012-06-04 17:26:21 -0700433 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700434 boolean searchVisible = false;
435 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800436 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700437 int coi = getCurrentOrientationIndexForGlobalIcons();
438 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
439 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700440 updateAppMarketIcon();
441 searchVisible = updateGlobalSearchIcon();
442 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700443 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700444 if (sGlobalSearchIcon[coi] != null) {
445 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700446 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700447 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700448 if (sVoiceSearchIcon[coi] != null) {
449 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700450 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700451 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700452 if (sAppMarketIcon[coi] != null) {
453 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800454 }
Winson Chungadf0c182012-08-23 14:59:07 -0700455 if (mSearchDropTargetBar != null) {
456 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
457 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458 }
Romain Guycbb89e42009-06-08 15:52:54 -0700459
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700461 if (sLocaleConfiguration == null) {
462 new AsyncTask<Void, Void, LocaleConfiguration>() {
463 @Override
464 protected LocaleConfiguration doInBackground(Void... unused) {
465 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
466 readConfiguration(Launcher.this, localeConfiguration);
467 return localeConfiguration;
468 }
469
470 @Override
471 protected void onPostExecute(LocaleConfiguration result) {
472 sLocaleConfiguration = result;
473 checkForLocaleChange(); // recursive, but now with a locale configuration
474 }
475 }.execute();
476 return;
477 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 final Configuration configuration = getResources().getConfiguration();
480
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700481 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 final String locale = configuration.locale.toString();
483
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700484 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 final int mcc = configuration.mcc;
486
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700487 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 final int mnc = configuration.mnc;
489
Romain Guy84f296c2009-11-04 15:00:44 -0800490 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491
Romain Guy84f296c2009-11-04 15:00:44 -0800492 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700493 sLocaleConfiguration.locale = locale;
494 sLocaleConfiguration.mcc = mcc;
495 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700496
Joe Onorato0589f0f2010-02-08 13:44:00 -0800497 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700498
499 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
500 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700501 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700502 public void run() {
503 writeConfiguration(Launcher.this, localeConfiguration);
504 }
505 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700506 }
507 }
508
509 private static class LocaleConfiguration {
510 public String locale;
511 public int mcc = -1;
512 public int mnc = -1;
513 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700514
Romain Guy98d01652009-06-30 16:21:04 -0700515 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
516 DataInputStream in = null;
517 try {
518 in = new DataInputStream(context.openFileInput(PREFERENCES));
519 configuration.locale = in.readUTF();
520 configuration.mcc = in.readInt();
521 configuration.mnc = in.readInt();
522 } catch (FileNotFoundException e) {
523 // Ignore
524 } catch (IOException e) {
525 // Ignore
526 } finally {
527 if (in != null) {
528 try {
529 in.close();
530 } catch (IOException e) {
531 // Ignore
532 }
533 }
534 }
535 }
536
537 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
538 DataOutputStream out = null;
539 try {
540 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
541 out.writeUTF(configuration.locale);
542 out.writeInt(configuration.mcc);
543 out.writeInt(configuration.mnc);
544 out.flush();
545 } catch (FileNotFoundException e) {
546 // Ignore
547 } catch (IOException e) {
548 //noinspection ResultOfMethodCallIgnored
549 context.getFileStreamPath(PREFERENCES).delete();
550 } finally {
551 if (out != null) {
552 try {
553 out.close();
554 } catch (IOException e) {
555 // Ignore
556 }
557 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800558 }
559 }
560
Adam Cohen716b51e2011-06-30 12:09:54 -0700561 public DragLayer getDragLayer() {
562 return mDragLayer;
563 }
564
Winson Chung36a62fe2012-05-06 18:04:42 -0700565 boolean isDraggingEnabled() {
566 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
567 // that is subsequently removed from the workspace in startBinding().
568 return !mModel.isLoadingWorkspace();
569 }
570
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 static int getScreen() {
572 synchronized (sLock) {
573 return sScreen;
574 }
575 }
576
577 static void setScreen(int screen) {
578 synchronized (sLock) {
579 sScreen = screen;
580 }
581 }
582
Winson Chung557d6ed2011-07-08 15:34:52 -0700583 /**
584 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
585 * a configuration step, this allows the proper animations to run after other transitions.
586 */
587 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700588 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800589 switch (args.requestCode) {
590 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700591 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
592 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800593 break;
594 case REQUEST_PICK_SHORTCUT:
595 processShortcut(args.intent);
596 break;
597 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700598 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
599 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700600 result = true;
601 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800602 case REQUEST_CREATE_APPWIDGET:
603 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800604 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700605 result = true;
606 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800607 case REQUEST_PICK_WALLPAPER:
608 // We just wanted the activity result here so we can clear mWaitingForResult
609 break;
610 }
Michael Jurka27614d22012-04-02 06:40:22 -0700611 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
612 // if you turned the screen off and then back while in All Apps, Launcher would not
613 // return to the workspace. Clearing mAddInfo.container here fixes this issue
614 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700615 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800616 }
617
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700619 protected void onActivityResult(
620 final int requestCode, final int resultCode, final Intent data) {
621 if (requestCode == REQUEST_BIND_APPWIDGET) {
622 int appWidgetId = data != null ?
623 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
624 if (resultCode == RESULT_CANCELED) {
625 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
626 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700627 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700628 }
629 return;
630 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700631 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800632 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
633 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700634 mWaitingForResult = false;
635
Adam Cohened66b2b2012-01-23 17:28:51 -0800636 // We have special handling for widgets
637 if (isWidgetDrop) {
638 int appWidgetId = data != null ?
639 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700640 if (appWidgetId < 0) {
641 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
642 "widget configuration activity.");
643 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
644 } else {
645 completeTwoStageWidgetDrop(resultCode, appWidgetId);
646 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800647 return;
648 }
649
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800650 // The pattern used here is that a user PICKs a specific application,
651 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
654 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700655 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800656 final PendingAddArguments args = new PendingAddArguments();
657 args.requestCode = requestCode;
658 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700659 args.container = mPendingAddInfo.container;
660 args.screen = mPendingAddInfo.screen;
661 args.cellX = mPendingAddInfo.cellX;
662 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800663 if (isWorkspaceLocked()) {
664 sPendingAddList.add(args);
665 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700666 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800669 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700670 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800671 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
672 null);
673 }
674
675 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700676 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700677 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800678 Runnable onCompleteRunnable = null;
679 int animationType = 0;
680
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700681 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800682 if (resultCode == RESULT_OK) {
683 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
684 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700685 mPendingAddWidgetInfo);
686 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800687 onCompleteRunnable = new Runnable() {
688 @Override
689 public void run() {
690 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
691 mPendingAddInfo.screen, layout, null);
692 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
693 null);
694 }
695 };
696 } else if (resultCode == RESULT_CANCELED) {
697 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
698 onCompleteRunnable = new Runnable() {
699 @Override
700 public void run() {
701 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
702 null);
703 }
704 };
705 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700706 if (mDragLayer.getAnimatedView() != null) {
707 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
708 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
709 animationType, boundWidget, true);
710 } else {
711 // The animated view may be null in the case of a rotation during widget configuration
712 onCompleteRunnable.run();
713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 }
715
716 @Override
717 protected void onResume() {
718 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700719
Winson Chung4a2afa32012-07-19 14:53:05 -0700720 // Restore the previous launcher state
721 if (mOnResumeState == State.WORKSPACE) {
722 showWorkspace(false);
723 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
724 showAllApps(false);
725 }
726 mOnResumeState = State.NONE;
727
Winson Chungde0fb8f2012-05-08 14:37:08 -0700728 // Process any items that were added while Launcher was away
729 InstallShortcutReceiver.flushInstallQueue(this);
730
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800731 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700732 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700733 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400734 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700735 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400736 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700737 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 }
Winson Chunge4e50662012-01-23 14:45:13 -0800739
740 // Reset the pressed state of icons that were locked in the press state while activities
741 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800742 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800743 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800744 mWaitingForResume.setStayPressed(false);
745 }
Winson Chunge4e50662012-01-23 14:45:13 -0800746 if (mAppsCustomizeContent != null) {
747 // Resets the previous all apps icon press state
748 mAppsCustomizeContent.resetDrawableState();
749 }
Adam Cohen06dff352012-06-01 17:17:08 -0700750 // It is possible that widgets can receive updates while launcher is not in the foreground.
751 // Consequently, the widgets will be inflated in the orientation of the foreground activity
752 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
753 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700754 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700755
756 // Again, as with the above scenario, it's possible that one or more of the global icons
757 // were updated in the wrong orientation.
758 updateGlobalIcons();
Adam Cohen06dff352012-06-01 17:17:08 -0700759 }
760
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700762 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700763 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
764 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
765 // when Launcher resumes and we are still in AllApps.
766 updateWallpaperVisibility(true);
767
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700768 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700769 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800770 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700771 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700772 }
Romain Guycbb89e42009-06-08 15:52:54 -0700773
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700774 @Override
775 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400776 // Flag the loader to stop early before switching
777 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700778 if (mAppsCustomizeContent != null) {
779 mAppsCustomizeContent.surrender();
780 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800781 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700782 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700783
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800784 // We can't hide the IME if it was forced open. So don't bother
785 /*
786 @Override
787 public void onWindowFocusChanged(boolean hasFocus) {
788 super.onWindowFocusChanged(hasFocus);
789
790 if (hasFocus) {
791 final InputMethodManager inputManager = (InputMethodManager)
792 getSystemService(Context.INPUT_METHOD_SERVICE);
793 WindowManager.LayoutParams lp = getWindow().getAttributes();
794 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
795 android.os.Handler()) {
796 protected void onReceiveResult(int resultCode, Bundle resultData) {
797 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
798 }
799 });
800 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
801 }
802 }
803 */
804
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800805 private boolean acceptFilter() {
806 final InputMethodManager inputManager = (InputMethodManager)
807 getSystemService(Context.INPUT_METHOD_SERVICE);
808 return !inputManager.isFullscreenMode();
809 }
810
811 @Override
812 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700813 final int uniChar = event.getUnicodeChar();
814 final boolean handled = super.onKeyDown(keyCode, event);
815 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
816 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800817 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
818 keyCode, event);
819 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700820 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700821 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700822 // showSearchDialog()
823 // If there are multiple keystrokes before the search dialog takes focus,
824 // onSearchRequested() will be called for every keystroke,
825 // but it is idempotent, so it's fine.
826 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800827 }
828 }
829
Joe Onorato8a9625e2010-01-28 15:55:35 -0800830 // Eat the long press event so the keyboard doesn't come up.
831 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
832 return true;
833 }
834
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800835 return handled;
836 }
837
Karl Rosaen138a0412009-04-23 19:00:21 -0700838 private String getTypedText() {
839 return mDefaultKeySsb.toString();
840 }
841
842 private void clearTypedText() {
843 mDefaultKeySsb.clear();
844 mDefaultKeySsb.clearSpans();
845 Selection.setSelection(mDefaultKeySsb, 0);
846 }
847
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800848 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700849 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
850 * State
851 */
852 private static State intToState(int stateOrdinal) {
853 State state = State.WORKSPACE;
854 final State[] stateValues = State.values();
855 for (int i = 0; i < stateValues.length; i++) {
856 if (stateValues[i].ordinal() == stateOrdinal) {
857 state = stateValues[i];
858 break;
859 }
860 }
861 return state;
862 }
863
864 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800865 * Restores the previous state, if it exists.
866 *
867 * @param savedState The previous state.
868 */
869 private void restoreState(Bundle savedState) {
870 if (savedState == null) {
871 return;
872 }
873
Michael Jurka883f55b2010-10-21 15:47:14 -0700874 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700875 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700876 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800877 }
878
Winson Chungf0c6ae02012-03-21 16:10:31 -0700879 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800880 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700881 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800882 }
883
Winson Chung3d503fb2011-07-13 17:25:49 -0700884 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
885 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700886
Winson Chung3d503fb2011-07-13 17:25:49 -0700887 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
888 mPendingAddInfo.container = pendingAddContainer;
889 mPendingAddInfo.screen = pendingAddScreen;
890 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
891 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700892 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
893 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
894 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700895 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800896 mRestoring = true;
897 }
898
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700899
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800900 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
901 if (renameFolder) {
902 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700903 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800904 mRestoring = true;
905 }
Winson Chunga12a2502010-12-20 14:41:35 -0800906
Winson Chung785d2eb2011-04-14 16:08:02 -0700907
908 // Restore the AppsCustomize tab
909 if (mAppsCustomizeTabHost != null) {
910 String curTab = savedState.getString("apps_customize_currentTab");
911 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700912 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700913 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700914 mAppsCustomizeContent.loadAssociatedPages(
915 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700916 }
917
Winson Chung5afbf7b2011-07-25 11:53:08 -0700918 int currentIndex = savedState.getInt("apps_customize_currentIndex");
919 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700920 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800921 }
922
923 /**
924 * Finds all the views we need and configure them properly.
925 */
926 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700927 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400928
Winson Chung4c98d922011-05-31 16:50:48 -0700929 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
930 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800931 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
932 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800933
Winson Chung4c98d922011-05-31 16:50:48 -0700934 // Setup the drag layer
935 mDragLayer.setup(this, dragController);
936
Winson Chung3d503fb2011-07-13 17:25:49 -0700937 // Setup the hotseat
938 mHotseat = (Hotseat) findViewById(R.id.hotseat);
939 if (mHotseat != null) {
940 mHotseat.setup(this);
941 }
942
Winson Chung4c98d922011-05-31 16:50:48 -0700943 // Setup the workspace
944 mWorkspace.setHapticFeedbackEnabled(false);
945 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700946 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700947 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700948
Winson Chungf0ea4d32011-06-06 14:27:16 -0700949 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700950 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700951
Winson Chungf0ea4d32011-06-06 14:27:16 -0700952 // Setup AppsCustomize
953 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
954 findViewById(R.id.apps_customize_pane);
955 mAppsCustomizeContent = (AppsCustomizePagedView)
956 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
957 mAppsCustomizeContent.setup(this, dragController);
Winson Chung0e721a42012-08-02 17:40:30 -0700958
Winson Chung3d503fb2011-07-13 17:25:49 -0700959 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700960 dragController.setDragScoller(mWorkspace);
961 dragController.setScrollView(mDragLayer);
962 dragController.setMoveTarget(mWorkspace);
963 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700964 if (mSearchDropTargetBar != null) {
965 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
968
969 /**
970 * Creates a view representing a shortcut.
971 *
972 * @param info The data structure describing the shortcut.
973 *
974 * @return A View inflated from R.layout.application.
975 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800976 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700978 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800979 }
980
981 /**
982 * Creates a view representing a shortcut inflated from the specified resource.
983 *
984 * @param layoutResId The id of the XML layout used to create the shortcut.
985 * @param parent The group the shortcut belongs to.
986 * @param info The data structure describing the shortcut.
987 *
988 * @return A View inflated from layoutResId.
989 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800990 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800991 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
992 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800993 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 return favorite;
995 }
996
997 /**
998 * Add an application shortcut to the workspace.
999 *
1000 * @param data The intent describing the application.
1001 * @param cellInfo The position on screen where to create the shortcut.
1002 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001003 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001004 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001005 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001006
Adam Cohenfbba09b2011-07-18 21:43:05 -07001007 // First we check if we already know the exact location where we want to add this item.
1008 if (cellX >= 0 && cellY >= 0) {
1009 cellXY[0] = cellX;
1010 cellXY[1] = cellY;
1011 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001012 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001013 return;
1014 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001016 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001017
Romain Guy73b979d2009-06-09 12:57:21 -07001018 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001019 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001020 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001021 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001022 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001023 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001024 } else {
1025 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 }
1027 }
Romain Guycbb89e42009-06-08 15:52:54 -07001028
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 /**
1030 * Add a shortcut to the workspace.
1031 *
1032 * @param data The intent describing the shortcut.
1033 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001035 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1036 int cellY) {
1037 int[] cellXY = mTmpAddItemCellCoordinates;
1038 int[] touchXY = mPendingAddInfo.dropPos;
1039 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001040
Michael Jurkad3ef3062010-11-23 16:23:58 -08001041 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001042
Adam Cohen558baaf2011-08-15 15:22:57 -07001043 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001044 if (info == null) {
1045 return;
1046 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001047 final View view = createShortcut(info);
1048
Adam Cohenfbba09b2011-07-18 21:43:05 -07001049 // First we check if we already know the exact location where we want to add this item.
1050 if (cellX >= 0 && cellY >= 0) {
1051 cellXY[0] = cellX;
1052 cellXY[1] = cellY;
1053 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001054
1055 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001056 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001057 true, null,null)) {
1058 return;
1059 }
1060 DragObject dragObject = new DragObject();
1061 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001062 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1063 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001064 return;
1065 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001066 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001067 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001068 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001069 foundCellSpan = (result != null);
1070 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001071 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001072 }
1073
1074 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001075 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001076 return;
1077 }
1078
Adam Cohen558baaf2011-08-15 15:22:57 -07001079 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080
1081 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001082 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1083 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001084 }
1085 }
1086
Adam Cohen2f093b62012-04-30 18:59:53 -07001087 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1088 int minHeight) {
1089 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001090 // We want to account for the extra amount of padding that we are adding to the widget
1091 // to ensure that it gets the full amount of space that it has requested
1092 int requiredWidth = minWidth + padding.left + padding.right;
1093 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001094 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001095 }
1096
Adam Cohen2f093b62012-04-30 18:59:53 -07001097 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1098 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001099 }
1100
Adam Cohen2f093b62012-04-30 18:59:53 -07001101 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1102 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001103 }
1104
Adam Cohen2f093b62012-04-30 18:59:53 -07001105 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1106 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001107 }
1108
Adam Cohen2f093b62012-04-30 18:59:53 -07001109 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1110 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001111 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001112 }
1113
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001114 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001115 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001116 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001117 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001118 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001120 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1121 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1122 if (appWidgetInfo == null) {
1123 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1124 }
Romain Guycbb89e42009-06-08 15:52:54 -07001125
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001126 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001127 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001128
Adam Cohen2f093b62012-04-30 18:59:53 -07001129 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1130 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001132 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001133 // We have saved the position to which the widget was dragged-- this really only matters
1134 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001135 int[] cellXY = mTmpAddItemCellCoordinates;
1136 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001137 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001138 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001139 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1140 cellXY[0] = mPendingAddInfo.cellX;
1141 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001142 spanXY[0] = mPendingAddInfo.spanX;
1143 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001144 foundCellSpan = true;
1145 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001146 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001147 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001148 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1149 spanXY[1], cellXY, finalSpan);
1150 spanXY[0] = finalSpan[0];
1151 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001152 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001153 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001154 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001155 }
1156
Michael Jurka0280c3b2010-09-17 15:00:07 -07001157 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001158 if (appWidgetId != -1) {
1159 // Deleting an app widget ID is a void call but writes to disk before returning
1160 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001161 new Thread("deleteAppWidgetId") {
1162 public void run() {
1163 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1164 }
1165 }.start();
1166 }
Winson Chung93eef082012-03-23 15:59:27 -07001167 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001168 return;
1169 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001171 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001172 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1173 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001174 launcherInfo.spanX = spanXY[0];
1175 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001176 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1177 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001178
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001179 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001180 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001181
1182 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001183 if (hostView == null) {
1184 // Perform actual inflation because we're live
1185 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1186 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1187 } else {
1188 // The AppWidgetHostView has already been inflated and instantiated
1189 launcherInfo.hostView = hostView;
1190 }
Romain Guycbb89e42009-06-08 15:52:54 -07001191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001193 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohen9e05a5e2012-09-10 15:53:09 -07001194 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
1195 launcherInfo.notifyWidgetSizeChanged(this);
1196 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001197 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001198 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001199
1200 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001201 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001202 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 }
Romain Guycbb89e42009-06-08 15:52:54 -07001204
Adam Cohended9f8d2010-11-03 13:25:16 -07001205 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1206 @Override
1207 public void onReceive(Context context, Intent intent) {
1208 final String action = intent.getAction();
1209 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1210 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001211 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001212 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001213
Winson Chungc100e8e2011-08-09 16:02:43 -07001214 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001215 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001216 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1217 mAppsCustomizeTabHost.reset();
1218 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001219 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001220 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1221 mUserPresent = true;
1222 updateRunning();
1223 }
1224 }
1225 };
1226
1227 @Override
1228 public void onAttachedToWindow() {
1229 super.onAttachedToWindow();
1230
1231 // Listen for broadcasts related to user-presence
1232 final IntentFilter filter = new IntentFilter();
1233 filter.addAction(Intent.ACTION_SCREEN_OFF);
1234 filter.addAction(Intent.ACTION_USER_PRESENT);
1235 registerReceiver(mReceiver, filter);
1236
Adam Cohend113e0c2010-11-11 10:48:05 -08001237 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001238 mVisible = true;
1239 }
1240
1241 @Override
1242 public void onDetachedFromWindow() {
1243 super.onDetachedFromWindow();
1244 mVisible = false;
1245
Adam Cohend113e0c2010-11-11 10:48:05 -08001246 if (mAttached) {
1247 unregisterReceiver(mReceiver);
1248 mAttached = false;
1249 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001250 updateRunning();
1251 }
1252
1253 public void onWindowVisibilityChanged(int visibility) {
1254 mVisible = visibility == View.VISIBLE;
1255 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001256 // The following code used to be in onResume, but it turns out onResume is called when
1257 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1258 // is a more appropriate event to handle
1259 if (mVisible) {
1260 mAppsCustomizeTabHost.onWindowVisible();
1261 if (!mWorkspaceLoading) {
1262 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001263 // We want to let Launcher draw itself at least once before we force it to build
1264 // layers on all the workspace pages, so that transitioning to Launcher from other
1265 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1266 // a true draw so we wait until the second preDraw call to be safe
1267 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001268 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001269 // We delay the layer building a bit in order to give
1270 // other message processing a time to run. In particular
1271 // this avoids a delay in hiding the IME if it was
1272 // currently shown, because doing that may involve
1273 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001274 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001275
Michael Jurka6ee21d22012-02-21 18:20:27 -08001276 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001277 return true;
1278 }
1279 });
1280 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001281 // When Launcher comes back to foreground, a different Activity might be responsible for
1282 // the app market intent, so refresh the icon
1283 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001284 clearTypedText();
1285 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001286 }
1287
1288 private void sendAdvanceMessage(long delay) {
1289 mHandler.removeMessages(ADVANCE_MSG);
1290 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1291 mHandler.sendMessageDelayed(msg, delay);
1292 mAutoAdvanceSentTime = System.currentTimeMillis();
1293 }
1294
1295 private void updateRunning() {
1296 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1297 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1298 mAutoAdvanceRunning = autoAdvanceRunning;
1299 if (autoAdvanceRunning) {
1300 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1301 sendAdvanceMessage(delay);
1302 } else {
1303 if (!mWidgetsToAdvance.isEmpty()) {
1304 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1305 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1306 }
1307 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001308 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001309 }
1310 }
1311 }
1312
1313 private final Handler mHandler = new Handler() {
1314 @Override
1315 public void handleMessage(Message msg) {
1316 if (msg.what == ADVANCE_MSG) {
1317 int i = 0;
1318 for (View key: mWidgetsToAdvance.keySet()) {
1319 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1320 final int delay = mAdvanceStagger * i;
1321 if (v instanceof Advanceable) {
1322 postDelayed(new Runnable() {
1323 public void run() {
1324 ((Advanceable) v).advance();
1325 }
1326 }, delay);
1327 }
1328 i++;
1329 }
1330 sendAdvanceMessage(mAdvanceInterval);
1331 }
1332 }
1333 };
1334
1335 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001336 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001337 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1338 if (v instanceof Advanceable) {
1339 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001340 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001341 updateRunning();
1342 }
1343 }
1344
1345 void removeWidgetToAutoAdvance(View hostView) {
1346 if (mWidgetsToAdvance.containsKey(hostView)) {
1347 mWidgetsToAdvance.remove(hostView);
1348 updateRunning();
1349 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001350 }
1351
Joe Onorato9c1289c2009-08-17 11:03:03 -04001352 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001353 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001354 launcherInfo.hostView = null;
1355 }
1356
Winson Chung93eef082012-03-23 15:59:27 -07001357 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1358 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1359 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001360 }
1361
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001362 public LauncherAppWidgetHost getAppWidgetHost() {
1363 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001364 }
Romain Guycbb89e42009-06-08 15:52:54 -07001365
Winson Chunga9abd0e2010-10-27 17:18:37 -07001366 public LauncherModel getModel() {
1367 return mModel;
1368 }
1369
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001370 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001371 getWindow().closeAllPanels();
1372
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001373 // Whatever we were doing is hereby canceled.
1374 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001375 }
1376
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 @Override
1378 protected void onNewIntent(Intent intent) {
1379 super.onNewIntent(intent);
1380
1381 // Close the menu
1382 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001383 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001384 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001385
Joe Onorato14f122b2009-11-19 14:06:36 -08001386 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1387 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001388
Adam Cohenac56cff2011-09-28 20:45:37 -07001389 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001390 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001391 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001392 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1393 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001394 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001395 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001396
1397 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001398 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001399
1400 // If we are already on home, then just animate back to the workspace, otherwise, just
1401 // wait until onResume to set the state back to Workspace
1402 if (alreadyOnHome) {
1403 showWorkspace(true);
1404 } else {
1405 mOnResumeState = State.WORKSPACE;
1406 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001407
Joe Onorato3a8820b2009-11-10 15:06:42 -08001408 final View v = getWindow().peekDecorView();
1409 if (v != null && v.getWindowToken() != null) {
1410 InputMethodManager imm = (InputMethodManager)getSystemService(
1411 INPUT_METHOD_SERVICE);
1412 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001413 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001414
Michael Jurka35aa14d2011-07-07 17:01:08 -07001415 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001416 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001417 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001418 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001419 }
1420 }
1421
1422 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001423 public void onRestoreInstanceState(Bundle state) {
1424 super.onRestoreInstanceState(state);
1425 for (int page: mSynchronouslyBoundPages) {
1426 mWorkspace.restoreInstanceStateForChild(page);
1427 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001428 }
1429
1430 @Override
1431 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001432 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001433 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001434
Michael Jurka883f55b2010-10-21 15:47:14 -07001435 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001436 // We close any open folder since it will not be re-opened, and we need to make sure
1437 // this state is reflected.
1438 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439
Winson Chung3d503fb2011-07-13 17:25:49 -07001440 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1441 mWaitingForResult) {
1442 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1443 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1444 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1445 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001446 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1447 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1448 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001449 }
1450
1451 if (mFolderInfo != null && mWaitingForResult) {
1452 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1453 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1454 }
Michael Jurka946ad472010-07-09 18:05:18 -07001455
Winson Chung785d2eb2011-04-14 16:08:02 -07001456 // Save the current AppsCustomize tab
1457 if (mAppsCustomizeTabHost != null) {
1458 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1459 if (currentTabTag != null) {
1460 outState.putString("apps_customize_currentTab", currentTabTag);
1461 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001462 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1463 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001464 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 }
1466
1467 @Override
1468 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001469 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001470
Winson Chunge7a03942011-08-05 15:05:12 -07001471 // Remove all pending runnables
1472 mHandler.removeMessages(ADVANCE_MSG);
1473 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001474 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001475
Winson Chungcd2b0142011-06-08 16:02:26 -07001476 // Stop callbacks from LauncherModel
1477 LauncherApplication app = ((LauncherApplication) getApplication());
1478 mModel.stopLoader();
1479 app.setLauncher(null);
1480
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001481 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001482 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001483 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001484 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001486 mAppWidgetHost = null;
1487
1488 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489
1490 TextKeyListener.getInstance().release();
1491
Winson Chung81b52252012-08-27 15:34:29 -07001492 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1493 // to prevent leaking Launcher activities on orientation change.
1494 if (mModel != null) {
1495 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1496 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001497
1498 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001499 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001500
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001501 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001502 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1503 mWorkspace.removeAllViews();
1504 mWorkspace = null;
1505 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001506
Michael Jurka2ecf9952012-06-18 12:52:28 -07001507 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 }
1509
Adam Cohena9cf38f2011-05-02 15:36:58 -07001510 public DragController getDragController() {
1511 return mDragController;
1512 }
1513
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 @Override
1515 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001516 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 super.startActivityForResult(intent, requestCode);
1518 }
1519
Winson Chung70d72102011-08-12 11:24:35 -07001520 /**
1521 * Indicates that we want global search for this activity by setting the globalSearch
1522 * argument for {@link #startSearch} to true.
1523 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001525 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001527
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001528 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001529
Karl Rosaen138a0412009-04-23 19:00:21 -07001530 if (initialQuery == null) {
1531 // Use any text typed in the launcher as the initial query
1532 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001534 if (appSearchData == null) {
1535 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001536 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 }
Winson Chungadf0c182012-08-23 14:59:07 -07001538 Rect sourceBounds = new Rect();
1539 if (mSearchDropTargetBar != null) {
1540 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1541 }
Romain Guycbb89e42009-06-08 15:52:54 -07001542
Michael Jurkaa33411c2012-06-14 16:18:21 -07001543 startGlobalSearch(initialQuery, selectInitialQuery,
1544 appSearchData, sourceBounds);
1545 }
1546
1547 /**
1548 * Starts the global search activity. This code is a copied from SearchManager
1549 */
1550 public void startGlobalSearch(String initialQuery,
1551 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001552 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001553 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1554 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1555 if (globalSearchActivity == null) {
1556 Log.w(TAG, "No global search activity found.");
1557 return;
1558 }
1559 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1560 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1561 intent.setComponent(globalSearchActivity);
1562 // Make sure that we have a Bundle to put source in
1563 if (appSearchData == null) {
1564 appSearchData = new Bundle();
1565 } else {
1566 appSearchData = new Bundle(appSearchData);
1567 }
1568 // Set source to package name of app that starts global search, if not set already.
1569 if (!appSearchData.containsKey("source")) {
1570 appSearchData.putString("source", getPackageName());
1571 }
1572 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1573 if (!TextUtils.isEmpty(initialQuery)) {
1574 intent.putExtra(SearchManager.QUERY, initialQuery);
1575 }
1576 if (selectInitialQuery) {
1577 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1578 }
1579 intent.setSourceBounds(sourceBounds);
1580 try {
1581 startActivity(intent);
1582 } catch (ActivityNotFoundException ex) {
1583 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1584 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 }
1586
Winson Chung70d72102011-08-12 11:24:35 -07001587 @Override
1588 public boolean onCreateOptionsMenu(Menu menu) {
1589 if (isWorkspaceLocked()) {
1590 return false;
1591 }
1592
1593 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001594
1595 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1596 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1597 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001598 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1599 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1600 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001601 String helpUrl = getString(R.string.help_url);
1602 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001603 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1604 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1605
Winson Chung70d72102011-08-12 11:24:35 -07001606 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1607 .setIcon(android.R.drawable.ic_menu_gallery)
1608 .setAlphabeticShortcut('W');
1609 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1610 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001611 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001612 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001613 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1614 .setIcon(android.R.drawable.ic_menu_preferences)
1615 .setIntent(settings)
1616 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001617 if (!helpUrl.isEmpty()) {
1618 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1619 .setIcon(android.R.drawable.ic_menu_help)
1620 .setIntent(help)
1621 .setAlphabeticShortcut('H');
1622 }
Winson Chung70d72102011-08-12 11:24:35 -07001623 return true;
1624 }
1625
1626 @Override
1627 public boolean onPrepareOptionsMenu(Menu menu) {
1628 super.onPrepareOptionsMenu(menu);
1629
1630 if (mAppsCustomizeTabHost.isTransitioning()) {
1631 return false;
1632 }
1633 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1634 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1635
1636 return true;
1637 }
1638
1639 @Override
1640 public boolean onOptionsItemSelected(MenuItem item) {
1641 switch (item.getItemId()) {
1642 case MENU_WALLPAPER_SETTINGS:
1643 startWallpaper();
1644 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001645 }
1646
1647 return super.onOptionsItemSelected(item);
1648 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001650 @Override
1651 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001652 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001653 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001654 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001655 }
1656
Joe Onorato9c1289c2009-08-17 11:03:03 -04001657 public boolean isWorkspaceLocked() {
1658 return mWorkspaceLoading || mWaitingForResult;
1659 }
1660
Michael Jurka0280c3b2010-09-17 15:00:07 -07001661 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001662 mPendingAddInfo.container = ItemInfo.NO_ID;
1663 mPendingAddInfo.screen = -1;
1664 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1665 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001666 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001667 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001668 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001669
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001670 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1671 AppWidgetProviderInfo appWidgetInfo) {
1672 if (appWidgetInfo.configure != null) {
1673 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001674
Bjorn Bringert7984c942009-12-09 15:38:25 +00001675 // Launch over to configure widget, if needed
1676 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001677 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001678 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001679 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001681 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001682 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1683 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001684 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001685 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 }
1687 }
Romain Guycbb89e42009-06-08 15:52:54 -07001688
Adam Cohenfbba09b2011-07-18 21:43:05 -07001689 /**
1690 * Process a shortcut drop.
1691 *
1692 * @param componentName The name of the component
1693 * @param screen The screen where it should be added
1694 * @param cell The cell it should be added to, optional
1695 * @param position The location on the screen where it was dropped, optional
1696 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001697 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1698 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001699 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001700 mPendingAddInfo.container = container;
1701 mPendingAddInfo.screen = screen;
1702 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001703
1704 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001705 mPendingAddInfo.cellX = cell[0];
1706 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001707 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001708
1709 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1710 createShortcutIntent.setComponent(componentName);
1711 processShortcut(createShortcutIntent);
1712 }
1713
Adam Cohenfbba09b2011-07-18 21:43:05 -07001714 /**
1715 * Process a widget drop.
1716 *
1717 * @param info The PendingAppWidgetInfo of the widget being added.
1718 * @param screen The screen where it should be added
1719 * @param cell The cell it should be added to, optional
1720 * @param position The location on the screen where it was dropped, optional
1721 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001722 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001723 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001724 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001725 mPendingAddInfo.container = info.container = container;
1726 mPendingAddInfo.screen = info.screen = screen;
1727 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001728 mPendingAddInfo.minSpanX = info.minSpanX;
1729 mPendingAddInfo.minSpanY = info.minSpanY;
1730
Adam Cohenfbba09b2011-07-18 21:43:05 -07001731 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001732 mPendingAddInfo.cellX = cell[0];
1733 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001734 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001735 if (span != null) {
1736 mPendingAddInfo.spanX = span[0];
1737 mPendingAddInfo.spanY = span[1];
1738 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001739
Adam Cohened66b2b2012-01-23 17:28:51 -08001740 AppWidgetHostView hostView = info.boundWidget;
1741 int appWidgetId;
1742 if (hostView != null) {
1743 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001744 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001745 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001746 // In this case, we either need to start an activity to get permission to bind
1747 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001748 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001749 Bundle options = info.bindOptions;
1750
1751 boolean success = false;
1752 if (options != null) {
1753 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1754 info.componentName, options);
1755 } else {
1756 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1757 info.componentName);
1758 }
1759 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001760 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001761 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001762 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001763 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1764 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1765 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001766 // TODO: we need to make sure that this accounts for the options bundle.
1767 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001768 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1769 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001770 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001771 }
1772
Joe Onoratodeb98af2010-02-19 14:59:39 -08001773 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001774 // Handle case where user selected "Applications"
1775 String applicationName = getResources().getString(R.string.group_applications);
1776 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001777
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001778 if (applicationName != null && applicationName.equals(shortcutName)) {
1779 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1780 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001781
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001782 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1783 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001784 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001785 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001786 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001787 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001788 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001789 }
1790
Winson Chung24ab2f12010-09-16 14:10:47 -07001791 void processWallpaper(Intent intent) {
1792 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1793 }
1794
Winson Chung3d503fb2011-07-13 17:25:49 -07001795 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1796 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001797 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001798 folderInfo.title = getText(R.string.folder_name);
1799
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001800 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001801 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1802 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001803 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001804
1805 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001806 FolderIcon newFolder =
1807 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1808 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1809 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001810 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001811 }
Romain Guycbb89e42009-06-08 15:52:54 -07001812
Joe Onorato9c1289c2009-08-17 11:03:03 -04001813 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001814 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001815 }
1816
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001817 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001818 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001819 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001820 Intent chooser = Intent.createChooser(pickWallpaper,
1821 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001822 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1823 // Removed in Eclair MR1
1824// WallpaperManager wm = (WallpaperManager)
1825// getSystemService(Context.WALLPAPER_SERVICE);
1826// WallpaperInfo wi = wm.getWallpaperInfo();
1827// if (wi != null && wi.getSettingsActivity() != null) {
1828// LabeledIntent li = new LabeledIntent(getPackageName(),
1829// R.string.configure_wallpaper, 0);
1830// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1831// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1832// }
Mike Clerona0618e42009-10-22 13:55:21 -07001833 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 }
1835
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001836 /**
1837 * Registers various content observers. The current implementation registers
1838 * only a favorites observer to keep track of the favorites applications.
1839 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001840 private void registerContentObservers() {
1841 ContentResolver resolver = getContentResolver();
1842 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1843 true, mWidgetObserver);
1844 }
1845
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001846 @Override
1847 public boolean dispatchKeyEvent(KeyEvent event) {
1848 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1849 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001850 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001851 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001852 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001853 if (doesFileExist(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001854 dumpState();
1855 return true;
1856 }
1857 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001858 }
1859 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1860 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001861 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001862 return true;
1863 }
1864 }
1865
1866 return super.dispatchKeyEvent(event);
1867 }
1868
Joe Onorato88ec0992009-11-19 13:16:06 -08001869 @Override
1870 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001871 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001872 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001873 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001874 Folder openFolder = mWorkspace.getOpenFolder();
1875 if (openFolder.isEditingName()) {
1876 openFolder.dismissEditingName();
1877 } else {
1878 closeFolder();
1879 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001880 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001881 mWorkspace.exitWidgetResizeMode();
1882
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001883 // Back button is a no-op here, but give at least some feedback for the button press
1884 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001885 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001886 }
1887
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001889 * Re-listen when widgets are reset.
1890 */
1891 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001892 if (mAppWidgetHost != null) {
1893 mAppWidgetHost.startListening();
1894 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001895 }
1896
1897 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001898 * Launches the intent referred by the clicked shortcut.
1899 *
1900 * @param v The view representing the clicked shortcut.
1901 */
1902 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001903 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1904 // view has detached (it's possible for this to happen if the view is removed mid touch).
1905 if (v.getWindowToken() == null) {
1906 return;
1907 }
1908
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001909 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001910 return;
1911 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001912
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001914 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001915 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001916 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001917 int[] pos = new int[2];
1918 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001919 intent.setSourceBounds(new Rect(pos[0], pos[1],
1920 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001921
1922 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001923
1924 if (success && v instanceof BubbleTextView) {
1925 mWaitingForResume = (BubbleTextView) v;
1926 mWaitingForResume.setStayPressed(true);
1927 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001928 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001929 if (v instanceof FolderIcon) {
1930 FolderIcon fi = (FolderIcon) v;
1931 handleFolderClick(fi);
1932 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001933 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001934 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001935 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001936 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001937 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001938 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 }
1940 }
1941
Michael Jurka0e260592010-06-30 17:07:39 -07001942 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001943 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001944 // clicking anywhere on the workspace causes the customization drawer to slide down
1945 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001946 return false;
1947 }
1948
Michael Jurkaaf442092010-06-10 17:01:57 -07001949 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001950 * Event handler for the search button
1951 *
1952 * @param v The view that was clicked.
1953 */
1954 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001955 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1956
Amith Yamasania135ba82011-08-09 17:42:01 -07001957 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001958 }
1959
1960 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001961 * Event handler for the voice button
1962 *
1963 * @param v The view that was clicked.
1964 */
1965 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001966 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001967
Michael Jurkaae65ee32012-04-30 11:45:54 -07001968 try {
1969 final SearchManager searchManager =
1970 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1971 ComponentName activityName = searchManager.getGlobalSearchActivity();
1972 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001973 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001974 if (activityName != null) {
1975 intent.setPackage(activityName.getPackageName());
1976 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001977 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07001978 } catch (ActivityNotFoundException e) {
1979 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001980 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001981 startActivitySafely(null, intent, "onClickVoiceButton");
1982 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001983 }
1984
1985 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001986 * Event handler for the "grid" button that appears on the home screen, which
1987 * enters all apps mode.
1988 *
1989 * @param v The view that was clicked.
1990 */
1991 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001992 showAllApps(true);
1993 }
1994
1995 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001996 // Provide the same haptic feedback that the system offers for virtual keys.
1997 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001998 }
1999
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002000 public void onClickAppMarketButton(View v) {
2001 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002002 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002003 } else {
2004 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002005 }
2006 }
2007
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002008 void startApplicationDetailsActivity(ComponentName componentName) {
2009 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002010 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2011 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002012 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002013 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002014 }
2015
Patrick Dubroy5539af72010-09-07 15:22:01 -07002016 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2017 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2018 // System applications cannot be installed. For now, show a toast explaining that.
2019 // We may give them the option of disabling apps this way.
2020 int messageId = R.string.uninstall_system_app_text;
2021 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2022 } else {
2023 String packageName = appInfo.componentName.getPackageName();
2024 String className = appInfo.componentName.getClassName();
2025 Intent intent = new Intent(
2026 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002027 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2028 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002029 startActivity(intent);
2030 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002031 }
2032
Michael Jurka86a720e2012-05-09 11:23:23 -07002033 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002035
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002037 // Only launch using the new animation if the shortcut has not opted out (this is a
2038 // private contract between launcher and may be ignored in the future).
2039 boolean useLaunchAnimation = (v != null) &&
2040 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2041 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002042 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2043 v.getMeasuredWidth(), v.getMeasuredHeight());
2044
2045 startActivity(intent, opts.toBundle());
2046 } else {
2047 startActivity(intent);
2048 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002049 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 } catch (SecurityException e) {
2051 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002052 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002054 "or use the exported attribute for this activity. "
2055 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002057 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002059
Michael Jurka86a720e2012-05-09 11:23:23 -07002060 boolean startActivitySafely(View v, Intent intent, Object tag) {
2061 boolean success = false;
2062 try {
2063 success = startActivity(v, intent, tag);
2064 } catch (ActivityNotFoundException e) {
2065 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2066 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2067 }
2068 return success;
2069 }
2070
Romain Guy8e633c52010-03-04 12:51:36 -08002071 void startActivityForResultSafely(Intent intent, int requestCode) {
2072 try {
2073 startActivityForResult(intent, requestCode);
2074 } catch (ActivityNotFoundException e) {
2075 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2076 } catch (SecurityException e) {
2077 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2078 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2079 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2080 "or use the exported attribute for this activity.", e);
2081 }
2082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083
Adam Cohena9cf38f2011-05-02 15:36:58 -07002084 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002085 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002086 Folder openFolder = mWorkspace.getFolderForTag(info);
2087
2088 // If the folder info reports that the associated folder is open, then verify that
2089 // it is actually opened. There have been a few instances where this gets out of sync.
2090 if (info.opened && openFolder == null) {
2091 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2092 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2093 info.opened = false;
2094 }
2095
Adam Cohenfb91f302012-06-11 15:45:18 -07002096 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 // Close any open folder
2098 closeFolder();
2099 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002100 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 } else {
2102 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 int folderScreen;
2104 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002105 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 // .. and close it
2107 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002108 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 // Close any folder open on the current screen
2110 closeFolder();
2111 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002112 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 }
2114 }
2115 }
2116 }
2117
Adam Cohen268c4752012-06-06 17:47:33 -07002118 /**
2119 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2120 * in the DragLayer in the exact absolute location of the original FolderIcon.
2121 */
2122 private void copyFolderIconToImage(FolderIcon fi) {
2123 final int width = fi.getMeasuredWidth();
2124 final int height = fi.getMeasuredHeight();
2125
2126 // Lazy load ImageView, Bitmap and Canvas
2127 if (mFolderIconImageView == null) {
2128 mFolderIconImageView = new ImageView(this);
2129 }
2130 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2131 mFolderIconBitmap.getHeight() != height) {
2132 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2133 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2134 }
2135
2136 DragLayer.LayoutParams lp;
2137 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2138 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2139 } else {
2140 lp = new DragLayer.LayoutParams(width, height);
2141 }
2142
Adam Cohen307fe232012-08-16 17:55:58 -07002143 // The layout from which the folder is being opened may be scaled, adjust the starting
2144 // view size by this scale factor.
2145 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002146 lp.customPosition = true;
2147 lp.x = mRectForFolderAnimation.left;
2148 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002149 lp.width = (int) (scale * width);
2150 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002151
2152 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2153 fi.draw(mFolderIconCanvas);
2154 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002155 if (fi.getFolder() != null) {
2156 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2157 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002158 }
Adam Cohen268c4752012-06-06 17:47:33 -07002159 // Just in case this image view is still in the drag layer from a previous animation,
2160 // we remove it and re-add it.
2161 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2162 mDragLayer.removeView(mFolderIconImageView);
2163 }
2164 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002165 if (fi.getFolder() != null) {
2166 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002167 }
Adam Cohen268c4752012-06-06 17:47:33 -07002168 }
2169
Adam Cohen2801caf2011-05-13 20:57:39 -07002170 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002171 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002172 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2173 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2174 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2175
Adam Cohenc51934b2011-07-26 21:07:43 -07002176 FolderInfo info = (FolderInfo) fi.getTag();
2177 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2178 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002179 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2180 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002181 }
2182
Adam Cohen268c4752012-06-06 17:47:33 -07002183 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2184 copyFolderIconToImage(fi);
2185 fi.setVisibility(View.INVISIBLE);
2186
Michael Jurka2ecf9952012-06-18 12:52:28 -07002187 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002188 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002189 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2190 oa.start();
2191 }
2192
Adam Cohen268c4752012-06-06 17:47:33 -07002193 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002194 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002195 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2196 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2197 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2198
Adam Cohen268c4752012-06-06 17:47:33 -07002199 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002200
Adam Cohen268c4752012-06-06 17:47:33 -07002201 // We remove and re-draw the FolderIcon in-case it has changed
2202 mDragLayer.removeView(mFolderIconImageView);
2203 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002204 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002205 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002206 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002207 oa.addListener(new AnimatorListenerAdapter() {
2208 @Override
2209 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002210 if (cl != null) {
2211 cl.clearFolderLeaveBehind();
2212 // Remove the ImageView copy of the FolderIcon and make the original visible.
2213 mDragLayer.removeView(mFolderIconImageView);
2214 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002215 }
2216 }
2217 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002218 oa.start();
2219 }
2220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002221 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002222 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002223 * is animated relative to the specified View. If the View is null, no animation
2224 * is played.
2225 *
2226 * @param folderInfo The FolderInfo describing the folder to open.
2227 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002228 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002229 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002230 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002231
Adam Cohena9cf38f2011-05-02 15:36:58 -07002232 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002233
Adam Cohen4554ee12011-08-03 16:13:21 -07002234 // Just verify that the folder hasn't already been added to the DragLayer.
2235 // There was a one-off crash where the folder had a parent already.
2236 if (folder.getParent() == null) {
2237 mDragLayer.addView(folder);
2238 mDragController.addDropTarget((DropTarget) folder);
2239 } else {
2240 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2241 folder.getParent() + ").");
2242 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002243 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002244 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002245 }
2246
2247 public void closeFolder() {
2248 Folder folder = mWorkspace.getOpenFolder();
2249 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002250 if (folder.isEditingName()) {
2251 folder.dismissEditingName();
2252 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002253 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002254
2255 // Dismiss the folder cling
2256 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002257 }
2258 }
2259
2260 void closeFolder(Folder folder) {
2261 folder.getInfo().opened = false;
2262
2263 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2264 if (parent != null) {
2265 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2266 shrinkAndFadeInFolderIcon(fi);
2267 }
2268 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002269 }
2270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002272 if (!isDraggingEnabled()) return false;
2273 if (isWorkspaceLocked()) return false;
2274 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002275
2276 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002277 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 }
2279
Michael Jurka0280c3b2010-09-17 15:00:07 -07002280 resetAddInfo();
2281 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002282 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002283 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 return true;
2285 }
2286
Winson Chung3d503fb2011-07-13 17:25:49 -07002287 // The hotseat touch handling does not go through Workspace, and we always allow long press
2288 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002289 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002290 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2291 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002292 if (itemUnderLongClick == null) {
2293 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002294 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2295 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002296 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002298 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002299 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002300 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002301 }
2302 }
2303 }
2304 return true;
2305 }
2306
Winson Chung3d503fb2011-07-13 17:25:49 -07002307 boolean isHotseatLayout(View layout) {
2308 return mHotseat != null && layout != null &&
2309 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2310 }
2311 Hotseat getHotseat() {
2312 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002313 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002314 SearchDropTargetBar getSearchBar() {
2315 return mSearchDropTargetBar;
2316 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002317
Winson Chung3d503fb2011-07-13 17:25:49 -07002318 /**
2319 * Returns the CellLayout of the specified container at the specified screen.
2320 */
2321 CellLayout getCellLayout(long container, int screen) {
2322 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2323 if (mHotseat != null) {
2324 return mHotseat.getLayout();
2325 } else {
2326 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002327 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002328 } else {
2329 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002330 }
2331 }
2332
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 Workspace getWorkspace() {
2334 return mWorkspace;
2335 }
2336
Daniel Sandler843e8602010-06-07 14:59:01 -04002337 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2338 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002339 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002340 }
2341
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002342 public boolean isAllAppsButtonRank(int rank) {
2343 return mHotseat.isAllAppsButtonRank(rank);
2344 }
2345
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002346 /**
2347 * Helper method for the cameraZoomIn/cameraZoomOut animations
2348 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002349 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002350 * @param scaleFactor The scale factor used for the zoom
2351 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002352 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002353 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002354 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002355 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002356
Winson Chung18f41f82012-05-09 13:28:10 -07002357 void disableWallpaperIfInAllApps() {
2358 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002359 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002360 if (mAppsCustomizeTabHost != null &&
2361 !mAppsCustomizeTabHost.isTransitioning()) {
2362 updateWallpaperVisibility(false);
2363 }
2364 }
2365 }
2366
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002367 void updateWallpaperVisibility(boolean visible) {
2368 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2369 int curflags = getWindow().getAttributes().flags
2370 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2371 if (wpflags != curflags) {
2372 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2373 }
2374 }
2375
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002376 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2377 if (v instanceof LauncherTransitionable) {
2378 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2379 }
2380 }
2381
Michael Jurkabed61d22012-02-14 22:51:29 -08002382 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2383 if (v instanceof LauncherTransitionable) {
2384 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2385 }
Winson Chung70442722012-02-10 15:43:22 -08002386
2387 // Update the workspace transition step as well
2388 dispatchOnLauncherTransitionStep(v, 0f);
2389 }
2390
2391 private void dispatchOnLauncherTransitionStep(View v, float t) {
2392 if (v instanceof LauncherTransitionable) {
2393 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2394 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002395 }
2396
2397 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2398 if (v instanceof LauncherTransitionable) {
2399 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2400 }
Winson Chung70442722012-02-10 15:43:22 -08002401
2402 // Update the workspace transition step as well
2403 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002404 }
2405
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002406 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002407 * Things to test when changing the following seven functions.
2408 * - Home from workspace
2409 * - from center screen
2410 * - from other screens
2411 * - Home from all apps
2412 * - from center screen
2413 * - from other screens
2414 * - Back from all apps
2415 * - from center screen
2416 * - from other screens
2417 * - Launch app from workspace and quit
2418 * - with back
2419 * - with home
2420 * - Launch app from all apps and quit
2421 * - with back
2422 * - with home
2423 * - Go to a screen that's not the default, then all
2424 * apps, and launch and app, and go back
2425 * - with back
2426 * -with home
2427 * - On workspace, long press power and go back
2428 * - with back
2429 * - with home
2430 * - On all apps, long press power and go back
2431 * - with back
2432 * - with home
2433 * - On workspace, power off
2434 * - On all apps, power off
2435 * - Launch an app and turn off the screen while in that app
2436 * - Go back with home key
2437 * - Go back with back key TODO: make this not go to workspace
2438 * - From all apps
2439 * - From workspace
2440 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2441 * - From all apps
2442 * - From the center workspace
2443 * - From another workspace
2444 */
2445
2446 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002447 * Zoom the camera out from the workspace to reveal 'toView'.
2448 * Assumes that the view to show is anchored at either the very top or very bottom
2449 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002450 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002451 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002452 if (mStateAnimation != null) {
2453 mStateAnimation.cancel();
2454 mStateAnimation = null;
2455 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002456 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002457
Winson Chungf0ea4d32011-06-06 14:27:16 -07002458 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2459 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2460 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002461 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002462 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002463 final int startDelay =
2464 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002465
Michael Jurkab3e22d92011-10-31 15:58:33 -07002466 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002467
Michael Jurkad74c9842011-07-10 12:44:21 -07002468 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002469 Animator workspaceAnim =
2470 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002471
2472 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002473 toView.setScaleX(scale);
2474 toView.setScaleY(scale);
2475 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2476 scaleAnim.
2477 scaleX(1f).scaleY(1f).
2478 setDuration(duration).
2479 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002480
Winson Chungf0ea4d32011-06-06 14:27:16 -07002481 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002482 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002483 final ObjectAnimator alphaAnim = ObjectAnimator
2484 .ofFloat(toView, "alpha", 0f, 1f)
2485 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002486 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002487 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2488 @Override
2489 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002490 if (animation == null) {
2491 throw new RuntimeException("animation is null");
2492 }
Winson Chung70442722012-02-10 15:43:22 -08002493 float t = (Float) animation.getAnimatedValue();
2494 dispatchOnLauncherTransitionStep(fromView, t);
2495 dispatchOnLauncherTransitionStep(toView, t);
2496 }
2497 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002498
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002499 // toView should appear right at the end of the workspace shrink
2500 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002501 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002502 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002503 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002504
2505 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002506 boolean animationCancelled = false;
2507
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002508 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002509 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002510 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002511 // Prepare the position
2512 toView.setTranslationX(0.0f);
2513 toView.setTranslationY(0.0f);
2514 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002515 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002516 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002517 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002518 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002519 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2520 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002521
Michael Jurkafa7969f2012-09-21 16:39:14 -07002522 if (mWorkspace != null && !springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002523 // Hide the workspace scrollbar
2524 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002525 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002526 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002527 if (!animationCancelled) {
2528 updateWallpaperVisibility(false);
2529 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002530
2531 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002532 if (mSearchDropTargetBar != null) {
2533 mSearchDropTargetBar.hideSearchBar(false);
2534 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002535 }
2536
Adam Cohencff6af82011-09-13 14:51:53 -07002537 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002538 public void onAnimationCancel(Animator animation) {
2539 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002540 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002541 });
2542
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002543 if (workspaceAnim != null) {
2544 mStateAnimation.play(workspaceAnim);
2545 }
Michael Jurka1899a362011-11-03 13:50:45 -07002546
2547 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002548 final ViewTreeObserver observer;
2549
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002550 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2551 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002552
2553 // If any of the objects being animated haven't been measured/laid out
2554 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002555 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002556 (mWorkspace.getMeasuredWidth() == 0) ||
2557 (toView.getMeasuredWidth() == 0)) {
2558 observer = mWorkspace.getViewTreeObserver();
2559 delayAnim = true;
2560 } else {
2561 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002562 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002563
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002564 final AnimatorSet stateAnimation = mStateAnimation;
2565 final Runnable startAnimRunnable = new Runnable() {
2566 public void run() {
2567 // Check that mStateAnimation hasn't changed while
2568 // we waited for a layout/draw pass
2569 if (mStateAnimation != stateAnimation)
2570 return;
2571 setPivotsForZoom(toView, scale);
2572 dispatchOnLauncherTransitionStart(fromView, animated, false);
2573 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka382aa182012-06-11 15:42:07 -07002574 toView.post(new Runnable() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002575 public void run() {
2576 // Check that mStateAnimation hasn't changed while
2577 // we waited for a layout/draw pass
2578 if (mStateAnimation != stateAnimation)
2579 return;
2580 mStateAnimation.start();
2581 }
2582 });
2583 }
2584 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002585 if (delayAnim) {
2586 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2587 public void onGlobalLayout() {
Michael Jurka382aa182012-06-11 15:42:07 -07002588 toView.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002589 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002590 }
2591 };
2592 observer.addOnGlobalLayoutListener(delayedStart);
2593 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002594 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002595 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002596 } else {
2597 toView.setTranslationX(0.0f);
2598 toView.setTranslationY(0.0f);
2599 toView.setScaleX(1.0f);
2600 toView.setScaleY(1.0f);
2601 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002602 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002603
Michael Jurkabed61d22012-02-14 22:51:29 -08002604 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2605 // Hide the workspace scrollbar
2606 mWorkspace.hideScrollingIndicator(true);
2607 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002608
2609 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002610 if (mSearchDropTargetBar != null) {
2611 mSearchDropTargetBar.hideSearchBar(false);
2612 }
Michael Jurkaabded662011-03-04 12:06:57 -08002613 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002614 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002615 dispatchOnLauncherTransitionStart(fromView, animated, false);
2616 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002617 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002618 dispatchOnLauncherTransitionStart(toView, animated, false);
2619 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002620 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002621 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002622 }
2623
2624 /**
2625 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002626 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002627 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002628 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002629 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2630 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002631
Michael Jurkab3e22d92011-10-31 15:58:33 -07002632 if (mStateAnimation != null) {
2633 mStateAnimation.cancel();
2634 mStateAnimation = null;
2635 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002636 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002637
Winson Chungf0ea4d32011-06-06 14:27:16 -07002638 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002639 final int fadeOutDuration =
2640 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002641 final float scaleFactor = (float)
2642 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2643 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002644 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002645 Animator workspaceAnim = null;
2646
2647 if (toState == State.WORKSPACE) {
2648 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2649 workspaceAnim = mWorkspace.getChangeStateAnimation(
2650 Workspace.State.NORMAL, animated, stagger);
2651 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2652 workspaceAnim = mWorkspace.getChangeStateAnimation(
2653 Workspace.State.SPRING_LOADED, animated);
2654 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002655
Michael Jurkab3e22d92011-10-31 15:58:33 -07002656 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002657 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002658 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002659 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002660 final LauncherViewPropertyAnimator scaleAnim =
2661 new LauncherViewPropertyAnimator(fromView);
2662 scaleAnim.
2663 scaleX(scaleFactor).scaleY(scaleFactor).
2664 setDuration(duration).
2665 setInterpolator(new Workspace.ZoomInInterpolator());
2666
2667 final ObjectAnimator alphaAnim = ObjectAnimator
2668 .ofFloat(fromView, "alpha", 1f, 0f)
2669 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002670 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002671 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2672 @Override
2673 public void onAnimationUpdate(ValueAnimator animation) {
2674 float t = 1f - (Float) animation.getAnimatedValue();
2675 dispatchOnLauncherTransitionStep(fromView, t);
2676 dispatchOnLauncherTransitionStep(toView, t);
2677 }
2678 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002679
Michael Jurka2ecf9952012-06-18 12:52:28 -07002680 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002681
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002682 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2683 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002684
2685 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002686 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002687 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002688 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002689 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002690 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2691 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002692 if (mWorkspace != null) {
2693 mWorkspace.hideScrollingIndicator(false);
2694 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002695 if (onCompleteRunnable != null) {
2696 onCompleteRunnable.run();
2697 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002698 }
2699 });
2700
Winson Chungf0ea4d32011-06-06 14:27:16 -07002701 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002702 if (workspaceAnim != null) {
2703 mStateAnimation.play(workspaceAnim);
2704 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002705 dispatchOnLauncherTransitionStart(fromView, animated, true);
2706 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002707 final Animator stateAnimation = mStateAnimation;
2708 mWorkspace.post(new Runnable() {
2709 public void run() {
2710 if (stateAnimation != mStateAnimation)
2711 return;
2712 mStateAnimation.start();
2713 }
2714 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002715 } else {
2716 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002717 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002718 dispatchOnLauncherTransitionStart(fromView, animated, true);
2719 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002720 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002721 dispatchOnLauncherTransitionStart(toView, animated, true);
2722 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002723 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002724 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002725 }
2726
Michael Jurkae326f182011-11-21 14:05:46 -08002727 @Override
2728 public void onTrimMemory(int level) {
2729 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002730 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002731 mAppsCustomizeTabHost.onTrimMemory();
2732 }
2733 }
2734
Winson Chung18f41f82012-05-09 13:28:10 -07002735 @Override
2736 public void onWindowFocusChanged(boolean hasFocus) {
2737 if (!hasFocus) {
2738 // When another window occludes launcher (like the notification shade, or recents),
2739 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2740 updateWallpaperVisibility(true);
2741 } else {
2742 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002743 mWorkspace.postDelayed(new Runnable() {
2744 @Override
2745 public void run() {
2746 disableWallpaperIfInAllApps();
2747 }
2748 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002749 }
2750 }
2751
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002752 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002753 showWorkspace(animated, null);
2754 }
2755
2756 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002757 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002758 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002759 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002760 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002761
Winson Chungc7d2b602012-05-16 17:02:20 -07002762 // Show the search bar (only animate if we were showing the drop target bar in spring
2763 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002764 if (mSearchDropTargetBar != null) {
2765 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2766 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002767
Michael Jurkab737ee62011-11-15 15:57:22 -08002768 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002769 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002770
2771 // Set focus to the AppsCustomize button
2772 if (mAllAppsButton != null) {
2773 mAllAppsButton.requestFocus();
2774 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002775 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002776
Michael Jurkab737ee62011-11-15 15:57:22 -08002777 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002778
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002779 // Change the state *after* we've called all the transition code
2780 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002781
Winson Chung5fb63472011-02-02 17:03:37 -08002782 // Resume the auto-advance of widgets
2783 mUserPresent = true;
2784 updateRunning();
2785
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002786 // send an accessibility event to announce the context change
2787 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002788 }
2789
Michael Jurkab3e22d92011-10-31 15:58:33 -07002790 void showAllApps(boolean animated) {
2791 if (mState != State.WORKSPACE) return;
2792
2793 showAppsCustomizeHelper(animated, false);
2794 mAppsCustomizeTabHost.requestFocus();
2795
Michael Jurkab3e22d92011-10-31 15:58:33 -07002796 // Change the state *after* we've called all the transition code
2797 mState = State.APPS_CUSTOMIZE;
2798
2799 // Pause the auto-advance of widgets until we are out of AllApps
2800 mUserPresent = false;
2801 updateRunning();
2802 closeFolder();
2803
2804 // Send an accessibility event to announce the context change
2805 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2806 }
2807
Adam Cohen7777d962011-08-18 18:58:38 -07002808 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002809 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002810 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002811 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002812 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002813 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002814 }
Adam Cohen7777d962011-08-18 18:58:38 -07002815
Adam Cohened66b2b2012-01-23 17:28:51 -08002816 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2817 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002818 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2819
Winson Chunge7a03942011-08-05 15:05:12 -07002820 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002821 @Override
2822 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002823 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002824 // Before we show workspace, hide all apps again because
2825 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2826 // clean up our state transition functions
2827 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002828 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002829 } else {
2830 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002831 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002832 }
2833 }, (extendedDelay ?
2834 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2835 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2836 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002837
Michael Jurkad3ef3062010-11-23 16:23:58 -08002838 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002839 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002840 final boolean animated = true;
2841 final boolean springLoaded = true;
2842 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002843 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002844 }
2845 // Otherwise, we are not in spring loaded mode, so don't do anything.
2846 }
2847
Michael Jurkab737ee62011-11-15 15:57:22 -08002848 void hideDockDivider() {
2849 if (mQsbDivider != null && mDockDivider != null) {
2850 mQsbDivider.setVisibility(View.INVISIBLE);
2851 mDockDivider.setVisibility(View.INVISIBLE);
2852 }
2853 }
2854
2855 void showDockDivider(boolean animated) {
2856 if (mQsbDivider != null && mDockDivider != null) {
2857 mQsbDivider.setVisibility(View.VISIBLE);
2858 mDockDivider.setVisibility(View.VISIBLE);
2859 if (mDividerAnimator != null) {
2860 mDividerAnimator.cancel();
2861 mQsbDivider.setAlpha(1f);
2862 mDockDivider.setAlpha(1f);
2863 mDividerAnimator = null;
2864 }
2865 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002866 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2867 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2868 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07002869 int duration = 0;
2870 if (mSearchDropTargetBar != null) {
2871 duration = mSearchDropTargetBar.getTransitionInDuration();
2872 }
2873 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08002874 mDividerAnimator.start();
2875 }
2876 }
2877 }
2878
Michael Jurkab3e22d92011-10-31 15:58:33 -07002879 void lockAllApps() {
2880 // TODO
2881 }
2882
2883 void unlockAllApps() {
2884 // TODO
2885 }
2886
Winson Chungf0ea4d32011-06-06 14:27:16 -07002887 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002888 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002889 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002890 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002891 if (!LauncherApplication.isScreenLarge()) {
2892 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002893 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002894 int duration = 0;
2895 if (mSearchDropTargetBar != null) {
2896 duration = mSearchDropTargetBar.getTransitionInDuration();
2897 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002898 mHotseat.animate().alpha(1f).setDuration(duration);
2899 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002900 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002901 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002902 }
2903 }
2904 }
2905
2906 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002907 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002908 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002909 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002910 if (!LauncherApplication.isScreenLarge()) {
2911 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002912 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002913 int duration = 0;
2914 if (mSearchDropTargetBar != null) {
2915 duration = mSearchDropTargetBar.getTransitionOutDuration();
2916 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002917 mHotseat.animate().alpha(0f).setDuration(duration);
2918 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002919 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002920 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002921 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002922 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002923 }
2924
Patrick Dubroy5f445422011-02-18 14:35:21 -08002925 /**
2926 * Add an item from all apps or customize onto the given workspace screen.
2927 * If layout is null, add to the current screen.
2928 */
2929 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002930 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002931 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002932 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002933 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002934
Winson Chungdff8ebb2011-09-08 17:25:31 -07002935 /** Maps the current orientation to an index for referencing orientation correct global icons */
2936 private int getCurrentOrientationIndexForGlobalIcons() {
2937 // default - 0, landscape - 1
2938 switch (getResources().getConfiguration().orientation) {
2939 case Configuration.ORIENTATION_LANDSCAPE:
2940 return 1;
2941 default:
2942 return 0;
2943 }
2944 }
2945
Michael Jurkaae65ee32012-04-30 11:45:54 -07002946 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002947 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002948 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002949 // Look for the toolbar icon specified in the activity meta-data
2950 Bundle metaData = packageManager.getActivityInfo(
2951 activityName, PackageManager.GET_META_DATA).metaData;
2952 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002953 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002954 if (iconResId != 0) {
2955 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002956 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002957 }
2958 }
2959 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002960 // This can happen if the activity defines an invalid drawable
2961 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2962 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002963 } catch (Resources.NotFoundException nfe) {
2964 // This can happen if the activity defines an invalid drawable
2965 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2966 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002967 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002968 return null;
2969 }
2970
2971 // if successful in getting icon, return it; otherwise, set button to use default drawable
2972 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002973 int buttonId, ComponentName activityName, int fallbackDrawableId,
2974 String toolbarResourceName) {
2975 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002976 Resources r = getResources();
2977 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2978 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002979
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002980 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002981 // If we were unable to find the icon via the meta-data, use a generic one
2982 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002983 toolbarIcon = r.getDrawable(fallbackDrawableId);
2984 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002985 if (button != null) {
2986 button.setCompoundDrawables(toolbarIcon, null, null, null);
2987 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002988 return null;
2989 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002990 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002991 if (button != null) {
2992 button.setCompoundDrawables(toolbarIcon, null, null, null);
2993 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002994 return toolbarIcon.getConstantState();
2995 }
2996 }
2997
2998 // if successful in getting icon, return it; otherwise, set button to use default drawable
2999 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003000 int buttonId, ComponentName activityName, int fallbackDrawableId,
3001 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003002 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003003 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003004
Michael Jurka19e0fc52011-07-22 18:00:21 -07003005 if (button != null) {
3006 // If we were unable to find the icon via the meta-data, use a
3007 // generic one
3008 if (toolbarIcon == null) {
3009 button.setImageResource(fallbackDrawableId);
3010 } else {
3011 button.setImageDrawable(toolbarIcon);
3012 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003013 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003014
3015 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3016
Michael Jurka0423dcf2010-10-05 14:56:18 -07003017 }
3018
Winson Chung1b884092012-06-08 17:13:02 -07003019 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003020 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003021 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003022 }
3023
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003024 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003025 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003026 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003027 }
3028
Winson Chungbb185bd2011-11-21 12:31:42 -08003029 private void invalidatePressedFocusedStates(View container, View button) {
3030 if (container instanceof HolographicLinearLayout) {
3031 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3032 layout.invalidatePressedFocusedStates();
3033 } else if (button instanceof HolographicImageView) {
3034 HolographicImageView view = (HolographicImageView) button;
3035 view.invalidatePressedFocusedStates();
3036 }
3037 }
3038
Winson Chungc51db6a2011-10-05 11:44:49 -07003039 private boolean updateGlobalSearchIcon() {
3040 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003041 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003042 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003043 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003044 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003045
Winson Chung1cad91e2011-05-25 17:41:01 -07003046 final SearchManager searchManager =
3047 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3048 ComponentName activityName = searchManager.getGlobalSearchActivity();
3049 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003050 int coi = getCurrentOrientationIndexForGlobalIcons();
3051 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003052 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3053 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3054 if (sGlobalSearchIcon[coi] == null) {
3055 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3056 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3057 TOOLBAR_ICON_METADATA_NAME);
3058 }
3059
Winson Chungc51db6a2011-10-05 11:44:49 -07003060 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3061 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003062 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003063 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003064 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003065 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003066 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3067 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3068 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003069 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003070 if (voiceButtonProxy != null) {
3071 voiceButtonProxy.setVisibility(View.GONE);
3072 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003073 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003074 }
3075 }
3076
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003077 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003078 final View searchButtonContainer = findViewById(R.id.search_button_container);
3079 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003080 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003081 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003082 }
3083
Winson Chungc51db6a2011-10-05 11:44:49 -07003084 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003085 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003086 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003087 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003088
Winson Chungc51db6a2011-10-05 11:44:49 -07003089 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003090 final SearchManager searchManager =
3091 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3092 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3093
3094 ComponentName activityName = null;
3095 if (globalSearchActivity != null) {
3096 // Check if the global search activity handles voice search
3097 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3098 intent.setPackage(globalSearchActivity.getPackageName());
3099 activityName = intent.resolveActivity(getPackageManager());
3100 }
3101
3102 if (activityName == null) {
3103 // Fallback: check if an activity other than the global search activity
3104 // resolves this
3105 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3106 activityName = intent.resolveActivity(getPackageManager());
3107 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003108 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003109 int coi = getCurrentOrientationIndexForGlobalIcons();
3110 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003111 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3112 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3113 if (sVoiceSearchIcon[coi] == null) {
3114 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3115 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3116 TOOLBAR_ICON_METADATA_NAME);
3117 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003118 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003119 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003120 if (voiceButtonProxy != null) {
3121 voiceButtonProxy.setVisibility(View.VISIBLE);
3122 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003123 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003124 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003125 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003126 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003127 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003128 if (voiceButtonProxy != null) {
3129 voiceButtonProxy.setVisibility(View.GONE);
3130 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003131 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003132 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003133 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003134
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003135 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003136 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3137 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003138 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003139 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003140 }
3141
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003142 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003143 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003144 */
3145 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003146 final View marketButton = findViewById(R.id.market_button);
3147 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3148 // Find the app market activity by resolving an intent.
3149 // (If multiple app markets are installed, it will return the ResolverActivity.)
3150 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003151 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003152 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003153 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003154 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003155 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3156 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003157 marketButton.setVisibility(View.VISIBLE);
3158 } else {
3159 // We should hide and disable the view so that we don't try and restore the visibility
3160 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3161 marketButton.setVisibility(View.GONE);
3162 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003163 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003164 }
3165
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003166 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003167 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3168 Resources r = getResources();
3169 Drawable marketIconDrawable = d.newDrawable(r);
3170 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3171 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3172 marketIconDrawable.setBounds(0, 0, w, h);
3173
3174 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003175 }
3176
Michael Jurkad7c28052012-04-27 15:43:36 -07003177 @Override
3178 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3179 boolean result = super.dispatchPopulateAccessibilityEvent(event);
3180 final List<CharSequence> text = event.getText();
3181 text.clear();
3182 text.add(getString(R.string.home));
3183 return result;
3184 }
3185
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003186 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003187 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003188 */
3189 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3190 @Override
3191 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003192 closeSystemDialogs();
3193 }
3194 }
3195
3196 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003197 * Receives notifications whenever the appwidgets are reset.
3198 */
3199 private class AppWidgetResetObserver extends ContentObserver {
3200 public AppWidgetResetObserver() {
3201 super(new Handler());
3202 }
3203
3204 @Override
3205 public void onChange(boolean selfChange) {
3206 onAppWidgetReset();
3207 }
3208 }
3209
3210 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003211 * If the activity is currently paused, signal that we need to re-run the loader
3212 * in onResume.
3213 *
3214 * This needs to be called from incoming places where resources might have been loaded
3215 * while we are paused. That is becaues the Configuration might be wrong
3216 * when we're not running, and if it comes back to what it was when we
3217 * were paused, we are not restarted.
3218 *
3219 * Implementation of the method from LauncherModel.Callbacks.
3220 *
3221 * @return true if we are currently paused. The caller might be able to
3222 * skip some work in that case since we will come back again.
3223 */
3224 public boolean setLoadOnResume() {
3225 if (mPaused) {
3226 Log.i(TAG, "setLoadOnResume");
3227 mOnResumeNeedsLoad = true;
3228 return true;
3229 } else {
3230 return false;
3231 }
3232 }
3233
3234 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003235 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003236 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003237 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003238 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003239 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003240 } else {
3241 return SCREEN_COUNT / 2;
3242 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003243 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003244
Joe Onorato9c1289c2009-08-17 11:03:03 -04003245 /**
3246 * Refreshes the shortcuts shown on the workspace.
3247 *
3248 * Implementation of the method from LauncherModel.Callbacks.
3249 */
3250 public void startBinding() {
3251 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003252
Winson Chungf0c6ae02012-03-21 16:10:31 -07003253 mNewShortcutAnimatePage = -1;
3254 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003255 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003256 int count = workspace.getChildCount();
3257 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003258 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003259 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3260 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003261 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003262 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003263 if (mHotseat != null) {
3264 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003265 }
3266 }
3267
3268 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003269 * Bind the items start-end from the list.
3270 *
3271 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003272 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003273 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003274 setLoadOnResume();
3275
Winson Chungf0c6ae02012-03-21 16:10:31 -07003276 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3277 Set<String> newApps = new HashSet<String>();
3278 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3279
3280 Workspace workspace = mWorkspace;
3281 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003282 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003283
3284 // Short circuit if we are loading dock items for a configuration which has no dock
3285 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3286 mHotseat == null) {
3287 continue;
3288 }
3289
Joe Onorato9c1289c2009-08-17 11:03:03 -04003290 switch (item.itemType) {
3291 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3292 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003293 ShortcutInfo info = (ShortcutInfo) item;
3294 String uri = info.intent.toUri(0).toString();
3295 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003296 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3297 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003298 boolean animateIconUp = false;
3299 synchronized (newApps) {
3300 if (newApps.contains(uri)) {
3301 animateIconUp = newApps.remove(uri);
3302 }
3303 }
3304 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003305 // Prepare the view to be animated up
3306 shortcut.setAlpha(0f);
3307 shortcut.setScaleX(0f);
3308 shortcut.setScaleY(0f);
3309 mNewShortcutAnimatePage = item.screen;
3310 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3311 mNewShortcutAnimateViews.add(shortcut);
3312 }
3313 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003314 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003315 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003316 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003317 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003318 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003319 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3320 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003321 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003322 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003323 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003324
Joe Onorato9c1289c2009-08-17 11:03:03 -04003325 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003326 }
3327
Joe Onorato9c1289c2009-08-17 11:03:03 -04003328 /**
3329 * Implementation of the method from LauncherModel.Callbacks.
3330 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003331 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003332 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003333 sFolders.clear();
3334 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003335 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003336
3337 /**
3338 * Add the views for a widget to the workspace.
3339 *
3340 * Implementation of the method from LauncherModel.Callbacks.
3341 */
3342 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003343 setLoadOnResume();
3344
Daniel Sandler843e8602010-06-07 14:59:01 -04003345 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3346 if (DEBUG_WIDGETS) {
3347 Log.d(TAG, "bindAppWidget: " + item);
3348 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003349 final Workspace workspace = mWorkspace;
3350
3351 final int appWidgetId = item.appWidgetId;
3352 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003353 if (DEBUG_WIDGETS) {
3354 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3355 }
3356
Joe Onorato9c1289c2009-08-17 11:03:03 -04003357 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3358
Joe Onorato9c1289c2009-08-17 11:03:03 -04003359 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003360 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003361
Winson Chung3d503fb2011-07-13 17:25:49 -07003362 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003363 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003364 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3365
Joe Onorato9c1289c2009-08-17 11:03:03 -04003366 workspace.requestLayout();
3367
Daniel Sandler843e8602010-06-07 14:59:01 -04003368 if (DEBUG_WIDGETS) {
3369 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3370 + (SystemClock.uptimeMillis()-start) + "ms");
3371 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003372 }
3373
Adam Cohen1462de32012-07-24 22:34:36 -07003374 public void onPageBoundSynchronously(int page) {
3375 mSynchronouslyBoundPages.add(page);
3376 }
3377
Joe Onorato9c1289c2009-08-17 11:03:03 -04003378 /**
3379 * Callback saying that there aren't any more items to bind.
3380 *
3381 * Implementation of the method from LauncherModel.Callbacks.
3382 */
3383 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003384 setLoadOnResume();
3385
Joe Onorato9c1289c2009-08-17 11:03:03 -04003386 if (mSavedState != null) {
3387 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003388 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003389 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003390 mSavedState = null;
3391 }
3392
Adam Cohen1462de32012-07-24 22:34:36 -07003393 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003394
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003395 // If we received the result of any pending adds while the loader was running (e.g. the
3396 // widget configuration forced an orientation change), process them now.
3397 for (int i = 0; i < sPendingAddList.size(); i++) {
3398 completeAdd(sPendingAddList.get(i));
3399 }
3400 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003401
Winson Chungc51db6a2011-10-05 11:44:49 -07003402 // Update the market app icon as necessary (the other icons will be managed in response to
3403 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003404 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003405
Winson Chungf0c6ae02012-03-21 16:10:31 -07003406 // Animate up any icons as necessary
3407 if (mVisible || mWorkspaceLoading) {
3408 Runnable newAppsRunnable = new Runnable() {
3409 @Override
3410 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003411 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003412 }
3413 };
Winson Chunga2413752012-04-03 14:22:34 -07003414
3415 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3416 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3417 if (canRunNewAppsAnimation()) {
3418 // If the user has not interacted recently, then either snap to the new page to show
3419 // the new-apps animation or just run them if they are to appear on the current page
3420 if (willSnapPage) {
3421 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3422 } else {
3423 runNewAppsAnimation(false);
3424 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003425 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003426 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003427 // are on another page (or just normally if they are added to the current page)
3428 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003429 }
3430 }
3431
3432 mWorkspaceLoading = false;
3433 }
3434
Winson Chunga2413752012-04-03 14:22:34 -07003435 private boolean canRunNewAppsAnimation() {
3436 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3437 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3438 }
3439
Winson Chungf0c6ae02012-03-21 16:10:31 -07003440 /**
3441 * Runs a new animation that scales up icons that were added while Launcher was in the
3442 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003443 *
3444 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003445 */
Winson Chunga2413752012-04-03 14:22:34 -07003446 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003447 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003448 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003449
3450 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003451 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3452 @Override
3453 public int compare(View a, View b) {
3454 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3455 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3456 int cellCountX = LauncherModel.getCellCountX();
3457 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3458 }
3459 });
Winson Chunga2413752012-04-03 14:22:34 -07003460
3461 // Animate each of the views in place (or show them immediately if requested)
3462 if (immediate) {
3463 for (View v : mNewShortcutAnimateViews) {
3464 v.setAlpha(1f);
3465 v.setScaleX(1f);
3466 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003467 }
Winson Chunga2413752012-04-03 14:22:34 -07003468 } else {
3469 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3470 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003471 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003472 PropertyValuesHolder.ofFloat("alpha", 1f),
3473 PropertyValuesHolder.ofFloat("scaleX", 1f),
3474 PropertyValuesHolder.ofFloat("scaleY", 1f));
3475 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3476 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3477 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3478 bounceAnims.add(bounceAnim);
3479 }
3480 anim.playTogether(bounceAnims);
3481 anim.addListener(new AnimatorListenerAdapter() {
3482 @Override
3483 public void onAnimationEnd(Animator animation) {
3484 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3485 }
3486 });
3487 anim.start();
3488 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003489
3490 // Clean up
3491 mNewShortcutAnimatePage = -1;
3492 mNewShortcutAnimateViews.clear();
3493 new Thread("clearNewAppsThread") {
3494 public void run() {
3495 mSharedPrefs.edit()
3496 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3497 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3498 .commit();
3499 }
3500 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003501 }
3502
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003503 @Override
3504 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003505 boolean searchVisible = updateGlobalSearchIcon();
3506 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003507 if (mSearchDropTargetBar != null) {
3508 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3509 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003510 }
3511
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003512 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003513 * Add the icons for all apps.
3514 *
3515 * Implementation of the method from LauncherModel.Callbacks.
3516 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003517 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003518 Runnable setAllAppsRunnable = new Runnable() {
3519 public void run() {
3520 if (mAppsCustomizeContent != null) {
3521 mAppsCustomizeContent.setApps(apps);
3522 }
3523 }
3524 };
3525
Michael Jurkac57b7a82011-08-09 22:02:20 -07003526 // Remove the progress bar entirely; we could also make it GONE
3527 // but better to remove it since we know it's not going to be used
3528 View progressBar = mAppsCustomizeTabHost.
3529 findViewById(R.id.apps_customize_progress_bar);
3530 if (progressBar != null) {
3531 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003532
3533 // We just post the call to setApps so the user sees the progress bar
3534 // disappear-- otherwise, it just looks like the progress bar froze
3535 // which doesn't look great
3536 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3537 } else {
3538 // If we did not initialize the spinner in onCreate, then we can directly set the
3539 // list of applications without waiting for any progress bars views to be hidden.
3540 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003541 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003542 }
3543
3544 /**
3545 * A package was installed.
3546 *
3547 * Implementation of the method from LauncherModel.Callbacks.
3548 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003549 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003550 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003551
Winson Chung785d2eb2011-04-14 16:08:02 -07003552 if (mAppsCustomizeContent != null) {
3553 mAppsCustomizeContent.addApps(apps);
3554 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003555 }
3556
3557 /**
3558 * A package was updated.
3559 *
3560 * Implementation of the method from LauncherModel.Callbacks.
3561 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003562 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003563 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003564 if (mWorkspace != null) {
3565 mWorkspace.updateShortcuts(apps);
3566 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003567
Winson Chung785d2eb2011-04-14 16:08:02 -07003568 if (mAppsCustomizeContent != null) {
3569 mAppsCustomizeContent.updateApps(apps);
3570 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003571 }
3572
3573 /**
3574 * A package was uninstalled.
3575 *
3576 * Implementation of the method from LauncherModel.Callbacks.
3577 */
Winson Chungcd810732012-06-18 16:45:43 -07003578 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003579 if (permanent) {
Winson Chungcd810732012-06-18 16:45:43 -07003580 mWorkspace.removeItems(packageNames);
Joe Onorato36115782010-06-17 13:28:48 -04003581 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003582
Winson Chung785d2eb2011-04-14 16:08:02 -07003583 if (mAppsCustomizeContent != null) {
Winson Chungcd810732012-06-18 16:45:43 -07003584 mAppsCustomizeContent.removeApps(packageNames);
Winson Chung785d2eb2011-04-14 16:08:02 -07003585 }
Winson Chunga1820962011-10-03 16:31:06 -07003586
3587 // Notify the drag controller
Winson Chungcd810732012-06-18 16:45:43 -07003588 mDragController.onAppsRemoved(packageNames, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003589 }
Joe Onoratobe386092009-11-17 17:32:16 -08003590
3591 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003592 * A number of packages were updated.
3593 */
3594 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003595 if (mAppsCustomizeContent != null) {
3596 mAppsCustomizeContent.onPackagesUpdated();
3597 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003598 }
3599
Winson Chung400438b2011-01-16 17:53:48 -08003600 private int mapConfigurationOriActivityInfoOri(int configOri) {
3601 final Display d = getWindowManager().getDefaultDisplay();
3602 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3603 switch (d.getRotation()) {
3604 case Surface.ROTATION_0:
3605 case Surface.ROTATION_180:
3606 // We are currently in the same basic orientation as the natural orientation
3607 naturalOri = configOri;
3608 break;
3609 case Surface.ROTATION_90:
3610 case Surface.ROTATION_270:
3611 // We are currently in the other basic orientation to the natural orientation
3612 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3613 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3614 break;
3615 }
3616
3617 int[] oriMap = {
3618 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3619 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3620 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3621 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3622 };
3623 // Since the map starts at portrait, we need to offset if this device's natural orientation
3624 // is landscape.
3625 int indexOffset = 0;
3626 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3627 indexOffset = 1;
3628 }
3629 return oriMap[(d.getRotation() + indexOffset) % 4];
3630 }
Adam Cohen446e9402011-09-15 18:21:21 -07003631
Winson Chung4b919f82012-05-01 10:44:08 -07003632 public boolean isRotationEnabled() {
Michael Jurka9bc8eba2012-05-21 20:36:44 -07003633 boolean forceEnableRotation = doesFileExist(FORCE_ENABLE_ROTATION_PROPERTY);
Winson Chung4b919f82012-05-01 10:44:08 -07003634 boolean enableRotation = forceEnableRotation ||
3635 getResources().getBoolean(R.bool.allow_rotation);
3636 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003637 }
Winson Chung4b919f82012-05-01 10:44:08 -07003638 public void lockScreenOrientation() {
3639 if (isRotationEnabled()) {
3640 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3641 .getConfiguration().orientation));
3642 }
3643 }
3644 public void unlockScreenOrientation(boolean immediate) {
3645 if (isRotationEnabled()) {
3646 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003647 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003648 } else {
3649 mHandler.postDelayed(new Runnable() {
3650 public void run() {
3651 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3652 }
3653 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003654 }
Winson Chung4b919f82012-05-01 10:44:08 -07003655 }
Winson Chung400438b2011-01-16 17:53:48 -08003656 }
3657
Winson Chung82f55532011-08-09 14:14:23 -07003658 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003659 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003660 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003661 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003662
Winson Chung7d7541e2011-09-16 20:14:36 -07003663 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003664 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003665 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
3666 Cling cling = (Cling) findViewById(clingId);
3667 if (cling != null) {
3668 cling.init(this, positionData);
3669 cling.setVisibility(View.VISIBLE);
3670 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3671 if (animate) {
3672 cling.buildLayer();
3673 cling.setAlpha(0f);
3674 cling.animate()
3675 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003676 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003677 .setDuration(SHOW_CLING_DURATION)
3678 .setStartDelay(delay)
3679 .start();
3680 } else {
3681 cling.setAlpha(1f);
3682 }
3683 }
3684 return cling;
3685 }
3686 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung82f55532011-08-09 14:14:23 -07003687 if (cling != null) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003688 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003689 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003690 anim.addListener(new AnimatorListenerAdapter() {
3691 public void onAnimationEnd(Animator animation) {
3692 cling.setVisibility(View.GONE);
3693 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003694 // We should update the shared preferences on a background thread
3695 new Thread("dismissClingThread") {
3696 public void run() {
3697 SharedPreferences.Editor editor = mSharedPrefs.edit();
3698 editor.putBoolean(flag, true);
3699 editor.commit();
3700 }
3701 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003702 };
3703 });
3704 anim.start();
3705 }
3706 }
Winson Chung9d9d74f2011-09-19 11:49:12 -07003707 private void removeCling(int id) {
3708 final View cling = findViewById(id);
3709 if (cling != null) {
3710 final ViewGroup parent = (ViewGroup) cling.getParent();
3711 parent.post(new Runnable() {
3712 @Override
3713 public void run() {
3714 parent.removeView(cling);
3715 }
3716 });
3717 }
3718 }
Michael Jurka974c3862012-05-22 22:00:31 -07003719
3720 private boolean skipCustomClingIfNoAccounts() {
3721 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3722 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3723 if (customCling) {
3724 AccountManager am = AccountManager.get(this);
3725 Account[] accounts = am.getAccountsByType("com.google");
3726 return accounts.length == 0;
3727 }
3728 return false;
3729 }
3730
Winson Chung7d7541e2011-09-16 20:14:36 -07003731 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003732 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003733 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003734 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3735 !skipCustomClingIfNoAccounts() ) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003736 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003737 } else {
3738 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003739 }
3740 }
3741 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003742 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003743 if (isClingsEnabled() &&
3744 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003745 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003746 } else {
3747 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003748 }
3749 }
3750 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003751 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003752 if (isClingsEnabled() &&
3753 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3754 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003755 } else {
3756 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003757 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003758 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003759 }
3760 public boolean isFolderClingVisible() {
3761 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003762 if (cling != null) {
3763 return cling.getVisibility() == View.VISIBLE;
3764 }
3765 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003766 }
Winson Chung82f55532011-08-09 14:14:23 -07003767 public void dismissWorkspaceCling(View v) {
3768 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003769 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003770 }
3771 public void dismissAllAppsCling(View v) {
3772 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003773 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3774 }
3775 public void dismissFolderCling(View v) {
3776 Cling cling = (Cling) findViewById(R.id.folder_cling);
3777 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003778 }
3779
Winson Chung80baf5a2010-08-09 16:03:15 -07003780 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003781 * Prints out out state for debugging.
3782 */
3783 public void dumpState() {
3784 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003785 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003786 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3787 Log.d(TAG, "mRestoring=" + mRestoring);
3788 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3789 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003790 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003791 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003792
Winson Chung785d2eb2011-04-14 16:08:02 -07003793 if (mAppsCustomizeContent != null) {
3794 mAppsCustomizeContent.dumpState();
3795 }
Joe Onoratobe386092009-11-17 17:32:16 -08003796 Log.d(TAG, "END launcher2 dump state");
3797 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003798
3799 @Override
3800 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3801 super.dump(prefix, fd, writer, args);
3802 writer.println(" ");
3803 writer.println("Debug logs: ");
3804 for (int i = 0; i < sDumpLogs.size(); i++) {
3805 writer.println(" " + sDumpLogs.get(i));
3806 }
3807 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003808
3809 public static void dumpDebugLogsToConsole() {
3810 Log.d(TAG, "");
3811 Log.d(TAG, "*********************");
3812 Log.d(TAG, "Launcher debug logs: ");
3813 for (int i = 0; i < sDumpLogs.size(); i++) {
3814 Log.d(TAG, " " + sDumpLogs.get(i));
3815 }
3816 Log.d(TAG, "*********************");
3817 Log.d(TAG, "");
3818 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003819}
Michael Jurka2763be32011-02-24 11:19:57 -08003820
Michael Jurkaabded662011-03-04 12:06:57 -08003821interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003822 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003823 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003824 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003825 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003826 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003827}