blob: 8fa2d35a30f41f00f18b0a341e91caf67db44123 [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
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
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;
Adam Cohen96d30a12013-07-16 18:13:21 -070053import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070055import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040057import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070058import android.os.AsyncTask;
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;
Adam Cohen96d30a12013-07-16 18:13:21 -070072import android.view.Display;
73import android.view.Gravity;
74import android.view.HapticFeedbackConstants;
75import android.view.KeyEvent;
76import android.view.LayoutInflater;
77import android.view.Menu;
78import android.view.MenuItem;
79import android.view.MotionEvent;
80import android.view.Surface;
81import android.view.View;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080082import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070083import android.view.ViewGroup;
84import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080085import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080087import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070088import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070089import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080090import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
95import android.widget.TextView;
96import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070097
Daniel Sandler325dc232013-06-05 22:57:57 -040098import com.android.launcher3.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;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700103import java.io.FileNotFoundException;
104import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700105import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700106import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700107import java.util.Collection;
108import java.util.Collections;
109import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700111import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700112import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700113import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800115/**
116 * Default launcher application.
117 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100118public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700119 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700120 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800121 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700122 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800123
Daniel Sandlerf061f822013-06-27 13:59:36 -0400124 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400125 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700126 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400127 static final boolean DEBUG_RESUME_TIME = 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
Mathew Inwood876a8462013-06-14 14:12:41 +0100144 /**
145 * IntentStarter uses request codes starting with this. This must be greater than all activity
146 * request codes used internally.
147 */
148 protected static final int REQUEST_LAST = 100;
149
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
151
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800152 static final int SCREEN_COUNT = 5;
153 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Romain Guy98d01652009-06-30 16:21:04 -0700155 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800156 // To turn on these properties, type
157 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
158 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
159 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800160
Winson Chung2672ff92012-05-04 16:22:30 -0700161 // The Intent extra that defines whether to ignore the launch animation
162 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400163 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700164
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800165 // Type: int
166 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700167 // Type: int
168 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700170 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
171 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
173 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700174 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700176 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: boolean
178 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
179 // Type: long
180 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700181 // Type: int
182 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
183 // Type: int
184 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
185 // Type: parcelable
186 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100188 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700189 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100190 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700191 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100192 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700193
Adam Cohen39a06042013-07-19 14:30:12 -0700194 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700195 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700196
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700197 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700198 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700199 private State mState = State.WORKSPACE;
200 private AnimatorSet mStateAnimation;
Michael Jurkab737ee62011-11-15 15:57:22 -0800201 private AnimatorSet mDividerAnimator;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700202
Joe Onorato9c1289c2009-08-17 11:03:03 -0400203 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700204 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
205 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700206 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700207 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800208
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800210 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800211
Winson Chunga2413752012-04-03 14:22:34 -0700212 // How long to wait before the new-shortcut animation automatically pans the workspace
213 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700214 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700215
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800216 private final BroadcastReceiver mCloseSystemDialogsReceiver
217 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800218 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
219
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800220 private LayoutInflater mInflater;
221
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800222 private Workspace mWorkspace;
Michael Jurkab737ee62011-11-15 15:57:22 -0800223 private View mQsbDivider;
Craig Mautner360310b2012-10-26 15:13:08 -0700224 private View mLauncherView;
Michael Jurkab737ee62011-11-15 15:57:22 -0800225 private DragLayer mDragLayer;
226 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700227 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700228
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700229 private AppWidgetManager mAppWidgetManager;
230 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700231
Michael Jurkac9d95c52011-08-29 14:03:34 -0700232 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700233 private AppWidgetProviderInfo mPendingAddWidgetInfo;
234
Michael Jurka0280c3b2010-09-17 15:00:07 -0700235 private int[] mTmpAddItemCellCoordinates = new int[2];
236
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237 private FolderInfo mFolderInfo;
238
Winson Chung3d503fb2011-07-13 17:25:49 -0700239 private Hotseat mHotseat;
Michael Jurka838a4ca2011-02-07 13:33:06 -0800240 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700241
Winson Chungc51db6a2011-10-05 11:44:49 -0700242 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700243 private AppsCustomizeTabHost mAppsCustomizeTabHost;
244 private AppsCustomizePagedView mAppsCustomizeContent;
245 private boolean mAutoAdvanceRunning = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800246
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700248 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
249 // scroll issues (because the workspace may not have been measured yet) and extra work.
250 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
251 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252
253 private SpannableStringBuilder mDefaultKeySsb = null;
254
Joe Onorato9c1289c2009-08-17 11:03:03 -0400255 private boolean mWorkspaceLoading = true;
256
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800257 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 private boolean mRestoring;
259 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700260 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261
Michael Jurka1e2f4652013-07-08 18:03:46 -0700262 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700263 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
264
Winson Chung4a2afa32012-07-19 14:53:05 -0700265 // Keep track of whether the user has left launcher
266 private static boolean sPausedFromUserAction = false;
267
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 private Bundle mSavedInstanceState;
269
Joe Onorato9c1289c2009-08-17 11:03:03 -0400270 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800271 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800272 private boolean mUserPresent = true;
273 private boolean mVisible = false;
274 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700275 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400276
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700277 private static LocaleConfiguration sLocaleConfiguration = null;
278
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700279 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700280
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700281 private Intent mAppMarketIntent = null;
282
Adam Cohended9f8d2010-11-03 13:25:16 -0700283 // Related to the auto-advancing of widgets
284 private final int ADVANCE_MSG = 1;
285 private final int mAdvanceInterval = 20000;
286 private final int mAdvanceStagger = 250;
287 private long mAutoAdvanceSentTime;
288 private long mAutoAdvanceTimeLeft = -1;
289 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
290 new HashMap<View, AppWidgetProviderInfo>();
291
Winson Chung400438b2011-01-16 17:53:48 -0800292 // Determines how long to wait after a rotation before restoring the screen orientation to
293 // match the sensor state.
294 private final int mRestoreScreenOrientationDelay = 500;
295
Michael Jurka4ef207b2010-11-29 17:05:45 -0800296 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700297 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
298 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
299 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800300
Craig Mautner360310b2012-10-26 15:13:08 -0700301 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700302
Adam Cohen1462de32012-07-24 22:34:36 -0700303 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
304
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700305 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
306
Winson Chung46353de2012-02-16 14:05:10 -0800307 // We only want to get the SharedPreferences once since it does an FS stat each time we get
308 // it from the context.
309 private SharedPreferences mSharedPrefs;
310
Adam Cohene25af792013-06-06 23:08:25 -0700311 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
312
Winson Chungf0c6ae02012-03-21 16:10:31 -0700313 // Holds the page that we need to animate to, and the icon views that we need to animate up
314 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700315 private ImageView mFolderIconImageView;
316 private Bitmap mFolderIconBitmap;
317 private Canvas mFolderIconCanvas;
318 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700319
Michael Jurkaddd62e92011-02-16 17:49:14 -0800320 private BubbleTextView mWaitingForResume;
321
Michael Jurka22143132012-10-04 17:42:38 +0200322 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
323 = new HideFromAccessibilityHelper();
324
Michael Jurkac1f5d262011-09-30 19:32:27 -0700325 private Runnable mBuildLayersRunnable = new Runnable() {
326 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700327 if (mWorkspace != null) {
328 mWorkspace.buildPageHardwareLayers();
329 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700330 }
331 };
332
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800333 private static ArrayList<PendingAddArguments> sPendingAddList
334 = new ArrayList<PendingAddArguments>();
335
Michael Jurka0a457bf2012-11-19 14:05:05 -0800336 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
337
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800338 private static class PendingAddArguments {
339 int requestCode;
340 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700341 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700342 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800343 int cellX;
344 int cellY;
345 }
346
Daniel Sandlerff02d492013-08-05 02:12:05 -0400347 private Stats mStats;
348
Michael Jurka0a457bf2012-11-19 14:05:05 -0800349 private static boolean isPropertyEnabled(String propertyName) {
350 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700351 }
352
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800353 @Override
354 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700355 if (DEBUG_STRICT_MODE) {
356 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
357 .detectDiskReads()
358 .detectDiskWrites()
359 .detectNetwork() // or .detectAll() for all detectable problems
360 .penaltyLog()
361 .build());
362 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
363 .detectLeakedSqlLiteObjects()
364 .detectLeakedClosableObjects()
365 .penaltyLog()
366 .penaltyDeath()
367 .build());
368 }
369
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800370 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400371
372 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
373 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400374 LauncherAppState app = LauncherAppState.getInstance();
375 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700376 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800377 mModel = app.setLauncher(this);
378 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400379 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800380 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700381
Daniel Sandlerff02d492013-08-05 02:12:05 -0400382 mStats = new Stats(this);
383
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700384 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700385 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
386 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700387
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700388 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
389 // this also ensures that any synchronous binding below doesn't re-trigger another
390 // LauncherModel load.
391 mPaused = false;
392
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800393 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200394 android.os.Debug.startMethodTracing(
395 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800396 }
397
398 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 setContentView(R.layout.launcher);
400 setupViews();
Winson Chung7d7541e2011-09-16 20:14:36 -0700401 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800402
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800403 registerContentObservers();
404
Joe Onorato7c312c12009-08-13 21:36:53 -0700405 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700406
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 mSavedState = savedInstanceState;
408 restoreState(mSavedState);
409
Winson Chunga12a2502010-12-20 14:41:35 -0800410 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700411 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200412 mAppsCustomizeContent.onPackagesUpdated(
413 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700414 }
Winson Chunga12a2502010-12-20 14:41:35 -0800415
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 if (PROFILE_STARTUP) {
417 android.os.Debug.stopMethodTracing();
418 }
419
420 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700421 if (sPausedFromUserAction) {
422 // If the user leaves launcher, then we should just load items asynchronously when
423 // they return.
424 mModel.startLoader(true, -1);
425 } else {
426 // We only load the page synchronously if the user rotates (or triggers a
427 // configuration change) while launcher is in the foreground
428 mModel.startLoader(true, mWorkspace.getCurrentPage());
429 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 }
431
432 // For handling default keys
433 mDefaultKeySsb = new SpannableStringBuilder();
434 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800435
436 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
437 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800438
Adam Cohenf9426d52012-06-04 17:26:21 -0700439 updateGlobalIcons();
440
441 // On large interfaces, we want the screen to auto-rotate based on the current orientation
442 unlockScreenOrientation(true);
443 }
444
Winson Chung4a2afa32012-07-19 14:53:05 -0700445 protected void onUserLeaveHint() {
446 super.onUserLeaveHint();
447 sPausedFromUserAction = true;
448 }
449
Winson Chung98ca0f72013-07-29 12:58:51 -0700450 /** To be overriden by subclasses to hint to Launcher that we have custom content */
451 protected boolean hasCustomContentToLeft() {
452 return false;
453 }
454
Adam Cohenf9426d52012-06-04 17:26:21 -0700455 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700456 boolean searchVisible = false;
457 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800458 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700459 int coi = getCurrentOrientationIndexForGlobalIcons();
460 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
461 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700462 updateAppMarketIcon();
463 searchVisible = updateGlobalSearchIcon();
464 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700465 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700466 if (sGlobalSearchIcon[coi] != null) {
467 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700468 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700469 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700470 if (sVoiceSearchIcon[coi] != null) {
471 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700472 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700473 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700474 if (sAppMarketIcon[coi] != null) {
475 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800476 }
Winson Chungadf0c182012-08-23 14:59:07 -0700477 if (mSearchDropTargetBar != null) {
478 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
479 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 }
Romain Guycbb89e42009-06-08 15:52:54 -0700481
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700483 if (sLocaleConfiguration == null) {
484 new AsyncTask<Void, Void, LocaleConfiguration>() {
485 @Override
486 protected LocaleConfiguration doInBackground(Void... unused) {
487 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
488 readConfiguration(Launcher.this, localeConfiguration);
489 return localeConfiguration;
490 }
491
492 @Override
493 protected void onPostExecute(LocaleConfiguration result) {
494 sLocaleConfiguration = result;
495 checkForLocaleChange(); // recursive, but now with a locale configuration
496 }
497 }.execute();
498 return;
499 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700500
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501 final Configuration configuration = getResources().getConfiguration();
502
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700503 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800504 final String locale = configuration.locale.toString();
505
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700506 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800507 final int mcc = configuration.mcc;
508
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700509 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510 final int mnc = configuration.mnc;
511
Romain Guy84f296c2009-11-04 15:00:44 -0800512 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800513
Romain Guy84f296c2009-11-04 15:00:44 -0800514 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700515 sLocaleConfiguration.locale = locale;
516 sLocaleConfiguration.mcc = mcc;
517 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700518
Joe Onorato0589f0f2010-02-08 13:44:00 -0800519 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700520
521 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
522 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700523 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700524 public void run() {
525 writeConfiguration(Launcher.this, localeConfiguration);
526 }
527 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700528 }
529 }
530
531 private static class LocaleConfiguration {
532 public String locale;
533 public int mcc = -1;
534 public int mnc = -1;
535 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700536
Romain Guy98d01652009-06-30 16:21:04 -0700537 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
538 DataInputStream in = null;
539 try {
540 in = new DataInputStream(context.openFileInput(PREFERENCES));
541 configuration.locale = in.readUTF();
542 configuration.mcc = in.readInt();
543 configuration.mnc = in.readInt();
544 } catch (FileNotFoundException e) {
545 // Ignore
546 } catch (IOException e) {
547 // Ignore
548 } finally {
549 if (in != null) {
550 try {
551 in.close();
552 } catch (IOException e) {
553 // Ignore
554 }
555 }
556 }
557 }
558
559 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
560 DataOutputStream out = null;
561 try {
562 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
563 out.writeUTF(configuration.locale);
564 out.writeInt(configuration.mcc);
565 out.writeInt(configuration.mnc);
566 out.flush();
567 } catch (FileNotFoundException e) {
568 // Ignore
569 } catch (IOException e) {
570 //noinspection ResultOfMethodCallIgnored
571 context.getFileStreamPath(PREFERENCES).delete();
572 } finally {
573 if (out != null) {
574 try {
575 out.close();
576 } catch (IOException e) {
577 // Ignore
578 }
579 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 }
581 }
582
Bjorn Bringertc459e522013-06-07 19:36:01 +0100583 public LayoutInflater getInflater() {
584 return mInflater;
585 }
586
Adam Cohen716b51e2011-06-30 12:09:54 -0700587 public DragLayer getDragLayer() {
588 return mDragLayer;
589 }
590
Winson Chung36a62fe2012-05-06 18:04:42 -0700591 boolean isDraggingEnabled() {
592 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
593 // that is subsequently removed from the workspace in startBinding().
594 return !mModel.isLoadingWorkspace();
595 }
596
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 static int getScreen() {
598 synchronized (sLock) {
599 return sScreen;
600 }
601 }
602
603 static void setScreen(int screen) {
604 synchronized (sLock) {
605 sScreen = screen;
606 }
607 }
608
Winson Chung557d6ed2011-07-08 15:34:52 -0700609 /**
610 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
611 * a configuration step, this allows the proper animations to run after other transitions.
612 */
613 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700614 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800615 switch (args.requestCode) {
616 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700617 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700618 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800619 break;
620 case REQUEST_PICK_SHORTCUT:
621 processShortcut(args.intent);
622 break;
623 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700624 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700625 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700626 result = true;
627 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800628 case REQUEST_CREATE_APPWIDGET:
629 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700630 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700631 result = true;
632 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800633 case REQUEST_PICK_WALLPAPER:
634 // We just wanted the activity result here so we can clear mWaitingForResult
635 break;
636 }
Michael Jurka27614d22012-04-02 06:40:22 -0700637 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
638 // if you turned the screen off and then back while in All Apps, Launcher would not
639 // return to the workspace. Clearing mAddInfo.container here fixes this issue
640 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700641 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800642 }
643
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800644 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700645 protected void onActivityResult(
646 final int requestCode, final int resultCode, final Intent data) {
647 if (requestCode == REQUEST_BIND_APPWIDGET) {
648 int appWidgetId = data != null ?
649 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
650 if (resultCode == RESULT_CANCELED) {
651 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
652 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700653 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700654 }
655 return;
656 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700657 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800658 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
659 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700660 mWaitingForResult = false;
661
Adam Cohened66b2b2012-01-23 17:28:51 -0800662 // We have special handling for widgets
663 if (isWidgetDrop) {
664 int appWidgetId = data != null ?
665 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700666 if (appWidgetId < 0) {
667 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
668 "widget configuration activity.");
669 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
670 } else {
671 completeTwoStageWidgetDrop(resultCode, appWidgetId);
672 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800673 return;
674 }
675
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 // The pattern used here is that a user PICKs a specific application,
677 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700678
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
680 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700681 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800682 final PendingAddArguments args = new PendingAddArguments();
683 args.requestCode = requestCode;
684 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700685 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700686 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700687 args.cellX = mPendingAddInfo.cellX;
688 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800689 if (isWorkspaceLocked()) {
690 sPendingAddList.add(args);
691 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700692 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800694 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800695 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700696 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800697 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
698 null);
699 }
700
701 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700702 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700703 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800704 Runnable onCompleteRunnable = null;
705 int animationType = 0;
706
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700707 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800708 if (resultCode == RESULT_OK) {
709 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
710 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700711 mPendingAddWidgetInfo);
712 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800713 onCompleteRunnable = new Runnable() {
714 @Override
715 public void run() {
716 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700717 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800718 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
719 null);
720 }
721 };
722 } else if (resultCode == RESULT_CANCELED) {
723 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
724 onCompleteRunnable = new Runnable() {
725 @Override
726 public void run() {
727 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
728 null);
729 }
730 };
731 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700732 if (mDragLayer.getAnimatedView() != null) {
733 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
734 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
735 animationType, boundWidget, true);
736 } else {
737 // The animated view may be null in the case of a rotation during widget configuration
738 onCompleteRunnable.run();
739 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800740 }
741
742 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700743 protected void onStop() {
744 super.onStop();
745 FirstFrameAnimatorHelper.setIsVisible(false);
746 }
747
748 @Override
749 protected void onStart() {
750 super.onStart();
751 FirstFrameAnimatorHelper.setIsVisible(true);
752 }
753
754 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800755 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200756 long startTime = 0;
757 if (DEBUG_RESUME_TIME) {
758 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400759 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200760 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800761 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700762
Winson Chung4a2afa32012-07-19 14:53:05 -0700763 // Restore the previous launcher state
764 if (mOnResumeState == State.WORKSPACE) {
765 showWorkspace(false);
766 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
767 showAllApps(false);
768 }
769 mOnResumeState = State.NONE;
770
Craig Mautner360310b2012-10-26 15:13:08 -0700771 // Background was set to gradient in onPause(), restore to black if in all apps.
772 setWorkspaceBackground(mState == State.WORKSPACE);
773
Winson Chungde0fb8f2012-05-08 14:37:08 -0700774 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700775 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700776
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800777 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700778 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700779 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400780 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700781 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400782 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700783 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800784 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700785 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200786 // We might have postponed some bind calls until onResume (see waitUntilResume) --
787 // execute them here
788 long startTimeCallbacks = 0;
789 if (DEBUG_RESUME_TIME) {
790 startTimeCallbacks = System.currentTimeMillis();
791 }
792
793 if (mAppsCustomizeContent != null) {
794 mAppsCustomizeContent.setBulkBind(true);
795 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700796 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
797 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200798 }
799 if (mAppsCustomizeContent != null) {
800 mAppsCustomizeContent.setBulkBind(false);
801 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700802 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200803 if (DEBUG_RESUME_TIME) {
804 Log.d(TAG, "Time spent processing callbacks in onResume: " +
805 (System.currentTimeMillis() - startTimeCallbacks));
806 }
Michael Jurka447bf842013-05-15 14:52:15 +0200807 }
Michael Jurka54554252013-08-01 12:52:23 +0200808 if (mOnResumeCallbacks.size() > 0) {
809 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
810 mOnResumeCallbacks.get(i).run();
811 }
812 mOnResumeCallbacks.clear();
813 }
Winson Chunge4e50662012-01-23 14:45:13 -0800814
815 // Reset the pressed state of icons that were locked in the press state while activities
816 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800817 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800818 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800819 mWaitingForResume.setStayPressed(false);
820 }
Winson Chunge4e50662012-01-23 14:45:13 -0800821 if (mAppsCustomizeContent != null) {
822 // Resets the previous all apps icon press state
823 mAppsCustomizeContent.resetDrawableState();
824 }
Adam Cohen06dff352012-06-01 17:17:08 -0700825 // It is possible that widgets can receive updates while launcher is not in the foreground.
826 // Consequently, the widgets will be inflated in the orientation of the foreground activity
827 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
828 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700829 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700830
831 // Again, as with the above scenario, it's possible that one or more of the global icons
832 // were updated in the wrong orientation.
833 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200834 if (DEBUG_RESUME_TIME) {
835 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
836 }
Adam Cohen06dff352012-06-01 17:17:08 -0700837 }
838
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800839 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700840 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700841 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
842 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
843 // when Launcher resumes and we are still in AllApps.
844 updateWallpaperVisibility(true);
845
Winson Chung997a9232013-07-24 15:33:46 -0700846 // Ensure that items added to Launcher are queued until Launcher returns
847 InstallShortcutReceiver.enableInstallQueue();
848
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700849 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700850 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800851 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700852 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700853 }
Romain Guycbb89e42009-06-08 15:52:54 -0700854
Adam Cohen66a01fd2013-06-11 12:48:00 -0700855 protected void onFinishBindingItems() {
856 }
857
Adam Cohenbffe7452013-07-22 18:21:45 -0700858 QSBScroller mQsbScroller = new QSBScroller() {
859 int scrollY = 0;
860
861 @Override
862 public void setScrollY(int scroll) {
863 scrollY = scroll;
864
865 if (mWorkspace.isOnOrMovingToCustomContent()) {
866 mSearchDropTargetBar.setTranslationY(- scrollY);
867 }
868 }
869 };
870
871 public void resetQSBScroll() {
872 mSearchDropTargetBar.animate().translationY(0).start();
873 }
874
875 public interface CustomContentCallbacks {
876 // Custom content is completely shown
877 public void onShow();
878
879 // Custom content is completely hidden
880 public void onHide();
881 }
882
883 public interface QSBScroller {
884 public void setScrollY(int scrollY);
885 }
886
Adam Cohen66a01fd2013-06-11 12:48:00 -0700887 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700888 public QSBScroller addToCustomContentPage(View customContent) {
889 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700890 }
891
Winson Chung98ca0f72013-07-29 12:58:51 -0700892 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700893 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700894 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700895 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700896 }
897
Adam Cohenedb40762013-07-18 16:45:45 -0700898 // The custom content needs to offset its content to account for the QSB
899 public int getTopOffsetForCustomContent() {
900 return mWorkspace.getPaddingTop();
901 }
902
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700903 @Override
904 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400905 // Flag the loader to stop early before switching
906 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700907 if (mAppsCustomizeContent != null) {
908 mAppsCustomizeContent.surrender();
909 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800910 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700911 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700912
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800913 // We can't hide the IME if it was forced open. So don't bother
914 /*
915 @Override
916 public void onWindowFocusChanged(boolean hasFocus) {
917 super.onWindowFocusChanged(hasFocus);
918
919 if (hasFocus) {
920 final InputMethodManager inputManager = (InputMethodManager)
921 getSystemService(Context.INPUT_METHOD_SERVICE);
922 WindowManager.LayoutParams lp = getWindow().getAttributes();
923 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
924 android.os.Handler()) {
925 protected void onReceiveResult(int resultCode, Bundle resultData) {
926 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
927 }
928 });
929 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
930 }
931 }
932 */
933
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800934 private boolean acceptFilter() {
935 final InputMethodManager inputManager = (InputMethodManager)
936 getSystemService(Context.INPUT_METHOD_SERVICE);
937 return !inputManager.isFullscreenMode();
938 }
939
940 @Override
941 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700942 final int uniChar = event.getUnicodeChar();
943 final boolean handled = super.onKeyDown(keyCode, event);
944 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
945 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
947 keyCode, event);
948 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700949 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700950 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700951 // showSearchDialog()
952 // If there are multiple keystrokes before the search dialog takes focus,
953 // onSearchRequested() will be called for every keystroke,
954 // but it is idempotent, so it's fine.
955 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800956 }
957 }
958
Joe Onorato8a9625e2010-01-28 15:55:35 -0800959 // Eat the long press event so the keyboard doesn't come up.
960 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
961 return true;
962 }
963
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800964 return handled;
965 }
966
Karl Rosaen138a0412009-04-23 19:00:21 -0700967 private String getTypedText() {
968 return mDefaultKeySsb.toString();
969 }
970
971 private void clearTypedText() {
972 mDefaultKeySsb.clear();
973 mDefaultKeySsb.clearSpans();
974 Selection.setSelection(mDefaultKeySsb, 0);
975 }
976
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700978 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
979 * State
980 */
981 private static State intToState(int stateOrdinal) {
982 State state = State.WORKSPACE;
983 final State[] stateValues = State.values();
984 for (int i = 0; i < stateValues.length; i++) {
985 if (stateValues[i].ordinal() == stateOrdinal) {
986 state = stateValues[i];
987 break;
988 }
989 }
990 return state;
991 }
992
993 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800994 * Restores the previous state, if it exists.
995 *
996 * @param savedState The previous state.
997 */
998 private void restoreState(Bundle savedState) {
999 if (savedState == null) {
1000 return;
1001 }
1002
Michael Jurka883f55b2010-10-21 15:47:14 -07001003 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001004 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001005 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001006 }
1007
Winson Chungf0c6ae02012-03-21 16:10:31 -07001008 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001009 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001010 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001011 }
1012
Winson Chung3d503fb2011-07-13 17:25:49 -07001013 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001014 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001015
Winson Chung3d503fb2011-07-13 17:25:49 -07001016 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1017 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001018 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001019 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1020 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001021 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1022 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1023 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001024 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 mRestoring = true;
1026 }
1027
1028 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1029 if (renameFolder) {
1030 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001031 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001032 mRestoring = true;
1033 }
Winson Chunga12a2502010-12-20 14:41:35 -08001034
Winson Chung785d2eb2011-04-14 16:08:02 -07001035 // Restore the AppsCustomize tab
1036 if (mAppsCustomizeTabHost != null) {
1037 String curTab = savedState.getString("apps_customize_currentTab");
1038 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001039 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001040 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001041 mAppsCustomizeContent.loadAssociatedPages(
1042 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001043 }
1044
Winson Chung5afbf7b2011-07-25 11:53:08 -07001045 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1046 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001047 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 }
1049
1050 /**
1051 * Finds all the views we need and configure them properly.
1052 */
1053 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001054 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001055
Craig Mautner360310b2012-10-26 15:13:08 -07001056 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001057 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1058 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001059 mQsbDivider = findViewById(R.id.qsb_divider);
Craig Mautner360310b2012-10-26 15:13:08 -07001060
1061 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1062 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001063
Winson Chung4c98d922011-05-31 16:50:48 -07001064 // Setup the drag layer
1065 mDragLayer.setup(this, dragController);
1066
Winson Chung3d503fb2011-07-13 17:25:49 -07001067 // Setup the hotseat
1068 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1069 if (mHotseat != null) {
1070 mHotseat.setup(this);
1071 }
1072
Winson Chung4c98d922011-05-31 16:50:48 -07001073 // Setup the workspace
1074 mWorkspace.setHapticFeedbackEnabled(false);
1075 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001076 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001077 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001078
Winson Chungf0ea4d32011-06-06 14:27:16 -07001079 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001080 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001081
Winson Chungf0ea4d32011-06-06 14:27:16 -07001082 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001083 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001084 mAppsCustomizeContent = (AppsCustomizePagedView)
1085 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1086 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001087
Winson Chung3d503fb2011-07-13 17:25:49 -07001088 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001089 dragController.setDragScoller(mWorkspace);
1090 dragController.setScrollView(mDragLayer);
1091 dragController.setMoveTarget(mWorkspace);
1092 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001093 if (mSearchDropTargetBar != null) {
1094 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001095 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001096
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001097 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001098 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001099 mWeightWatcher = new WeightWatcher(this);
1100 mWeightWatcher.setAlpha(0.5f);
1101 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001102 new FrameLayout.LayoutParams(
1103 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001104 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001105 Gravity.BOTTOM)
1106 );
Adam Cohen39a06042013-07-19 14:30:12 -07001107
1108 boolean show = shouldShowWeightWatcher();
1109 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001110 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001111 }
1112
1113 /**
1114 * Creates a view representing a shortcut.
1115 *
1116 * @param info The data structure describing the shortcut.
1117 *
1118 * @return A View inflated from R.layout.application.
1119 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001120 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001121 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001122 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001123 }
1124
1125 /**
1126 * Creates a view representing a shortcut inflated from the specified resource.
1127 *
1128 * @param layoutResId The id of the XML layout used to create the shortcut.
1129 * @param parent The group the shortcut belongs to.
1130 * @param info The data structure describing the shortcut.
1131 *
1132 * @return A View inflated from layoutResId.
1133 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001134 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001135 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1136 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001138 return favorite;
1139 }
1140
1141 /**
1142 * Add an application shortcut to the workspace.
1143 *
1144 * @param data The intent describing the application.
1145 * @param cellInfo The position on screen where to create the shortcut.
1146 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001147 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001148 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001149 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001150
Adam Cohenfbba09b2011-07-18 21:43:05 -07001151 // First we check if we already know the exact location where we want to add this item.
1152 if (cellX >= 0 && cellY >= 0) {
1153 cellXY[0] = cellX;
1154 cellXY[1] = cellY;
1155 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001156 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001157 return;
1158 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001160 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001161
Romain Guy73b979d2009-06-09 12:57:21 -07001162 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001163 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001164 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001165 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001166 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001167 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001168 } else {
1169 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 }
1171 }
Romain Guycbb89e42009-06-08 15:52:54 -07001172
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001173 /**
1174 * Add a shortcut to the workspace.
1175 *
1176 * @param data The intent describing the shortcut.
1177 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001178 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001179 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001180 int cellY) {
1181 int[] cellXY = mTmpAddItemCellCoordinates;
1182 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001183 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001184
Michael Jurkad3ef3062010-11-23 16:23:58 -08001185 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001186
Adam Cohen558baaf2011-08-15 15:22:57 -07001187 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001188 if (info == null) {
1189 return;
1190 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001191 final View view = createShortcut(info);
1192
Adam Cohenfbba09b2011-07-18 21:43:05 -07001193 // First we check if we already know the exact location where we want to add this item.
1194 if (cellX >= 0 && cellY >= 0) {
1195 cellXY[0] = cellX;
1196 cellXY[1] = cellY;
1197 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001198
1199 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001200 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001201 true, null,null)) {
1202 return;
1203 }
1204 DragObject dragObject = new DragObject();
1205 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001206 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1207 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001208 return;
1209 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001210 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001211 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001212 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001213 foundCellSpan = (result != null);
1214 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001215 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001216 }
1217
1218 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001219 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001220 return;
1221 }
1222
Adam Cohendcd297f2013-06-18 13:13:40 -07001223 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001224
1225 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001226 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001227 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 }
1229 }
1230
Adam Cohen2f093b62012-04-30 18:59:53 -07001231 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1232 int minHeight) {
1233 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001234 // We want to account for the extra amount of padding that we are adding to the widget
1235 // to ensure that it gets the full amount of space that it has requested
1236 int requiredWidth = minWidth + padding.left + padding.right;
1237 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2f093b62012-04-30 18:59:53 -07001238 return CellLayout.rectToCell(context.getResources(), requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001239 }
1240
Adam Cohen2f093b62012-04-30 18:59:53 -07001241 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1242 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001243 }
1244
Adam Cohen2f093b62012-04-30 18:59:53 -07001245 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1246 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001247 }
1248
Adam Cohen2f093b62012-04-30 18:59:53 -07001249 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1250 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001251 }
1252
Adam Cohen2f093b62012-04-30 18:59:53 -07001253 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1254 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001255 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001256 }
1257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001259 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001260 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001261 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001262 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001263 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001264 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001265 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1266 if (appWidgetInfo == null) {
1267 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1268 }
Romain Guycbb89e42009-06-08 15:52:54 -07001269
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001270 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001271 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001272
Adam Cohen2f093b62012-04-30 18:59:53 -07001273 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1274 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001275
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001277 // We have saved the position to which the widget was dragged-- this really only matters
1278 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001279 int[] cellXY = mTmpAddItemCellCoordinates;
1280 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001281 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001282 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001283 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1284 cellXY[0] = mPendingAddInfo.cellX;
1285 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001286 spanXY[0] = mPendingAddInfo.spanX;
1287 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001288 foundCellSpan = true;
1289 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001290 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001291 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001292 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1293 spanXY[1], cellXY, finalSpan);
1294 spanXY[0] = finalSpan[0];
1295 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001296 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001297 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001298 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001299 }
1300
Michael Jurka0280c3b2010-09-17 15:00:07 -07001301 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001302 if (appWidgetId != -1) {
1303 // Deleting an app widget ID is a void call but writes to disk before returning
1304 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001305 new Thread("deleteAppWidgetId") {
1306 public void run() {
1307 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1308 }
1309 }.start();
1310 }
Winson Chung93eef082012-03-23 15:59:27 -07001311 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001312 return;
1313 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001314
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001315 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001316 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1317 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001318 launcherInfo.spanX = spanXY[0];
1319 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001320 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1321 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001322
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001324 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
1326 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001327 if (hostView == null) {
1328 // Perform actual inflation because we're live
1329 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1330 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1331 } else {
1332 // The AppWidgetHostView has already been inflated and instantiated
1333 launcherInfo.hostView = hostView;
1334 }
Romain Guycbb89e42009-06-08 15:52:54 -07001335
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001336 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001337 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001338 launcherInfo.notifyWidgetSizeChanged(this);
1339
Adam Cohendcd297f2013-06-18 13:13:40 -07001340 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001341 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001342
1343 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001345 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001346 }
Romain Guycbb89e42009-06-08 15:52:54 -07001347
Adam Cohended9f8d2010-11-03 13:25:16 -07001348 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1349 @Override
1350 public void onReceive(Context context, Intent intent) {
1351 final String action = intent.getAction();
1352 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1353 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001354 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001355 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001356
Winson Chungc100e8e2011-08-09 16:02:43 -07001357 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001358 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001359 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1360 mAppsCustomizeTabHost.reset();
1361 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001362 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001363 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1364 mUserPresent = true;
1365 updateRunning();
1366 }
1367 }
1368 };
1369
1370 @Override
1371 public void onAttachedToWindow() {
1372 super.onAttachedToWindow();
1373
1374 // Listen for broadcasts related to user-presence
1375 final IntentFilter filter = new IntentFilter();
1376 filter.addAction(Intent.ACTION_SCREEN_OFF);
1377 filter.addAction(Intent.ACTION_USER_PRESENT);
1378 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001379 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001380 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001381 mVisible = true;
1382 }
1383
1384 @Override
1385 public void onDetachedFromWindow() {
1386 super.onDetachedFromWindow();
1387 mVisible = false;
1388
Adam Cohend113e0c2010-11-11 10:48:05 -08001389 if (mAttached) {
1390 unregisterReceiver(mReceiver);
1391 mAttached = false;
1392 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001393 updateRunning();
1394 }
1395
1396 public void onWindowVisibilityChanged(int visibility) {
1397 mVisible = visibility == View.VISIBLE;
1398 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001399 // The following code used to be in onResume, but it turns out onResume is called when
1400 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1401 // is a more appropriate event to handle
1402 if (mVisible) {
1403 mAppsCustomizeTabHost.onWindowVisible();
1404 if (!mWorkspaceLoading) {
1405 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001406 // We want to let Launcher draw itself at least once before we force it to build
1407 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001408 // apps is nice and speedy.
1409 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001410 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001411 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001412 if (mStarted) return;
1413 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001414 // We delay the layer building a bit in order to give
1415 // other message processing a time to run. In particular
1416 // this avoids a delay in hiding the IME if it was
1417 // currently shown, because doing that may involve
1418 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001419 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001420 final ViewTreeObserver.OnDrawListener listener = this;
1421 mWorkspace.post(new Runnable() {
1422 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001423 if (mWorkspace != null &&
1424 mWorkspace.getViewTreeObserver() != null) {
1425 mWorkspace.getViewTreeObserver().
1426 removeOnDrawListener(listener);
1427 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001428 }
1429 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001430 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001431 }
1432 });
1433 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001434 // When Launcher comes back to foreground, a different Activity might be responsible for
1435 // the app market intent, so refresh the icon
1436 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001437 clearTypedText();
1438 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001439 }
1440
1441 private void sendAdvanceMessage(long delay) {
1442 mHandler.removeMessages(ADVANCE_MSG);
1443 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1444 mHandler.sendMessageDelayed(msg, delay);
1445 mAutoAdvanceSentTime = System.currentTimeMillis();
1446 }
1447
1448 private void updateRunning() {
1449 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1450 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1451 mAutoAdvanceRunning = autoAdvanceRunning;
1452 if (autoAdvanceRunning) {
1453 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1454 sendAdvanceMessage(delay);
1455 } else {
1456 if (!mWidgetsToAdvance.isEmpty()) {
1457 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1458 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1459 }
1460 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001461 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001462 }
1463 }
1464 }
1465
1466 private final Handler mHandler = new Handler() {
1467 @Override
1468 public void handleMessage(Message msg) {
1469 if (msg.what == ADVANCE_MSG) {
1470 int i = 0;
1471 for (View key: mWidgetsToAdvance.keySet()) {
1472 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1473 final int delay = mAdvanceStagger * i;
1474 if (v instanceof Advanceable) {
1475 postDelayed(new Runnable() {
1476 public void run() {
1477 ((Advanceable) v).advance();
1478 }
1479 }, delay);
1480 }
1481 i++;
1482 }
1483 sendAdvanceMessage(mAdvanceInterval);
1484 }
1485 }
1486 };
1487
1488 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001489 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001490 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1491 if (v instanceof Advanceable) {
1492 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001493 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001494 updateRunning();
1495 }
1496 }
1497
1498 void removeWidgetToAutoAdvance(View hostView) {
1499 if (mWidgetsToAdvance.containsKey(hostView)) {
1500 mWidgetsToAdvance.remove(hostView);
1501 updateRunning();
1502 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001503 }
1504
Joe Onorato9c1289c2009-08-17 11:03:03 -04001505 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001506 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001507 launcherInfo.hostView = null;
1508 }
1509
Winson Chung93eef082012-03-23 15:59:27 -07001510 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1511 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1512 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001513 }
1514
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001515 public LauncherAppWidgetHost getAppWidgetHost() {
1516 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001517 }
Romain Guycbb89e42009-06-08 15:52:54 -07001518
Winson Chunga9abd0e2010-10-27 17:18:37 -07001519 public LauncherModel getModel() {
1520 return mModel;
1521 }
1522
Bjorn Bringertc459e522013-06-07 19:36:01 +01001523 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001524 getWindow().closeAllPanels();
1525
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001526 // Whatever we were doing is hereby canceled.
1527 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001528 }
1529
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 @Override
1531 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001532 long startTime = 0;
1533 if (DEBUG_RESUME_TIME) {
1534 startTime = System.currentTimeMillis();
1535 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001536 super.onNewIntent(intent);
1537
1538 // Close the menu
1539 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001540 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001541 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001542
Jamie Genniscb222e82012-10-23 15:44:26 -07001543 final boolean alreadyOnHome =
1544 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001545 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001546
Jamie Genniscb222e82012-10-23 15:44:26 -07001547 Runnable processIntent = new Runnable() {
1548 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001549 if (mWorkspace == null) {
1550 // Can be cases where mWorkspace is null, this prevents a NPE
1551 return;
1552 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001553 Folder openFolder = mWorkspace.getOpenFolder();
1554 // In all these cases, only animate if we're already on home
1555 mWorkspace.exitWidgetResizeMode();
1556 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1557 openFolder == null) {
1558 mWorkspace.moveToDefaultScreen(true);
1559 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001560
Jamie Genniscb222e82012-10-23 15:44:26 -07001561 closeFolder();
1562 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001563
Jamie Genniscb222e82012-10-23 15:44:26 -07001564 // If we are already on home, then just animate back to the workspace,
1565 // otherwise, just wait until onResume to set the state back to Workspace
1566 if (alreadyOnHome) {
1567 showWorkspace(true);
1568 } else {
1569 mOnResumeState = State.WORKSPACE;
1570 }
1571
1572 final View v = getWindow().peekDecorView();
1573 if (v != null && v.getWindowToken() != null) {
1574 InputMethodManager imm = (InputMethodManager)getSystemService(
1575 INPUT_METHOD_SERVICE);
1576 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1577 }
1578
1579 // Reset AllApps to its initial state
1580 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1581 mAppsCustomizeTabHost.reset();
1582 }
1583 }
1584 };
1585
1586 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1587 // Delay processing of the intent to allow the status bar animation to finish
1588 // first in order to avoid janky animations.
1589 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001590 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001591 // Process the intent immediately.
1592 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001593 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001594
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001595 }
Michael Jurka447bf842013-05-15 14:52:15 +02001596 if (DEBUG_RESUME_TIME) {
1597 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1598 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599 }
1600
1601 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001602 public void onRestoreInstanceState(Bundle state) {
1603 super.onRestoreInstanceState(state);
1604 for (int page: mSynchronouslyBoundPages) {
1605 mWorkspace.restoreInstanceStateForChild(page);
1606 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 }
1608
1609 @Override
1610 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001611 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001612 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613
Michael Jurka883f55b2010-10-21 15:47:14 -07001614 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001615 // We close any open folder since it will not be re-opened, and we need to make sure
1616 // this state is reflected.
1617 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001618
Adam Cohendcd297f2013-06-18 13:13:40 -07001619 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001620 mWaitingForResult) {
1621 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001622 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001623 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1624 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001625 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1626 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1627 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 }
1629
1630 if (mFolderInfo != null && mWaitingForResult) {
1631 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1632 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1633 }
Michael Jurka946ad472010-07-09 18:05:18 -07001634
Winson Chung785d2eb2011-04-14 16:08:02 -07001635 // Save the current AppsCustomize tab
1636 if (mAppsCustomizeTabHost != null) {
1637 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1638 if (currentTabTag != null) {
1639 outState.putString("apps_customize_currentTab", currentTabTag);
1640 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001641 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1642 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001643 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001644 }
1645
1646 @Override
1647 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001649
Winson Chunge7a03942011-08-05 15:05:12 -07001650 // Remove all pending runnables
1651 mHandler.removeMessages(ADVANCE_MSG);
1652 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001653 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001654
Winson Chungcd2b0142011-06-08 16:02:26 -07001655 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001656 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001657 mModel.stopLoader();
1658 app.setLauncher(null);
1659
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001660 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001661 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001662 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001663 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001665 mAppWidgetHost = null;
1666
1667 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668
1669 TextKeyListener.getInstance().release();
1670
Winson Chung81b52252012-08-27 15:34:29 -07001671 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1672 // to prevent leaking Launcher activities on orientation change.
1673 if (mModel != null) {
1674 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1675 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001676
1677 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001678 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001679
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001680 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001681 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1682 mWorkspace.removeAllViews();
1683 mWorkspace = null;
1684 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001685
Michael Jurka2ecf9952012-06-18 12:52:28 -07001686 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001687 }
1688
Adam Cohena9cf38f2011-05-02 15:36:58 -07001689 public DragController getDragController() {
1690 return mDragController;
1691 }
1692
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 @Override
1694 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001695 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 super.startActivityForResult(intent, requestCode);
1697 }
1698
Winson Chung70d72102011-08-12 11:24:35 -07001699 /**
1700 * Indicates that we want global search for this activity by setting the globalSearch
1701 * argument for {@link #startSearch} to true.
1702 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001704 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001706
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001707 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001708
Karl Rosaen138a0412009-04-23 19:00:21 -07001709 if (initialQuery == null) {
1710 // Use any text typed in the launcher as the initial query
1711 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001712 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 if (appSearchData == null) {
1714 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001715 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001716 }
Winson Chungadf0c182012-08-23 14:59:07 -07001717 Rect sourceBounds = new Rect();
1718 if (mSearchDropTargetBar != null) {
1719 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1720 }
Romain Guycbb89e42009-06-08 15:52:54 -07001721
Bjorn Bringertc459e522013-06-07 19:36:01 +01001722 startSearch(initialQuery, selectInitialQuery,
1723 appSearchData, sourceBounds);
1724 }
1725
1726 public void startSearch(String initialQuery,
1727 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001728 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001729 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001730 }
1731
1732 /**
1733 * Starts the global search activity. This code is a copied from SearchManager
1734 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001735 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001736 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001737 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001738 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1739 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1740 if (globalSearchActivity == null) {
1741 Log.w(TAG, "No global search activity found.");
1742 return;
1743 }
1744 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1745 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1746 intent.setComponent(globalSearchActivity);
1747 // Make sure that we have a Bundle to put source in
1748 if (appSearchData == null) {
1749 appSearchData = new Bundle();
1750 } else {
1751 appSearchData = new Bundle(appSearchData);
1752 }
1753 // Set source to package name of app that starts global search, if not set already.
1754 if (!appSearchData.containsKey("source")) {
1755 appSearchData.putString("source", getPackageName());
1756 }
1757 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1758 if (!TextUtils.isEmpty(initialQuery)) {
1759 intent.putExtra(SearchManager.QUERY, initialQuery);
1760 }
1761 if (selectInitialQuery) {
1762 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1763 }
1764 intent.setSourceBounds(sourceBounds);
1765 try {
1766 startActivity(intent);
1767 } catch (ActivityNotFoundException ex) {
1768 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1769 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001770 }
1771
Winson Chung70d72102011-08-12 11:24:35 -07001772 @Override
1773 public boolean onCreateOptionsMenu(Menu menu) {
1774 if (isWorkspaceLocked()) {
1775 return false;
1776 }
1777
1778 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001779
1780 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1781 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1782 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001783 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1784 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1785 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001786 String helpUrl = getString(R.string.help_url);
1787 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001788 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1789 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1790
Winson Chung70d72102011-08-12 11:24:35 -07001791 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1792 .setIcon(android.R.drawable.ic_menu_gallery)
1793 .setAlphabeticShortcut('W');
1794 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1795 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001796 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001797 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001798 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1799 .setIcon(android.R.drawable.ic_menu_preferences)
1800 .setIntent(settings)
1801 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001802 if (!helpUrl.isEmpty()) {
1803 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1804 .setIcon(android.R.drawable.ic_menu_help)
1805 .setIntent(help)
1806 .setAlphabeticShortcut('H');
1807 }
Winson Chung70d72102011-08-12 11:24:35 -07001808 return true;
1809 }
1810
1811 @Override
1812 public boolean onPrepareOptionsMenu(Menu menu) {
1813 super.onPrepareOptionsMenu(menu);
1814
1815 if (mAppsCustomizeTabHost.isTransitioning()) {
1816 return false;
1817 }
1818 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1819 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1820
1821 return true;
1822 }
1823
1824 @Override
1825 public boolean onOptionsItemSelected(MenuItem item) {
1826 switch (item.getItemId()) {
1827 case MENU_WALLPAPER_SETTINGS:
1828 startWallpaper();
1829 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001830 }
1831
1832 return super.onOptionsItemSelected(item);
1833 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001835 @Override
1836 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001837 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001838 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001839 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001840 }
1841
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 public boolean isWorkspaceLocked() {
1843 return mWorkspaceLoading || mWaitingForResult;
1844 }
1845
Michael Jurka0280c3b2010-09-17 15:00:07 -07001846 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001847 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001848 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001849 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1850 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001851 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001852 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001853 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001854
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001855 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1856 AppWidgetProviderInfo appWidgetInfo) {
1857 if (appWidgetInfo.configure != null) {
1858 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001859
Bjorn Bringert7984c942009-12-09 15:38:25 +00001860 // Launch over to configure widget, if needed
1861 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001862 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001863 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001864 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001865 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001866 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001867 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001868 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001869 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001870 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001871 }
1872 }
Romain Guycbb89e42009-06-08 15:52:54 -07001873
Adam Cohenfbba09b2011-07-18 21:43:05 -07001874 /**
1875 * Process a shortcut drop.
1876 *
1877 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001878 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001879 * @param cell The cell it should be added to, optional
1880 * @param position The location on the screen where it was dropped, optional
1881 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001882 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001883 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001884 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001885 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001886 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001887 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001888
1889 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001890 mPendingAddInfo.cellX = cell[0];
1891 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001892 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001893
1894 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1895 createShortcutIntent.setComponent(componentName);
1896 processShortcut(createShortcutIntent);
1897 }
1898
Adam Cohenfbba09b2011-07-18 21:43:05 -07001899 /**
1900 * Process a widget drop.
1901 *
1902 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001903 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001904 * @param cell The cell it should be added to, optional
1905 * @param position The location on the screen where it was dropped, optional
1906 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001907 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001908 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001909 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001910 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001911 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001912 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001913 mPendingAddInfo.minSpanX = info.minSpanX;
1914 mPendingAddInfo.minSpanY = info.minSpanY;
1915
Adam Cohenfbba09b2011-07-18 21:43:05 -07001916 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001917 mPendingAddInfo.cellX = cell[0];
1918 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001919 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001920 if (span != null) {
1921 mPendingAddInfo.spanX = span[0];
1922 mPendingAddInfo.spanY = span[1];
1923 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001924
Adam Cohened66b2b2012-01-23 17:28:51 -08001925 AppWidgetHostView hostView = info.boundWidget;
1926 int appWidgetId;
1927 if (hostView != null) {
1928 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001929 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001930 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001931 // In this case, we either need to start an activity to get permission to bind
1932 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001933 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001934 Bundle options = info.bindOptions;
1935
1936 boolean success = false;
1937 if (options != null) {
1938 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1939 info.componentName, options);
1940 } else {
1941 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1942 info.componentName);
1943 }
1944 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001945 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001946 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001947 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001948 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1949 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1950 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001951 // TODO: we need to make sure that this accounts for the options bundle.
1952 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001953 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1954 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001955 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001956 }
1957
Joe Onoratodeb98af2010-02-19 14:59:39 -08001958 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001959 // Handle case where user selected "Applications"
1960 String applicationName = getResources().getString(R.string.group_applications);
1961 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001962
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001963 if (applicationName != null && applicationName.equals(shortcutName)) {
1964 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
1965 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07001966
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001967 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
1968 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07001969 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07001970 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001971 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07001972 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001973 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 }
1975
Winson Chung24ab2f12010-09-16 14:10:47 -07001976 void processWallpaper(Intent intent) {
1977 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
1978 }
1979
Adam Cohendcd297f2013-06-18 13:13:40 -07001980 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001981 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07001982 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 folderInfo.title = getText(R.string.folder_name);
1984
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001985 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07001986 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07001987 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001988 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989
1990 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07001991 FolderIcon newFolder =
1992 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07001993 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001994 isWorkspaceLocked());
Adam Cohendf035382011-04-11 17:22:04 -07001995 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 }
Romain Guycbb89e42009-06-08 15:52:54 -07001997
Joe Onorato9c1289c2009-08-17 11:03:03 -04001998 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001999 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002000 }
2001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002003 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Dianne Hackborn8355ae32009-09-07 21:47:51 -07002005 Intent chooser = Intent.createChooser(pickWallpaper,
2006 getText(R.string.chooser_wallpaper));
Romain Guyf2826c72009-11-12 17:39:34 -08002007 // NOTE: Adds a configure option to the chooser if the wallpaper supports it
2008 // Removed in Eclair MR1
2009// WallpaperManager wm = (WallpaperManager)
2010// getSystemService(Context.WALLPAPER_SERVICE);
2011// WallpaperInfo wi = wm.getWallpaperInfo();
2012// if (wi != null && wi.getSettingsActivity() != null) {
2013// LabeledIntent li = new LabeledIntent(getPackageName(),
2014// R.string.configure_wallpaper, 0);
2015// li.setClassName(wi.getPackageName(), wi.getSettingsActivity());
2016// chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { li });
2017// }
Mike Clerona0618e42009-10-22 13:55:21 -07002018 startActivityForResult(chooser, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 }
2020
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002021 /**
2022 * Registers various content observers. The current implementation registers
2023 * only a favorites observer to keep track of the favorites applications.
2024 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002025 private void registerContentObservers() {
2026 ContentResolver resolver = getContentResolver();
2027 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2028 true, mWidgetObserver);
2029 }
2030
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 @Override
2032 public boolean dispatchKeyEvent(KeyEvent event) {
2033 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2034 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002036 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002037 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002038 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002039 dumpState();
2040 return true;
2041 }
2042 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002043 }
2044 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2045 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002046 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 return true;
2048 }
2049 }
2050
2051 return super.dispatchKeyEvent(event);
2052 }
2053
Joe Onorato88ec0992009-11-19 13:16:06 -08002054 @Override
2055 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002056 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002057 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002058 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002059 Folder openFolder = mWorkspace.getOpenFolder();
2060 if (openFolder.isEditingName()) {
2061 openFolder.dismissEditingName();
2062 } else {
2063 closeFolder();
2064 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002065 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002066 mWorkspace.exitWidgetResizeMode();
2067
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002068 // Back button is a no-op here, but give at least some feedback for the button press
2069 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002070 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002071 }
2072
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002073 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002074 * Re-listen when widgets are reset.
2075 */
2076 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002077 if (mAppWidgetHost != null) {
2078 mAppWidgetHost.startListening();
2079 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002080 }
2081
2082 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 * Launches the intent referred by the clicked shortcut.
2084 *
2085 * @param v The view representing the clicked shortcut.
2086 */
2087 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002088 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2089 // view has detached (it's possible for this to happen if the view is removed mid touch).
2090 if (v.getWindowToken() == null) {
2091 return;
2092 }
2093
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002094 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002095 return;
2096 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002097
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002098 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002099 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002101 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2102 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002103
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002104 // Check for special shortcuts
2105 if (intent.getComponent() != null) {
2106 final String shortcutClass = intent.getComponent().getClassName();
2107
2108 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2109 showAllApps(true);
2110 return;
2111 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2112 MemoryDumpActivity.startDump(this);
2113 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002114 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2115 toggleShowWeightWatcher();
2116 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002117 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002118 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002119
2120 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002121 int[] pos = new int[2];
2122 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002123 intent.setSourceBounds(new Rect(pos[0], pos[1],
2124 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002125
2126 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002127
Daniel Sandlerff02d492013-08-05 02:12:05 -04002128 mStats.recordLaunch(intent, shortcut);
2129
Michael Jurkaddd62e92011-02-16 17:49:14 -08002130 if (success && v instanceof BubbleTextView) {
2131 mWaitingForResume = (BubbleTextView) v;
2132 mWaitingForResume.setStayPressed(true);
2133 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002134 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002135 if (v instanceof FolderIcon) {
2136 FolderIcon fi = (FolderIcon) v;
2137 handleFolderClick(fi);
2138 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002139 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002140 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002141 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002142 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002143 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002144 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002145 }
2146 }
2147
Michael Jurka0e260592010-06-30 17:07:39 -07002148 public boolean onTouch(View v, MotionEvent event) {
Michael Jurkadee05892010-07-27 10:01:56 -07002149 // this is an intercepted event being forwarded from mWorkspace;
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002150 // clicking anywhere on the workspace causes the customization drawer to slide down
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002151 if (event.getAction() == MotionEvent.ACTION_DOWN) {
2152 showWorkspace(true);
2153 }
Michael Jurka0e260592010-06-30 17:07:39 -07002154 return false;
2155 }
2156
Michael Jurkaaf442092010-06-10 17:01:57 -07002157 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002158 * Event handler for the search button
2159 *
2160 * @param v The view that was clicked.
2161 */
2162 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002163 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2164
Amith Yamasania135ba82011-08-09 17:42:01 -07002165 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002166 }
2167
2168 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002169 * Event handler for the voice button
2170 *
2171 * @param v The view that was clicked.
2172 */
2173 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002174 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002175
Bjorn Bringertc459e522013-06-07 19:36:01 +01002176 startVoice();
2177 }
2178
2179 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002180 try {
2181 final SearchManager searchManager =
2182 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2183 ComponentName activityName = searchManager.getGlobalSearchActivity();
2184 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002185 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002186 if (activityName != null) {
2187 intent.setPackage(activityName.getPackageName());
2188 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002189 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002190 } catch (ActivityNotFoundException e) {
2191 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002192 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002193 startActivitySafely(null, intent, "onClickVoiceButton");
2194 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002195 }
2196
2197 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002198 * Event handler for the "grid" button that appears on the home screen, which
2199 * enters all apps mode.
2200 *
2201 * @param v The view that was clicked.
2202 */
2203 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002204 showAllApps(true);
2205 }
2206
2207 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002208 // Provide the same haptic feedback that the system offers for virtual keys.
2209 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002210 }
2211
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002212 public void onClickAppMarketButton(View v) {
2213 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002214 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002215 } else {
2216 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002217 }
2218 }
2219
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002220 void startApplicationDetailsActivity(ComponentName componentName) {
2221 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002222 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2223 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002224 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002225 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002226 }
2227
Michael Jurka1e2f4652013-07-08 18:03:46 -07002228 // returns true if the activity was started
2229 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2230 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002231 // System applications cannot be installed. For now, show a toast explaining that.
2232 // We may give them the option of disabling apps this way.
2233 int messageId = R.string.uninstall_system_app_text;
2234 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002235 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002236 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002237 String packageName = componentName.getPackageName();
2238 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002239 Intent intent = new Intent(
2240 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002241 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2242 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002243 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002244 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002245 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002246 }
2247
Michael Jurka86a720e2012-05-09 11:23:23 -07002248 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002250
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002251 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002252 // Only launch using the new animation if the shortcut has not opted out (this is a
2253 // private contract between launcher and may be ignored in the future).
2254 boolean useLaunchAnimation = (v != null) &&
2255 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2256 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002257 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2258 v.getMeasuredWidth(), v.getMeasuredHeight());
2259
2260 startActivity(intent, opts.toBundle());
2261 } else {
2262 startActivity(intent);
2263 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002264 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002265 } catch (SecurityException e) {
2266 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002267 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002268 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002269 "or use the exported attribute for this activity. "
2270 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002272 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002273 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002274
Michael Jurka86a720e2012-05-09 11:23:23 -07002275 boolean startActivitySafely(View v, Intent intent, Object tag) {
2276 boolean success = false;
2277 try {
2278 success = startActivity(v, intent, tag);
2279 } catch (ActivityNotFoundException e) {
2280 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2281 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2282 }
2283 return success;
2284 }
2285
Romain Guy8e633c52010-03-04 12:51:36 -08002286 void startActivityForResultSafely(Intent intent, int requestCode) {
2287 try {
2288 startActivityForResult(intent, requestCode);
2289 } catch (ActivityNotFoundException e) {
2290 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2291 } catch (SecurityException e) {
2292 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2293 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2294 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2295 "or use the exported attribute for this activity.", e);
2296 }
2297 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002298
Adam Cohena9cf38f2011-05-02 15:36:58 -07002299 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002300 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002301 Folder openFolder = mWorkspace.getFolderForTag(info);
2302
2303 // If the folder info reports that the associated folder is open, then verify that
2304 // it is actually opened. There have been a few instances where this gets out of sync.
2305 if (info.opened && openFolder == null) {
2306 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002307 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002308 info.opened = false;
2309 }
2310
Adam Cohenfb91f302012-06-11 15:45:18 -07002311 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002312 // Close any open folder
2313 closeFolder();
2314 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002315 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002316 } else {
2317 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002318 int folderScreen;
2319 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002320 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002321 // .. and close it
2322 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002323 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002324 // Close any folder open on the current screen
2325 closeFolder();
2326 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002327 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002328 }
2329 }
2330 }
2331 }
2332
Adam Cohen268c4752012-06-06 17:47:33 -07002333 /**
2334 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2335 * in the DragLayer in the exact absolute location of the original FolderIcon.
2336 */
2337 private void copyFolderIconToImage(FolderIcon fi) {
2338 final int width = fi.getMeasuredWidth();
2339 final int height = fi.getMeasuredHeight();
2340
2341 // Lazy load ImageView, Bitmap and Canvas
2342 if (mFolderIconImageView == null) {
2343 mFolderIconImageView = new ImageView(this);
2344 }
2345 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2346 mFolderIconBitmap.getHeight() != height) {
2347 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2348 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2349 }
2350
2351 DragLayer.LayoutParams lp;
2352 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2353 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2354 } else {
2355 lp = new DragLayer.LayoutParams(width, height);
2356 }
2357
Adam Cohen307fe232012-08-16 17:55:58 -07002358 // The layout from which the folder is being opened may be scaled, adjust the starting
2359 // view size by this scale factor.
2360 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002361 lp.customPosition = true;
2362 lp.x = mRectForFolderAnimation.left;
2363 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002364 lp.width = (int) (scale * width);
2365 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002366
2367 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2368 fi.draw(mFolderIconCanvas);
2369 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002370 if (fi.getFolder() != null) {
2371 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2372 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002373 }
Adam Cohen268c4752012-06-06 17:47:33 -07002374 // Just in case this image view is still in the drag layer from a previous animation,
2375 // we remove it and re-add it.
2376 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2377 mDragLayer.removeView(mFolderIconImageView);
2378 }
2379 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002380 if (fi.getFolder() != null) {
2381 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002382 }
Adam Cohen268c4752012-06-06 17:47:33 -07002383 }
2384
Adam Cohen2801caf2011-05-13 20:57:39 -07002385 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002386 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002387 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2388 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2389 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2390
Adam Cohenc51934b2011-07-26 21:07:43 -07002391 FolderInfo info = (FolderInfo) fi.getTag();
2392 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2393 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002394 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2395 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002396 }
2397
Adam Cohen268c4752012-06-06 17:47:33 -07002398 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2399 copyFolderIconToImage(fi);
2400 fi.setVisibility(View.INVISIBLE);
2401
Michael Jurka2ecf9952012-06-18 12:52:28 -07002402 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002403 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002404 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2405 oa.start();
2406 }
2407
Adam Cohen268c4752012-06-06 17:47:33 -07002408 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002409 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002410 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2411 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2412 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2413
Adam Cohen268c4752012-06-06 17:47:33 -07002414 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002415
Adam Cohen268c4752012-06-06 17:47:33 -07002416 // We remove and re-draw the FolderIcon in-case it has changed
2417 mDragLayer.removeView(mFolderIconImageView);
2418 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002419 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002420 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002421 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002422 oa.addListener(new AnimatorListenerAdapter() {
2423 @Override
2424 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002425 if (cl != null) {
2426 cl.clearFolderLeaveBehind();
2427 // Remove the ImageView copy of the FolderIcon and make the original visible.
2428 mDragLayer.removeView(mFolderIconImageView);
2429 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002430 }
2431 }
2432 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002433 oa.start();
2434 }
2435
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002437 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 * is animated relative to the specified View. If the View is null, no animation
2439 * is played.
2440 *
2441 * @param folderInfo The FolderInfo describing the folder to open.
2442 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002443 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002444 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002445 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002446
Adam Cohena9cf38f2011-05-02 15:36:58 -07002447 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002448
Adam Cohen4554ee12011-08-03 16:13:21 -07002449 // Just verify that the folder hasn't already been added to the DragLayer.
2450 // There was a one-off crash where the folder had a parent already.
2451 if (folder.getParent() == null) {
2452 mDragLayer.addView(folder);
2453 mDragController.addDropTarget((DropTarget) folder);
2454 } else {
2455 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2456 folder.getParent() + ").");
2457 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002458 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002459 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002460
2461 // Notify the accessibility manager that this folder "window" has appeared and occluded
2462 // the workspace items
2463 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2464 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002465 }
2466
2467 public void closeFolder() {
2468 Folder folder = mWorkspace.getOpenFolder();
2469 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002470 if (folder.isEditingName()) {
2471 folder.dismissEditingName();
2472 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002473 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002474
2475 // Dismiss the folder cling
2476 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002477 }
2478 }
2479
2480 void closeFolder(Folder folder) {
2481 folder.getInfo().opened = false;
2482
2483 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2484 if (parent != null) {
2485 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2486 shrinkAndFadeInFolderIcon(fi);
2487 }
2488 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002489
2490 // Notify the accessibility manager that this folder "window" has disappeard and no
2491 // longer occludeds the workspace items
2492 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002493 }
2494
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002496 if (!isDraggingEnabled()) return false;
2497 if (isWorkspaceLocked()) return false;
2498 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499
2500 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002501 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002502 }
2503
Michael Jurka0280c3b2010-09-17 15:00:07 -07002504 resetAddInfo();
2505 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002506 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002507 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 return true;
2509 }
2510
Winson Chung3d503fb2011-07-13 17:25:49 -07002511 // The hotseat touch handling does not go through Workspace, and we always allow long press
2512 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002513 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002514 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2515 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002516 if (itemUnderLongClick == null) {
2517 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002518 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2519 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002520 // Disabling reordering until we sort out some issues.
Adam Cohendedbd962013-07-11 14:21:49 -07002521 if (mWorkspace.getIdForScreen((CellLayout) v) >= 0) {
2522 mWorkspace.startReordering();
2523 } else {
2524 startWallpaper();
2525 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002526 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002527 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002528 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002529 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002530 }
2531 }
2532 }
2533 return true;
2534 }
2535
Winson Chung3d503fb2011-07-13 17:25:49 -07002536 boolean isHotseatLayout(View layout) {
2537 return mHotseat != null && layout != null &&
2538 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2539 }
2540 Hotseat getHotseat() {
2541 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002542 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002543 SearchDropTargetBar getSearchBar() {
2544 return mSearchDropTargetBar;
2545 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002546
Winson Chung3d503fb2011-07-13 17:25:49 -07002547 /**
2548 * Returns the CellLayout of the specified container at the specified screen.
2549 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002550 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002551 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2552 if (mHotseat != null) {
2553 return mHotseat.getLayout();
2554 } else {
2555 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002556 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002557 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002558 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002559 }
2560 }
2561
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002562 Workspace getWorkspace() {
2563 return mWorkspace;
2564 }
2565
Daniel Sandler843e8602010-06-07 14:59:01 -04002566 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002567 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002568 }
2569
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002570 /**
2571 * Helper method for the cameraZoomIn/cameraZoomOut animations
2572 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002573 * @param scaleFactor The scale factor used for the zoom
2574 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002575 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002576 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002577 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002578 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002579
Winson Chung18f41f82012-05-09 13:28:10 -07002580 void disableWallpaperIfInAllApps() {
2581 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002582 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002583 if (mAppsCustomizeTabHost != null &&
2584 !mAppsCustomizeTabHost.isTransitioning()) {
2585 updateWallpaperVisibility(false);
2586 }
2587 }
2588 }
2589
Craig Mautner360310b2012-10-26 15:13:08 -07002590 private void setWorkspaceBackground(boolean workspace) {
2591 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002592 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002593 }
2594
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002595 void updateWallpaperVisibility(boolean visible) {
2596 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2597 int curflags = getWindow().getAttributes().flags
2598 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2599 if (wpflags != curflags) {
2600 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2601 }
Craig Mautner360310b2012-10-26 15:13:08 -07002602 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002603 }
2604
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002605 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2606 if (v instanceof LauncherTransitionable) {
2607 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2608 }
2609 }
2610
Michael Jurkabed61d22012-02-14 22:51:29 -08002611 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2612 if (v instanceof LauncherTransitionable) {
2613 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2614 }
Winson Chung70442722012-02-10 15:43:22 -08002615
2616 // Update the workspace transition step as well
2617 dispatchOnLauncherTransitionStep(v, 0f);
2618 }
2619
2620 private void dispatchOnLauncherTransitionStep(View v, float t) {
2621 if (v instanceof LauncherTransitionable) {
2622 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2623 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002624 }
2625
2626 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2627 if (v instanceof LauncherTransitionable) {
2628 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2629 }
Winson Chung70442722012-02-10 15:43:22 -08002630
2631 // Update the workspace transition step as well
2632 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002633 }
2634
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002635 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002636 * Things to test when changing the following seven functions.
2637 * - Home from workspace
2638 * - from center screen
2639 * - from other screens
2640 * - Home from all apps
2641 * - from center screen
2642 * - from other screens
2643 * - Back from all apps
2644 * - from center screen
2645 * - from other screens
2646 * - Launch app from workspace and quit
2647 * - with back
2648 * - with home
2649 * - Launch app from all apps and quit
2650 * - with back
2651 * - with home
2652 * - Go to a screen that's not the default, then all
2653 * apps, and launch and app, and go back
2654 * - with back
2655 * -with home
2656 * - On workspace, long press power and go back
2657 * - with back
2658 * - with home
2659 * - On all apps, long press power and go back
2660 * - with back
2661 * - with home
2662 * - On workspace, power off
2663 * - On all apps, power off
2664 * - Launch an app and turn off the screen while in that app
2665 * - Go back with home key
2666 * - Go back with back key TODO: make this not go to workspace
2667 * - From all apps
2668 * - From workspace
2669 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2670 * - From all apps
2671 * - From the center workspace
2672 * - From another workspace
2673 */
2674
2675 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002676 * Zoom the camera out from the workspace to reveal 'toView'.
2677 * Assumes that the view to show is anchored at either the very top or very bottom
2678 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002679 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002680 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002681 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002682 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002683 mStateAnimation.cancel();
2684 mStateAnimation = null;
2685 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002686 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002687
Winson Chungf0ea4d32011-06-06 14:27:16 -07002688 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2689 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2690 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002691 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002692 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002693 final int startDelay =
2694 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002695
Michael Jurkab3e22d92011-10-31 15:58:33 -07002696 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002697
Michael Jurkad74c9842011-07-10 12:44:21 -07002698 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002699 Animator workspaceAnim =
2700 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002701
2702 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002703 toView.setScaleX(scale);
2704 toView.setScaleY(scale);
2705 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2706 scaleAnim.
2707 scaleX(1f).scaleY(1f).
2708 setDuration(duration).
2709 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002710
Winson Chungf0ea4d32011-06-06 14:27:16 -07002711 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002712 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002713 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002714 .ofFloat(toView, "alpha", 0f, 1f)
2715 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002716 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002717 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2718 @Override
2719 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002720 if (animation == null) {
2721 throw new RuntimeException("animation is null");
2722 }
Winson Chung70442722012-02-10 15:43:22 -08002723 float t = (Float) animation.getAnimatedValue();
2724 dispatchOnLauncherTransitionStep(fromView, t);
2725 dispatchOnLauncherTransitionStep(toView, t);
2726 }
2727 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002728
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002729 // toView should appear right at the end of the workspace shrink
2730 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002731 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002732 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002733 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002734
2735 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002736 boolean animationCancelled = false;
2737
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002738 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002739 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002740 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002741 // Prepare the position
2742 toView.setTranslationX(0.0f);
2743 toView.setTranslationY(0.0f);
2744 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002745 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002746 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002747 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002748 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002749 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2750 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002751
Daniel Sandlere4f98912013-06-25 15:13:26 -04002752 if (mWorkspace != null
2753 && !springLoaded
2754 && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkab737ee62011-11-15 15:57:22 -08002755 hideDockDivider();
Winson Chung32174c82011-07-19 15:47:55 -07002756 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002757 if (!animationCancelled) {
2758 updateWallpaperVisibility(false);
2759 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002760
2761 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002762 if (mSearchDropTargetBar != null) {
2763 mSearchDropTargetBar.hideSearchBar(false);
2764 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002765 }
2766
Adam Cohencff6af82011-09-13 14:51:53 -07002767 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002768 public void onAnimationCancel(Animator animation) {
2769 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002770 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002771 });
2772
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002773 if (workspaceAnim != null) {
2774 mStateAnimation.play(workspaceAnim);
2775 }
Michael Jurka1899a362011-11-03 13:50:45 -07002776
2777 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002778
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002779 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2780 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002781
2782 // If any of the objects being animated haven't been measured/laid out
2783 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002784 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002785 (mWorkspace.getMeasuredWidth() == 0) ||
2786 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002787 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002788 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002789
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002790 final AnimatorSet stateAnimation = mStateAnimation;
2791 final Runnable startAnimRunnable = new Runnable() {
2792 public void run() {
2793 // Check that mStateAnimation hasn't changed while
2794 // we waited for a layout/draw pass
2795 if (mStateAnimation != stateAnimation)
2796 return;
2797 setPivotsForZoom(toView, scale);
2798 dispatchOnLauncherTransitionStart(fromView, animated, false);
2799 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002800 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002801 }
2802 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002803 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002804 final ViewTreeObserver observer = toView.getViewTreeObserver();
2805 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2806 public void onGlobalLayout() {
2807 startAnimRunnable.run();
2808 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2809 }
2810 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002811 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002812 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002813 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002814 } else {
2815 toView.setTranslationX(0.0f);
2816 toView.setTranslationY(0.0f);
2817 toView.setScaleX(1.0f);
2818 toView.setScaleY(1.0f);
2819 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002820 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002821
Daniel Sandlere4f98912013-06-25 15:13:26 -04002822 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002823 hideDockDivider();
Winson Chungc7d2b602012-05-16 17:02:20 -07002824
2825 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002826 if (mSearchDropTargetBar != null) {
2827 mSearchDropTargetBar.hideSearchBar(false);
2828 }
Michael Jurkaabded662011-03-04 12:06:57 -08002829 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002830 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002831 dispatchOnLauncherTransitionStart(fromView, animated, false);
2832 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002833 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002834 dispatchOnLauncherTransitionStart(toView, animated, false);
2835 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002836 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002837 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002838 }
2839
2840 /**
2841 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002842 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002843 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002844 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002845 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2846 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002847
Michael Jurkab3e22d92011-10-31 15:58:33 -07002848 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002849 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002850 mStateAnimation.cancel();
2851 mStateAnimation = null;
2852 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002853 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002854
Winson Chungf0ea4d32011-06-06 14:27:16 -07002855 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002856 final int fadeOutDuration =
2857 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002858 final float scaleFactor = (float)
2859 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2860 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002861 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002862 Animator workspaceAnim = null;
2863
2864 if (toState == State.WORKSPACE) {
2865 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2866 workspaceAnim = mWorkspace.getChangeStateAnimation(
2867 Workspace.State.NORMAL, animated, stagger);
2868 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2869 workspaceAnim = mWorkspace.getChangeStateAnimation(
2870 Workspace.State.SPRING_LOADED, animated);
2871 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002872
Michael Jurkab3e22d92011-10-31 15:58:33 -07002873 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002874 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002875 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002876 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002877 final LauncherViewPropertyAnimator scaleAnim =
2878 new LauncherViewPropertyAnimator(fromView);
2879 scaleAnim.
2880 scaleX(scaleFactor).scaleY(scaleFactor).
2881 setDuration(duration).
2882 setInterpolator(new Workspace.ZoomInInterpolator());
2883
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002884 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002885 .ofFloat(fromView, "alpha", 1f, 0f)
2886 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002887 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002888 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2889 @Override
2890 public void onAnimationUpdate(ValueAnimator animation) {
2891 float t = 1f - (Float) animation.getAnimatedValue();
2892 dispatchOnLauncherTransitionStep(fromView, t);
2893 dispatchOnLauncherTransitionStep(toView, t);
2894 }
2895 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002896
Michael Jurka2ecf9952012-06-18 12:52:28 -07002897 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002898
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002899 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2900 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002901 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002902
2903 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002904 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002905 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002906 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002907 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002908 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2909 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002910 if (onCompleteRunnable != null) {
2911 onCompleteRunnable.run();
2912 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002913 mAppsCustomizeContent.updateCurrentPageScroll();
2914 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002915 }
2916 });
2917
Winson Chungf0ea4d32011-06-06 14:27:16 -07002918 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002919 if (workspaceAnim != null) {
2920 mStateAnimation.play(workspaceAnim);
2921 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002922 dispatchOnLauncherTransitionStart(fromView, animated, true);
2923 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002924 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002925 } else {
2926 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002927 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002928 dispatchOnLauncherTransitionStart(fromView, animated, true);
2929 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002930 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002931 dispatchOnLauncherTransitionStart(toView, animated, true);
2932 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002933 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002934 }
2935
Michael Jurkae326f182011-11-21 14:05:46 -08002936 @Override
2937 public void onTrimMemory(int level) {
2938 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002939 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002940 mAppsCustomizeTabHost.onTrimMemory();
2941 }
2942 }
2943
Winson Chung18f41f82012-05-09 13:28:10 -07002944 @Override
2945 public void onWindowFocusChanged(boolean hasFocus) {
2946 if (!hasFocus) {
2947 // When another window occludes launcher (like the notification shade, or recents),
2948 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2949 updateWallpaperVisibility(true);
2950 } else {
2951 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002952 mWorkspace.postDelayed(new Runnable() {
2953 @Override
2954 public void run() {
2955 disableWallpaperIfInAllApps();
2956 }
2957 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002958 }
2959 }
2960
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002961 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002962 showWorkspace(animated, null);
2963 }
2964
2965 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002966 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002967 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002968 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08002969 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002970
Winson Chungc7d2b602012-05-16 17:02:20 -07002971 // Show the search bar (only animate if we were showing the drop target bar in spring
2972 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07002973 if (mSearchDropTargetBar != null) {
2974 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
2975 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002976
Michael Jurkab737ee62011-11-15 15:57:22 -08002977 // We only need to animate in the dock divider if we're going from spring loaded mode
Winson Chungc7d2b602012-05-16 17:02:20 -07002978 showDockDivider(animated && wasInSpringLoadedMode);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002979
2980 // Set focus to the AppsCustomize button
2981 if (mAllAppsButton != null) {
2982 mAllAppsButton.requestFocus();
2983 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002984 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07002985
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002986 // Change the state *after* we've called all the transition code
2987 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08002988
Winson Chung5fb63472011-02-02 17:03:37 -08002989 // Resume the auto-advance of widgets
2990 mUserPresent = true;
2991 updateRunning();
2992
alanv1d4fde62012-10-17 13:15:47 -07002993 // Send an accessibility event to announce the context change
2994 getWindow().getDecorView()
2995 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002996
Bjorn Bringert69688aa2013-06-11 20:26:38 +01002997 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002998 }
2999
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003000 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003001 }
3002
Michael Jurkab3e22d92011-10-31 15:58:33 -07003003 void showAllApps(boolean animated) {
3004 if (mState != State.WORKSPACE) return;
3005
3006 showAppsCustomizeHelper(animated, false);
3007 mAppsCustomizeTabHost.requestFocus();
3008
Michael Jurkab3e22d92011-10-31 15:58:33 -07003009 // Change the state *after* we've called all the transition code
3010 mState = State.APPS_CUSTOMIZE;
3011
3012 // Pause the auto-advance of widgets until we are out of AllApps
3013 mUserPresent = false;
3014 updateRunning();
3015 closeFolder();
3016
3017 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003018 getWindow().getDecorView()
3019 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003020 }
3021
Adam Cohen7777d962011-08-18 18:58:38 -07003022 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003023 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003024 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Michael Jurkab737ee62011-11-15 15:57:22 -08003025 hideDockDivider();
Winson Chungc07918d2011-07-01 15:35:26 -07003026 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003027 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003028 }
Adam Cohen7777d962011-08-18 18:58:38 -07003029
Adam Cohened66b2b2012-01-23 17:28:51 -08003030 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3031 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003032 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3033
Winson Chunge7a03942011-08-05 15:05:12 -07003034 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003035 @Override
3036 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003037 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003038 // Before we show workspace, hide all apps again because
3039 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3040 // clean up our state transition functions
3041 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003042 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003043 } else {
3044 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003045 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003046 }
3047 }, (extendedDelay ?
3048 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3049 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3050 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003051
Michael Jurkad3ef3062010-11-23 16:23:58 -08003052 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003053 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003054 final boolean animated = true;
3055 final boolean springLoaded = true;
3056 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003057 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003058 }
3059 // Otherwise, we are not in spring loaded mode, so don't do anything.
3060 }
3061
Michael Jurkab737ee62011-11-15 15:57:22 -08003062 void hideDockDivider() {
Winson Chungd2be3812013-07-16 11:11:32 -07003063 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003064 mQsbDivider.setVisibility(View.INVISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003065 }
3066 }
3067
3068 void showDockDivider(boolean animated) {
Winson Chungd2be3812013-07-16 11:11:32 -07003069 if (mQsbDivider != null) {
Michael Jurkab737ee62011-11-15 15:57:22 -08003070 mQsbDivider.setVisibility(View.VISIBLE);
Michael Jurkab737ee62011-11-15 15:57:22 -08003071 if (mDividerAnimator != null) {
3072 mDividerAnimator.cancel();
3073 mQsbDivider.setAlpha(1f);
Michael Jurkab737ee62011-11-15 15:57:22 -08003074 mDividerAnimator = null;
3075 }
3076 if (animated) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003077 mDividerAnimator = LauncherAnimUtils.createAnimatorSet();
Winson Chungd2be3812013-07-16 11:11:32 -07003078 mDividerAnimator.playTogether(LauncherAnimUtils.ofFloat(mQsbDivider, "alpha", 1f));
Winson Chungadf0c182012-08-23 14:59:07 -07003079 int duration = 0;
3080 if (mSearchDropTargetBar != null) {
3081 duration = mSearchDropTargetBar.getTransitionInDuration();
3082 }
3083 mDividerAnimator.setDuration(duration);
Michael Jurkab737ee62011-11-15 15:57:22 -08003084 mDividerAnimator.start();
3085 }
3086 }
3087 }
3088
Michael Jurkab3e22d92011-10-31 15:58:33 -07003089 void lockAllApps() {
3090 // TODO
3091 }
3092
3093 void unlockAllApps() {
3094 // TODO
3095 }
3096
Winson Chungf0ea4d32011-06-06 14:27:16 -07003097 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003098 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003099 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003100 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003101 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003102 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003103 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003104 int duration = 0;
3105 if (mSearchDropTargetBar != null) {
3106 duration = mSearchDropTargetBar.getTransitionInDuration();
3107 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003108 mHotseat.animate().alpha(1f).setDuration(duration);
3109 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003110 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003111 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003112 }
3113 }
3114 }
3115
3116 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003117 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003118 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003119 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003120 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003121 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003122 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003123 int duration = 0;
3124 if (mSearchDropTargetBar != null) {
3125 duration = mSearchDropTargetBar.getTransitionOutDuration();
3126 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003127 mHotseat.animate().alpha(0f).setDuration(duration);
3128 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003129 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003130 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003131 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003132 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003133 }
3134
Patrick Dubroy5f445422011-02-18 14:35:21 -08003135 /**
3136 * Add an item from all apps or customize onto the given workspace screen.
3137 * If layout is null, add to the current screen.
3138 */
3139 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003140 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003141 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003142 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003143 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003144
Winson Chungdff8ebb2011-09-08 17:25:31 -07003145 /** Maps the current orientation to an index for referencing orientation correct global icons */
3146 private int getCurrentOrientationIndexForGlobalIcons() {
3147 // default - 0, landscape - 1
3148 switch (getResources().getConfiguration().orientation) {
3149 case Configuration.ORIENTATION_LANDSCAPE:
3150 return 1;
3151 default:
3152 return 0;
3153 }
3154 }
3155
Michael Jurkaae65ee32012-04-30 11:45:54 -07003156 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003157 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003158 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003159 // Look for the toolbar icon specified in the activity meta-data
3160 Bundle metaData = packageManager.getActivityInfo(
3161 activityName, PackageManager.GET_META_DATA).metaData;
3162 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003163 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003164 if (iconResId != 0) {
3165 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003166 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003167 }
3168 }
3169 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003170 // This can happen if the activity defines an invalid drawable
3171 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3172 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003173 } catch (Resources.NotFoundException nfe) {
3174 // This can happen if the activity defines an invalid drawable
3175 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3176 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003177 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003178 return null;
3179 }
3180
3181 // if successful in getting icon, return it; otherwise, set button to use default drawable
3182 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003183 int buttonId, ComponentName activityName, int fallbackDrawableId,
3184 String toolbarResourceName) {
3185 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003186 Resources r = getResources();
3187 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3188 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003189
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003190 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003191 // If we were unable to find the icon via the meta-data, use a generic one
3192 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003193 toolbarIcon = r.getDrawable(fallbackDrawableId);
3194 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003195 if (button != null) {
3196 button.setCompoundDrawables(toolbarIcon, null, null, null);
3197 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003198 return null;
3199 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003200 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003201 if (button != null) {
3202 button.setCompoundDrawables(toolbarIcon, null, null, null);
3203 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003204 return toolbarIcon.getConstantState();
3205 }
3206 }
3207
3208 // if successful in getting icon, return it; otherwise, set button to use default drawable
3209 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003210 int buttonId, ComponentName activityName, int fallbackDrawableId,
3211 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003212 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003213 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003214
Michael Jurka19e0fc52011-07-22 18:00:21 -07003215 if (button != null) {
3216 // If we were unable to find the icon via the meta-data, use a
3217 // generic one
3218 if (toolbarIcon == null) {
3219 button.setImageResource(fallbackDrawableId);
3220 } else {
3221 button.setImageDrawable(toolbarIcon);
3222 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003223 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003224
3225 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3226
Michael Jurka0423dcf2010-10-05 14:56:18 -07003227 }
3228
Winson Chung1b884092012-06-08 17:13:02 -07003229 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003230 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003231 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003232 }
3233
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003234 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003235 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003236 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003237 }
3238
Winson Chungbb185bd2011-11-21 12:31:42 -08003239 private void invalidatePressedFocusedStates(View container, View button) {
3240 if (container instanceof HolographicLinearLayout) {
3241 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3242 layout.invalidatePressedFocusedStates();
3243 } else if (button instanceof HolographicImageView) {
3244 HolographicImageView view = (HolographicImageView) button;
3245 view.invalidatePressedFocusedStates();
3246 }
3247 }
3248
Winson Chungc51db6a2011-10-05 11:44:49 -07003249 private boolean updateGlobalSearchIcon() {
3250 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003251 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003252 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003253 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003254
Winson Chung1cad91e2011-05-25 17:41:01 -07003255 final SearchManager searchManager =
3256 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3257 ComponentName activityName = searchManager.getGlobalSearchActivity();
3258 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003259 int coi = getCurrentOrientationIndexForGlobalIcons();
3260 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003261 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3262 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3263 if (sGlobalSearchIcon[coi] == null) {
3264 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3265 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3266 TOOLBAR_ICON_METADATA_NAME);
3267 }
3268
Winson Chungc51db6a2011-10-05 11:44:49 -07003269 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3270 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003271 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003272 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003273 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003274 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003275 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3276 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3277 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003278 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003279 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003280 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003281 }
3282 }
3283
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003284 private void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003285 final View searchButtonContainer = findViewById(R.id.search_button_container);
3286 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003287 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003288 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003289 }
3290
Winson Chungc51db6a2011-10-05 11:44:49 -07003291 private boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003292 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003293 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003294
Winson Chungc51db6a2011-10-05 11:44:49 -07003295 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003296 final SearchManager searchManager =
3297 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3298 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3299
3300 ComponentName activityName = null;
3301 if (globalSearchActivity != null) {
3302 // Check if the global search activity handles voice search
3303 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3304 intent.setPackage(globalSearchActivity.getPackageName());
3305 activityName = intent.resolveActivity(getPackageManager());
3306 }
3307
3308 if (activityName == null) {
3309 // Fallback: check if an activity other than the global search activity
3310 // resolves this
3311 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3312 activityName = intent.resolveActivity(getPackageManager());
3313 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003314 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003315 int coi = getCurrentOrientationIndexForGlobalIcons();
3316 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003317 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3318 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3319 if (sVoiceSearchIcon[coi] == null) {
3320 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3321 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3322 TOOLBAR_ICON_METADATA_NAME);
3323 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003324 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003325 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003326 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003327 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003328 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003329 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003330 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003331 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003332 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003333 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003334 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003335 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003336
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003337 private void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003338 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3339 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003340 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003341 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003342 }
3343
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003344 public void setVoiceButtonProxyVisible(boolean visible) {
3345 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3346 if (voiceButtonProxy != null) {
3347 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3348 }
3349 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003350 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003351 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003352 */
3353 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003354 final View marketButton = findViewById(R.id.market_button);
3355 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3356 // Find the app market activity by resolving an intent.
3357 // (If multiple app markets are installed, it will return the ResolverActivity.)
3358 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003359 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003360 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003361 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003362 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003363 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3364 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003365 marketButton.setVisibility(View.VISIBLE);
3366 } else {
3367 // We should hide and disable the view so that we don't try and restore the visibility
3368 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3369 marketButton.setVisibility(View.GONE);
3370 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003371 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003372 }
3373
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003374 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003375 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3376 Resources r = getResources();
3377 Drawable marketIconDrawable = d.newDrawable(r);
3378 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3379 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3380 marketIconDrawable.setBounds(0, 0, w, h);
3381
3382 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003383 }
3384
Michael Jurkad7c28052012-04-27 15:43:36 -07003385 @Override
3386 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003387 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003388 final List<CharSequence> text = event.getText();
3389 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003390 // Populate event with a fake title based on the current state.
3391 if (mState == State.APPS_CUSTOMIZE) {
3392 text.add(getString(R.string.all_apps_button_label));
3393 } else {
3394 text.add(getString(R.string.all_apps_home_button_label));
3395 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003396 return result;
3397 }
3398
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003399 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003400 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003401 */
3402 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3403 @Override
3404 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003405 closeSystemDialogs();
3406 }
3407 }
3408
3409 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003410 * Receives notifications whenever the appwidgets are reset.
3411 */
3412 private class AppWidgetResetObserver extends ContentObserver {
3413 public AppWidgetResetObserver() {
3414 super(new Handler());
3415 }
3416
3417 @Override
3418 public void onChange(boolean selfChange) {
3419 onAppWidgetReset();
3420 }
3421 }
3422
3423 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003424 * If the activity is currently paused, signal that we need to run the passed Runnable
3425 * in onResume.
3426 *
3427 * This needs to be called from incoming places where resources might have been loaded
3428 * while we are paused. That is becaues the Configuration might be wrong
3429 * when we're not running, and if it comes back to what it was when we
3430 * were paused, we are not restarted.
3431 *
3432 * Implementation of the method from LauncherModel.Callbacks.
3433 *
3434 * @return true if we are currently paused. The caller might be able to
3435 * skip some work in that case since we will come back again.
3436 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003437 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003438 if (mPaused) {
3439 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003440 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003441 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003442 }
3443 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003444 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003445 return true;
3446 } else {
3447 return false;
3448 }
3449 }
3450
Michael Jurkac402cd92013-05-20 15:49:32 +02003451 private boolean waitUntilResume(Runnable run) {
3452 return waitUntilResume(run, false);
3453 }
3454
Michael Jurka1e2f4652013-07-08 18:03:46 -07003455 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003456 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003457 }
3458
3459 public void removeOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003460 mOnResumeCallbacks.remove(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003461 }
3462
Michael Jurka7607c2f2013-04-03 14:33:19 -07003463 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003464 * If the activity is currently paused, signal that we need to re-run the loader
3465 * in onResume.
3466 *
3467 * This needs to be called from incoming places where resources might have been loaded
3468 * while we are paused. That is becaues the Configuration might be wrong
3469 * when we're not running, and if it comes back to what it was when we
3470 * were paused, we are not restarted.
3471 *
3472 * Implementation of the method from LauncherModel.Callbacks.
3473 *
3474 * @return true if we are currently paused. The caller might be able to
3475 * skip some work in that case since we will come back again.
3476 */
3477 public boolean setLoadOnResume() {
3478 if (mPaused) {
3479 Log.i(TAG, "setLoadOnResume");
3480 mOnResumeNeedsLoad = true;
3481 return true;
3482 } else {
3483 return false;
3484 }
3485 }
3486
3487 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003488 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003489 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003490 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003491 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003492 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003493 } else {
3494 return SCREEN_COUNT / 2;
3495 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003496 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003497
Joe Onorato9c1289c2009-08-17 11:03:03 -04003498 /**
3499 * Refreshes the shortcuts shown on the workspace.
3500 *
3501 * Implementation of the method from LauncherModel.Callbacks.
3502 */
3503 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003504 // If we're starting binding all over again, clear any bind calls we'd postponed in
3505 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3506 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003507 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003508
Michael Jurka7607c2f2013-04-03 14:33:19 -07003509 final Workspace workspace = mWorkspace;
Adam Cohendf035382011-04-11 17:22:04 -07003510 mWorkspace.clearDropTargets();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003511 int count = workspace.getChildCount();
3512 for (int i = 0; i < count; i++) {
Joe Onorato3c2f7e12009-10-31 19:17:31 -04003513 // Use removeAllViewsInLayout() to avoid an extra requestLayout() and invalidate().
Winson Chung7a25a9e2011-01-30 13:33:56 -08003514 final CellLayout layoutParent = (CellLayout) workspace.getChildAt(i);
3515 layoutParent.removeAllViewsInLayout();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003516 }
Michael Jurka05bf6442011-11-30 20:28:53 -08003517 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003518 if (mHotseat != null) {
3519 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003520 }
3521 }
3522
Adam Cohendcd297f2013-06-18 13:13:40 -07003523 @Override
3524 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003525 bindAddScreens(orderedScreenIds);
3526 mWorkspace.addExtraEmptyScreen();
3527 }
3528
3529 @Override
3530 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003531 int count = orderedScreenIds.size();
3532 for (int i = 0; i < count; i++) {
Winson Chung8481e322013-08-09 16:06:38 -07003533 Log.d(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")");
Winson Chung64359a52013-07-08 17:17:08 -07003534 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i), false);
Adam Cohendcd297f2013-06-18 13:13:40 -07003535 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003536 }
3537
Adam Cohen39a06042013-07-19 14:30:12 -07003538 private boolean shouldShowWeightWatcher() {
3539 String spKey = LauncherAppState.getSharedPreferencesKey();
3540 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003541 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003542
3543 return show;
3544 }
3545
3546 private void toggleShowWeightWatcher() {
3547 String spKey = LauncherAppState.getSharedPreferencesKey();
3548 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3549 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3550
3551 show = !show;
3552
3553 SharedPreferences.Editor editor = sp.edit();
3554 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3555 editor.commit();
3556
3557 if (mWeightWatcher != null) {
3558 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3559 }
3560 }
3561
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003562 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003563 * Bind the items start-end from the list.
3564 *
3565 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003566 */
Winson Chung64359a52013-07-08 17:17:08 -07003567 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3568 final boolean forceAnimateIcons) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003569 if (waitUntilResume(new Runnable() {
3570 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003571 bindItems(shortcuts, start, end, forceAnimateIcons);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003572 }
3573 })) {
3574 return;
3575 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003576
Winson Chungf0c6ae02012-03-21 16:10:31 -07003577 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003578 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3579 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003580 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003581 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003582 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003583 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003584 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003585
3586 // Short circuit if we are loading dock items for a configuration which has no dock
3587 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3588 mHotseat == null) {
3589 continue;
3590 }
3591
Joe Onorato9c1289c2009-08-17 11:03:03 -04003592 switch (item.itemType) {
3593 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3594 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003595 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003596 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003597
Winson Chung64359a52013-07-08 17:17:08 -07003598 /*
3599 * TODO: FIX collision case
3600 */
Winson Chungce376632013-07-11 16:12:41 -07003601 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3602 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3603 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3604 throw new RuntimeException("OCCUPIED");
3605 }
Winson Chung64359a52013-07-08 17:17:08 -07003606 }
3607
Adam Cohendcd297f2013-06-18 13:13:40 -07003608 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3609 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003610 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003611 // Animate all the applications up now
3612 shortcut.setAlpha(0f);
3613 shortcut.setScaleX(0f);
3614 shortcut.setScaleY(0f);
3615 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003616 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003617 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003618 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003619 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003620 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003621 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003622 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003623 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3624 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003625 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003626 default:
3627 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003628 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003629 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003630
Winson Chung997a9232013-07-24 15:33:46 -07003631 if (animateIcons) {
3632 // Animate to the correct page
3633 if (newShortcutsScreenId > -1) {
3634 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3635 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3636 if (newShortcutsScreenId != currentScreenId) {
3637 mWorkspace.snapToPage(newScreenIndex);
3638 }
3639 }
3640
Winson Chung64359a52013-07-08 17:17:08 -07003641 // We post the animation slightly delayed to prevent slowdowns when we are loading
3642 // right after we return to launcher.
3643 mWorkspace.postDelayed(new Runnable() {
3644 public void run() {
3645 anim.playTogether(bounceAnims);
3646 anim.start();
3647 }
3648 }, NEW_APPS_ANIMATION_DELAY);
3649 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003650 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003651 }
3652
Joe Onorato9c1289c2009-08-17 11:03:03 -04003653 /**
3654 * Implementation of the method from LauncherModel.Callbacks.
3655 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003656 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
3657 if (waitUntilResume(new Runnable() {
3658 public void run() {
3659 bindFolders(folders);
3660 }
3661 })) {
3662 return;
3663 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003664 sFolders.clear();
3665 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003666 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003667
3668 /**
3669 * Add the views for a widget to the workspace.
3670 *
3671 * Implementation of the method from LauncherModel.Callbacks.
3672 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003673 public void bindAppWidget(final LauncherAppWidgetInfo item) {
3674 if (waitUntilResume(new Runnable() {
3675 public void run() {
3676 bindAppWidget(item);
3677 }
3678 })) {
3679 return;
3680 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003681
Daniel Sandler843e8602010-06-07 14:59:01 -04003682 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3683 if (DEBUG_WIDGETS) {
3684 Log.d(TAG, "bindAppWidget: " + item);
3685 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003686 final Workspace workspace = mWorkspace;
3687
3688 final int appWidgetId = item.appWidgetId;
3689 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003690 if (DEBUG_WIDGETS) {
3691 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3692 }
3693
Joe Onorato9c1289c2009-08-17 11:03:03 -04003694 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3695
Joe Onorato9c1289c2009-08-17 11:03:03 -04003696 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003697 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003698
Adam Cohendcd297f2013-06-18 13:13:40 -07003699 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003700 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003701 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3702
Joe Onorato9c1289c2009-08-17 11:03:03 -04003703 workspace.requestLayout();
3704
Daniel Sandler843e8602010-06-07 14:59:01 -04003705 if (DEBUG_WIDGETS) {
3706 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3707 + (SystemClock.uptimeMillis()-start) + "ms");
3708 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003709 }
3710
Adam Cohen1462de32012-07-24 22:34:36 -07003711 public void onPageBoundSynchronously(int page) {
3712 mSynchronouslyBoundPages.add(page);
3713 }
3714
Joe Onorato9c1289c2009-08-17 11:03:03 -04003715 /**
3716 * Callback saying that there aren't any more items to bind.
3717 *
3718 * Implementation of the method from LauncherModel.Callbacks.
3719 */
Adam Cohene25af792013-06-06 23:08:25 -07003720 public void finishBindingItems(final boolean upgradePath) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003721 if (waitUntilResume(new Runnable() {
3722 public void run() {
Adam Cohene25af792013-06-06 23:08:25 -07003723 finishBindingItems(upgradePath);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003724 }
3725 })) {
3726 return;
3727 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003728 if (mSavedState != null) {
3729 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003730 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003731 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003732 mSavedState = null;
3733 }
3734
Winson Chung98ca0f72013-07-29 12:58:51 -07003735 // Create the custom content page here before onLayout to prevent flashing
3736 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3737 mWorkspace.createCustomContentPage();
3738 }
3739
Adam Cohen1462de32012-07-24 22:34:36 -07003740 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003741
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003742 // If we received the result of any pending adds while the loader was running (e.g. the
3743 // widget configuration forced an orientation change), process them now.
3744 for (int i = 0; i < sPendingAddList.size(); i++) {
3745 completeAdd(sPendingAddList.get(i));
3746 }
3747 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003748
Winson Chungc51db6a2011-10-05 11:44:49 -07003749 // Update the market app icon as necessary (the other icons will be managed in response to
3750 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003751 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003752
Winson Chungf0c6ae02012-03-21 16:10:31 -07003753 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003754 if (upgradePath) {
Adam Cohena0b57492013-06-14 15:33:35 -07003755 mWorkspace.stripDuplicateApps();
3756 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003757 }
Adam Cohen99894d92013-06-14 11:22:59 -07003758
Adam Cohen66a01fd2013-06-11 12:48:00 -07003759 mWorkspace.post(new Runnable() {
3760 @Override
3761 public void run() {
3762 onFinishBindingItems();
3763 }
3764 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003765 }
3766
Winson Chunga2413752012-04-03 14:22:34 -07003767 private boolean canRunNewAppsAnimation() {
3768 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3769 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3770 }
3771
Winson Chungc9168342013-06-26 14:54:55 -07003772 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3773 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3774 PropertyValuesHolder.ofFloat("alpha", 1f),
3775 PropertyValuesHolder.ofFloat("scaleX", 1f),
3776 PropertyValuesHolder.ofFloat("scaleY", 1f));
3777 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3778 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3779 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3780 return bounceAnim;
3781 }
3782
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003783 @Override
3784 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003785 boolean searchVisible = updateGlobalSearchIcon();
3786 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003787 if (mSearchDropTargetBar != null) {
3788 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3789 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003790 }
3791
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003792 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003793 * Add the icons for all apps.
3794 *
3795 * Implementation of the method from LauncherModel.Callbacks.
3796 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003797 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003798 if (mIntentsOnWorkspaceFromUpgradePath != null) {
3799 getHotseat().addAllAppsFolder(mIconCache, apps,
Winson Chung156ab5b2013-07-12 14:14:16 -07003800 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
Winson Chung64359a52013-07-08 17:17:08 -07003801 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003802 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003803 }
3804
3805 /**
3806 * A package was updated.
3807 *
3808 * Implementation of the method from LauncherModel.Callbacks.
3809 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003810 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
3811 if (waitUntilResume(new Runnable() {
3812 public void run() {
3813 bindAppsUpdated(apps);
3814 }
3815 })) {
3816 return;
3817 }
3818
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003819 if (mWorkspace != null) {
3820 mWorkspace.updateShortcuts(apps);
3821 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003822 }
3823
3824 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003825 * A package was uninstalled. We take both the super set of packageNames
3826 * in addition to specific applications to remove, the reason being that
3827 * this can be called when a package is updated as well. In that scenario,
3828 * we only remove specific components from the workspace, where as
3829 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003830 *
3831 * Implementation of the method from LauncherModel.Callbacks.
3832 */
Winson Chung83892cc2013-05-01 16:53:33 -07003833 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3834 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003835 final boolean packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003836 if (waitUntilResume(new Runnable() {
3837 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003838 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003839 }
3840 })) {
3841 return;
3842 }
3843
Winson Chung64359a52013-07-08 17:17:08 -07003844 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003845 mWorkspace.removeItemsByPackageName(packageNames);
3846 } else {
3847 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003848 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003849
Winson Chunga1820962011-10-03 16:31:06 -07003850 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003851 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003852 }
Joe Onoratobe386092009-11-17 17:32:16 -08003853
3854 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003855 * A number of packages were updated.
3856 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003857 private ArrayList<Object> mWidgetsAndShortcuts;
3858 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003859 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003860 bindPackagesUpdated(mWidgetsAndShortcuts);
3861 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003862 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003863 };
3864
3865 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3866 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3867 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003868 return;
3869 }
3870
Winson Chung64359a52013-07-08 17:17:08 -07003871 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003872 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003873 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003874 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003875 }
3876
Winson Chung400438b2011-01-16 17:53:48 -08003877 private int mapConfigurationOriActivityInfoOri(int configOri) {
3878 final Display d = getWindowManager().getDefaultDisplay();
3879 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3880 switch (d.getRotation()) {
3881 case Surface.ROTATION_0:
3882 case Surface.ROTATION_180:
3883 // We are currently in the same basic orientation as the natural orientation
3884 naturalOri = configOri;
3885 break;
3886 case Surface.ROTATION_90:
3887 case Surface.ROTATION_270:
3888 // We are currently in the other basic orientation to the natural orientation
3889 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3890 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3891 break;
3892 }
3893
3894 int[] oriMap = {
3895 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3896 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3897 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3898 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3899 };
3900 // Since the map starts at portrait, we need to offset if this device's natural orientation
3901 // is landscape.
3902 int indexOffset = 0;
3903 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3904 indexOffset = 1;
3905 }
3906 return oriMap[(d.getRotation() + indexOffset) % 4];
3907 }
Adam Cohen446e9402011-09-15 18:21:21 -07003908
Winson Chung4b919f82012-05-01 10:44:08 -07003909 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003910 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003911 getResources().getBoolean(R.bool.allow_rotation);
3912 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003913 }
Winson Chung4b919f82012-05-01 10:44:08 -07003914 public void lockScreenOrientation() {
3915 if (isRotationEnabled()) {
3916 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3917 .getConfiguration().orientation));
3918 }
3919 }
3920 public void unlockScreenOrientation(boolean immediate) {
3921 if (isRotationEnabled()) {
3922 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003923 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003924 } else {
3925 mHandler.postDelayed(new Runnable() {
3926 public void run() {
3927 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3928 }
3929 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003930 }
Winson Chung4b919f82012-05-01 10:44:08 -07003931 }
Winson Chung400438b2011-01-16 17:53:48 -08003932 }
3933
Winson Chung82f55532011-08-09 14:14:23 -07003934 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003935 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003936 if (DISABLE_CLINGS) {
3937 return false;
3938 }
3939
Brett Chabot2a9e2282011-08-23 15:03:13 -07003940 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003941 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003942
Michael Jurkae233a8b2013-03-19 13:49:20 +01003943 // Restricted secondary users (child mode) will potentially have very few apps
3944 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003945// boolean supportsLimitedUsers =
3946// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3947// Account[] accounts = AccountManager.get(this).getAccounts();
3948// if (supportsLimitedUsers && accounts.length == 0) {
3949// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3950// Bundle restrictions = um.getUserRestrictions();
3951// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3952// return false;
3953// }
3954// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003955 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003956 }
Michael Jurka22143132012-10-04 17:42:38 +02003957
Winson Chung7d7541e2011-09-16 20:14:36 -07003958 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003959 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003960 if (cling != null) {
3961 cling.init(this, positionData);
3962 cling.setVisibility(View.VISIBLE);
3963 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3964 if (animate) {
3965 cling.buildLayer();
3966 cling.setAlpha(0f);
3967 cling.animate()
3968 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07003969 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07003970 .setDuration(SHOW_CLING_DURATION)
3971 .setStartDelay(delay)
3972 .start();
3973 } else {
3974 cling.setAlpha(1f);
3975 }
Michael Jurka22143132012-10-04 17:42:38 +02003976 cling.setFocusableInTouchMode(true);
3977 cling.post(new Runnable() {
3978 public void run() {
3979 cling.setFocusable(true);
3980 cling.requestFocus();
3981 }
3982 });
3983 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
3984 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07003985 }
3986 return cling;
3987 }
Michael Jurka22143132012-10-04 17:42:38 +02003988
Winson Chung7d7541e2011-09-16 20:14:36 -07003989 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08003990 // To catch cases where siblings of top-level views are made invisible, just check whether
3991 // the cling is directly set to GONE before dismissing it.
3992 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07003993 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07003994 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07003995 anim.addListener(new AnimatorListenerAdapter() {
3996 public void onAnimationEnd(Animator animation) {
3997 cling.setVisibility(View.GONE);
3998 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08003999 // We should update the shared preferences on a background thread
4000 new Thread("dismissClingThread") {
4001 public void run() {
4002 SharedPreferences.Editor editor = mSharedPrefs.edit();
4003 editor.putBoolean(flag, true);
4004 editor.commit();
4005 }
4006 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004007 };
4008 });
4009 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004010 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004011 }
4012 }
Michael Jurka22143132012-10-04 17:42:38 +02004013
Winson Chung9d9d74f2011-09-19 11:49:12 -07004014 private void removeCling(int id) {
4015 final View cling = findViewById(id);
4016 if (cling != null) {
4017 final ViewGroup parent = (ViewGroup) cling.getParent();
4018 parent.post(new Runnable() {
4019 @Override
4020 public void run() {
4021 parent.removeView(cling);
4022 }
4023 });
Michael Jurka22143132012-10-04 17:42:38 +02004024 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004025 }
4026 }
Michael Jurka974c3862012-05-22 22:00:31 -07004027
4028 private boolean skipCustomClingIfNoAccounts() {
4029 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4030 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4031 if (customCling) {
4032 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004033 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004034 Account[] accounts = am.getAccountsByType("com.google");
4035 return accounts.length == 0;
4036 }
4037 return false;
4038 }
4039
Winson Chung7d7541e2011-09-16 20:14:36 -07004040 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004041 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004042 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004043 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4044 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004045 // If we're not using the default workspace layout, replace workspace cling
4046 // with a custom workspace cling (usually specified in an overlay)
4047 // For now, only do this on tablets
4048 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004049 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004050 // Use a custom cling
4051 View cling = findViewById(R.id.workspace_cling);
4052 ViewGroup clingParent = (ViewGroup) cling.getParent();
4053 int clingIndex = clingParent.indexOfChild(cling);
4054 clingParent.removeViewAt(clingIndex);
4055 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4056 clingParent.addView(customCling, clingIndex);
4057 customCling.setId(R.id.workspace_cling);
4058 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004059 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004060 } else {
4061 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004062 }
4063 }
4064 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004065 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004066 if (isClingsEnabled() &&
4067 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004068 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004069 } else {
4070 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004071 }
4072 }
4073 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004074 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004075 if (isClingsEnabled() &&
4076 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4077 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004078 } else {
4079 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004080 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004081 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004082 }
4083 public boolean isFolderClingVisible() {
4084 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004085 if (cling != null) {
4086 return cling.getVisibility() == View.VISIBLE;
4087 }
4088 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004089 }
Winson Chung82f55532011-08-09 14:14:23 -07004090 public void dismissWorkspaceCling(View v) {
4091 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004092 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004093 }
4094 public void dismissAllAppsCling(View v) {
4095 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004096 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4097 }
4098 public void dismissFolderCling(View v) {
4099 Cling cling = (Cling) findViewById(R.id.folder_cling);
4100 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004101 }
4102
Winson Chung80baf5a2010-08-09 16:03:15 -07004103 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004104 * Prints out out state for debugging.
4105 */
4106 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004107 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004108 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004109 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4110 Log.d(TAG, "mRestoring=" + mRestoring);
4111 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4112 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004113 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004114 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004115
Winson Chung785d2eb2011-04-14 16:08:02 -07004116 if (mAppsCustomizeContent != null) {
4117 mAppsCustomizeContent.dumpState();
4118 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004119 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004120 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004121
4122 @Override
4123 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4124 super.dump(prefix, fd, writer, args);
4125 writer.println(" ");
4126 writer.println("Debug logs: ");
4127 for (int i = 0; i < sDumpLogs.size(); i++) {
4128 writer.println(" " + sDumpLogs.get(i));
4129 }
4130 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004131
4132 public static void dumpDebugLogsToConsole() {
4133 Log.d(TAG, "");
4134 Log.d(TAG, "*********************");
4135 Log.d(TAG, "Launcher debug logs: ");
4136 for (int i = 0; i < sDumpLogs.size(); i++) {
4137 Log.d(TAG, " " + sDumpLogs.get(i));
4138 }
4139 Log.d(TAG, "*********************");
4140 Log.d(TAG, "");
4141 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004142}
Michael Jurka2763be32011-02-24 11:19:57 -08004143
Michael Jurkaabded662011-03-04 12:06:57 -08004144interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004145 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004146 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004147 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004148 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004149 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004150}