blob: 85bcdc0191f84c7897b019ef1d2f3a7a2faa7559 [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;
Winson Chung5f8afe62013-08-12 16:19:28 -070054import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040058import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070059import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080060import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020061import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080062import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070063import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070064import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040065import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070066import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080067import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080068import android.text.Selection;
69import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070070import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080071import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070072import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070073import android.view.Display;
74import android.view.Gravity;
75import android.view.HapticFeedbackConstants;
76import android.view.KeyEvent;
77import android.view.LayoutInflater;
78import android.view.Menu;
79import android.view.MenuItem;
80import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
86import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080087import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080089import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070090import android.view.animation.AccelerateDecelerateInterpolator;
Winson Chung7a74ac92011-09-20 17:43:51 -070091import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080092import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080093import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070094import android.widget.Advanceable;
95import android.widget.FrameLayout;
96import android.widget.ImageView;
97import android.widget.TextView;
98import android.widget.Toast;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070099
Daniel Sandler325dc232013-06-05 22:57:57 -0400100import com.android.launcher3.DropTarget.DragObject;
Romain Guyedcce092010-03-04 13:03:17 -0800101
Adam Cohenc0dcf592011-06-01 15:30:43 -0700102import java.io.DataInputStream;
103import java.io.DataOutputStream;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700104import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700105import java.io.FileNotFoundException;
106import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700107import java.io.PrintWriter;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700108import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700109import java.util.Collection;
110import java.util.Collections;
111import java.util.Comparator;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.util.HashMap;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700113import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700114import java.util.List;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700115import java.util.Set;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800117/**
118 * Default launcher application.
119 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100120public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700121 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungcd810732012-06-18 16:45:43 -0700122 View.OnTouchListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800123 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700124 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800125
Daniel Sandlerf061f822013-06-27 13:59:36 -0400126 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400127 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700128 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400129 static final boolean DEBUG_RESUME_TIME = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700130
Winson Chung70d72102011-08-12 11:24:35 -0700131 private static final int MENU_GROUP_WALLPAPER = 1;
132 private static final int MENU_WALLPAPER_SETTINGS = Menu.FIRST + 1;
133 private static final int MENU_MANAGE_APPS = MENU_WALLPAPER_SETTINGS + 1;
Winson Chung236d4312011-08-22 15:12:27 -0700134 private static final int MENU_SYSTEM_SETTINGS = MENU_MANAGE_APPS + 1;
135 private static final int MENU_HELP = MENU_SYSTEM_SETTINGS + 1;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800136
137 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700138 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139 private static final int REQUEST_PICK_APPLICATION = 6;
140 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700141 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700142 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Michael Jurka8b805b12012-04-18 14:23:14 -0700144 private static final int REQUEST_BIND_APPWIDGET = 11;
145
Mathew Inwood876a8462013-06-14 14:12:41 +0100146 /**
147 * IntentStarter uses request codes starting with this. This must be greater than all activity
148 * request codes used internally.
149 */
150 protected static final int REQUEST_LAST = 100;
151
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
153
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800154 static final int SCREEN_COUNT = 5;
155 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800156
Romain Guy98d01652009-06-30 16:21:04 -0700157 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800158 // To turn on these properties, type
159 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
160 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
161 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162
Winson Chung2672ff92012-05-04 16:22:30 -0700163 // The Intent extra that defines whether to ignore the launch animation
164 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400165 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700166
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167 // Type: int
168 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700169 // Type: int
170 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700172 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
173 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
175 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700176 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800177 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700178 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 // Type: boolean
180 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
181 // Type: long
182 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700183 // Type: int
184 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
185 // Type: int
186 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
187 // Type: parcelable
188 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100190 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700191 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100192 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700193 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100194 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700195
Adam Cohen39a06042013-07-19 14:30:12 -0700196 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700197 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700198
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700199 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700200 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700201 private State mState = State.WORKSPACE;
202 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700203
Joe Onorato9c1289c2009-08-17 11:03:03 -0400204 static final int APPWIDGET_HOST_ID = 1024;
Winson Chung557d6ed2011-07-08 15:34:52 -0700205 private static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
206 private static final int EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT = 600;
Winson Chung7a74ac92011-09-20 17:43:51 -0700207 private static final int SHOW_CLING_DURATION = 550;
Winson Chung7d7541e2011-09-16 20:14:36 -0700208 private static final int DISMISS_CLING_DURATION = 250;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800209
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800210 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800211 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800212
Winson Chunga2413752012-04-03 14:22:34 -0700213 // How long to wait before the new-shortcut animation automatically pans the workspace
214 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 10;
Winson Chung64359a52013-07-08 17:17:08 -0700215 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700216
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800217 private final BroadcastReceiver mCloseSystemDialogsReceiver
218 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800219 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
220
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800221 private LayoutInflater mInflater;
222
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800223 private Workspace mWorkspace;
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;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700240 private View mOverviewPanel;
241
Michael Jurka838a4ca2011-02-07 13:33:06 -0800242 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700243
Winson Chungc51db6a2011-10-05 11:44:49 -0700244 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700245 private AppsCustomizeTabHost mAppsCustomizeTabHost;
246 private AppsCustomizePagedView mAppsCustomizeContent;
247 private boolean mAutoAdvanceRunning = false;
Cristina Stancu476493b2013-08-07 17:20:14 +0100248 private View mQsbBar;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700251 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
252 // scroll issues (because the workspace may not have been measured yet) and extra work.
253 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
254 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255
256 private SpannableStringBuilder mDefaultKeySsb = null;
257
Joe Onorato9c1289c2009-08-17 11:03:03 -0400258 private boolean mWorkspaceLoading = true;
259
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800260 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800261 private boolean mRestoring;
262 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700263 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264
Michael Jurka1e2f4652013-07-08 18:03:46 -0700265 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700266 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
267
Winson Chung4a2afa32012-07-19 14:53:05 -0700268 // Keep track of whether the user has left launcher
269 private static boolean sPausedFromUserAction = false;
270
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800271 private Bundle mSavedInstanceState;
272
Joe Onorato9c1289c2009-08-17 11:03:03 -0400273 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800274 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800275 private boolean mUserPresent = true;
276 private boolean mVisible = false;
277 private boolean mAttached = false;
Adam Cohen7564d982013-06-06 23:11:58 -0700278 private static final boolean DISABLE_CLINGS = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400279
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700280 private static LocaleConfiguration sLocaleConfiguration = null;
281
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700282 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700283
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700284 private Intent mAppMarketIntent = null;
285
Adam Cohended9f8d2010-11-03 13:25:16 -0700286 // Related to the auto-advancing of widgets
287 private final int ADVANCE_MSG = 1;
288 private final int mAdvanceInterval = 20000;
289 private final int mAdvanceStagger = 250;
290 private long mAutoAdvanceSentTime;
291 private long mAutoAdvanceTimeLeft = -1;
292 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
293 new HashMap<View, AppWidgetProviderInfo>();
294
Winson Chung400438b2011-01-16 17:53:48 -0800295 // Determines how long to wait after a rotation before restoring the screen orientation to
296 // match the sensor state.
297 private final int mRestoreScreenOrientationDelay = 500;
298
Michael Jurka4ef207b2010-11-29 17:05:45 -0800299 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700300 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
301 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
302 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800303
Craig Mautner360310b2012-10-26 15:13:08 -0700304 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700305
Adam Cohen1462de32012-07-24 22:34:36 -0700306 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
307
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700308 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
309
Winson Chung46353de2012-02-16 14:05:10 -0800310 // We only want to get the SharedPreferences once since it does an FS stat each time we get
311 // it from the context.
312 private SharedPreferences mSharedPrefs;
313
Adam Cohene25af792013-06-06 23:08:25 -0700314 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
315
Winson Chungf0c6ae02012-03-21 16:10:31 -0700316 // Holds the page that we need to animate to, and the icon views that we need to animate up
317 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700318 private ImageView mFolderIconImageView;
319 private Bitmap mFolderIconBitmap;
320 private Canvas mFolderIconCanvas;
321 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700322
Michael Jurkaddd62e92011-02-16 17:49:14 -0800323 private BubbleTextView mWaitingForResume;
324
Michael Jurka22143132012-10-04 17:42:38 +0200325 private HideFromAccessibilityHelper mHideFromAccessibilityHelper
326 = new HideFromAccessibilityHelper();
327
Michael Jurkac1f5d262011-09-30 19:32:27 -0700328 private Runnable mBuildLayersRunnable = new Runnable() {
329 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700330 if (mWorkspace != null) {
331 mWorkspace.buildPageHardwareLayers();
332 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700333 }
334 };
335
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800336 private static ArrayList<PendingAddArguments> sPendingAddList
337 = new ArrayList<PendingAddArguments>();
338
Michael Jurka0a457bf2012-11-19 14:05:05 -0800339 private static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
340
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800341 private static class PendingAddArguments {
342 int requestCode;
343 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700344 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700345 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800346 int cellX;
347 int cellY;
348 }
349
Daniel Sandlerff02d492013-08-05 02:12:05 -0400350 private Stats mStats;
351
Michael Jurka0a457bf2012-11-19 14:05:05 -0800352 private static boolean isPropertyEnabled(String propertyName) {
353 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700354 }
355
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800356 @Override
357 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700358 if (DEBUG_STRICT_MODE) {
359 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
360 .detectDiskReads()
361 .detectDiskWrites()
362 .detectNetwork() // or .detectAll() for all detectable problems
363 .penaltyLog()
364 .build());
365 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
366 .detectLeakedSqlLiteObjects()
367 .detectLeakedClosableObjects()
368 .penaltyLog()
369 .penaltyDeath()
370 .build());
371 }
372
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800373 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400374
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400375 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400376 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung5f8afe62013-08-12 16:19:28 -0700377
378 // Determine the dynamic grid properties
379 Point smallestSize = new Point();
380 Point largestSize = new Point();
381 Point realSize = new Point();
382 Display display = getWindowManager().getDefaultDisplay();
383 display.getCurrentSizeRange(smallestSize, largestSize);
384 display.getRealSize(realSize);
385 // Lazy-initialize the dynamic grid
386 DeviceProfile grid = app.initDynamicGrid(this,
387 Math.min(smallestSize.x, smallestSize.y),
388 Math.min(largestSize.x, largestSize.y),
389 realSize.x, realSize.y);
390
391 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400392 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700393 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800394 mModel = app.setLauncher(this);
395 mIconCache = app.getIconCache();
Joe Onorato41a12d22009-10-31 18:30:00 -0400396 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800397 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700398
Daniel Sandlerff02d492013-08-05 02:12:05 -0400399 mStats = new Stats(this);
400
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700401 mAppWidgetManager = AppWidgetManager.getInstance(this);
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700402 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
403 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700404
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700405 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
406 // this also ensures that any synchronous binding below doesn't re-trigger another
407 // LauncherModel load.
408 mPaused = false;
409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200411 android.os.Debug.startMethodTracing(
412 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800413 }
414
415 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 setContentView(R.layout.launcher);
417 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100418 grid.layout(this);
Winson Chung7d7541e2011-09-16 20:14:36 -0700419 showFirstRunWorkspaceCling();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800421 registerContentObservers();
422
Joe Onorato7c312c12009-08-13 21:36:53 -0700423 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700424
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 mSavedState = savedInstanceState;
426 restoreState(mSavedState);
427
Winson Chunga12a2502010-12-20 14:41:35 -0800428 // Update customization drawer _after_ restoring the states
Winson Chung785d2eb2011-04-14 16:08:02 -0700429 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200430 mAppsCustomizeContent.onPackagesUpdated(
431 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chung785d2eb2011-04-14 16:08:02 -0700432 }
Winson Chunga12a2502010-12-20 14:41:35 -0800433
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800434 if (PROFILE_STARTUP) {
435 android.os.Debug.stopMethodTracing();
436 }
437
438 if (!mRestoring) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700439 if (sPausedFromUserAction) {
440 // If the user leaves launcher, then we should just load items asynchronously when
441 // they return.
442 mModel.startLoader(true, -1);
443 } else {
444 // We only load the page synchronously if the user rotates (or triggers a
445 // configuration change) while launcher is in the foreground
446 mModel.startLoader(true, mWorkspace.getCurrentPage());
447 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 }
449
450 // For handling default keys
451 mDefaultKeySsb = new SpannableStringBuilder();
452 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800453
454 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
455 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800456
Adam Cohenf9426d52012-06-04 17:26:21 -0700457 updateGlobalIcons();
458
459 // On large interfaces, we want the screen to auto-rotate based on the current orientation
460 unlockScreenOrientation(true);
461 }
462
Winson Chung4a2afa32012-07-19 14:53:05 -0700463 protected void onUserLeaveHint() {
464 super.onUserLeaveHint();
465 sPausedFromUserAction = true;
466 }
467
Winson Chung98ca0f72013-07-29 12:58:51 -0700468 /** To be overriden by subclasses to hint to Launcher that we have custom content */
469 protected boolean hasCustomContentToLeft() {
470 return false;
471 }
472
Adam Cohenf9426d52012-06-04 17:26:21 -0700473 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700474 boolean searchVisible = false;
475 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800476 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700477 int coi = getCurrentOrientationIndexForGlobalIcons();
478 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
479 sAppMarketIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700480 updateAppMarketIcon();
481 searchVisible = updateGlobalSearchIcon();
482 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700483 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700484 if (sGlobalSearchIcon[coi] != null) {
485 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700486 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700487 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700488 if (sVoiceSearchIcon[coi] != null) {
489 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700490 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700491 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700492 if (sAppMarketIcon[coi] != null) {
493 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800494 }
Winson Chungadf0c182012-08-23 14:59:07 -0700495 if (mSearchDropTargetBar != null) {
496 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
497 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 }
Romain Guycbb89e42009-06-08 15:52:54 -0700499
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800500 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700501 if (sLocaleConfiguration == null) {
502 new AsyncTask<Void, Void, LocaleConfiguration>() {
503 @Override
504 protected LocaleConfiguration doInBackground(Void... unused) {
505 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
506 readConfiguration(Launcher.this, localeConfiguration);
507 return localeConfiguration;
508 }
509
510 @Override
511 protected void onPostExecute(LocaleConfiguration result) {
512 sLocaleConfiguration = result;
513 checkForLocaleChange(); // recursive, but now with a locale configuration
514 }
515 }.execute();
516 return;
517 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700518
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800519 final Configuration configuration = getResources().getConfiguration();
520
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700521 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800522 final String locale = configuration.locale.toString();
523
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700524 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800525 final int mcc = configuration.mcc;
526
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700527 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800528 final int mnc = configuration.mnc;
529
Romain Guy84f296c2009-11-04 15:00:44 -0800530 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800531
Romain Guy84f296c2009-11-04 15:00:44 -0800532 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700533 sLocaleConfiguration.locale = locale;
534 sLocaleConfiguration.mcc = mcc;
535 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700536
Joe Onorato0589f0f2010-02-08 13:44:00 -0800537 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700538
539 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
540 new Thread("WriteLocaleConfiguration") {
Gilles Debunnedd6c9922010-10-25 11:23:41 -0700541 @Override
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700542 public void run() {
543 writeConfiguration(Launcher.this, localeConfiguration);
544 }
545 }.start();
Romain Guy98d01652009-06-30 16:21:04 -0700546 }
547 }
548
549 private static class LocaleConfiguration {
550 public String locale;
551 public int mcc = -1;
552 public int mnc = -1;
553 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700554
Romain Guy98d01652009-06-30 16:21:04 -0700555 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
556 DataInputStream in = null;
557 try {
558 in = new DataInputStream(context.openFileInput(PREFERENCES));
559 configuration.locale = in.readUTF();
560 configuration.mcc = in.readInt();
561 configuration.mnc = in.readInt();
562 } catch (FileNotFoundException e) {
563 // Ignore
564 } catch (IOException e) {
565 // Ignore
566 } finally {
567 if (in != null) {
568 try {
569 in.close();
570 } catch (IOException e) {
571 // Ignore
572 }
573 }
574 }
575 }
576
577 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
578 DataOutputStream out = null;
579 try {
580 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
581 out.writeUTF(configuration.locale);
582 out.writeInt(configuration.mcc);
583 out.writeInt(configuration.mnc);
584 out.flush();
585 } catch (FileNotFoundException e) {
586 // Ignore
587 } catch (IOException e) {
588 //noinspection ResultOfMethodCallIgnored
589 context.getFileStreamPath(PREFERENCES).delete();
590 } finally {
591 if (out != null) {
592 try {
593 out.close();
594 } catch (IOException e) {
595 // Ignore
596 }
597 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800598 }
599 }
600
Bjorn Bringertc459e522013-06-07 19:36:01 +0100601 public LayoutInflater getInflater() {
602 return mInflater;
603 }
604
Adam Cohen716b51e2011-06-30 12:09:54 -0700605 public DragLayer getDragLayer() {
606 return mDragLayer;
607 }
608
Winson Chung36a62fe2012-05-06 18:04:42 -0700609 boolean isDraggingEnabled() {
610 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
611 // that is subsequently removed from the workspace in startBinding().
612 return !mModel.isLoadingWorkspace();
613 }
614
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 static int getScreen() {
616 synchronized (sLock) {
617 return sScreen;
618 }
619 }
620
621 static void setScreen(int screen) {
622 synchronized (sLock) {
623 sScreen = screen;
624 }
625 }
626
Winson Chung557d6ed2011-07-08 15:34:52 -0700627 /**
628 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
629 * a configuration step, this allows the proper animations to run after other transitions.
630 */
631 private boolean completeAdd(PendingAddArguments args) {
Winson Chungb8472bb2011-08-05 13:49:21 -0700632 boolean result = false;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800633 switch (args.requestCode) {
634 case REQUEST_PICK_APPLICATION:
Adam Cohendcd297f2013-06-18 13:13:40 -0700635 completeAddApplication(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700636 args.cellY);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800637 break;
638 case REQUEST_PICK_SHORTCUT:
639 processShortcut(args.intent);
640 break;
641 case REQUEST_CREATE_SHORTCUT:
Adam Cohendcd297f2013-06-18 13:13:40 -0700642 completeAddShortcut(args.intent, args.container, args.screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700643 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700644 result = true;
645 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800646 case REQUEST_CREATE_APPWIDGET:
647 int appWidgetId = args.intent.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -0700648 completeAddAppWidget(appWidgetId, args.container, args.screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700649 result = true;
650 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800651 case REQUEST_PICK_WALLPAPER:
652 // We just wanted the activity result here so we can clear mWaitingForResult
653 break;
654 }
Michael Jurka27614d22012-04-02 06:40:22 -0700655 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
656 // if you turned the screen off and then back while in All Apps, Launcher would not
657 // return to the workspace. Clearing mAddInfo.container here fixes this issue
658 resetAddInfo();
Winson Chungb8472bb2011-08-05 13:49:21 -0700659 return result;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800660 }
661
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800662 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700663 protected void onActivityResult(
664 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700665 // Reset the startActivity waiting flag
666 mWaitingForResult = false;
667
Michael Jurka8b805b12012-04-18 14:23:14 -0700668 if (requestCode == REQUEST_BIND_APPWIDGET) {
669 int appWidgetId = data != null ?
670 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
671 if (resultCode == RESULT_CANCELED) {
672 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
673 } else if (resultCode == RESULT_OK) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700674 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null, mPendingAddWidgetInfo);
Michael Jurka8b805b12012-04-18 14:23:14 -0700675 }
676 return;
677 }
Winson Chung557d6ed2011-07-08 15:34:52 -0700678 boolean delayExitSpringLoadedMode = false;
Adam Cohened66b2b2012-01-23 17:28:51 -0800679 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
680 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700681
Adam Cohened66b2b2012-01-23 17:28:51 -0800682 // We have special handling for widgets
683 if (isWidgetDrop) {
684 int appWidgetId = data != null ?
685 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
Winson Chung5aaab772012-04-27 15:24:02 -0700686 if (appWidgetId < 0) {
687 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not returned from the \\" +
688 "widget configuration activity.");
689 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
690 } else {
691 completeTwoStageWidgetDrop(resultCode, appWidgetId);
692 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800693 return;
694 }
695
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 // The pattern used here is that a user PICKs a specific application,
697 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700698
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800699 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
700 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700701 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800702 final PendingAddArguments args = new PendingAddArguments();
703 args.requestCode = requestCode;
704 args.intent = data;
Winson Chung3d503fb2011-07-13 17:25:49 -0700705 args.container = mPendingAddInfo.container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700706 args.screenId = mPendingAddInfo.screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -0700707 args.cellX = mPendingAddInfo.cellX;
708 args.cellY = mPendingAddInfo.cellY;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800709 if (isWorkspaceLocked()) {
710 sPendingAddList.add(args);
711 } else {
Winson Chung557d6ed2011-07-08 15:34:52 -0700712 delayExitSpringLoadedMode = completeAdd(args);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800713 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800714 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800715 mDragLayer.clearAnimatedView();
Winson Chung557d6ed2011-07-08 15:34:52 -0700716 // Exit spring loaded mode if necessary after cancelling the configuration of a widget
Adam Cohened66b2b2012-01-23 17:28:51 -0800717 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), delayExitSpringLoadedMode,
718 null);
719 }
720
721 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700722 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700723 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800724 Runnable onCompleteRunnable = null;
725 int animationType = 0;
726
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700727 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800728 if (resultCode == RESULT_OK) {
729 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
730 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700731 mPendingAddWidgetInfo);
732 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800733 onCompleteRunnable = new Runnable() {
734 @Override
735 public void run() {
736 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700737 mPendingAddInfo.screenId, layout, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800738 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
739 null);
740 }
741 };
742 } else if (resultCode == RESULT_CANCELED) {
743 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
744 onCompleteRunnable = new Runnable() {
745 @Override
746 public void run() {
747 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED), false,
748 null);
749 }
750 };
751 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700752 if (mDragLayer.getAnimatedView() != null) {
753 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
754 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
755 animationType, boundWidget, true);
756 } else {
757 // The animated view may be null in the case of a rotation during widget configuration
758 onCompleteRunnable.run();
759 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800760 }
761
762 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700763 protected void onStop() {
764 super.onStop();
765 FirstFrameAnimatorHelper.setIsVisible(false);
766 }
767
768 @Override
769 protected void onStart() {
770 super.onStart();
771 FirstFrameAnimatorHelper.setIsVisible(true);
772 }
773
774 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800775 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200776 long startTime = 0;
777 if (DEBUG_RESUME_TIME) {
778 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400779 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200780 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700782
Winson Chung4a2afa32012-07-19 14:53:05 -0700783 // Restore the previous launcher state
784 if (mOnResumeState == State.WORKSPACE) {
785 showWorkspace(false);
786 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
787 showAllApps(false);
788 }
789 mOnResumeState = State.NONE;
790
Craig Mautner360310b2012-10-26 15:13:08 -0700791 // Background was set to gradient in onPause(), restore to black if in all apps.
792 setWorkspaceBackground(mState == State.WORKSPACE);
793
Winson Chungde0fb8f2012-05-08 14:37:08 -0700794 // Process any items that were added while Launcher was away
Winson Chung997a9232013-07-24 15:33:46 -0700795 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Winson Chungde0fb8f2012-05-08 14:37:08 -0700796
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800797 mPaused = false;
Winson Chung4a2afa32012-07-19 14:53:05 -0700798 sPausedFromUserAction = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700799 if (mRestoring || mOnResumeNeedsLoad) {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400800 mWorkspaceLoading = true;
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700801 mModel.startLoader(true, -1);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400802 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700803 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800804 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700805 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200806 // We might have postponed some bind calls until onResume (see waitUntilResume) --
807 // execute them here
808 long startTimeCallbacks = 0;
809 if (DEBUG_RESUME_TIME) {
810 startTimeCallbacks = System.currentTimeMillis();
811 }
812
813 if (mAppsCustomizeContent != null) {
814 mAppsCustomizeContent.setBulkBind(true);
815 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700816 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
817 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200818 }
819 if (mAppsCustomizeContent != null) {
820 mAppsCustomizeContent.setBulkBind(false);
821 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700822 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200823 if (DEBUG_RESUME_TIME) {
824 Log.d(TAG, "Time spent processing callbacks in onResume: " +
825 (System.currentTimeMillis() - startTimeCallbacks));
826 }
Michael Jurka447bf842013-05-15 14:52:15 +0200827 }
Michael Jurka54554252013-08-01 12:52:23 +0200828 if (mOnResumeCallbacks.size() > 0) {
829 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
830 mOnResumeCallbacks.get(i).run();
831 }
832 mOnResumeCallbacks.clear();
833 }
Winson Chunge4e50662012-01-23 14:45:13 -0800834
835 // Reset the pressed state of icons that were locked in the press state while activities
836 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -0800837 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -0800838 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -0800839 mWaitingForResume.setStayPressed(false);
840 }
Winson Chunge4e50662012-01-23 14:45:13 -0800841 if (mAppsCustomizeContent != null) {
842 // Resets the previous all apps icon press state
843 mAppsCustomizeContent.resetDrawableState();
844 }
Adam Cohen06dff352012-06-01 17:17:08 -0700845 // It is possible that widgets can receive updates while launcher is not in the foreground.
846 // Consequently, the widgets will be inflated in the orientation of the foreground activity
847 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
848 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -0700849 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -0700850
851 // Again, as with the above scenario, it's possible that one or more of the global icons
852 // were updated in the wrong orientation.
853 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +0200854 if (DEBUG_RESUME_TIME) {
855 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
856 }
Adam Cohen06dff352012-06-01 17:17:08 -0700857 }
858
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800859 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700860 protected void onPause() {
Winson Chungca889b32012-05-07 15:34:34 -0700861 // NOTE: We want all transitions from launcher to act as if the wallpaper were enabled
862 // to be consistent. So re-enable the flag here, and we will re-disable it as necessary
863 // when Launcher resumes and we are still in AllApps.
864 updateWallpaperVisibility(true);
865
Winson Chung997a9232013-07-24 15:33:46 -0700866 // Ensure that items added to Launcher are queued until Launcher returns
867 InstallShortcutReceiver.enableInstallQueue();
868
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700869 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700870 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -0800871 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -0700872 mDragController.resetLastGestureUpTime();
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700873 }
Romain Guycbb89e42009-06-08 15:52:54 -0700874
Adam Cohen66a01fd2013-06-11 12:48:00 -0700875 protected void onFinishBindingItems() {
876 }
877
Adam Cohenbffe7452013-07-22 18:21:45 -0700878 QSBScroller mQsbScroller = new QSBScroller() {
879 int scrollY = 0;
880
881 @Override
882 public void setScrollY(int scroll) {
883 scrollY = scroll;
884
885 if (mWorkspace.isOnOrMovingToCustomContent()) {
886 mSearchDropTargetBar.setTranslationY(- scrollY);
887 }
888 }
889 };
890
891 public void resetQSBScroll() {
892 mSearchDropTargetBar.animate().translationY(0).start();
893 }
894
895 public interface CustomContentCallbacks {
896 // Custom content is completely shown
897 public void onShow();
898
899 // Custom content is completely hidden
900 public void onHide();
901 }
902
903 public interface QSBScroller {
904 public void setScrollY(int scrollY);
905 }
906
Adam Cohen66a01fd2013-06-11 12:48:00 -0700907 // Add a fullscreen unpadded view to the workspace to the left all other screens.
Winson Chung98ca0f72013-07-29 12:58:51 -0700908 public QSBScroller addToCustomContentPage(View customContent) {
909 return addToCustomContentPage(customContent, null);
Adam Cohenbffe7452013-07-22 18:21:45 -0700910 }
911
Winson Chung98ca0f72013-07-29 12:58:51 -0700912 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohenbffe7452013-07-22 18:21:45 -0700913 CustomContentCallbacks callbacks) {
Winson Chung98ca0f72013-07-29 12:58:51 -0700914 mWorkspace.addToCustomContentPage(customContent, callbacks);
Adam Cohenbffe7452013-07-22 18:21:45 -0700915 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -0700916 }
917
Adam Cohenedb40762013-07-18 16:45:45 -0700918 // The custom content needs to offset its content to account for the QSB
919 public int getTopOffsetForCustomContent() {
920 return mWorkspace.getPaddingTop();
921 }
922
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700923 @Override
924 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -0400925 // Flag the loader to stop early before switching
926 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -0700927 if (mAppsCustomizeContent != null) {
928 mAppsCustomizeContent.surrender();
929 }
Romain Guy13c2e7b2010-03-10 19:45:00 -0800930 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -0700931 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700932
Joe Onorato2d7e7d02010-01-29 15:57:19 -0800933 // We can't hide the IME if it was forced open. So don't bother
934 /*
935 @Override
936 public void onWindowFocusChanged(boolean hasFocus) {
937 super.onWindowFocusChanged(hasFocus);
938
939 if (hasFocus) {
940 final InputMethodManager inputManager = (InputMethodManager)
941 getSystemService(Context.INPUT_METHOD_SERVICE);
942 WindowManager.LayoutParams lp = getWindow().getAttributes();
943 inputManager.hideSoftInputFromWindow(lp.token, 0, new android.os.ResultReceiver(new
944 android.os.Handler()) {
945 protected void onReceiveResult(int resultCode, Bundle resultData) {
946 Log.d(TAG, "ResultReceiver got resultCode=" + resultCode);
947 }
948 });
949 Log.d(TAG, "called hideSoftInputFromWindow from onWindowFocusChanged");
950 }
951 }
952 */
953
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 private boolean acceptFilter() {
955 final InputMethodManager inputManager = (InputMethodManager)
956 getSystemService(Context.INPUT_METHOD_SERVICE);
957 return !inputManager.isFullscreenMode();
958 }
959
960 @Override
961 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -0700962 final int uniChar = event.getUnicodeChar();
963 final boolean handled = super.onKeyDown(keyCode, event);
964 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
965 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800966 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
967 keyCode, event);
968 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -0700969 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -0700970 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -0700971 // showSearchDialog()
972 // If there are multiple keystrokes before the search dialog takes focus,
973 // onSearchRequested() will be called for every keystroke,
974 // but it is idempotent, so it's fine.
975 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800976 }
977 }
978
Joe Onorato8a9625e2010-01-28 15:55:35 -0800979 // Eat the long press event so the keyboard doesn't come up.
980 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
981 return true;
982 }
983
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 return handled;
985 }
986
Karl Rosaen138a0412009-04-23 19:00:21 -0700987 private String getTypedText() {
988 return mDefaultKeySsb.toString();
989 }
990
991 private void clearTypedText() {
992 mDefaultKeySsb.clear();
993 mDefaultKeySsb.clearSpans();
994 Selection.setSelection(mDefaultKeySsb, 0);
995 }
996
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 /**
Michael Jurka883f55b2010-10-21 15:47:14 -0700998 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
999 * State
1000 */
1001 private static State intToState(int stateOrdinal) {
1002 State state = State.WORKSPACE;
1003 final State[] stateValues = State.values();
1004 for (int i = 0; i < stateValues.length; i++) {
1005 if (stateValues[i].ordinal() == stateOrdinal) {
1006 state = stateValues[i];
1007 break;
1008 }
1009 }
1010 return state;
1011 }
1012
1013 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001014 * Restores the previous state, if it exists.
1015 *
1016 * @param savedState The previous state.
1017 */
1018 private void restoreState(Bundle savedState) {
1019 if (savedState == null) {
1020 return;
1021 }
1022
Michael Jurka883f55b2010-10-21 15:47:14 -07001023 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001024 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001025 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001026 }
1027
Winson Chungf0c6ae02012-03-21 16:10:31 -07001028 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN, -1);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001029 if (currentScreen > -1) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001030 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001031 }
1032
Winson Chung3d503fb2011-07-13 17:25:49 -07001033 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001034 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001035
Winson Chung3d503fb2011-07-13 17:25:49 -07001036 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1037 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001038 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001039 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1040 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001041 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1042 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1043 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen87a9f5b2012-05-29 16:16:51 -07001044 mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001045 mRestoring = true;
1046 }
1047
1048 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1049 if (renameFolder) {
1050 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001051 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001052 mRestoring = true;
1053 }
Winson Chunga12a2502010-12-20 14:41:35 -08001054
Winson Chung785d2eb2011-04-14 16:08:02 -07001055 // Restore the AppsCustomize tab
1056 if (mAppsCustomizeTabHost != null) {
1057 String curTab = savedState.getString("apps_customize_currentTab");
1058 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001059 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001060 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001061 mAppsCustomizeContent.loadAssociatedPages(
1062 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001063 }
1064
Winson Chung5afbf7b2011-07-25 11:53:08 -07001065 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1066 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001067 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001068 }
1069
1070 /**
1071 * Finds all the views we need and configure them properly.
1072 */
1073 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001074 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001075
Craig Mautner360310b2012-10-26 15:13:08 -07001076 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001077 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1078 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Craig Mautner360310b2012-10-26 15:13:08 -07001079
1080 mLauncherView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
1081 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001082
Winson Chung4c98d922011-05-31 16:50:48 -07001083 // Setup the drag layer
1084 mDragLayer.setup(this, dragController);
1085
Winson Chung3d503fb2011-07-13 17:25:49 -07001086 // Setup the hotseat
1087 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1088 if (mHotseat != null) {
1089 mHotseat.setup(this);
1090 }
1091
Adam Cohenf358a4b2013-07-23 16:47:31 -07001092 mOverviewPanel = findViewById(R.id.overview_panel);
1093 findViewById(R.id.widget_button).setOnClickListener(new OnClickListener() {
1094 @Override
1095 public void onClick(View arg0) {
1096 showAllApps(true);
1097 }
1098 });
1099 findViewById(R.id.wallpaper_button).setOnClickListener(new OnClickListener() {
1100 @Override
1101 public void onClick(View arg0) {
1102 startWallpaper();
1103 }
1104 });
1105
Winson Chung4c98d922011-05-31 16:50:48 -07001106 // Setup the workspace
1107 mWorkspace.setHapticFeedbackEnabled(false);
1108 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001109 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001110 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001111
Winson Chungf0ea4d32011-06-06 14:27:16 -07001112 // Get the search/delete bar
Winson Chungc51db6a2011-10-05 11:44:49 -07001113 mSearchDropTargetBar = (SearchDropTargetBar) mDragLayer.findViewById(R.id.qsb_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001114
Winson Chungf0ea4d32011-06-06 14:27:16 -07001115 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001116 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001117 mAppsCustomizeContent = (AppsCustomizePagedView)
1118 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1119 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001120
Winson Chung3d503fb2011-07-13 17:25:49 -07001121 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001122 dragController.setDragScoller(mWorkspace);
1123 dragController.setScrollView(mDragLayer);
1124 dragController.setMoveTarget(mWorkspace);
1125 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001126 if (mSearchDropTargetBar != null) {
1127 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001128 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001129
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001130 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001131 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001132 mWeightWatcher = new WeightWatcher(this);
1133 mWeightWatcher.setAlpha(0.5f);
1134 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001135 new FrameLayout.LayoutParams(
1136 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001137 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001138 Gravity.BOTTOM)
1139 );
Adam Cohen39a06042013-07-19 14:30:12 -07001140
1141 boolean show = shouldShowWeightWatcher();
1142 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001143 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001144 }
1145
1146 /**
1147 * Creates a view representing a shortcut.
1148 *
1149 * @param info The data structure describing the shortcut.
1150 *
1151 * @return A View inflated from R.layout.application.
1152 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001153 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001154 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001155 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001156 }
1157
1158 /**
1159 * Creates a view representing a shortcut inflated from the specified resource.
1160 *
1161 * @param layoutResId The id of the XML layout used to create the shortcut.
1162 * @param parent The group the shortcut belongs to.
1163 * @param info The data structure describing the shortcut.
1164 *
1165 * @return A View inflated from layoutResId.
1166 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001167 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001168 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1169 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001171 return favorite;
1172 }
1173
1174 /**
1175 * Add an application shortcut to the workspace.
1176 *
1177 * @param data The intent describing the application.
1178 * @param cellInfo The position on screen where to create the shortcut.
1179 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001180 void completeAddApplication(Intent data, long container, long screenId, int cellX, int cellY) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001181 final int[] cellXY = mTmpAddItemCellCoordinates;
Adam Cohendcd297f2013-06-18 13:13:40 -07001182 final CellLayout layout = getCellLayout(container, screenId);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001183
Adam Cohenfbba09b2011-07-18 21:43:05 -07001184 // First we check if we already know the exact location where we want to add this item.
1185 if (cellX >= 0 && cellY >= 0) {
1186 cellXY[0] = cellX;
1187 cellXY[1] = cellY;
1188 } else if (!layout.findCellForSpan(cellXY, 1, 1)) {
Winson Chung93eef082012-03-23 15:59:27 -07001189 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001190 return;
1191 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192
Patrick Dubroy002cbf42011-03-03 16:36:21 -08001193 final ShortcutInfo info = mModel.getShortcutInfo(getPackageManager(), data, this);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001194
Romain Guy73b979d2009-06-09 12:57:21 -07001195 if (info != null) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07001196 info.setActivity(this, data.getComponent(), Intent.FLAG_ACTIVITY_NEW_TASK |
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001198 info.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001199 mWorkspace.addApplicationShortcut(info, layout, container, screenId, cellXY[0], cellXY[1],
Adam Cohenfbba09b2011-07-18 21:43:05 -07001200 isWorkspaceLocked(), cellX, cellY);
Joe Onorato0589f0f2010-02-08 13:44:00 -08001201 } else {
1202 Log.e(TAG, "Couldn't find ActivityInfo for selected application: " + data);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001203 }
1204 }
Romain Guycbb89e42009-06-08 15:52:54 -07001205
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001206 /**
1207 * Add a shortcut to the workspace.
1208 *
1209 * @param data The intent describing the shortcut.
1210 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001211 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001212 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001213 int cellY) {
1214 int[] cellXY = mTmpAddItemCellCoordinates;
1215 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001216 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001217
Michael Jurkad3ef3062010-11-23 16:23:58 -08001218 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001219
Adam Cohen558baaf2011-08-15 15:22:57 -07001220 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001221 if (info == null) {
1222 return;
1223 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001224 final View view = createShortcut(info);
1225
Adam Cohenfbba09b2011-07-18 21:43:05 -07001226 // First we check if we already know the exact location where we want to add this item.
1227 if (cellX >= 0 && cellY >= 0) {
1228 cellXY[0] = cellX;
1229 cellXY[1] = cellY;
1230 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001231
1232 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001233 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001234 true, null,null)) {
1235 return;
1236 }
1237 DragObject dragObject = new DragObject();
1238 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001239 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1240 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001241 return;
1242 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001243 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001244 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001245 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001246 foundCellSpan = (result != null);
1247 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001248 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001249 }
1250
1251 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001252 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001253 return;
1254 }
1255
Adam Cohendcd297f2013-06-18 13:13:40 -07001256 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001257
1258 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001259 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001260 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001261 }
1262 }
1263
Adam Cohen2f093b62012-04-30 18:59:53 -07001264 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1265 int minHeight) {
1266 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001267 // We want to account for the extra amount of padding that we are adding to the widget
1268 // to ensure that it gets the full amount of space that it has requested
1269 int requiredWidth = minWidth + padding.left + padding.right;
1270 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001271 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001272 }
1273
Adam Cohen2f093b62012-04-30 18:59:53 -07001274 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1275 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001276 }
1277
Adam Cohen2f093b62012-04-30 18:59:53 -07001278 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1279 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001280 }
1281
Adam Cohen2f093b62012-04-30 18:59:53 -07001282 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1283 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001284 }
1285
Adam Cohen2f093b62012-04-30 18:59:53 -07001286 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1287 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001288 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001289 }
1290
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001291 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001292 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001294 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001295 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001296 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001297 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001298 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1299 if (appWidgetInfo == null) {
1300 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1301 }
Romain Guycbb89e42009-06-08 15:52:54 -07001302
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001303 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001304 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001305
Adam Cohen2f093b62012-04-30 18:59:53 -07001306 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1307 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001308
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001309 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001310 // We have saved the position to which the widget was dragged-- this really only matters
1311 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001312 int[] cellXY = mTmpAddItemCellCoordinates;
1313 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001314 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001315 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001316 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1317 cellXY[0] = mPendingAddInfo.cellX;
1318 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001319 spanXY[0] = mPendingAddInfo.spanX;
1320 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001321 foundCellSpan = true;
1322 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001323 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001324 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001325 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1326 spanXY[1], cellXY, finalSpan);
1327 spanXY[0] = finalSpan[0];
1328 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001329 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001330 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001331 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001332 }
1333
Michael Jurka0280c3b2010-09-17 15:00:07 -07001334 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001335 if (appWidgetId != -1) {
1336 // Deleting an app widget ID is a void call but writes to disk before returning
1337 // to the caller...
Winson Chungf7640c82011-02-28 13:47:29 -08001338 new Thread("deleteAppWidgetId") {
1339 public void run() {
1340 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
1341 }
1342 }.start();
1343 }
Winson Chung93eef082012-03-23 15:59:27 -07001344 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001345 return;
1346 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001348 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001349 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1350 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001351 launcherInfo.spanX = spanXY[0];
1352 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001353 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1354 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001355
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001356 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001357 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001358
1359 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001360 if (hostView == null) {
1361 // Perform actual inflation because we're live
1362 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1363 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1364 } else {
1365 // The AppWidgetHostView has already been inflated and instantiated
1366 launcherInfo.hostView = hostView;
1367 }
Romain Guycbb89e42009-06-08 15:52:54 -07001368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001370 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001371 launcherInfo.notifyWidgetSizeChanged(this);
1372
Adam Cohendcd297f2013-06-18 13:13:40 -07001373 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001374 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001375
1376 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001378 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001379 }
Romain Guycbb89e42009-06-08 15:52:54 -07001380
Adam Cohended9f8d2010-11-03 13:25:16 -07001381 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1382 @Override
1383 public void onReceive(Context context, Intent intent) {
1384 final String action = intent.getAction();
1385 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1386 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001387 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001388 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001389
Winson Chungc100e8e2011-08-09 16:02:43 -07001390 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001391 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001392 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
1393 mAppsCustomizeTabHost.reset();
1394 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001395 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001396 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1397 mUserPresent = true;
1398 updateRunning();
1399 }
1400 }
1401 };
1402
1403 @Override
1404 public void onAttachedToWindow() {
1405 super.onAttachedToWindow();
1406
1407 // Listen for broadcasts related to user-presence
1408 final IntentFilter filter = new IntentFilter();
1409 filter.addAction(Intent.ACTION_SCREEN_OFF);
1410 filter.addAction(Intent.ACTION_USER_PRESENT);
1411 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001412 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohend113e0c2010-11-11 10:48:05 -08001413 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001414 mVisible = true;
1415 }
1416
1417 @Override
1418 public void onDetachedFromWindow() {
1419 super.onDetachedFromWindow();
1420 mVisible = false;
1421
Adam Cohend113e0c2010-11-11 10:48:05 -08001422 if (mAttached) {
1423 unregisterReceiver(mReceiver);
1424 mAttached = false;
1425 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001426 updateRunning();
1427 }
1428
1429 public void onWindowVisibilityChanged(int visibility) {
1430 mVisible = visibility == View.VISIBLE;
1431 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001432 // The following code used to be in onResume, but it turns out onResume is called when
1433 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1434 // is a more appropriate event to handle
1435 if (mVisible) {
1436 mAppsCustomizeTabHost.onWindowVisible();
1437 if (!mWorkspaceLoading) {
1438 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001439 // We want to let Launcher draw itself at least once before we force it to build
1440 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001441 // apps is nice and speedy.
1442 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001443 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001444 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001445 if (mStarted) return;
1446 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001447 // We delay the layer building a bit in order to give
1448 // other message processing a time to run. In particular
1449 // this avoids a delay in hiding the IME if it was
1450 // currently shown, because doing that may involve
1451 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001452 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001453 final ViewTreeObserver.OnDrawListener listener = this;
1454 mWorkspace.post(new Runnable() {
1455 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001456 if (mWorkspace != null &&
1457 mWorkspace.getViewTreeObserver() != null) {
1458 mWorkspace.getViewTreeObserver().
1459 removeOnDrawListener(listener);
1460 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001461 }
1462 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001463 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001464 }
1465 });
1466 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001467 // When Launcher comes back to foreground, a different Activity might be responsible for
1468 // the app market intent, so refresh the icon
1469 updateAppMarketIcon();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001470 clearTypedText();
1471 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001472 }
1473
1474 private void sendAdvanceMessage(long delay) {
1475 mHandler.removeMessages(ADVANCE_MSG);
1476 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1477 mHandler.sendMessageDelayed(msg, delay);
1478 mAutoAdvanceSentTime = System.currentTimeMillis();
1479 }
1480
1481 private void updateRunning() {
1482 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1483 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1484 mAutoAdvanceRunning = autoAdvanceRunning;
1485 if (autoAdvanceRunning) {
1486 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1487 sendAdvanceMessage(delay);
1488 } else {
1489 if (!mWidgetsToAdvance.isEmpty()) {
1490 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1491 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1492 }
1493 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001494 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001495 }
1496 }
1497 }
1498
1499 private final Handler mHandler = new Handler() {
1500 @Override
1501 public void handleMessage(Message msg) {
1502 if (msg.what == ADVANCE_MSG) {
1503 int i = 0;
1504 for (View key: mWidgetsToAdvance.keySet()) {
1505 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1506 final int delay = mAdvanceStagger * i;
1507 if (v instanceof Advanceable) {
1508 postDelayed(new Runnable() {
1509 public void run() {
1510 ((Advanceable) v).advance();
1511 }
1512 }, delay);
1513 }
1514 i++;
1515 }
1516 sendAdvanceMessage(mAdvanceInterval);
1517 }
1518 }
1519 };
1520
1521 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001522 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001523 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1524 if (v instanceof Advanceable) {
1525 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001526 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001527 updateRunning();
1528 }
1529 }
1530
1531 void removeWidgetToAutoAdvance(View hostView) {
1532 if (mWidgetsToAdvance.containsKey(hostView)) {
1533 mWidgetsToAdvance.remove(hostView);
1534 updateRunning();
1535 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001536 }
1537
Joe Onorato9c1289c2009-08-17 11:03:03 -04001538 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001539 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001540 launcherInfo.hostView = null;
1541 }
1542
Winson Chung93eef082012-03-23 15:59:27 -07001543 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1544 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1545 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001546 }
1547
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001548 public LauncherAppWidgetHost getAppWidgetHost() {
1549 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 }
Romain Guycbb89e42009-06-08 15:52:54 -07001551
Winson Chunga9abd0e2010-10-27 17:18:37 -07001552 public LauncherModel getModel() {
1553 return mModel;
1554 }
1555
Bjorn Bringertc459e522013-06-07 19:36:01 +01001556 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001557 getWindow().closeAllPanels();
1558
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001559 // Whatever we were doing is hereby canceled.
1560 mWaitingForResult = false;
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001561 }
1562
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 @Override
1564 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001565 long startTime = 0;
1566 if (DEBUG_RESUME_TIME) {
1567 startTime = System.currentTimeMillis();
1568 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 super.onNewIntent(intent);
1570
1571 // Close the menu
1572 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001573 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001574 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001575
Jamie Genniscb222e82012-10-23 15:44:26 -07001576 final boolean alreadyOnHome =
1577 ((intent.getFlags() & Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
Joe Onorato14f122b2009-11-19 14:06:36 -08001578 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001579
Jamie Genniscb222e82012-10-23 15:44:26 -07001580 Runnable processIntent = new Runnable() {
1581 public void run() {
Michael Jurkaffc26822012-11-16 18:21:22 -08001582 if (mWorkspace == null) {
1583 // Can be cases where mWorkspace is null, this prevents a NPE
1584 return;
1585 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001586 Folder openFolder = mWorkspace.getOpenFolder();
1587 // In all these cases, only animate if we're already on home
1588 mWorkspace.exitWidgetResizeMode();
1589 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1590 openFolder == null) {
1591 mWorkspace.moveToDefaultScreen(true);
1592 }
Adam Cohenac56cff2011-09-28 20:45:37 -07001593
Jamie Genniscb222e82012-10-23 15:44:26 -07001594 closeFolder();
1595 exitSpringLoadedDragMode();
Winson Chung4a2afa32012-07-19 14:53:05 -07001596
Jamie Genniscb222e82012-10-23 15:44:26 -07001597 // If we are already on home, then just animate back to the workspace,
1598 // otherwise, just wait until onResume to set the state back to Workspace
1599 if (alreadyOnHome) {
1600 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001601 if (mWorkspace.isInOverviewMode()) {
1602 mWorkspace.exitOverviewMode();
1603 }
Jamie Genniscb222e82012-10-23 15:44:26 -07001604 } else {
1605 mOnResumeState = State.WORKSPACE;
1606 }
1607
1608 final View v = getWindow().peekDecorView();
1609 if (v != null && v.getWindowToken() != null) {
1610 InputMethodManager imm = (InputMethodManager)getSystemService(
1611 INPUT_METHOD_SERVICE);
1612 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1613 }
1614
1615 // Reset AllApps to its initial state
1616 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
1617 mAppsCustomizeTabHost.reset();
1618 }
1619 }
1620 };
1621
1622 if (alreadyOnHome && !mWorkspace.hasWindowFocus()) {
1623 // Delay processing of the intent to allow the status bar animation to finish
1624 // first in order to avoid janky animations.
1625 mWorkspace.postDelayed(processIntent, 350);
Winson Chung4a2afa32012-07-19 14:53:05 -07001626 } else {
Jamie Genniscb222e82012-10-23 15:44:26 -07001627 // Process the intent immediately.
1628 processIntent.run();
Winson Chung4a2afa32012-07-19 14:53:05 -07001629 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001630
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001631 }
Michael Jurka447bf842013-05-15 14:52:15 +02001632 if (DEBUG_RESUME_TIME) {
1633 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1634 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 }
1636
1637 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001638 public void onRestoreInstanceState(Bundle state) {
1639 super.onRestoreInstanceState(state);
1640 for (int page: mSynchronouslyBoundPages) {
1641 mWorkspace.restoreInstanceStateForChild(page);
1642 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643 }
1644
1645 @Override
1646 protected void onSaveInstanceState(Bundle outState) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001647 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN, mWorkspace.getNextPage());
Adam Cohen95bb8002011-07-03 23:40:28 -07001648 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001649
Michael Jurka883f55b2010-10-21 15:47:14 -07001650 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001651 // We close any open folder since it will not be re-opened, and we need to make sure
1652 // this state is reflected.
1653 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001654
Adam Cohendcd297f2013-06-18 13:13:40 -07001655 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001656 mWaitingForResult) {
1657 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001658 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001659 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1660 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001661 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1662 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1663 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001664 }
1665
1666 if (mFolderInfo != null && mWaitingForResult) {
1667 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1668 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1669 }
Michael Jurka946ad472010-07-09 18:05:18 -07001670
Winson Chung785d2eb2011-04-14 16:08:02 -07001671 // Save the current AppsCustomize tab
1672 if (mAppsCustomizeTabHost != null) {
1673 String currentTabTag = mAppsCustomizeTabHost.getCurrentTabTag();
1674 if (currentTabTag != null) {
1675 outState.putString("apps_customize_currentTab", currentTabTag);
1676 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001677 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1678 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001679 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001680 }
1681
1682 @Override
1683 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001684 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001685
Winson Chunge7a03942011-08-05 15:05:12 -07001686 // Remove all pending runnables
1687 mHandler.removeMessages(ADVANCE_MSG);
1688 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001689 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001690
Winson Chungcd2b0142011-06-08 16:02:26 -07001691 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001692 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001693 mModel.stopLoader();
1694 app.setLauncher(null);
1695
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001696 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001697 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001698 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001699 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001701 mAppWidgetHost = null;
1702
1703 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704
1705 TextKeyListener.getInstance().release();
1706
Winson Chung81b52252012-08-27 15:34:29 -07001707 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
1708 // to prevent leaking Launcher activities on orientation change.
1709 if (mModel != null) {
1710 mModel.unbindItemInfosAndClearQueuedBindRunnables();
1711 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08001712
1713 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001714 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001715
Adam Cohenaccf3bf2012-04-30 16:07:43 -07001716 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001717 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
1718 mWorkspace.removeAllViews();
1719 mWorkspace = null;
1720 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08001721
Michael Jurka2ecf9952012-06-18 12:52:28 -07001722 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001723 }
1724
Adam Cohena9cf38f2011-05-02 15:36:58 -07001725 public DragController getDragController() {
1726 return mDragController;
1727 }
1728
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729 @Override
1730 public void startActivityForResult(Intent intent, int requestCode) {
Romain Guy08f97492009-06-29 14:41:20 -07001731 if (requestCode >= 0) mWaitingForResult = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001732 super.startActivityForResult(intent, requestCode);
1733 }
1734
Winson Chung70d72102011-08-12 11:24:35 -07001735 /**
1736 * Indicates that we want global search for this activity by setting the globalSearch
1737 * argument for {@link #startSearch} to true.
1738 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07001740 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001741 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001742
Michael Jurkac0e8fca2010-10-06 16:41:29 -07001743 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07001744
Karl Rosaen138a0412009-04-23 19:00:21 -07001745 if (initialQuery == null) {
1746 // Use any text typed in the launcher as the initial query
1747 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07001748 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 if (appSearchData == null) {
1750 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01001751 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001752 }
Winson Chungadf0c182012-08-23 14:59:07 -07001753 Rect sourceBounds = new Rect();
1754 if (mSearchDropTargetBar != null) {
1755 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
1756 }
Romain Guycbb89e42009-06-08 15:52:54 -07001757
Bjorn Bringertc459e522013-06-07 19:36:01 +01001758 startSearch(initialQuery, selectInitialQuery,
1759 appSearchData, sourceBounds);
1760 }
1761
1762 public void startSearch(String initialQuery,
1763 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07001764 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01001765 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07001766 }
1767
1768 /**
1769 * Starts the global search activity. This code is a copied from SearchManager
1770 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01001771 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07001772 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001773 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07001774 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
1775 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
1776 if (globalSearchActivity == null) {
1777 Log.w(TAG, "No global search activity found.");
1778 return;
1779 }
1780 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
1781 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
1782 intent.setComponent(globalSearchActivity);
1783 // Make sure that we have a Bundle to put source in
1784 if (appSearchData == null) {
1785 appSearchData = new Bundle();
1786 } else {
1787 appSearchData = new Bundle(appSearchData);
1788 }
1789 // Set source to package name of app that starts global search, if not set already.
1790 if (!appSearchData.containsKey("source")) {
1791 appSearchData.putString("source", getPackageName());
1792 }
1793 intent.putExtra(SearchManager.APP_DATA, appSearchData);
1794 if (!TextUtils.isEmpty(initialQuery)) {
1795 intent.putExtra(SearchManager.QUERY, initialQuery);
1796 }
1797 if (selectInitialQuery) {
1798 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
1799 }
1800 intent.setSourceBounds(sourceBounds);
1801 try {
1802 startActivity(intent);
1803 } catch (ActivityNotFoundException ex) {
1804 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
1805 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001806 }
1807
Winson Chung70d72102011-08-12 11:24:35 -07001808 @Override
1809 public boolean onCreateOptionsMenu(Menu menu) {
1810 if (isWorkspaceLocked()) {
1811 return false;
1812 }
1813
1814 super.onCreateOptionsMenu(menu);
Winson Chungdff8ebb2011-09-08 17:25:31 -07001815
1816 Intent manageApps = new Intent(Settings.ACTION_MANAGE_ALL_APPLICATIONS_SETTINGS);
1817 manageApps.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1818 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung236d4312011-08-22 15:12:27 -07001819 Intent settings = new Intent(android.provider.Settings.ACTION_SETTINGS);
1820 settings.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1821 | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
Michael Jurkab964f9c2011-09-27 22:10:05 -07001822 String helpUrl = getString(R.string.help_url);
1823 Intent help = new Intent(Intent.ACTION_VIEW, Uri.parse(helpUrl));
Winson Chungdff8ebb2011-09-08 17:25:31 -07001824 help.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
1825 | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
1826
Winson Chung70d72102011-08-12 11:24:35 -07001827 menu.add(MENU_GROUP_WALLPAPER, MENU_WALLPAPER_SETTINGS, 0, R.string.menu_wallpaper)
1828 .setIcon(android.R.drawable.ic_menu_gallery)
1829 .setAlphabeticShortcut('W');
1830 menu.add(0, MENU_MANAGE_APPS, 0, R.string.menu_manage_apps)
1831 .setIcon(android.R.drawable.ic_menu_manage)
Winson Chungdff8ebb2011-09-08 17:25:31 -07001832 .setIntent(manageApps)
Winson Chung70d72102011-08-12 11:24:35 -07001833 .setAlphabeticShortcut('M');
Winson Chung236d4312011-08-22 15:12:27 -07001834 menu.add(0, MENU_SYSTEM_SETTINGS, 0, R.string.menu_settings)
1835 .setIcon(android.R.drawable.ic_menu_preferences)
1836 .setIntent(settings)
1837 .setAlphabeticShortcut('P');
Michael Jurkab964f9c2011-09-27 22:10:05 -07001838 if (!helpUrl.isEmpty()) {
1839 menu.add(0, MENU_HELP, 0, R.string.menu_help)
1840 .setIcon(android.R.drawable.ic_menu_help)
1841 .setIntent(help)
1842 .setAlphabeticShortcut('H');
1843 }
Winson Chung70d72102011-08-12 11:24:35 -07001844 return true;
1845 }
1846
1847 @Override
1848 public boolean onPrepareOptionsMenu(Menu menu) {
1849 super.onPrepareOptionsMenu(menu);
1850
1851 if (mAppsCustomizeTabHost.isTransitioning()) {
1852 return false;
1853 }
1854 boolean allAppsVisible = (mAppsCustomizeTabHost.getVisibility() == View.VISIBLE);
1855 menu.setGroupVisible(MENU_GROUP_WALLPAPER, !allAppsVisible);
1856
1857 return true;
1858 }
1859
1860 @Override
1861 public boolean onOptionsItemSelected(MenuItem item) {
1862 switch (item.getItemId()) {
1863 case MENU_WALLPAPER_SETTINGS:
1864 startWallpaper();
1865 return true;
Winson Chung70d72102011-08-12 11:24:35 -07001866 }
1867
1868 return super.onOptionsItemSelected(item);
1869 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001870
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001871 @Override
1872 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07001873 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07001874 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07001875 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07001876 }
1877
Joe Onorato9c1289c2009-08-17 11:03:03 -04001878 public boolean isWorkspaceLocked() {
1879 return mWorkspaceLoading || mWaitingForResult;
1880 }
1881
Michael Jurka0280c3b2010-09-17 15:00:07 -07001882 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07001883 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07001884 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001885 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
1886 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08001887 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07001888 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07001889 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07001890
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001891 void addAppWidgetImpl(final int appWidgetId, ItemInfo info, AppWidgetHostView boundWidget,
1892 AppWidgetProviderInfo appWidgetInfo) {
1893 if (appWidgetInfo.configure != null) {
1894 mPendingAddWidgetInfo = appWidgetInfo;
Michael Jurkaaf442092010-06-10 17:01:57 -07001895
Bjorn Bringert7984c942009-12-09 15:38:25 +00001896 // Launch over to configure widget, if needed
1897 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001898 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00001899 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Romain Guy8e633c52010-03-04 12:51:36 -08001900 startActivityForResultSafely(intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001901 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00001902 // Otherwise just add it
Adam Cohendcd297f2013-06-18 13:13:40 -07001903 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001904 appWidgetInfo);
Winson Chung557d6ed2011-07-08 15:34:52 -07001905 // Exit spring loaded mode if necessary after adding the widget
Adam Cohened66b2b2012-01-23 17:28:51 -08001906 exitSpringLoadedDragModeDelayed(true, false, null);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001907 }
1908 }
Romain Guycbb89e42009-06-08 15:52:54 -07001909
Adam Cohenfbba09b2011-07-18 21:43:05 -07001910 /**
1911 * Process a shortcut drop.
1912 *
1913 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07001914 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001915 * @param cell The cell it should be added to, optional
1916 * @param position The location on the screen where it was dropped, optional
1917 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001918 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07001919 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001920 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001921 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001922 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001923 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001924
1925 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001926 mPendingAddInfo.cellX = cell[0];
1927 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001928 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001929
1930 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
1931 createShortcutIntent.setComponent(componentName);
1932 processShortcut(createShortcutIntent);
1933 }
1934
Adam Cohenfbba09b2011-07-18 21:43:05 -07001935 /**
1936 * Process a widget drop.
1937 *
1938 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07001939 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07001940 * @param cell The cell it should be added to, optional
1941 * @param position The location on the screen where it was dropped, optional
1942 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001943 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08001944 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001945 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07001946 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07001947 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07001948 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08001949 mPendingAddInfo.minSpanX = info.minSpanX;
1950 mPendingAddInfo.minSpanY = info.minSpanY;
1951
Adam Cohenfbba09b2011-07-18 21:43:05 -07001952 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07001953 mPendingAddInfo.cellX = cell[0];
1954 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07001955 }
Adam Cohend41fbf52012-02-16 23:53:59 -08001956 if (span != null) {
1957 mPendingAddInfo.spanX = span[0];
1958 mPendingAddInfo.spanY = span[1];
1959 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001960
Adam Cohened66b2b2012-01-23 17:28:51 -08001961 AppWidgetHostView hostView = info.boundWidget;
1962 int appWidgetId;
1963 if (hostView != null) {
1964 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001965 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08001966 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001967 // In this case, we either need to start an activity to get permission to bind
1968 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08001969 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07001970 Bundle options = info.bindOptions;
1971
1972 boolean success = false;
1973 if (options != null) {
1974 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1975 info.componentName, options);
1976 } else {
1977 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
1978 info.componentName);
1979 }
1980 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001981 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07001982 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001983 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07001984 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
1985 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
1986 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07001987 // TODO: we need to make sure that this accounts for the options bundle.
1988 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07001989 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
1990 }
Adam Cohened66b2b2012-01-23 17:28:51 -08001991 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001992 }
1993
Joe Onoratodeb98af2010-02-19 14:59:39 -08001994 void processShortcut(Intent intent) {
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001995 // Handle case where user selected "Applications"
1996 String applicationName = getResources().getString(R.string.group_applications);
1997 String shortcutName = intent.getStringExtra(Intent.EXTRA_SHORTCUT_NAME);
Romain Guycbb89e42009-06-08 15:52:54 -07001998
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07001999 if (applicationName != null && applicationName.equals(shortcutName)) {
2000 Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
2001 mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
Romain Guycbb89e42009-06-08 15:52:54 -07002002
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002003 Intent pickIntent = new Intent(Intent.ACTION_PICK_ACTIVITY);
2004 pickIntent.putExtra(Intent.EXTRA_INTENT, mainIntent);
Winson Chung58f20882010-10-01 13:44:56 -07002005 pickIntent.putExtra(Intent.EXTRA_TITLE, getText(R.string.title_select_application));
Joe Onorato4a79a042010-09-24 16:17:21 -07002006 startActivityForResultSafely(pickIntent, REQUEST_PICK_APPLICATION);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002007 } else {
Joe Onorato4a79a042010-09-24 16:17:21 -07002008 startActivityForResultSafely(intent, REQUEST_CREATE_SHORTCUT);
Jeffrey Sharkeyc7fdae12009-03-24 20:41:22 -07002009 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002010 }
2011
Winson Chung24ab2f12010-09-16 14:10:47 -07002012 void processWallpaper(Intent intent) {
2013 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2014 }
2015
Adam Cohendcd297f2013-06-18 13:13:40 -07002016 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002017 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002018 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 folderInfo.title = getText(R.string.folder_name);
2020
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002022 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002023 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002024 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025
2026 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002027 FolderIcon newFolder =
2028 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002029 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002030 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002031 // Force measure the new folder icon
2032 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2033 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002034 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002035 }
Romain Guycbb89e42009-06-08 15:52:54 -07002036
Joe Onorato9c1289c2009-08-17 11:03:03 -04002037 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002038 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002039 }
2040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 private void startWallpaper() {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002042 showWorkspace(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
Michael Jurka104c4562013-07-08 18:03:46 -07002044 pickWallpaper.setComponent(
2045 new ComponentName(getPackageName(), WallpaperPickerActivity.class.getName()));
2046 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 }
2048
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002049 /**
2050 * Registers various content observers. The current implementation registers
2051 * only a favorites observer to keep track of the favorites applications.
2052 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002053 private void registerContentObservers() {
2054 ContentResolver resolver = getContentResolver();
2055 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2056 true, mWidgetObserver);
2057 }
2058
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059 @Override
2060 public boolean dispatchKeyEvent(KeyEvent event) {
2061 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2062 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002064 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002065 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002066 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002067 dumpState();
2068 return true;
2069 }
2070 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002071 }
2072 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2073 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002074 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 return true;
2076 }
2077 }
2078
2079 return super.dispatchKeyEvent(event);
2080 }
2081
Joe Onorato88ec0992009-11-19 13:16:06 -08002082 @Override
2083 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002084 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002085 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002086 } else if (mWorkspace.isInOverviewMode()) {
2087 mWorkspace.exitOverviewMode();
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002088 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002089 Folder openFolder = mWorkspace.getOpenFolder();
2090 if (openFolder.isEditingName()) {
2091 openFolder.dismissEditingName();
2092 } else {
2093 closeFolder();
2094 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002095 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002096 mWorkspace.exitWidgetResizeMode();
2097
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002098 // Back button is a no-op here, but give at least some feedback for the button press
2099 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002100 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002101 }
2102
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002104 * Re-listen when widgets are reset.
2105 */
2106 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002107 if (mAppWidgetHost != null) {
2108 mAppWidgetHost.startListening();
2109 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002110 }
2111
2112 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002113 * Launches the intent referred by the clicked shortcut.
2114 *
2115 * @param v The view representing the clicked shortcut.
2116 */
2117 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002118 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2119 // view has detached (it's possible for this to happen if the view is removed mid touch).
2120 if (v.getWindowToken() == null) {
2121 return;
2122 }
2123
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002124 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002125 return;
2126 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002127
Adam Cohenf358a4b2013-07-23 16:47:31 -07002128 if (v instanceof PageIndicator) {
2129 if (!mWorkspace.isInOverviewMode()) {
2130 mWorkspace.enterOverviewMode();
2131 }
2132 return;
2133 }
2134
2135 if (v instanceof CellLayout) {
2136 if (mWorkspace.isInOverviewMode()) {
2137 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v));
2138 }
2139 }
2140
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002142 if (tag instanceof ShortcutInfo) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143 // Open shortcut
Daniel Sandlerff02d492013-08-05 02:12:05 -04002144 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2145 final Intent intent = shortcut.intent;
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002146
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002147 // Check for special shortcuts
2148 if (intent.getComponent() != null) {
2149 final String shortcutClass = intent.getComponent().getClassName();
2150
2151 if (shortcutClass.equals(WidgetAdder.class.getName())) {
2152 showAllApps(true);
2153 return;
2154 } else if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2155 MemoryDumpActivity.startDump(this);
2156 return;
Adam Cohen39a06042013-07-19 14:30:12 -07002157 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2158 toggleShowWeightWatcher();
2159 return;
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002160 }
Adam Cohenb5fe60c2013-06-06 22:03:51 -07002161 }
Daniel Sandlerf8577a32013-06-26 14:04:59 -04002162
2163 // Start activities
Joe Onorato13724ea2009-12-02 21:16:35 -08002164 int[] pos = new int[2];
2165 v.getLocationOnScreen(pos);
Romain Guyfb5411e2010-02-24 10:04:17 -08002166 intent.setSourceBounds(new Rect(pos[0], pos[1],
2167 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Winson Chungc7450e32012-04-17 17:34:08 -07002168
2169 boolean success = startActivitySafely(v, intent, tag);
Michael Jurkaddd62e92011-02-16 17:49:14 -08002170
Daniel Sandlerff02d492013-08-05 02:12:05 -04002171 mStats.recordLaunch(intent, shortcut);
2172
Michael Jurkaddd62e92011-02-16 17:49:14 -08002173 if (success && v instanceof BubbleTextView) {
2174 mWaitingForResume = (BubbleTextView) v;
2175 mWaitingForResume.setStayPressed(true);
2176 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002177 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002178 if (v instanceof FolderIcon) {
2179 FolderIcon fi = (FolderIcon) v;
2180 handleFolderClick(fi);
2181 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002182 } else if (v == mAllAppsButton) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002183 if (isAllAppsVisible()) {
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002184 showWorkspace(true);
Joe Onorato7404ee42009-07-31 11:54:44 -07002185 } else {
Michael Jurka2a552322011-10-11 15:22:05 -07002186 onClickAllAppsButton(v);
Joe Onorato7404ee42009-07-31 11:54:44 -07002187 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002188 }
2189 }
2190
Michael Jurka0e260592010-06-30 17:07:39 -07002191 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002192 return false;
2193 }
2194
Michael Jurkaaf442092010-06-10 17:01:57 -07002195 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002196 * Event handler for the search button
2197 *
2198 * @param v The view that was clicked.
2199 */
2200 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002201 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2202
Amith Yamasania135ba82011-08-09 17:42:01 -07002203 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002204 }
2205
2206 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002207 * Event handler for the voice button
2208 *
2209 * @param v The view that was clicked.
2210 */
2211 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002212 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002213
Bjorn Bringertc459e522013-06-07 19:36:01 +01002214 startVoice();
2215 }
2216
2217 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002218 try {
2219 final SearchManager searchManager =
2220 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2221 ComponentName activityName = searchManager.getGlobalSearchActivity();
2222 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002223 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002224 if (activityName != null) {
2225 intent.setPackage(activityName.getPackageName());
2226 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002227 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002228 } catch (ActivityNotFoundException e) {
2229 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002230 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002231 startActivitySafely(null, intent, "onClickVoiceButton");
2232 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002233 }
2234
2235 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002236 * Event handler for the "grid" button that appears on the home screen, which
2237 * enters all apps mode.
2238 *
2239 * @param v The view that was clicked.
2240 */
2241 public void onClickAllAppsButton(View v) {
Michael Jurka5130e402011-10-13 04:55:35 -07002242 showAllApps(true);
2243 }
2244
2245 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002246 // Provide the same haptic feedback that the system offers for virtual keys.
2247 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002248 }
2249
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002250 public void onClickAppMarketButton(View v) {
2251 if (mAppMarketIntent != null) {
Winson Chungc7450e32012-04-17 17:34:08 -07002252 startActivitySafely(v, mAppMarketIntent, "app market");
Winson Chung4f916f42012-03-26 15:30:59 -07002253 } else {
2254 Log.e(TAG, "Invalid app market intent.");
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002255 }
2256 }
2257
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002258 void startApplicationDetailsActivity(ComponentName componentName) {
2259 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002260 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2261 Uri.fromParts("package", packageName, null));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002262 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002263 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002264 }
2265
Michael Jurka1e2f4652013-07-08 18:03:46 -07002266 // returns true if the activity was started
2267 boolean startApplicationUninstallActivity(ComponentName componentName, int flags) {
2268 if ((flags & ApplicationInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002269 // System applications cannot be installed. For now, show a toast explaining that.
2270 // We may give them the option of disabling apps this way.
2271 int messageId = R.string.uninstall_system_app_text;
2272 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002273 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002274 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002275 String packageName = componentName.getPackageName();
2276 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002277 Intent intent = new Intent(
2278 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002279 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2280 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Patrick Dubroy5539af72010-09-07 15:22:01 -07002281 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002282 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002283 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002284 }
2285
Michael Jurka86a720e2012-05-09 11:23:23 -07002286 boolean startActivity(View v, Intent intent, Object tag) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Winson Chungc7450e32012-04-17 17:34:08 -07002288
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002289 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002290 // Only launch using the new animation if the shortcut has not opted out (this is a
2291 // private contract between launcher and may be ignored in the future).
2292 boolean useLaunchAnimation = (v != null) &&
2293 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
2294 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002295 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2296 v.getMeasuredWidth(), v.getMeasuredHeight());
2297
2298 startActivity(intent, opts.toBundle());
2299 } else {
2300 startActivity(intent);
2301 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002302 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 } catch (SecurityException e) {
2304 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002305 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002306 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002307 "or use the exported attribute for this activity. "
2308 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002310 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002312
Michael Jurka86a720e2012-05-09 11:23:23 -07002313 boolean startActivitySafely(View v, Intent intent, Object tag) {
2314 boolean success = false;
2315 try {
2316 success = startActivity(v, intent, tag);
2317 } catch (ActivityNotFoundException e) {
2318 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2319 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2320 }
2321 return success;
2322 }
2323
Romain Guy8e633c52010-03-04 12:51:36 -08002324 void startActivityForResultSafely(Intent intent, int requestCode) {
2325 try {
2326 startActivityForResult(intent, requestCode);
2327 } catch (ActivityNotFoundException e) {
2328 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2329 } catch (SecurityException e) {
2330 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2331 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
2332 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
2333 "or use the exported attribute for this activity.", e);
2334 }
2335 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002336
Adam Cohena9cf38f2011-05-02 15:36:58 -07002337 private void handleFolderClick(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002338 final FolderInfo info = folderIcon.getFolderInfo();
Adam Cohen3c81a382011-08-31 22:25:51 -07002339 Folder openFolder = mWorkspace.getFolderForTag(info);
2340
2341 // If the folder info reports that the associated folder is open, then verify that
2342 // it is actually opened. There have been a few instances where this gets out of sync.
2343 if (info.opened && openFolder == null) {
2344 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
Adam Cohendcd297f2013-06-18 13:13:40 -07002345 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
Adam Cohen3c81a382011-08-31 22:25:51 -07002346 info.opened = false;
2347 }
2348
Adam Cohenfb91f302012-06-11 15:45:18 -07002349 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002350 // Close any open folder
2351 closeFolder();
2352 // Open the requested folder
Adam Cohena9cf38f2011-05-02 15:36:58 -07002353 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002354 } else {
2355 // Find the open folder...
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002356 int folderScreen;
2357 if (openFolder != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07002358 folderScreen = mWorkspace.getPageForView(openFolder);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002359 // .. and close it
2360 closeFolder(openFolder);
Michael Jurka0142d492010-08-25 17:46:15 -07002361 if (folderScreen != mWorkspace.getCurrentPage()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002362 // Close any folder open on the current screen
2363 closeFolder();
2364 // Pull the folder onto this screen
Adam Cohena9cf38f2011-05-02 15:36:58 -07002365 openFolder(folderIcon);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 }
2367 }
2368 }
2369 }
2370
Adam Cohen268c4752012-06-06 17:47:33 -07002371 /**
2372 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2373 * in the DragLayer in the exact absolute location of the original FolderIcon.
2374 */
2375 private void copyFolderIconToImage(FolderIcon fi) {
2376 final int width = fi.getMeasuredWidth();
2377 final int height = fi.getMeasuredHeight();
2378
2379 // Lazy load ImageView, Bitmap and Canvas
2380 if (mFolderIconImageView == null) {
2381 mFolderIconImageView = new ImageView(this);
2382 }
2383 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2384 mFolderIconBitmap.getHeight() != height) {
2385 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2386 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2387 }
2388
2389 DragLayer.LayoutParams lp;
2390 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2391 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2392 } else {
2393 lp = new DragLayer.LayoutParams(width, height);
2394 }
2395
Adam Cohen307fe232012-08-16 17:55:58 -07002396 // The layout from which the folder is being opened may be scaled, adjust the starting
2397 // view size by this scale factor.
2398 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002399 lp.customPosition = true;
2400 lp.x = mRectForFolderAnimation.left;
2401 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002402 lp.width = (int) (scale * width);
2403 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002404
2405 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2406 fi.draw(mFolderIconCanvas);
2407 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002408 if (fi.getFolder() != null) {
2409 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2410 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002411 }
Adam Cohen268c4752012-06-06 17:47:33 -07002412 // Just in case this image view is still in the drag layer from a previous animation,
2413 // we remove it and re-add it.
2414 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2415 mDragLayer.removeView(mFolderIconImageView);
2416 }
2417 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002418 if (fi.getFolder() != null) {
2419 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002420 }
Adam Cohen268c4752012-06-06 17:47:33 -07002421 }
2422
Adam Cohen2801caf2011-05-13 20:57:39 -07002423 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002424 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002425 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2426 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2427 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2428
Adam Cohenc51934b2011-07-26 21:07:43 -07002429 FolderInfo info = (FolderInfo) fi.getTag();
2430 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2431 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002432 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2433 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002434 }
2435
Adam Cohen268c4752012-06-06 17:47:33 -07002436 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2437 copyFolderIconToImage(fi);
2438 fi.setVisibility(View.INVISIBLE);
2439
Michael Jurka2ecf9952012-06-18 12:52:28 -07002440 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002441 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002442 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2443 oa.start();
2444 }
2445
Adam Cohen268c4752012-06-06 17:47:33 -07002446 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002447 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002448 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2449 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2450 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2451
Adam Cohen268c4752012-06-06 17:47:33 -07002452 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002453
Adam Cohen268c4752012-06-06 17:47:33 -07002454 // We remove and re-draw the FolderIcon in-case it has changed
2455 mDragLayer.removeView(mFolderIconImageView);
2456 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002457 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002458 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002459 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002460 oa.addListener(new AnimatorListenerAdapter() {
2461 @Override
2462 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002463 if (cl != null) {
2464 cl.clearFolderLeaveBehind();
2465 // Remove the ImageView copy of the FolderIcon and make the original visible.
2466 mDragLayer.removeView(mFolderIconImageView);
2467 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002468 }
2469 }
2470 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002471 oa.start();
2472 }
2473
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002474 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002475 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002476 * is animated relative to the specified View. If the View is null, no animation
2477 * is played.
2478 *
2479 * @param folderInfo The FolderInfo describing the folder to open.
2480 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002481 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002482 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002483 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484
Adam Cohena9cf38f2011-05-02 15:36:58 -07002485 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002486
Adam Cohen4554ee12011-08-03 16:13:21 -07002487 // Just verify that the folder hasn't already been added to the DragLayer.
2488 // There was a one-off crash where the folder had a parent already.
2489 if (folder.getParent() == null) {
2490 mDragLayer.addView(folder);
2491 mDragController.addDropTarget((DropTarget) folder);
2492 } else {
2493 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2494 folder.getParent() + ").");
2495 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002496 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002497 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002498
2499 // Notify the accessibility manager that this folder "window" has appeared and occluded
2500 // the workspace items
2501 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2502 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002503 }
2504
2505 public void closeFolder() {
2506 Folder folder = mWorkspace.getOpenFolder();
2507 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002508 if (folder.isEditingName()) {
2509 folder.dismissEditingName();
2510 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002511 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002512
2513 // Dismiss the folder cling
2514 dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002515 }
2516 }
2517
2518 void closeFolder(Folder folder) {
2519 folder.getInfo().opened = false;
2520
2521 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2522 if (parent != null) {
2523 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2524 shrinkAndFadeInFolderIcon(fi);
2525 }
2526 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002527
2528 // Notify the accessibility manager that this folder "window" has disappeard and no
2529 // longer occludeds the workspace items
2530 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002531 }
2532
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002533 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002534 if (!isDraggingEnabled()) return false;
2535 if (isWorkspaceLocked()) return false;
2536 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002537
2538 if (!(v instanceof CellLayout)) {
Michael Jurka8c920dd2011-01-20 14:16:56 -08002539 v = (View) v.getParent().getParent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002540 }
2541
Michael Jurka0280c3b2010-09-17 15:00:07 -07002542 resetAddInfo();
2543 CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002544 // This happens when long clicking an item with the dpad/trackball
Adam Cohenfaea1f82011-07-21 16:23:57 -07002545 if (longClickCellInfo == null) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002546 return true;
2547 }
2548
Winson Chung3d503fb2011-07-13 17:25:49 -07002549 // The hotseat touch handling does not go through Workspace, and we always allow long press
2550 // on hotseat items.
Michael Jurka0280c3b2010-09-17 15:00:07 -07002551 final View itemUnderLongClick = longClickCellInfo.cell;
Winson Chung3d503fb2011-07-13 17:25:49 -07002552 boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
2553 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002554 if (itemUnderLongClick == null) {
2555 // User long pressed on empty space
Michael Jurka0280c3b2010-09-17 15:00:07 -07002556 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2557 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohen7d30a372013-07-01 17:03:59 -07002558 // Disabling reordering until we sort out some issues.
Adam Cohenf358a4b2013-07-23 16:47:31 -07002559 if (mWorkspace.isInOverviewMode()) {
2560 mWorkspace.startReordering(v);
Adam Cohendedbd962013-07-11 14:21:49 -07002561 } else {
Adam Cohenf358a4b2013-07-23 16:47:31 -07002562 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07002563 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002564 } else {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002565 if (!(itemUnderLongClick instanceof Folder)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002566 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07002567 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002568 }
2569 }
2570 }
2571 return true;
2572 }
2573
Winson Chung3d503fb2011-07-13 17:25:49 -07002574 boolean isHotseatLayout(View layout) {
2575 return mHotseat != null && layout != null &&
2576 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
2577 }
2578 Hotseat getHotseat() {
2579 return mHotseat;
Romain Guy1fbc1c82009-11-09 20:43:08 -08002580 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002581 View getOverviewPanel() {
2582 return mOverviewPanel;
2583 }
Adam Cohenebea84d2011-11-09 17:20:41 -08002584 SearchDropTargetBar getSearchBar() {
2585 return mSearchDropTargetBar;
2586 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08002587
Winson Chung3d503fb2011-07-13 17:25:49 -07002588 /**
2589 * Returns the CellLayout of the specified container at the specified screen.
2590 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002591 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002592 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2593 if (mHotseat != null) {
2594 return mHotseat.getLayout();
2595 } else {
2596 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08002597 }
Winson Chung3d503fb2011-07-13 17:25:49 -07002598 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07002599 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08002600 }
2601 }
2602
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002603 Workspace getWorkspace() {
2604 return mWorkspace;
2605 }
2606
Daniel Sandler843e8602010-06-07 14:59:01 -04002607 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002608 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04002609 }
2610
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002611 /**
2612 * Helper method for the cameraZoomIn/cameraZoomOut animations
2613 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002614 * @param scaleFactor The scale factor used for the zoom
2615 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07002616 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002617 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07002618 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002619 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05002620
Winson Chung18f41f82012-05-09 13:28:10 -07002621 void disableWallpaperIfInAllApps() {
2622 // Only disable it if we are in all apps
Winson Chungc93e5ae2012-07-23 20:48:26 -07002623 if (isAllAppsVisible()) {
Winson Chung18f41f82012-05-09 13:28:10 -07002624 if (mAppsCustomizeTabHost != null &&
2625 !mAppsCustomizeTabHost.isTransitioning()) {
2626 updateWallpaperVisibility(false);
2627 }
2628 }
2629 }
2630
Craig Mautner360310b2012-10-26 15:13:08 -07002631 private void setWorkspaceBackground(boolean workspace) {
2632 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02002633 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07002634 }
2635
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002636 void updateWallpaperVisibility(boolean visible) {
2637 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
2638 int curflags = getWindow().getAttributes().flags
2639 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
2640 if (wpflags != curflags) {
2641 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
2642 }
Craig Mautner360310b2012-10-26 15:13:08 -07002643 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002644 }
2645
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002646 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
2647 if (v instanceof LauncherTransitionable) {
2648 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
2649 }
2650 }
2651
Michael Jurkabed61d22012-02-14 22:51:29 -08002652 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
2653 if (v instanceof LauncherTransitionable) {
2654 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
2655 }
Winson Chung70442722012-02-10 15:43:22 -08002656
2657 // Update the workspace transition step as well
2658 dispatchOnLauncherTransitionStep(v, 0f);
2659 }
2660
2661 private void dispatchOnLauncherTransitionStep(View v, float t) {
2662 if (v instanceof LauncherTransitionable) {
2663 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
2664 }
Michael Jurkabed61d22012-02-14 22:51:29 -08002665 }
2666
2667 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
2668 if (v instanceof LauncherTransitionable) {
2669 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
2670 }
Winson Chung70442722012-02-10 15:43:22 -08002671
2672 // Update the workspace transition step as well
2673 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08002674 }
2675
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002676 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07002677 * Things to test when changing the following seven functions.
2678 * - Home from workspace
2679 * - from center screen
2680 * - from other screens
2681 * - Home from all apps
2682 * - from center screen
2683 * - from other screens
2684 * - Back from all apps
2685 * - from center screen
2686 * - from other screens
2687 * - Launch app from workspace and quit
2688 * - with back
2689 * - with home
2690 * - Launch app from all apps and quit
2691 * - with back
2692 * - with home
2693 * - Go to a screen that's not the default, then all
2694 * apps, and launch and app, and go back
2695 * - with back
2696 * -with home
2697 * - On workspace, long press power and go back
2698 * - with back
2699 * - with home
2700 * - On all apps, long press power and go back
2701 * - with back
2702 * - with home
2703 * - On workspace, power off
2704 * - On all apps, power off
2705 * - Launch an app and turn off the screen while in that app
2706 * - Go back with home key
2707 * - Go back with back key TODO: make this not go to workspace
2708 * - From all apps
2709 * - From workspace
2710 * - Enter and exit car mode (becuase it causes an extra configuration changed)
2711 * - From all apps
2712 * - From the center workspace
2713 * - From another workspace
2714 */
2715
2716 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002717 * Zoom the camera out from the workspace to reveal 'toView'.
2718 * Assumes that the view to show is anchored at either the very top or very bottom
2719 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002720 */
Michael Jurkabed61d22012-02-14 22:51:29 -08002721 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002722 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002723 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002724 mStateAnimation.cancel();
2725 mStateAnimation = null;
2726 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002727 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002728
Winson Chungf0ea4d32011-06-06 14:27:16 -07002729 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
2730 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
2731 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08002732 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002733 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07002734 final int startDelay =
2735 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07002736
Michael Jurkab3e22d92011-10-31 15:58:33 -07002737 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002738
Michael Jurkad74c9842011-07-10 12:44:21 -07002739 // Shrink workspaces away if going to AppsCustomize from workspace
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002740 Animator workspaceAnim =
2741 mWorkspace.getChangeStateAnimation(Workspace.State.SMALL, animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002742
2743 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08002744 toView.setScaleX(scale);
2745 toView.setScaleY(scale);
2746 final LauncherViewPropertyAnimator scaleAnim = new LauncherViewPropertyAnimator(toView);
2747 scaleAnim.
2748 scaleX(1f).scaleY(1f).
2749 setDuration(duration).
2750 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08002751
Winson Chungf0ea4d32011-06-06 14:27:16 -07002752 toView.setVisibility(View.VISIBLE);
Adam Cohenc00f0b92011-12-06 19:45:06 -08002753 toView.setAlpha(0f);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002754 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002755 .ofFloat(toView, "alpha", 0f, 1f)
2756 .setDuration(fadeDuration);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002757 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
Winson Chung70442722012-02-10 15:43:22 -08002758 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2759 @Override
2760 public void onAnimationUpdate(ValueAnimator animation) {
Michael Jurka059798a2012-09-04 09:20:16 -07002761 if (animation == null) {
2762 throw new RuntimeException("animation is null");
2763 }
Winson Chung70442722012-02-10 15:43:22 -08002764 float t = (Float) animation.getAnimatedValue();
2765 dispatchOnLauncherTransitionStep(fromView, t);
2766 dispatchOnLauncherTransitionStep(toView, t);
2767 }
2768 });
Adam Cohenf16e5712011-01-13 13:31:45 -08002769
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002770 // toView should appear right at the end of the workspace shrink
2771 // animation
Michael Jurka2ecf9952012-06-18 12:52:28 -07002772 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002773 mStateAnimation.play(scaleAnim).after(startDelay);
Michael Jurka7407d2a2011-12-12 21:48:38 -08002774 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002775
2776 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Adam Cohenf4ddea32011-09-12 13:46:42 -07002777 boolean animationCancelled = false;
2778
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002779 @Override
Chet Haaseb1254a62010-09-07 13:35:00 -07002780 public void onAnimationStart(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002781 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002782 // Prepare the position
2783 toView.setTranslationX(0.0f);
2784 toView.setTranslationY(0.0f);
2785 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002786 toView.bringToFront();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002787 }
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002788 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002789 public void onAnimationEnd(Animator animation) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002790 dispatchOnLauncherTransitionEnd(fromView, animated, false);
2791 dispatchOnLauncherTransitionEnd(toView, animated, false);
Winson Chung32174c82011-07-19 15:47:55 -07002792
Adam Cohenf4ddea32011-09-12 13:46:42 -07002793 if (!animationCancelled) {
2794 updateWallpaperVisibility(false);
2795 }
Winson Chungc7d2b602012-05-16 17:02:20 -07002796
2797 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002798 if (mSearchDropTargetBar != null) {
2799 mSearchDropTargetBar.hideSearchBar(false);
2800 }
Adam Cohenf4ddea32011-09-12 13:46:42 -07002801 }
2802
Adam Cohencff6af82011-09-13 14:51:53 -07002803 @Override
Adam Cohenf4ddea32011-09-12 13:46:42 -07002804 public void onAnimationCancel(Animator animation) {
2805 animationCancelled = true;
Michael Jurka3c4c20f2010-10-28 15:36:06 -07002806 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002807 });
2808
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002809 if (workspaceAnim != null) {
2810 mStateAnimation.play(workspaceAnim);
2811 }
Michael Jurka1899a362011-11-03 13:50:45 -07002812
2813 boolean delayAnim = false;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002814
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002815 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
2816 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002817
2818 // If any of the objects being animated haven't been measured/laid out
2819 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08002820 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002821 (mWorkspace.getMeasuredWidth() == 0) ||
2822 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002823 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07002824 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002825
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002826 final AnimatorSet stateAnimation = mStateAnimation;
2827 final Runnable startAnimRunnable = new Runnable() {
2828 public void run() {
2829 // Check that mStateAnimation hasn't changed while
2830 // we waited for a layout/draw pass
2831 if (mStateAnimation != stateAnimation)
2832 return;
2833 setPivotsForZoom(toView, scale);
2834 dispatchOnLauncherTransitionStart(fromView, animated, false);
2835 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002836 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002837 }
2838 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002839 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002840 final ViewTreeObserver observer = toView.getViewTreeObserver();
2841 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
2842 public void onGlobalLayout() {
2843 startAnimRunnable.run();
2844 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
2845 }
2846 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002847 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07002848 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07002849 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002850 } else {
2851 toView.setTranslationX(0.0f);
2852 toView.setTranslationY(0.0f);
2853 toView.setScaleX(1.0f);
2854 toView.setScaleY(1.0f);
2855 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07002856 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07002857
Daniel Sandlere4f98912013-06-25 15:13:26 -04002858 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07002859 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07002860 if (mSearchDropTargetBar != null) {
2861 mSearchDropTargetBar.hideSearchBar(false);
2862 }
Michael Jurkaabded662011-03-04 12:06:57 -08002863 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002864 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002865 dispatchOnLauncherTransitionStart(fromView, animated, false);
2866 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002867 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08002868 dispatchOnLauncherTransitionStart(toView, animated, false);
2869 dispatchOnLauncherTransitionEnd(toView, animated, false);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002870 updateWallpaperVisibility(false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002871 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002872 }
2873
2874 /**
2875 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07002876 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07002877 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002878 */
Adam Cohened66b2b2012-01-23 17:28:51 -08002879 private void hideAppsCustomizeHelper(State toState, final boolean animated,
2880 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08002881
Michael Jurkab3e22d92011-10-31 15:58:33 -07002882 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002883 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07002884 mStateAnimation.cancel();
2885 mStateAnimation = null;
2886 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002887 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08002888
Winson Chungf0ea4d32011-06-06 14:27:16 -07002889 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Michael Jurka159b4cc2012-01-17 03:00:35 -08002890 final int fadeOutDuration =
2891 res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Winson Chungf0ea4d32011-06-06 14:27:16 -07002892 final float scaleFactor = (float)
2893 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
2894 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08002895 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002896 Animator workspaceAnim = null;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002897 if (toState == State.WORKSPACE) {
2898 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
2899 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohenf358a4b2013-07-23 16:47:31 -07002900 Workspace.State.NORMAL, animated, stagger, -1);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002901 } else if (toState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
2902 workspaceAnim = mWorkspace.getChangeStateAnimation(
2903 Workspace.State.SPRING_LOADED, animated);
2904 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07002905
Michael Jurkab3e22d92011-10-31 15:58:33 -07002906 setPivotsForZoom(fromView, scaleFactor);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002907 updateWallpaperVisibility(true);
Winson Chung4afe9b32011-07-27 17:46:20 -07002908 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002909 if (animated) {
Michael Jurka159b4cc2012-01-17 03:00:35 -08002910 final LauncherViewPropertyAnimator scaleAnim =
2911 new LauncherViewPropertyAnimator(fromView);
2912 scaleAnim.
2913 scaleX(scaleFactor).scaleY(scaleFactor).
2914 setDuration(duration).
2915 setInterpolator(new Workspace.ZoomInInterpolator());
2916
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002917 final ObjectAnimator alphaAnim = LauncherAnimUtils
Michael Jurka159b4cc2012-01-17 03:00:35 -08002918 .ofFloat(fromView, "alpha", 1f, 0f)
2919 .setDuration(fadeOutDuration);
Adam Cohencff6af82011-09-13 14:51:53 -07002920 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
Winson Chung70442722012-02-10 15:43:22 -08002921 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
2922 @Override
2923 public void onAnimationUpdate(ValueAnimator animation) {
2924 float t = 1f - (Float) animation.getAnimatedValue();
2925 dispatchOnLauncherTransitionStep(fromView, t);
2926 dispatchOnLauncherTransitionStep(toView, t);
2927 }
2928 });
Michael Jurka159b4cc2012-01-17 03:00:35 -08002929
Michael Jurka2ecf9952012-06-18 12:52:28 -07002930 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Michael Jurkabed61d22012-02-14 22:51:29 -08002931
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002932 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
2933 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Chet Haasebc2f0822012-10-26 17:59:53 -07002934 mAppsCustomizeContent.pauseScrolling();
Michael Jurkabed61d22012-02-14 22:51:29 -08002935
2936 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07002937 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08002938 public void onAnimationEnd(Animator animation) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07002939 updateWallpaperVisibility(true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002940 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08002941 dispatchOnLauncherTransitionEnd(fromView, animated, true);
2942 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08002943 if (onCompleteRunnable != null) {
2944 onCompleteRunnable.run();
2945 }
Chet Haasebc2f0822012-10-26 17:59:53 -07002946 mAppsCustomizeContent.updateCurrentPageScroll();
2947 mAppsCustomizeContent.resumeScrolling();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002948 }
2949 });
2950
Winson Chungf0ea4d32011-06-06 14:27:16 -07002951 mStateAnimation.playTogether(scaleAnim, alphaAnim);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08002952 if (workspaceAnim != null) {
2953 mStateAnimation.play(workspaceAnim);
2954 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002955 dispatchOnLauncherTransitionStart(fromView, animated, true);
2956 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01002957 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002958 } else {
2959 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002960 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002961 dispatchOnLauncherTransitionStart(fromView, animated, true);
2962 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07002963 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08002964 dispatchOnLauncherTransitionStart(toView, animated, true);
2965 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07002966 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07002967 }
2968
Michael Jurkae326f182011-11-21 14:05:46 -08002969 @Override
2970 public void onTrimMemory(int level) {
2971 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07002972 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08002973 mAppsCustomizeTabHost.onTrimMemory();
2974 }
2975 }
2976
Winson Chung18f41f82012-05-09 13:28:10 -07002977 @Override
2978 public void onWindowFocusChanged(boolean hasFocus) {
2979 if (!hasFocus) {
2980 // When another window occludes launcher (like the notification shade, or recents),
2981 // ensure that we enable the wallpaper flag so that transitions are done correctly.
2982 updateWallpaperVisibility(true);
2983 } else {
2984 // When launcher has focus again, disable the wallpaper if we are in AllApps
Winson Chung6cf79092012-06-07 14:50:10 -07002985 mWorkspace.postDelayed(new Runnable() {
2986 @Override
2987 public void run() {
2988 disableWallpaperIfInAllApps();
2989 }
2990 }, 500);
Winson Chung18f41f82012-05-09 13:28:10 -07002991 }
2992 }
2993
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002994 void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08002995 showWorkspace(animated, null);
2996 }
2997
2998 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07002999 if (mState != State.WORKSPACE) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003000 boolean wasInSpringLoadedMode = (mState == State.APPS_CUSTOMIZE_SPRING_LOADED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003001 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003002 hideAppsCustomizeHelper(State.WORKSPACE, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003003
Winson Chungc7d2b602012-05-16 17:02:20 -07003004 // Show the search bar (only animate if we were showing the drop target bar in spring
3005 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003006 if (mSearchDropTargetBar != null) {
3007 mSearchDropTargetBar.showSearchBar(wasInSpringLoadedMode);
3008 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003009
Michael Jurkab3e22d92011-10-31 15:58:33 -07003010 // Set focus to the AppsCustomize button
3011 if (mAllAppsButton != null) {
3012 mAllAppsButton.requestFocus();
3013 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003014 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003015
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003016 // Change the state *after* we've called all the transition code
3017 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003018
Winson Chung5fb63472011-02-02 17:03:37 -08003019 // Resume the auto-advance of widgets
3020 mUserPresent = true;
3021 updateRunning();
3022
alanv1d4fde62012-10-17 13:15:47 -07003023 // Send an accessibility event to announce the context change
3024 getWindow().getDecorView()
3025 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003026
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003027 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003028 }
3029
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003030 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003031 }
3032
Michael Jurkab3e22d92011-10-31 15:58:33 -07003033 void showAllApps(boolean animated) {
3034 if (mState != State.WORKSPACE) return;
3035
3036 showAppsCustomizeHelper(animated, false);
3037 mAppsCustomizeTabHost.requestFocus();
3038
Michael Jurkab3e22d92011-10-31 15:58:33 -07003039 // Change the state *after* we've called all the transition code
3040 mState = State.APPS_CUSTOMIZE;
3041
3042 // Pause the auto-advance of widgets until we are out of AllApps
3043 mUserPresent = false;
3044 updateRunning();
3045 closeFolder();
3046
3047 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003048 getWindow().getDecorView()
3049 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003050 }
3051
Adam Cohen7777d962011-08-18 18:58:38 -07003052 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003053 if (isAllAppsVisible()) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003054 hideAppsCustomizeHelper(State.APPS_CUSTOMIZE_SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003055 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003056 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003057 }
Adam Cohen7777d962011-08-18 18:58:38 -07003058
Adam Cohened66b2b2012-01-23 17:28:51 -08003059 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, boolean extendedDelay,
3060 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003061 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3062
Winson Chunge7a03942011-08-05 15:05:12 -07003063 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003064 @Override
3065 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003066 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003067 // Before we show workspace, hide all apps again because
3068 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3069 // clean up our state transition functions
3070 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003071 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003072 } else {
3073 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003074 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003075 }
3076 }, (extendedDelay ?
3077 EXIT_SPRINGLOADED_MODE_LONG_TIMEOUT :
3078 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT));
3079 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003080
Michael Jurkad3ef3062010-11-23 16:23:58 -08003081 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003082 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003083 final boolean animated = true;
3084 final boolean springLoaded = true;
3085 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003086 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003087 }
3088 // Otherwise, we are not in spring loaded mode, so don't do anything.
3089 }
3090
Michael Jurkab3e22d92011-10-31 15:58:33 -07003091 void lockAllApps() {
3092 // TODO
3093 }
3094
3095 void unlockAllApps() {
3096 // TODO
3097 }
3098
Winson Chungf0ea4d32011-06-06 14:27:16 -07003099 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003100 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003101 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003102 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003103 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003104 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003105 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003106 int duration = 0;
3107 if (mSearchDropTargetBar != null) {
3108 duration = mSearchDropTargetBar.getTransitionInDuration();
3109 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003110 mHotseat.animate().alpha(1f).setDuration(duration);
3111 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003112 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003113 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003114 }
3115 }
3116 }
3117
3118 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003119 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003120 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003121 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003122 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003123 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003124 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003125 int duration = 0;
3126 if (mSearchDropTargetBar != null) {
3127 duration = mSearchDropTargetBar.getTransitionOutDuration();
3128 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003129 mHotseat.animate().alpha(0f).setDuration(duration);
3130 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003131 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003132 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003133 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003134 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003135 }
3136
Patrick Dubroy5f445422011-02-18 14:35:21 -08003137 /**
3138 * Add an item from all apps or customize onto the given workspace screen.
3139 * If layout is null, add to the current screen.
3140 */
3141 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003142 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003143 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003144 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003145 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003146
Winson Chungdff8ebb2011-09-08 17:25:31 -07003147 /** Maps the current orientation to an index for referencing orientation correct global icons */
3148 private int getCurrentOrientationIndexForGlobalIcons() {
3149 // default - 0, landscape - 1
3150 switch (getResources().getConfiguration().orientation) {
3151 case Configuration.ORIENTATION_LANDSCAPE:
3152 return 1;
3153 default:
3154 return 0;
3155 }
3156 }
3157
Michael Jurkaae65ee32012-04-30 11:45:54 -07003158 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003159 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003160 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003161 // Look for the toolbar icon specified in the activity meta-data
3162 Bundle metaData = packageManager.getActivityInfo(
3163 activityName, PackageManager.GET_META_DATA).metaData;
3164 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003165 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003166 if (iconResId != 0) {
3167 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003168 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003169 }
3170 }
3171 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003172 // This can happen if the activity defines an invalid drawable
3173 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3174 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003175 } catch (Resources.NotFoundException nfe) {
3176 // This can happen if the activity defines an invalid drawable
3177 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3178 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003179 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003180 return null;
3181 }
3182
3183 // if successful in getting icon, return it; otherwise, set button to use default drawable
3184 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003185 int buttonId, ComponentName activityName, int fallbackDrawableId,
3186 String toolbarResourceName) {
3187 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003188 Resources r = getResources();
3189 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3190 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003191
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003192 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003193 // If we were unable to find the icon via the meta-data, use a generic one
3194 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003195 toolbarIcon = r.getDrawable(fallbackDrawableId);
3196 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003197 if (button != null) {
3198 button.setCompoundDrawables(toolbarIcon, null, null, null);
3199 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003200 return null;
3201 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003202 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003203 if (button != null) {
3204 button.setCompoundDrawables(toolbarIcon, null, null, null);
3205 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003206 return toolbarIcon.getConstantState();
3207 }
3208 }
3209
3210 // if successful in getting icon, return it; otherwise, set button to use default drawable
3211 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003212 int buttonId, ComponentName activityName, int fallbackDrawableId,
3213 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003214 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003215 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003216
Michael Jurka19e0fc52011-07-22 18:00:21 -07003217 if (button != null) {
3218 // If we were unable to find the icon via the meta-data, use a
3219 // generic one
3220 if (toolbarIcon == null) {
3221 button.setImageResource(fallbackDrawableId);
3222 } else {
3223 button.setImageDrawable(toolbarIcon);
3224 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003225 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003226
3227 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3228
Michael Jurka0423dcf2010-10-05 14:56:18 -07003229 }
3230
Winson Chung1b884092012-06-08 17:13:02 -07003231 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003232 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003233 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003234 }
3235
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003236 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003237 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003238 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003239 }
3240
Winson Chungbb185bd2011-11-21 12:31:42 -08003241 private void invalidatePressedFocusedStates(View container, View button) {
3242 if (container instanceof HolographicLinearLayout) {
3243 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3244 layout.invalidatePressedFocusedStates();
3245 } else if (button instanceof HolographicImageView) {
3246 HolographicImageView view = (HolographicImageView) button;
3247 view.invalidatePressedFocusedStates();
3248 }
3249 }
3250
Cristina Stancu476493b2013-08-07 17:20:14 +01003251 public View getQsbBar() {
3252 if (mQsbBar == null) {
3253 mQsbBar = mInflater.inflate(R.layout.qsb_bar, mSearchDropTargetBar);
3254 }
3255 return mQsbBar;
3256 }
3257
3258 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003259 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003260 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003261 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003262 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003263
Winson Chung1cad91e2011-05-25 17:41:01 -07003264 final SearchManager searchManager =
3265 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3266 ComponentName activityName = searchManager.getGlobalSearchActivity();
3267 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003268 int coi = getCurrentOrientationIndexForGlobalIcons();
3269 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003270 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3271 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3272 if (sGlobalSearchIcon[coi] == null) {
3273 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3274 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3275 TOOLBAR_ICON_METADATA_NAME);
3276 }
3277
Winson Chungc51db6a2011-10-05 11:44:49 -07003278 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3279 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003280 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003281 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003282 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003283 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003284 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3285 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
3286 searchButton.setVisibility(View.GONE);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003287 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003288 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003289 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003290 }
3291 }
3292
Cristina Stancu476493b2013-08-07 17:20:14 +01003293 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003294 final View searchButtonContainer = findViewById(R.id.search_button_container);
3295 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003296 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003297 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003298 }
3299
Cristina Stancu476493b2013-08-07 17:20:14 +01003300 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003301 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003302 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003303
Winson Chungc51db6a2011-10-05 11:44:49 -07003304 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003305 final SearchManager searchManager =
3306 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3307 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3308
3309 ComponentName activityName = null;
3310 if (globalSearchActivity != null) {
3311 // Check if the global search activity handles voice search
3312 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3313 intent.setPackage(globalSearchActivity.getPackageName());
3314 activityName = intent.resolveActivity(getPackageManager());
3315 }
3316
3317 if (activityName == null) {
3318 // Fallback: check if an activity other than the global search activity
3319 // resolves this
3320 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3321 activityName = intent.resolveActivity(getPackageManager());
3322 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003323 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003324 int coi = getCurrentOrientationIndexForGlobalIcons();
3325 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003326 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3327 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3328 if (sVoiceSearchIcon[coi] == null) {
3329 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3330 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3331 TOOLBAR_ICON_METADATA_NAME);
3332 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003333 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003334 voiceButton.setVisibility(View.VISIBLE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003335 setVoiceButtonProxyVisible(true);
Winson Chungbb185bd2011-11-21 12:31:42 -08003336 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003337 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003338 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003339 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003340 voiceButton.setVisibility(View.GONE);
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003341 setVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003342 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003343 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003344 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003345
Cristina Stancu476493b2013-08-07 17:20:14 +01003346 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003347 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3348 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003349 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003350 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003351 }
3352
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003353 public void setVoiceButtonProxyVisible(boolean visible) {
3354 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3355 if (voiceButtonProxy != null) {
3356 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
3357 }
3358 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003359 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003360 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003361 */
3362 private void updateAppMarketIcon() {
Winson Chung785d2eb2011-04-14 16:08:02 -07003363 final View marketButton = findViewById(R.id.market_button);
3364 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3365 // Find the app market activity by resolving an intent.
3366 // (If multiple app markets are installed, it will return the ResolverActivity.)
3367 ComponentName activityName = intent.resolveActivity(getPackageManager());
Winson Chung6a26e5b2011-05-26 14:36:06 -07003368 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003369 int coi = getCurrentOrientationIndexForGlobalIcons();
Winson Chung785d2eb2011-04-14 16:08:02 -07003370 mAppMarketIntent = intent;
Winson Chungdff8ebb2011-09-08 17:25:31 -07003371 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003372 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3373 TOOLBAR_ICON_METADATA_NAME);
Winson Chung785d2eb2011-04-14 16:08:02 -07003374 marketButton.setVisibility(View.VISIBLE);
3375 } else {
3376 // We should hide and disable the view so that we don't try and restore the visibility
3377 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3378 marketButton.setVisibility(View.GONE);
3379 marketButton.setEnabled(false);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003380 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003381 }
3382
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003383 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chung1b884092012-06-08 17:13:02 -07003384 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
3385 Resources r = getResources();
3386 Drawable marketIconDrawable = d.newDrawable(r);
3387 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3388 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
3389 marketIconDrawable.setBounds(0, 0, w, h);
3390
3391 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003392 }
3393
Michael Jurkad7c28052012-04-27 15:43:36 -07003394 @Override
3395 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003396 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003397 final List<CharSequence> text = event.getText();
3398 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003399 // Populate event with a fake title based on the current state.
3400 if (mState == State.APPS_CUSTOMIZE) {
3401 text.add(getString(R.string.all_apps_button_label));
3402 } else {
3403 text.add(getString(R.string.all_apps_home_button_label));
3404 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003405 return result;
3406 }
3407
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003408 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003409 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003410 */
3411 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
3412 @Override
3413 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003414 closeSystemDialogs();
3415 }
3416 }
3417
3418 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003419 * Receives notifications whenever the appwidgets are reset.
3420 */
3421 private class AppWidgetResetObserver extends ContentObserver {
3422 public AppWidgetResetObserver() {
3423 super(new Handler());
3424 }
3425
3426 @Override
3427 public void onChange(boolean selfChange) {
3428 onAppWidgetReset();
3429 }
3430 }
3431
3432 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003433 * If the activity is currently paused, signal that we need to run the passed Runnable
3434 * in onResume.
3435 *
3436 * This needs to be called from incoming places where resources might have been loaded
3437 * while we are paused. That is becaues the Configuration might be wrong
3438 * when we're not running, and if it comes back to what it was when we
3439 * were paused, we are not restarted.
3440 *
3441 * Implementation of the method from LauncherModel.Callbacks.
3442 *
3443 * @return true if we are currently paused. The caller might be able to
3444 * skip some work in that case since we will come back again.
3445 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003446 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003447 if (mPaused) {
3448 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003449 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003450 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003451 }
3452 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003453 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003454 return true;
3455 } else {
3456 return false;
3457 }
3458 }
3459
Michael Jurkac402cd92013-05-20 15:49:32 +02003460 private boolean waitUntilResume(Runnable run) {
3461 return waitUntilResume(run, false);
3462 }
3463
Michael Jurka1e2f4652013-07-08 18:03:46 -07003464 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003465 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003466 }
3467
Michael Jurka7607c2f2013-04-03 14:33:19 -07003468 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003469 * If the activity is currently paused, signal that we need to re-run the loader
3470 * in onResume.
3471 *
3472 * This needs to be called from incoming places where resources might have been loaded
3473 * while we are paused. That is becaues the Configuration might be wrong
3474 * when we're not running, and if it comes back to what it was when we
3475 * were paused, we are not restarted.
3476 *
3477 * Implementation of the method from LauncherModel.Callbacks.
3478 *
3479 * @return true if we are currently paused. The caller might be able to
3480 * skip some work in that case since we will come back again.
3481 */
3482 public boolean setLoadOnResume() {
3483 if (mPaused) {
3484 Log.i(TAG, "setLoadOnResume");
3485 mOnResumeNeedsLoad = true;
3486 return true;
3487 } else {
3488 return false;
3489 }
3490 }
3491
3492 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003493 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003494 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003495 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003496 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003497 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003498 } else {
3499 return SCREEN_COUNT / 2;
3500 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003501 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003502
Joe Onorato9c1289c2009-08-17 11:03:03 -04003503 /**
3504 * Refreshes the shortcuts shown on the workspace.
3505 *
3506 * Implementation of the method from LauncherModel.Callbacks.
3507 */
3508 public void startBinding() {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003509 // If we're starting binding all over again, clear any bind calls we'd postponed in
3510 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3511 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003512 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003513
Winson Chungd64d1762013-08-20 14:37:16 -07003514 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003515 mWorkspace.clearDropTargets();
Winson Chungd64d1762013-08-20 14:37:16 -07003516 mWorkspace.removeAllViews();
3517
Michael Jurka05bf6442011-11-30 20:28:53 -08003518 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003519 if (mHotseat != null) {
3520 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003521 }
3522 }
3523
Adam Cohendcd297f2013-06-18 13:13:40 -07003524 @Override
3525 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003526 bindAddScreens(orderedScreenIds);
3527 mWorkspace.addExtraEmptyScreen();
3528 }
3529
3530 @Override
3531 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003532 int count = orderedScreenIds.size();
3533 for (int i = 0; i < count; i++) {
Winson Chung76828c82013-08-19 15:43:29 -07003534 Log.w(TAG, "10249126 - bindAddScreens(" + orderedScreenIds.get(i) + ")");
Adam Cohen89bddfa2013-08-20 11:57:13 -07003535 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003536 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003537 }
3538
Adam Cohen39a06042013-07-19 14:30:12 -07003539 private boolean shouldShowWeightWatcher() {
3540 String spKey = LauncherAppState.getSharedPreferencesKey();
3541 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003542 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003543
3544 return show;
3545 }
3546
3547 private void toggleShowWeightWatcher() {
3548 String spKey = LauncherAppState.getSharedPreferencesKey();
3549 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3550 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3551
3552 show = !show;
3553
3554 SharedPreferences.Editor editor = sp.edit();
3555 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3556 editor.commit();
3557
3558 if (mWeightWatcher != null) {
3559 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3560 }
3561 }
3562
Winson Chungd64d1762013-08-20 14:37:16 -07003563 public void bindAppsAdded(final ArrayList<Long> newScreens,
3564 final ArrayList<ItemInfo> addNotAnimated,
3565 final ArrayList<ItemInfo> addAnimated) {
3566 Runnable r = new Runnable() {
3567 public void run() {
3568 bindAppsAdded(newScreens, addNotAnimated, addAnimated);
3569 }
3570 };
3571 if (waitUntilResume(r)) {
3572 return;
3573 }
3574
3575 Log.w(TAG, "10249126 - bindAppsAdded(" + newScreens.size() + ")");
3576
3577 // Add the new screens
3578 bindAddScreens(newScreens);
3579
3580 // We add the items without animation on non-visible pages, and with
3581 // animations on the new page (which we will try and snap to).
3582 if (!addNotAnimated.isEmpty()) {
3583 bindItems(addNotAnimated, 0,
3584 addNotAnimated.size(), false);
3585 }
3586 if (!addAnimated.isEmpty()) {
3587 bindItems(addAnimated, 0,
3588 addAnimated.size(), true);
3589 }
3590 }
3591
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003592 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003593 * Bind the items start-end from the list.
3594 *
3595 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003596 */
Winson Chung64359a52013-07-08 17:17:08 -07003597 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3598 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003599 Runnable r = new Runnable() {
3600 public void run() {
3601 bindItems(shortcuts, start, end, forceAnimateIcons);
3602 }
3603 };
3604 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003605 return;
3606 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003607
Winson Chungf0c6ae02012-03-21 16:10:31 -07003608 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003609 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3610 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003611 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003612 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003613 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003614 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003615 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003616
3617 // Short circuit if we are loading dock items for a configuration which has no dock
3618 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3619 mHotseat == null) {
3620 continue;
3621 }
3622
Joe Onorato9c1289c2009-08-17 11:03:03 -04003623 switch (item.itemType) {
3624 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3625 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003626 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003627 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003628
Winson Chung64359a52013-07-08 17:17:08 -07003629 /*
3630 * TODO: FIX collision case
3631 */
Winson Chungce376632013-07-11 16:12:41 -07003632 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3633 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3634 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
3635 throw new RuntimeException("OCCUPIED");
3636 }
Winson Chung64359a52013-07-08 17:17:08 -07003637 }
3638
Adam Cohendcd297f2013-06-18 13:13:40 -07003639 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3640 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003641 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003642 // Animate all the applications up now
3643 shortcut.setAlpha(0f);
3644 shortcut.setScaleX(0f);
3645 shortcut.setScaleY(0f);
3646 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003647 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003648 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003649 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003650 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003651 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003652 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003653 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003654 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3655 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003656 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003657 default:
3658 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003659 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003660 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003661
Winson Chung997a9232013-07-24 15:33:46 -07003662 if (animateIcons) {
3663 // Animate to the correct page
3664 if (newShortcutsScreenId > -1) {
3665 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
3666 int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
3667 if (newShortcutsScreenId != currentScreenId) {
3668 mWorkspace.snapToPage(newScreenIndex);
3669 }
3670 }
3671
Winson Chung64359a52013-07-08 17:17:08 -07003672 // We post the animation slightly delayed to prevent slowdowns when we are loading
3673 // right after we return to launcher.
3674 mWorkspace.postDelayed(new Runnable() {
3675 public void run() {
3676 anim.playTogether(bounceAnims);
3677 anim.start();
3678 }
3679 }, NEW_APPS_ANIMATION_DELAY);
3680 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003681 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003682 }
3683
Joe Onorato9c1289c2009-08-17 11:03:03 -04003684 /**
3685 * Implementation of the method from LauncherModel.Callbacks.
3686 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003687 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003688 Runnable r = new Runnable() {
3689 public void run() {
3690 bindFolders(folders);
3691 }
3692 };
3693 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003694 return;
3695 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07003696 sFolders.clear();
3697 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003698 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003699
3700 /**
3701 * Add the views for a widget to the workspace.
3702 *
3703 * Implementation of the method from LauncherModel.Callbacks.
3704 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003705 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003706 Runnable r = new Runnable() {
3707 public void run() {
3708 bindAppWidget(item);
3709 }
3710 };
3711 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003712 return;
3713 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003714
Daniel Sandler843e8602010-06-07 14:59:01 -04003715 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3716 if (DEBUG_WIDGETS) {
3717 Log.d(TAG, "bindAppWidget: " + item);
3718 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003719 final Workspace workspace = mWorkspace;
3720
3721 final int appWidgetId = item.appWidgetId;
3722 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04003723 if (DEBUG_WIDGETS) {
3724 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
3725 }
3726
Joe Onorato9c1289c2009-08-17 11:03:03 -04003727 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
3728
Joe Onorato9c1289c2009-08-17 11:03:03 -04003729 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07003730 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003731
Adam Cohendcd297f2013-06-18 13:13:40 -07003732 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04003733 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07003734 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
3735
Joe Onorato9c1289c2009-08-17 11:03:03 -04003736 workspace.requestLayout();
3737
Daniel Sandler843e8602010-06-07 14:59:01 -04003738 if (DEBUG_WIDGETS) {
3739 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
3740 + (SystemClock.uptimeMillis()-start) + "ms");
3741 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003742 }
3743
Adam Cohen1462de32012-07-24 22:34:36 -07003744 public void onPageBoundSynchronously(int page) {
3745 mSynchronouslyBoundPages.add(page);
3746 }
3747
Joe Onorato9c1289c2009-08-17 11:03:03 -04003748 /**
3749 * Callback saying that there aren't any more items to bind.
3750 *
3751 * Implementation of the method from LauncherModel.Callbacks.
3752 */
Adam Cohene25af792013-06-06 23:08:25 -07003753 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07003754 Runnable r = new Runnable() {
3755 public void run() {
3756 finishBindingItems(upgradePath);
3757 }
3758 };
3759 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003760 return;
3761 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003762 if (mSavedState != null) {
3763 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07003764 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003765 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003766 mSavedState = null;
3767 }
3768
Winson Chung98ca0f72013-07-29 12:58:51 -07003769 // Create the custom content page here before onLayout to prevent flashing
3770 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
3771 mWorkspace.createCustomContentPage();
3772 }
3773
Adam Cohen1462de32012-07-24 22:34:36 -07003774 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003775
Patrick Dubroy002cbf42011-03-03 16:36:21 -08003776 // If we received the result of any pending adds while the loader was running (e.g. the
3777 // widget configuration forced an orientation change), process them now.
3778 for (int i = 0; i < sPendingAddList.size(); i++) {
3779 completeAdd(sPendingAddList.get(i));
3780 }
3781 sPendingAddList.clear();
Joe Onorato9c1289c2009-08-17 11:03:03 -04003782
Winson Chungc51db6a2011-10-05 11:44:49 -07003783 // Update the market app icon as necessary (the other icons will be managed in response to
3784 // package changes in bindSearchablesChanged()
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003785 updateAppMarketIcon();
Michael Jurkac1f5d262011-09-30 19:32:27 -07003786
Winson Chungf0c6ae02012-03-21 16:10:31 -07003787 mWorkspaceLoading = false;
Adam Cohene25af792013-06-06 23:08:25 -07003788 if (upgradePath) {
Adam Cohena0b57492013-06-14 15:33:35 -07003789 mWorkspace.stripDuplicateApps();
3790 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.stripDuplicateApps();
Adam Cohene25af792013-06-06 23:08:25 -07003791 }
Adam Cohen99894d92013-06-14 11:22:59 -07003792
Adam Cohen66a01fd2013-06-11 12:48:00 -07003793 mWorkspace.post(new Runnable() {
3794 @Override
3795 public void run() {
3796 onFinishBindingItems();
3797 }
3798 });
Winson Chungf0c6ae02012-03-21 16:10:31 -07003799 }
3800
Winson Chunga2413752012-04-03 14:22:34 -07003801 private boolean canRunNewAppsAnimation() {
3802 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
3803 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
3804 }
3805
Winson Chungc9168342013-06-26 14:54:55 -07003806 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
3807 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
3808 PropertyValuesHolder.ofFloat("alpha", 1f),
3809 PropertyValuesHolder.ofFloat("scaleX", 1f),
3810 PropertyValuesHolder.ofFloat("scaleY", 1f));
3811 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
3812 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
3813 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
3814 return bounceAnim;
3815 }
3816
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003817 @Override
3818 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003819 boolean searchVisible = updateGlobalSearchIcon();
3820 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07003821 if (mSearchDropTargetBar != null) {
3822 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
3823 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01003824 }
3825
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003826 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003827 * Add the icons for all apps.
3828 *
3829 * Implementation of the method from LauncherModel.Callbacks.
3830 */
Michael Jurkac57b7a82011-08-09 22:02:20 -07003831 public void bindAllApplications(final ArrayList<ApplicationInfo> apps) {
Winson Chung64359a52013-07-08 17:17:08 -07003832 if (mIntentsOnWorkspaceFromUpgradePath != null) {
Daniel Sandler8707e0f2013-08-15 15:54:18 -07003833 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
3834 getHotseat().addAllAppsFolder(mIconCache, apps,
3835 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
3836 }
Winson Chung64359a52013-07-08 17:17:08 -07003837 mIntentsOnWorkspaceFromUpgradePath = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07003838 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003839 }
3840
3841 /**
3842 * A package was updated.
3843 *
3844 * Implementation of the method from LauncherModel.Callbacks.
3845 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003846 public void bindAppsUpdated(final ArrayList<ApplicationInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003847 Runnable r = new Runnable() {
3848 public void run() {
3849 bindAppsUpdated(apps);
3850 }
3851 };
3852 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003853 return;
3854 }
3855
Patrick Dubroyf5afda72011-02-28 12:04:18 -08003856 if (mWorkspace != null) {
3857 mWorkspace.updateShortcuts(apps);
3858 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003859 }
3860
3861 /**
Winson Chung83892cc2013-05-01 16:53:33 -07003862 * A package was uninstalled. We take both the super set of packageNames
3863 * in addition to specific applications to remove, the reason being that
3864 * this can be called when a package is updated as well. In that scenario,
3865 * we only remove specific components from the workspace, where as
3866 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04003867 *
3868 * Implementation of the method from LauncherModel.Callbacks.
3869 */
Winson Chung83892cc2013-05-01 16:53:33 -07003870 public void bindComponentsRemoved(final ArrayList<String> packageNames,
3871 final ArrayList<ApplicationInfo> appInfos,
Winson Chung64359a52013-07-08 17:17:08 -07003872 final boolean packageRemoved) {
Winson Chungd64d1762013-08-20 14:37:16 -07003873 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003874 public void run() {
Winson Chung64359a52013-07-08 17:17:08 -07003875 bindComponentsRemoved(packageNames, appInfos, packageRemoved);
Winson Chung83892cc2013-05-01 16:53:33 -07003876 }
Winson Chungd64d1762013-08-20 14:37:16 -07003877 };
3878 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07003879 return;
3880 }
3881
Winson Chung64359a52013-07-08 17:17:08 -07003882 if (packageRemoved) {
Winson Chung83892cc2013-05-01 16:53:33 -07003883 mWorkspace.removeItemsByPackageName(packageNames);
3884 } else {
3885 mWorkspace.removeItemsByApplicationInfo(appInfos);
Joe Onorato36115782010-06-17 13:28:48 -04003886 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003887
Winson Chunga1820962011-10-03 16:31:06 -07003888 // Notify the drag controller
Winson Chung83892cc2013-05-01 16:53:33 -07003889 mDragController.onAppsRemoved(appInfos, this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003890 }
Joe Onoratobe386092009-11-17 17:32:16 -08003891
3892 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07003893 * A number of packages were updated.
3894 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003895 private ArrayList<Object> mWidgetsAndShortcuts;
3896 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07003897 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02003898 bindPackagesUpdated(mWidgetsAndShortcuts);
3899 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07003900 }
Michael Jurkac402cd92013-05-20 15:49:32 +02003901 };
3902
3903 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
3904 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
3905 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07003906 return;
3907 }
3908
Winson Chung64359a52013-07-08 17:17:08 -07003909 // Update the widgets pane
Winson Chung785d2eb2011-04-14 16:08:02 -07003910 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003911 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07003912 }
Winson Chung80baf5a2010-08-09 16:03:15 -07003913 }
3914
Winson Chung400438b2011-01-16 17:53:48 -08003915 private int mapConfigurationOriActivityInfoOri(int configOri) {
3916 final Display d = getWindowManager().getDefaultDisplay();
3917 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
3918 switch (d.getRotation()) {
3919 case Surface.ROTATION_0:
3920 case Surface.ROTATION_180:
3921 // We are currently in the same basic orientation as the natural orientation
3922 naturalOri = configOri;
3923 break;
3924 case Surface.ROTATION_90:
3925 case Surface.ROTATION_270:
3926 // We are currently in the other basic orientation to the natural orientation
3927 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
3928 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
3929 break;
3930 }
3931
3932 int[] oriMap = {
3933 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
3934 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
3935 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
3936 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
3937 };
3938 // Since the map starts at portrait, we need to offset if this device's natural orientation
3939 // is landscape.
3940 int indexOffset = 0;
3941 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
3942 indexOffset = 1;
3943 }
3944 return oriMap[(d.getRotation() + indexOffset) % 4];
3945 }
Adam Cohen446e9402011-09-15 18:21:21 -07003946
Winson Chung4b919f82012-05-01 10:44:08 -07003947 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08003948 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07003949 getResources().getBoolean(R.bool.allow_rotation);
3950 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08003951 }
Winson Chung4b919f82012-05-01 10:44:08 -07003952 public void lockScreenOrientation() {
3953 if (isRotationEnabled()) {
3954 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
3955 .getConfiguration().orientation));
3956 }
3957 }
3958 public void unlockScreenOrientation(boolean immediate) {
3959 if (isRotationEnabled()) {
3960 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07003961 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07003962 } else {
3963 mHandler.postDelayed(new Runnable() {
3964 public void run() {
3965 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
3966 }
3967 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07003968 }
Winson Chung4b919f82012-05-01 10:44:08 -07003969 }
Winson Chung400438b2011-01-16 17:53:48 -08003970 }
3971
Winson Chung82f55532011-08-09 14:14:23 -07003972 /* Cling related */
Winson Chung7d7541e2011-09-16 20:14:36 -07003973 private boolean isClingsEnabled() {
Adam Cohen7564d982013-06-06 23:11:58 -07003974 if (DISABLE_CLINGS) {
3975 return false;
3976 }
3977
Brett Chabot2a9e2282011-08-23 15:03:13 -07003978 // disable clings when running in a test harness
Winson Chung7d7541e2011-09-16 20:14:36 -07003979 if(ActivityManager.isRunningInTestHarness()) return false;
Brett Chabot2a9e2282011-08-23 15:03:13 -07003980
Michael Jurkae233a8b2013-03-19 13:49:20 +01003981 // Restricted secondary users (child mode) will potentially have very few apps
3982 // seeded when they start up for the first time. Clings won't work well with that
Daniel Sandler325dc232013-06-05 22:57:57 -04003983// boolean supportsLimitedUsers =
3984// android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.JELLY_BEAN_MR2;
3985// Account[] accounts = AccountManager.get(this).getAccounts();
3986// if (supportsLimitedUsers && accounts.length == 0) {
3987// UserManager um = (UserManager) getSystemService(Context.USER_SERVICE);
3988// Bundle restrictions = um.getUserRestrictions();
3989// if (restrictions.getBoolean(UserManager.DISALLOW_MODIFY_ACCOUNTS, false)) {
3990// return false;
3991// }
3992// }
Winson Chung7d7541e2011-09-16 20:14:36 -07003993 return true;
Winson Chung82f55532011-08-09 14:14:23 -07003994 }
Michael Jurka22143132012-10-04 17:42:38 +02003995
Winson Chung7d7541e2011-09-16 20:14:36 -07003996 private Cling initCling(int clingId, int[] positionData, boolean animate, int delay) {
Michael Jurka22143132012-10-04 17:42:38 +02003997 final Cling cling = (Cling) findViewById(clingId);
Winson Chung7d7541e2011-09-16 20:14:36 -07003998 if (cling != null) {
3999 cling.init(this, positionData);
4000 cling.setVisibility(View.VISIBLE);
4001 cling.setLayerType(View.LAYER_TYPE_HARDWARE, null);
4002 if (animate) {
4003 cling.buildLayer();
4004 cling.setAlpha(0f);
4005 cling.animate()
4006 .alpha(1f)
Winson Chung7a74ac92011-09-20 17:43:51 -07004007 .setInterpolator(new AccelerateInterpolator())
Winson Chung7d7541e2011-09-16 20:14:36 -07004008 .setDuration(SHOW_CLING_DURATION)
4009 .setStartDelay(delay)
4010 .start();
4011 } else {
4012 cling.setAlpha(1f);
4013 }
Michael Jurka22143132012-10-04 17:42:38 +02004014 cling.setFocusableInTouchMode(true);
4015 cling.post(new Runnable() {
4016 public void run() {
4017 cling.setFocusable(true);
4018 cling.requestFocus();
4019 }
4020 });
4021 mHideFromAccessibilityHelper.setImportantForAccessibilityToNo(
4022 mDragLayer, clingId == R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004023 }
4024 return cling;
4025 }
Michael Jurka22143132012-10-04 17:42:38 +02004026
Winson Chung7d7541e2011-09-16 20:14:36 -07004027 private void dismissCling(final Cling cling, final String flag, int duration) {
Winson Chung7819abd2012-11-29 14:29:38 -08004028 // To catch cases where siblings of top-level views are made invisible, just check whether
4029 // the cling is directly set to GONE before dismissing it.
4030 if (cling != null && cling.getVisibility() != View.GONE) {
Michael Jurka2ecf9952012-06-18 12:52:28 -07004031 ObjectAnimator anim = LauncherAnimUtils.ofFloat(cling, "alpha", 0f);
Winson Chung7d7541e2011-09-16 20:14:36 -07004032 anim.setDuration(duration);
Winson Chung82f55532011-08-09 14:14:23 -07004033 anim.addListener(new AnimatorListenerAdapter() {
4034 public void onAnimationEnd(Animator animation) {
4035 cling.setVisibility(View.GONE);
4036 cling.cleanup();
Winson Chung46353de2012-02-16 14:05:10 -08004037 // We should update the shared preferences on a background thread
4038 new Thread("dismissClingThread") {
4039 public void run() {
4040 SharedPreferences.Editor editor = mSharedPrefs.edit();
4041 editor.putBoolean(flag, true);
4042 editor.commit();
4043 }
4044 }.start();
Winson Chung82f55532011-08-09 14:14:23 -07004045 };
4046 });
4047 anim.start();
Michael Jurka22143132012-10-04 17:42:38 +02004048 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung82f55532011-08-09 14:14:23 -07004049 }
4050 }
Michael Jurka22143132012-10-04 17:42:38 +02004051
Winson Chung9d9d74f2011-09-19 11:49:12 -07004052 private void removeCling(int id) {
4053 final View cling = findViewById(id);
4054 if (cling != null) {
4055 final ViewGroup parent = (ViewGroup) cling.getParent();
4056 parent.post(new Runnable() {
4057 @Override
4058 public void run() {
4059 parent.removeView(cling);
4060 }
4061 });
Michael Jurka22143132012-10-04 17:42:38 +02004062 mHideFromAccessibilityHelper.restoreImportantForAccessibility(mDragLayer);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004063 }
4064 }
Michael Jurka974c3862012-05-22 22:00:31 -07004065
4066 private boolean skipCustomClingIfNoAccounts() {
4067 Cling cling = (Cling) findViewById(R.id.workspace_cling);
4068 boolean customCling = cling.getDrawIdentifier().equals("workspace_custom");
4069 if (customCling) {
4070 AccountManager am = AccountManager.get(this);
Daniel Sandler325dc232013-06-05 22:57:57 -04004071 if (am == null) return false;
Michael Jurka974c3862012-05-22 22:00:31 -07004072 Account[] accounts = am.getAccountsByType("com.google");
4073 return accounts.length == 0;
4074 }
4075 return false;
4076 }
4077
Winson Chung7d7541e2011-09-16 20:14:36 -07004078 public void showFirstRunWorkspaceCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004079 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004080 if (isClingsEnabled() &&
Michael Jurka974c3862012-05-22 22:00:31 -07004081 !mSharedPrefs.getBoolean(Cling.WORKSPACE_CLING_DISMISSED_KEY, false) &&
4082 !skipCustomClingIfNoAccounts() ) {
Michael Jurka45355c42012-10-08 13:21:35 +02004083 // If we're not using the default workspace layout, replace workspace cling
4084 // with a custom workspace cling (usually specified in an overlay)
4085 // For now, only do this on tablets
4086 if (mSharedPrefs.getInt(LauncherProvider.DEFAULT_WORKSPACE_RESOURCE_ID, 0) != 0 &&
Michael Jurkaa1131212012-10-09 14:46:26 +02004087 getResources().getBoolean(R.bool.config_useCustomClings)) {
Michael Jurka45355c42012-10-08 13:21:35 +02004088 // Use a custom cling
4089 View cling = findViewById(R.id.workspace_cling);
4090 ViewGroup clingParent = (ViewGroup) cling.getParent();
4091 int clingIndex = clingParent.indexOfChild(cling);
4092 clingParent.removeViewAt(clingIndex);
4093 View customCling = mInflater.inflate(R.layout.custom_workspace_cling, clingParent, false);
4094 clingParent.addView(customCling, clingIndex);
4095 customCling.setId(R.id.workspace_cling);
4096 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004097 initCling(R.id.workspace_cling, null, false, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004098 } else {
4099 removeCling(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004100 }
4101 }
4102 public void showFirstRunAllAppsCling(int[] position) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004103 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004104 if (isClingsEnabled() &&
4105 !mSharedPrefs.getBoolean(Cling.ALLAPPS_CLING_DISMISSED_KEY, false)) {
Winson Chung7d7541e2011-09-16 20:14:36 -07004106 initCling(R.id.all_apps_cling, position, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004107 } else {
4108 removeCling(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004109 }
4110 }
4111 public Cling showFirstRunFoldersCling() {
Winson Chung7d7541e2011-09-16 20:14:36 -07004112 // Enable the clings only if they have not been dismissed before
Winson Chung46353de2012-02-16 14:05:10 -08004113 if (isClingsEnabled() &&
4114 !mSharedPrefs.getBoolean(Cling.FOLDER_CLING_DISMISSED_KEY, false)) {
4115 return initCling(R.id.folder_cling, null, true, 0);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004116 } else {
4117 removeCling(R.id.folder_cling);
Winson Chung46353de2012-02-16 14:05:10 -08004118 return null;
Winson Chung7d7541e2011-09-16 20:14:36 -07004119 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004120 }
Michael Jurka104c4562013-07-08 18:03:46 -07004121 protected SharedPreferences getSharedPrefs() {
4122 return mSharedPrefs;
4123 }
Winson Chung7d7541e2011-09-16 20:14:36 -07004124 public boolean isFolderClingVisible() {
4125 Cling cling = (Cling) findViewById(R.id.folder_cling);
Winson Chung9d9d74f2011-09-19 11:49:12 -07004126 if (cling != null) {
4127 return cling.getVisibility() == View.VISIBLE;
4128 }
4129 return false;
Winson Chung7d7541e2011-09-16 20:14:36 -07004130 }
Winson Chung82f55532011-08-09 14:14:23 -07004131 public void dismissWorkspaceCling(View v) {
4132 Cling cling = (Cling) findViewById(R.id.workspace_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004133 dismissCling(cling, Cling.WORKSPACE_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004134 }
4135 public void dismissAllAppsCling(View v) {
4136 Cling cling = (Cling) findViewById(R.id.all_apps_cling);
Winson Chung7d7541e2011-09-16 20:14:36 -07004137 dismissCling(cling, Cling.ALLAPPS_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
4138 }
4139 public void dismissFolderCling(View v) {
4140 Cling cling = (Cling) findViewById(R.id.folder_cling);
4141 dismissCling(cling, Cling.FOLDER_CLING_DISMISSED_KEY, DISMISS_CLING_DURATION);
Winson Chung82f55532011-08-09 14:14:23 -07004142 }
4143
Winson Chung80baf5a2010-08-09 16:03:15 -07004144 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004145 * Prints out out state for debugging.
4146 */
4147 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004148 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004149 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004150 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4151 Log.d(TAG, "mRestoring=" + mRestoring);
4152 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4153 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004154 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004155 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004156
Winson Chung785d2eb2011-04-14 16:08:02 -07004157 if (mAppsCustomizeContent != null) {
4158 mAppsCustomizeContent.dumpState();
4159 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004160 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004161 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004162
4163 @Override
4164 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4165 super.dump(prefix, fd, writer, args);
4166 writer.println(" ");
4167 writer.println("Debug logs: ");
4168 for (int i = 0; i < sDumpLogs.size(); i++) {
4169 writer.println(" " + sDumpLogs.get(i));
4170 }
4171 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004172
4173 public static void dumpDebugLogsToConsole() {
4174 Log.d(TAG, "");
4175 Log.d(TAG, "*********************");
4176 Log.d(TAG, "Launcher debug logs: ");
4177 for (int i = 0; i < sDumpLogs.size(); i++) {
4178 Log.d(TAG, " " + sDumpLogs.get(i));
4179 }
4180 Log.d(TAG, "*********************");
4181 Log.d(TAG, "");
4182 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004183}
Michael Jurka2763be32011-02-24 11:19:57 -08004184
Michael Jurkaabded662011-03-04 12:06:57 -08004185interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08004186 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07004187 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08004188 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08004189 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08004190 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08004191}