blob: 64e382a7b2d36abc6206f26e91bb2b56908db109 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Joe Onoratoa5902522009-07-30 13:37:37 -070018package com.android.launcher2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Michael Jurka974c3862012-05-22 22:00:31 -070020import android.accounts.Account;
21import android.accounts.AccountManager;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080023import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070025import android.animation.ObjectAnimator;
26import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070027import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080028import android.animation.ValueAnimator.AnimatorUpdateListener;
Michael Jurka946ad472010-07-09 18:05:18 -070029import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070030import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070031import android.app.ActivityOptions;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070044import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070045import android.content.pm.ActivityInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080046import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070047import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070049import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080050import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070051import android.graphics.Bitmap;
52import android.graphics.Canvas;
53import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070054import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040056import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070057import android.os.AsyncTask;
Adam Cohen9e05a5e2012-09-10 15:53:09 -070058import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080059import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020060import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080061import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070062import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070063import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040064import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070065import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080066import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080067import android.text.Selection;
68import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070069import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070071import android.util.Log;
Winson Chung400438b2011-01-16 17:53:48 -080072import android.view.Display;
Joe Onorato0d44e942009-11-16 18:20:51 -080073import android.view.HapticFeedbackConstants;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080074import android.view.KeyEvent;
75import android.view.LayoutInflater;
76import android.view.Menu;
77import android.view.MenuItem;
Michael Jurka0e260592010-06-30 17:07:39 -070078import android.view.MotionEvent;
Winson Chung400438b2011-01-16 17:53:48 -080079import android.view.Surface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080080import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080081import android.view.View.OnLongClickListener;
Winson Chung82f55532011-08-09 14:14:23 -070082import android.view.ViewGroup;
Michael Jurkab737ee62011-11-15 15:57:22 -080083import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080084import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Dianne Hackbornbb003a52011-08-30 14:40:07 -070085import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080086import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070087import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070088import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080089import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080090import android.view.inputmethod.InputMethodManager;
Adam Cohended9f8d2010-11-03 13:25:16 -070091import android.widget.Advanceable;
Michael Jurkaaf442092010-06-10 17:01:57 -070092import android.widget.ImageView;
Winson Chung68846fd2010-10-29 11:00:27 -070093import android.widget.TextView;
94import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070095
Adam Cohendf2cc412011-04-27 16:56:57 -070096import com.android.common.Search;
97import com.android.launcher.R;
Adam Cohen558baaf2011-08-15 15:22:57 -070098import com.android.launcher2.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -080099
Adam Cohenc0dcf592011-06-01 15:30:43 -0700100import java.io.DataInputStream;
101import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700102import java.io.FileDescriptor;
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700103import java.io.FileInputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700104import java.io.FileNotFoundException;
105import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700106import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700107import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700108import java.util.Collection;
109import java.util.Collections;
110import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700111import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700112import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700113import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700114import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800116/**
117 * Default launcher application.
118 */
Michael Jurka946ad472010-07-09 18:05:18 -0700119public final class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700120 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700121 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800122 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700123 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800124
Joe Onorato9c1289c2009-08-17 11:03:03 -0400125 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400126 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700127 static final boolean DEBUG_STRICT_MODE = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700128
Winson Chung70d72102011-08-12 11:24:35 -0700129 private static final int MENU_GROUP_WALLPAPER = 1;
130 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
131 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700132 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
133 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134
135 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700136 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137 private static final int REQUEST_PICK_APPLICATION = 6;
138 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700139 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700140 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Michael Jurka8b805b12012-04-18 14:23:14 -0700142 private static final int REQUEST_BIND_APPWIDGET = 11;
143
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800144 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
145
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800146 static final int SCREEN_COUNT = 5;
147 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148
Romain Guy98d01652009-06-30 16:21:04 -0700149 private static final String PREFERENCES = "launcher.preferences";
Michael Jurkaf3bade62012-06-12 11:00:21 -0700150 static final String FORCE_ENABLE_ROTATION_PROPERTY = "debug.force_enable_rotation";
151 static final String DUMP_STATE_PROPERTY = "debug.dumpstate";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Winson Chung2672ff92012-05-04 16:22:30 -0700153 // The Intent extra that defines whether to ignore the launch animation
154 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
155 "com.android.launcher.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
156
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157 // Type: int
158 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700159 // Type: int
160 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800161 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700162 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
163 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
165 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700166 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700168 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: boolean
170 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
171 // Type: long
172 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700173 // Type: int
174 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
175 // Type: int
176 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
177 // Type: parcelable
178 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700180 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700181 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
182 "com.android.launcher.toolbar_search_icon";
183 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
184 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700185
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700186 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700187 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700188 private State mState = State.WORKSPACE;
189 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800190 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700191
Joe Onorato9c1289c2009-08-17 11:03:03 -0400192 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700193 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
194 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700195 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700196 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800197
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800198 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800199 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Winson Chunga2413752012-04-03 14:22:34 -0700201 // How long to wait before the new-shortcut animation automatically pans the workspace
202 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
203
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800204 private final BroadcastReceiver mCloseSystemDialogsReceiver
205 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800206 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
207
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208 private LayoutInflater mInflater;
209
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800211 private View mQsbDivider;
212 private View mDockDivider;
213 private DragLayer mDragLayer;
214 private DragController mDragController;
Romain Guycbb89e42009-06-08 15:52:54 -0700215
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700216 private AppWidgetManager mAppWidgetManager;
217 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700218
Michael Jurkac9d95c52011-08-29 14:03:34 -0700219 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700220 private AppWidgetProviderInfo mPendingAddWidgetInfo;
221
Michael Jurka0280c3b2010-09-17 15:00:07 -0700222 private int[] mTmpAddItemCellCoordinates = new int[2];
223
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800224 private FolderInfo mFolderInfo;
225
Winson Chung3d503fb2011-07-13 17:25:49 -0700226 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800227 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700228
Winson Chungc51db6a2011-10-05 11:44:49 -0700229 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700230 private AppsCustomizeTabHost mAppsCustomizeTabHost;
231 private AppsCustomizePagedView mAppsCustomizeContent;
232 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700235 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
236 // scroll issues (because the workspace may not have been measured yet) and extra work.
237 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
238 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239
240 private SpannableStringBuilder mDefaultKeySsb = null;
241
Joe Onorato9c1289c2009-08-17 11:03:03 -0400242 private boolean mWorkspaceLoading = true;
243
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800244 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 private boolean mRestoring;
246 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700247 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800248
Winson Chung4a2afa32012-07-19 14:53:05 -0700249 // Keep track of whether the user has left launcher
250 private static boolean sPausedFromUserAction = false;
251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 private Bundle mSavedInstanceState;
253
Joe Onorato9c1289c2009-08-17 11:03:03 -0400254 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800255 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800256 private boolean mUserPresent = true;
257 private boolean mVisible = false;
258 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400259
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700260 private static LocaleConfiguration sLocaleConfiguration = null;
261
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700262 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700263
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700264 private Intent mAppMarketIntent = null;
265
Adam Cohended9f8d2010-11-03 13:25:16 -0700266 // Related to the auto-advancing of widgets
267 private final int ADVANCE_MSG = 1;
268 private final int mAdvanceInterval = 20000;
269 private final int mAdvanceStagger = 250;
270 private long mAutoAdvanceSentTime;
271 private long mAutoAdvanceTimeLeft = -1;
272 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
273 new HashMap<View, AppWidgetProviderInfo>();
274
Winson Chung400438b2011-01-16 17:53:48 -0800275 // Determines how long to wait after a rotation before restoring the screen orientation to
276 // match the sensor state.
277 private final int mRestoreScreenOrientationDelay = 500;
278
Michael Jurka4ef207b2010-11-29 17:05:45 -0800279 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700280 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
281 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
282 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800283
Adam Cohen1462de32012-07-24 22:34:36 -0700284 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
285
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700286 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
287
Winson Chung46353de2012-02-16 14:05:10 -0800288 // We only want to get the SharedPreferences once since it does an FS stat each time we get
289 // it from the context.
290 private SharedPreferences mSharedPrefs;
291
Winson Chungf0c6ae02012-03-21 16:10:31 -0700292 // Holds the page that we need to animate to, and the icon views that we need to animate up
293 // when we scroll to that page on resume.
294 private int mNewShortcutAnimatePage = -1;
295 private ArrayList<View> mNewShortcutAnimateViews = new ArrayList<View>();
Adam Cohen268c4752012-06-06 17:47:33 -0700296 private ImageView mFolderIconImageView;
297 private Bitmap mFolderIconBitmap;
298 private Canvas mFolderIconCanvas;
299 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700300
Michael Jurkaddd62e92011-02-16 17:49:14 -0800301 private BubbleTextView mWaitingForResume;
302
Michael Jurka22143132012-10-04 17:42:38 +0200303 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
304 = new HideFromAccessibilityHelper();
305
Michael Jurkac1f5d262011-09-30 19:32:27 -0700306 private Runnable mBuildLayersRunnable = new Runnable() {
307 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700308 if (mWorkspace != null) {
309 mWorkspace.buildPageHardwareLayers();
310 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700311 }
312 };
313
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800314 private static ArrayList<PendingAddArguments> sPendingAddList
315 = new ArrayList<PendingAddArguments>();
316
317 private static class PendingAddArguments {
318 int requestCode;
319 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700320 long container;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800321 int screen;
322 int cellX;
323 int cellY;
324 }
325
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700326
327 private boolean doesFileExist(String filename) {
328 FileInputStream fis = null;
329 try {
330 fis = openFileInput(filename);
331 fis.close();
332 return true;
333 } catch (java.io.FileNotFoundException e) {
334 return false;
335 } catch (java.io.IOException e) {
336 return true;
337 }
338 }
339
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800340 @Override
341 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700342 if (DEBUG_STRICT_MODE) {
343 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
344 .detectDiskReads()
345 .detectDiskWrites()
346 .detectNetwork() // or .detectAll() for all detectable problems
347 .penaltyLog()
348 .build());
349 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
350 .detectLeakedSqlLiteObjects()
351 .detectLeakedClosableObjects()
352 .penaltyLog()
353 .penaltyDeath()
354 .build());
355 }
356
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800357 super.onCreate(savedInstanceState);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800358 LauncherApplication app = ((LauncherApplication)getApplication());
Winson Chungf0c6ae02012-03-21 16:10:31 -0700359 mSharedPrefs = getSharedPreferences(LauncherApplication.getSharedPreferencesKey(),
360 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800361 mModel = app.setLauncher(this);
362 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400363 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800364 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700365
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700366 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700367 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
368 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700369
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700370 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
371 // this also ensures that any synchronous binding below doesn't re-trigger another
372 // LauncherModel load.
373 mPaused = false;
374
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800375 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200376 android.os.Debug.startMethodTracing(
377 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 }
379
380 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800381 setContentView(R.layout.launcher);
382 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700383 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800384
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800385 registerContentObservers();
386
Joe Onorato7c312c12009-08-13 21:36:53 -0700387 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 mSavedState = savedInstanceState;
390 restoreState(mSavedState);
391
Winson Chunga12a2502010-12-20 14:41:35 -0800392 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700393 if (mAppsCustomizeContent != null) {
394 mAppsCustomizeContent.onPackagesUpdated();
395 }
Winson Chunga12a2502010-12-20 14:41:35 -0800396
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 if (PROFILE_STARTUP) {
398 android.os.Debug.stopMethodTracing();
399 }
400
401 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700402 if (sPausedFromUserAction) {
403 // If the user leaves launcher, then we should just load items asynchronously when
404 // they return.
405 mModel.startLoader(true, -1);
406 } else {
407 // We only load the page synchronously if the user rotates (or triggers a
408 // configuration change) while launcher is in the foreground
409 mModel.startLoader(true, mWorkspace.getCurrentPage());
410 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800411 }
412
Michael Jurkac57b7a82011-08-09 22:02:20 -0700413 if (!mModel.isAllAppsLoaded()) {
414 ViewGroup appsCustomizeContentParent = (ViewGroup) mAppsCustomizeContent.getParent();
415 mInflater.inflate(R.layout.apps_customize_progressbar, appsCustomizeContentParent);
416 }
417
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800418 // For handling default keys
419 mDefaultKeySsb = new SpannableStringBuilder();
420 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800421
422 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
423 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800424
Adam Cohenf9426d52012-06-04 17:26:21 -0700425 updateGlobalIcons();
426
427 // On large interfaces, we want the screen to auto-rotate based on the current orientation
428 unlockScreenOrientation(true);
429 }
430
Winson Chung4a2afa32012-07-19 14:53:05 -0700431 protected void onUserLeaveHint() {
432 super.onUserLeaveHint();
433 sPausedFromUserAction = true;
434 }
435
Adam Cohenf9426d52012-06-04 17:26:21 -0700436 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700437 boolean searchVisible = false;
438 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800439 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700440 int coi = getCurrentOrientationIndexForGlobalIcons();
441 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
442 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700443 updateAppMarketIcon();
444 searchVisible = updateGlobalSearchIcon();
445 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700446 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700447 if (sGlobalSearchIcon[coi] != null) {
448 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700449 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700450 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700451 if (sVoiceSearchIcon[coi] != null) {
452 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700453 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700454 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700455 if (sAppMarketIcon[coi] != null) {
456 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800457 }
Winson Chungadf0c182012-08-23 14:59:07 -0700458 if (mSearchDropTargetBar != null) {
459 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
460 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 }
Romain Guycbb89e42009-06-08 15:52:54 -0700462
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700464 if (sLocaleConfiguration == null) {
465 new AsyncTask<Void, Void, LocaleConfiguration>() {
466 @Override
467 protected LocaleConfiguration doInBackground(Void... unused) {
468 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
469 readConfiguration(Launcher.this, localeConfiguration);
470 return localeConfiguration;
471 }
472
473 @Override
474 protected void onPostExecute(LocaleConfiguration result) {
475 sLocaleConfiguration = result;
476 checkForLocaleChange(); // recursive, but now with a locale configuration
477 }
478 }.execute();
479 return;
480 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700481
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 final Configuration configuration = getResources().getConfiguration();
483
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700484 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800485 final String locale = configuration.locale.toString();
486
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700487 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488 final int mcc = configuration.mcc;
489
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700490 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800491 final int mnc = configuration.mnc;
492
Romain Guy84f296c2009-11-04 15:00:44 -0800493 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494
Romain Guy84f296c2009-11-04 15:00:44 -0800495 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700496 sLocaleConfiguration.locale = locale;
497 sLocaleConfiguration.mcc = mcc;
498 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700499
Joe Onorato0589f0f2010-02-08 13:44:00 -0800500 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700501
502 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
503 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700504 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700505 public void run() {
506 writeConfiguration(Launcher.this, localeConfiguration);
507 }
508 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700509 }
510 }
511
512 private static class LocaleConfiguration {
513 public String locale;
514 public int mcc = -1;
515 public int mnc = -1;
516 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700517
Romain Guy98d01652009-06-30 16:21:04 -0700518 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
519 DataInputStream in = null;
520 try {
521 in = new DataInputStream(context.openFileInput(PREFERENCES));
522 configuration.locale = in.readUTF();
523 configuration.mcc = in.readInt();
524 configuration.mnc = in.readInt();
525 } catch (FileNotFoundException e) {
526 // Ignore
527 } catch (IOException e) {
528 // Ignore
529 } finally {
530 if (in != null) {
531 try {
532 in.close();
533 } catch (IOException e) {
534 // Ignore
535 }
536 }
537 }
538 }
539
540 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
541 DataOutputStream out = null;
542 try {
543 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
544 out.writeUTF(configuration.locale);
545 out.writeInt(configuration.mcc);
546 out.writeInt(configuration.mnc);
547 out.flush();
548 } catch (FileNotFoundException e) {
549 // Ignore
550 } catch (IOException e) {
551 //noinspection ResultOfMethodCallIgnored
552 context.getFileStreamPath(PREFERENCES).delete();
553 } finally {
554 if (out != null) {
555 try {
556 out.close();
557 } catch (IOException e) {
558 // Ignore
559 }
560 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800561 }
562 }
563
Adam Cohen716b51e2011-06-30 12:09:54 -0700564 public DragLayer getDragLayer() {
565 return mDragLayer;
566 }
567
Winson Chung36a62fe2012-05-06 18:04:42 -0700568 boolean isDraggingEnabled() {
569 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
570 // that is subsequently removed from the workspace in startBinding().
571 return !mModel.isLoadingWorkspace();
572 }
573
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 static int getScreen() {
575 synchronized (sLock) {
576 return sScreen;
577 }
578 }
579
580 static void setScreen(int screen) {
581 synchronized (sLock) {
582 sScreen = screen;
583 }
584 }
585
Winson Chung557d6ed2011-07-08 15:34:52 -0700586 /**
587 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
588 * a configuration step, this allows the proper animations to run after other transitions.
589 */
590 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700591 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800592 switch (args.requestCode) {
593 case REQUEST_PICK_APPLICATION:
Winson Chung3d503fb2011-07-13 17:25:49 -0700594 completeAddApplication(args.intent, args.container, args.screen, args.cellX,
595 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800596 break;
597 case REQUEST_PICK_SHORTCUT:
598 processShortcut(args.intent);
599 break;
600 case REQUEST_CREATE_SHORTCUT:
Winson Chung3d503fb2011-07-13 17:25:49 -0700601 completeAddShortcut(args.intent, args.container, args.screen, args.cellX,
602 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700603 result = true;
604 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800605 case REQUEST_CREATE_APPWIDGET:
606 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohened66b2b2012-01-23 17:28:51 -0800607 completeAddAppWidget(appWidgetId, args.container, args.screen, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700608 result = true;
609 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800610 case REQUEST_PICK_WALLPAPER:
611 // We just wanted the activity result here so we can clear mWaitingForResult
612 break;
613 }
Michael Jurka27614d22012-04-02 06:40:22 -0700614 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
615 // if you turned the screen off and then back while in All Apps, Launcher would not
616 // return to the workspace. Clearing mAddInfo.container here fixes this issue
617 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700618 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800619 }
620
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700622 protected void onActivityResult(
623 final int requestCode, final int resultCode, final Intent data) {
624 if (requestCode == REQUEST_BIND_APPWIDGET) {
625 int appWidgetId = data != null ?
626 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
627 if (resultCode == RESULT_CANCELED) {
628 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
629 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700630 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700631 }
632 return;
633 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700634 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800635 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
636 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700637 mWaitingForResult = false;
638
Adam Cohened66b2b2012-01-23 17:28:51 -0800639 // We have special handling for widgets
640 if (isWidgetDrop) {
641 int appWidgetId = data != null ?
642 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700643 if (appWidgetId < 0) {
644 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
645 "widget configuration activity.");
646 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
647 } else {
648 completeTwoStageWidgetDrop(resultCode, appWidgetId);
649 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800650 return;
651 }
652
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800653 // The pattern used here is that a user PICKs a specific application,
654 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700655
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800656 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
657 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700658 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800659 final PendingAddArguments args = new PendingAddArguments();
660 args.requestCode = requestCode;
661 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700662 args.container = mPendingAddInfo.container;
663 args.screen = mPendingAddInfo.screen;
664 args.cellX = mPendingAddInfo.cellX;
665 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800666 if (isWorkspaceLocked()) {
667 sPendingAddList.add(args);
668 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700669 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800671 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800672 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700673 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800674 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
675 null);
676 }
677
678 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700679 CellLayout cellLayout =
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700680 (CellLayout) mWorkspace.getChildAt(mPendingAddInfo.screen);
Adam Cohened66b2b2012-01-23 17:28:51 -0800681 Runnable onCompleteRunnable = null;
682 int animationType = 0;
683
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700684 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800685 if (resultCode == RESULT_OK) {
686 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
687 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700688 mPendingAddWidgetInfo);
689 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800690 onCompleteRunnable = new Runnable() {
691 @Override
692 public void run() {
693 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
694 mPendingAddInfo.screen, layout, null);
695 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
696 null);
697 }
698 };
699 } else if (resultCode == RESULT_CANCELED) {
700 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
701 onCompleteRunnable = new Runnable() {
702 @Override
703 public void run() {
704 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
705 null);
706 }
707 };
708 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700709 if (mDragLayer.getAnimatedView() != null) {
710 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
711 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
712 animationType, boundWidget, true);
713 } else {
714 // The animated view may be null in the case of a rotation during widget configuration
715 onCompleteRunnable.run();
716 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800717 }
718
719 @Override
720 protected void onResume() {
721 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700722
Winson Chung4a2afa32012-07-19 14:53:05 -0700723 // Restore the previous launcher state
724 if (mOnResumeState == State.WORKSPACE) {
725 showWorkspace(false);
726 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
727 showAllApps(false);
728 }
729 mOnResumeState = State.NONE;
730
Winson Chungde0fb8f2012-05-08 14:37:08 -0700731 // Process any items that were added while Launcher was away
732 InstallShortcutReceiver.flushInstallQueue(this);
733
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800734 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700735 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700736 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400737 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700738 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400739 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700740 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800741 }
Winson Chunge4e50662012-01-23 14:45:13 -0800742
743 // Reset the pressed state of icons that were locked in the press state while activities
744 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800745 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800746 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800747 mWaitingForResume.setStayPressed(false);
748 }
Winson Chunge4e50662012-01-23 14:45:13 -0800749 if (mAppsCustomizeContent != null) {
750 // Resets the previous all apps icon press state
751 mAppsCustomizeContent.resetDrawableState();
752 }
Adam Cohen06dff352012-06-01 17:17:08 -0700753 // It is possible that widgets can receive updates while launcher is not in the foreground.
754 // Consequently, the widgets will be inflated in the orientation of the foreground activity
755 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
756 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700757 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700758
759 // Again, as with the above scenario, it's possible that one or more of the global icons
760 // were updated in the wrong orientation.
761 updateGlobalIcons();
Adam Cohen06dff352012-06-01 17:17:08 -0700762 }
763
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800764 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700765 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700766 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
767 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
768 // when Launcher resumes and we are still in AllApps.
769 updateWallpaperVisibility(true);
770
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700771 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700772 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800773 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700774 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700775 }
Romain Guycbb89e42009-06-08 15:52:54 -0700776
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700777 @Override
778 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400779 // Flag the loader to stop early before switching
780 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700781 if (mAppsCustomizeContent != null) {
782 mAppsCustomizeContent.surrender();
783 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800784 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700785 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700786
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800787 // We can't hide the IME if it was forced open. So don't bother
788 /*
789 @Override
790 public void onWindowFocusChanged(boolean hasFocus) {
791 super.onWindowFocusChanged(hasFocus);
792
793 if (hasFocus) {
794 final InputMethodManager inputManager = (InputMethodManager)
795 getSystemService(Context.INPUT_METHOD_SERVICE);
796 WindowManager.LayoutParams lp = getWindow().getAttributes();
797 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
798 android.os.Handler()) {
799 protected void onReceiveResult(int resultCode, Bundle resultData) {
800 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
801 }
802 });
803 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
804 }
805 }
806 */
807
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800808 private boolean acceptFilter() {
809 final InputMethodManager inputManager = (InputMethodManager)
810 getSystemService(Context.INPUT_METHOD_SERVICE);
811 return !inputManager.isFullscreenMode();
812 }
813
814 @Override
815 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700816 final int uniChar = event.getUnicodeChar();
817 final boolean handled = super.onKeyDown(keyCode, event);
818 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
819 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800820 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
821 keyCode, event);
822 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700823 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700824 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700825 // showSearchDialog()
826 // If there are multiple keystrokes before the search dialog takes focus,
827 // onSearchRequested() will be called for every keystroke,
828 // but it is idempotent, so it's fine.
829 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800830 }
831 }
832
Joe Onorato8a9625e2010-01-28 15:55:35 -0800833 // Eat the long press event so the keyboard doesn't come up.
834 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
835 return true;
836 }
837
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800838 return handled;
839 }
840
Karl Rosaen138a0412009-04-23 19:00:21 -0700841 private String getTypedText() {
842 return mDefaultKeySsb.toString();
843 }
844
845 private void clearTypedText() {
846 mDefaultKeySsb.clear();
847 mDefaultKeySsb.clearSpans();
848 Selection.setSelection(mDefaultKeySsb, 0);
849 }
850
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800851 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700852 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
853 * State
854 */
855 private static State intToState(int stateOrdinal) {
856 State state = State.WORKSPACE;
857 final State[] stateValues = State.values();
858 for (int i = 0; i < stateValues.length; i++) {
859 if (stateValues[i].ordinal() == stateOrdinal) {
860 state = stateValues[i];
861 break;
862 }
863 }
864 return state;
865 }
866
867 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 * Restores the previous state, if it exists.
869 *
870 * @param savedState The previous state.
871 */
872 private void restoreState(Bundle savedState) {
873 if (savedState == null) {
874 return;
875 }
876
Michael Jurka883f55b2010-10-21 15:47:14 -0700877 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -0700878 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700879 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -0800880 }
881
Winson Chungf0c6ae02012-03-21 16:10:31 -0700882 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800883 if (currentScreen > -1) {
Michael Jurka0142d492010-08-25 17:46:15 -0700884 mWorkspace.setCurrentPage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800885 }
886
Winson Chung3d503fb2011-07-13 17:25:49 -0700887 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
888 final int pendingAddScreen = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -0700889
Winson Chung3d503fb2011-07-13 17:25:49 -0700890 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
891 mPendingAddInfo.container = pendingAddContainer;
892 mPendingAddInfo.screen = pendingAddScreen;
893 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
894 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700895 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
896 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
897 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -0700898 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800899 mRestoring = true;
900 }
901
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700902
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800903 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
904 if (renameFolder) {
905 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700906 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800907 mRestoring = true;
908 }
Winson Chunga12a2502010-12-20 14:41:35 -0800909
Winson Chung785d2eb2011-04-14 16:08:02 -0700910
911 // Restore the AppsCustomize tab
912 if (mAppsCustomizeTabHost != null) {
913 String curTab = savedState.getString("apps_customize_currentTab");
914 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -0700915 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -0700916 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -0700917 mAppsCustomizeContent.loadAssociatedPages(
918 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -0700919 }
920
Winson Chung5afbf7b2011-07-25 11:53:08 -0700921 int currentIndex = savedState.getInt("apps_customize_currentIndex");
922 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -0700923 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800924 }
925
926 /**
927 * Finds all the views we need and configure them properly.
928 */
929 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -0700930 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -0400931
Winson Chung4c98d922011-05-31 16:50:48 -0700932 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
933 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Michael Jurkab737ee62011-11-15 15:57:22 -0800934 mQsbDivider = (ImageView) findViewById(R.id.qsb_divider);
935 mDockDivider = (ImageView) findViewById(R.id.dock_divider);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800936
Winson Chung4c98d922011-05-31 16:50:48 -0700937 // Setup the drag layer
938 mDragLayer.setup(this, dragController);
939
Winson Chung3d503fb2011-07-13 17:25:49 -0700940 // Setup the hotseat
941 mHotseat = (Hotseat) findViewById(R.id.hotseat);
942 if (mHotseat != null) {
943 mHotseat.setup(this);
944 }
945
Winson Chung4c98d922011-05-31 16:50:48 -0700946 // Setup the workspace
947 mWorkspace.setHapticFeedbackEnabled(false);
948 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -0700949 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -0700950 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -0700951
Winson Chungf0ea4d32011-06-06 14:27:16 -0700952 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -0700953 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -0700954
Winson Chungf0ea4d32011-06-06 14:27:16 -0700955 // Setup AppsCustomize
956 mAppsCustomizeTabHost = (AppsCustomizeTabHost)
957 findViewById(R.id.apps_customize_pane);
958 mAppsCustomizeContent = (AppsCustomizePagedView)
959 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
960 mAppsCustomizeContent.setup(this, dragController);
Winson Chung0e721a42012-08-02 17:40:30 -0700961
Winson Chung3d503fb2011-07-13 17:25:49 -0700962 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -0700963 dragController.setDragScoller(mWorkspace);
964 dragController.setScrollView(mDragLayer);
965 dragController.setMoveTarget(mWorkspace);
966 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -0700967 if (mSearchDropTargetBar != null) {
968 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -0800969 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800970 }
971
972 /**
973 * Creates a view representing a shortcut.
974 *
975 * @param info The data structure describing the shortcut.
976 *
977 * @return A View inflated from R.layout.application.
978 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800979 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -0700981 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 }
983
984 /**
985 * Creates a view representing a shortcut inflated from the specified resource.
986 *
987 * @param layoutResId The id of the XML layout used to create the shortcut.
988 * @param parent The group the shortcut belongs to.
989 * @param info The data structure describing the shortcut.
990 *
991 * @return A View inflated from layoutResId.
992 */
Joe Onorato0589f0f2010-02-08 13:44:00 -0800993 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -0800994 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
995 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800996 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 return favorite;
998 }
999
1000 /**
1001 * Add an application shortcut to the workspace.
1002 *
1003 * @param data The intent describing the application.
1004 * @param cellInfo The position on screen where to create the shortcut.
1005 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001006 void completeAddApplication(Intent data, long container, int screen, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001007 final int[] cellXY = mTmpAddItemCellCoordinates;
Winson Chung3d503fb2011-07-13 17:25:49 -07001008 final CellLayout layout = getCellLayout(container, screen);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001009
Adam Cohenfbba09b2011-07-18 21:43:05 -07001010 // First we check if we already know the exact location where we want to add this item.
1011 if (cellX >= 0 && cellY >= 0) {
1012 cellXY[0] = cellX;
1013 cellXY[1] = cellY;
1014 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001015 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001016 return;
1017 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001018
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001019 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001020
Romain Guy73b979d2009-06-09 12:57:21 -07001021 if (info != null) {
Joe Onorato0589f0f2010-02-08 13:44:00 -08001022 info.setActivity(data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001024 info.container = ItemInfo.NO_ID;
Winson Chung3d503fb2011-07-13 17:25:49 -07001025 mWorkspace.addApplicationShortcut(info, layout, container, screen, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001026 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001027 } else {
1028 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 }
1030 }
Romain Guycbb89e42009-06-08 15:52:54 -07001031
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 /**
1033 * Add a shortcut to the workspace.
1034 *
1035 * @param data The intent describing the shortcut.
1036 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001037 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001038 private void completeAddShortcut(Intent data, long container, int screen, int cellX,
1039 int cellY) {
1040 int[] cellXY = mTmpAddItemCellCoordinates;
1041 int[] touchXY = mPendingAddInfo.dropPos;
1042 CellLayout layout = getCellLayout(container, screen);
Romain Guycbb89e42009-06-08 15:52:54 -07001043
Michael Jurkad3ef3062010-11-23 16:23:58 -08001044 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001045
Adam Cohen558baaf2011-08-15 15:22:57 -07001046 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001047 if (info == null) {
1048 return;
1049 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001050 final View view = createShortcut(info);
1051
Adam Cohenfbba09b2011-07-18 21:43:05 -07001052 // First we check if we already know the exact location where we want to add this item.
1053 if (cellX >= 0 && cellY >= 0) {
1054 cellXY[0] = cellX;
1055 cellXY[1] = cellY;
1056 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001057
1058 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001059 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001060 true, null,null)) {
1061 return;
1062 }
1063 DragObject dragObject = new DragObject();
1064 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001065 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1066 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001067 return;
1068 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001069 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001070 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001071 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001072 foundCellSpan = (result != null);
1073 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001074 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001075 }
1076
1077 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001078 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001079 return;
1080 }
1081
Adam Cohen558baaf2011-08-15 15:22:57 -07001082 LauncherModel.addItemToDatabase(this, info, container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001083
1084 if (!mRestoring) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001085 mWorkspace.addInScreen(view, container, screen, cellXY[0], cellXY[1], 1, 1,
1086 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 }
1088 }
1089
Adam Cohen2f093b62012-04-30 18:59:53 -07001090 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1091 int minHeight) {
1092 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001093 // We want to account for the extra amount of padding that we are adding to the widget
1094 // to ensure that it gets the full amount of space that it has requested
1095 int requiredWidth = minWidth + padding.left + padding.right;
1096 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001097 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001098 }
1099
Adam Cohen2f093b62012-04-30 18:59:53 -07001100 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1101 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001102 }
1103
Adam Cohen2f093b62012-04-30 18:59:53 -07001104 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1105 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001106 }
1107
Adam Cohen2f093b62012-04-30 18:59:53 -07001108 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1109 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001110 }
1111
Adam Cohen2f093b62012-04-30 18:59:53 -07001112 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1113 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001114 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001115 }
1116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001117 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001118 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001119 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001120 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001121 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001122 */
Adam Cohened66b2b2012-01-23 17:28:51 -08001123 private void completeAddAppWidget(final int appWidgetId, long container, int screen,
1124 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1125 if (appWidgetInfo == null) {
1126 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1127 }
Romain Guycbb89e42009-06-08 15:52:54 -07001128
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001129 // Calculate the grid spans needed to fit this widget
Winson Chung3d503fb2011-07-13 17:25:49 -07001130 CellLayout layout = getCellLayout(container, screen);
Adam Cohen09353862011-07-14 16:10:03 -07001131
Adam Cohen2f093b62012-04-30 18:59:53 -07001132 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1133 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001134
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001135 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001136 // We have saved the position to which the widget was dragged-- this really only matters
1137 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001138 int[] cellXY = mTmpAddItemCellCoordinates;
1139 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001140 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001141 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001142 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1143 cellXY[0] = mPendingAddInfo.cellX;
1144 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001145 spanXY[0] = mPendingAddInfo.spanX;
1146 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001147 foundCellSpan = true;
1148 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001149 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001150 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001151 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1152 spanXY[1], cellXY, finalSpan);
1153 spanXY[0] = finalSpan[0];
1154 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001155 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001156 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001157 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001158 }
1159
Michael Jurka0280c3b2010-09-17 15:00:07 -07001160 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001161 if (appWidgetId != -1) {
1162 // Deleting an app widget ID is a void call but writes to disk before returning
1163 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001164 new Thread("deleteAppWidgetId") {
1165 public void run() {
1166 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1167 }
1168 }.start();
1169 }
Winson Chung93eef082012-03-23 15:59:27 -07001170 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001171 return;
1172 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001174 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001175 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1176 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001177 launcherInfo.spanX = spanXY[0];
1178 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001179 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1180 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001181
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001182 LauncherModel.addItemToDatabase(this, launcherInfo,
Winson Chung3d503fb2011-07-13 17:25:49 -07001183 container, screen, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001184
1185 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001186 if (hostView == null) {
1187 // Perform actual inflation because we're live
1188 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1189 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1190 } else {
1191 // The AppWidgetHostView has already been inflated and instantiated
1192 launcherInfo.hostView = hostView;
1193 }
Romain Guycbb89e42009-06-08 15:52:54 -07001194
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001195 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001196 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohen9e05a5e2012-09-10 15:53:09 -07001197 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
1198 launcherInfo.notifyWidgetSizeChanged(this);
1199 }
Winson Chung3d503fb2011-07-13 17:25:49 -07001200 mWorkspace.addInScreen(launcherInfo.hostView, container, screen, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001201 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001202
1203 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001204 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001205 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 }
Romain Guycbb89e42009-06-08 15:52:54 -07001207
Adam Cohended9f8d2010-11-03 13:25:16 -07001208 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1209 @Override
1210 public void onReceive(Context context, Intent intent) {
1211 final String action = intent.getAction();
1212 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1213 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001214 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001215 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001216
Winson Chungc100e8e2011-08-09 16:02:43 -07001217 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001218 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001219 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1220 mAppsCustomizeTabHost.reset();
1221 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001222 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001223 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1224 mUserPresent = true;
1225 updateRunning();
1226 }
1227 }
1228 };
1229
1230 @Override
1231 public void onAttachedToWindow() {
1232 super.onAttachedToWindow();
1233
1234 // Listen for broadcasts related to user-presence
1235 final IntentFilter filter = new IntentFilter();
1236 filter.addAction(Intent.ACTION_SCREEN_OFF);
1237 filter.addAction(Intent.ACTION_USER_PRESENT);
1238 registerReceiver(mReceiver, filter);
1239
Adam Cohend113e0c2010-11-11 10:48:05 -08001240 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001241 mVisible = true;
1242 }
1243
1244 @Override
1245 public void onDetachedFromWindow() {
1246 super.onDetachedFromWindow();
1247 mVisible = false;
1248
Adam Cohend113e0c2010-11-11 10:48:05 -08001249 if (mAttached) {
1250 unregisterReceiver(mReceiver);
1251 mAttached = false;
1252 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001253 updateRunning();
1254 }
1255
1256 public void onWindowVisibilityChanged(int visibility) {
1257 mVisible = visibility == View.VISIBLE;
1258 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001259 // The following code used to be in onResume, but it turns out onResume is called when
1260 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1261 // is a more appropriate event to handle
1262 if (mVisible) {
1263 mAppsCustomizeTabHost.onWindowVisible();
1264 if (!mWorkspaceLoading) {
1265 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001266 // We want to let Launcher draw itself at least once before we force it to build
1267 // layers on all the workspace pages, so that transitioning to Launcher from other
1268 // apps is nice and speedy. Usually the first call to preDraw doesn't correspond to
1269 // a true draw so we wait until the second preDraw call to be safe
1270 observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001271 public boolean onPreDraw() {
Michael Jurka6ee21d22012-02-21 18:20:27 -08001272 // We delay the layer building a bit in order to give
1273 // other message processing a time to run. In particular
1274 // this avoids a delay in hiding the IME if it was
1275 // currently shown, because doing that may involve
1276 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001277 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Winson Chung31ce0732012-05-06 13:36:43 -07001278
Michael Jurka6ee21d22012-02-21 18:20:27 -08001279 observer.removeOnPreDrawListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001280 return true;
1281 }
1282 });
1283 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001284 // When Launcher comes back to foreground, a different Activity might be responsible for
1285 // the app market intent, so refresh the icon
1286 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001287 clearTypedText();
1288 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001289 }
1290
1291 private void sendAdvanceMessage(long delay) {
1292 mHandler.removeMessages(ADVANCE_MSG);
1293 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1294 mHandler.sendMessageDelayed(msg, delay);
1295 mAutoAdvanceSentTime = System.currentTimeMillis();
1296 }
1297
1298 private void updateRunning() {
1299 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1300 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1301 mAutoAdvanceRunning = autoAdvanceRunning;
1302 if (autoAdvanceRunning) {
1303 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1304 sendAdvanceMessage(delay);
1305 } else {
1306 if (!mWidgetsToAdvance.isEmpty()) {
1307 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1308 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1309 }
1310 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001311 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001312 }
1313 }
1314 }
1315
1316 private final Handler mHandler = new Handler() {
1317 @Override
1318 public void handleMessage(Message msg) {
1319 if (msg.what == ADVANCE_MSG) {
1320 int i = 0;
1321 for (View key: mWidgetsToAdvance.keySet()) {
1322 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1323 final int delay = mAdvanceStagger * i;
1324 if (v instanceof Advanceable) {
1325 postDelayed(new Runnable() {
1326 public void run() {
1327 ((Advanceable) v).advance();
1328 }
1329 }, delay);
1330 }
1331 i++;
1332 }
1333 sendAdvanceMessage(mAdvanceInterval);
1334 }
1335 }
1336 };
1337
1338 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001339 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001340 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1341 if (v instanceof Advanceable) {
1342 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001343 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001344 updateRunning();
1345 }
1346 }
1347
1348 void removeWidgetToAutoAdvance(View hostView) {
1349 if (mWidgetsToAdvance.containsKey(hostView)) {
1350 mWidgetsToAdvance.remove(hostView);
1351 updateRunning();
1352 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001353 }
1354
Joe Onorato9c1289c2009-08-17 11:03:03 -04001355 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001356 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001357 launcherInfo.hostView = null;
1358 }
1359
Winson Chung93eef082012-03-23 15:59:27 -07001360 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1361 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1362 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001363 }
1364
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001365 public LauncherAppWidgetHost getAppWidgetHost() {
1366 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001367 }
Romain Guycbb89e42009-06-08 15:52:54 -07001368
Winson Chunga9abd0e2010-10-27 17:18:37 -07001369 public LauncherModel getModel() {
1370 return mModel;
1371 }
1372
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001373 void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001374 getWindow().closeAllPanels();
1375
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001376 // Whatever we were doing is hereby canceled.
1377 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001378 }
1379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 @Override
1381 protected void onNewIntent(Intent intent) {
1382 super.onNewIntent(intent);
1383
1384 // Close the menu
1385 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001386 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001387 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001388
Joe Onorato14f122b2009-11-19 14:06:36 -08001389 boolean alreadyOnHome = ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1390 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001391
Adam Cohenac56cff2011-09-28 20:45:37 -07001392 Folder openFolder = mWorkspace.getOpenFolder();
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001393 // In all these cases, only animate if we're already on home
Patrick Dubroy758a9232011-03-03 19:54:56 -08001394 mWorkspace.exitWidgetResizeMode();
Adam Cohenac56cff2011-09-28 20:45:37 -07001395 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1396 openFolder == null) {
Patrick Dubroy6ec2e182011-02-23 13:31:16 -08001397 mWorkspace.moveToDefaultScreen(true);
Joe Onorato3a8820b2009-11-10 15:06:42 -08001398 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001399
1400 closeFolder();
Winson Chungde1af762011-07-21 16:44:07 -07001401 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001402
1403 // If we are already on home, then just animate back to the workspace, otherwise, just
1404 // wait until onResume to set the state back to Workspace
1405 if (alreadyOnHome) {
1406 showWorkspace(true);
1407 } else {
1408 mOnResumeState = State.WORKSPACE;
1409 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001410
Joe Onorato3a8820b2009-11-10 15:06:42 -08001411 final View v = getWindow().peekDecorView();
1412 if (v != null && v.getWindowToken() != null) {
1413 InputMethodManager imm = (InputMethodManager)getSystemService(
1414 INPUT_METHOD_SERVICE);
1415 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 }
Winson Chung337cd9d2011-03-30 10:39:30 -07001417
Michael Jurka35aa14d2011-07-07 17:01:08 -07001418 // Reset AllApps to its initial state
Adam Cohenb64d36e2011-10-17 21:48:02 -07001419 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Winson Chungc100e8e2011-08-09 16:02:43 -07001420 mAppsCustomizeTabHost.reset();
Winson Chung785d2eb2011-04-14 16:08:02 -07001421 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001422 }
1423 }
1424
1425 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001426 public void onRestoreInstanceState(Bundle state) {
1427 super.onRestoreInstanceState(state);
1428 for (int page: mSynchronouslyBoundPages) {
1429 mWorkspace.restoreInstanceStateForChild(page);
1430 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001431 }
1432
1433 @Override
1434 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001435 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001436 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437
Michael Jurka883f55b2010-10-21 15:47:14 -07001438 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001439 // We close any open folder since it will not be re-opened, and we need to make sure
1440 // this state is reflected.
1441 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442
Winson Chung3d503fb2011-07-13 17:25:49 -07001443 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screen > -1 &&
1444 mWaitingForResult) {
1445 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
1446 outState.putInt(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screen);
1447 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1448 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001449 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1450 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1451 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 }
1453
1454 if (mFolderInfo != null && mWaitingForResult) {
1455 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1456 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1457 }
Michael Jurka946ad472010-07-09 18:05:18 -07001458
Winson Chung785d2eb2011-04-14 16:08:02 -07001459 // Save the current AppsCustomize tab
1460 if (mAppsCustomizeTabHost != null) {
1461 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1462 if (currentTabTag != null) {
1463 outState.putString("apps_customize_currentTab", currentTabTag);
1464 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001465 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1466 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001467 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001468 }
1469
1470 @Override
1471 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001472 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001473
Winson Chunge7a03942011-08-05 15:05:12 -07001474 // Remove all pending runnables
1475 mHandler.removeMessages(ADVANCE_MSG);
1476 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001477 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001478
Winson Chungcd2b0142011-06-08 16:02:26 -07001479 // Stop callbacks from LauncherModel
1480 LauncherApplication app = ((LauncherApplication) getApplication());
1481 mModel.stopLoader();
1482 app.setLauncher(null);
1483
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001484 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001485 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001486 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001487 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001488 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001489 mAppWidgetHost = null;
1490
1491 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492
1493 TextKeyListener.getInstance().release();
1494
Winson Chung81b52252012-08-27 15:34:29 -07001495 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1496 // to prevent leaking Launcher activities on orientation change.
1497 if (mModel != null) {
1498 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1499 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001500
1501 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001502 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001503
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001504 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001505 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1506 mWorkspace.removeAllViews();
1507 mWorkspace = null;
1508 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001509
Michael Jurka2ecf9952012-06-18 12:52:28 -07001510 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 }
1512
Adam Cohena9cf38f2011-05-02 15:36:58 -07001513 public DragController getDragController() {
1514 return mDragController;
1515 }
1516
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 @Override
1518 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001519 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001520 super.startActivityForResult(intent, requestCode);
1521 }
1522
Winson Chung70d72102011-08-12 11:24:35 -07001523 /**
1524 * Indicates that we want global search for this activity by setting the globalSearch
1525 * argument for {@link #startSearch} to true.
1526 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001527 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001528 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001530
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001531 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001532
Karl Rosaen138a0412009-04-23 19:00:21 -07001533 if (initialQuery == null) {
1534 // Use any text typed in the launcher as the initial query
1535 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001537 if (appSearchData == null) {
1538 appSearchData = new Bundle();
Bjorn Bringert3e244cf2010-02-10 14:17:35 +00001539 appSearchData.putString(Search.SOURCE, "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001540 }
Winson Chungadf0c182012-08-23 14:59:07 -07001541 Rect sourceBounds = new Rect();
1542 if (mSearchDropTargetBar != null) {
1543 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1544 }
Romain Guycbb89e42009-06-08 15:52:54 -07001545
Michael Jurkaa33411c2012-06-14 16:18:21 -07001546 startGlobalSearch(initialQuery, selectInitialQuery,
1547 appSearchData, sourceBounds);
1548 }
1549
1550 /**
1551 * Starts the global search activity. This code is a copied from SearchManager
1552 */
1553 public void startGlobalSearch(String initialQuery,
1554 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001555 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001556 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1557 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1558 if (globalSearchActivity == null) {
1559 Log.w(TAG, "No global search activity found.");
1560 return;
1561 }
1562 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1563 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1564 intent.setComponent(globalSearchActivity);
1565 // Make sure that we have a Bundle to put source in
1566 if (appSearchData == null) {
1567 appSearchData = new Bundle();
1568 } else {
1569 appSearchData = new Bundle(appSearchData);
1570 }
1571 // Set source to package name of app that starts global search, if not set already.
1572 if (!appSearchData.containsKey("source")) {
1573 appSearchData.putString("source", getPackageName());
1574 }
1575 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1576 if (!TextUtils.isEmpty(initialQuery)) {
1577 intent.putExtra(SearchManager.QUERY, initialQuery);
1578 }
1579 if (selectInitialQuery) {
1580 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1581 }
1582 intent.setSourceBounds(sourceBounds);
1583 try {
1584 startActivity(intent);
1585 } catch (ActivityNotFoundException ex) {
1586 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1587 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 }
1589
Winson Chung70d72102011-08-12 11:24:35 -07001590 @Override
1591 public boolean onCreateOptionsMenu(Menu menu) {
1592 if (isWorkspaceLocked()) {
1593 return false;
1594 }
1595
1596 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001597
1598 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1599 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1600 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001601 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1602 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1603 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001604 String helpUrl = getString(R.string.help_url);
1605 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001606 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1607 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1608
Winson Chung70d72102011-08-12 11:24:35 -07001609 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1610 .setIcon(android.R.drawable.ic_menu_gallery)
1611 .setAlphabeticShortcut('W');
1612 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1613 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001614 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001615 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001616 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1617 .setIcon(android.R.drawable.ic_menu_preferences)
1618 .setIntent(settings)
1619 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001620 if (!helpUrl.isEmpty()) {
1621 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1622 .setIcon(android.R.drawable.ic_menu_help)
1623 .setIntent(help)
1624 .setAlphabeticShortcut('H');
1625 }
Winson Chung70d72102011-08-12 11:24:35 -07001626 return true;
1627 }
1628
1629 @Override
1630 public boolean onPrepareOptionsMenu(Menu menu) {
1631 super.onPrepareOptionsMenu(menu);
1632
1633 if (mAppsCustomizeTabHost.isTransitioning()) {
1634 return false;
1635 }
1636 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1637 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1638
1639 return true;
1640 }
1641
1642 @Override
1643 public boolean onOptionsItemSelected(MenuItem item) {
1644 switch (item.getItemId()) {
1645 case MENU_WALLPAPER_SETTINGS:
1646 startWallpaper();
1647 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001648 }
1649
1650 return super.onOptionsItemSelected(item);
1651 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001653 @Override
1654 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001655 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001656 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001657 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001658 }
1659
Joe Onorato9c1289c2009-08-17 11:03:03 -04001660 public boolean isWorkspaceLocked() {
1661 return mWorkspaceLoading || mWaitingForResult;
1662 }
1663
Michael Jurka0280c3b2010-09-17 15:00:07 -07001664 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001665 mPendingAddInfo.container = ItemInfo.NO_ID;
1666 mPendingAddInfo.screen = -1;
1667 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1668 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001669 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001670 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001671 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001672
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001673 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1674 AppWidgetProviderInfo appWidgetInfo) {
1675 if (appWidgetInfo.configure != null) {
1676 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001677
Bjorn Bringert7984c942009-12-09 15:38:25 +00001678 // Launch over to configure widget, if needed
1679 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001680 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001681 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001682 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001683 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001684 // Otherwise just add it
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001685 completeAddAppWidget(appWidgetId, info.container, info.screen, boundWidget,
1686 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001687 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001688 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001689 }
1690 }
Romain Guycbb89e42009-06-08 15:52:54 -07001691
Adam Cohenfbba09b2011-07-18 21:43:05 -07001692 /**
1693 * Process a shortcut drop.
1694 *
1695 * @param componentName The name of the component
1696 * @param screen The screen where it should be added
1697 * @param cell The cell it should be added to, optional
1698 * @param position The location on the screen where it was dropped, optional
1699 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001700 void processShortcutFromDrop(ComponentName componentName, long container, int screen,
1701 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001702 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001703 mPendingAddInfo.container = container;
1704 mPendingAddInfo.screen = screen;
1705 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001706
1707 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001708 mPendingAddInfo.cellX = cell[0];
1709 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001710 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001711
1712 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1713 createShortcutIntent.setComponent(componentName);
1714 processShortcut(createShortcutIntent);
1715 }
1716
Adam Cohenfbba09b2011-07-18 21:43:05 -07001717 /**
1718 * Process a widget drop.
1719 *
1720 * @param info The PendingAppWidgetInfo of the widget being added.
1721 * @param screen The screen where it should be added
1722 * @param cell The cell it should be added to, optional
1723 * @param position The location on the screen where it was dropped, optional
1724 */
Winson Chung3d503fb2011-07-13 17:25:49 -07001725 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, int screen,
Adam Cohend41fbf52012-02-16 23:53:59 -08001726 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001727 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001728 mPendingAddInfo.container = info.container = container;
1729 mPendingAddInfo.screen = info.screen = screen;
1730 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001731 mPendingAddInfo.minSpanX = info.minSpanX;
1732 mPendingAddInfo.minSpanY = info.minSpanY;
1733
Adam Cohenfbba09b2011-07-18 21:43:05 -07001734 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001735 mPendingAddInfo.cellX = cell[0];
1736 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001737 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001738 if (span != null) {
1739 mPendingAddInfo.spanX = span[0];
1740 mPendingAddInfo.spanY = span[1];
1741 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001742
Adam Cohened66b2b2012-01-23 17:28:51 -08001743 AppWidgetHostView hostView = info.boundWidget;
1744 int appWidgetId;
1745 if (hostView != null) {
1746 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001747 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001748 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001749 // In this case, we either need to start an activity to get permission to bind
1750 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001751 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001752 Bundle options = info.bindOptions;
1753
1754 boolean success = false;
1755 if (options != null) {
1756 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1757 info.componentName, options);
1758 } else {
1759 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1760 info.componentName);
1761 }
1762 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001763 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001764 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001765 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001766 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1767 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1768 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001769 // TODO: we need to make sure that this accounts for the options bundle.
1770 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001771 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1772 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001773 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001774 }
1775
Joe Onoratodeb98af2010-02-19 14:59:39 -08001776 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001777 // Handle case where user selected "Applications"
1778 String applicationName = getResources().getString(R.string.group_applications);
1779 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001780
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001781 if (applicationName != null && applicationName.equals(shortcutName)) {
1782 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1783 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001784
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001785 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1786 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001787 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001788 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001789 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001790 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001791 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001792 }
1793
Winson Chung24ab2f12010-09-16 14:10:47 -07001794 void processWallpaper(Intent intent) {
1795 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1796 }
1797
Winson Chung3d503fb2011-07-13 17:25:49 -07001798 FolderIcon addFolder(CellLayout layout, long container, final int screen, int cellX,
1799 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001800 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001801 folderInfo.title = getText(R.string.folder_name);
1802
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001803 // Update the model
Winson Chung3d503fb2011-07-13 17:25:49 -07001804 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screen, cellX, cellY,
1805 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001806 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001807
1808 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001809 FolderIcon newFolder =
1810 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
1811 mWorkspace.addInScreen(newFolder, container, screen, cellX, cellY, 1, 1,
1812 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001813 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001814 }
Romain Guycbb89e42009-06-08 15:52:54 -07001815
Joe Onorato9c1289c2009-08-17 11:03:03 -04001816 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001817 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001818 }
1819
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001820 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001821 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001822 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07001823 Intent chooser = Intent.createChooser(pickWallpaper,
1824 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08001825 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
1826 // Removed in Eclair MR1
1827// WallpaperManager wm = (WallpaperManager)
1828// getSystemService(Context.WALLPAPER_SERVICE);
1829// WallpaperInfo wi = wm.getWallpaperInfo();
1830// if (wi != null && wi.getSettingsActivity() != null) {
1831// LabeledIntent li = new LabeledIntent(getPackageName(),
1832// R.string.configure_wallpaper, 0);
1833// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
1834// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
1835// }
Mike Clerona0618e42009-10-22 13:55:21 -07001836 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 }
1838
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001839 /**
1840 * Registers various content observers. The current implementation registers
1841 * only a favorites observer to keep track of the favorites applications.
1842 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001843 private void registerContentObservers() {
1844 ContentResolver resolver = getContentResolver();
1845 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
1846 true, mWidgetObserver);
1847 }
1848
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001849 @Override
1850 public boolean dispatchKeyEvent(KeyEvent event) {
1851 if (event.getAction() == KeyEvent.ACTION_DOWN) {
1852 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001853 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07001854 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08001855 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka9bc8eba2012-05-21 20:36:44 -07001856 if (doesFileExist(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08001857 dumpState();
1858 return true;
1859 }
1860 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07001861 }
1862 } else if (event.getAction() == KeyEvent.ACTION_UP) {
1863 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07001864 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 return true;
1866 }
1867 }
1868
1869 return super.dispatchKeyEvent(event);
1870 }
1871
Joe Onorato88ec0992009-11-19 13:16:06 -08001872 @Override
1873 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001874 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001875 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001876 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07001877 Folder openFolder = mWorkspace.getOpenFolder();
1878 if (openFolder.isEditingName()) {
1879 openFolder.dismissEditingName();
1880 } else {
1881 closeFolder();
1882 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001883 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08001884 mWorkspace.exitWidgetResizeMode();
1885
Patrick Dubroy94f78a52011-02-28 17:39:16 -08001886 // Back button is a no-op here, but give at least some feedback for the button press
1887 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07001888 }
Joe Onorato88ec0992009-11-19 13:16:06 -08001889 }
1890
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001891 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001892 * Re-listen when widgets are reset.
1893 */
1894 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08001895 if (mAppWidgetHost != null) {
1896 mAppWidgetHost.startListening();
1897 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001898 }
1899
1900 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 * Launches the intent referred by the clicked shortcut.
1902 *
1903 * @param v The view representing the clicked shortcut.
1904 */
1905 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001906 // Make sure that rogue clicks don't get through while allapps is launching, or after the
1907 // view has detached (it's possible for this to happen if the view is removed mid touch).
1908 if (v.getWindowToken() == null) {
1909 return;
1910 }
1911
Winson Chung9b0b2fe2012-02-24 13:03:34 -08001912 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07001913 return;
1914 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07001915
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08001917 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001918 // Open shortcut
Romain Guyfb5411e2010-02-24 10:04:17 -08001919 final Intent intent = ((ShortcutInfo) tag).intent;
Joe Onorato13724ea2009-12-02 21:16:35 -08001920 int[] pos = new int[2];
1921 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08001922 intent.setSourceBounds(new Rect(pos[0], pos[1],
1923 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07001924
1925 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08001926
1927 if (success && v instanceof BubbleTextView) {
1928 mWaitingForResume = (BubbleTextView) v;
1929 mWaitingForResume.setStayPressed(true);
1930 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001931 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07001932 if (v instanceof FolderIcon) {
1933 FolderIcon fi = (FolderIcon) v;
1934 handleFolderClick(fi);
1935 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07001936 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001937 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001938 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07001939 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07001940 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07001941 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001942 }
1943 }
1944
Michael Jurka0e260592010-06-30 17:07:39 -07001945 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07001946 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001947 // clicking anywhere on the workspace causes the customization drawer to slide down
1948 showWorkspace(true);
Michael Jurka0e260592010-06-30 17:07:39 -07001949 return false;
1950 }
1951
Michael Jurkaaf442092010-06-10 17:01:57 -07001952 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001953 * Event handler for the search button
1954 *
1955 * @param v The view that was clicked.
1956 */
1957 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001958 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
1959
Amith Yamasania135ba82011-08-09 17:42:01 -07001960 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001961 }
1962
1963 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001964 * Event handler for the voice button
1965 *
1966 * @param v The view that was clicked.
1967 */
1968 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08001969 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001970
Michael Jurkaae65ee32012-04-30 11:45:54 -07001971 try {
1972 final SearchManager searchManager =
1973 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1974 ComponentName activityName = searchManager.getGlobalSearchActivity();
1975 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001976 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001977 if (activityName != null) {
1978 intent.setPackage(activityName.getPackageName());
1979 }
Michael Jurka86a720e2012-05-09 11:23:23 -07001980 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07001981 } catch (ActivityNotFoundException e) {
1982 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07001983 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07001984 startActivitySafely(null, intent, "onClickVoiceButton");
1985 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08001986 }
1987
1988 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07001989 * Event handler for the "grid" button that appears on the home screen, which
1990 * enters all apps mode.
1991 *
1992 * @param v The view that was clicked.
1993 */
1994 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07001995 showAllApps(true);
1996 }
1997
1998 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07001999 // Provide the same haptic feedback that the system offers for virtual keys.
2000 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002001 }
2002
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002003 public void onClickAppMarketButton(View v) {
2004 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002005 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002006 } else {
2007 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002008 }
2009 }
2010
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002011 void startApplicationDetailsActivity(ComponentName componentName) {
2012 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002013 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2014 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002015 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002016 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002017 }
2018
Patrick Dubroy5539af72010-09-07 15:22:01 -07002019 void startApplicationUninstallActivity(ApplicationInfo appInfo) {
2020 if ((appInfo.flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
2021 // System applications cannot be installed. For now, show a toast explaining that.
2022 // We may give them the option of disabling apps this way.
2023 int messageId = R.string.uninstall_system_app_text;
2024 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
2025 } else {
2026 String packageName = appInfo.componentName.getPackageName();
2027 String className = appInfo.componentName.getClassName();
2028 Intent intent = new Intent(
2029 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002030 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2031 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002032 startActivity(intent);
2033 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002034 }
2035
Michael Jurka86a720e2012-05-09 11:23:23 -07002036 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002040 // Only launch using the new animation if the shortcut has not opted out (this is a
2041 // private contract between launcher and may be ignored in the future).
2042 boolean useLaunchAnimation = (v != null) &&
2043 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2044 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002045 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2046 v.getMeasuredWidth(), v.getMeasuredHeight());
2047
2048 startActivity(intent, opts.toBundle());
2049 } else {
2050 startActivity(intent);
2051 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002052 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 } catch (SecurityException e) {
2054 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002055 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002057 "or use the exported attribute for this activity. "
2058 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002060 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002062
Michael Jurka86a720e2012-05-09 11:23:23 -07002063 boolean startActivitySafely(View v, Intent intent, Object tag) {
2064 boolean success = false;
2065 try {
2066 success = startActivity(v, intent, tag);
2067 } catch (ActivityNotFoundException e) {
2068 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2069 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2070 }
2071 return success;
2072 }
2073
Romain Guy8e633c52010-03-04 12:51:36 -08002074 void startActivityForResultSafely(Intent intent, int requestCode) {
2075 try {
2076 startActivityForResult(intent, requestCode);
2077 } catch (ActivityNotFoundException e) {
2078 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2079 } catch (SecurityException e) {
2080 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2081 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2082 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2083 "or use the exported attribute for this activity.", e);
2084 }
2085 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086
Adam Cohena9cf38f2011-05-02 15:36:58 -07002087 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002088 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002089 Folder openFolder = mWorkspace.getFolderForTag(info);
2090
2091 // If the folder info reports that the associated folder is open, then verify that
2092 // it is actually opened. There have been a few instances where this gets out of sync.
2093 if (info.opened && openFolder == null) {
2094 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2095 + info.screen + " (" + info.cellX + ", " + info.cellY + ")");
2096 info.opened = false;
2097 }
2098
Adam Cohenfb91f302012-06-11 15:45:18 -07002099 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 // Close any open folder
2101 closeFolder();
2102 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002103 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 } else {
2105 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 int folderScreen;
2107 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002108 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109 // .. and close it
2110 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002111 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 // Close any folder open on the current screen
2113 closeFolder();
2114 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002115 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002116 }
2117 }
2118 }
2119 }
2120
Adam Cohen268c4752012-06-06 17:47:33 -07002121 /**
2122 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2123 * in the DragLayer in the exact absolute location of the original FolderIcon.
2124 */
2125 private void copyFolderIconToImage(FolderIcon fi) {
2126 final int width = fi.getMeasuredWidth();
2127 final int height = fi.getMeasuredHeight();
2128
2129 // Lazy load ImageView, Bitmap and Canvas
2130 if (mFolderIconImageView == null) {
2131 mFolderIconImageView = new ImageView(this);
2132 }
2133 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2134 mFolderIconBitmap.getHeight() != height) {
2135 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2136 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2137 }
2138
2139 DragLayer.LayoutParams lp;
2140 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2141 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2142 } else {
2143 lp = new DragLayer.LayoutParams(width, height);
2144 }
2145
Adam Cohen307fe232012-08-16 17:55:58 -07002146 // The layout from which the folder is being opened may be scaled, adjust the starting
2147 // view size by this scale factor.
2148 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002149 lp.customPosition = true;
2150 lp.x = mRectForFolderAnimation.left;
2151 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002152 lp.width = (int) (scale * width);
2153 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002154
2155 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2156 fi.draw(mFolderIconCanvas);
2157 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002158 if (fi.getFolder() != null) {
2159 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2160 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002161 }
Adam Cohen268c4752012-06-06 17:47:33 -07002162 // Just in case this image view is still in the drag layer from a previous animation,
2163 // we remove it and re-add it.
2164 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2165 mDragLayer.removeView(mFolderIconImageView);
2166 }
2167 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002168 if (fi.getFolder() != null) {
2169 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002170 }
Adam Cohen268c4752012-06-06 17:47:33 -07002171 }
2172
Adam Cohen2801caf2011-05-13 20:57:39 -07002173 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002174 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002175 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2176 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2177 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2178
Adam Cohenc51934b2011-07-26 21:07:43 -07002179 FolderInfo info = (FolderInfo) fi.getTag();
2180 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2181 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002182 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2183 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002184 }
2185
Adam Cohen268c4752012-06-06 17:47:33 -07002186 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2187 copyFolderIconToImage(fi);
2188 fi.setVisibility(View.INVISIBLE);
2189
Michael Jurka2ecf9952012-06-18 12:52:28 -07002190 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002191 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002192 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2193 oa.start();
2194 }
2195
Adam Cohen268c4752012-06-06 17:47:33 -07002196 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002197 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002198 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2199 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2200 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2201
Adam Cohen268c4752012-06-06 17:47:33 -07002202 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002203
Adam Cohen268c4752012-06-06 17:47:33 -07002204 // We remove and re-draw the FolderIcon in-case it has changed
2205 mDragLayer.removeView(mFolderIconImageView);
2206 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002207 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002208 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002209 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002210 oa.addListener(new AnimatorListenerAdapter() {
2211 @Override
2212 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002213 if (cl != null) {
2214 cl.clearFolderLeaveBehind();
2215 // Remove the ImageView copy of the FolderIcon and make the original visible.
2216 mDragLayer.removeView(mFolderIconImageView);
2217 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002218 }
2219 }
2220 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002221 oa.start();
2222 }
2223
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002224 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002225 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002226 * is animated relative to the specified View. If the View is null, no animation
2227 * is played.
2228 *
2229 * @param folderInfo The FolderInfo describing the folder to open.
2230 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002231 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002232 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002233 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002234
Adam Cohena9cf38f2011-05-02 15:36:58 -07002235 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002236
Adam Cohen4554ee12011-08-03 16:13:21 -07002237 // Just verify that the folder hasn't already been added to the DragLayer.
2238 // There was a one-off crash where the folder had a parent already.
2239 if (folder.getParent() == null) {
2240 mDragLayer.addView(folder);
2241 mDragController.addDropTarget((DropTarget) folder);
2242 } else {
2243 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2244 folder.getParent() + ").");
2245 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002246 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002247 growAndFadeOutFolderIcon(folderIcon);
Adam Cohen4554ee12011-08-03 16:13:21 -07002248 }
2249
2250 public void closeFolder() {
2251 Folder folder = mWorkspace.getOpenFolder();
2252 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002253 if (folder.isEditingName()) {
2254 folder.dismissEditingName();
2255 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002256 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002257
2258 // Dismiss the folder cling
2259 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002260 }
2261 }
2262
2263 void closeFolder(Folder folder) {
2264 folder.getInfo().opened = false;
2265
2266 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2267 if (parent != null) {
2268 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2269 shrinkAndFadeInFolderIcon(fi);
2270 }
2271 folder.animateClosed();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002272 }
2273
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002274 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002275 if (!isDraggingEnabled()) return false;
2276 if (isWorkspaceLocked()) return false;
2277 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278
2279 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002280 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002281 }
2282
Michael Jurka0280c3b2010-09-17 15:00:07 -07002283 resetAddInfo();
2284 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002285 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002286 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 return true;
2288 }
2289
Winson Chung3d503fb2011-07-13 17:25:49 -07002290 // The hotseat touch handling does not go through Workspace, and we always allow long press
2291 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002292 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002293 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2294 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002295 if (itemUnderLongClick == null) {
2296 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002297 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2298 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002299 startWallpaper();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002300 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002301 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002302 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002303 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002304 }
2305 }
2306 }
2307 return true;
2308 }
2309
Winson Chung3d503fb2011-07-13 17:25:49 -07002310 boolean isHotseatLayout(View layout) {
2311 return mHotseat != null && layout != null &&
2312 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2313 }
2314 Hotseat getHotseat() {
2315 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002316 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002317 SearchDropTargetBar getSearchBar() {
2318 return mSearchDropTargetBar;
2319 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002320
Winson Chung3d503fb2011-07-13 17:25:49 -07002321 /**
2322 * Returns the CellLayout of the specified container at the specified screen.
2323 */
2324 CellLayout getCellLayout(long container, int screen) {
2325 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2326 if (mHotseat != null) {
2327 return mHotseat.getLayout();
2328 } else {
2329 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002330 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002331 } else {
2332 return (CellLayout) mWorkspace.getChildAt(screen);
Romain Guya6abce82009-11-10 02:54:41 -08002333 }
2334 }
2335
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336 Workspace getWorkspace() {
2337 return mWorkspace;
2338 }
2339
Daniel Sandler843e8602010-06-07 14:59:01 -04002340 // Now a part of LauncherModel.Callbacks. Used to reorder loading steps.
2341 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002342 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002343 }
2344
Andrew Flynn0dca1ec2012-02-29 13:33:22 -08002345 public boolean isAllAppsButtonRank(int rank) {
2346 return mHotseat.isAllAppsButtonRank(rank);
2347 }
2348
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002349 /**
2350 * Helper method for the cameraZoomIn/cameraZoomOut animations
2351 * @param view The view being animated
Winson Chungf0ea4d32011-06-06 14:27:16 -07002352 * @param state The state that we are moving in or out of (eg. APPS_CUSTOMIZE)
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002353 * @param scaleFactor The scale factor used for the zoom
2354 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002355 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002356 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002357 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002358 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002359
Winson Chung18f41f82012-05-09 13:28:10 -07002360 void disableWallpaperIfInAllApps() {
2361 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002362 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002363 if (mAppsCustomizeTabHost != null &&
2364 !mAppsCustomizeTabHost.isTransitioning()) {
2365 updateWallpaperVisibility(false);
2366 }
2367 }
2368 }
2369
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002370 void updateWallpaperVisibility(boolean visible) {
2371 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2372 int curflags = getWindow().getAttributes().flags
2373 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2374 if (wpflags != curflags) {
2375 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2376 }
2377 }
2378
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002379 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2380 if (v instanceof LauncherTransitionable) {
2381 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2382 }
2383 }
2384
Michael Jurkabed61d22012-02-14 22:51:29 -08002385 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2386 if (v instanceof LauncherTransitionable) {
2387 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2388 }
Winson Chung70442722012-02-10 15:43:22 -08002389
2390 // Update the workspace transition step as well
2391 dispatchOnLauncherTransitionStep(v, 0f);
2392 }
2393
2394 private void dispatchOnLauncherTransitionStep(View v, float t) {
2395 if (v instanceof LauncherTransitionable) {
2396 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2397 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002398 }
2399
2400 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2401 if (v instanceof LauncherTransitionable) {
2402 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2403 }
Winson Chung70442722012-02-10 15:43:22 -08002404
2405 // Update the workspace transition step as well
2406 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002407 }
2408
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002409 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002410 * Things to test when changing the following seven functions.
2411 * - Home from workspace
2412 * - from center screen
2413 * - from other screens
2414 * - Home from all apps
2415 * - from center screen
2416 * - from other screens
2417 * - Back from all apps
2418 * - from center screen
2419 * - from other screens
2420 * - Launch app from workspace and quit
2421 * - with back
2422 * - with home
2423 * - Launch app from all apps and quit
2424 * - with back
2425 * - with home
2426 * - Go to a screen that's not the default, then all
2427 * apps, and launch and app, and go back
2428 * - with back
2429 * -with home
2430 * - On workspace, long press power and go back
2431 * - with back
2432 * - with home
2433 * - On all apps, long press power and go back
2434 * - with back
2435 * - with home
2436 * - On workspace, power off
2437 * - On all apps, power off
2438 * - Launch an app and turn off the screen while in that app
2439 * - Go back with home key
2440 * - Go back with back key TODO: make this not go to workspace
2441 * - From all apps
2442 * - From workspace
2443 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2444 * - From all apps
2445 * - From the center workspace
2446 * - From another workspace
2447 */
2448
2449 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002450 * Zoom the camera out from the workspace to reveal 'toView'.
2451 * Assumes that the view to show is anchored at either the very top or very bottom
2452 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002453 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002454 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002455 if (mStateAnimation != null) {
2456 mStateAnimation.cancel();
2457 mStateAnimation = null;
2458 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002459 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002460
Winson Chungf0ea4d32011-06-06 14:27:16 -07002461 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2462 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2463 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002464 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002465 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002466 final int startDelay =
2467 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002468
Michael Jurkab3e22d92011-10-31 15:58:33 -07002469 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002470
Michael Jurkad74c9842011-07-10 12:44:21 -07002471 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002472 Animator workspaceAnim =
2473 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002474
2475 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002476 toView.setScaleX(scale);
2477 toView.setScaleY(scale);
2478 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2479 scaleAnim.
2480 scaleX(1f).scaleY(1f).
2481 setDuration(duration).
2482 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002483
Winson Chungf0ea4d32011-06-06 14:27:16 -07002484 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002485 toView.setAlpha(0f);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002486 final ObjectAnimator alphaAnim = ObjectAnimator
2487 .ofFloat(toView, "alpha", 0f, 1f)
2488 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002489 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002490 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2491 @Override
2492 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002493 if (animation == null) {
2494 throw new RuntimeException("animation is null");
2495 }
Winson Chung70442722012-02-10 15:43:22 -08002496 float t = (Float) animation.getAnimatedValue();
2497 dispatchOnLauncherTransitionStep(fromView, t);
2498 dispatchOnLauncherTransitionStep(toView, t);
2499 }
2500 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002501
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002502 // toView should appear right at the end of the workspace shrink
2503 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002504 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002505 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002506 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002507
2508 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002509 boolean animationCancelled = false;
2510
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002511 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002512 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002513 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002514 // Prepare the position
2515 toView.setTranslationX(0.0f);
2516 toView.setTranslationY(0.0f);
2517 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002518 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002519 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002520 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002521 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002522 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2523 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002524
Michael Jurkafa7969f2012-09-21 16:39:14 -07002525 if (mWorkspace != null && !springLoaded && !LauncherApplication.isScreenLarge()) {
Winson Chung32174c82011-07-19 15:47:55 -07002526 // Hide the workspace scrollbar
2527 mWorkspace.hideScrollingIndicator(true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002528 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002529 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002530 if (!animationCancelled) {
2531 updateWallpaperVisibility(false);
2532 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002533
2534 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002535 if (mSearchDropTargetBar != null) {
2536 mSearchDropTargetBar.hideSearchBar(false);
2537 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002538 }
2539
Adam Cohencff6af82011-09-13 14:51:53 -07002540 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002541 public void onAnimationCancel(Animator animation) {
2542 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002543 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002544 });
2545
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002546 if (workspaceAnim != null) {
2547 mStateAnimation.play(workspaceAnim);
2548 }
Michael Jurka1899a362011-11-03 13:50:45 -07002549
2550 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002551 final ViewTreeObserver observer;
2552
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002553 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2554 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002555
2556 // If any of the objects being animated haven't been measured/laid out
2557 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002558 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002559 (mWorkspace.getMeasuredWidth() == 0) ||
2560 (toView.getMeasuredWidth() == 0)) {
2561 observer = mWorkspace.getViewTreeObserver();
2562 delayAnim = true;
2563 } else {
2564 observer = null;
Michael Jurka1899a362011-11-03 13:50:45 -07002565 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002566
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002567 final AnimatorSet stateAnimation = mStateAnimation;
2568 final Runnable startAnimRunnable = new Runnable() {
2569 public void run() {
2570 // Check that mStateAnimation hasn't changed while
2571 // we waited for a layout/draw pass
2572 if (mStateAnimation != stateAnimation)
2573 return;
2574 setPivotsForZoom(toView, scale);
2575 dispatchOnLauncherTransitionStart(fromView, animated, false);
2576 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurka382aa182012-06-11 15:42:07 -07002577 toView.post(new Runnable() {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002578 public void run() {
2579 // Check that mStateAnimation hasn't changed while
2580 // we waited for a layout/draw pass
2581 if (mStateAnimation != stateAnimation)
2582 return;
2583 mStateAnimation.start();
2584 }
2585 });
2586 }
2587 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002588 if (delayAnim) {
2589 final OnGlobalLayoutListener delayedStart = new OnGlobalLayoutListener() {
2590 public void onGlobalLayout() {
Michael Jurka382aa182012-06-11 15:42:07 -07002591 toView.post(startAnimRunnable);
Michael Jurka3a9fced2012-04-13 14:44:29 -07002592 observer.removeOnGlobalLayoutListener(this);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002593 }
2594 };
2595 observer.addOnGlobalLayoutListener(delayedStart);
2596 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002597 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002598 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002599 } else {
2600 toView.setTranslationX(0.0f);
2601 toView.setTranslationY(0.0f);
2602 toView.setScaleX(1.0f);
2603 toView.setScaleY(1.0f);
2604 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002605 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002606
Michael Jurkabed61d22012-02-14 22:51:29 -08002607 if (!springLoaded && !LauncherApplication.isScreenLarge()) {
2608 // Hide the workspace scrollbar
2609 mWorkspace.hideScrollingIndicator(true);
2610 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002611
2612 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002613 if (mSearchDropTargetBar != null) {
2614 mSearchDropTargetBar.hideSearchBar(false);
2615 }
Michael Jurkaabded662011-03-04 12:06:57 -08002616 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002617 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002618 dispatchOnLauncherTransitionStart(fromView, animated, false);
2619 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002620 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002621 dispatchOnLauncherTransitionStart(toView, animated, false);
2622 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002623 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002624 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002625 }
2626
2627 /**
2628 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002629 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002630 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002631 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002632 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2633 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002634
Michael Jurkab3e22d92011-10-31 15:58:33 -07002635 if (mStateAnimation != null) {
2636 mStateAnimation.cancel();
2637 mStateAnimation = null;
2638 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002639 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002640
Winson Chungf0ea4d32011-06-06 14:27:16 -07002641 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002642 final int fadeOutDuration =
2643 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002644 final float scaleFactor = (float)
2645 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2646 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002647 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002648 Animator workspaceAnim = null;
2649
2650 if (toState == State.WORKSPACE) {
2651 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2652 workspaceAnim = mWorkspace.getChangeStateAnimation(
2653 Workspace.State.NORMAL, animated, stagger);
2654 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2655 workspaceAnim = mWorkspace.getChangeStateAnimation(
2656 Workspace.State.SPRING_LOADED, animated);
2657 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002658
Michael Jurkab3e22d92011-10-31 15:58:33 -07002659 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002660 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002661 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002662 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002663 final LauncherViewPropertyAnimator scaleAnim =
2664 new LauncherViewPropertyAnimator(fromView);
2665 scaleAnim.
2666 scaleX(scaleFactor).scaleY(scaleFactor).
2667 setDuration(duration).
2668 setInterpolator(new Workspace.ZoomInInterpolator());
2669
2670 final ObjectAnimator alphaAnim = ObjectAnimator
2671 .ofFloat(fromView, "alpha", 1f, 0f)
2672 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002673 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002674 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2675 @Override
2676 public void onAnimationUpdate(ValueAnimator animation) {
2677 float t = 1f - (Float) animation.getAnimatedValue();
2678 dispatchOnLauncherTransitionStep(fromView, t);
2679 dispatchOnLauncherTransitionStep(toView, t);
2680 }
2681 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002682
Michael Jurka2ecf9952012-06-18 12:52:28 -07002683 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002684
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002685 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2686 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002687
2688 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002689 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002690 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002691 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002692 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002693 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2694 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkabafdaaf2012-04-26 13:43:25 -07002695 if (mWorkspace != null) {
2696 mWorkspace.hideScrollingIndicator(false);
2697 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002698 if (onCompleteRunnable != null) {
2699 onCompleteRunnable.run();
2700 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002701 }
2702 });
2703
Winson Chungf0ea4d32011-06-06 14:27:16 -07002704 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002705 if (workspaceAnim != null) {
2706 mStateAnimation.play(workspaceAnim);
2707 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002708 dispatchOnLauncherTransitionStart(fromView, animated, true);
2709 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002710 final Animator stateAnimation = mStateAnimation;
2711 mWorkspace.post(new Runnable() {
2712 public void run() {
2713 if (stateAnimation != mStateAnimation)
2714 return;
2715 mStateAnimation.start();
2716 }
2717 });
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002718 } else {
2719 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002720 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002721 dispatchOnLauncherTransitionStart(fromView, animated, true);
2722 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002723 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002724 dispatchOnLauncherTransitionStart(toView, animated, true);
2725 dispatchOnLauncherTransitionEnd(toView, animated, true);
Michael Jurkab737ee62011-11-15 15:57:22 -08002726 mWorkspace.hideScrollingIndicator(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002727 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002728 }
2729
Michael Jurkae326f182011-11-21 14:05:46 -08002730 @Override
2731 public void onTrimMemory(int level) {
2732 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002733 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002734 mAppsCustomizeTabHost.onTrimMemory();
2735 }
2736 }
2737
Winson Chung18f41f82012-05-09 13:28:10 -07002738 @Override
2739 public void onWindowFocusChanged(boolean hasFocus) {
2740 if (!hasFocus) {
2741 // When another window occludes launcher (like the notification shade, or recents),
2742 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2743 updateWallpaperVisibility(true);
2744 } else {
2745 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002746 mWorkspace.postDelayed(new Runnable() {
2747 @Override
2748 public void run() {
2749 disableWallpaperIfInAllApps();
2750 }
2751 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002752 }
2753 }
2754
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002755 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002756 showWorkspace(animated, null);
2757 }
2758
2759 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002760 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002761 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002762 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002763 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002764
Winson Chungc7d2b602012-05-16 17:02:20 -07002765 // Show the search bar (only animate if we were showing the drop target bar in spring
2766 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002767 if (mSearchDropTargetBar != null) {
2768 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2769 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002770
Michael Jurkab737ee62011-11-15 15:57:22 -08002771 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002772 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002773
2774 // Set focus to the AppsCustomize button
2775 if (mAllAppsButton != null) {
2776 mAllAppsButton.requestFocus();
2777 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002778 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002779
Michael Jurkab737ee62011-11-15 15:57:22 -08002780 mWorkspace.flashScrollingIndicator(animated);
Adam Cohen7777d962011-08-18 18:58:38 -07002781
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002782 // Change the state *after* we've called all the transition code
2783 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002784
Winson Chung5fb63472011-02-02 17:03:37 -08002785 // Resume the auto-advance of widgets
2786 mUserPresent = true;
2787 updateRunning();
2788
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002789 // send an accessibility event to announce the context change
2790 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
Joe Onorato00acb122009-08-04 16:04:30 -04002791 }
2792
Michael Jurkab3e22d92011-10-31 15:58:33 -07002793 void showAllApps(boolean animated) {
2794 if (mState != State.WORKSPACE) return;
2795
2796 showAppsCustomizeHelper(animated, false);
2797 mAppsCustomizeTabHost.requestFocus();
2798
Michael Jurkab3e22d92011-10-31 15:58:33 -07002799 // Change the state *after* we've called all the transition code
2800 mState = State.APPS_CUSTOMIZE;
2801
2802 // Pause the auto-advance of widgets until we are out of AllApps
2803 mUserPresent = false;
2804 updateRunning();
2805 closeFolder();
2806
2807 // Send an accessibility event to announce the context change
2808 getWindow().getDecorView().sendAccessibilityEvent(AccessibilityEvent.TYPE_VIEW_SELECTED);
2809 }
2810
Adam Cohen7777d962011-08-18 18:58:38 -07002811 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002812 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002813 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08002814 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07002815 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07002816 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002817 }
Adam Cohen7777d962011-08-18 18:58:38 -07002818
Adam Cohened66b2b2012-01-23 17:28:51 -08002819 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
2820 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07002821 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
2822
Winson Chunge7a03942011-08-05 15:05:12 -07002823 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07002824 @Override
2825 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002826 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07002827 // Before we show workspace, hide all apps again because
2828 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
2829 // clean up our state transition functions
2830 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002831 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07002832 } else {
2833 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07002834 }
Winson Chung557d6ed2011-07-08 15:34:52 -07002835 }
2836 }, (extendedDelay ?
2837 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
2838 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
2839 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07002840
Michael Jurkad3ef3062010-11-23 16:23:58 -08002841 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07002842 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002843 final boolean animated = true;
2844 final boolean springLoaded = true;
2845 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07002846 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07002847 }
2848 // Otherwise, we are not in spring loaded mode, so don't do anything.
2849 }
2850
Michael Jurkab737ee62011-11-15 15:57:22 -08002851 void hideDockDivider() {
2852 if (mQsbDivider != null && mDockDivider != null) {
2853 mQsbDivider.setVisibility(View.INVISIBLE);
2854 mDockDivider.setVisibility(View.INVISIBLE);
2855 }
2856 }
2857
2858 void showDockDivider(boolean animated) {
2859 if (mQsbDivider != null && mDockDivider != null) {
2860 mQsbDivider.setVisibility(View.VISIBLE);
2861 mDockDivider.setVisibility(View.VISIBLE);
2862 if (mDividerAnimator != null) {
2863 mDividerAnimator.cancel();
2864 mQsbDivider.setAlpha(1f);
2865 mDockDivider.setAlpha(1f);
2866 mDividerAnimator = null;
2867 }
2868 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07002869 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
2870 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f),
2871 LauncherAnimUtils.ofFloat(mDockDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07002872 int duration = 0;
2873 if (mSearchDropTargetBar != null) {
2874 duration = mSearchDropTargetBar.getTransitionInDuration();
2875 }
2876 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08002877 mDividerAnimator.start();
2878 }
2879 }
2880 }
2881
Michael Jurkab3e22d92011-10-31 15:58:33 -07002882 void lockAllApps() {
2883 // TODO
2884 }
2885
2886 void unlockAllApps() {
2887 // TODO
2888 }
2889
Winson Chungf0ea4d32011-06-06 14:27:16 -07002890 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002891 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002892 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002893 void showHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002894 if (!LauncherApplication.isScreenLarge()) {
2895 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002896 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002897 int duration = 0;
2898 if (mSearchDropTargetBar != null) {
2899 duration = mSearchDropTargetBar.getTransitionInDuration();
2900 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002901 mHotseat.animate().alpha(1f).setDuration(duration);
2902 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002903 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002904 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002905 }
2906 }
2907 }
2908
2909 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07002910 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07002911 */
Winson Chung3d503fb2011-07-13 17:25:49 -07002912 void hideHotseat(boolean animated) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07002913 if (!LauncherApplication.isScreenLarge()) {
2914 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002915 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07002916 int duration = 0;
2917 if (mSearchDropTargetBar != null) {
2918 duration = mSearchDropTargetBar.getTransitionOutDuration();
2919 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08002920 mHotseat.animate().alpha(0f).setDuration(duration);
2921 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002922 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07002923 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002924 }
Winson Chungb26f3d62011-06-02 10:49:29 -07002925 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08002926 }
2927
Patrick Dubroy5f445422011-02-18 14:35:21 -08002928 /**
2929 * Add an item from all apps or customize onto the given workspace screen.
2930 * If layout is null, add to the current screen.
2931 */
2932 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002933 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07002934 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07002935 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07002936 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002937
Winson Chungdff8ebb2011-09-08 17:25:31 -07002938 /** Maps the current orientation to an index for referencing orientation correct global icons */
2939 private int getCurrentOrientationIndexForGlobalIcons() {
2940 // default - 0, landscape - 1
2941 switch (getResources().getConfiguration().orientation) {
2942 case Configuration.ORIENTATION_LANDSCAPE:
2943 return 1;
2944 default:
2945 return 0;
2946 }
2947 }
2948
Michael Jurkaae65ee32012-04-30 11:45:54 -07002949 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07002950 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002951 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07002952 // Look for the toolbar icon specified in the activity meta-data
2953 Bundle metaData = packageManager.getActivityInfo(
2954 activityName, PackageManager.GET_META_DATA).metaData;
2955 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002956 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002957 if (iconResId != 0) {
2958 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002959 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002960 }
2961 }
2962 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07002963 // This can happen if the activity defines an invalid drawable
2964 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
2965 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01002966 } catch (Resources.NotFoundException nfe) {
2967 // This can happen if the activity defines an invalid drawable
2968 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
2969 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07002970 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002971 return null;
2972 }
2973
2974 // if successful in getting icon, return it; otherwise, set button to use default drawable
2975 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07002976 int buttonId, ComponentName activityName, int fallbackDrawableId,
2977 String toolbarResourceName) {
2978 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07002979 Resources r = getResources();
2980 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
2981 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002982
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002983 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002984 // If we were unable to find the icon via the meta-data, use a generic one
2985 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07002986 toolbarIcon = r.getDrawable(fallbackDrawableId);
2987 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002988 if (button != null) {
2989 button.setCompoundDrawables(toolbarIcon, null, null, null);
2990 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002991 return null;
2992 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07002993 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07002994 if (button != null) {
2995 button.setCompoundDrawables(toolbarIcon, null, null, null);
2996 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08002997 return toolbarIcon.getConstantState();
2998 }
2999 }
3000
3001 // if successful in getting icon, return it; otherwise, set button to use default drawable
3002 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003003 int buttonId, ComponentName activityName, int fallbackDrawableId,
3004 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003005 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003006 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003007
Michael Jurka19e0fc52011-07-22 18:00:21 -07003008 if (button != null) {
3009 // If we were unable to find the icon via the meta-data, use a
3010 // generic one
3011 if (toolbarIcon == null) {
3012 button.setImageResource(fallbackDrawableId);
3013 } else {
3014 button.setImageDrawable(toolbarIcon);
3015 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003016 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003017
3018 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3019
Michael Jurka0423dcf2010-10-05 14:56:18 -07003020 }
3021
Winson Chung1b884092012-06-08 17:13:02 -07003022 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003023 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003024 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003025 }
3026
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003027 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003028 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003029 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003030 }
3031
Winson Chungbb185bd2011-11-21 12:31:42 -08003032 private void invalidatePressedFocusedStates(View container, View button) {
3033 if (container instanceof HolographicLinearLayout) {
3034 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3035 layout.invalidatePressedFocusedStates();
3036 } else if (button instanceof HolographicImageView) {
3037 HolographicImageView view = (HolographicImageView) button;
3038 view.invalidatePressedFocusedStates();
3039 }
3040 }
3041
Winson Chungc51db6a2011-10-05 11:44:49 -07003042 private boolean updateGlobalSearchIcon() {
3043 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003044 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003045 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003046 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003047 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003048
Winson Chung1cad91e2011-05-25 17:41:01 -07003049 final SearchManager searchManager =
3050 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3051 ComponentName activityName = searchManager.getGlobalSearchActivity();
3052 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003053 int coi = getCurrentOrientationIndexForGlobalIcons();
3054 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003055 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3056 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3057 if (sGlobalSearchIcon[coi] == null) {
3058 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3059 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3060 TOOLBAR_ICON_METADATA_NAME);
3061 }
3062
Winson Chungc51db6a2011-10-05 11:44:49 -07003063 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3064 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003065 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003066 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003067 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003068 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003069 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3070 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3071 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003072 voiceButton.setVisibility(View.GONE);
Michael Jurkac60498a2012-05-07 15:29:42 -07003073 if (voiceButtonProxy != null) {
3074 voiceButtonProxy.setVisibility(View.GONE);
3075 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003076 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003077 }
3078 }
3079
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003080 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003081 final View searchButtonContainer = findViewById(R.id.search_button_container);
3082 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003083 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003084 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003085 }
3086
Winson Chungc51db6a2011-10-05 11:44:49 -07003087 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003088 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003089 final View voiceButton = findViewById(R.id.voice_button);
Winson Chunge3fbfa92012-04-24 14:34:28 -07003090 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
Winson Chung97d85d22011-04-13 11:27:36 -07003091
Winson Chungc51db6a2011-10-05 11:44:49 -07003092 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003093 final SearchManager searchManager =
3094 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3095 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3096
3097 ComponentName activityName = null;
3098 if (globalSearchActivity != null) {
3099 // Check if the global search activity handles voice search
3100 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3101 intent.setPackage(globalSearchActivity.getPackageName());
3102 activityName = intent.resolveActivity(getPackageManager());
3103 }
3104
3105 if (activityName == null) {
3106 // Fallback: check if an activity other than the global search activity
3107 // resolves this
3108 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3109 activityName = intent.resolveActivity(getPackageManager());
3110 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003111 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003112 int coi = getCurrentOrientationIndexForGlobalIcons();
3113 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003114 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3115 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3116 if (sVoiceSearchIcon[coi] == null) {
3117 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3118 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3119 TOOLBAR_ICON_METADATA_NAME);
3120 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003121 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003122 voiceButton.setVisibility(View.VISIBLE);
Jim Miller14091b12012-04-24 19:27:54 -07003123 if (voiceButtonProxy != null) {
3124 voiceButtonProxy.setVisibility(View.VISIBLE);
3125 }
Winson Chungbb185bd2011-11-21 12:31:42 -08003126 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003127 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003128 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003129 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003130 voiceButton.setVisibility(View.GONE);
Jim Miller14091b12012-04-24 19:27:54 -07003131 if (voiceButtonProxy != null) {
3132 voiceButtonProxy.setVisibility(View.GONE);
3133 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003134 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003135 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003136 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003137
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003138 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003139 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3140 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003141 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003142 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003143 }
3144
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003145 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003146 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003147 */
3148 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003149 final View marketButton = findViewById(R.id.market_button);
3150 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3151 // Find the app market activity by resolving an intent.
3152 // (If multiple app markets are installed, it will return the ResolverActivity.)
3153 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003154 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003155 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003156 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003157 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003158 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3159 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003160 marketButton.setVisibility(View.VISIBLE);
3161 } else {
3162 // We should hide and disable the view so that we don't try and restore the visibility
3163 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3164 marketButton.setVisibility(View.GONE);
3165 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003166 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003167 }
3168
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003169 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003170 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3171 Resources r = getResources();
3172 Drawable marketIconDrawable = d.newDrawable(r);
3173 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3174 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3175 marketIconDrawable.setBounds(0, 0, w, h);
3176
3177 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003178 }
3179
Michael Jurkad7c28052012-04-27 15:43:36 -07003180 @Override
3181 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
3182 boolean result = super.dispatchPopulateAccessibilityEvent(event);
3183 final List<CharSequence> text = event.getText();
3184 text.clear();
3185 text.add(getString(R.string.home));
3186 return result;
3187 }
3188
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003189 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003190 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003191 */
3192 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3193 @Override
3194 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003195 closeSystemDialogs();
3196 }
3197 }
3198
3199 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003200 * Receives notifications whenever the appwidgets are reset.
3201 */
3202 private class AppWidgetResetObserver extends ContentObserver {
3203 public AppWidgetResetObserver() {
3204 super(new Handler());
3205 }
3206
3207 @Override
3208 public void onChange(boolean selfChange) {
3209 onAppWidgetReset();
3210 }
3211 }
3212
3213 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003214 * If the activity is currently paused, signal that we need to re-run the loader
3215 * in onResume.
3216 *
3217 * This needs to be called from incoming places where resources might have been loaded
3218 * while we are paused. That is becaues the Configuration might be wrong
3219 * when we're not running, and if it comes back to what it was when we
3220 * were paused, we are not restarted.
3221 *
3222 * Implementation of the method from LauncherModel.Callbacks.
3223 *
3224 * @return true if we are currently paused. The caller might be able to
3225 * skip some work in that case since we will come back again.
3226 */
3227 public boolean setLoadOnResume() {
3228 if (mPaused) {
3229 Log.i(TAG, "setLoadOnResume");
3230 mOnResumeNeedsLoad = true;
3231 return true;
3232 } else {
3233 return false;
3234 }
3235 }
3236
3237 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003238 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003239 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003240 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003241 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003242 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003243 } else {
3244 return SCREEN_COUNT / 2;
3245 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003246 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003247
Joe Onorato9c1289c2009-08-17 11:03:03 -04003248 /**
3249 * Refreshes the shortcuts shown on the workspace.
3250 *
3251 * Implementation of the method from LauncherModel.Callbacks.
3252 */
3253 public void startBinding() {
3254 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003255
Winson Chungf0c6ae02012-03-21 16:10:31 -07003256 mNewShortcutAnimatePage = -1;
3257 mNewShortcutAnimateViews.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003258 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003259 int count = workspace.getChildCount();
3260 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003261 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003262 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3263 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003264 }
Michael Jurka05bf644e2011-11-30 20:28:53 -08003265 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003266 if (mHotseat != null) {
3267 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003268 }
3269 }
3270
3271 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003272 * Bind the items start-end from the list.
3273 *
3274 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003275 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003276 public void bindItems(ArrayList<ItemInfo> shortcuts, int start, int end) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003277 setLoadOnResume();
3278
Winson Chungf0c6ae02012-03-21 16:10:31 -07003279 // Get the list of added shortcuts and intersect them with the set of shortcuts here
3280 Set<String> newApps = new HashSet<String>();
3281 newApps = mSharedPrefs.getStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, newApps);
3282
3283 Workspace workspace = mWorkspace;
3284 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003285 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003286
3287 // Short circuit if we are loading dock items for a configuration which has no dock
3288 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3289 mHotseat == null) {
3290 continue;
3291 }
3292
Joe Onorato9c1289c2009-08-17 11:03:03 -04003293 switch (item.itemType) {
3294 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3295 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003296 ShortcutInfo info = (ShortcutInfo) item;
3297 String uri = info.intent.toUri(0).toString();
3298 View shortcut = createShortcut(info);
Winson Chung3d503fb2011-07-13 17:25:49 -07003299 workspace.addInScreen(shortcut, item.container, item.screen, item.cellX,
3300 item.cellY, 1, 1, false);
Winson Chungbfeac062012-06-06 15:56:08 -07003301 boolean animateIconUp = false;
3302 synchronized (newApps) {
3303 if (newApps.contains(uri)) {
3304 animateIconUp = newApps.remove(uri);
3305 }
3306 }
3307 if (animateIconUp) {
Winson Chungf0c6ae02012-03-21 16:10:31 -07003308 // Prepare the view to be animated up
3309 shortcut.setAlpha(0f);
3310 shortcut.setScaleX(0f);
3311 shortcut.setScaleY(0f);
3312 mNewShortcutAnimatePage = item.screen;
3313 if (!mNewShortcutAnimateViews.contains(shortcut)) {
3314 mNewShortcutAnimateViews.add(shortcut);
3315 }
3316 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003317 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003318 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003319 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003320 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003321 (FolderInfo) item, mIconCache);
Winson Chung3d503fb2011-07-13 17:25:49 -07003322 workspace.addInScreen(newFolder, item.container, item.screen, item.cellX,
3323 item.cellY, 1, 1, false);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003324 break;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003325 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003326 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003327
Joe Onorato9c1289c2009-08-17 11:03:03 -04003328 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003329 }
3330
Joe Onorato9c1289c2009-08-17 11:03:03 -04003331 /**
3332 * Implementation of the method from LauncherModel.Callbacks.
3333 */
Joe Onoratoad72e172009-11-06 16:25:04 -05003334 public void bindFolders(HashMap<Long, FolderInfo> folders) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003335 setLoadOnResume();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003336 sFolders.clear();
3337 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003339
3340 /**
3341 * Add the views for a widget to the workspace.
3342 *
3343 * Implementation of the method from LauncherModel.Callbacks.
3344 */
3345 public void bindAppWidget(LauncherAppWidgetInfo item) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003346 setLoadOnResume();
3347
Daniel Sandler843e8602010-06-07 14:59:01 -04003348 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3349 if (DEBUG_WIDGETS) {
3350 Log.d(TAG, "bindAppWidget: " + item);
3351 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003352 final Workspace workspace = mWorkspace;
3353
3354 final int appWidgetId = item.appWidgetId;
3355 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003356 if (DEBUG_WIDGETS) {
3357 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3358 }
3359
Joe Onorato9c1289c2009-08-17 11:03:03 -04003360 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3361
Joe Onorato9c1289c2009-08-17 11:03:03 -04003362 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003363 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003364
Winson Chung3d503fb2011-07-13 17:25:49 -07003365 workspace.addInScreen(item.hostView, item.container, item.screen, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003366 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003367 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3368
Joe Onorato9c1289c2009-08-17 11:03:03 -04003369 workspace.requestLayout();
3370
Daniel Sandler843e8602010-06-07 14:59:01 -04003371 if (DEBUG_WIDGETS) {
3372 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3373 + (SystemClock.uptimeMillis()-start) + "ms");
3374 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003375 }
3376
Adam Cohen1462de32012-07-24 22:34:36 -07003377 public void onPageBoundSynchronously(int page) {
3378 mSynchronouslyBoundPages.add(page);
3379 }
3380
Joe Onorato9c1289c2009-08-17 11:03:03 -04003381 /**
3382 * Callback saying that there aren't any more items to bind.
3383 *
3384 * Implementation of the method from LauncherModel.Callbacks.
3385 */
3386 public void finishBindingItems() {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003387 setLoadOnResume();
3388
Joe Onorato9c1289c2009-08-17 11:03:03 -04003389 if (mSavedState != null) {
3390 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003391 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003392 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003393 mSavedState = null;
3394 }
3395
Adam Cohen1462de32012-07-24 22:34:36 -07003396 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003397
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003398 // If we received the result of any pending adds while the loader was running (e.g. the
3399 // widget configuration forced an orientation change), process them now.
3400 for (int i = 0; i < sPendingAddList.size(); i++) {
3401 completeAdd(sPendingAddList.get(i));
3402 }
3403 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003404
Winson Chungc51db6a2011-10-05 11:44:49 -07003405 // Update the market app icon as necessary (the other icons will be managed in response to
3406 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003407 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003408
Winson Chungf0c6ae02012-03-21 16:10:31 -07003409 // Animate up any icons as necessary
3410 if (mVisible || mWorkspaceLoading) {
3411 Runnable newAppsRunnable = new Runnable() {
3412 @Override
3413 public void run() {
Winson Chunga2413752012-04-03 14:22:34 -07003414 runNewAppsAnimation(false);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003415 }
3416 };
Winson Chunga2413752012-04-03 14:22:34 -07003417
3418 boolean willSnapPage = mNewShortcutAnimatePage > -1 &&
3419 mNewShortcutAnimatePage != mWorkspace.getCurrentPage();
3420 if (canRunNewAppsAnimation()) {
3421 // If the user has not interacted recently, then either snap to the new page to show
3422 // the new-apps animation or just run them if they are to appear on the current page
3423 if (willSnapPage) {
3424 mWorkspace.snapToPage(mNewShortcutAnimatePage, newAppsRunnable);
3425 } else {
3426 runNewAppsAnimation(false);
3427 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003428 } else {
Winson Chung318eee02012-04-12 10:59:27 -07003429 // If the user has interacted recently, then just add the items in place if they
Winson Chunga2413752012-04-03 14:22:34 -07003430 // are on another page (or just normally if they are added to the current page)
3431 runNewAppsAnimation(willSnapPage);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003432 }
3433 }
3434
3435 mWorkspaceLoading = false;
3436 }
3437
Winson Chunga2413752012-04-03 14:22:34 -07003438 private boolean canRunNewAppsAnimation() {
3439 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3440 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3441 }
3442
Winson Chungf0c6ae02012-03-21 16:10:31 -07003443 /**
3444 * Runs a new animation that scales up icons that were added while Launcher was in the
3445 * background.
Winson Chunga2413752012-04-03 14:22:34 -07003446 *
3447 * @param immediate whether to run the animation or show the results immediately
Winson Chungf0c6ae02012-03-21 16:10:31 -07003448 */
Winson Chunga2413752012-04-03 14:22:34 -07003449 private void runNewAppsAnimation(boolean immediate) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003450 AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003451 Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chunga2413752012-04-03 14:22:34 -07003452
3453 // Order these new views spatially so that they animate in order
Winson Chungf0c6ae02012-03-21 16:10:31 -07003454 Collections.sort(mNewShortcutAnimateViews, new Comparator<View>() {
3455 @Override
3456 public int compare(View a, View b) {
3457 CellLayout.LayoutParams alp = (CellLayout.LayoutParams) a.getLayoutParams();
3458 CellLayout.LayoutParams blp = (CellLayout.LayoutParams) b.getLayoutParams();
3459 int cellCountX = LauncherModel.getCellCountX();
3460 return (alp.cellY * cellCountX + alp.cellX) - (blp.cellY * cellCountX + blp.cellX);
3461 }
3462 });
Winson Chunga2413752012-04-03 14:22:34 -07003463
3464 // Animate each of the views in place (or show them immediately if requested)
3465 if (immediate) {
3466 for (View v : mNewShortcutAnimateViews) {
3467 v.setAlpha(1f);
3468 v.setScaleX(1f);
3469 v.setScaleY(1f);
Winson Chungf0c6ae02012-03-21 16:10:31 -07003470 }
Winson Chunga2413752012-04-03 14:22:34 -07003471 } else {
3472 for (int i = 0; i < mNewShortcutAnimateViews.size(); ++i) {
3473 View v = mNewShortcutAnimateViews.get(i);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003474 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
Winson Chunga2413752012-04-03 14:22:34 -07003475 PropertyValuesHolder.ofFloat("alpha", 1f),
3476 PropertyValuesHolder.ofFloat("scaleX", 1f),
3477 PropertyValuesHolder.ofFloat("scaleY", 1f));
3478 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3479 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3480 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3481 bounceAnims.add(bounceAnim);
3482 }
3483 anim.playTogether(bounceAnims);
3484 anim.addListener(new AnimatorListenerAdapter() {
3485 @Override
3486 public void onAnimationEnd(Animator animation) {
3487 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
3488 }
3489 });
3490 anim.start();
3491 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003492
3493 // Clean up
3494 mNewShortcutAnimatePage = -1;
3495 mNewShortcutAnimateViews.clear();
3496 new Thread("clearNewAppsThread") {
3497 public void run() {
3498 mSharedPrefs.edit()
3499 .putInt(InstallShortcutReceiver.NEW_APPS_PAGE_KEY, -1)
3500 .putStringSet(InstallShortcutReceiver.NEW_APPS_LIST_KEY, null)
3501 .commit();
3502 }
3503 }.start();
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003504 }
3505
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003506 @Override
3507 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003508 boolean searchVisible = updateGlobalSearchIcon();
3509 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003510 if (mSearchDropTargetBar != null) {
3511 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3512 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003513 }
3514
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003515 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003516 * Add the icons for all apps.
3517 *
3518 * Implementation of the method from LauncherModel.Callbacks.
3519 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003520 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003521 Runnable setAllAppsRunnable = new Runnable() {
3522 public void run() {
3523 if (mAppsCustomizeContent != null) {
3524 mAppsCustomizeContent.setApps(apps);
3525 }
3526 }
3527 };
3528
Michael Jurkac57b7a82011-08-09 22:02:20 -07003529 // Remove the progress bar entirely; we could also make it GONE
3530 // but better to remove it since we know it's not going to be used
3531 View progressBar = mAppsCustomizeTabHost.
3532 findViewById(R.id.apps_customize_progress_bar);
3533 if (progressBar != null) {
3534 ((ViewGroup)progressBar.getParent()).removeView(progressBar);
Winson Chungc93e5ae2012-07-23 20:48:26 -07003535
3536 // We just post the call to setApps so the user sees the progress bar
3537 // disappear-- otherwise, it just looks like the progress bar froze
3538 // which doesn't look great
3539 mAppsCustomizeTabHost.post(setAllAppsRunnable);
3540 } else {
3541 // If we did not initialize the spinner in onCreate, then we can directly set the
3542 // list of applications without waiting for any progress bars views to be hidden.
3543 setAllAppsRunnable.run();
Winson Chung785d2eb2011-04-14 16:08:02 -07003544 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003545 }
3546
3547 /**
3548 * A package was installed.
3549 *
3550 * Implementation of the method from LauncherModel.Callbacks.
3551 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003552 public void bindAppsAdded(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003553 setLoadOnResume();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003554
Winson Chung785d2eb2011-04-14 16:08:02 -07003555 if (mAppsCustomizeContent != null) {
3556 mAppsCustomizeContent.addApps(apps);
3557 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003558 }
3559
3560 /**
3561 * A package was updated.
3562 *
3563 * Implementation of the method from LauncherModel.Callbacks.
3564 */
Joe Onorato64e6be72010-03-05 15:05:52 -05003565 public void bindAppsUpdated(ArrayList<ApplicationInfo> apps) {
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003566 setLoadOnResume();
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003567 if (mWorkspace != null) {
3568 mWorkspace.updateShortcuts(apps);
3569 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003570
Winson Chung785d2eb2011-04-14 16:08:02 -07003571 if (mAppsCustomizeContent != null) {
3572 mAppsCustomizeContent.updateApps(apps);
3573 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003574 }
3575
3576 /**
3577 * A package was uninstalled.
3578 *
3579 * Implementation of the method from LauncherModel.Callbacks.
3580 */
Winson Chungcd810732012-06-18 16:45:43 -07003581 public void bindAppsRemoved(ArrayList<String> packageNames, boolean permanent) {
Joe Onorato36115782010-06-17 13:28:48 -04003582 if (permanent) {
Winson Chungcd810732012-06-18 16:45:43 -07003583 mWorkspace.removeItems(packageNames);
Joe Onorato36115782010-06-17 13:28:48 -04003584 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003585
Winson Chung785d2eb2011-04-14 16:08:02 -07003586 if (mAppsCustomizeContent != null) {
Winson Chungcd810732012-06-18 16:45:43 -07003587 mAppsCustomizeContent.removeApps(packageNames);
Winson Chung785d2eb2011-04-14 16:08:02 -07003588 }
Winson Chunga1820962011-10-03 16:31:06 -07003589
3590 // Notify the drag controller
Winson Chungcd810732012-06-18 16:45:43 -07003591 mDragController.onAppsRemoved(packageNames, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003592 }
Joe Onoratobe386092009-11-17 17:32:16 -08003593
3594 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003595 * A number of packages were updated.
3596 */
3597 public void bindPackagesUpdated() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003598 if (mAppsCustomizeContent != null) {
3599 mAppsCustomizeContent.onPackagesUpdated();
3600 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003601 }
3602
Winson Chung400438b2011-01-16 17:53:48 -08003603 private int mapConfigurationOriActivityInfoOri(int configOri) {
3604 final Display d = getWindowManager().getDefaultDisplay();
3605 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3606 switch (d.getRotation()) {
3607 case Surface.ROTATION_0:
3608 case Surface.ROTATION_180:
3609 // We are currently in the same basic orientation as the natural orientation
3610 naturalOri = configOri;
3611 break;
3612 case Surface.ROTATION_90:
3613 case Surface.ROTATION_270:
3614 // We are currently in the other basic orientation to the natural orientation
3615 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3616 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3617 break;
3618 }
3619
3620 int[] oriMap = {
3621 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3622 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3623 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3624 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3625 };
3626 // Since the map starts at portrait, we need to offset if this device's natural orientation
3627 // is landscape.
3628 int indexOffset = 0;
3629 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3630 indexOffset = 1;
3631 }
3632 return oriMap[(d.getRotation() + indexOffset) % 4];
3633 }
Adam Cohen446e9402011-09-15 18:21:21 -07003634
Winson Chung4b919f82012-05-01 10:44:08 -07003635 public boolean isRotationEnabled() {
Michael Jurka9bc8eba2012-05-21 20:36:44 -07003636 boolean forceEnableRotation = doesFileExist(FORCE_ENABLE_ROTATION_PROPERTY);
Winson Chung4b919f82012-05-01 10:44:08 -07003637 boolean enableRotation = forceEnableRotation ||
3638 getResources().getBoolean(R.bool.allow_rotation);
3639 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003640 }
Winson Chung4b919f82012-05-01 10:44:08 -07003641 public void lockScreenOrientation() {
3642 if (isRotationEnabled()) {
3643 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3644 .getConfiguration().orientation));
3645 }
3646 }
3647 public void unlockScreenOrientation(boolean immediate) {
3648 if (isRotationEnabled()) {
3649 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003650 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003651 } else {
3652 mHandler.postDelayed(new Runnable() {
3653 public void run() {
3654 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3655 }
3656 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003657 }
Winson Chung4b919f82012-05-01 10:44:08 -07003658 }
Winson Chung400438b2011-01-16 17:53:48 -08003659 }
3660
Winson Chung82f55532011-08-09 14:14:23 -07003661 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003662 private boolean isClingsEnabled() {
Brett Chabot2a9e2282011-08-23 15:03:13 -07003663 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003664 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003665
Winson Chung7d7541e2011-09-16 20:14:36 -07003666 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003667 }
Michael Jurka22143132012-10-04 17:42:38 +02003668
Winson Chung7d7541e2011-09-16 20:14:36 -07003669 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003670 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003671 if (cling != null) {
3672 cling.init(this, positionData);
3673 cling.setVisibility(View.VISIBLE);
3674 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3675 if (animate) {
3676 cling.buildLayer();
3677 cling.setAlpha(0f);
3678 cling.animate()
3679 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003680 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003681 .setDuration(SHOW_CLING_DURATION)
3682 .setStartDelay(delay)
3683 .start();
3684 } else {
3685 cling.setAlpha(1f);
3686 }
Michael Jurka22143132012-10-04 17:42:38 +02003687 cling.setFocusableInTouchMode(true);
3688 cling.post(new Runnable() {
3689 public void run() {
3690 cling.setFocusable(true);
3691 cling.requestFocus();
3692 }
3693 });
3694 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3695 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003696 }
3697 return cling;
3698 }
Michael Jurka22143132012-10-04 17:42:38 +02003699
Winson Chung7d7541e2011-09-16 20:14:36 -07003700 private void dismissCling(final Cling cling, final String flag, int duration) {
Michael Jurka22143132012-10-04 17:42:38 +02003701 if (cling != null && cling.getVisibility() == View.VISIBLE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003702 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003703 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003704 anim.addListener(new AnimatorListenerAdapter() {
3705 public void onAnimationEnd(Animator animation) {
3706 cling.setVisibility(View.GONE);
3707 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003708 // We should update the shared preferences on a background thread
3709 new Thread("dismissClingThread") {
3710 public void run() {
3711 SharedPreferences.Editor editor = mSharedPrefs.edit();
3712 editor.putBoolean(flag, true);
3713 editor.commit();
3714 }
3715 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07003716 };
3717 });
3718 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02003719 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07003720 }
3721 }
Michael Jurka22143132012-10-04 17:42:38 +02003722
Winson Chung9d9d74f2011-09-19 11:49:12 -07003723 private void removeCling(int id) {
3724 final View cling = findViewById(id);
3725 if (cling != null) {
3726 final ViewGroup parent = (ViewGroup) cling.getParent();
3727 parent.post(new Runnable() {
3728 @Override
3729 public void run() {
3730 parent.removeView(cling);
3731 }
3732 });
Michael Jurka22143132012-10-04 17:42:38 +02003733 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003734 }
3735 }
Michael Jurka974c3862012-05-22 22:00:31 -07003736
3737 private boolean skipCustomClingIfNoAccounts() {
3738 Cling cling = (Cling) findViewById(R.id.workspace_cling);
3739 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
3740 if (customCling) {
3741 AccountManager am = AccountManager.get(this);
3742 Account[] accounts = am.getAccountsByType("com.google");
3743 return accounts.length == 0;
3744 }
3745 return false;
3746 }
3747
Winson Chung7d7541e2011-09-16 20:14:36 -07003748 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003749 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003750 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07003751 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
3752 !skipCustomClingIfNoAccounts() ) {
Michael Jurka68255cb2012-10-05 15:02:00 +02003753 // If we're not using the default workspace layout, replace workspace cling
3754 // with a custom workspace cling (usually specified in an overlay)
3755 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0) {
3756 // Use a custom cling
3757 View cling = findViewById(R.id.workspace_cling);
3758 ViewGroup clingParent = (ViewGroup) cling.getParent();
3759 int clingIndex = clingParent.indexOfChild(cling);
3760 clingParent.removeViewAt(clingIndex);
3761 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
3762 clingParent.addView(customCling, clingIndex);
3763 customCling.setId(R.id.workspace_cling);
3764 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003765 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003766 } else {
3767 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003768 }
3769 }
3770 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003771 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003772 if (isClingsEnabled() &&
3773 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07003774 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003775 } else {
3776 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003777 }
3778 }
3779 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07003780 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08003781 if (isClingsEnabled() &&
3782 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
3783 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003784 } else {
3785 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08003786 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07003787 }
Winson Chung7d7541e2011-09-16 20:14:36 -07003788 }
3789 public boolean isFolderClingVisible() {
3790 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07003791 if (cling != null) {
3792 return cling.getVisibility() == View.VISIBLE;
3793 }
3794 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07003795 }
Winson Chung82f55532011-08-09 14:14:23 -07003796 public void dismissWorkspaceCling(View v) {
3797 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003798 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003799 }
3800 public void dismissAllAppsCling(View v) {
3801 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003802 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
3803 }
3804 public void dismissFolderCling(View v) {
3805 Cling cling = (Cling) findViewById(R.id.folder_cling);
3806 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07003807 }
3808
Winson Chung80baf5a2010-08-09 16:03:15 -07003809 /**
Joe Onoratobe386092009-11-17 17:32:16 -08003810 * Prints out out state for debugging.
3811 */
3812 public void dumpState() {
3813 Log.d(TAG, "BEGIN launcher2 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08003814 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08003815 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
3816 Log.d(TAG, "mRestoring=" + mRestoring);
3817 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
3818 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003819 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08003820 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07003821
Winson Chung785d2eb2011-04-14 16:08:02 -07003822 if (mAppsCustomizeContent != null) {
3823 mAppsCustomizeContent.dumpState();
3824 }
Joe Onoratobe386092009-11-17 17:32:16 -08003825 Log.d(TAG, "END launcher2 dump state");
3826 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07003827
3828 @Override
3829 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
3830 super.dump(prefix, fd, writer, args);
3831 writer.println(" ");
3832 writer.println("Debug logs: ");
3833 for (int i = 0; i < sDumpLogs.size(); i++) {
3834 writer.println(" " + sDumpLogs.get(i));
3835 }
3836 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07003837
3838 public static void dumpDebugLogsToConsole() {
3839 Log.d(TAG, "");
3840 Log.d(TAG, "*********************");
3841 Log.d(TAG, "Launcher debug logs: ");
3842 for (int i = 0; i < sDumpLogs.size(); i++) {
3843 Log.d(TAG, " " + sDumpLogs.get(i));
3844 }
3845 Log.d(TAG, "*********************");
3846 Log.d(TAG, "");
3847 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003848}
Michael Jurka2763be32011-02-24 11:19:57 -08003849
Michael Jurkaabded662011-03-04 12:06:57 -08003850interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003851 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003852 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08003853 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08003854 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08003855 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08003856}