blob: d3a436237a3fc9b9ecf20b24771c778d18299eda [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
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Winson Chung70442722012-02-10 15:43:22 -080026import android.animation.ValueAnimator.AnimatorUpdateListener;
Adam Cohen0b395352014-06-09 22:54:36 +000027import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070029import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070030import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
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;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Winson Chung5f8afe62013-08-12 16:19:28 -070056import android.graphics.Point;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
Patrick Dubroy4ed62782010-08-17 15:11:18 -070068import android.provider.Settings;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080069import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070072import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070074import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070075import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Display;
77import android.view.Gravity;
78import android.view.HapticFeedbackConstants;
79import android.view.KeyEvent;
80import android.view.LayoutInflater;
81import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.MotionEvent;
83import android.view.Surface;
84import android.view.View;
Adam Cohen6c5891a2014-07-09 23:53:15 -070085import android.view.ViewAnimationUtils;
Adam Cohen0b395352014-06-09 22:54:36 +000086import android.view.Window;
Adam Cohenf358a4b2013-07-23 16:47:31 -070087import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080088import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.ViewGroup;
90import android.view.ViewTreeObserver;
Michael Jurka2a4b1a82011-12-07 14:00:02 -080091import android.view.ViewTreeObserver.OnGlobalLayoutListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080093import android.view.accessibility.AccessibilityEvent;
Adam Cohencff6af82011-09-13 14:51:53 -070094import android.view.animation.AccelerateDecelerateInterpolator;
Adam Cohen6c5891a2014-07-09 23:53:15 -070095import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080096import android.view.animation.DecelerateInterpolator;
Adam Cohen6c5891a2014-07-09 23:53:15 -070097import android.view.animation.LinearInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080098import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070099import android.widget.Advanceable;
100import android.widget.FrameLayout;
101import android.widget.ImageView;
102import android.widget.TextView;
103import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700104
Kenny Guyed131872014-04-30 03:02:21 +0100105import com.android.launcher3.compat.LauncherActivityInfoCompat;
106import com.android.launcher3.compat.LauncherAppsCompat;
107import com.android.launcher3.compat.UserHandleCompat;
108import com.android.launcher3.compat.UserManagerCompat;
Daniel Sandler325dc232013-06-05 22:57:57 -0400109import com.android.launcher3.DropTarget.DragObject;
Anjali Koppalf5d29132014-02-28 13:33:27 -0800110import com.android.launcher3.PagedView.PageSwitchListener;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700111
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.io.DataInputStream;
113import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700115import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700119import java.io.PrintWriter;
Adam Cohen0b395352014-06-09 22:54:36 +0000120import java.lang.reflect.Field;
121import java.lang.reflect.InvocationTargetException;
122import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700125import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700126import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700128import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000129import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130
Winson Chunga6945242014-01-08 14:04:34 -0800131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132/**
133 * Default launcher application.
134 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100135public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700136 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700137 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800138 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700139 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Daniel Sandlerf061f822013-06-27 13:59:36 -0400141 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400142 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700143 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400144 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700145 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700146
Dan Sandlerd5024042014-01-09 15:01:33 -0500147 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
148
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700150 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700153 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Michael Jurka8b805b12012-04-18 14:23:14 -0700155 private static final int REQUEST_BIND_APPWIDGET = 11;
156
Mathew Inwood876a8462013-06-14 14:12:41 +0100157 /**
158 * IntentStarter uses request codes starting with this. This must be greater than all activity
159 * request codes used internally.
160 */
161 protected static final int REQUEST_LAST = 100;
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
164
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800165 static final int SCREEN_COUNT = 5;
166 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Romain Guy98d01652009-06-30 16:21:04 -0700168 private static final String PREFERENCES = "launcher.preferences";
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 // To turn on these properties, type
170 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
171 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
172 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800173 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Winson Chung2672ff92012-05-04 16:22:30 -0700175 // The Intent extra that defines whether to ignore the launch animation
176 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400177 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 // Type: int
180 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700181 // Type: int
182 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700184 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
185 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
187 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700188 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700190 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 // Type: boolean
192 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
193 // Type: long
194 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700195 // Type: int
196 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
197 // Type: int
198 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
199 // Type: parcelable
200 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000201 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800202 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000203 // Type: int[]
204 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Adam Cohen432609a2014-03-13 17:03:22 -0700206 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800207 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
208
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100209 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700210 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100211 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700212 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100213 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700214
Adam Cohen39a06042013-07-19 14:30:12 -0700215 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700216 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700217
Winson Chunga6945242014-01-08 14:04:34 -0800218 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
219
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700220 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700221 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700222 private State mState = State.WORKSPACE;
223 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700224
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700225 private boolean mIsSafeModeEnabled;
226
Joe Onorato9c1289c2009-08-17 11:03:03 -0400227 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700228 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
229 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700230 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800231
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800233 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000235 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
236 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
237
Winson Chunga2413752012-04-03 14:22:34 -0700238 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700239 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
240 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700241 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700242
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800243 private final BroadcastReceiver mCloseSystemDialogsReceiver
244 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800245 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
246
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 private LayoutInflater mInflater;
248
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800249 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700250 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800251 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800252 private DragLayer mDragLayer;
253 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700254 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800255 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700256
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700257 private AppWidgetManager mAppWidgetManager;
258 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700259
Michael Jurkac9d95c52011-08-29 14:03:34 -0700260 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700261 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800262 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700263
Michael Jurka0280c3b2010-09-17 15:00:07 -0700264 private int[] mTmpAddItemCellCoordinates = new int[2];
265
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800266 private FolderInfo mFolderInfo;
267
Winson Chung3d503fb2011-07-13 17:25:49 -0700268 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800269 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700270
Michael Jurka838a4ca2011-02-07 13:33:06 -0800271 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700272
Winson Chungc51db6a2011-10-05 11:44:49 -0700273 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700274 private AppsCustomizeTabHost mAppsCustomizeTabHost;
275 private AppsCustomizePagedView mAppsCustomizeContent;
276 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800277 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800278
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700280 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
281 // scroll issues (because the workspace may not have been measured yet) and extra work.
282 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
283 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284
285 private SpannableStringBuilder mDefaultKeySsb = null;
286
Joe Onorato9c1289c2009-08-17 11:03:03 -0400287 private boolean mWorkspaceLoading = true;
288
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800289 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290 private boolean mRestoring;
291 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700292 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293
Michael Jurka1e2f4652013-07-08 18:03:46 -0700294 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700295 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
296
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 private Bundle mSavedInstanceState;
298
Joe Onorato9c1289c2009-08-17 11:03:03 -0400299 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800300 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800301 private boolean mUserPresent = true;
302 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700303 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800304 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700306 private static LocaleConfiguration sLocaleConfiguration = null;
307
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700308 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700309
Adam Cohen61f560d2013-09-30 15:58:20 -0700310 private View.OnTouchListener mHapticFeedbackTouchListener;
311
Adam Cohended9f8d2010-11-03 13:25:16 -0700312 // Related to the auto-advancing of widgets
313 private final int ADVANCE_MSG = 1;
314 private final int mAdvanceInterval = 20000;
315 private final int mAdvanceStagger = 250;
316 private long mAutoAdvanceSentTime;
317 private long mAutoAdvanceTimeLeft = -1;
318 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
319 new HashMap<View, AppWidgetProviderInfo>();
320
Winson Chung400438b2011-01-16 17:53:48 -0800321 // Determines how long to wait after a rotation before restoring the screen orientation to
322 // match the sensor state.
323 private final int mRestoreScreenOrientationDelay = 500;
324
Michael Jurka4ef207b2010-11-29 17:05:45 -0800325 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700326 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
327 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
328 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800329
Winson Chungd64a6662013-09-30 11:06:59 -0700330 private Intent mAppMarketIntent = null;
331 private static final boolean DISABLE_MARKET_BUTTON = true;
332
Craig Mautner360310b2012-10-26 15:13:08 -0700333 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700334
Adam Cohen1462de32012-07-24 22:34:36 -0700335 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700336 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700337
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700338 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700339 static Date sDateStamp = new Date();
340 static DateFormat sDateFormat =
341 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
342 static long sRunStart = System.currentTimeMillis();
343 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700344
Winson Chung46353de2012-02-16 14:05:10 -0800345 // We only want to get the SharedPreferences once since it does an FS stat each time we get
346 // it from the context.
347 private SharedPreferences mSharedPrefs;
348
Adam Cohene25af792013-06-06 23:08:25 -0700349 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
350
Winson Chungf0c6ae02012-03-21 16:10:31 -0700351 // Holds the page that we need to animate to, and the icon views that we need to animate up
352 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700353 private ImageView mFolderIconImageView;
354 private Bitmap mFolderIconBitmap;
355 private Canvas mFolderIconCanvas;
356 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700357
Michael Jurkaddd62e92011-02-16 17:49:14 -0800358 private BubbleTextView mWaitingForResume;
359
Michael Jurkac1f5d262011-09-30 19:32:27 -0700360 private Runnable mBuildLayersRunnable = new Runnable() {
361 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700362 if (mWorkspace != null) {
363 mWorkspace.buildPageHardwareLayers();
364 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700365 }
366 };
367
Adam Cohendb364c32014-05-20 14:23:40 -0700368 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800369
Michael Jurka7267fa52013-09-26 15:29:57 -0700370 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800371
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800372 private static class PendingAddArguments {
373 int requestCode;
374 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700375 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700376 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800377 int cellX;
378 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700379 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800380 }
381
Daniel Sandlerff02d492013-08-05 02:12:05 -0400382 private Stats mStats;
383
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800384 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800385 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700386 }
387
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800388 @Override
389 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700390 if (DEBUG_STRICT_MODE) {
391 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
392 .detectDiskReads()
393 .detectDiskWrites()
394 .detectNetwork() // or .detectAll() for all detectable problems
395 .penaltyLog()
396 .build());
397 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
398 .detectLeakedSqlLiteObjects()
399 .detectLeakedClosableObjects()
400 .penaltyLog()
401 .penaltyDeath()
402 .build());
403 }
404
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800405 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400406
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400407 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400408 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700409 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700410 // Determine the dynamic grid properties
411 Point smallestSize = new Point();
412 Point largestSize = new Point();
413 Point realSize = new Point();
414 Display display = getWindowManager().getDefaultDisplay();
415 display.getCurrentSizeRange(smallestSize, largestSize);
416 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700417 DisplayMetrics dm = new DisplayMetrics();
418 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700419
Winson Chung5f8afe62013-08-12 16:19:28 -0700420 // Lazy-initialize the dynamic grid
421 DeviceProfile grid = app.initDynamicGrid(this,
422 Math.min(smallestSize.x, smallestSize.y),
423 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700424 realSize.x, realSize.y,
425 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700426
427 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400428 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700429 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700430 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800431 mModel = app.setLauncher(this);
432 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700433 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400434 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800435 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700437
Daniel Sandlerff02d492013-08-05 02:12:05 -0400438 mStats = new Stats(this);
439
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700440 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700441
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700442 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
443 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700444
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700445 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
446 // this also ensures that any synchronous binding below doesn't re-trigger another
447 // LauncherModel load.
448 mPaused = false;
449
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200451 android.os.Debug.startMethodTracing(
452 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 }
454
Adam Cohen53805212013-10-01 10:39:23 -0700455
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700458
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800459 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100460 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800462 registerContentObservers();
463
Joe Onorato7c312c12009-08-13 21:36:53 -0700464 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700465
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 mSavedState = savedInstanceState;
467 restoreState(mSavedState);
468
469 if (PROFILE_STARTUP) {
470 android.os.Debug.stopMethodTracing();
471 }
472
473 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700474 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700475 // If the user leaves launcher, then we should just load items asynchronously when
476 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500477 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 } else {
479 // We only load the page synchronously if the user rotates (or triggers a
480 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800481 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700482 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800483 }
484
485 // For handling default keys
486 mDefaultKeySsb = new SpannableStringBuilder();
487 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800488
489 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
490 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800491
Adam Cohenf9426d52012-06-04 17:26:21 -0700492 updateGlobalIcons();
493
494 // On large interfaces, we want the screen to auto-rotate based on the current orientation
495 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700496
Adam Cohen432609a2014-03-13 17:03:22 -0700497 if (shouldShowIntroScreen()) {
498 showIntroScreen();
499 } else {
500 showFirstRunActivity();
501 }
502
Winson Chunge43a1e72014-01-15 10:33:02 -0800503 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
504 // on the device, then we always show the first run cling experience (or if there is no
505 // launcher2). Otherwise, we prompt the user upon started for migration
Winson Chunge43a1e72014-01-15 10:33:02 -0800506 if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
507 if (mModel.canMigrateFromOldLauncherDb(this)) {
508 mLauncherClings.showMigrationCling();
509 } else {
510 mLauncherClings.showFirstRunCling();
511 }
Winson Chunga6945242014-01-08 14:04:34 -0800512 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800513 mLauncherClings.removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800514 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700515 }
516
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700517 @Override
518 public void onLauncherProviderChange() { }
519
Winson Chung98ca0f72013-07-29 12:58:51 -0700520 /** To be overriden by subclasses to hint to Launcher that we have custom content */
521 protected boolean hasCustomContentToLeft() {
522 return false;
523 }
524
Dave Hawkeya8881582013-09-17 15:55:33 -0600525 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500526 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600527 * {@link #addToCustomContentPage}. This will only be invoked if
528 * {@link #hasCustomContentToLeft()} is {@code true}.
529 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500530 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600531 }
532
533 /**
534 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
535 * ensure the custom content page is added or removed if necessary.
536 */
537 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600538 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600539 // Not bound yet, wait for bindScreens to be called.
540 return;
541 }
542
543 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
544 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500545 mWorkspace.createCustomContentContainer();
546 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600547 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
548 mWorkspace.removeCustomContentPage();
549 }
550 }
551
Adam Cohenf9426d52012-06-04 17:26:21 -0700552 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700553 boolean searchVisible = false;
554 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800555 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700556 int coi = getCurrentOrientationIndexForGlobalIcons();
557 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
558 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700559 if (!DISABLE_MARKET_BUTTON) {
560 updateAppMarketIcon();
561 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700562 searchVisible = updateGlobalSearchIcon();
563 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700564 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700565 if (sGlobalSearchIcon[coi] != null) {
566 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700567 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700568 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700569 if (sVoiceSearchIcon[coi] != null) {
570 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700571 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700572 }
Winson Chungd64a6662013-09-30 11:06:59 -0700573 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700574 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800575 }
Winson Chungadf0c182012-08-23 14:59:07 -0700576 if (mSearchDropTargetBar != null) {
577 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
578 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800579 }
Romain Guycbb89e42009-06-08 15:52:54 -0700580
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800581 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700582 if (sLocaleConfiguration == null) {
583 new AsyncTask<Void, Void, LocaleConfiguration>() {
584 @Override
585 protected LocaleConfiguration doInBackground(Void... unused) {
586 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
587 readConfiguration(Launcher.this, localeConfiguration);
588 return localeConfiguration;
589 }
590
591 @Override
592 protected void onPostExecute(LocaleConfiguration result) {
593 sLocaleConfiguration = result;
594 checkForLocaleChange(); // recursive, but now with a locale configuration
595 }
596 }.execute();
597 return;
598 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700599
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 final Configuration configuration = getResources().getConfiguration();
601
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700602 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 final String locale = configuration.locale.toString();
604
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700605 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 final int mcc = configuration.mcc;
607
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700608 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609 final int mnc = configuration.mnc;
610
Romain Guy84f296c2009-11-04 15:00:44 -0800611 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612
Romain Guy84f296c2009-11-04 15:00:44 -0800613 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700614 sLocaleConfiguration.locale = locale;
615 sLocaleConfiguration.mcc = mcc;
616 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700617
Joe Onorato0589f0f2010-02-08 13:44:00 -0800618 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700619
620 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100621 new AsyncTask<Void, Void, Void>() {
622 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700623 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100624 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700625 }
Michael Jurka43467462013-11-14 12:03:58 +0100626 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700627 }
628 }
629
630 private static class LocaleConfiguration {
631 public String locale;
632 public int mcc = -1;
633 public int mnc = -1;
634 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700635
Romain Guy98d01652009-06-30 16:21:04 -0700636 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
637 DataInputStream in = null;
638 try {
639 in = new DataInputStream(context.openFileInput(PREFERENCES));
640 configuration.locale = in.readUTF();
641 configuration.mcc = in.readInt();
642 configuration.mnc = in.readInt();
643 } catch (FileNotFoundException e) {
644 // Ignore
645 } catch (IOException e) {
646 // Ignore
647 } finally {
648 if (in != null) {
649 try {
650 in.close();
651 } catch (IOException e) {
652 // Ignore
653 }
654 }
655 }
656 }
657
658 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
659 DataOutputStream out = null;
660 try {
661 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
662 out.writeUTF(configuration.locale);
663 out.writeInt(configuration.mcc);
664 out.writeInt(configuration.mnc);
665 out.flush();
666 } catch (FileNotFoundException e) {
667 // Ignore
668 } catch (IOException e) {
669 //noinspection ResultOfMethodCallIgnored
670 context.getFileStreamPath(PREFERENCES).delete();
671 } finally {
672 if (out != null) {
673 try {
674 out.close();
675 } catch (IOException e) {
676 // Ignore
677 }
678 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800679 }
680 }
681
Anton Hanssona2f665f2013-09-26 12:18:32 +0100682 public Stats getStats() {
683 return mStats;
684 }
685
Bjorn Bringertc459e522013-06-07 19:36:01 +0100686 public LayoutInflater getInflater() {
687 return mInflater;
688 }
689
Winson Chung36a62fe2012-05-06 18:04:42 -0700690 boolean isDraggingEnabled() {
691 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
692 // that is subsequently removed from the workspace in startBinding().
693 return !mModel.isLoadingWorkspace();
694 }
695
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800696 static int getScreen() {
697 synchronized (sLock) {
698 return sScreen;
699 }
700 }
701
702 static void setScreen(int screen) {
703 synchronized (sLock) {
704 sScreen = screen;
705 }
706 }
707
Winson Chung557d6ed2011-07-08 15:34:52 -0700708 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000709 * Copied from View -- the View version of the method isn't called
710 * anywhere else in our process and only exists for API level 17+,
711 * so it's ok to keep our own version with no API requirement.
712 */
713 public static int generateViewId() {
714 for (;;) {
715 final int result = sNextGeneratedId.get();
716 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
717 int newValue = result + 1;
718 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
719 if (sNextGeneratedId.compareAndSet(result, newValue)) {
720 return result;
721 }
722 }
723 }
724
725 public int getViewIdForItem(ItemInfo info) {
726 // This cast is safe given the > 2B range for int.
727 int itemId = (int) info.id;
728 if (mItemIdToViewId.containsKey(itemId)) {
729 return mItemIdToViewId.get(itemId);
730 }
731 int viewId = generateViewId();
732 mItemIdToViewId.put(itemId, viewId);
733 return viewId;
734 }
735
736 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700737 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
738 * a configuration step, this allows the proper animations to run after other transitions.
739 */
Adam Cohendb364c32014-05-20 14:23:40 -0700740 private long completeAdd(PendingAddArguments args) {
741
742 long screenId = ensurePendingDropLayoutExists(args.screenId);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800743 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800744 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700745 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700746 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700747 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800748 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700749 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700750 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800751 }
Michael Jurka27614d22012-04-02 06:40:22 -0700752 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
753 // if you turned the screen off and then back while in All Apps, Launcher would not
754 // return to the workspace. Clearing mAddInfo.container here fixes this issue
755 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700756 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800757 }
758
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800759 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700760 protected void onActivityResult(
761 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700762 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700763 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700764 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800765 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700766
Adam Cohenad4e15c2013-10-17 16:21:35 -0700767 Runnable exitSpringLoaded = new Runnable() {
768 @Override
769 public void run() {
770 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
771 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
772 }
773 };
774
Michael Jurka8b805b12012-04-18 14:23:14 -0700775 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700776 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700777 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
778 if (resultCode == RESULT_CANCELED) {
779 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700780 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700782 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700783 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
784 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700785 }
786 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200787 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
788 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
789 mWorkspace.exitOverviewMode(false);
790 }
791 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700792 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200793
Adam Cohened66b2b2012-01-23 17:28:51 -0800794 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
795 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700796
Adam Cohendb364c32014-05-20 14:23:40 -0700797 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800798 // We have special handling for widgets
799 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800800 final int appWidgetId;
801 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
802 : -1;
803 if (widgetId < 0) {
804 appWidgetId = pendingAddWidgetId;
805 } else {
806 appWidgetId = widgetId;
807 }
808
Adam Cohenad4e15c2013-10-17 16:21:35 -0700809 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800810 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700811 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
812 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700813 result = RESULT_CANCELED;
814 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700815 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700816 @Override
817 public void run() {
818 exitSpringLoadedDragModeDelayed(false, 0, null);
819 }
820 };
Adam Cohendb364c32014-05-20 14:23:40 -0700821 if (workspaceLocked) {
822 // No need to remove the empty screen if we're mid-binding, as the
823 // the bind will not add the empty screen.
824 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
825 } else {
826 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
827 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
828 }
Winson Chung5aaab772012-04-27 15:24:02 -0700829 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700830 if (!workspaceLocked) {
831 mPendingAddInfo.screenId = ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
832 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
833
834 dropLayout.setDropPending(true);
835 final Runnable onComplete = new Runnable() {
836 @Override
837 public void run() {
838 completeTwoStageWidgetDrop(resultCode, appWidgetId);
839 dropLayout.setDropPending(false);
840 }
841 };
842 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
843 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
844 } else {
845 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
846 mPendingAddInfo);
847 sPendingAddItem = args;
848 }
Winson Chung5aaab772012-04-27 15:24:02 -0700849 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800850 return;
851 }
852
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 // The pattern used here is that a user PICKs a specific application,
854 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700855
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
857 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700858 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700859 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
860 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800861 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700862 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800863 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700864 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700865 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
866 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 }
Adam Cohen00074722013-10-11 17:46:09 -0700868 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700869 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700870 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800872 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800873 }
874
Adam Cohendb364c32014-05-20 14:23:40 -0700875 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
876 appWidgetId, ItemInfo info) {
877 PendingAddArguments args = new PendingAddArguments();
878 args.requestCode = requestCode;
879 args.intent = data;
880 args.container = info.container;
881 args.screenId = info.screenId;
882 args.cellX = info.cellX;
883 args.cellY = info.cellY;
884 args.appWidgetId = appWidgetId;
885 return args;
886 }
887
888 /**
889 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
890 *
891 * @param screenId the screen id to check
892 * @return the new screen, or screenId if it exists
893 */
894 private long ensurePendingDropLayoutExists(long screenId) {
895 CellLayout dropLayout =
896 (CellLayout) mWorkspace.getScreenWithId(screenId);
897 if (dropLayout == null) {
898 // it's possible that the add screen was removed because it was
899 // empty and a re-bind occurred
900 mWorkspace.addExtraEmptyScreen();
901 return mWorkspace.commitExtraEmptyScreen();
902 } else {
903 return screenId;
904 }
905 }
906
Adam Cohened66b2b2012-01-23 17:28:51 -0800907 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700908 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700909 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800910 Runnable onCompleteRunnable = null;
911 int animationType = 0;
912
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700913 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 if (resultCode == RESULT_OK) {
915 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
916 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700917 mPendingAddWidgetInfo);
918 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800919 onCompleteRunnable = new Runnable() {
920 @Override
921 public void run() {
922 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700923 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700924 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
925 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 }
927 };
928 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800929 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800930 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800931 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700932 if (mDragLayer.getAnimatedView() != null) {
933 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
934 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
935 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700936 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700937 // The animated view may be null in the case of a rotation during widget configuration
938 onCompleteRunnable.run();
939 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800940 }
941
942 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700943 protected void onStop() {
944 super.onStop();
945 FirstFrameAnimatorHelper.setIsVisible(false);
946 }
947
948 @Override
949 protected void onStart() {
950 super.onStart();
951 FirstFrameAnimatorHelper.setIsVisible(true);
952 }
953
954 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800955 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200956 long startTime = 0;
957 if (DEBUG_RESUME_TIME) {
958 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400959 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200960 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800961 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700962
Winson Chung4a2afa32012-07-19 14:53:05 -0700963 // Restore the previous launcher state
964 if (mOnResumeState == State.WORKSPACE) {
965 showWorkspace(false);
966 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c62013-11-06 15:49:51 -0800967 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700968 }
969 mOnResumeState = State.NONE;
970
Craig Mautner360310b2012-10-26 15:13:08 -0700971 // Background was set to gradient in onPause(), restore to black if in all apps.
972 setWorkspaceBackground(mState == State.WORKSPACE);
973
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800974 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700975 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700976 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500977 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400978 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700979 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800980 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700981 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200982 // We might have postponed some bind calls until onResume (see waitUntilResume) --
983 // execute them here
984 long startTimeCallbacks = 0;
985 if (DEBUG_RESUME_TIME) {
986 startTimeCallbacks = System.currentTimeMillis();
987 }
988
989 if (mAppsCustomizeContent != null) {
990 mAppsCustomizeContent.setBulkBind(true);
991 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700992 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
993 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200994 }
995 if (mAppsCustomizeContent != null) {
996 mAppsCustomizeContent.setBulkBind(false);
997 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700998 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200999 if (DEBUG_RESUME_TIME) {
1000 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1001 (System.currentTimeMillis() - startTimeCallbacks));
1002 }
Michael Jurka447bf842013-05-15 14:52:15 +02001003 }
Michael Jurka54554252013-08-01 12:52:23 +02001004 if (mOnResumeCallbacks.size() > 0) {
1005 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1006 mOnResumeCallbacks.get(i).run();
1007 }
1008 mOnResumeCallbacks.clear();
1009 }
Winson Chunge4e50662012-01-23 14:45:13 -08001010
1011 // Reset the pressed state of icons that were locked in the press state while activities
1012 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001013 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001014 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001015 mWaitingForResume.setStayPressed(false);
1016 }
Winson Chunge4e50662012-01-23 14:45:13 -08001017 if (mAppsCustomizeContent != null) {
1018 // Resets the previous all apps icon press state
1019 mAppsCustomizeContent.resetDrawableState();
1020 }
Winson Chung82963d52013-10-09 11:20:57 -07001021
Adam Cohen06dff352012-06-01 17:17:08 -07001022 // It is possible that widgets can receive updates while launcher is not in the foreground.
1023 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1024 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1025 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001026 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001027
Winson Chung780fe592013-09-26 14:48:44 -07001028 // Process any items that were added while Launcher was away.
1029 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1030
Winson Chung5841efa2013-09-30 18:06:44 -07001031 // Update the voice search button proxy
1032 updateVoiceButtonProxyVisible(false);
1033
Adam Cohenf9426d52012-06-04 17:26:21 -07001034 // Again, as with the above scenario, it's possible that one or more of the global icons
1035 // were updated in the wrong orientation.
1036 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001037 if (DEBUG_RESUME_TIME) {
1038 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1039 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001040
1041 if (mWorkspace.getCustomContentCallbacks() != null) {
1042 // If we are resuming and the custom content is the current page, we call onShow().
1043 // It is also poassible that onShow will instead be called slightly after first layout
1044 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1045 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001046 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001047 }
1048 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001049 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001050 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001051 }
1052
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001053 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001054 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001055 // Ensure that items added to Launcher are queued until Launcher returns
1056 InstallShortcutReceiver.enableInstallQueue();
1057
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001058 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001059 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001060 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001061 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001062
1063 // We call onHide() aggressively. The custom content callbacks should be able to
1064 // debounce excess onHide calls.
1065 if (mWorkspace.getCustomContentCallbacks() != null) {
1066 mWorkspace.getCustomContentCallbacks().onHide();
1067 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001068 }
Romain Guycbb89e42009-06-08 15:52:54 -07001069
Adam Cohenbffe7452013-07-22 18:21:45 -07001070 QSBScroller mQsbScroller = new QSBScroller() {
1071 int scrollY = 0;
1072
1073 @Override
1074 public void setScrollY(int scroll) {
1075 scrollY = scroll;
1076
1077 if (mWorkspace.isOnOrMovingToCustomContent()) {
1078 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001079 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001080 }
1081 }
1082 };
1083
1084 public void resetQSBScroll() {
1085 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001086 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001087 }
1088
1089 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001090 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1091 // by a onResume or by scrolling otherwise.
1092 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001093
1094 // Custom content is completely hidden
1095 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001096
1097 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1098 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001099
1100 // Indicates whether the user is allowed to scroll away from the custom content.
1101 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001102 }
1103
Jorim Jaggid017f882014-01-14 17:08:48 -08001104 protected boolean hasSettings() {
1105 return false;
1106 }
1107
Adam Cohenbffe7452013-07-22 18:21:45 -07001108 public interface QSBScroller {
1109 public void setScrollY(int scrollY);
1110 }
1111
Winson Chung98ca0f72013-07-29 12:58:51 -07001112 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001113 CustomContentCallbacks callbacks, String description) {
1114 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001115 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001116 }
1117
Adam Cohenedb40762013-07-18 16:45:45 -07001118 // The custom content needs to offset its content to account for the QSB
1119 public int getTopOffsetForCustomContent() {
1120 return mWorkspace.getPaddingTop();
1121 }
1122
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001123 @Override
1124 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001125 // Flag the loader to stop early before switching
1126 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001127 if (mAppsCustomizeContent != null) {
1128 mAppsCustomizeContent.surrender();
1129 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001130 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001131 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001132
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001133 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001134 @Override
1135 public void onWindowFocusChanged(boolean hasFocus) {
1136 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001137 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001138 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001139
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001140 private boolean acceptFilter() {
1141 final InputMethodManager inputManager = (InputMethodManager)
1142 getSystemService(Context.INPUT_METHOD_SERVICE);
1143 return !inputManager.isFullscreenMode();
1144 }
1145
1146 @Override
1147 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001148 final int uniChar = event.getUnicodeChar();
1149 final boolean handled = super.onKeyDown(keyCode, event);
1150 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1151 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001152 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1153 keyCode, event);
1154 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001155 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001156 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001157 // showSearchDialog()
1158 // If there are multiple keystrokes before the search dialog takes focus,
1159 // onSearchRequested() will be called for every keystroke,
1160 // but it is idempotent, so it's fine.
1161 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001162 }
1163 }
1164
Joe Onorato8a9625e2010-01-28 15:55:35 -08001165 // Eat the long press event so the keyboard doesn't come up.
1166 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1167 return true;
1168 }
1169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001170 return handled;
1171 }
1172
Karl Rosaen138a0412009-04-23 19:00:21 -07001173 private String getTypedText() {
1174 return mDefaultKeySsb.toString();
1175 }
1176
1177 private void clearTypedText() {
1178 mDefaultKeySsb.clear();
1179 mDefaultKeySsb.clearSpans();
1180 Selection.setSelection(mDefaultKeySsb, 0);
1181 }
1182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001183 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001184 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1185 * State
1186 */
1187 private static State intToState(int stateOrdinal) {
1188 State state = State.WORKSPACE;
1189 final State[] stateValues = State.values();
1190 for (int i = 0; i < stateValues.length; i++) {
1191 if (stateValues[i].ordinal() == stateOrdinal) {
1192 state = stateValues[i];
1193 break;
1194 }
1195 }
1196 return state;
1197 }
1198
1199 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 * Restores the previous state, if it exists.
1201 *
1202 * @param savedState The previous state.
1203 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001204 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001205 private void restoreState(Bundle savedState) {
1206 if (savedState == null) {
1207 return;
1208 }
1209
Michael Jurka883f55b2010-10-21 15:47:14 -07001210 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001211 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001212 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001213 }
1214
Adam Cohen21cd0022013-10-09 18:57:02 -07001215 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1216 PagedView.INVALID_RESTORE_PAGE);
1217 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001218 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001219 }
1220
Winson Chung3d503fb2011-07-13 17:25:49 -07001221 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001222 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001223
Winson Chung3d503fb2011-07-13 17:25:49 -07001224 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1225 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001226 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001227 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1228 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001229 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1230 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1231 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001232 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001233 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001234 mRestoring = true;
1235 }
1236
1237 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1238 if (renameFolder) {
1239 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001240 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001241 mRestoring = true;
1242 }
Winson Chunga12a2502010-12-20 14:41:35 -08001243
Winson Chung785d2eb2011-04-14 16:08:02 -07001244 // Restore the AppsCustomize tab
1245 if (mAppsCustomizeTabHost != null) {
1246 String curTab = savedState.getString("apps_customize_currentTab");
1247 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001248 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001249 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001250 mAppsCustomizeContent.loadAssociatedPages(
1251 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001252 }
1253
Winson Chung5afbf7b2011-07-25 11:53:08 -07001254 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1255 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001256 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001257 mItemIdToViewId = (HashMap<Integer, Integer>)
1258 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001259 }
1260
1261 /**
1262 * Finds all the views we need and configure them properly.
1263 */
1264 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001265 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001266
Craig Mautner360310b2012-10-26 15:13:08 -07001267 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001268 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1269 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001270 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001271 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001272
John Spurlock77e1f472013-09-11 10:09:51 -04001273 mLauncherView.setSystemUiVisibility(
1274 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001275 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001276
Winson Chung4c98d922011-05-31 16:50:48 -07001277 // Setup the drag layer
1278 mDragLayer.setup(this, dragController);
1279
Winson Chung3d503fb2011-07-13 17:25:49 -07001280 // Setup the hotseat
1281 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1282 if (mHotseat != null) {
1283 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001284 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001285 }
1286
Jorim Jaggid017f882014-01-14 17:08:48 -08001287 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001288 View widgetButton = findViewById(R.id.widget_button);
1289 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001290 @Override
1291 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001292 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001293 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001294 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001295 }
1296 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001297 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1298
1299 View wallpaperButton = findViewById(R.id.wallpaper_button);
1300 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001301 @Override
1302 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001303 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001304 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001305 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001306 }
1307 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001308 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1309
1310 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001311 if (hasSettings()) {
1312 settingsButton.setOnClickListener(new OnClickListener() {
1313 @Override
1314 public void onClick(View arg0) {
1315 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001316 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001317 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001318 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001319 });
1320 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1321 } else {
1322 settingsButton.setVisibility(View.GONE);
1323 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1324 lp.gravity = Gravity.END | Gravity.TOP;
1325 widgetButton.requestLayout();
1326 }
1327
Adam Cohendbdff6b2013-09-18 19:09:15 -07001328 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001329
Winson Chung4c98d922011-05-31 16:50:48 -07001330 // Setup the workspace
1331 mWorkspace.setHapticFeedbackEnabled(false);
1332 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001333 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001334 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001335
Winson Chungf0ea4d32011-06-06 14:27:16 -07001336 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001337 mSearchDropTargetBar = (SearchDropTargetBar)
1338 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001339
Winson Chungf0ea4d32011-06-06 14:27:16 -07001340 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001341 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001342 mAppsCustomizeContent = (AppsCustomizePagedView)
1343 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1344 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001345
Winson Chung3d503fb2011-07-13 17:25:49 -07001346 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001347 dragController.setDragScoller(mWorkspace);
1348 dragController.setScrollView(mDragLayer);
1349 dragController.setMoveTarget(mWorkspace);
1350 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001351 if (mSearchDropTargetBar != null) {
1352 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001353 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001354
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001355 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001356 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001357 mWeightWatcher = new WeightWatcher(this);
1358 mWeightWatcher.setAlpha(0.5f);
1359 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001360 new FrameLayout.LayoutParams(
1361 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001362 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001363 Gravity.BOTTOM)
1364 );
Adam Cohen39a06042013-07-19 14:30:12 -07001365
1366 boolean show = shouldShowWeightWatcher();
1367 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001368 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 }
1370
1371 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001372 * Sets the all apps button. This method is called from {@link Hotseat}.
1373 */
1374 public void setAllAppsButton(View allAppsButton) {
1375 mAllAppsButton = allAppsButton;
1376 }
1377
1378 public View getAllAppsButton() {
1379 return mAllAppsButton;
1380 }
1381
1382 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001383 * Creates a view representing a shortcut.
1384 *
1385 * @param info The data structure describing the shortcut.
1386 *
1387 * @return A View inflated from R.layout.application.
1388 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001389 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001390 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001391 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001392 }
1393
1394 /**
1395 * Creates a view representing a shortcut inflated from the specified resource.
1396 *
1397 * @param layoutResId The id of the XML layout used to create the shortcut.
1398 * @param parent The group the shortcut belongs to.
1399 * @param info The data structure describing the shortcut.
1400 *
1401 * @return A View inflated from layoutResId.
1402 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001403 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001404 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001405 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001406 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001407 return favorite;
1408 }
1409
1410 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001411 * Add a shortcut to the workspace.
1412 *
1413 * @param data The intent describing the shortcut.
1414 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001416 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001417 int cellY) {
1418 int[] cellXY = mTmpAddItemCellCoordinates;
1419 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001420 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001421
Michael Jurkad3ef3062010-11-23 16:23:58 -08001422 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001423
Adam Cohen558baaf2011-08-15 15:22:57 -07001424 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001425 if (info == null) {
1426 return;
1427 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001428 final View view = createShortcut(info);
1429
Adam Cohenfbba09b2011-07-18 21:43:05 -07001430 // First we check if we already know the exact location where we want to add this item.
1431 if (cellX >= 0 && cellY >= 0) {
1432 cellXY[0] = cellX;
1433 cellXY[1] = cellY;
1434 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001435
1436 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001437 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001438 true, null,null)) {
1439 return;
1440 }
1441 DragObject dragObject = new DragObject();
1442 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001443 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1444 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001445 return;
1446 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001447 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001448 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001449 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001450 foundCellSpan = (result != null);
1451 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001452 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001453 }
1454
1455 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001456 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001457 return;
1458 }
1459
Adam Cohendcd297f2013-06-18 13:13:40 -07001460 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461
1462 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001463 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001464 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 }
1466 }
1467
Adam Cohen2f093b62012-04-30 18:59:53 -07001468 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1469 int minHeight) {
1470 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001471 // We want to account for the extra amount of padding that we are adding to the widget
1472 // to ensure that it gets the full amount of space that it has requested
1473 int requiredWidth = minWidth + padding.left + padding.right;
1474 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001475 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001476 }
1477
Adam Cohen2f093b62012-04-30 18:59:53 -07001478 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1479 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001480 }
1481
Adam Cohen2f093b62012-04-30 18:59:53 -07001482 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1483 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001484 }
1485
Adam Cohen2f093b62012-04-30 18:59:53 -07001486 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1487 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001488 }
1489
Adam Cohen2f093b62012-04-30 18:59:53 -07001490 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1491 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001492 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001493 }
1494
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001496 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001498 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001499 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001500 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001501 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001502 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1503 if (appWidgetInfo == null) {
1504 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1505 }
Romain Guycbb89e42009-06-08 15:52:54 -07001506
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001507 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001508 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001509
Adam Cohen2f093b62012-04-30 18:59:53 -07001510 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1511 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001512
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001514 // We have saved the position to which the widget was dragged-- this really only matters
1515 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001516 int[] cellXY = mTmpAddItemCellCoordinates;
1517 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001518 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001519 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001520 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1521 cellXY[0] = mPendingAddInfo.cellX;
1522 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001523 spanXY[0] = mPendingAddInfo.spanX;
1524 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001525 foundCellSpan = true;
1526 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001527 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001528 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001529 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1530 spanXY[1], cellXY, finalSpan);
1531 spanXY[0] = finalSpan[0];
1532 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001533 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001534 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001535 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001536 }
1537
Michael Jurka0280c3b2010-09-17 15:00:07 -07001538 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001539 if (appWidgetId != -1) {
1540 // Deleting an app widget ID is a void call but writes to disk before returning
1541 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001542 new AsyncTask<Void, Void, Void>() {
1543 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001544 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001545 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001546 }
Michael Jurka43467462013-11-14 12:03:58 +01001547 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001548 }
Winson Chung93eef082012-03-23 15:59:27 -07001549 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001550 return;
1551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001553 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001554 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1555 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001556 launcherInfo.spanX = spanXY[0];
1557 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001558 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1559 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001560
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001561 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001562 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563
1564 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001565 if (hostView == null) {
1566 // Perform actual inflation because we're live
1567 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1568 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1569 } else {
1570 // The AppWidgetHostView has already been inflated and instantiated
1571 launcherInfo.hostView = hostView;
1572 }
Romain Guycbb89e42009-06-08 15:52:54 -07001573
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001574 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001575 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001576 launcherInfo.notifyWidgetSizeChanged(this);
1577
Adam Cohendcd297f2013-06-18 13:13:40 -07001578 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001579 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001580
1581 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001583 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
Romain Guycbb89e42009-06-08 15:52:54 -07001585
Adam Cohended9f8d2010-11-03 13:25:16 -07001586 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1587 @Override
1588 public void onReceive(Context context, Intent intent) {
1589 final String action = intent.getAction();
1590 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1591 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001592 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001593 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001594
Winson Chungc100e8e2011-08-09 16:02:43 -07001595 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001596 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001597 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001598 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001599 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001600 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1601 mUserPresent = true;
1602 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001603 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1604 mModel.resetLoadedState(false, true);
1605 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1606 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1607 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1608 mModel.resetLoadedState(false, true);
1609 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1610 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1611 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001612 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1613 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1614 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001615 }
1616 }
1617 };
1618
1619 @Override
1620 public void onAttachedToWindow() {
1621 super.onAttachedToWindow();
1622
1623 // Listen for broadcasts related to user-presence
1624 final IntentFilter filter = new IntentFilter();
1625 filter.addAction(Intent.ACTION_SCREEN_OFF);
1626 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001627 // For handling managed profiles
1628 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1629 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001630 if (ENABLE_DEBUG_INTENTS) {
1631 filter.addAction(DebugIntents.DELETE_DATABASE);
1632 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1633 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001634 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001635 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001636 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001637 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001638 mVisible = true;
1639 }
1640
Adam Cohen0b395352014-06-09 22:54:36 +00001641 /**
1642 * Sets up transparent navigation and status bars in LMP.
1643 * This method is a no-op for other platform versions.
1644 */
1645 @TargetApi(19)
1646 private void setupTransparentSystemBarsForLmp() {
1647 // TODO(sansid): use the APIs directly when compiling against L sdk.
1648 // Currently we use reflection to access the flags and the API to set the transparency
1649 // on the System bars.
1650 if (Utilities.isLmp()) {
1651 try {
1652 getWindow().getAttributes().systemUiVisibility |=
1653 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1654 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1655 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1656 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1657 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1658 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1659 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1660 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1661
1662 Method setStatusBarColorMethod =
1663 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1664 Method setNavigationBarColorMethod =
1665 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1666 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1667 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1668 } catch (NoSuchFieldException e) {
1669 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1670 } catch (NoSuchMethodException ex) {
1671 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1672 } catch (IllegalAccessException e) {
1673 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1674 } catch (IllegalArgumentException e) {
1675 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1676 } catch (InvocationTargetException e) {
1677 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1678 } finally {}
1679 }
1680 }
1681
Adam Cohended9f8d2010-11-03 13:25:16 -07001682 @Override
1683 public void onDetachedFromWindow() {
1684 super.onDetachedFromWindow();
1685 mVisible = false;
1686
Adam Cohend113e0c2010-11-11 10:48:05 -08001687 if (mAttached) {
1688 unregisterReceiver(mReceiver);
1689 mAttached = false;
1690 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001691 updateRunning();
1692 }
1693
1694 public void onWindowVisibilityChanged(int visibility) {
1695 mVisible = visibility == View.VISIBLE;
1696 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001697 // The following code used to be in onResume, but it turns out onResume is called when
1698 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1699 // is a more appropriate event to handle
1700 if (mVisible) {
1701 mAppsCustomizeTabHost.onWindowVisible();
1702 if (!mWorkspaceLoading) {
1703 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001704 // We want to let Launcher draw itself at least once before we force it to build
1705 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001706 // apps is nice and speedy.
1707 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001708 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001709 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001710 if (mStarted) return;
1711 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001712 // We delay the layer building a bit in order to give
1713 // other message processing a time to run. In particular
1714 // this avoids a delay in hiding the IME if it was
1715 // currently shown, because doing that may involve
1716 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001717 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001718 final ViewTreeObserver.OnDrawListener listener = this;
1719 mWorkspace.post(new Runnable() {
1720 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001721 if (mWorkspace != null &&
1722 mWorkspace.getViewTreeObserver() != null) {
1723 mWorkspace.getViewTreeObserver().
1724 removeOnDrawListener(listener);
1725 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001726 }
1727 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001728 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001729 }
1730 });
1731 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001732 // When Launcher comes back to foreground, a different Activity might be responsible for
1733 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001734 if (!DISABLE_MARKET_BUTTON) {
1735 updateAppMarketIcon();
1736 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001737 clearTypedText();
1738 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 }
1740
1741 private void sendAdvanceMessage(long delay) {
1742 mHandler.removeMessages(ADVANCE_MSG);
1743 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1744 mHandler.sendMessageDelayed(msg, delay);
1745 mAutoAdvanceSentTime = System.currentTimeMillis();
1746 }
1747
1748 private void updateRunning() {
1749 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1750 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1751 mAutoAdvanceRunning = autoAdvanceRunning;
1752 if (autoAdvanceRunning) {
1753 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1754 sendAdvanceMessage(delay);
1755 } else {
1756 if (!mWidgetsToAdvance.isEmpty()) {
1757 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1758 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1759 }
1760 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001761 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 }
1763 }
1764 }
1765
1766 private final Handler mHandler = new Handler() {
1767 @Override
1768 public void handleMessage(Message msg) {
1769 if (msg.what == ADVANCE_MSG) {
1770 int i = 0;
1771 for (View key: mWidgetsToAdvance.keySet()) {
1772 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1773 final int delay = mAdvanceStagger * i;
1774 if (v instanceof Advanceable) {
1775 postDelayed(new Runnable() {
1776 public void run() {
1777 ((Advanceable) v).advance();
1778 }
1779 }, delay);
1780 }
1781 i++;
1782 }
1783 sendAdvanceMessage(mAdvanceInterval);
1784 }
1785 }
1786 };
1787
1788 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001789 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001790 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1791 if (v instanceof Advanceable) {
1792 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001793 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001794 updateRunning();
1795 }
1796 }
1797
1798 void removeWidgetToAutoAdvance(View hostView) {
1799 if (mWidgetsToAdvance.containsKey(hostView)) {
1800 mWidgetsToAdvance.remove(hostView);
1801 updateRunning();
1802 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001803 }
1804
Joe Onorato9c1289c2009-08-17 11:03:03 -04001805 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001806 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001807 launcherInfo.hostView = null;
1808 }
1809
Winson Chung93eef082012-03-23 15:59:27 -07001810 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1811 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1812 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001813 }
1814
Winson Chunga6945242014-01-08 14:04:34 -08001815 public DragLayer getDragLayer() {
1816 return mDragLayer;
1817 }
1818
1819 public Workspace getWorkspace() {
1820 return mWorkspace;
1821 }
1822
1823 public Hotseat getHotseat() {
1824 return mHotseat;
1825 }
1826
Jorim Jaggid017f882014-01-14 17:08:48 -08001827 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001828 return mOverviewPanel;
1829 }
1830
1831 public SearchDropTargetBar getSearchBar() {
1832 return mSearchDropTargetBar;
1833 }
1834
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001835 public LauncherAppWidgetHost getAppWidgetHost() {
1836 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001837 }
Romain Guycbb89e42009-06-08 15:52:54 -07001838
Winson Chunga9abd0e2010-10-27 17:18:37 -07001839 public LauncherModel getModel() {
1840 return mModel;
1841 }
1842
Winson Chunga6945242014-01-08 14:04:34 -08001843 public LauncherClings getLauncherClings() {
1844 return mLauncherClings;
1845 }
1846
1847 protected SharedPreferences getSharedPrefs() {
1848 return mSharedPrefs;
1849 }
1850
Bjorn Bringertc459e522013-06-07 19:36:01 +01001851 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001852 getWindow().closeAllPanels();
1853
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001854 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001855 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001856 }
1857
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001858 @Override
1859 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001860 long startTime = 0;
1861 if (DEBUG_RESUME_TIME) {
1862 startTime = System.currentTimeMillis();
1863 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001864 super.onNewIntent(intent);
1865
1866 // Close the menu
1867 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001868 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001869 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001870
Adam Cohened307df2013-10-02 09:37:31 -07001871 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1872 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1873 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001874
Adam Cohen6fecd412013-10-02 17:41:50 -07001875 if (mWorkspace == null) {
1876 // Can be cases where mWorkspace is null, this prevents a NPE
1877 return;
1878 }
1879 Folder openFolder = mWorkspace.getOpenFolder();
1880 // In all these cases, only animate if we're already on home
1881 mWorkspace.exitWidgetResizeMode();
1882 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001883 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001884 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001885 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001886
Adam Cohen6fecd412013-10-02 17:41:50 -07001887 closeFolder();
1888 exitSpringLoadedDragMode();
1889
1890 // If we are already on home, then just animate back to the workspace,
1891 // otherwise, just wait until onResume to set the state back to Workspace
1892 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001893 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001894 } else {
1895 mOnResumeState = State.WORKSPACE;
1896 }
1897
1898 final View v = getWindow().peekDecorView();
1899 if (v != null && v.getWindowToken() != null) {
1900 InputMethodManager imm = (InputMethodManager)getSystemService(
1901 INPUT_METHOD_SERVICE);
1902 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1903 }
1904
1905 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001906 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001907 mAppsCustomizeTabHost.reset();
1908 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001909
1910 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001911 }
Adam Cohened307df2013-10-02 09:37:31 -07001912
Michael Jurka447bf842013-05-15 14:52:15 +02001913 if (DEBUG_RESUME_TIME) {
1914 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1915 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001916 }
1917
Adam Coppa120b8e2013-11-12 16:26:04 +00001918 /**
1919 * Override point for subclasses to prevent movement to the default screen when the home
1920 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1921 */
1922 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1923 return true;
1924 }
1925
1926 /**
1927 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1928 */
1929 protected void onHomeIntent() {
1930 // Do nothing
1931 }
1932
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001933 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001934 public void onRestoreInstanceState(Bundle state) {
1935 super.onRestoreInstanceState(state);
1936 for (int page: mSynchronouslyBoundPages) {
1937 mWorkspace.restoreInstanceStateForChild(page);
1938 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001939 }
1940
1941 @Override
1942 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001943 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001944 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1945 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001946 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001947 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001948
Michael Jurka883f55b2010-10-21 15:47:14 -07001949 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001950 // We close any open folder since it will not be re-opened, and we need to make sure
1951 // this state is reflected.
1952 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001953
Adam Cohendcd297f2013-06-18 13:13:40 -07001954 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001955 mWaitingForResult) {
1956 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001957 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001958 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1959 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001960 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1961 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1962 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001963 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001964 }
1965
1966 if (mFolderInfo != null && mWaitingForResult) {
1967 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1968 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1969 }
Michael Jurka946ad472010-07-09 18:05:18 -07001970
Winson Chung785d2eb2011-04-14 16:08:02 -07001971 // Save the current AppsCustomize tab
1972 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08001973 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1974 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001975 if (currentTabTag != null) {
1976 outState.putString("apps_customize_currentTab", currentTabTag);
1977 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001978 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1979 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001980 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001981 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 }
1983
1984 @Override
1985 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001987
Winson Chunge7a03942011-08-05 15:05:12 -07001988 // Remove all pending runnables
1989 mHandler.removeMessages(ADVANCE_MSG);
1990 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001991 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001992
Winson Chungcd2b0142011-06-08 16:02:26 -07001993 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001994 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001995 mModel.stopLoader();
1996 app.setLauncher(null);
1997
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001998 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001999 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002001 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002002 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002003 mAppWidgetHost = null;
2004
2005 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006
2007 TextKeyListener.getInstance().release();
2008
Winson Chung81b52252012-08-27 15:34:29 -07002009 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2010 // to prevent leaking Launcher activities on orientation change.
2011 if (mModel != null) {
2012 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2013 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002014
2015 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002016 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002017
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002018 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002019 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002020 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002021 mWorkspace = null;
2022 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002023
Michael Jurka2ecf9952012-06-18 12:52:28 -07002024 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 }
2026
Adam Cohena9cf38f2011-05-02 15:36:58 -07002027 public DragController getDragController() {
2028 return mDragController;
2029 }
2030
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002031 @Override
2032 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002033 if (requestCode >= 0) {
2034 setWaitingForResult(true);
2035 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 super.startActivityForResult(intent, requestCode);
2037 }
2038
Winson Chung70d72102011-08-12 11:24:35 -07002039 /**
2040 * Indicates that we want global search for this activity by setting the globalSearch
2041 * argument for {@link #startSearch} to true.
2042 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002044 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002046
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002047 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002048
Karl Rosaen138a0412009-04-23 19:00:21 -07002049 if (initialQuery == null) {
2050 // Use any text typed in the launcher as the initial query
2051 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002052 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 if (appSearchData == null) {
2054 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002055 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056 }
Winson Chungadf0c182012-08-23 14:59:07 -07002057 Rect sourceBounds = new Rect();
2058 if (mSearchDropTargetBar != null) {
2059 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2060 }
Romain Guycbb89e42009-06-08 15:52:54 -07002061
Bjorn Bringertc459e522013-06-07 19:36:01 +01002062 startSearch(initialQuery, selectInitialQuery,
2063 appSearchData, sourceBounds);
2064 }
2065
2066 public void startSearch(String initialQuery,
2067 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002068 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002069 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002070 }
2071
2072 /**
2073 * Starts the global search activity. This code is a copied from SearchManager
2074 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002075 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002076 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002077 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002078 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2079 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2080 if (globalSearchActivity == null) {
2081 Log.w(TAG, "No global search activity found.");
2082 return;
2083 }
2084 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2085 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2086 intent.setComponent(globalSearchActivity);
2087 // Make sure that we have a Bundle to put source in
2088 if (appSearchData == null) {
2089 appSearchData = new Bundle();
2090 } else {
2091 appSearchData = new Bundle(appSearchData);
2092 }
2093 // Set source to package name of app that starts global search, if not set already.
2094 if (!appSearchData.containsKey("source")) {
2095 appSearchData.putString("source", getPackageName());
2096 }
2097 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2098 if (!TextUtils.isEmpty(initialQuery)) {
2099 intent.putExtra(SearchManager.QUERY, initialQuery);
2100 }
2101 if (selectInitialQuery) {
2102 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2103 }
2104 intent.setSourceBounds(sourceBounds);
2105 try {
2106 startActivity(intent);
2107 } catch (ActivityNotFoundException ex) {
2108 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2109 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 }
2111
Yura4f93ec62014-02-04 14:15:21 +00002112 public boolean isOnCustomContent() {
2113 return mWorkspace.isOnOrMovingToCustomContent();
2114 }
2115
Winson Chung70d72102011-08-12 11:24:35 -07002116 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002117 public boolean onPrepareOptionsMenu(Menu menu) {
2118 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002119 if (!isOnCustomContent()) {
2120 // Close any open folders
2121 closeFolder();
2122 // Stop resizing any widgets
2123 mWorkspace.exitWidgetResizeMode();
2124 if (!mWorkspace.isInOverviewMode()) {
2125 // Show the overview mode
2126 showOverviewMode(true);
2127 } else {
2128 showWorkspace(true);
2129 }
Winson Chunge0298742014-01-17 12:03:00 -08002130 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002131 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002132 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002133
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002134 @Override
2135 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002136 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002137 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002138 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002139 }
2140
Joe Onorato9c1289c2009-08-17 11:03:03 -04002141 public boolean isWorkspaceLocked() {
2142 return mWorkspaceLoading || mWaitingForResult;
2143 }
2144
Adam Cohen517a7f52014-03-01 12:12:59 -08002145 public boolean isWorkspaceLoading() {
2146 return mWorkspaceLoading;
2147 }
2148
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002149 private void setWorkspaceLoading(boolean value) {
2150 boolean isLocked = isWorkspaceLocked();
2151 mWorkspaceLoading = value;
2152 if (isLocked != isWorkspaceLocked()) {
2153 onWorkspaceLockedChanged();
2154 }
2155 }
2156
2157 private void setWaitingForResult(boolean value) {
2158 boolean isLocked = isWorkspaceLocked();
2159 mWaitingForResult = value;
2160 if (isLocked != isWorkspaceLocked()) {
2161 onWorkspaceLockedChanged();
2162 }
2163 }
2164
2165 protected void onWorkspaceLockedChanged() { }
2166
Michael Jurka0280c3b2010-09-17 15:00:07 -07002167 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002168 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002169 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002170 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2171 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002172 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002173 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002174 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002175
Adam Cohenad4e15c2013-10-17 16:21:35 -07002176 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2177 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2178 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2179 }
2180
2181 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2182 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2183 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002184 if (appWidgetInfo.configure != null) {
2185 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002186 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002187
Bjorn Bringert7984c942009-12-09 15:38:25 +00002188 // Launch over to configure widget, if needed
2189 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002190 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002191 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002192 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002193 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002194 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002195 Runnable onComplete = new Runnable() {
2196 @Override
2197 public void run() {
2198 // Exit spring loaded mode if necessary after adding the widget
2199 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2200 null);
2201 }
2202 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002203 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002204 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002205 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002206 }
2207 }
Romain Guycbb89e42009-06-08 15:52:54 -07002208
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002209 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002210 // Close any folders that may be open.
2211 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002212 mWorkspace.moveToCustomContentScreen(animate);
2213 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002214 /**
2215 * Process a shortcut drop.
2216 *
2217 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002218 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002219 * @param cell The cell it should be added to, optional
2220 * @param position The location on the screen where it was dropped, optional
2221 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002222 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002223 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002224 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002225 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002226 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002227 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002228
2229 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002230 mPendingAddInfo.cellX = cell[0];
2231 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002232 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002233
2234 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2235 createShortcutIntent.setComponent(componentName);
2236 processShortcut(createShortcutIntent);
2237 }
2238
Adam Cohenfbba09b2011-07-18 21:43:05 -07002239 /**
2240 * Process a widget drop.
2241 *
2242 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002243 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002244 * @param cell The cell it should be added to, optional
2245 * @param position The location on the screen where it was dropped, optional
2246 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002247 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002248 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002249 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002250 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002251 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002252 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002253 mPendingAddInfo.minSpanX = info.minSpanX;
2254 mPendingAddInfo.minSpanY = info.minSpanY;
2255
Adam Cohenfbba09b2011-07-18 21:43:05 -07002256 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002257 mPendingAddInfo.cellX = cell[0];
2258 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002259 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002260 if (span != null) {
2261 mPendingAddInfo.spanX = span[0];
2262 mPendingAddInfo.spanY = span[1];
2263 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002264
Adam Cohened66b2b2012-01-23 17:28:51 -08002265 AppWidgetHostView hostView = info.boundWidget;
2266 int appWidgetId;
2267 if (hostView != null) {
2268 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002269 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002270 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002271 // In this case, we either need to start an activity to get permission to bind
2272 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002273 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002274 Bundle options = info.bindOptions;
2275
2276 boolean success = false;
2277 if (options != null) {
2278 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2279 info.componentName, options);
2280 } else {
2281 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2282 info.componentName);
2283 }
2284 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002285 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002286 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002287 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002288 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2289 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2290 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002291 // TODO: we need to make sure that this accounts for the options bundle.
2292 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002293 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2294 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002295 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002296 }
2297
Joe Onoratodeb98af2010-02-19 14:59:39 -08002298 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002299 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002300 }
2301
Winson Chung24ab2f12010-09-16 14:10:47 -07002302 void processWallpaper(Intent intent) {
2303 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2304 }
2305
Adam Cohendcd297f2013-06-18 13:13:40 -07002306 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002307 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002308 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002309 folderInfo.title = getText(R.string.folder_name);
2310
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002311 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002312 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002313 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002314 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002315
2316 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002317 FolderIcon newFolder =
2318 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002319 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002320 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002321 // Force measure the new folder icon
2322 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2323 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002324 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002325 }
Romain Guycbb89e42009-06-08 15:52:54 -07002326
Joe Onorato9c1289c2009-08-17 11:03:03 -04002327 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002328 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002329 }
2330
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002331 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002332 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002333 }
2334
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002335 /**
2336 * Registers various content observers. The current implementation registers
2337 * only a favorites observer to keep track of the favorites applications.
2338 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002339 private void registerContentObservers() {
2340 ContentResolver resolver = getContentResolver();
2341 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2342 true, mWidgetObserver);
2343 }
2344
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002345 @Override
2346 public boolean dispatchKeyEvent(KeyEvent event) {
2347 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2348 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002349 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002350 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002351 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002352 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002353 dumpState();
2354 return true;
2355 }
2356 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002357 }
2358 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2359 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002360 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002361 return true;
2362 }
2363 }
2364
2365 return super.dispatchKeyEvent(event);
2366 }
2367
Joe Onorato88ec0992009-11-19 13:16:06 -08002368 @Override
2369 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002370 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002371 if (mAppsCustomizeContent.getContentType() ==
2372 AppsCustomizePagedView.ContentType.Applications) {
2373 showWorkspace(true);
2374 } else {
2375 showOverviewMode(true);
2376 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002377 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002378 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002379 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002380 Folder openFolder = mWorkspace.getOpenFolder();
2381 if (openFolder.isEditingName()) {
2382 openFolder.dismissEditingName();
2383 } else {
2384 closeFolder();
2385 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002386 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002387 mWorkspace.exitWidgetResizeMode();
2388
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002389 // Back button is a no-op here, but give at least some feedback for the button press
2390 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002391 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002392 }
2393
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002395 * Re-listen when widgets are reset.
2396 */
2397 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002398 if (mAppWidgetHost != null) {
2399 mAppWidgetHost.startListening();
2400 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002401 }
2402
2403 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 * Launches the intent referred by the clicked shortcut.
2405 *
2406 * @param v The view representing the clicked shortcut.
2407 */
2408 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002409 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2410 // view has detached (it's possible for this to happen if the view is removed mid touch).
2411 if (v.getWindowToken() == null) {
2412 return;
2413 }
2414
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002415 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002416 return;
2417 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002418
Adam Cohen1697b792013-09-17 19:08:21 -07002419 if (v instanceof Workspace) {
2420 if (mWorkspace.isInOverviewMode()) {
2421 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002422 }
2423 return;
2424 }
2425
2426 if (v instanceof CellLayout) {
2427 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002428 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002429 }
2430 }
2431
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002433 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002434 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002436 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002437 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002438 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002439 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002440 onClickAllAppsButton(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 }
2442 }
2443
Michael Jurka0e260592010-06-30 17:07:39 -07002444 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002445 return false;
2446 }
2447
Michael Jurkaaf442092010-06-10 17:01:57 -07002448 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002449 * Event handler for the search button
2450 *
2451 * @param v The view that was clicked.
2452 */
2453 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002454 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2455
Amith Yamasania135ba82011-08-09 17:42:01 -07002456 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002457 }
2458
2459 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002460 * Event handler for the voice button
2461 *
2462 * @param v The view that was clicked.
2463 */
2464 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002465 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002466
Bjorn Bringertc459e522013-06-07 19:36:01 +01002467 startVoice();
2468 }
2469
2470 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002471 try {
2472 final SearchManager searchManager =
2473 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2474 ComponentName activityName = searchManager.getGlobalSearchActivity();
2475 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002476 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002477 if (activityName != null) {
2478 intent.setPackage(activityName.getPackageName());
2479 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002480 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002481 } catch (ActivityNotFoundException e) {
2482 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002483 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002484 startActivitySafely(null, intent, "onClickVoiceButton");
2485 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002486 }
2487
2488 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002489 * Event handler for the "grid" button that appears on the home screen, which
2490 * enters all apps mode.
2491 *
2492 * @param v The view that was clicked.
2493 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002494 protected void onClickAllAppsButton(View v) {
2495 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2496 if (isAllAppsVisible()) {
2497 showWorkspace(true);
2498 } else {
2499 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2500 }
2501 }
2502
2503 /**
Mohammadinamul Sheik3b1a54a2014-03-18 11:28:19 -07002504 * Event handler for a paged view icon click.
2505 * @param v The view that was clicked.
2506 * @param appInfo The {link AppInfo} of the view.
2507 */
2508 public void onClickPagedViewIcon(View v, AppInfo appInfo) {
2509 if (LOGD) Log.d(TAG, "onClickPagedViewIcon");
2510 startActivitySafely(v, appInfo.intent, appInfo);
2511 getStats().recordLaunch(appInfo.intent);
2512 }
2513
2514 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002515 * Event handler for an app shortcut click.
2516 *
2517 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2518 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002519 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002520 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2521 Object tag = v.getTag();
2522 if (!(tag instanceof ShortcutInfo)) {
2523 throw new IllegalArgumentException("Input must be a Shortcut");
2524 }
2525
2526 // Open shortcut
2527 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2528 final Intent intent = shortcut.intent;
2529
2530 // Check for special shortcuts
2531 if (intent.getComponent() != null) {
2532 final String shortcutClass = intent.getComponent().getClassName();
2533
2534 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2535 MemoryDumpActivity.startDump(this);
2536 return;
2537 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2538 toggleShowWeightWatcher();
2539 return;
2540 }
2541 }
2542
Chris Wren40c5ed32014-06-24 18:24:23 -04002543 // Check for abandoned promise
2544 if (shortcut.isAbandoned() && v instanceof BubbleTextView) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002545 AlertDialog.Builder builder = new AlertDialog.Builder(this);
2546 builder.setTitle(R.string.abandoned_promises_title);
Chris Wren803a4be2014-07-01 16:52:49 -04002547 builder.setMessage(R.string.abandoned_promise_explanation);
Chris Wren40c5ed32014-06-24 18:24:23 -04002548 builder.setPositiveButton(R.string.abandoned_search,
2549 new DialogInterface.OnClickListener() {
2550 public void onClick(DialogInterface dialog, int id) {
2551 startAppShortcutActivity(v);
2552 }
2553 }
2554 );
Chris Wren40c5ed32014-06-24 18:24:23 -04002555 builder.setNeutralButton(R.string.abandoned_clean_this,
2556 new DialogInterface.OnClickListener() {
2557 public void onClick(DialogInterface dialog, int id) {
2558 final BubbleTextView bubble = (BubbleTextView) v;
2559 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2560 mWorkspace.removeAbandonedPromise(bubble, user);
2561 }
2562 });
2563 builder.create().show();
2564 return;
2565 }
2566
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002567 // Start activities
Chris Wren40c5ed32014-06-24 18:24:23 -04002568 startAppShortcutActivity(v);
2569 }
2570
2571 private void startAppShortcutActivity(View v) {
2572 Object tag = v.getTag();
2573 if (!(tag instanceof ShortcutInfo)) {
2574 throw new IllegalArgumentException("Input must be a Shortcut");
2575 }
2576 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2577 final Intent intent = shortcut.intent;
2578
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002579 int[] pos = new int[2];
2580 v.getLocationOnScreen(pos);
2581 intent.setSourceBounds(new Rect(pos[0], pos[1],
2582 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2583
2584 boolean success = startActivitySafely(v, intent, tag);
2585
2586 mStats.recordLaunch(intent, shortcut);
2587
2588 if (success && v instanceof BubbleTextView) {
2589 mWaitingForResume = (BubbleTextView) v;
2590 mWaitingForResume.setStayPressed(true);
2591 }
2592 }
2593
2594 /**
2595 * Event handler for a folder icon click.
2596 *
2597 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2598 */
2599 protected void onClickFolderIcon(View v) {
2600 if (LOGD) Log.d(TAG, "onClickFolder");
2601 if (!(v instanceof FolderIcon)){
2602 throw new IllegalArgumentException("Input must be a FolderIcon");
2603 }
2604
2605 FolderIcon folderIcon = (FolderIcon) v;
2606 final FolderInfo info = folderIcon.getFolderInfo();
2607 Folder openFolder = mWorkspace.getFolderForTag(info);
2608
2609 // If the folder info reports that the associated folder is open, then verify that
2610 // it is actually opened. There have been a few instances where this gets out of sync.
2611 if (info.opened && openFolder == null) {
2612 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2613 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2614 info.opened = false;
2615 }
2616
2617 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2618 // Close any open folder
2619 closeFolder();
2620 // Open the requested folder
2621 openFolder(folderIcon);
2622 } else {
2623 // Find the open folder...
2624 int folderScreen;
2625 if (openFolder != null) {
2626 folderScreen = mWorkspace.getPageForView(openFolder);
2627 // .. and close it
2628 closeFolder(openFolder);
2629 if (folderScreen != mWorkspace.getCurrentPage()) {
2630 // Close any folder open on the current screen
2631 closeFolder();
2632 // Pull the folder onto this screen
2633 openFolder(folderIcon);
2634 }
2635 }
2636 }
Michael Jurka5130e402011-10-13 04:55:35 -07002637 }
2638
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002639 /**
2640 * Event handler for the (Add) Widgets button that appears after a long press
2641 * on the home screen.
2642 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002643 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002644 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002645 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2646 }
2647
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002648 /**
2649 * Event handler for the wallpaper picker button that appears after a long press
2650 * on the home screen.
2651 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002652 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002653 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2654 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2655 pickWallpaper.setComponent(getWallpaperPickerComponent());
2656 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2657 }
2658
2659 /**
2660 * Event handler for a click on the settings button that appears after a long press
2661 * on the home screen.
2662 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002663 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002664 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2665 }
2666
Michael Jurka5130e402011-10-13 04:55:35 -07002667 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002668 // Provide the same haptic feedback that the system offers for virtual keys.
2669 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002670 }
2671
Adam Cohen61f560d2013-09-30 15:58:20 -07002672 public void performHapticFeedbackOnTouchDown(View v) {
2673 // Provide the same haptic feedback that the system offers for virtual keys.
2674 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2675 }
2676
2677 public View.OnTouchListener getHapticFeedbackTouchListener() {
2678 if (mHapticFeedbackTouchListener == null) {
2679 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2680 @Override
2681 public boolean onTouch(View v, MotionEvent event) {
2682 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2683 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2684 }
2685 return false;
2686 }
2687 };
2688 }
2689 return mHapticFeedbackTouchListener;
2690 }
2691
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002692 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002693 if (!DISABLE_MARKET_BUTTON) {
2694 if (mAppMarketIntent != null) {
2695 startActivitySafely(v, mAppMarketIntent, "app market");
2696 } else {
2697 Log.e(TAG, "Invalid app market intent.");
2698 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002699 }
2700 }
2701
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002702 public void onDragStarted(View view) {}
2703
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002704 /**
2705 * Called when the user stops interacting with the launcher.
2706 * This implies that the user is now on the homescreen and is not doing housekeeping.
2707 */
2708 protected void onInteractionEnd() {}
2709
2710 /**
2711 * Called when the user starts interacting with the launcher.
2712 * The possible interactions are:
2713 * - open all apps
2714 * - reorder an app shortcut, or a widget
2715 * - open the overview mode.
2716 * This is a good time to stop doing things that only make sense
2717 * when the user is on the homescreen and not doing housekeeping.
2718 */
2719 protected void onInteractionBegin() {}
2720
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002721 void startApplicationDetailsActivity(ComponentName componentName) {
2722 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002723 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2724 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002725 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2726 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002727 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002728 }
2729
Michael Jurka1e2f4652013-07-08 18:03:46 -07002730 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002731 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2732 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002733 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002734 // System applications cannot be installed. For now, show a toast explaining that.
2735 // We may give them the option of disabling apps this way.
2736 int messageId = R.string.uninstall_system_app_text;
2737 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002738 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002739 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002740 String packageName = componentName.getPackageName();
2741 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002742 Intent intent = new Intent(
2743 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002744 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2745 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002746 if (user != null) {
2747 user.addToIntent(intent, Intent.EXTRA_USER);
2748 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002749 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002750 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002751 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002752 }
2753
Michael Jurka86a720e2012-05-09 11:23:23 -07002754 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002755 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002756 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002757 // Only launch using the new animation if the shortcut has not opted out (this is a
2758 // private contract between launcher and may be ignored in the future).
2759 boolean useLaunchAnimation = (v != null) &&
2760 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002761 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2762 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002763
Kenny Guy1317e2d2014-05-08 18:52:50 +01002764 UserHandleCompat user = null;
2765 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2766 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2767 user = userManager.getUserForSerialNumber(serialNumber);
2768 }
2769
2770 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002771 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002772 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2773 v.getMeasuredWidth(), v.getMeasuredHeight());
Kenny Guy1317e2d2014-05-08 18:52:50 +01002774 optsBundle = opts.toBundle();
2775 }
Adam Cohen6ea3b112014-06-11 11:38:49 -07002776 if (useLaunchAnimation && Utilities.isLmp()) {
2777 ActivityOptions opts = ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim);
2778 optsBundle = opts.toBundle();
2779 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002780
2781 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2782 // Could be launching some bookkeeping activity
2783 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002784 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002785 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002786 launcherApps.startActivityForProfile(intent.getComponent(), user,
2787 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002788 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002789 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002790 } catch (SecurityException e) {
2791 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002792 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002793 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002794 "or use the exported attribute for this activity. "
2795 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002796 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002797 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002798 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002799
Michael Jurka86a720e2012-05-09 11:23:23 -07002800 boolean startActivitySafely(View v, Intent intent, Object tag) {
2801 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002802 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2803 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2804 return false;
2805 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002806 try {
2807 success = startActivity(v, intent, tag);
2808 } catch (ActivityNotFoundException e) {
2809 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2810 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2811 }
2812 return success;
2813 }
2814
Adam Cohen268c4752012-06-06 17:47:33 -07002815 /**
2816 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2817 * in the DragLayer in the exact absolute location of the original FolderIcon.
2818 */
2819 private void copyFolderIconToImage(FolderIcon fi) {
2820 final int width = fi.getMeasuredWidth();
2821 final int height = fi.getMeasuredHeight();
2822
2823 // Lazy load ImageView, Bitmap and Canvas
2824 if (mFolderIconImageView == null) {
2825 mFolderIconImageView = new ImageView(this);
2826 }
2827 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2828 mFolderIconBitmap.getHeight() != height) {
2829 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2830 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2831 }
2832
2833 DragLayer.LayoutParams lp;
2834 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2835 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2836 } else {
2837 lp = new DragLayer.LayoutParams(width, height);
2838 }
2839
Adam Cohen307fe232012-08-16 17:55:58 -07002840 // The layout from which the folder is being opened may be scaled, adjust the starting
2841 // view size by this scale factor.
2842 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002843 lp.customPosition = true;
2844 lp.x = mRectForFolderAnimation.left;
2845 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002846 lp.width = (int) (scale * width);
2847 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002848
2849 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2850 fi.draw(mFolderIconCanvas);
2851 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002852 if (fi.getFolder() != null) {
2853 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2854 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002855 }
Adam Cohen268c4752012-06-06 17:47:33 -07002856 // Just in case this image view is still in the drag layer from a previous animation,
2857 // we remove it and re-add it.
2858 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2859 mDragLayer.removeView(mFolderIconImageView);
2860 }
2861 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002862 if (fi.getFolder() != null) {
2863 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002864 }
Adam Cohen268c4752012-06-06 17:47:33 -07002865 }
2866
Adam Cohen2801caf2011-05-13 20:57:39 -07002867 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002868 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002869 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2870 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2871 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2872
Adam Cohenc51934b2011-07-26 21:07:43 -07002873 FolderInfo info = (FolderInfo) fi.getTag();
2874 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2875 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002876 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2877 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002878 }
2879
Adam Cohen268c4752012-06-06 17:47:33 -07002880 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2881 copyFolderIconToImage(fi);
2882 fi.setVisibility(View.INVISIBLE);
2883
Michael Jurka2ecf9952012-06-18 12:52:28 -07002884 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002885 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002886 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2887 oa.start();
2888 }
2889
Adam Cohen268c4752012-06-06 17:47:33 -07002890 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002891 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002892 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2893 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2894 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2895
Adam Cohen268c4752012-06-06 17:47:33 -07002896 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002897
Adam Cohen268c4752012-06-06 17:47:33 -07002898 // We remove and re-draw the FolderIcon in-case it has changed
2899 mDragLayer.removeView(mFolderIconImageView);
2900 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002901 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002902 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002903 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002904 oa.addListener(new AnimatorListenerAdapter() {
2905 @Override
2906 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002907 if (cl != null) {
2908 cl.clearFolderLeaveBehind();
2909 // Remove the ImageView copy of the FolderIcon and make the original visible.
2910 mDragLayer.removeView(mFolderIconImageView);
2911 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002912 }
2913 }
2914 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002915 oa.start();
2916 }
2917
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002918 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002919 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002920 * is animated relative to the specified View. If the View is null, no animation
2921 * is played.
2922 *
2923 * @param folderInfo The FolderInfo describing the folder to open.
2924 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002925 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002926 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002927 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002928
Adam Cohena9cf38f2011-05-02 15:36:58 -07002929 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002930
Adam Cohen4554ee12011-08-03 16:13:21 -07002931 // Just verify that the folder hasn't already been added to the DragLayer.
2932 // There was a one-off crash where the folder had a parent already.
2933 if (folder.getParent() == null) {
2934 mDragLayer.addView(folder);
2935 mDragController.addDropTarget((DropTarget) folder);
2936 } else {
2937 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2938 folder.getParent() + ").");
2939 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002940 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002941 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002942
2943 // Notify the accessibility manager that this folder "window" has appeared and occluded
2944 // the workspace items
2945 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2946 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002947 }
2948
2949 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002950 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002951 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002952 if (folder.isEditingName()) {
2953 folder.dismissEditingName();
2954 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002955 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002956
2957 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002958 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002959 }
2960 }
2961
2962 void closeFolder(Folder folder) {
2963 folder.getInfo().opened = false;
2964
2965 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2966 if (parent != null) {
2967 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2968 shrinkAndFadeInFolderIcon(fi);
2969 }
2970 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002971
2972 // Notify the accessibility manager that this folder "window" has disappeard and no
2973 // longer occludeds the workspace items
2974 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002975 }
2976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002978 if (!isDraggingEnabled()) return false;
2979 if (isWorkspaceLocked()) return false;
2980 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002981
Adam Cohen1697b792013-09-17 19:08:21 -07002982 if (v instanceof Workspace) {
2983 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002984 if (mWorkspace.enterOverviewMode()) {
2985 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2986 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2987 return true;
2988 } else {
2989 return false;
2990 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002991 } else {
2992 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002993 }
Adam Cohen1697b792013-09-17 19:08:21 -07002994 }
2995
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002996 CellLayout.CellInfo longClickCellInfo = null;
2997 View itemUnderLongClick = null;
2998 if (v.getTag() instanceof ItemInfo) {
2999 ItemInfo info = (ItemInfo) v.getTag();
3000 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3001 itemUnderLongClick = longClickCellInfo.cell;
3002 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003003 }
3004
Winson Chung3d503fb2011-07-13 17:25:49 -07003005 // The hotseat touch handling does not go through Workspace, and we always allow long press
3006 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003007 final boolean inHotseat = isHotseatLayout(v);
3008 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003009 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003010 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003011 // User long pressed on empty space
3012 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3013 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003014 if (mWorkspace.isInOverviewMode()) {
3015 mWorkspace.startReordering(v);
3016 } else {
3017 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003018 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003019 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003020 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3021 mHotseat.getOrderInHotseat(
3022 longClickCellInfo.cellX,
3023 longClickCellInfo.cellY));
3024 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003025 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003026 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003027 }
3028 }
3029 }
3030 return true;
3031 }
3032
Winson Chung3d503fb2011-07-13 17:25:49 -07003033 boolean isHotseatLayout(View layout) {
3034 return mHotseat != null && layout != null &&
3035 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3036 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003037
Winson Chung3d503fb2011-07-13 17:25:49 -07003038 /**
3039 * Returns the CellLayout of the specified container at the specified screen.
3040 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003041 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003042 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3043 if (mHotseat != null) {
3044 return mHotseat.getLayout();
3045 } else {
3046 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003047 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003048 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003049 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003050 }
3051 }
3052
Daniel Sandler843e8602010-06-07 14:59:01 -04003053 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003054 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003055 }
3056
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003057 /**
3058 * Helper method for the cameraZoomIn/cameraZoomOut animations
3059 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003060 * @param scaleFactor The scale factor used for the zoom
3061 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07003062 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003063 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07003064 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003065 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003066
Craig Mautner360310b2012-10-26 15:13:08 -07003067 private void setWorkspaceBackground(boolean workspace) {
3068 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003069 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003070 }
3071
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003072 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003073 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3074 int curflags = getWindow().getAttributes().flags
3075 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3076 if (wpflags != curflags) {
3077 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3078 }
Craig Mautner360310b2012-10-26 15:13:08 -07003079 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003080 }
3081
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003082 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3083 if (v instanceof LauncherTransitionable) {
3084 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3085 }
3086 }
3087
Michael Jurkabed61d22012-02-14 22:51:29 -08003088 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3089 if (v instanceof LauncherTransitionable) {
3090 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3091 }
Winson Chung70442722012-02-10 15:43:22 -08003092
3093 // Update the workspace transition step as well
3094 dispatchOnLauncherTransitionStep(v, 0f);
3095 }
3096
3097 private void dispatchOnLauncherTransitionStep(View v, float t) {
3098 if (v instanceof LauncherTransitionable) {
3099 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3100 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003101 }
3102
3103 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3104 if (v instanceof LauncherTransitionable) {
3105 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3106 }
Winson Chung70442722012-02-10 15:43:22 -08003107
3108 // Update the workspace transition step as well
3109 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003110 }
3111
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003112 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003113 * Things to test when changing the following seven functions.
3114 * - Home from workspace
3115 * - from center screen
3116 * - from other screens
3117 * - Home from all apps
3118 * - from center screen
3119 * - from other screens
3120 * - Back from all apps
3121 * - from center screen
3122 * - from other screens
3123 * - Launch app from workspace and quit
3124 * - with back
3125 * - with home
3126 * - Launch app from all apps and quit
3127 * - with back
3128 * - with home
3129 * - Go to a screen that's not the default, then all
3130 * apps, and launch and app, and go back
3131 * - with back
3132 * -with home
3133 * - On workspace, long press power and go back
3134 * - with back
3135 * - with home
3136 * - On all apps, long press power and go back
3137 * - with back
3138 * - with home
3139 * - On workspace, power off
3140 * - On all apps, power off
3141 * - Launch an app and turn off the screen while in that app
3142 * - Go back with home key
3143 * - Go back with back key TODO: make this not go to workspace
3144 * - From all apps
3145 * - From workspace
3146 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3147 * - From all apps
3148 * - From the center workspace
3149 * - From another workspace
3150 */
3151
3152 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003153 * Zoom the camera out from the workspace to reveal 'toView'.
3154 * Assumes that the view to show is anchored at either the very top or very bottom
3155 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003156 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003157 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003158 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3159 showAppsCustomizeHelper(animated, springLoaded, contentType);
3160 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003161
Winson Chungc58497e2013-09-03 17:48:37 -07003162 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3163 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003164 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003165 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003166 mStateAnimation.cancel();
3167 mStateAnimation = null;
3168 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003169
3170 boolean material = Utilities.isLmp();
3171
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003172 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003173
Winson Chungf0ea4d32011-06-06 14:27:16 -07003174 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3175 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003176 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
3177
Winson Chungf0ea4d32011-06-06 14:27:16 -07003178 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003179 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003180 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003181 final int startDelay =
3182 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003183
Michael Jurkab3e22d92011-10-31 15:58:33 -07003184 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003185
Adam Cohen6c5891a2014-07-09 23:53:15 -07003186 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3187 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003188 Animator workspaceAnim =
Adam Cohen6c5891a2014-07-09 23:53:15 -07003189 mWorkspace.getChangeStateAnimation(workspaceState, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003190 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003191 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3192 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003193 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3194 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003195
3196 if (animated) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07003197 if (!material) {
3198 toView.setScaleX(scale);
3199 toView.setScaleY(scale);
3200 final LauncherViewPropertyAnimator scaleAnim =
3201 new LauncherViewPropertyAnimator(toView);
3202 scaleAnim.
3203 scaleX(1f).scaleY(1f).
3204 setDuration(duration).
3205 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003206
Adam Cohen6c5891a2014-07-09 23:53:15 -07003207 toView.setVisibility(View.VISIBLE);
3208 toView.setAlpha(0f);
3209 final ObjectAnimator alphaAnim = LauncherAnimUtils
3210 .ofFloat(toView, "alpha", 0f, 1f)
3211 .setDuration(fadeDuration);
3212 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
3213 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3214 @Override
3215 public void onAnimationUpdate(ValueAnimator animation) {
3216 if (animation == null) {
3217 throw new RuntimeException("animation is null");
3218 }
3219 float t = (Float) animation.getAnimatedValue();
3220 dispatchOnLauncherTransitionStep(fromView, t);
3221 dispatchOnLauncherTransitionStep(toView, t);
Michael Jurka059798a2012-09-04 09:20:16 -07003222 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003223 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003224
Adam Cohen6c5891a2014-07-09 23:53:15 -07003225 // toView should appear right at the end of the workspace shrink
3226 // animation
3227 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
3228 mStateAnimation.play(scaleAnim).after(startDelay);
3229 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003230
Adam Cohen6c5891a2014-07-09 23:53:15 -07003231 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3232 @Override
3233 public void onAnimationStart(Animator animation) {
3234 // Prepare the position
3235 toView.setTranslationX(0.0f);
3236 toView.setTranslationY(0.0f);
3237 toView.setVisibility(View.VISIBLE);
3238 toView.bringToFront();
Winson Chungadf0c182012-08-23 14:59:07 -07003239 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003240 @Override
3241 public void onAnimationEnd(Animator animation) {
3242 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3243 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003244
Adam Cohen6c5891a2014-07-09 23:53:15 -07003245 // Hide the search bar
3246 if (mSearchDropTargetBar != null) {
3247 mSearchDropTargetBar.hideSearchBar(false);
3248 }
3249 }
3250 });
3251 } else {
3252 int width = toView.getMeasuredWidth();
3253 int height = toView.getMeasuredHeight();
3254 float revealRadius = (float) Math.sqrt((width * width) / 4 + height * height);
3255
3256 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
3257
3258 AppsCustomizePagedView content = (AppsCustomizePagedView)
3259 toView.findViewById(R.id.apps_customize_pane_content);
3260
3261 View page = content.getPageAt(content.getCurrentPage());
3262 View revealView = content;
3263
3264 float yDrift = height / 2f - 400;
3265
3266 LauncherViewPropertyAnimator panelAlphaAndDrift =
3267 new LauncherViewPropertyAnimator(revealView);
3268 revealView.setTranslationY(yDrift);
3269 revealView.setAlpha(0.3f);
3270 panelAlphaAndDrift.alpha(1)
3271 .translationY(0)
3272 .setDuration(revealDuration)
3273 .setInterpolator(new LogDecelerateInterpolator(100, 0));
3274
3275 mStateAnimation.play(panelAlphaAndDrift);
3276
3277 if (page instanceof CellLayout) {
3278 CellLayout cellLayout = (CellLayout) page;
3279 cellLayout.enableHardwareLayer(true);
3280
3281 View iconsView = cellLayout.getShortcutsAndWidgets();
3282 iconsView.setAlpha(0f);
3283
3284 LauncherViewPropertyAnimator iconsAlpha =
3285 new LauncherViewPropertyAnimator(iconsView);
3286 iconsAlpha.alpha(1f)
3287 .setDuration(revealDuration - 100)
3288 .setInterpolator(new LogDecelerateInterpolator(100, 0));
3289 mStateAnimation.play(iconsAlpha);
3290 }
3291
3292 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3293 pageIndicators.setAlpha(0f);
3294 final LauncherViewPropertyAnimator indicatorsAlpha =
3295 new LauncherViewPropertyAnimator(pageIndicators);
3296 indicatorsAlpha.alpha(1f);
3297 indicatorsAlpha.setDuration(revealDuration);
3298 mStateAnimation.play(indicatorsAlpha);
3299
3300 width = revealView.getMeasuredWidth();
3301
3302 ValueAnimator reveal =
3303 ViewAnimationUtils.createCircularReveal(revealView, width / 2,
3304 height / 2 + 100, 0f, revealRadius);
3305 reveal.setDuration(revealDuration);
3306 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3307
3308 toView.setTranslationX(0);
3309 toView.setTranslationY(0);
3310 toView.setAlpha(1f);
3311 // toView should appear right at the end of the workspace shrink
3312 // animation
3313 mStateAnimation.play(reveal);
3314
3315 reveal.addListener(new AnimatorListenerAdapter() {
3316 @Override
3317 public void onAnimationStart(Animator animation) {
3318 // Prepare the position
3319 toView.bringToFront();
3320 toView.setVisibility(View.VISIBLE);
3321 }
3322 });
3323
3324 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3325 @Override
3326 public void onAnimationEnd(Animator animation) {
3327 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3328 dispatchOnLauncherTransitionEnd(toView, animated, false);
3329
3330 // Hide the search bar
3331 if (mSearchDropTargetBar != null) {
3332 mSearchDropTargetBar.hideSearchBar(false);
3333 }
3334 }
3335 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003336 }
Michael Jurka1899a362011-11-03 13:50:45 -07003337
3338 boolean delayAnim = false;
Adam Cohen6c5891a2014-07-09 23:53:15 -07003339 if (workspaceAnim != null) {
3340 mStateAnimation.play(workspaceAnim);
3341 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003342 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3343 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003344
3345 // If any of the objects being animated haven't been measured/laid out
3346 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003347 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003348 (mWorkspace.getMeasuredWidth() == 0) ||
3349 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003350 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003351 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003352
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003353 final AnimatorSet stateAnimation = mStateAnimation;
3354 final Runnable startAnimRunnable = new Runnable() {
3355 public void run() {
3356 // Check that mStateAnimation hasn't changed while
3357 // we waited for a layout/draw pass
3358 if (mStateAnimation != stateAnimation)
3359 return;
3360 setPivotsForZoom(toView, scale);
3361 dispatchOnLauncherTransitionStart(fromView, animated, false);
3362 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003363 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003364 }
3365 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003366 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003367 final ViewTreeObserver observer = toView.getViewTreeObserver();
3368 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3369 public void onGlobalLayout() {
3370 startAnimRunnable.run();
3371 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3372 }
3373 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003374 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003375 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003376 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003377 } else {
3378 toView.setTranslationX(0.0f);
3379 toView.setTranslationY(0.0f);
3380 toView.setScaleX(1.0f);
3381 toView.setScaleY(1.0f);
3382 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003383 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003384
Daniel Sandlere4f98912013-06-25 15:13:26 -04003385 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003386 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003387 if (mSearchDropTargetBar != null) {
3388 mSearchDropTargetBar.hideSearchBar(false);
3389 }
Michael Jurkaabded662011-03-04 12:06:57 -08003390 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003391 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003392 dispatchOnLauncherTransitionStart(fromView, animated, false);
3393 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003394 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003395 dispatchOnLauncherTransitionStart(toView, animated, false);
3396 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003397 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003398 }
3399
3400 /**
3401 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003402 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003403 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003404 */
Adam Cohened307df2013-10-02 09:37:31 -07003405 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003406 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003407
Michael Jurkab3e22d92011-10-31 15:58:33 -07003408 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003409 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003410 mStateAnimation.cancel();
3411 mStateAnimation = null;
3412 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003413
3414 boolean material = Utilities.isLmp();
3415
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003416 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003417
Winson Chungf0ea4d32011-06-06 14:27:16 -07003418 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003419 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
3420 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
3421
Winson Chungf0ea4d32011-06-06 14:27:16 -07003422 final float scaleFactor = (float)
3423 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3424 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003425 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003426 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003427 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003428 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3429 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003430 toState, animated, stagger, -1);
3431 } else if (toState == Workspace.State.SPRING_LOADED ||
3432 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003433 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003434 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003435 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003436
Michael Jurkab3e22d92011-10-31 15:58:33 -07003437 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003438 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003439 if (animated) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07003440 if (!material) {
3441 final LauncherViewPropertyAnimator scaleAnim =
3442 new LauncherViewPropertyAnimator(fromView);
3443 scaleAnim.
3444 scaleX(scaleFactor).scaleY(scaleFactor).
3445 setDuration(duration).
3446 setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurka159b4cc2012-01-17 03:00:35 -08003447
Adam Cohen6c5891a2014-07-09 23:53:15 -07003448 final ObjectAnimator alphaAnim = LauncherAnimUtils
3449 .ofFloat(fromView, "alpha", 1f, 0f)
3450 .setDuration(fadeOutDuration);
3451 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
3452 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3453 @Override
3454 public void onAnimationUpdate(ValueAnimator animation) {
3455 float t = 1f - (Float) animation.getAnimatedValue();
3456 dispatchOnLauncherTransitionStep(fromView, t);
3457 dispatchOnLauncherTransitionStep(toView, t);
3458 }
3459 });
3460
3461 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
3462
3463 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3464 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3465 mAppsCustomizeContent.stopScrolling();
3466
3467 mStateAnimation.playTogether(scaleAnim, alphaAnim);
3468 } else {
3469 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
3470
3471 int width = fromView.getMeasuredWidth();
3472 int height = fromView.getMeasuredHeight();
3473 float revealRadius = (float) Math.sqrt((width * width) / 4 + height * height);
3474
3475 AppsCustomizePagedView content = (AppsCustomizePagedView)
3476 fromView.findViewById(R.id.apps_customize_pane_content);
3477
3478 final View page = content.getPageAt(content.getNextPage());
3479 View revealView = page;
3480
3481 float yDrift = height / 2f - 400;
3482
3483 LauncherViewPropertyAnimator panelAlphaAndDrift =
3484 new LauncherViewPropertyAnimator(revealView);
3485 revealView.setTranslationY(0);
3486 revealView.setAlpha(1);
3487 panelAlphaAndDrift.alpha(0)
3488 .translationY(yDrift)
3489 .setDuration(revealDuration)
3490 .setInterpolator(new LogDecelerateInterpolator(100, 0));
3491
3492 mStateAnimation.play(panelAlphaAndDrift);
3493
3494 if (page instanceof CellLayout) {
3495 final CellLayout cellLayout = (CellLayout) page;
3496 cellLayout.enableHardwareLayer(true);
3497
3498 final View iconsView = cellLayout.getShortcutsAndWidgets();
3499
3500 LauncherViewPropertyAnimator iconsAlpha =
3501 new LauncherViewPropertyAnimator(iconsView);
3502 iconsAlpha.alpha(0f)
3503 .setDuration(revealDuration - 100)
3504 .setInterpolator(new LogDecelerateInterpolator(100, 0));
3505
3506 mStateAnimation.play(iconsAlpha);
3507
3508 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3509 @Override
3510 public void onAnimationEnd(Animator animation) {
3511 cellLayout.setTranslationY(0);
3512 cellLayout.setAlpha(1f);
3513 iconsView.setAlpha(1f);
3514 }
3515 });
Winson Chung70442722012-02-10 15:43:22 -08003516 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003517
Adam Cohen6c5891a2014-07-09 23:53:15 -07003518 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3519 final LauncherViewPropertyAnimator indicatorsAlpha =
3520 new LauncherViewPropertyAnimator(pageIndicators);
3521 indicatorsAlpha.alpha(0f);
3522 indicatorsAlpha.setDuration(revealDuration);
3523 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3524 mStateAnimation.play(indicatorsAlpha);
Michael Jurkabed61d22012-02-14 22:51:29 -08003525
Adam Cohen6c5891a2014-07-09 23:53:15 -07003526 width = revealView.getMeasuredWidth();
3527
3528 ValueAnimator reveal =
3529 ViewAnimationUtils.createCircularReveal(revealView, width / 2,
3530 height / 2 + 100, revealRadius, 0f);
3531 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3532 reveal.setDuration(revealDuration);
3533
3534 reveal.addListener(new AnimatorListenerAdapter() {
3535 @Override
3536 public void onAnimationEnd(Animator animation) {
3537 fromView.setVisibility(View.GONE);
3538 }
3539 });
3540
3541 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3542 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3543 mAppsCustomizeContent.stopScrolling();
3544
3545 mStateAnimation.play(reveal);
3546 }
3547 if (workspaceAnim != null) {
3548 mStateAnimation.play(workspaceAnim);
3549 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003550
3551 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003552 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003553 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003554 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003555 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3556 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003557 if (onCompleteRunnable != null) {
3558 onCompleteRunnable.run();
3559 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003560 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003561 }
3562 });
3563
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003564 dispatchOnLauncherTransitionStart(fromView, animated, true);
3565 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003566 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003567 } else {
3568 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003569 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003570 dispatchOnLauncherTransitionStart(fromView, animated, true);
3571 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003572 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003573 dispatchOnLauncherTransitionStart(toView, animated, true);
3574 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003575 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003576 }
3577
Michael Jurkae326f182011-11-21 14:05:46 -08003578 @Override
3579 public void onTrimMemory(int level) {
3580 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003581 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003582 mAppsCustomizeTabHost.onTrimMemory();
3583 }
3584 }
3585
Adam Cohened307df2013-10-02 09:37:31 -07003586 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003587 showWorkspace(animated, null);
3588 }
3589
Adam Cohened307df2013-10-02 09:37:31 -07003590 protected void showWorkspace() {
3591 showWorkspace(true);
3592 }
3593
Adam Cohened66b2b2012-01-23 17:28:51 -08003594 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003595 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003596 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003597 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003598 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003599
Winson Chungc7d2b602012-05-16 17:02:20 -07003600 // Show the search bar (only animate if we were showing the drop target bar in spring
3601 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003602 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003603 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003604 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003605
Michael Jurkab3e22d92011-10-31 15:58:33 -07003606 // Set focus to the AppsCustomize button
3607 if (mAllAppsButton != null) {
3608 mAllAppsButton.requestFocus();
3609 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003610 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003611
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003612 // Change the state *after* we've called all the transition code
3613 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003614
Winson Chung5fb63472011-02-02 17:03:37 -08003615 // Resume the auto-advance of widgets
3616 mUserPresent = true;
3617 updateRunning();
3618
alanv1d4fde62012-10-17 13:15:47 -07003619 // Send an accessibility event to announce the context change
3620 getWindow().getDecorView()
3621 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003622
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003623 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003624 }
3625
Adam Cohened307df2013-10-02 09:37:31 -07003626 void showOverviewMode(boolean animated) {
3627 mWorkspace.setVisibility(View.VISIBLE);
3628 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3629 mState = State.WORKSPACE;
3630 onWorkspaceShown(animated);
3631 }
3632
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003633 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003634 }
3635
Winson Chung82963d52013-10-09 11:20:57 -07003636 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3637 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003638 if (mState != State.WORKSPACE) return;
3639
Winson Chung82963d52013-10-09 11:20:57 -07003640 if (resetPageToZero) {
3641 mAppsCustomizeTabHost.reset();
3642 }
Winson Chungc58497e2013-09-03 17:48:37 -07003643 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003644 mAppsCustomizeTabHost.post(new Runnable() {
3645 @Override
3646 public void run() {
3647 // We post this in-case the all apps view isn't yet constructed.
3648 mAppsCustomizeTabHost.requestFocus();
3649 }
3650 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003651
Michael Jurkab3e22d92011-10-31 15:58:33 -07003652 // Change the state *after* we've called all the transition code
3653 mState = State.APPS_CUSTOMIZE;
3654
3655 // Pause the auto-advance of widgets until we are out of AllApps
3656 mUserPresent = false;
3657 updateRunning();
3658 closeFolder();
3659
3660 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003661 getWindow().getDecorView()
3662 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003663 }
3664
Adam Cohen7777d962011-08-18 18:58:38 -07003665 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003666 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003667 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003668 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003669 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003670 }
Adam Cohen7777d962011-08-18 18:58:38 -07003671
Adam Cohenad4e15c2013-10-17 16:21:35 -07003672 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003673 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003674 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3675
Winson Chunge7a03942011-08-05 15:05:12 -07003676 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003677 @Override
3678 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003679 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003680 // Before we show workspace, hide all apps again because
3681 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3682 // clean up our state transition functions
3683 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003684 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003685 } else {
3686 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003687 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003688 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003689 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003690 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003691
Michael Jurkad3ef3062010-11-23 16:23:58 -08003692 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003693 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003694 final boolean animated = true;
3695 final boolean springLoaded = true;
3696 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003697 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003698 }
3699 // Otherwise, we are not in spring loaded mode, so don't do anything.
3700 }
3701
Michael Jurkab3e22d92011-10-31 15:58:33 -07003702 void lockAllApps() {
3703 // TODO
3704 }
3705
3706 void unlockAllApps() {
3707 // TODO
3708 }
3709
Winson Chungf0ea4d32011-06-06 14:27:16 -07003710 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003711 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003712 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003713 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003714 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003715 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003716 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003717 int duration = 0;
3718 if (mSearchDropTargetBar != null) {
3719 duration = mSearchDropTargetBar.getTransitionInDuration();
3720 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003721 mHotseat.animate().alpha(1f).setDuration(duration);
3722 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003723 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003724 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003725 }
3726 }
3727 }
3728
3729 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003730 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003731 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003732 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003733 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003734 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003735 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003736 int duration = 0;
3737 if (mSearchDropTargetBar != null) {
3738 duration = mSearchDropTargetBar.getTransitionOutDuration();
3739 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003740 mHotseat.animate().alpha(0f).setDuration(duration);
3741 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003742 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003743 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003744 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003745 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003746 }
3747
Patrick Dubroy5f445422011-02-18 14:35:21 -08003748 /**
3749 * Add an item from all apps or customize onto the given workspace screen.
3750 * If layout is null, add to the current screen.
3751 */
3752 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003753 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003754 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003755 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003756 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003757
Winson Chungdff8ebb2011-09-08 17:25:31 -07003758 /** Maps the current orientation to an index for referencing orientation correct global icons */
3759 private int getCurrentOrientationIndexForGlobalIcons() {
3760 // default - 0, landscape - 1
3761 switch (getResources().getConfiguration().orientation) {
3762 case Configuration.ORIENTATION_LANDSCAPE:
3763 return 1;
3764 default:
3765 return 0;
3766 }
3767 }
3768
Michael Jurkaae65ee32012-04-30 11:45:54 -07003769 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003770 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003771 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003772 // Look for the toolbar icon specified in the activity meta-data
3773 Bundle metaData = packageManager.getActivityInfo(
3774 activityName, PackageManager.GET_META_DATA).metaData;
3775 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003776 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003777 if (iconResId != 0) {
3778 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003779 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003780 }
3781 }
3782 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003783 // This can happen if the activity defines an invalid drawable
3784 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3785 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003786 } catch (Resources.NotFoundException nfe) {
3787 // This can happen if the activity defines an invalid drawable
3788 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3789 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003790 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003791 return null;
3792 }
3793
3794 // if successful in getting icon, return it; otherwise, set button to use default drawable
3795 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003796 int buttonId, ComponentName activityName, int fallbackDrawableId,
3797 String toolbarResourceName) {
3798 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003799 Resources r = getResources();
3800 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3801 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003802
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003803 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003804 // If we were unable to find the icon via the meta-data, use a generic one
3805 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003806 toolbarIcon = r.getDrawable(fallbackDrawableId);
3807 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003808 if (button != null) {
3809 button.setCompoundDrawables(toolbarIcon, null, null, null);
3810 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003811 return null;
3812 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003813 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003814 if (button != null) {
3815 button.setCompoundDrawables(toolbarIcon, null, null, null);
3816 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003817 return toolbarIcon.getConstantState();
3818 }
3819 }
3820
3821 // if successful in getting icon, return it; otherwise, set button to use default drawable
3822 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003823 int buttonId, ComponentName activityName, int fallbackDrawableId,
3824 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003825 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003826 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003827
Michael Jurka19e0fc52011-07-22 18:00:21 -07003828 if (button != null) {
3829 // If we were unable to find the icon via the meta-data, use a
3830 // generic one
3831 if (toolbarIcon == null) {
3832 button.setImageResource(fallbackDrawableId);
3833 } else {
3834 button.setImageDrawable(toolbarIcon);
3835 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003836 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003837
3838 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3839
Michael Jurka0423dcf2010-10-05 14:56:18 -07003840 }
3841
Winson Chung1b884092012-06-08 17:13:02 -07003842 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003843 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003844 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003845 }
3846
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003847 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003848 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003849 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003850 }
3851
Winson Chungbb185bd2011-11-21 12:31:42 -08003852 private void invalidatePressedFocusedStates(View container, View button) {
3853 if (container instanceof HolographicLinearLayout) {
3854 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3855 layout.invalidatePressedFocusedStates();
3856 } else if (button instanceof HolographicImageView) {
3857 HolographicImageView view = (HolographicImageView) button;
3858 view.invalidatePressedFocusedStates();
3859 }
3860 }
3861
Cristina Stancu476493b2013-08-07 17:20:14 +01003862 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003863 if (mQsb == null) {
3864 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3865 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003866 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003867 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003868 }
3869
3870 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003871 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003872 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003873 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003874 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003875
Winson Chung1cad91e2011-05-25 17:41:01 -07003876 final SearchManager searchManager =
3877 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3878 ComponentName activityName = searchManager.getGlobalSearchActivity();
3879 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003880 int coi = getCurrentOrientationIndexForGlobalIcons();
3881 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003882 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3883 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3884 if (sGlobalSearchIcon[coi] == null) {
3885 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3886 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3887 TOOLBAR_ICON_METADATA_NAME);
3888 }
3889
Winson Chungc51db6a2011-10-05 11:44:49 -07003890 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3891 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003892 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003893 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003894 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003895 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003896 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3897 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003898 if (searchButton != null) searchButton.setVisibility(View.GONE);
3899 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003900 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003901 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003902 }
3903 }
3904
Cristina Stancu476493b2013-08-07 17:20:14 +01003905 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003906 final View searchButtonContainer = findViewById(R.id.search_button_container);
3907 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003908 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003909 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003910 }
3911
Cristina Stancu476493b2013-08-07 17:20:14 +01003912 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003913 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003914 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003915
Winson Chungc51db6a2011-10-05 11:44:49 -07003916 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003917 final SearchManager searchManager =
3918 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3919 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3920
3921 ComponentName activityName = null;
3922 if (globalSearchActivity != null) {
3923 // Check if the global search activity handles voice search
3924 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3925 intent.setPackage(globalSearchActivity.getPackageName());
3926 activityName = intent.resolveActivity(getPackageManager());
3927 }
3928
3929 if (activityName == null) {
3930 // Fallback: check if an activity other than the global search activity
3931 // resolves this
3932 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3933 activityName = intent.resolveActivity(getPackageManager());
3934 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003935 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003936 int coi = getCurrentOrientationIndexForGlobalIcons();
3937 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003938 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3939 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3940 if (sVoiceSearchIcon[coi] == null) {
3941 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3942 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3943 TOOLBAR_ICON_METADATA_NAME);
3944 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003945 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003946 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003947 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003948 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003949 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003950 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003951 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003952 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003953 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003954 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003955 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003956 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003957
Cristina Stancu476493b2013-08-07 17:20:14 +01003958 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003959 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3960 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003961 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003962 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003963 }
3964
Winson Chung5841efa2013-09-30 18:06:44 -07003965 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003966 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3967 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003968 boolean visible = !forceDisableVoiceButtonProxy &&
3969 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003970 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003971 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003972 }
3973 }
Winson Chung5841efa2013-09-30 18:06:44 -07003974
3975 /**
3976 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3977 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3978 */
3979 public void disableVoiceButtonProxy(boolean disabled) {
3980 updateVoiceButtonProxyVisible(disabled);
3981 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003982 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003983 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003984 */
3985 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003986 if (!DISABLE_MARKET_BUTTON) {
3987 final View marketButton = findViewById(R.id.market_button);
3988 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3989 // Find the app market activity by resolving an intent.
3990 // (If multiple app markets are installed, it will return the ResolverActivity.)
3991 ComponentName activityName = intent.resolveActivity(getPackageManager());
3992 if (activityName != null) {
3993 int coi = getCurrentOrientationIndexForGlobalIcons();
3994 mAppMarketIntent = intent;
3995 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3996 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3997 TOOLBAR_ICON_METADATA_NAME);
3998 marketButton.setVisibility(View.VISIBLE);
3999 } else {
4000 // We should hide and disable the view so that we don't try and restore the visibility
4001 // of it when we swap between drag & normal states from IconDropTarget subclasses.
4002 marketButton.setVisibility(View.GONE);
4003 marketButton.setEnabled(false);
4004 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004005 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004006 }
4007
Michael Jurkae7bf83b2010-12-14 18:02:21 -08004008 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07004009 if (!DISABLE_MARKET_BUTTON) {
4010 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
4011 Resources r = getResources();
4012 Drawable marketIconDrawable = d.newDrawable(r);
4013 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
4014 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
4015 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07004016
Winson Chungd64a6662013-09-30 11:06:59 -07004017 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
4018 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08004019 }
4020
Michael Jurkad7c28052012-04-27 15:43:36 -07004021 @Override
4022 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004023 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004024 final List<CharSequence> text = event.getText();
4025 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004026 // Populate event with a fake title based on the current state.
4027 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004028 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004029 } else {
4030 text.add(getString(R.string.all_apps_home_button_label));
4031 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004032 return result;
4033 }
4034
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004035 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004036 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004037 */
4038 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4039 @Override
4040 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004041 closeSystemDialogs();
4042 }
4043 }
4044
4045 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004046 * Receives notifications whenever the appwidgets are reset.
4047 */
4048 private class AppWidgetResetObserver extends ContentObserver {
4049 public AppWidgetResetObserver() {
4050 super(new Handler());
4051 }
4052
4053 @Override
4054 public void onChange(boolean selfChange) {
4055 onAppWidgetReset();
4056 }
4057 }
4058
4059 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004060 * If the activity is currently paused, signal that we need to run the passed Runnable
4061 * in onResume.
4062 *
4063 * This needs to be called from incoming places where resources might have been loaded
4064 * while we are paused. That is becaues the Configuration might be wrong
4065 * when we're not running, and if it comes back to what it was when we
4066 * were paused, we are not restarted.
4067 *
4068 * Implementation of the method from LauncherModel.Callbacks.
4069 *
4070 * @return true if we are currently paused. The caller might be able to
4071 * skip some work in that case since we will come back again.
4072 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004073 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004074 if (mPaused) {
4075 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004076 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004077 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004078 }
4079 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004080 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004081 return true;
4082 } else {
4083 return false;
4084 }
4085 }
4086
Michael Jurkac402cd92013-05-20 15:49:32 +02004087 private boolean waitUntilResume(Runnable run) {
4088 return waitUntilResume(run, false);
4089 }
4090
Michael Jurka1e2f4652013-07-08 18:03:46 -07004091 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004092 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004093 }
4094
Michael Jurka7607c2f2013-04-03 14:33:19 -07004095 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004096 * If the activity is currently paused, signal that we need to re-run the loader
4097 * in onResume.
4098 *
4099 * This needs to be called from incoming places where resources might have been loaded
4100 * while we are paused. That is becaues the Configuration might be wrong
4101 * when we're not running, and if it comes back to what it was when we
4102 * were paused, we are not restarted.
4103 *
4104 * Implementation of the method from LauncherModel.Callbacks.
4105 *
4106 * @return true if we are currently paused. The caller might be able to
4107 * skip some work in that case since we will come back again.
4108 */
4109 public boolean setLoadOnResume() {
4110 if (mPaused) {
4111 Log.i(TAG, "setLoadOnResume");
4112 mOnResumeNeedsLoad = true;
4113 return true;
4114 } else {
4115 return false;
4116 }
4117 }
4118
4119 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004120 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004121 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004122 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004123 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004124 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004125 } else {
4126 return SCREEN_COUNT / 2;
4127 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004128 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004129
Joe Onorato9c1289c2009-08-17 11:03:03 -04004130 /**
4131 * Refreshes the shortcuts shown on the workspace.
4132 *
4133 * Implementation of the method from LauncherModel.Callbacks.
4134 */
4135 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004136 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004137
Michael Jurka7607c2f2013-04-03 14:33:19 -07004138 // If we're starting binding all over again, clear any bind calls we'd postponed in
4139 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4140 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004141 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004142
Winson Chungd64d1762013-08-20 14:37:16 -07004143 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004144 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004145 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004146
Michael Jurka05bf644e2011-11-30 20:28:53 -08004147 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004148 if (mHotseat != null) {
4149 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004150 }
4151 }
4152
Adam Cohendcd297f2013-06-18 13:13:40 -07004153 @Override
4154 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004155 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004156
Adam Cohen5084cba2013-09-03 12:01:16 -07004157 // If there are no screens, we need to have an empty screen
4158 if (orderedScreenIds.size() == 0) {
4159 mWorkspace.addExtraEmptyScreen();
4160 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004161
4162 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004163 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004164 if (hasCustomContentToLeft()) {
4165 mWorkspace.createCustomContentContainer();
4166 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004167 }
Winson Chung64359a52013-07-08 17:17:08 -07004168 }
4169
4170 @Override
4171 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004172 // Log to disk
4173 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4174 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4175 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004176 int count = orderedScreenIds.size();
4177 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004178 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004179 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004180 }
4181
Adam Cohen39a06042013-07-19 14:30:12 -07004182 private boolean shouldShowWeightWatcher() {
4183 String spKey = LauncherAppState.getSharedPreferencesKey();
4184 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004185 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004186
4187 return show;
4188 }
4189
4190 private void toggleShowWeightWatcher() {
4191 String spKey = LauncherAppState.getSharedPreferencesKey();
4192 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4193 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4194
4195 show = !show;
4196
4197 SharedPreferences.Editor editor = sp.edit();
4198 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4199 editor.commit();
4200
4201 if (mWeightWatcher != null) {
4202 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4203 }
4204 }
4205
Winson Chungd64d1762013-08-20 14:37:16 -07004206 public void bindAppsAdded(final ArrayList<Long> newScreens,
4207 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004208 final ArrayList<ItemInfo> addAnimated,
4209 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004210 Runnable r = new Runnable() {
4211 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004212 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004213 }
4214 };
4215 if (waitUntilResume(r)) {
4216 return;
4217 }
4218
Winson Chungd64d1762013-08-20 14:37:16 -07004219 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004220 if (newScreens != null) {
4221 bindAddScreens(newScreens);
4222 }
Winson Chungd64d1762013-08-20 14:37:16 -07004223
4224 // We add the items without animation on non-visible pages, and with
4225 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004226 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004227 bindItems(addNotAnimated, 0,
4228 addNotAnimated.size(), false);
4229 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004230 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004231 bindItems(addAnimated, 0,
4232 addAnimated.size(), true);
4233 }
Winson Chungc58497e2013-09-03 17:48:37 -07004234
Winson Chung87412982013-10-03 18:34:14 -07004235 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004236 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004237
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004238 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004239 addedApps != null && mAppsCustomizeContent != null) {
4240 mAppsCustomizeContent.addApps(addedApps);
4241 }
Winson Chungd64d1762013-08-20 14:37:16 -07004242 }
4243
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004244 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004245 * Bind the items start-end from the list.
4246 *
4247 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004248 */
Winson Chung64359a52013-07-08 17:17:08 -07004249 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4250 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004251 Runnable r = new Runnable() {
4252 public void run() {
4253 bindItems(shortcuts, start, end, forceAnimateIcons);
4254 }
4255 };
4256 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004257 return;
4258 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004259
Winson Chungf0c6ae02012-03-21 16:10:31 -07004260 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004261 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4262 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004263 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004264 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004265 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004266 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004267 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004268
4269 // Short circuit if we are loading dock items for a configuration which has no dock
4270 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4271 mHotseat == null) {
4272 continue;
4273 }
4274
Joe Onorato9c1289c2009-08-17 11:03:03 -04004275 switch (item.itemType) {
4276 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4277 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004278 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004279 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004280
Winson Chung64359a52013-07-08 17:17:08 -07004281 /*
4282 * TODO: FIX collision case
4283 */
Winson Chungce376632013-07-11 16:12:41 -07004284 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4285 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4286 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004287 View v = cl.getChildAt(item.cellX, item.cellY);
4288 Object tag = v.getTag();
4289 String desc = "Collision while binding workspace item: " + item
4290 + ". Collides with " + tag;
4291 if (LauncherAppState.isDogfoodBuild()) {
4292 throw (new RuntimeException(desc));
4293 } else {
4294 Log.d(TAG, desc);
4295 }
Winson Chungce376632013-07-11 16:12:41 -07004296 }
Winson Chung64359a52013-07-08 17:17:08 -07004297 }
4298
Adam Cohendcd297f2013-06-18 13:13:40 -07004299 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4300 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004301 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004302 // Animate all the applications up now
4303 shortcut.setAlpha(0f);
4304 shortcut.setScaleX(0f);
4305 shortcut.setScaleY(0f);
4306 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004307 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004308 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004309 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004310 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004311 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004312 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004313 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004314 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4315 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004316 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004317 default:
4318 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004319 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004320 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004321
Winson Chung997a9232013-07-24 15:33:46 -07004322 if (animateIcons) {
4323 // Animate to the correct page
4324 if (newShortcutsScreenId > -1) {
4325 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004326 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004327 final Runnable startBounceAnimRunnable = new Runnable() {
4328 public void run() {
4329 anim.playTogether(bounceAnims);
4330 anim.start();
4331 }
4332 };
Winson Chung997a9232013-07-24 15:33:46 -07004333 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004334 // We post the animation slightly delayed to prevent slowdowns
4335 // when we are loading right after we return to launcher.
4336 mWorkspace.postDelayed(new Runnable() {
4337 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004338 if (mWorkspace != null) {
4339 mWorkspace.snapToPage(newScreenIndex);
4340 mWorkspace.postDelayed(startBounceAnimRunnable,
4341 NEW_APPS_ANIMATION_DELAY);
4342 }
Winson Chung94d67682013-09-25 16:29:40 -07004343 }
4344 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004345 } else {
4346 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004347 }
4348 }
Winson Chung64359a52013-07-08 17:17:08 -07004349 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004350 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004351 }
4352
Joe Onorato9c1289c2009-08-17 11:03:03 -04004353 /**
4354 * Implementation of the method from LauncherModel.Callbacks.
4355 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004356 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004357 Runnable r = new Runnable() {
4358 public void run() {
4359 bindFolders(folders);
4360 }
4361 };
4362 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004363 return;
4364 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004365 sFolders.clear();
4366 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004367 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004368
4369 /**
4370 * Add the views for a widget to the workspace.
4371 *
4372 * Implementation of the method from LauncherModel.Callbacks.
4373 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004374 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004375 Runnable r = new Runnable() {
4376 public void run() {
4377 bindAppWidget(item);
4378 }
4379 };
4380 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004381 return;
4382 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004383
Daniel Sandler843e8602010-06-07 14:59:01 -04004384 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4385 if (DEBUG_WIDGETS) {
4386 Log.d(TAG, "bindAppWidget: " + item);
4387 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004388 final Workspace workspace = mWorkspace;
4389
4390 final int appWidgetId = item.appWidgetId;
4391 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004392 if (DEBUG_WIDGETS) {
4393 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4394 }
4395
Joe Onorato9c1289c2009-08-17 11:03:03 -04004396 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4397
Joe Onorato9c1289c2009-08-17 11:03:03 -04004398 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004399 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004400
Adam Cohendcd297f2013-06-18 13:13:40 -07004401 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004402 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004403 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4404
Joe Onorato9c1289c2009-08-17 11:03:03 -04004405 workspace.requestLayout();
4406
Daniel Sandler843e8602010-06-07 14:59:01 -04004407 if (DEBUG_WIDGETS) {
4408 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4409 + (SystemClock.uptimeMillis()-start) + "ms");
4410 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004411 }
4412
Adam Cohen1462de32012-07-24 22:34:36 -07004413 public void onPageBoundSynchronously(int page) {
4414 mSynchronouslyBoundPages.add(page);
4415 }
4416
Joe Onorato9c1289c2009-08-17 11:03:03 -04004417 /**
4418 * Callback saying that there aren't any more items to bind.
4419 *
4420 * Implementation of the method from LauncherModel.Callbacks.
4421 */
Adam Cohene25af792013-06-06 23:08:25 -07004422 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004423 Runnable r = new Runnable() {
4424 public void run() {
4425 finishBindingItems(upgradePath);
4426 }
4427 };
4428 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004429 return;
4430 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004431 if (mSavedState != null) {
4432 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004433 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004434 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004435 mSavedState = null;
4436 }
4437
Adam Cohen1462de32012-07-24 22:34:36 -07004438 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004439
Winson Chungc51db6a2011-10-05 11:44:49 -07004440 // Update the market app icon as necessary (the other icons will be managed in response to
4441 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004442 if (!DISABLE_MARKET_BUTTON) {
4443 updateAppMarketIcon();
4444 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004445
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004446 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004447
4448 // If we received the result of any pending adds while the loader was running (e.g. the
4449 // widget configuration forced an orientation change), process them now.
4450 if (sPendingAddItem != null) {
4451 final long screenId = completeAdd(sPendingAddItem);
4452
4453 // TODO: this moves the user to the page where the pending item was added. Ideally,
4454 // the screen would be guaranteed to exist after bind, and the page would be set through
4455 // the workspace restore process.
4456 mWorkspace.post(new Runnable() {
4457 @Override
4458 public void run() {
4459 mWorkspace.snapToScreenId(screenId);
4460 }
4461 });
4462 sPendingAddItem = null;
4463 }
4464
Adam Cohene25af792013-06-06 23:08:25 -07004465 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004466 mWorkspace.getUniqueComponents(true, null);
4467 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004468 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004469 }
4470
Winson Chunga0b7e862013-09-05 16:03:15 -07004471 public boolean isAllAppsButtonRank(int rank) {
4472 if (mHotseat != null) {
4473 return mHotseat.isAllAppsButtonRank(rank);
4474 }
4475 return false;
4476 }
4477
Winson Chunga2413752012-04-03 14:22:34 -07004478 private boolean canRunNewAppsAnimation() {
4479 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4480 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4481 }
4482
Winson Chungc9168342013-06-26 14:54:55 -07004483 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4484 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4485 PropertyValuesHolder.ofFloat("alpha", 1f),
4486 PropertyValuesHolder.ofFloat("scaleX", 1f),
4487 PropertyValuesHolder.ofFloat("scaleY", 1f));
4488 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4489 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4490 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4491 return bounceAnim;
4492 }
4493
Adam Cohen27772732013-11-11 14:00:56 +00004494 public boolean useVerticalBarLayout() {
4495 return LauncherAppState.getInstance().getDynamicGrid().
4496 getDeviceProfile().isVerticalBarLayout();
4497 }
4498
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004499 protected Rect getSearchBarBounds() {
4500 return LauncherAppState.getInstance().getDynamicGrid().
4501 getDeviceProfile().getSearchBarBounds();
4502 }
4503
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004504 @Override
4505 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004506 boolean searchVisible = updateGlobalSearchIcon();
4507 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004508 if (mSearchDropTargetBar != null) {
4509 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4510 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004511 }
4512
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004513 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004514 * Add the icons for all apps.
4515 *
4516 * Implementation of the method from LauncherModel.Callbacks.
4517 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004518 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004519 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004520 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4521 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4522 getHotseat().addAllAppsFolder(mIconCache, apps,
4523 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4524 }
4525 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004526 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004527 if (mAppsCustomizeContent != null) {
4528 mAppsCustomizeContent.onPackagesUpdated(
4529 LauncherModel.getSortedWidgetsAndShortcuts(this));
4530 }
Winson Chungc58497e2013-09-03 17:48:37 -07004531 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004532 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004533 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004534 mAppsCustomizeContent.onPackagesUpdated(
4535 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004536 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004537 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004538 }
4539
4540 /**
4541 * A package was updated.
4542 *
4543 * Implementation of the method from LauncherModel.Callbacks.
4544 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004545 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004546 Runnable r = new Runnable() {
4547 public void run() {
4548 bindAppsUpdated(apps);
4549 }
4550 };
4551 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004552 return;
4553 }
4554
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004555 if (mWorkspace != null) {
4556 mWorkspace.updateShortcuts(apps);
4557 }
Winson Chungc58497e2013-09-03 17:48:37 -07004558
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004559 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004560 mAppsCustomizeContent != null) {
4561 mAppsCustomizeContent.updateApps(apps);
4562 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004563 }
4564
4565 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004566 * Update the state of a package, typically related to install state.
4567 *
4568 * Implementation of the method from LauncherModel.Callbacks.
4569 */
4570 public void updatePackageState(String pkgName, int state) {
4571 if (mWorkspace != null) {
4572 mWorkspace.updatePackageState(pkgName, state);
4573 }
4574 }
4575
4576 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004577 * A package was uninstalled. We take both the super set of packageNames
4578 * in addition to specific applications to remove, the reason being that
4579 * this can be called when a package is updated as well. In that scenario,
4580 * we only remove specific components from the workspace, where as
4581 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004582 *
4583 * Implementation of the method from LauncherModel.Callbacks.
4584 */
Winson Chung83892cc2013-05-01 16:53:33 -07004585 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Kenny Guyed131872014-04-30 03:02:21 +01004586 final ArrayList<AppInfo> appInfos, final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004587 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004588 public void run() {
Kenny Guyed131872014-04-30 03:02:21 +01004589 bindComponentsRemoved(packageNames, appInfos, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004590 }
Winson Chungd64d1762013-08-20 14:37:16 -07004591 };
4592 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004593 return;
4594 }
4595
Winson Chungdf95eb12013-10-16 14:57:07 -07004596 if (!packageNames.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004597 mWorkspace.removeItemsByPackageName(packageNames, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07004598 }
4599 if (!appInfos.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004600 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
Joe Onorato36115782010-06-17 13:28:48 -04004601 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004602
Winson Chunga1820962011-10-03 16:31:06 -07004603 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004604 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004605
Winson Chungdf95eb12013-10-16 14:57:07 -07004606 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004607 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004608 mAppsCustomizeContent != null) {
4609 mAppsCustomizeContent.removeApps(appInfos);
4610 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004611 }
Joe Onoratobe386092009-11-17 17:32:16 -08004612
4613 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004614 * A number of packages were updated.
4615 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004616 private ArrayList<Object> mWidgetsAndShortcuts;
4617 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004618 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004619 bindPackagesUpdated(mWidgetsAndShortcuts);
4620 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004621 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004622 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004623 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4624 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4625 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004626 return;
4627 }
4628
Winson Chung64359a52013-07-08 17:17:08 -07004629 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004630 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004631 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004632 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004633 }
4634
Winson Chung400438b2011-01-16 17:53:48 -08004635 private int mapConfigurationOriActivityInfoOri(int configOri) {
4636 final Display d = getWindowManager().getDefaultDisplay();
4637 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4638 switch (d.getRotation()) {
4639 case Surface.ROTATION_0:
4640 case Surface.ROTATION_180:
4641 // We are currently in the same basic orientation as the natural orientation
4642 naturalOri = configOri;
4643 break;
4644 case Surface.ROTATION_90:
4645 case Surface.ROTATION_270:
4646 // We are currently in the other basic orientation to the natural orientation
4647 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4648 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4649 break;
4650 }
4651
4652 int[] oriMap = {
4653 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4654 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4655 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4656 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4657 };
4658 // Since the map starts at portrait, we need to offset if this device's natural orientation
4659 // is landscape.
4660 int indexOffset = 0;
4661 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4662 indexOffset = 1;
4663 }
4664 return oriMap[(d.getRotation() + indexOffset) % 4];
4665 }
Adam Cohen446e9402011-09-15 18:21:21 -07004666
Winson Chung4b919f82012-05-01 10:44:08 -07004667 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004668 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004669 getResources().getBoolean(R.bool.allow_rotation);
4670 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004671 }
Winson Chung4b919f82012-05-01 10:44:08 -07004672 public void lockScreenOrientation() {
4673 if (isRotationEnabled()) {
4674 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4675 .getConfiguration().orientation));
4676 }
4677 }
4678 public void unlockScreenOrientation(boolean immediate) {
4679 if (isRotationEnabled()) {
4680 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004681 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004682 } else {
4683 mHandler.postDelayed(new Runnable() {
4684 public void run() {
4685 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4686 }
4687 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004688 }
Winson Chung4b919f82012-05-01 10:44:08 -07004689 }
Winson Chung400438b2011-01-16 17:53:48 -08004690 }
4691
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004692 /**
4693 * Called when the SearchBar hint should be changed.
4694 *
4695 * @param hint the hint to be displayed in the search bar.
4696 */
4697 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004698 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004699 }
4700
Winson Chunge43a1e72014-01-15 10:33:02 -08004701 protected boolean isLauncherPreinstalled() {
4702 PackageManager pm = getPackageManager();
4703 try {
4704 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4705 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4706 return true;
4707 } else {
4708 return false;
4709 }
4710 } catch (NameNotFoundException e) {
4711 e.printStackTrace();
4712 return false;
4713 }
4714 }
4715
Adam Cohenea90f832014-05-21 15:03:34 -07004716 /**
4717 * This method indicates whether or not we should suggest default wallpaper dimensions
4718 * when our wallpaper cropper was not yet used to set a wallpaper.
4719 */
4720 protected boolean overrideWallpaperDimensions() {
4721 return true;
4722 }
4723
Adam Cohen5eed5d82014-05-19 13:12:13 -07004724 protected boolean shouldClingFocusHotseatApp() {
4725 return false;
4726 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004727 protected String getFirstRunClingSearchBarHint() {
4728 return "";
4729 }
4730 protected String getFirstRunCustomContentHint() {
4731 return "";
4732 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004733 protected int getFirstRunFocusedHotseatAppDrawableId() {
4734 return -1;
4735 }
4736 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4737 return null;
4738 }
4739 protected int getFirstRunFocusedHotseatAppRank() {
4740 return -1;
4741 }
4742 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4743 return "";
4744 }
4745 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4746 return "";
4747 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004748
Winson Chungaf40f202013-09-18 18:26:31 -07004749 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004750 mLauncherClings.dismissFirstRunCling(v);
4751 }
4752 public void dismissMigrationClingCopyApps(View v) {
4753 mLauncherClings.dismissMigrationClingCopyApps(v);
4754 }
4755 public void dismissMigrationClingUseDefault(View v) {
4756 mLauncherClings.dismissMigrationClingUseDefault(v);
4757 }
4758 public void dismissMigrationWorkspaceCling(View v) {
4759 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004760 }
Winson Chung82f55532011-08-09 14:14:23 -07004761 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004762 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004763 }
4764 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004765 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004766 }
Adam Cohen6268f2d2014-05-16 16:42:35 -07004767 public void markFolderClingDismissedIfNecessary() {
4768 mLauncherClings.markFolderClingDismissedIfNecessary();
4769 }
Adam Cohen432609a2014-03-13 17:03:22 -07004770
4771 /**
4772 * To be overridden by subclasses to indicate that there is an activity to launch
4773 * before showing the standard launcher experience.
4774 */
4775 protected boolean hasFirstRunActivity() {
4776 return false;
4777 }
4778
4779 /**
4780 * To be overridden by subclasses to launch any first run activity
4781 */
4782 protected Intent getFirstRunActivity() {
4783 return null;
4784 }
4785
Winson Chunga6945242014-01-08 14:04:34 -08004786 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004787 return !ActivityManager.isRunningInTestHarness() &&
4788 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004789 }
4790
Adam Cohen4a9423d2014-03-28 14:22:31 -07004791 protected boolean hasRunFirstRunActivity() {
4792 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4793 }
4794
Adam Cohen432609a2014-03-13 17:03:22 -07004795 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004796 if (shouldRunFirstRunActivity() &&
4797 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004798 Intent firstRunIntent = getFirstRunActivity();
4799 if (firstRunIntent != null) {
4800 startActivity(firstRunIntent);
4801 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004802 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004803 }
4804 }
Adam Cohen432609a2014-03-13 17:03:22 -07004805 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004806 }
4807
4808 private void markFirstRunActivityShown() {
4809 SharedPreferences.Editor editor = mSharedPrefs.edit();
4810 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4811 editor.apply();
4812 }
4813
Adam Cohen432609a2014-03-13 17:03:22 -07004814 /**
4815 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4816 * screen that must be displayed and dismissed.
4817 */
4818 protected boolean hasDismissableIntroScreen() {
4819 return false;
4820 }
4821
4822 /**
4823 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4824 */
4825 protected View getIntroScreen() {
4826 return null;
4827 }
4828
4829 /**
4830 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4831 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4832 */
4833 private boolean shouldShowIntroScreen() {
4834 return hasDismissableIntroScreen() &&
4835 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4836 }
4837
4838 protected void showIntroScreen() {
4839 View introScreen = getIntroScreen();
4840 changeWallpaperVisiblity(false);
4841 if (introScreen != null) {
4842 mDragLayer.showOverlayView(introScreen);
4843 }
4844 }
4845
4846 public void dismissIntroScreen() {
4847 markIntroScreenDismissed();
4848 if (showFirstRunActivity()) {
4849 // We delay hiding the intro view until the first run activity is showing. This
4850 // avoids a blip.
4851 mWorkspace.postDelayed(new Runnable() {
4852 @Override
4853 public void run() {
4854 mDragLayer.dismissOverlayView();
4855 }
4856 }, ACTIVITY_START_DELAY);
4857 } else {
4858 mDragLayer.dismissOverlayView();
4859 }
4860 changeWallpaperVisiblity(true);
4861 }
4862
4863 private void markIntroScreenDismissed() {
4864 SharedPreferences.Editor editor = mSharedPrefs.edit();
4865 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4866 editor.apply();
4867 }
4868
Winson Chunga6945242014-01-08 14:04:34 -08004869 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004870 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4871 if (mHotseat != null) mHotseat.setAlpha(1f);
4872 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4873 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004874 }
4875
4876 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004877 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4878 if (mHotseat != null) mHotseat.setAlpha(0f);
4879 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4880 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004881 }
4882
Mathew Inwood72fbec12013-11-19 15:45:07 +00004883 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004884 // Called from search suggestion, not supported in other profiles.
4885 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4886 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4887 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4888 myUser);
4889 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004890 return null;
4891 }
Kenny Guyed131872014-04-30 03:02:21 +01004892 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004893 }
4894
4895 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4896 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004897 // Called from search suggestion, not supported in other profiles.
4898 return createShortcutDragInfo(shortcutIntent, caption, icon,
4899 UserHandleCompat.myUserHandle());
4900 }
4901
4902 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4903 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004904 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
4905 String contentDescription = userManager.getBadgedLabelForUser(caption.toString(), user);
4906 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004907 }
4908
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004909 protected void moveWorkspaceToDefaultScreen() {
4910 mWorkspace.moveToDefaultScreen(false);
4911 }
4912
Mathew Inwood72fbec12013-11-19 15:45:07 +00004913 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4914 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004915 mWorkspace.onExternalDragStartedWithItem(dragView);
4916 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004917 }
4918
Anjali Koppalf5d29132014-02-28 13:33:27 -08004919 @Override
4920 public void onPageSwitch(View newPage, int newPageIndex) {
4921 }
4922
Winson Chung80baf5a2010-08-09 16:03:15 -07004923 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004924 * Prints out out state for debugging.
4925 */
4926 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004927 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004928 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004929 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4930 Log.d(TAG, "mRestoring=" + mRestoring);
4931 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4932 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004933 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004934 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004935
Winson Chung785d2eb2011-04-14 16:08:02 -07004936 if (mAppsCustomizeContent != null) {
4937 mAppsCustomizeContent.dumpState();
4938 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004939 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004940 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004941
4942 @Override
4943 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4944 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004945 synchronized (sDumpLogs) {
4946 writer.println(" ");
4947 writer.println("Debug logs: ");
4948 for (int i = 0; i < sDumpLogs.size(); i++) {
4949 writer.println(" " + sDumpLogs.get(i));
4950 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004951 }
4952 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004953
4954 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004955 if (DEBUG_DUMP_LOG) {
4956 synchronized (sDumpLogs) {
4957 Log.d(TAG, "");
4958 Log.d(TAG, "*********************");
4959 Log.d(TAG, "Launcher debug logs: ");
4960 for (int i = 0; i < sDumpLogs.size(); i++) {
4961 Log.d(TAG, " " + sDumpLogs.get(i));
4962 }
4963 Log.d(TAG, "*********************");
4964 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004965 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004966 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004967 }
4968
4969 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004970 addDumpLog(tag, log, null, debugLog);
4971 }
4972
4973 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004974 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004975 if (e != null) {
4976 Log.d(tag, log, e);
4977 } else {
4978 Log.d(tag, log);
4979 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004980 }
Winson Chungede41292013-09-19 16:27:36 -07004981 if (DEBUG_DUMP_LOG) {
4982 sDateStamp.setTime(System.currentTimeMillis());
4983 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004984 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4985 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004986 }
Winson Chungede41292013-09-19 16:27:36 -07004987 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004988 }
4989
Winson Chungede41292013-09-19 16:27:36 -07004990 public void dumpLogsToLocalData() {
4991 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004992 new AsyncTask<Void, Void, Void>() {
4993 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004994 boolean success = false;
4995 sDateStamp.setTime(sRunStart);
4996 String FILENAME = sDateStamp.getMonth() + "-"
4997 + sDateStamp.getDay() + "_"
4998 + sDateStamp.getHours() + "-"
4999 + sDateStamp.getMinutes() + "_"
5000 + sDateStamp.getSeconds() + ".txt";
5001
5002 FileOutputStream fos = null;
5003 File outFile = null;
5004 try {
5005 outFile = new File(getFilesDir(), FILENAME);
5006 outFile.createNewFile();
5007 fos = new FileOutputStream(outFile);
5008 } catch (Exception e) {
5009 e.printStackTrace();
5010 }
5011 if (fos != null) {
5012 PrintWriter writer = new PrintWriter(fos);
5013
5014 writer.println(" ");
5015 writer.println("Debug logs: ");
5016 synchronized (sDumpLogs) {
5017 for (int i = 0; i < sDumpLogs.size(); i++) {
5018 writer.println(" " + sDumpLogs.get(i));
5019 }
5020 }
5021 writer.close();
5022 }
5023 try {
5024 if (fos != null) {
5025 fos.close();
5026 success = true;
5027 }
5028 } catch (IOException e) {
5029 e.printStackTrace();
5030 }
Michael Jurka43467462013-11-14 12:03:58 +01005031 return null;
Winson Chungede41292013-09-19 16:27:36 -07005032 }
Michael Jurka43467462013-11-14 12:03:58 +01005033 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005034 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005035 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005036}
Michael Jurka2763be32011-02-24 11:19:57 -08005037
Michael Jurkaabded662011-03-04 12:06:57 -08005038interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005039 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005040 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005041 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005042 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005043 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005044}
Dan Sandlerd5024042014-01-09 15:01:33 -05005045
5046interface DebugIntents {
5047 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5048 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005049}