blob: 9ad3917facae8c78daaf55a0b56bcf504a47b3bf [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
Joe Onorato9c1289c2009-08-17 11:03:03 -0400225 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700226 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
227 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700228 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800229
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800230 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800231 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000233 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
234 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
235
Winson Chunga2413752012-04-03 14:22:34 -0700236 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700237 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
238 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700239 private static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700240
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800241 private final BroadcastReceiver mCloseSystemDialogsReceiver
242 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800243 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
244
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800245 private LayoutInflater mInflater;
246
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800247 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700248 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800249 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800250 private DragLayer mDragLayer;
251 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700252 private View mWeightWatcher;
Winson Chunga6945242014-01-08 14:04:34 -0800253 private LauncherClings mLauncherClings;
Romain Guycbb89e42009-06-08 15:52:54 -0700254
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700255 private AppWidgetManager mAppWidgetManager;
256 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700257
Michael Jurkac9d95c52011-08-29 14:03:34 -0700258 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700259 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800260 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700261
Michael Jurka0280c3b2010-09-17 15:00:07 -0700262 private int[] mTmpAddItemCellCoordinates = new int[2];
263
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800264 private FolderInfo mFolderInfo;
265
Winson Chung3d503fb2011-07-13 17:25:49 -0700266 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800267 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700268
Michael Jurka838a4ca2011-02-07 13:33:06 -0800269 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700270
Winson Chungc51db6a2011-10-05 11:44:49 -0700271 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700272 private AppsCustomizeTabHost mAppsCustomizeTabHost;
273 private AppsCustomizePagedView mAppsCustomizeContent;
274 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800275 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800276
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800277 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700278 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
279 // scroll issues (because the workspace may not have been measured yet) and extra work.
280 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
281 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282
283 private SpannableStringBuilder mDefaultKeySsb = null;
284
Joe Onorato9c1289c2009-08-17 11:03:03 -0400285 private boolean mWorkspaceLoading = true;
286
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800287 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800288 private boolean mRestoring;
289 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700290 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291
Michael Jurka1e2f4652013-07-08 18:03:46 -0700292 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700293 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
294
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295 private Bundle mSavedInstanceState;
296
Joe Onorato9c1289c2009-08-17 11:03:03 -0400297 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800298 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800299 private boolean mUserPresent = true;
300 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700301 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800302 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400303
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700304 private static LocaleConfiguration sLocaleConfiguration = null;
305
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700306 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700307
Adam Cohen61f560d2013-09-30 15:58:20 -0700308 private View.OnTouchListener mHapticFeedbackTouchListener;
309
Adam Cohended9f8d2010-11-03 13:25:16 -0700310 // Related to the auto-advancing of widgets
311 private final int ADVANCE_MSG = 1;
312 private final int mAdvanceInterval = 20000;
313 private final int mAdvanceStagger = 250;
314 private long mAutoAdvanceSentTime;
315 private long mAutoAdvanceTimeLeft = -1;
316 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
317 new HashMap<View, AppWidgetProviderInfo>();
318
Winson Chung400438b2011-01-16 17:53:48 -0800319 // Determines how long to wait after a rotation before restoring the screen orientation to
320 // match the sensor state.
321 private final int mRestoreScreenOrientationDelay = 500;
322
Michael Jurka4ef207b2010-11-29 17:05:45 -0800323 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700324 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
325 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
326 private static Drawable.ConstantState[] sAppMarketIcon = new Drawable.ConstantState[2];
Michael Jurka4ef207b2010-11-29 17:05:45 -0800327
Winson Chungd64a6662013-09-30 11:06:59 -0700328 private Intent mAppMarketIntent = null;
329 private static final boolean DISABLE_MARKET_BUTTON = true;
330
Craig Mautner360310b2012-10-26 15:13:08 -0700331 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700332
Adam Cohen1462de32012-07-24 22:34:36 -0700333 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700334 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700335
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700336 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700337 static Date sDateStamp = new Date();
338 static DateFormat sDateFormat =
339 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
340 static long sRunStart = System.currentTimeMillis();
341 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700342
Winson Chung46353de2012-02-16 14:05:10 -0800343 // We only want to get the SharedPreferences once since it does an FS stat each time we get
344 // it from the context.
345 private SharedPreferences mSharedPrefs;
346
Adam Cohene25af792013-06-06 23:08:25 -0700347 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
348
Winson Chungf0c6ae02012-03-21 16:10:31 -0700349 // Holds the page that we need to animate to, and the icon views that we need to animate up
350 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700351 private ImageView mFolderIconImageView;
352 private Bitmap mFolderIconBitmap;
353 private Canvas mFolderIconCanvas;
354 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700355
Michael Jurkaddd62e92011-02-16 17:49:14 -0800356 private BubbleTextView mWaitingForResume;
357
Michael Jurkac1f5d262011-09-30 19:32:27 -0700358 private Runnable mBuildLayersRunnable = new Runnable() {
359 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700360 if (mWorkspace != null) {
361 mWorkspace.buildPageHardwareLayers();
362 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700363 }
364 };
365
Adam Cohendb364c32014-05-20 14:23:40 -0700366 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800367
Michael Jurka7267fa52013-09-26 15:29:57 -0700368 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800369
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800370 private static class PendingAddArguments {
371 int requestCode;
372 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700373 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700374 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800375 int cellX;
376 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700377 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800378 }
379
Daniel Sandlerff02d492013-08-05 02:12:05 -0400380 private Stats mStats;
381
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800382 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800383 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700384 }
385
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800386 @Override
387 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700388 if (DEBUG_STRICT_MODE) {
389 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
390 .detectDiskReads()
391 .detectDiskWrites()
392 .detectNetwork() // or .detectAll() for all detectable problems
393 .penaltyLog()
394 .build());
395 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
396 .detectLeakedSqlLiteObjects()
397 .detectLeakedClosableObjects()
398 .penaltyLog()
399 .penaltyDeath()
400 .build());
401 }
402
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800403 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400404
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400405 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400406 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700407 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700408 // Determine the dynamic grid properties
409 Point smallestSize = new Point();
410 Point largestSize = new Point();
411 Point realSize = new Point();
412 Display display = getWindowManager().getDefaultDisplay();
413 display.getCurrentSizeRange(smallestSize, largestSize);
414 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700415 DisplayMetrics dm = new DisplayMetrics();
416 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700417
Winson Chung5f8afe62013-08-12 16:19:28 -0700418 // Lazy-initialize the dynamic grid
419 DeviceProfile grid = app.initDynamicGrid(this,
420 Math.min(smallestSize.x, smallestSize.y),
421 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700422 realSize.x, realSize.y,
423 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700424
425 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400426 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700427 Context.MODE_PRIVATE);
Joe Onorato0589f0f2010-02-08 13:44:00 -0800428 mModel = app.setLauncher(this);
429 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700430 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400431 mDragController = new DragController(this);
Winson Chunga6945242014-01-08 14:04:34 -0800432 mLauncherClings = new LauncherClings(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700434
Daniel Sandlerff02d492013-08-05 02:12:05 -0400435 mStats = new Stats(this);
436
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700437 mAppWidgetManager = AppWidgetManager.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700438
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700439 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
440 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700441
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700442 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
443 // this also ensures that any synchronous binding below doesn't re-trigger another
444 // LauncherModel load.
445 mPaused = false;
446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200448 android.os.Debug.startMethodTracing(
449 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800450 }
451
Adam Cohen53805212013-10-01 10:39:23 -0700452
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800453 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700455
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100457 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800458
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800459 registerContentObservers();
460
Joe Onorato7c312c12009-08-13 21:36:53 -0700461 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700462
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800463 mSavedState = savedInstanceState;
464 restoreState(mSavedState);
465
466 if (PROFILE_STARTUP) {
467 android.os.Debug.stopMethodTracing();
468 }
469
470 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700471 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700472 // If the user leaves launcher, then we should just load items asynchronously when
473 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500474 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700475 } else {
476 // We only load the page synchronously if the user rotates (or triggers a
477 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800478 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700479 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 }
481
482 // For handling default keys
483 mDefaultKeySsb = new SpannableStringBuilder();
484 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800485
486 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
487 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800488
Adam Cohenf9426d52012-06-04 17:26:21 -0700489 updateGlobalIcons();
490
491 // On large interfaces, we want the screen to auto-rotate based on the current orientation
492 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700493
Adam Cohen432609a2014-03-13 17:03:22 -0700494 if (shouldShowIntroScreen()) {
495 showIntroScreen();
496 } else {
497 showFirstRunActivity();
498 }
499
Winson Chunge43a1e72014-01-15 10:33:02 -0800500 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
501 // on the device, then we always show the first run cling experience (or if there is no
502 // launcher2). Otherwise, we prompt the user upon started for migration
Winson Chunge43a1e72014-01-15 10:33:02 -0800503 if (mLauncherClings.shouldShowFirstRunOrMigrationClings()) {
504 if (mModel.canMigrateFromOldLauncherDb(this)) {
505 mLauncherClings.showMigrationCling();
506 } else {
507 mLauncherClings.showFirstRunCling();
508 }
Winson Chunga6945242014-01-08 14:04:34 -0800509 } else {
Winson Chunge43a1e72014-01-15 10:33:02 -0800510 mLauncherClings.removeFirstRunAndMigrationClings();
Winson Chunga6945242014-01-08 14:04:34 -0800511 }
Adam Cohenf9426d52012-06-04 17:26:21 -0700512 }
513
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700514 @Override
515 public void onLauncherProviderChange() { }
516
Winson Chung98ca0f72013-07-29 12:58:51 -0700517 /** To be overriden by subclasses to hint to Launcher that we have custom content */
518 protected boolean hasCustomContentToLeft() {
519 return false;
520 }
521
Dave Hawkeya8881582013-09-17 15:55:33 -0600522 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500523 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600524 * {@link #addToCustomContentPage}. This will only be invoked if
525 * {@link #hasCustomContentToLeft()} is {@code true}.
526 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500527 protected void populateCustomContentContainer() {
Dave Hawkeya8881582013-09-17 15:55:33 -0600528 }
529
530 /**
531 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
532 * ensure the custom content page is added or removed if necessary.
533 */
534 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600535 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600536 // Not bound yet, wait for bindScreens to be called.
537 return;
538 }
539
540 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
541 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500542 mWorkspace.createCustomContentContainer();
543 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600544 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
545 mWorkspace.removeCustomContentPage();
546 }
547 }
548
Adam Cohenf9426d52012-06-04 17:26:21 -0700549 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700550 boolean searchVisible = false;
551 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800552 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700553 int coi = getCurrentOrientationIndexForGlobalIcons();
554 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null ||
555 sAppMarketIcon[coi] == null) {
Winson Chungd64a6662013-09-30 11:06:59 -0700556 if (!DISABLE_MARKET_BUTTON) {
557 updateAppMarketIcon();
558 }
Winson Chungc51db6a2011-10-05 11:44:49 -0700559 searchVisible = updateGlobalSearchIcon();
560 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700561 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700562 if (sGlobalSearchIcon[coi] != null) {
563 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700564 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700565 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700566 if (sVoiceSearchIcon[coi] != null) {
567 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700568 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700569 }
Winson Chungd64a6662013-09-30 11:06:59 -0700570 if (!DISABLE_MARKET_BUTTON && sAppMarketIcon[coi] != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -0700571 updateAppMarketIcon(sAppMarketIcon[coi]);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800572 }
Winson Chungadf0c182012-08-23 14:59:07 -0700573 if (mSearchDropTargetBar != null) {
574 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
575 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800576 }
Romain Guycbb89e42009-06-08 15:52:54 -0700577
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800578 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700579 if (sLocaleConfiguration == null) {
580 new AsyncTask<Void, Void, LocaleConfiguration>() {
581 @Override
582 protected LocaleConfiguration doInBackground(Void... unused) {
583 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
584 readConfiguration(Launcher.this, localeConfiguration);
585 return localeConfiguration;
586 }
587
588 @Override
589 protected void onPostExecute(LocaleConfiguration result) {
590 sLocaleConfiguration = result;
591 checkForLocaleChange(); // recursive, but now with a locale configuration
592 }
593 }.execute();
594 return;
595 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700596
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800597 final Configuration configuration = getResources().getConfiguration();
598
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700599 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800600 final String locale = configuration.locale.toString();
601
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700602 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800603 final int mcc = configuration.mcc;
604
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700605 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800606 final int mnc = configuration.mnc;
607
Romain Guy84f296c2009-11-04 15:00:44 -0800608 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800609
Romain Guy84f296c2009-11-04 15:00:44 -0800610 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700611 sLocaleConfiguration.locale = locale;
612 sLocaleConfiguration.mcc = mcc;
613 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700614
Joe Onorato0589f0f2010-02-08 13:44:00 -0800615 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700616
617 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100618 new AsyncTask<Void, Void, Void>() {
619 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700620 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100621 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700622 }
Michael Jurka43467462013-11-14 12:03:58 +0100623 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700624 }
625 }
626
627 private static class LocaleConfiguration {
628 public String locale;
629 public int mcc = -1;
630 public int mnc = -1;
631 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700632
Romain Guy98d01652009-06-30 16:21:04 -0700633 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
634 DataInputStream in = null;
635 try {
636 in = new DataInputStream(context.openFileInput(PREFERENCES));
637 configuration.locale = in.readUTF();
638 configuration.mcc = in.readInt();
639 configuration.mnc = in.readInt();
640 } catch (FileNotFoundException e) {
641 // Ignore
642 } catch (IOException e) {
643 // Ignore
644 } finally {
645 if (in != null) {
646 try {
647 in.close();
648 } catch (IOException e) {
649 // Ignore
650 }
651 }
652 }
653 }
654
655 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
656 DataOutputStream out = null;
657 try {
658 out = new DataOutputStream(context.openFileOutput(PREFERENCES, MODE_PRIVATE));
659 out.writeUTF(configuration.locale);
660 out.writeInt(configuration.mcc);
661 out.writeInt(configuration.mnc);
662 out.flush();
663 } catch (FileNotFoundException e) {
664 // Ignore
665 } catch (IOException e) {
666 //noinspection ResultOfMethodCallIgnored
667 context.getFileStreamPath(PREFERENCES).delete();
668 } finally {
669 if (out != null) {
670 try {
671 out.close();
672 } catch (IOException e) {
673 // Ignore
674 }
675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800676 }
677 }
678
Anton Hanssona2f665f2013-09-26 12:18:32 +0100679 public Stats getStats() {
680 return mStats;
681 }
682
Bjorn Bringertc459e522013-06-07 19:36:01 +0100683 public LayoutInflater getInflater() {
684 return mInflater;
685 }
686
Winson Chung36a62fe2012-05-06 18:04:42 -0700687 boolean isDraggingEnabled() {
688 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
689 // that is subsequently removed from the workspace in startBinding().
690 return !mModel.isLoadingWorkspace();
691 }
692
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800693 static int getScreen() {
694 synchronized (sLock) {
695 return sScreen;
696 }
697 }
698
699 static void setScreen(int screen) {
700 synchronized (sLock) {
701 sScreen = screen;
702 }
703 }
704
Winson Chung557d6ed2011-07-08 15:34:52 -0700705 /**
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000706 * Copied from View -- the View version of the method isn't called
707 * anywhere else in our process and only exists for API level 17+,
708 * so it's ok to keep our own version with no API requirement.
709 */
710 public static int generateViewId() {
711 for (;;) {
712 final int result = sNextGeneratedId.get();
713 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
714 int newValue = result + 1;
715 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
716 if (sNextGeneratedId.compareAndSet(result, newValue)) {
717 return result;
718 }
719 }
720 }
721
722 public int getViewIdForItem(ItemInfo info) {
723 // This cast is safe given the > 2B range for int.
724 int itemId = (int) info.id;
725 if (mItemIdToViewId.containsKey(itemId)) {
726 return mItemIdToViewId.get(itemId);
727 }
728 int viewId = generateViewId();
729 mItemIdToViewId.put(itemId, viewId);
730 return viewId;
731 }
732
733 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700734 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
735 * a configuration step, this allows the proper animations to run after other transitions.
736 */
Adam Cohendb364c32014-05-20 14:23:40 -0700737 private long completeAdd(PendingAddArguments args) {
738
739 long screenId = ensurePendingDropLayoutExists(args.screenId);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800740 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800741 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700742 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700743 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700744 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800745 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700746 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700747 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800748 }
Michael Jurka27614d22012-04-02 06:40:22 -0700749 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
750 // if you turned the screen off and then back while in All Apps, Launcher would not
751 // return to the workspace. Clearing mAddInfo.container here fixes this issue
752 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700753 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800754 }
755
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800756 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700757 protected void onActivityResult(
758 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700759 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700760 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700761 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800762 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700763
Adam Cohenad4e15c2013-10-17 16:21:35 -0700764 Runnable exitSpringLoaded = new Runnable() {
765 @Override
766 public void run() {
767 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
768 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
769 }
770 };
771
Michael Jurka8b805b12012-04-18 14:23:14 -0700772 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700773 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700774 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
775 if (resultCode == RESULT_CANCELED) {
776 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700777 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700778 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700779 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700780 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
781 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700782 }
783 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200784 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
785 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
786 mWorkspace.exitOverviewMode(false);
787 }
788 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700789 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200790
Adam Cohened66b2b2012-01-23 17:28:51 -0800791 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
792 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700793
Adam Cohendb364c32014-05-20 14:23:40 -0700794 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800795 // We have special handling for widgets
796 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800797 final int appWidgetId;
798 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
799 : -1;
800 if (widgetId < 0) {
801 appWidgetId = pendingAddWidgetId;
802 } else {
803 appWidgetId = widgetId;
804 }
805
Adam Cohenad4e15c2013-10-17 16:21:35 -0700806 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800807 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700808 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
809 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700810 result = RESULT_CANCELED;
811 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700812 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700813 @Override
814 public void run() {
815 exitSpringLoadedDragModeDelayed(false, 0, null);
816 }
817 };
Adam Cohendb364c32014-05-20 14:23:40 -0700818 if (workspaceLocked) {
819 // No need to remove the empty screen if we're mid-binding, as the
820 // the bind will not add the empty screen.
821 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
822 } else {
823 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
824 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
825 }
Winson Chung5aaab772012-04-27 15:24:02 -0700826 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700827 if (!workspaceLocked) {
828 mPendingAddInfo.screenId = ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
829 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
830
831 dropLayout.setDropPending(true);
832 final Runnable onComplete = new Runnable() {
833 @Override
834 public void run() {
835 completeTwoStageWidgetDrop(resultCode, appWidgetId);
836 dropLayout.setDropPending(false);
837 }
838 };
839 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
840 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
841 } else {
842 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
843 mPendingAddInfo);
844 sPendingAddItem = args;
845 }
Winson Chung5aaab772012-04-27 15:24:02 -0700846 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800847 return;
848 }
849
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800850 // The pattern used here is that a user PICKs a specific application,
851 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700852
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
854 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700855 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700856 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
857 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800858 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700859 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800860 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700861 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700862 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
863 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800864 }
Adam Cohen00074722013-10-11 17:46:09 -0700865 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700866 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700867 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800868 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800869 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800870 }
871
Adam Cohendb364c32014-05-20 14:23:40 -0700872 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
873 appWidgetId, ItemInfo info) {
874 PendingAddArguments args = new PendingAddArguments();
875 args.requestCode = requestCode;
876 args.intent = data;
877 args.container = info.container;
878 args.screenId = info.screenId;
879 args.cellX = info.cellX;
880 args.cellY = info.cellY;
881 args.appWidgetId = appWidgetId;
882 return args;
883 }
884
885 /**
886 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
887 *
888 * @param screenId the screen id to check
889 * @return the new screen, or screenId if it exists
890 */
891 private long ensurePendingDropLayoutExists(long screenId) {
892 CellLayout dropLayout =
893 (CellLayout) mWorkspace.getScreenWithId(screenId);
894 if (dropLayout == null) {
895 // it's possible that the add screen was removed because it was
896 // empty and a re-bind occurred
897 mWorkspace.addExtraEmptyScreen();
898 return mWorkspace.commitExtraEmptyScreen();
899 } else {
900 return screenId;
901 }
902 }
903
Adam Cohened66b2b2012-01-23 17:28:51 -0800904 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700905 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700906 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800907 Runnable onCompleteRunnable = null;
908 int animationType = 0;
909
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700910 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800911 if (resultCode == RESULT_OK) {
912 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
913 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700914 mPendingAddWidgetInfo);
915 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800916 onCompleteRunnable = new Runnable() {
917 @Override
918 public void run() {
919 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700920 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700921 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
922 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800923 }
924 };
925 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800926 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800927 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800928 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700929 if (mDragLayer.getAnimatedView() != null) {
930 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
931 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
932 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700933 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700934 // The animated view may be null in the case of a rotation during widget configuration
935 onCompleteRunnable.run();
936 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800937 }
938
939 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700940 protected void onStop() {
941 super.onStop();
942 FirstFrameAnimatorHelper.setIsVisible(false);
943 }
944
945 @Override
946 protected void onStart() {
947 super.onStart();
948 FirstFrameAnimatorHelper.setIsVisible(true);
949 }
950
951 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200953 long startTime = 0;
954 if (DEBUG_RESUME_TIME) {
955 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400956 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800958 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700959
Winson Chung4a2afa32012-07-19 14:53:05 -0700960 // Restore the previous launcher state
961 if (mOnResumeState == State.WORKSPACE) {
962 showWorkspace(false);
963 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800964 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700965 }
966 mOnResumeState = State.NONE;
967
Craig Mautner360310b2012-10-26 15:13:08 -0700968 // Background was set to gradient in onPause(), restore to black if in all apps.
969 setWorkspaceBackground(mState == State.WORKSPACE);
970
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800971 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700972 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700973 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -0500974 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -0400975 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700976 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700978 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +0200979 // We might have postponed some bind calls until onResume (see waitUntilResume) --
980 // execute them here
981 long startTimeCallbacks = 0;
982 if (DEBUG_RESUME_TIME) {
983 startTimeCallbacks = System.currentTimeMillis();
984 }
985
986 if (mAppsCustomizeContent != null) {
987 mAppsCustomizeContent.setBulkBind(true);
988 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700989 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
990 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +0200991 }
992 if (mAppsCustomizeContent != null) {
993 mAppsCustomizeContent.setBulkBind(false);
994 }
Michael Jurka1e2f4652013-07-08 18:03:46 -0700995 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +0200996 if (DEBUG_RESUME_TIME) {
997 Log.d(TAG, "Time spent processing callbacks in onResume: " +
998 (System.currentTimeMillis() - startTimeCallbacks));
999 }
Michael Jurka447bf842013-05-15 14:52:15 +02001000 }
Michael Jurka54554252013-08-01 12:52:23 +02001001 if (mOnResumeCallbacks.size() > 0) {
1002 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1003 mOnResumeCallbacks.get(i).run();
1004 }
1005 mOnResumeCallbacks.clear();
1006 }
Winson Chunge4e50662012-01-23 14:45:13 -08001007
1008 // Reset the pressed state of icons that were locked in the press state while activities
1009 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001010 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001011 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001012 mWaitingForResume.setStayPressed(false);
1013 }
Winson Chunge4e50662012-01-23 14:45:13 -08001014 if (mAppsCustomizeContent != null) {
1015 // Resets the previous all apps icon press state
1016 mAppsCustomizeContent.resetDrawableState();
1017 }
Winson Chung82963d52013-10-09 11:20:57 -07001018
Adam Cohen06dff352012-06-01 17:17:08 -07001019 // It is possible that widgets can receive updates while launcher is not in the foreground.
1020 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1021 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1022 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001023 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001024
Winson Chung780fe592013-09-26 14:48:44 -07001025 // Process any items that were added while Launcher was away.
1026 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1027
Winson Chung5841efa2013-09-30 18:06:44 -07001028 // Update the voice search button proxy
1029 updateVoiceButtonProxyVisible(false);
1030
Adam Cohenf9426d52012-06-04 17:26:21 -07001031 // Again, as with the above scenario, it's possible that one or more of the global icons
1032 // were updated in the wrong orientation.
1033 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001034 if (DEBUG_RESUME_TIME) {
1035 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1036 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001037
1038 if (mWorkspace.getCustomContentCallbacks() != null) {
1039 // If we are resuming and the custom content is the current page, we call onShow().
1040 // It is also poassible that onShow will instead be called slightly after first layout
1041 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1042 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001043 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001044 }
1045 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001046 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001047 mWorkspace.onResume();
Adam Cohen06dff352012-06-01 17:17:08 -07001048 }
1049
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001050 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001051 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001052 // Ensure that items added to Launcher are queued until Launcher returns
1053 InstallShortcutReceiver.enableInstallQueue();
1054
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001055 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001056 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001057 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001058 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001059
1060 // We call onHide() aggressively. The custom content callbacks should be able to
1061 // debounce excess onHide calls.
1062 if (mWorkspace.getCustomContentCallbacks() != null) {
1063 mWorkspace.getCustomContentCallbacks().onHide();
1064 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001065 }
Romain Guycbb89e42009-06-08 15:52:54 -07001066
Adam Cohenbffe7452013-07-22 18:21:45 -07001067 QSBScroller mQsbScroller = new QSBScroller() {
1068 int scrollY = 0;
1069
1070 @Override
1071 public void setScrollY(int scroll) {
1072 scrollY = scroll;
1073
1074 if (mWorkspace.isOnOrMovingToCustomContent()) {
1075 mSearchDropTargetBar.setTranslationY(- scrollY);
Mark Brophyd7dc6812013-09-20 17:27:32 +01001076 getQsbBar().setTranslationY(-scrollY);
Adam Cohenbffe7452013-07-22 18:21:45 -07001077 }
1078 }
1079 };
1080
1081 public void resetQSBScroll() {
1082 mSearchDropTargetBar.animate().translationY(0).start();
Mark Brophyd7dc6812013-09-20 17:27:32 +01001083 getQsbBar().animate().translationY(0).start();
Adam Cohenbffe7452013-07-22 18:21:45 -07001084 }
1085
1086 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001087 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1088 // by a onResume or by scrolling otherwise.
1089 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001090
1091 // Custom content is completely hidden
1092 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001093
1094 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1095 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001096
1097 // Indicates whether the user is allowed to scroll away from the custom content.
1098 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001099 }
1100
Jorim Jaggid017f882014-01-14 17:08:48 -08001101 protected boolean hasSettings() {
1102 return false;
1103 }
1104
Adam Cohenbffe7452013-07-22 18:21:45 -07001105 public interface QSBScroller {
1106 public void setScrollY(int scrollY);
1107 }
1108
Winson Chung98ca0f72013-07-29 12:58:51 -07001109 public QSBScroller addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001110 CustomContentCallbacks callbacks, String description) {
1111 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohenbffe7452013-07-22 18:21:45 -07001112 return mQsbScroller;
Adam Cohen66a01fd2013-06-11 12:48:00 -07001113 }
1114
Adam Cohenedb40762013-07-18 16:45:45 -07001115 // The custom content needs to offset its content to account for the QSB
1116 public int getTopOffsetForCustomContent() {
1117 return mWorkspace.getPaddingTop();
1118 }
1119
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001120 @Override
1121 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001122 // Flag the loader to stop early before switching
1123 mModel.stopLoader();
Winson Chung785d2eb2011-04-14 16:08:02 -07001124 if (mAppsCustomizeContent != null) {
1125 mAppsCustomizeContent.surrender();
1126 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001127 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001128 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001129
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001130 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001131 @Override
1132 public void onWindowFocusChanged(boolean hasFocus) {
1133 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001134 mHasFocus = hasFocus;
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001135 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001136
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001137 private boolean acceptFilter() {
1138 final InputMethodManager inputManager = (InputMethodManager)
1139 getSystemService(Context.INPUT_METHOD_SERVICE);
1140 return !inputManager.isFullscreenMode();
1141 }
1142
1143 @Override
1144 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001145 final int uniChar = event.getUnicodeChar();
1146 final boolean handled = super.onKeyDown(keyCode, event);
1147 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1148 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001149 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1150 keyCode, event);
1151 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001152 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001153 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001154 // showSearchDialog()
1155 // If there are multiple keystrokes before the search dialog takes focus,
1156 // onSearchRequested() will be called for every keystroke,
1157 // but it is idempotent, so it's fine.
1158 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001159 }
1160 }
1161
Joe Onorato8a9625e2010-01-28 15:55:35 -08001162 // Eat the long press event so the keyboard doesn't come up.
1163 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1164 return true;
1165 }
1166
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001167 return handled;
1168 }
1169
Karl Rosaen138a0412009-04-23 19:00:21 -07001170 private String getTypedText() {
1171 return mDefaultKeySsb.toString();
1172 }
1173
1174 private void clearTypedText() {
1175 mDefaultKeySsb.clear();
1176 mDefaultKeySsb.clearSpans();
1177 Selection.setSelection(mDefaultKeySsb, 0);
1178 }
1179
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001180 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001181 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1182 * State
1183 */
1184 private static State intToState(int stateOrdinal) {
1185 State state = State.WORKSPACE;
1186 final State[] stateValues = State.values();
1187 for (int i = 0; i < stateValues.length; i++) {
1188 if (stateValues[i].ordinal() == stateOrdinal) {
1189 state = stateValues[i];
1190 break;
1191 }
1192 }
1193 return state;
1194 }
1195
1196 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001197 * Restores the previous state, if it exists.
1198 *
1199 * @param savedState The previous state.
1200 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001201 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001202 private void restoreState(Bundle savedState) {
1203 if (savedState == null) {
1204 return;
1205 }
1206
Michael Jurka883f55b2010-10-21 15:47:14 -07001207 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001208 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001209 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001210 }
1211
Adam Cohen21cd0022013-10-09 18:57:02 -07001212 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1213 PagedView.INVALID_RESTORE_PAGE);
1214 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001215 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001216 }
1217
Winson Chung3d503fb2011-07-13 17:25:49 -07001218 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001219 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001220
Winson Chung3d503fb2011-07-13 17:25:49 -07001221 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1222 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001223 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001224 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1225 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001226 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1227 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1228 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001229 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001230 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 mRestoring = true;
1232 }
1233
1234 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1235 if (renameFolder) {
1236 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001237 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001238 mRestoring = true;
1239 }
Winson Chunga12a2502010-12-20 14:41:35 -08001240
Winson Chung785d2eb2011-04-14 16:08:02 -07001241 // Restore the AppsCustomize tab
1242 if (mAppsCustomizeTabHost != null) {
1243 String curTab = savedState.getString("apps_customize_currentTab");
1244 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001245 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001246 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001247 mAppsCustomizeContent.loadAssociatedPages(
1248 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001249 }
1250
Winson Chung5afbf7b2011-07-25 11:53:08 -07001251 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1252 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001253 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001254 mItemIdToViewId = (HashMap<Integer, Integer>)
1255 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001256 }
1257
1258 /**
1259 * Finds all the views we need and configure them properly.
1260 */
1261 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001262 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001263
Craig Mautner360310b2012-10-26 15:13:08 -07001264 mLauncherView = findViewById(R.id.launcher);
Winson Chung4c98d922011-05-31 16:50:48 -07001265 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1266 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001267 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001268 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001269
John Spurlock77e1f472013-09-11 10:09:51 -04001270 mLauncherView.setSystemUiVisibility(
1271 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001272 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273
Winson Chung4c98d922011-05-31 16:50:48 -07001274 // Setup the drag layer
1275 mDragLayer.setup(this, dragController);
1276
Winson Chung3d503fb2011-07-13 17:25:49 -07001277 // Setup the hotseat
1278 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1279 if (mHotseat != null) {
1280 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001281 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001282 }
1283
Jorim Jaggid017f882014-01-14 17:08:48 -08001284 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001285 View widgetButton = findViewById(R.id.widget_button);
1286 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001287 @Override
1288 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001289 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001290 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001291 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001292 }
1293 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001294 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1295
1296 View wallpaperButton = findViewById(R.id.wallpaper_button);
1297 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001298 @Override
1299 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001300 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001301 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001302 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001303 }
1304 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001305 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1306
1307 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001308 if (hasSettings()) {
1309 settingsButton.setOnClickListener(new OnClickListener() {
1310 @Override
1311 public void onClick(View arg0) {
1312 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001313 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001314 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001315 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001316 });
1317 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1318 } else {
1319 settingsButton.setVisibility(View.GONE);
1320 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1321 lp.gravity = Gravity.END | Gravity.TOP;
1322 widgetButton.requestLayout();
1323 }
1324
Adam Cohendbdff6b2013-09-18 19:09:15 -07001325 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001326
Winson Chung4c98d922011-05-31 16:50:48 -07001327 // Setup the workspace
1328 mWorkspace.setHapticFeedbackEnabled(false);
1329 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001330 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001331 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001332
Winson Chungf0ea4d32011-06-06 14:27:16 -07001333 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001334 mSearchDropTargetBar = (SearchDropTargetBar)
1335 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001336
Winson Chungf0ea4d32011-06-06 14:27:16 -07001337 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001338 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001339 mAppsCustomizeContent = (AppsCustomizePagedView)
1340 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1341 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001342
Winson Chung3d503fb2011-07-13 17:25:49 -07001343 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001344 dragController.setDragScoller(mWorkspace);
1345 dragController.setScrollView(mDragLayer);
1346 dragController.setMoveTarget(mWorkspace);
1347 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001348 if (mSearchDropTargetBar != null) {
1349 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001350 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001351
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001352 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001353 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001354 mWeightWatcher = new WeightWatcher(this);
1355 mWeightWatcher.setAlpha(0.5f);
1356 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001357 new FrameLayout.LayoutParams(
1358 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001359 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001360 Gravity.BOTTOM)
1361 );
Adam Cohen39a06042013-07-19 14:30:12 -07001362
1363 boolean show = shouldShowWeightWatcher();
1364 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001365 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 }
1367
1368 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001369 * Sets the all apps button. This method is called from {@link Hotseat}.
1370 */
1371 public void setAllAppsButton(View allAppsButton) {
1372 mAllAppsButton = allAppsButton;
1373 }
1374
1375 public View getAllAppsButton() {
1376 return mAllAppsButton;
1377 }
1378
1379 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001380 * Creates a view representing a shortcut.
1381 *
1382 * @param info The data structure describing the shortcut.
1383 *
1384 * @return A View inflated from R.layout.application.
1385 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001386 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001387 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001388 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001389 }
1390
1391 /**
1392 * Creates a view representing a shortcut inflated from the specified resource.
1393 *
1394 * @param layoutResId The id of the XML layout used to create the shortcut.
1395 * @param parent The group the shortcut belongs to.
1396 * @param info The data structure describing the shortcut.
1397 *
1398 * @return A View inflated from layoutResId.
1399 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001400 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001401 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
1402 favorite.applyFromShortcutInfo(info, mIconCache);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 favorite.setOnClickListener(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 return favorite;
1405 }
1406
1407 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001408 * Add a shortcut to the workspace.
1409 *
1410 * @param data The intent describing the shortcut.
1411 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001412 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001413 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001414 int cellY) {
1415 int[] cellXY = mTmpAddItemCellCoordinates;
1416 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001417 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001418
Michael Jurkad3ef3062010-11-23 16:23:58 -08001419 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001420
Adam Cohen558baaf2011-08-15 15:22:57 -07001421 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001422 if (info == null) {
1423 return;
1424 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001425 final View view = createShortcut(info);
1426
Adam Cohenfbba09b2011-07-18 21:43:05 -07001427 // First we check if we already know the exact location where we want to add this item.
1428 if (cellX >= 0 && cellY >= 0) {
1429 cellXY[0] = cellX;
1430 cellXY[1] = cellY;
1431 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001432
1433 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001434 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001435 true, null,null)) {
1436 return;
1437 }
1438 DragObject dragObject = new DragObject();
1439 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001440 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1441 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001442 return;
1443 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001444 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001445 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001446 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001447 foundCellSpan = (result != null);
1448 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001449 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001450 }
1451
1452 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001453 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001454 return;
1455 }
1456
Adam Cohendcd297f2013-06-18 13:13:40 -07001457 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458
1459 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001460 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001461 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 }
1463 }
1464
Adam Cohen2f093b62012-04-30 18:59:53 -07001465 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1466 int minHeight) {
1467 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001468 // We want to account for the extra amount of padding that we are adding to the widget
1469 // to ensure that it gets the full amount of space that it has requested
1470 int requiredWidth = minWidth + padding.left + padding.right;
1471 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001472 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001473 }
1474
Adam Cohen2f093b62012-04-30 18:59:53 -07001475 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1476 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001477 }
1478
Adam Cohen2f093b62012-04-30 18:59:53 -07001479 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1480 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001481 }
1482
Adam Cohen2f093b62012-04-30 18:59:53 -07001483 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1484 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001485 }
1486
Adam Cohen2f093b62012-04-30 18:59:53 -07001487 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1488 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001489 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001490 }
1491
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001493 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001495 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001496 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001497 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001498 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001499 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1500 if (appWidgetInfo == null) {
1501 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1502 }
Romain Guycbb89e42009-06-08 15:52:54 -07001503
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001504 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001505 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001506
Adam Cohen2f093b62012-04-30 18:59:53 -07001507 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1508 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001509
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001510 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001511 // We have saved the position to which the widget was dragged-- this really only matters
1512 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001513 int[] cellXY = mTmpAddItemCellCoordinates;
1514 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001515 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001516 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001517 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1518 cellXY[0] = mPendingAddInfo.cellX;
1519 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001520 spanXY[0] = mPendingAddInfo.spanX;
1521 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001522 foundCellSpan = true;
1523 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001524 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001525 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001526 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1527 spanXY[1], cellXY, finalSpan);
1528 spanXY[0] = finalSpan[0];
1529 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001530 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001531 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001532 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001533 }
1534
Michael Jurka0280c3b2010-09-17 15:00:07 -07001535 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001536 if (appWidgetId != -1) {
1537 // Deleting an app widget ID is a void call but writes to disk before returning
1538 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001539 new AsyncTask<Void, Void, Void>() {
1540 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001541 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001542 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001543 }
Michael Jurka43467462013-11-14 12:03:58 +01001544 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001545 }
Winson Chung93eef082012-03-23 15:59:27 -07001546 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001547 return;
1548 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001550 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001551 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1552 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001553 launcherInfo.spanX = spanXY[0];
1554 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001555 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1556 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Romain Guycbb89e42009-06-08 15:52:54 -07001557
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001558 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001559 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560
1561 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001562 if (hostView == null) {
1563 // Perform actual inflation because we're live
1564 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1565 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1566 } else {
1567 // The AppWidgetHostView has already been inflated and instantiated
1568 launcherInfo.hostView = hostView;
1569 }
Romain Guycbb89e42009-06-08 15:52:54 -07001570
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001571 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001572 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001573 launcherInfo.notifyWidgetSizeChanged(this);
1574
Adam Cohendcd297f2013-06-18 13:13:40 -07001575 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001576 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001577
1578 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001579 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001580 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001581 }
Romain Guycbb89e42009-06-08 15:52:54 -07001582
Adam Cohended9f8d2010-11-03 13:25:16 -07001583 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1584 @Override
1585 public void onReceive(Context context, Intent intent) {
1586 final String action = intent.getAction();
1587 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1588 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001589 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001590 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001591
Winson Chungc100e8e2011-08-09 16:02:43 -07001592 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001593 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001594 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001595 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001596 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001597 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1598 mUserPresent = true;
1599 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001600 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1601 mModel.resetLoadedState(false, true);
1602 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1603 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1604 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1605 mModel.resetLoadedState(false, true);
1606 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1607 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1608 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001609 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1610 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1611 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001612 }
1613 }
1614 };
1615
1616 @Override
1617 public void onAttachedToWindow() {
1618 super.onAttachedToWindow();
1619
1620 // Listen for broadcasts related to user-presence
1621 final IntentFilter filter = new IntentFilter();
1622 filter.addAction(Intent.ACTION_SCREEN_OFF);
1623 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001624 // For handling managed profiles
1625 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1626 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001627 if (ENABLE_DEBUG_INTENTS) {
1628 filter.addAction(DebugIntents.DELETE_DATABASE);
1629 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1630 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001631 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001632 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001633 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001634 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001635 mVisible = true;
1636 }
1637
Adam Cohen0b395352014-06-09 22:54:36 +00001638 /**
1639 * Sets up transparent navigation and status bars in LMP.
1640 * This method is a no-op for other platform versions.
1641 */
1642 @TargetApi(19)
1643 private void setupTransparentSystemBarsForLmp() {
1644 // TODO(sansid): use the APIs directly when compiling against L sdk.
1645 // Currently we use reflection to access the flags and the API to set the transparency
1646 // on the System bars.
1647 if (Utilities.isLmp()) {
1648 try {
1649 getWindow().getAttributes().systemUiVisibility |=
1650 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1651 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1652 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1653 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1654 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1655 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1656 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1657 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1658
1659 Method setStatusBarColorMethod =
1660 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1661 Method setNavigationBarColorMethod =
1662 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1663 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1664 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1665 } catch (NoSuchFieldException e) {
1666 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1667 } catch (NoSuchMethodException ex) {
1668 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1669 } catch (IllegalAccessException e) {
1670 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1671 } catch (IllegalArgumentException e) {
1672 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1673 } catch (InvocationTargetException e) {
1674 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1675 } finally {}
1676 }
1677 }
1678
Adam Cohended9f8d2010-11-03 13:25:16 -07001679 @Override
1680 public void onDetachedFromWindow() {
1681 super.onDetachedFromWindow();
1682 mVisible = false;
1683
Adam Cohend113e0c2010-11-11 10:48:05 -08001684 if (mAttached) {
1685 unregisterReceiver(mReceiver);
1686 mAttached = false;
1687 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001688 updateRunning();
1689 }
1690
1691 public void onWindowVisibilityChanged(int visibility) {
1692 mVisible = visibility == View.VISIBLE;
1693 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001694 // The following code used to be in onResume, but it turns out onResume is called when
1695 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1696 // is a more appropriate event to handle
1697 if (mVisible) {
1698 mAppsCustomizeTabHost.onWindowVisible();
1699 if (!mWorkspaceLoading) {
1700 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001701 // We want to let Launcher draw itself at least once before we force it to build
1702 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001703 // apps is nice and speedy.
1704 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001705 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001706 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001707 if (mStarted) return;
1708 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001709 // We delay the layer building a bit in order to give
1710 // other message processing a time to run. In particular
1711 // this avoids a delay in hiding the IME if it was
1712 // currently shown, because doing that may involve
1713 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001714 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001715 final ViewTreeObserver.OnDrawListener listener = this;
1716 mWorkspace.post(new Runnable() {
1717 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001718 if (mWorkspace != null &&
1719 mWorkspace.getViewTreeObserver() != null) {
1720 mWorkspace.getViewTreeObserver().
1721 removeOnDrawListener(listener);
1722 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001723 }
1724 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001725 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001726 }
1727 });
1728 }
Michael Jurka6ee21d22012-02-21 18:20:27 -08001729 // When Launcher comes back to foreground, a different Activity might be responsible for
1730 // the app market intent, so refresh the icon
Winson Chungd64a6662013-09-30 11:06:59 -07001731 if (!DISABLE_MARKET_BUTTON) {
1732 updateAppMarketIcon();
1733 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001734 clearTypedText();
1735 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001736 }
1737
1738 private void sendAdvanceMessage(long delay) {
1739 mHandler.removeMessages(ADVANCE_MSG);
1740 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1741 mHandler.sendMessageDelayed(msg, delay);
1742 mAutoAdvanceSentTime = System.currentTimeMillis();
1743 }
1744
1745 private void updateRunning() {
1746 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1747 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1748 mAutoAdvanceRunning = autoAdvanceRunning;
1749 if (autoAdvanceRunning) {
1750 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1751 sendAdvanceMessage(delay);
1752 } else {
1753 if (!mWidgetsToAdvance.isEmpty()) {
1754 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1755 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1756 }
1757 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001758 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 }
1760 }
1761 }
1762
1763 private final Handler mHandler = new Handler() {
1764 @Override
1765 public void handleMessage(Message msg) {
1766 if (msg.what == ADVANCE_MSG) {
1767 int i = 0;
1768 for (View key: mWidgetsToAdvance.keySet()) {
1769 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1770 final int delay = mAdvanceStagger * i;
1771 if (v instanceof Advanceable) {
1772 postDelayed(new Runnable() {
1773 public void run() {
1774 ((Advanceable) v).advance();
1775 }
1776 }, delay);
1777 }
1778 i++;
1779 }
1780 sendAdvanceMessage(mAdvanceInterval);
1781 }
1782 }
1783 };
1784
1785 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001786 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001787 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1788 if (v instanceof Advanceable) {
1789 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001790 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001791 updateRunning();
1792 }
1793 }
1794
1795 void removeWidgetToAutoAdvance(View hostView) {
1796 if (mWidgetsToAdvance.containsKey(hostView)) {
1797 mWidgetsToAdvance.remove(hostView);
1798 updateRunning();
1799 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001800 }
1801
Joe Onorato9c1289c2009-08-17 11:03:03 -04001802 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001803 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001804 launcherInfo.hostView = null;
1805 }
1806
Winson Chung93eef082012-03-23 15:59:27 -07001807 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1808 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1809 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001810 }
1811
Winson Chunga6945242014-01-08 14:04:34 -08001812 public DragLayer getDragLayer() {
1813 return mDragLayer;
1814 }
1815
1816 public Workspace getWorkspace() {
1817 return mWorkspace;
1818 }
1819
1820 public Hotseat getHotseat() {
1821 return mHotseat;
1822 }
1823
Jorim Jaggid017f882014-01-14 17:08:48 -08001824 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001825 return mOverviewPanel;
1826 }
1827
1828 public SearchDropTargetBar getSearchBar() {
1829 return mSearchDropTargetBar;
1830 }
1831
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001832 public LauncherAppWidgetHost getAppWidgetHost() {
1833 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001834 }
Romain Guycbb89e42009-06-08 15:52:54 -07001835
Winson Chunga9abd0e2010-10-27 17:18:37 -07001836 public LauncherModel getModel() {
1837 return mModel;
1838 }
1839
Winson Chunga6945242014-01-08 14:04:34 -08001840 public LauncherClings getLauncherClings() {
1841 return mLauncherClings;
1842 }
1843
1844 protected SharedPreferences getSharedPrefs() {
1845 return mSharedPrefs;
1846 }
1847
Bjorn Bringertc459e522013-06-07 19:36:01 +01001848 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001849 getWindow().closeAllPanels();
1850
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001851 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001852 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001853 }
1854
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001855 @Override
1856 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001857 long startTime = 0;
1858 if (DEBUG_RESUME_TIME) {
1859 startTime = System.currentTimeMillis();
1860 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001861 super.onNewIntent(intent);
1862
1863 // Close the menu
1864 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001865 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001866 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001867
Adam Cohened307df2013-10-02 09:37:31 -07001868 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1869 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1870 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001871
Adam Cohen6fecd412013-10-02 17:41:50 -07001872 if (mWorkspace == null) {
1873 // Can be cases where mWorkspace is null, this prevents a NPE
1874 return;
1875 }
1876 Folder openFolder = mWorkspace.getOpenFolder();
1877 // In all these cases, only animate if we're already on home
1878 mWorkspace.exitWidgetResizeMode();
1879 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Coppa120b8e2013-11-12 16:26:04 +00001880 openFolder == null && shouldMoveToDefaultScreenOnHomeIntent()) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001881 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001882 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001883
Adam Cohen6fecd412013-10-02 17:41:50 -07001884 closeFolder();
1885 exitSpringLoadedDragMode();
1886
1887 // If we are already on home, then just animate back to the workspace,
1888 // otherwise, just wait until onResume to set the state back to Workspace
1889 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001890 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001891 } else {
1892 mOnResumeState = State.WORKSPACE;
1893 }
1894
1895 final View v = getWindow().peekDecorView();
1896 if (v != null && v.getWindowToken() != null) {
1897 InputMethodManager imm = (InputMethodManager)getSystemService(
1898 INPUT_METHOD_SERVICE);
1899 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1900 }
1901
1902 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001903 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001904 mAppsCustomizeTabHost.reset();
1905 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001906
1907 onHomeIntent();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001908 }
Adam Cohened307df2013-10-02 09:37:31 -07001909
Michael Jurka447bf842013-05-15 14:52:15 +02001910 if (DEBUG_RESUME_TIME) {
1911 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1912 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001913 }
1914
Adam Coppa120b8e2013-11-12 16:26:04 +00001915 /**
1916 * Override point for subclasses to prevent movement to the default screen when the home
1917 * button is pressed. Used (for example) in GEL, to prevent movement during a search.
1918 */
1919 protected boolean shouldMoveToDefaultScreenOnHomeIntent() {
1920 return true;
1921 }
1922
1923 /**
1924 * Override point for subclasses to provide custom behaviour for when a home intent is fired.
1925 */
1926 protected void onHomeIntent() {
1927 // Do nothing
1928 }
1929
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001930 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001931 public void onRestoreInstanceState(Bundle state) {
1932 super.onRestoreInstanceState(state);
1933 for (int page: mSynchronouslyBoundPages) {
1934 mWorkspace.restoreInstanceStateForChild(page);
1935 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001936 }
1937
1938 @Override
1939 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001940 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001941 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1942 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001943 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001944 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945
Michael Jurka883f55b2010-10-21 15:47:14 -07001946 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001947 // We close any open folder since it will not be re-opened, and we need to make sure
1948 // this state is reflected.
1949 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950
Adam Cohendcd297f2013-06-18 13:13:40 -07001951 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001952 mWaitingForResult) {
1953 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001954 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001955 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1956 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001957 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1958 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1959 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001960 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001961 }
1962
1963 if (mFolderInfo != null && mWaitingForResult) {
1964 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
1965 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
1966 }
Michael Jurka946ad472010-07-09 18:05:18 -07001967
Winson Chung785d2eb2011-04-14 16:08:02 -07001968 // Save the current AppsCustomize tab
1969 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08001970 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
1971 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07001972 if (currentTabTag != null) {
1973 outState.putString("apps_customize_currentTab", currentTabTag);
1974 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07001975 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
1976 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001977 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001978 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 }
1980
1981 @Override
1982 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07001984
Winson Chunge7a03942011-08-05 15:05:12 -07001985 // Remove all pending runnables
1986 mHandler.removeMessages(ADVANCE_MSG);
1987 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07001988 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07001989
Winson Chungcd2b0142011-06-08 16:02:26 -07001990 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04001991 LauncherAppState app = (LauncherAppState.getInstance());
Winson Chungcd2b0142011-06-08 16:02:26 -07001992 mModel.stopLoader();
1993 app.setLauncher(null);
1994
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001995 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001996 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08001998 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001999 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002000 mAppWidgetHost = null;
2001
2002 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002003
2004 TextKeyListener.getInstance().release();
2005
Winson Chung81b52252012-08-27 15:34:29 -07002006 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2007 // to prevent leaking Launcher activities on orientation change.
2008 if (mModel != null) {
2009 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2010 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002011
2012 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002013 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002014
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002015 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002016 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002017 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002018 mWorkspace = null;
2019 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002020
Michael Jurka2ecf9952012-06-18 12:52:28 -07002021 LauncherAnimUtils.onDestroyActivity();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
2023
Adam Cohena9cf38f2011-05-02 15:36:58 -07002024 public DragController getDragController() {
2025 return mDragController;
2026 }
2027
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 @Override
2029 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002030 if (requestCode >= 0) {
2031 setWaitingForResult(true);
2032 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033 super.startActivityForResult(intent, requestCode);
2034 }
2035
Winson Chung70d72102011-08-12 11:24:35 -07002036 /**
2037 * Indicates that we want global search for this activity by setting the globalSearch
2038 * argument for {@link #startSearch} to true.
2039 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002041 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002043
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002044 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002045
Karl Rosaen138a0412009-04-23 19:00:21 -07002046 if (initialQuery == null) {
2047 // Use any text typed in the launcher as the initial query
2048 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002049 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002050 if (appSearchData == null) {
2051 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002052 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002053 }
Winson Chungadf0c182012-08-23 14:59:07 -07002054 Rect sourceBounds = new Rect();
2055 if (mSearchDropTargetBar != null) {
2056 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2057 }
Romain Guycbb89e42009-06-08 15:52:54 -07002058
Bjorn Bringertc459e522013-06-07 19:36:01 +01002059 startSearch(initialQuery, selectInitialQuery,
2060 appSearchData, sourceBounds);
2061 }
2062
2063 public void startSearch(String initialQuery,
2064 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Michael Jurkaa33411c2012-06-14 16:18:21 -07002065 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002066 appSearchData, sourceBounds);
Michael Jurkaa33411c2012-06-14 16:18:21 -07002067 }
2068
2069 /**
2070 * Starts the global search activity. This code is a copied from SearchManager
2071 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002072 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002073 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002074 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002075 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2076 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2077 if (globalSearchActivity == null) {
2078 Log.w(TAG, "No global search activity found.");
2079 return;
2080 }
2081 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2082 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2083 intent.setComponent(globalSearchActivity);
2084 // Make sure that we have a Bundle to put source in
2085 if (appSearchData == null) {
2086 appSearchData = new Bundle();
2087 } else {
2088 appSearchData = new Bundle(appSearchData);
2089 }
2090 // Set source to package name of app that starts global search, if not set already.
2091 if (!appSearchData.containsKey("source")) {
2092 appSearchData.putString("source", getPackageName());
2093 }
2094 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2095 if (!TextUtils.isEmpty(initialQuery)) {
2096 intent.putExtra(SearchManager.QUERY, initialQuery);
2097 }
2098 if (selectInitialQuery) {
2099 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2100 }
2101 intent.setSourceBounds(sourceBounds);
2102 try {
2103 startActivity(intent);
2104 } catch (ActivityNotFoundException ex) {
2105 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2106 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002107 }
2108
Yura4f93ec62014-02-04 14:15:21 +00002109 public boolean isOnCustomContent() {
2110 return mWorkspace.isOnOrMovingToCustomContent();
2111 }
2112
Winson Chung70d72102011-08-12 11:24:35 -07002113 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002114 public boolean onPrepareOptionsMenu(Menu menu) {
2115 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002116 if (!isOnCustomContent()) {
2117 // Close any open folders
2118 closeFolder();
2119 // Stop resizing any widgets
2120 mWorkspace.exitWidgetResizeMode();
2121 if (!mWorkspace.isInOverviewMode()) {
2122 // Show the overview mode
2123 showOverviewMode(true);
2124 } else {
2125 showWorkspace(true);
2126 }
Winson Chunge0298742014-01-17 12:03:00 -08002127 }
Michael Jurkab94f3f82013-09-11 18:08:54 +02002128 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002129 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002131 @Override
2132 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002133 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002134 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002135 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002136 }
2137
Joe Onorato9c1289c2009-08-17 11:03:03 -04002138 public boolean isWorkspaceLocked() {
2139 return mWorkspaceLoading || mWaitingForResult;
2140 }
2141
Adam Cohen517a7f52014-03-01 12:12:59 -08002142 public boolean isWorkspaceLoading() {
2143 return mWorkspaceLoading;
2144 }
2145
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002146 private void setWorkspaceLoading(boolean value) {
2147 boolean isLocked = isWorkspaceLocked();
2148 mWorkspaceLoading = value;
2149 if (isLocked != isWorkspaceLocked()) {
2150 onWorkspaceLockedChanged();
2151 }
2152 }
2153
2154 private void setWaitingForResult(boolean value) {
2155 boolean isLocked = isWorkspaceLocked();
2156 mWaitingForResult = value;
2157 if (isLocked != isWorkspaceLocked()) {
2158 onWorkspaceLockedChanged();
2159 }
2160 }
2161
2162 protected void onWorkspaceLockedChanged() { }
2163
Michael Jurka0280c3b2010-09-17 15:00:07 -07002164 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002165 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002166 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002167 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2168 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002169 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002170 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002171 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002172
Adam Cohenad4e15c2013-10-17 16:21:35 -07002173 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2174 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2175 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2176 }
2177
2178 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2179 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2180 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002181 if (appWidgetInfo.configure != null) {
2182 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002183 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002184
Bjorn Bringert7984c942009-12-09 15:38:25 +00002185 // Launch over to configure widget, if needed
2186 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_CONFIGURE);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002187 intent.setComponent(appWidgetInfo.configure);
Bjorn Bringert7984c942009-12-09 15:38:25 +00002188 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
Michael Jurkaa805e1a2013-08-22 15:00:33 +02002189 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_APPWIDGET);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002190 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002191 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002192 Runnable onComplete = new Runnable() {
2193 @Override
2194 public void run() {
2195 // Exit spring loaded mode if necessary after adding the widget
2196 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2197 null);
2198 }
2199 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002200 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002201 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002202 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 }
2204 }
Romain Guycbb89e42009-06-08 15:52:54 -07002205
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002206 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002207 // Close any folders that may be open.
2208 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002209 mWorkspace.moveToCustomContentScreen(animate);
2210 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002211 /**
2212 * Process a shortcut drop.
2213 *
2214 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002215 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002216 * @param cell The cell it should be added to, optional
2217 * @param position The location on the screen where it was dropped, optional
2218 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002219 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002220 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002221 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002222 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002223 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002224 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002225
2226 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002227 mPendingAddInfo.cellX = cell[0];
2228 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002229 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002230
2231 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2232 createShortcutIntent.setComponent(componentName);
2233 processShortcut(createShortcutIntent);
2234 }
2235
Adam Cohenfbba09b2011-07-18 21:43:05 -07002236 /**
2237 * Process a widget drop.
2238 *
2239 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002240 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002241 * @param cell The cell it should be added to, optional
2242 * @param position The location on the screen where it was dropped, optional
2243 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002244 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002245 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002246 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002247 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002248 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002249 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002250 mPendingAddInfo.minSpanX = info.minSpanX;
2251 mPendingAddInfo.minSpanY = info.minSpanY;
2252
Adam Cohenfbba09b2011-07-18 21:43:05 -07002253 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002254 mPendingAddInfo.cellX = cell[0];
2255 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002256 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002257 if (span != null) {
2258 mPendingAddInfo.spanX = span[0];
2259 mPendingAddInfo.spanY = span[1];
2260 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002261
Adam Cohened66b2b2012-01-23 17:28:51 -08002262 AppWidgetHostView hostView = info.boundWidget;
2263 int appWidgetId;
2264 if (hostView != null) {
2265 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002266 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002267 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002268 // In this case, we either need to start an activity to get permission to bind
2269 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002270 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002271 Bundle options = info.bindOptions;
2272
2273 boolean success = false;
2274 if (options != null) {
2275 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2276 info.componentName, options);
2277 } else {
2278 success = mAppWidgetManager.bindAppWidgetIdIfAllowed(appWidgetId,
2279 info.componentName);
2280 }
2281 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002282 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002283 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002284 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002285 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2286 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2287 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Adam Cohendd70d662012-10-04 16:53:44 -07002288 // TODO: we need to make sure that this accounts for the options bundle.
2289 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002290 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2291 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002292 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002293 }
2294
Joe Onoratodeb98af2010-02-19 14:59:39 -08002295 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002296 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002297 }
2298
Winson Chung24ab2f12010-09-16 14:10:47 -07002299 void processWallpaper(Intent intent) {
2300 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2301 }
2302
Adam Cohendcd297f2013-06-18 13:13:40 -07002303 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002304 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002305 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002306 folderInfo.title = getText(R.string.folder_name);
2307
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002308 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002309 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002310 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002311 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002312
2313 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002314 FolderIcon newFolder =
2315 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002316 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002317 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002318 // Force measure the new folder icon
2319 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2320 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002321 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002322 }
Romain Guycbb89e42009-06-08 15:52:54 -07002323
Joe Onorato9c1289c2009-08-17 11:03:03 -04002324 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002325 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002326 }
2327
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002328 protected ComponentName getWallpaperPickerComponent() {
Michael Jurka7ad868b2013-12-12 15:04:25 +01002329 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002330 }
2331
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002332 /**
2333 * Registers various content observers. The current implementation registers
2334 * only a favorites observer to keep track of the favorites applications.
2335 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002336 private void registerContentObservers() {
2337 ContentResolver resolver = getContentResolver();
2338 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2339 true, mWidgetObserver);
2340 }
2341
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002342 @Override
2343 public boolean dispatchKeyEvent(KeyEvent event) {
2344 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2345 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002347 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002348 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002349 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002350 dumpState();
2351 return true;
2352 }
2353 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002354 }
2355 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2356 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002357 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002358 return true;
2359 }
2360 }
2361
2362 return super.dispatchKeyEvent(event);
2363 }
2364
Joe Onorato88ec0992009-11-19 13:16:06 -08002365 @Override
2366 public void onBackPressed() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07002367 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002368 if (mAppsCustomizeContent.getContentType() ==
2369 AppsCustomizePagedView.ContentType.Applications) {
2370 showWorkspace(true);
2371 } else {
2372 showOverviewMode(true);
2373 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002374 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002375 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002376 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002377 Folder openFolder = mWorkspace.getOpenFolder();
2378 if (openFolder.isEditingName()) {
2379 openFolder.dismissEditingName();
2380 } else {
2381 closeFolder();
2382 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002383 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002384 mWorkspace.exitWidgetResizeMode();
2385
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002386 // Back button is a no-op here, but give at least some feedback for the button press
2387 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002388 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002389 }
2390
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002392 * Re-listen when widgets are reset.
2393 */
2394 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002395 if (mAppWidgetHost != null) {
2396 mAppWidgetHost.startListening();
2397 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002398 }
2399
2400 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002401 * Launches the intent referred by the clicked shortcut.
2402 *
2403 * @param v The view representing the clicked shortcut.
2404 */
2405 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002406 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2407 // view has detached (it's possible for this to happen if the view is removed mid touch).
2408 if (v.getWindowToken() == null) {
2409 return;
2410 }
2411
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002412 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002413 return;
2414 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002415
Adam Cohen1697b792013-09-17 19:08:21 -07002416 if (v instanceof Workspace) {
2417 if (mWorkspace.isInOverviewMode()) {
2418 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002419 }
2420 return;
2421 }
2422
2423 if (v instanceof CellLayout) {
2424 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002425 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002426 }
2427 }
2428
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002430 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002431 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002433 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002434 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002435 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002436 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002437 onClickAllAppsButton(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002438 }
2439 }
2440
Michael Jurka0e260592010-06-30 17:07:39 -07002441 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002442 return false;
2443 }
2444
Michael Jurkaaf442092010-06-10 17:01:57 -07002445 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002446 * Event handler for the search button
2447 *
2448 * @param v The view that was clicked.
2449 */
2450 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002451 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2452
Amith Yamasania135ba82011-08-09 17:42:01 -07002453 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002454 }
2455
2456 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002457 * Event handler for the voice button
2458 *
2459 * @param v The view that was clicked.
2460 */
2461 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002462 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002463
Bjorn Bringertc459e522013-06-07 19:36:01 +01002464 startVoice();
2465 }
2466
2467 public void startVoice() {
Michael Jurkaae65ee32012-04-30 11:45:54 -07002468 try {
2469 final SearchManager searchManager =
2470 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2471 ComponentName activityName = searchManager.getGlobalSearchActivity();
2472 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002473 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002474 if (activityName != null) {
2475 intent.setPackage(activityName.getPackageName());
2476 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002477 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002478 } catch (ActivityNotFoundException e) {
2479 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002480 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002481 startActivitySafely(null, intent, "onClickVoiceButton");
2482 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002483 }
2484
2485 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002486 * Event handler for the "grid" button that appears on the home screen, which
2487 * enters all apps mode.
2488 *
2489 * @param v The view that was clicked.
2490 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002491 protected void onClickAllAppsButton(View v) {
2492 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2493 if (isAllAppsVisible()) {
2494 showWorkspace(true);
2495 } else {
2496 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2497 }
2498 }
2499
2500 /**
Mohammadinamul Sheik3b1a54a2014-03-18 11:28:19 -07002501 * Event handler for a paged view icon click.
2502 * @param v The view that was clicked.
2503 * @param appInfo The {link AppInfo} of the view.
2504 */
2505 public void onClickPagedViewIcon(View v, AppInfo appInfo) {
2506 if (LOGD) Log.d(TAG, "onClickPagedViewIcon");
2507 startActivitySafely(v, appInfo.intent, appInfo);
2508 getStats().recordLaunch(appInfo.intent);
2509 }
2510
2511 /**
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002512 * Event handler for an app shortcut click.
2513 *
2514 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2515 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002516 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002517 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2518 Object tag = v.getTag();
2519 if (!(tag instanceof ShortcutInfo)) {
2520 throw new IllegalArgumentException("Input must be a Shortcut");
2521 }
2522
2523 // Open shortcut
2524 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2525 final Intent intent = shortcut.intent;
2526
2527 // Check for special shortcuts
2528 if (intent.getComponent() != null) {
2529 final String shortcutClass = intent.getComponent().getClassName();
2530
2531 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2532 MemoryDumpActivity.startDump(this);
2533 return;
2534 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2535 toggleShowWeightWatcher();
2536 return;
2537 }
2538 }
2539
Chris Wren40c5ed32014-06-24 18:24:23 -04002540 // Check for abandoned promise
2541 if (shortcut.isAbandoned() && v instanceof BubbleTextView) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002542 AlertDialog.Builder builder = new AlertDialog.Builder(this);
2543 builder.setTitle(R.string.abandoned_promises_title);
Chris Wren803a4be2014-07-01 16:52:49 -04002544 builder.setMessage(R.string.abandoned_promise_explanation);
Chris Wren40c5ed32014-06-24 18:24:23 -04002545 builder.setPositiveButton(R.string.abandoned_search,
2546 new DialogInterface.OnClickListener() {
2547 public void onClick(DialogInterface dialog, int id) {
2548 startAppShortcutActivity(v);
2549 }
2550 }
2551 );
Chris Wren40c5ed32014-06-24 18:24:23 -04002552 builder.setNeutralButton(R.string.abandoned_clean_this,
2553 new DialogInterface.OnClickListener() {
2554 public void onClick(DialogInterface dialog, int id) {
2555 final BubbleTextView bubble = (BubbleTextView) v;
2556 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2557 mWorkspace.removeAbandonedPromise(bubble, user);
2558 }
2559 });
2560 builder.create().show();
2561 return;
2562 }
2563
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002564 // Start activities
Chris Wren40c5ed32014-06-24 18:24:23 -04002565 startAppShortcutActivity(v);
2566 }
2567
2568 private void startAppShortcutActivity(View v) {
2569 Object tag = v.getTag();
2570 if (!(tag instanceof ShortcutInfo)) {
2571 throw new IllegalArgumentException("Input must be a Shortcut");
2572 }
2573 final ShortcutInfo shortcut = (ShortcutInfo) tag;
2574 final Intent intent = shortcut.intent;
2575
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002576 int[] pos = new int[2];
2577 v.getLocationOnScreen(pos);
2578 intent.setSourceBounds(new Rect(pos[0], pos[1],
2579 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
2580
2581 boolean success = startActivitySafely(v, intent, tag);
2582
2583 mStats.recordLaunch(intent, shortcut);
2584
2585 if (success && v instanceof BubbleTextView) {
2586 mWaitingForResume = (BubbleTextView) v;
2587 mWaitingForResume.setStayPressed(true);
2588 }
2589 }
2590
2591 /**
2592 * Event handler for a folder icon click.
2593 *
2594 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2595 */
2596 protected void onClickFolderIcon(View v) {
2597 if (LOGD) Log.d(TAG, "onClickFolder");
2598 if (!(v instanceof FolderIcon)){
2599 throw new IllegalArgumentException("Input must be a FolderIcon");
2600 }
2601
2602 FolderIcon folderIcon = (FolderIcon) v;
2603 final FolderInfo info = folderIcon.getFolderInfo();
2604 Folder openFolder = mWorkspace.getFolderForTag(info);
2605
2606 // If the folder info reports that the associated folder is open, then verify that
2607 // it is actually opened. There have been a few instances where this gets out of sync.
2608 if (info.opened && openFolder == null) {
2609 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2610 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2611 info.opened = false;
2612 }
2613
2614 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2615 // Close any open folder
2616 closeFolder();
2617 // Open the requested folder
2618 openFolder(folderIcon);
2619 } else {
2620 // Find the open folder...
2621 int folderScreen;
2622 if (openFolder != null) {
2623 folderScreen = mWorkspace.getPageForView(openFolder);
2624 // .. and close it
2625 closeFolder(openFolder);
2626 if (folderScreen != mWorkspace.getCurrentPage()) {
2627 // Close any folder open on the current screen
2628 closeFolder();
2629 // Pull the folder onto this screen
2630 openFolder(folderIcon);
2631 }
2632 }
2633 }
Michael Jurka5130e402011-10-13 04:55:35 -07002634 }
2635
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002636 /**
2637 * Event handler for the (Add) Widgets button that appears after a long press
2638 * on the home screen.
2639 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002640 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002641 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002642 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2643 }
2644
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002645 /**
2646 * Event handler for the wallpaper picker button that appears after a long press
2647 * on the home screen.
2648 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002649 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002650 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2651 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2652 pickWallpaper.setComponent(getWallpaperPickerComponent());
2653 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
2654 }
2655
2656 /**
2657 * Event handler for a click on the settings button that appears after a long press
2658 * on the home screen.
2659 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002660 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002661 if (LOGD) Log.d(TAG, "onClickSettingsButton");
2662 }
2663
Michael Jurka5130e402011-10-13 04:55:35 -07002664 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002665 // Provide the same haptic feedback that the system offers for virtual keys.
2666 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002667 }
2668
Adam Cohen61f560d2013-09-30 15:58:20 -07002669 public void performHapticFeedbackOnTouchDown(View v) {
2670 // Provide the same haptic feedback that the system offers for virtual keys.
2671 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2672 }
2673
2674 public View.OnTouchListener getHapticFeedbackTouchListener() {
2675 if (mHapticFeedbackTouchListener == null) {
2676 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2677 @Override
2678 public boolean onTouch(View v, MotionEvent event) {
2679 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2680 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2681 }
2682 return false;
2683 }
2684 };
2685 }
2686 return mHapticFeedbackTouchListener;
2687 }
2688
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002689 public void onClickAppMarketButton(View v) {
Winson Chungd64a6662013-09-30 11:06:59 -07002690 if (!DISABLE_MARKET_BUTTON) {
2691 if (mAppMarketIntent != null) {
2692 startActivitySafely(v, mAppMarketIntent, "app market");
2693 } else {
2694 Log.e(TAG, "Invalid app market intent.");
2695 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07002696 }
2697 }
2698
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002699 public void onDragStarted(View view) {}
2700
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002701 /**
2702 * Called when the user stops interacting with the launcher.
2703 * This implies that the user is now on the homescreen and is not doing housekeeping.
2704 */
2705 protected void onInteractionEnd() {}
2706
2707 /**
2708 * Called when the user starts interacting with the launcher.
2709 * The possible interactions are:
2710 * - open all apps
2711 * - reorder an app shortcut, or a widget
2712 * - open the overview mode.
2713 * This is a good time to stop doing things that only make sense
2714 * when the user is on the homescreen and not doing housekeeping.
2715 */
2716 protected void onInteractionBegin() {}
2717
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002718 void startApplicationDetailsActivity(ComponentName componentName) {
2719 String packageName = componentName.getPackageName();
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002720 Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS,
2721 Uri.fromParts("package", packageName, null));
Adrian Roos850f9132014-01-13 17:44:52 -08002722 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK |
2723 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Winson Chung3b1b36b2012-04-24 18:51:14 -07002724 startActivitySafely(null, intent, "startApplicationDetailsActivity");
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002725 }
2726
Michael Jurka1e2f4652013-07-08 18:03:46 -07002727 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002728 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2729 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002730 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002731 // System applications cannot be installed. For now, show a toast explaining that.
2732 // We may give them the option of disabling apps this way.
2733 int messageId = R.string.uninstall_system_app_text;
2734 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002735 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002736 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002737 String packageName = componentName.getPackageName();
2738 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002739 Intent intent = new Intent(
2740 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002741 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2742 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002743 if (user != null) {
2744 user.addToIntent(intent, Intent.EXTRA_USER);
2745 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002746 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002747 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002748 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002749 }
2750
Michael Jurka86a720e2012-05-09 11:23:23 -07002751 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002752 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002753 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002754 // Only launch using the new animation if the shortcut has not opted out (this is a
2755 // private contract between launcher and may be ignored in the future).
2756 boolean useLaunchAnimation = (v != null) &&
2757 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002758 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2759 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002760
Kenny Guy1317e2d2014-05-08 18:52:50 +01002761 UserHandleCompat user = null;
2762 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2763 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2764 user = userManager.getUserForSerialNumber(serialNumber);
2765 }
2766
2767 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002768 if (useLaunchAnimation) {
Winson Chungc7450e32012-04-17 17:34:08 -07002769 ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
2770 v.getMeasuredWidth(), v.getMeasuredHeight());
Kenny Guy1317e2d2014-05-08 18:52:50 +01002771 optsBundle = opts.toBundle();
2772 }
Adam Cohen6ea3b112014-06-11 11:38:49 -07002773 if (useLaunchAnimation && Utilities.isLmp()) {
2774 ActivityOptions opts = ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim);
2775 optsBundle = opts.toBundle();
2776 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002777
2778 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2779 // Could be launching some bookkeeping activity
2780 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002781 } else {
Kenny Guyc2bd8102014-06-30 12:30:31 +01002782 launcherApps.startActivityForProfile(intent.getComponent(), user,
2783 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002784 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002785 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002786 } catch (SecurityException e) {
2787 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002788 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002789 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002790 "or use the exported attribute for this activity. "
2791 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002792 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002793 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002794 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002795
Michael Jurka86a720e2012-05-09 11:23:23 -07002796 boolean startActivitySafely(View v, Intent intent, Object tag) {
2797 boolean success = false;
2798 try {
2799 success = startActivity(v, intent, tag);
2800 } catch (ActivityNotFoundException e) {
2801 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2802 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2803 }
2804 return success;
2805 }
2806
Adam Cohen268c4752012-06-06 17:47:33 -07002807 /**
2808 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2809 * in the DragLayer in the exact absolute location of the original FolderIcon.
2810 */
2811 private void copyFolderIconToImage(FolderIcon fi) {
2812 final int width = fi.getMeasuredWidth();
2813 final int height = fi.getMeasuredHeight();
2814
2815 // Lazy load ImageView, Bitmap and Canvas
2816 if (mFolderIconImageView == null) {
2817 mFolderIconImageView = new ImageView(this);
2818 }
2819 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2820 mFolderIconBitmap.getHeight() != height) {
2821 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2822 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2823 }
2824
2825 DragLayer.LayoutParams lp;
2826 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
2827 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
2828 } else {
2829 lp = new DragLayer.LayoutParams(width, height);
2830 }
2831
Adam Cohen307fe232012-08-16 17:55:58 -07002832 // The layout from which the folder is being opened may be scaled, adjust the starting
2833 // view size by this scale factor.
2834 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07002835 lp.customPosition = true;
2836 lp.x = mRectForFolderAnimation.left;
2837 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07002838 lp.width = (int) (scale * width);
2839 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07002840
2841 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
2842 fi.draw(mFolderIconCanvas);
2843 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07002844 if (fi.getFolder() != null) {
2845 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
2846 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07002847 }
Adam Cohen268c4752012-06-06 17:47:33 -07002848 // Just in case this image view is still in the drag layer from a previous animation,
2849 // we remove it and re-add it.
2850 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
2851 mDragLayer.removeView(mFolderIconImageView);
2852 }
2853 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07002854 if (fi.getFolder() != null) {
2855 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07002856 }
Adam Cohen268c4752012-06-06 17:47:33 -07002857 }
2858
Adam Cohen2801caf2011-05-13 20:57:39 -07002859 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002860 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002861 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
2862 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
2863 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
2864
Adam Cohenc51934b2011-07-26 21:07:43 -07002865 FolderInfo info = (FolderInfo) fi.getTag();
2866 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
2867 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07002868 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
2869 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07002870 }
2871
Adam Cohen268c4752012-06-06 17:47:33 -07002872 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
2873 copyFolderIconToImage(fi);
2874 fi.setVisibility(View.INVISIBLE);
2875
Michael Jurka2ecf9952012-06-18 12:52:28 -07002876 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002877 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002878 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
2879 oa.start();
2880 }
2881
Adam Cohen268c4752012-06-06 17:47:33 -07002882 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002883 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07002884 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
2885 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
2886 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
2887
Adam Cohen268c4752012-06-06 17:47:33 -07002888 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07002889
Adam Cohen268c4752012-06-06 17:47:33 -07002890 // We remove and re-draw the FolderIcon in-case it has changed
2891 mDragLayer.removeView(mFolderIconImageView);
2892 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07002893 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07002894 scaleX, scaleY);
Adam Cohen2801caf2011-05-13 20:57:39 -07002895 oa.setDuration(getResources().getInteger(R.integer.config_folderAnimDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07002896 oa.addListener(new AnimatorListenerAdapter() {
2897 @Override
2898 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07002899 if (cl != null) {
2900 cl.clearFolderLeaveBehind();
2901 // Remove the ImageView copy of the FolderIcon and make the original visible.
2902 mDragLayer.removeView(mFolderIconImageView);
2903 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07002904 }
2905 }
2906 });
Adam Cohen2801caf2011-05-13 20:57:39 -07002907 oa.start();
2908 }
2909
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002910 /**
Michael Jurka774bd372010-10-22 13:40:50 -07002911 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002912 * is animated relative to the specified View. If the View is null, no animation
2913 * is played.
2914 *
2915 * @param folderInfo The FolderInfo describing the folder to open.
2916 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07002917 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07002918 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07002919 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002920
Adam Cohena9cf38f2011-05-02 15:36:58 -07002921 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002922
Adam Cohen4554ee12011-08-03 16:13:21 -07002923 // Just verify that the folder hasn't already been added to the DragLayer.
2924 // There was a one-off crash where the folder had a parent already.
2925 if (folder.getParent() == null) {
2926 mDragLayer.addView(folder);
2927 mDragController.addDropTarget((DropTarget) folder);
2928 } else {
2929 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
2930 folder.getParent() + ").");
2931 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07002932 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07002933 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07002934
2935 // Notify the accessibility manager that this folder "window" has appeared and occluded
2936 // the workspace items
2937 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
2938 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07002939 }
2940
2941 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08002942 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07002943 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07002944 if (folder.isEditingName()) {
2945 folder.dismissEditingName();
2946 }
Adam Cohen4554ee12011-08-03 16:13:21 -07002947 closeFolder(folder);
Winson Chung7d7541e2011-09-16 20:14:36 -07002948
2949 // Dismiss the folder cling
Winson Chunga6945242014-01-08 14:04:34 -08002950 mLauncherClings.dismissFolderCling(null);
Adam Cohen4554ee12011-08-03 16:13:21 -07002951 }
2952 }
2953
2954 void closeFolder(Folder folder) {
2955 folder.getInfo().opened = false;
2956
2957 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
2958 if (parent != null) {
2959 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
2960 shrinkAndFadeInFolderIcon(fi);
2961 }
2962 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07002963
2964 // Notify the accessibility manager that this folder "window" has disappeard and no
2965 // longer occludeds the workspace items
2966 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002967 }
2968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002969 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07002970 if (!isDraggingEnabled()) return false;
2971 if (isWorkspaceLocked()) return false;
2972 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002973
Adam Cohen1697b792013-09-17 19:08:21 -07002974 if (v instanceof Workspace) {
2975 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07002976 if (mWorkspace.enterOverviewMode()) {
2977 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
2978 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
2979 return true;
2980 } else {
2981 return false;
2982 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07002983 } else {
2984 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07002985 }
Adam Cohen1697b792013-09-17 19:08:21 -07002986 }
2987
Adam Cohene0aaa0d2014-05-12 12:44:22 -07002988 CellLayout.CellInfo longClickCellInfo = null;
2989 View itemUnderLongClick = null;
2990 if (v.getTag() instanceof ItemInfo) {
2991 ItemInfo info = (ItemInfo) v.getTag();
2992 longClickCellInfo = new CellLayout.CellInfo(v, info);;
2993 itemUnderLongClick = longClickCellInfo.cell;
2994 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002995 }
2996
Winson Chung3d503fb2011-07-13 17:25:49 -07002997 // The hotseat touch handling does not go through Workspace, and we always allow long press
2998 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05002999 final boolean inHotseat = isHotseatLayout(v);
3000 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003001 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003002 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003003 // User long pressed on empty space
3004 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3005 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003006 if (mWorkspace.isInOverviewMode()) {
3007 mWorkspace.startReordering(v);
3008 } else {
3009 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003010 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003011 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003012 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3013 mHotseat.getOrderInHotseat(
3014 longClickCellInfo.cellX,
3015 longClickCellInfo.cellY));
3016 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003017 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003018 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003019 }
3020 }
3021 }
3022 return true;
3023 }
3024
Winson Chung3d503fb2011-07-13 17:25:49 -07003025 boolean isHotseatLayout(View layout) {
3026 return mHotseat != null && layout != null &&
3027 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3028 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003029
Winson Chung3d503fb2011-07-13 17:25:49 -07003030 /**
3031 * Returns the CellLayout of the specified container at the specified screen.
3032 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003033 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003034 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3035 if (mHotseat != null) {
3036 return mHotseat.getLayout();
3037 } else {
3038 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003039 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003040 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003041 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003042 }
3043 }
3044
Daniel Sandler843e8602010-06-07 14:59:01 -04003045 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003046 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003047 }
3048
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003049 /**
3050 * Helper method for the cameraZoomIn/cameraZoomOut animations
3051 * @param view The view being animated
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003052 * @param scaleFactor The scale factor used for the zoom
3053 */
Michael Jurkab3e22d92011-10-31 15:58:33 -07003054 private void setPivotsForZoom(View view, float scaleFactor) {
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003055 view.setPivotX(view.getWidth() / 2.0f);
Adam Cohen7777d962011-08-18 18:58:38 -07003056 view.setPivotY(view.getHeight() / 2.0f);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003057 }
Daniel Sandlerc351eb82010-03-03 15:05:19 -05003058
Craig Mautner360310b2012-10-26 15:13:08 -07003059 private void setWorkspaceBackground(boolean workspace) {
3060 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003061 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003062 }
3063
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003064 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003065 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3066 int curflags = getWindow().getAttributes().flags
3067 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3068 if (wpflags != curflags) {
3069 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3070 }
Craig Mautner360310b2012-10-26 15:13:08 -07003071 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003072 }
3073
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003074 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3075 if (v instanceof LauncherTransitionable) {
3076 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3077 }
3078 }
3079
Michael Jurkabed61d22012-02-14 22:51:29 -08003080 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3081 if (v instanceof LauncherTransitionable) {
3082 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3083 }
Winson Chung70442722012-02-10 15:43:22 -08003084
3085 // Update the workspace transition step as well
3086 dispatchOnLauncherTransitionStep(v, 0f);
3087 }
3088
3089 private void dispatchOnLauncherTransitionStep(View v, float t) {
3090 if (v instanceof LauncherTransitionable) {
3091 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3092 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003093 }
3094
3095 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3096 if (v instanceof LauncherTransitionable) {
3097 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3098 }
Winson Chung70442722012-02-10 15:43:22 -08003099
3100 // Update the workspace transition step as well
3101 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003102 }
3103
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003104 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003105 * Things to test when changing the following seven functions.
3106 * - Home from workspace
3107 * - from center screen
3108 * - from other screens
3109 * - Home from all apps
3110 * - from center screen
3111 * - from other screens
3112 * - Back from all apps
3113 * - from center screen
3114 * - from other screens
3115 * - Launch app from workspace and quit
3116 * - with back
3117 * - with home
3118 * - Launch app from all apps and quit
3119 * - with back
3120 * - with home
3121 * - Go to a screen that's not the default, then all
3122 * apps, and launch and app, and go back
3123 * - with back
3124 * -with home
3125 * - On workspace, long press power and go back
3126 * - with back
3127 * - with home
3128 * - On all apps, long press power and go back
3129 * - with back
3130 * - with home
3131 * - On workspace, power off
3132 * - On all apps, power off
3133 * - Launch an app and turn off the screen while in that app
3134 * - Go back with home key
3135 * - Go back with back key TODO: make this not go to workspace
3136 * - From all apps
3137 * - From workspace
3138 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3139 * - From all apps
3140 * - From the center workspace
3141 * - From another workspace
3142 */
3143
3144 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003145 * Zoom the camera out from the workspace to reveal 'toView'.
3146 * Assumes that the view to show is anchored at either the very top or very bottom
3147 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003148 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003149 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003150 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3151 showAppsCustomizeHelper(animated, springLoaded, contentType);
3152 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003153
Winson Chungc58497e2013-09-03 17:48:37 -07003154 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3155 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003156 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003157 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003158 mStateAnimation.cancel();
3159 mStateAnimation = null;
3160 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003161
3162 boolean material = Utilities.isLmp();
3163
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003164 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003165
Winson Chungf0ea4d32011-06-06 14:27:16 -07003166 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3167 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003168 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
3169
Winson Chungf0ea4d32011-06-06 14:27:16 -07003170 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003171 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003172 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Adam Cohencff6af82011-09-13 14:51:53 -07003173 final int startDelay =
3174 res.getInteger(R.integer.config_workspaceAppsCustomizeAnimationStagger);
Patrick Dubroy558654c2010-07-23 16:48:11 -07003175
Michael Jurkab3e22d92011-10-31 15:58:33 -07003176 setPivotsForZoom(toView, scale);
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003177
Adam Cohen6c5891a2014-07-09 23:53:15 -07003178 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3179 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003180 Animator workspaceAnim =
Adam Cohen6c5891a2014-07-09 23:53:15 -07003181 mWorkspace.getChangeStateAnimation(workspaceState, animated);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003182 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003183 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3184 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003185 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3186 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003187
3188 if (animated) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07003189 if (!material) {
3190 toView.setScaleX(scale);
3191 toView.setScaleY(scale);
3192 final LauncherViewPropertyAnimator scaleAnim =
3193 new LauncherViewPropertyAnimator(toView);
3194 scaleAnim.
3195 scaleX(1f).scaleY(1f).
3196 setDuration(duration).
3197 setInterpolator(new Workspace.ZoomOutInterpolator());
Adam Cohen61033d32010-11-15 18:29:44 -08003198
Adam Cohen6c5891a2014-07-09 23:53:15 -07003199 toView.setVisibility(View.VISIBLE);
3200 toView.setAlpha(0f);
3201 final ObjectAnimator alphaAnim = LauncherAnimUtils
3202 .ofFloat(toView, "alpha", 0f, 1f)
3203 .setDuration(fadeDuration);
3204 alphaAnim.setInterpolator(new DecelerateInterpolator(1.5f));
3205 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3206 @Override
3207 public void onAnimationUpdate(ValueAnimator animation) {
3208 if (animation == null) {
3209 throw new RuntimeException("animation is null");
3210 }
3211 float t = (Float) animation.getAnimatedValue();
3212 dispatchOnLauncherTransitionStep(fromView, t);
3213 dispatchOnLauncherTransitionStep(toView, t);
Michael Jurka059798a2012-09-04 09:20:16 -07003214 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003215 });
Adam Cohenf16e5712011-01-13 13:31:45 -08003216
Adam Cohen6c5891a2014-07-09 23:53:15 -07003217 // toView should appear right at the end of the workspace shrink
3218 // animation
3219 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
3220 mStateAnimation.play(scaleAnim).after(startDelay);
3221 mStateAnimation.play(alphaAnim).after(startDelay);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003222
Adam Cohen6c5891a2014-07-09 23:53:15 -07003223 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3224 @Override
3225 public void onAnimationStart(Animator animation) {
3226 // Prepare the position
3227 toView.setTranslationX(0.0f);
3228 toView.setTranslationY(0.0f);
3229 toView.setVisibility(View.VISIBLE);
3230 toView.bringToFront();
Winson Chungadf0c182012-08-23 14:59:07 -07003231 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003232 @Override
3233 public void onAnimationEnd(Animator animation) {
3234 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3235 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003236
Adam Cohen6c5891a2014-07-09 23:53:15 -07003237 // Hide the search bar
3238 if (mSearchDropTargetBar != null) {
3239 mSearchDropTargetBar.hideSearchBar(false);
3240 }
3241 }
3242 });
3243 } else {
3244 int width = toView.getMeasuredWidth();
3245 int height = toView.getMeasuredHeight();
3246 float revealRadius = (float) Math.sqrt((width * width) / 4 + height * height);
3247
3248 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
3249
3250 AppsCustomizePagedView content = (AppsCustomizePagedView)
3251 toView.findViewById(R.id.apps_customize_pane_content);
3252
3253 View page = content.getPageAt(content.getCurrentPage());
3254 View revealView = content;
3255
3256 float yDrift = height / 2f - 400;
3257
3258 LauncherViewPropertyAnimator panelAlphaAndDrift =
3259 new LauncherViewPropertyAnimator(revealView);
3260 revealView.setTranslationY(yDrift);
3261 revealView.setAlpha(0.3f);
3262 panelAlphaAndDrift.alpha(1)
3263 .translationY(0)
3264 .setDuration(revealDuration)
3265 .setInterpolator(new LogDecelerateInterpolator(100, 0));
3266
3267 mStateAnimation.play(panelAlphaAndDrift);
3268
3269 if (page instanceof CellLayout) {
3270 CellLayout cellLayout = (CellLayout) page;
3271 cellLayout.enableHardwareLayer(true);
3272
3273 View iconsView = cellLayout.getShortcutsAndWidgets();
3274 iconsView.setAlpha(0f);
3275
3276 LauncherViewPropertyAnimator iconsAlpha =
3277 new LauncherViewPropertyAnimator(iconsView);
3278 iconsAlpha.alpha(1f)
3279 .setDuration(revealDuration - 100)
3280 .setInterpolator(new LogDecelerateInterpolator(100, 0));
3281 mStateAnimation.play(iconsAlpha);
3282 }
3283
3284 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3285 pageIndicators.setAlpha(0f);
3286 final LauncherViewPropertyAnimator indicatorsAlpha =
3287 new LauncherViewPropertyAnimator(pageIndicators);
3288 indicatorsAlpha.alpha(1f);
3289 indicatorsAlpha.setDuration(revealDuration);
3290 mStateAnimation.play(indicatorsAlpha);
3291
3292 width = revealView.getMeasuredWidth();
3293
3294 ValueAnimator reveal =
3295 ViewAnimationUtils.createCircularReveal(revealView, width / 2,
3296 height / 2 + 100, 0f, revealRadius);
3297 reveal.setDuration(revealDuration);
3298 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3299
3300 toView.setTranslationX(0);
3301 toView.setTranslationY(0);
3302 toView.setAlpha(1f);
3303 // toView should appear right at the end of the workspace shrink
3304 // animation
3305 mStateAnimation.play(reveal);
3306
3307 reveal.addListener(new AnimatorListenerAdapter() {
3308 @Override
3309 public void onAnimationStart(Animator animation) {
3310 // Prepare the position
3311 toView.bringToFront();
3312 toView.setVisibility(View.VISIBLE);
3313 }
3314 });
3315
3316 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3317 @Override
3318 public void onAnimationEnd(Animator animation) {
3319 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3320 dispatchOnLauncherTransitionEnd(toView, animated, false);
3321
3322 // Hide the search bar
3323 if (mSearchDropTargetBar != null) {
3324 mSearchDropTargetBar.hideSearchBar(false);
3325 }
3326 }
3327 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003328 }
Michael Jurka1899a362011-11-03 13:50:45 -07003329
3330 boolean delayAnim = false;
Adam Cohen6c5891a2014-07-09 23:53:15 -07003331 if (workspaceAnim != null) {
3332 mStateAnimation.play(workspaceAnim);
3333 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003334 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3335 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003336
3337 // If any of the objects being animated haven't been measured/laid out
3338 // yet, delay the animation until we get a layout pass
Michael Jurkabed61d22012-02-14 22:51:29 -08003339 if ((((LauncherTransitionable) toView).getContent().getMeasuredWidth() == 0) ||
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003340 (mWorkspace.getMeasuredWidth() == 0) ||
3341 (toView.getMeasuredWidth() == 0)) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003342 delayAnim = true;
Michael Jurka1899a362011-11-03 13:50:45 -07003343 }
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003344
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003345 final AnimatorSet stateAnimation = mStateAnimation;
3346 final Runnable startAnimRunnable = new Runnable() {
3347 public void run() {
3348 // Check that mStateAnimation hasn't changed while
3349 // we waited for a layout/draw pass
3350 if (mStateAnimation != stateAnimation)
3351 return;
3352 setPivotsForZoom(toView, scale);
3353 dispatchOnLauncherTransitionStart(fromView, animated, false);
3354 dispatchOnLauncherTransitionStart(toView, animated, false);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003355 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003356 }
3357 };
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003358 if (delayAnim) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003359 final ViewTreeObserver observer = toView.getViewTreeObserver();
3360 observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
3361 public void onGlobalLayout() {
3362 startAnimRunnable.run();
3363 toView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
3364 }
3365 });
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003366 } else {
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003367 startAnimRunnable.run();
Michael Jurka1899a362011-11-03 13:50:45 -07003368 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003369 } else {
3370 toView.setTranslationX(0.0f);
3371 toView.setTranslationY(0.0f);
3372 toView.setScaleX(1.0f);
3373 toView.setScaleY(1.0f);
3374 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003375 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003376
Daniel Sandlere4f98912013-06-25 15:13:26 -04003377 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003378 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003379 if (mSearchDropTargetBar != null) {
3380 mSearchDropTargetBar.hideSearchBar(false);
3381 }
Michael Jurkaabded662011-03-04 12:06:57 -08003382 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003383 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003384 dispatchOnLauncherTransitionStart(fromView, animated, false);
3385 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003386 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003387 dispatchOnLauncherTransitionStart(toView, animated, false);
3388 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003389 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003390 }
3391
3392 /**
3393 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003394 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003395 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003396 */
Adam Cohened307df2013-10-02 09:37:31 -07003397 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003398 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003399
Michael Jurkab3e22d92011-10-31 15:58:33 -07003400 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003401 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003402 mStateAnimation.cancel();
3403 mStateAnimation = null;
3404 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003405
3406 boolean material = Utilities.isLmp();
3407
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003408 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003409
Winson Chungf0ea4d32011-06-06 14:27:16 -07003410 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003411 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
3412 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
3413
Winson Chungf0ea4d32011-06-06 14:27:16 -07003414 final float scaleFactor = (float)
3415 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3416 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003417 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003418 Animator workspaceAnim = null;
Adam Cohened307df2013-10-02 09:37:31 -07003419 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003420 int stagger = res.getInteger(R.integer.config_appsCustomizeWorkspaceAnimationStagger);
3421 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003422 toState, animated, stagger, -1);
3423 } else if (toState == Workspace.State.SPRING_LOADED ||
3424 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003425 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohened307df2013-10-02 09:37:31 -07003426 toState, animated);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003427 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003428
Michael Jurkab3e22d92011-10-31 15:58:33 -07003429 setPivotsForZoom(fromView, scaleFactor);
Winson Chung4afe9b32011-07-27 17:46:20 -07003430 showHotseat(animated);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003431 if (animated) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07003432 if (!material) {
3433 final LauncherViewPropertyAnimator scaleAnim =
3434 new LauncherViewPropertyAnimator(fromView);
3435 scaleAnim.
3436 scaleX(scaleFactor).scaleY(scaleFactor).
3437 setDuration(duration).
3438 setInterpolator(new Workspace.ZoomInInterpolator());
Michael Jurka159b4cc2012-01-17 03:00:35 -08003439
Adam Cohen6c5891a2014-07-09 23:53:15 -07003440 final ObjectAnimator alphaAnim = LauncherAnimUtils
3441 .ofFloat(fromView, "alpha", 1f, 0f)
3442 .setDuration(fadeOutDuration);
3443 alphaAnim.setInterpolator(new AccelerateDecelerateInterpolator());
3444 alphaAnim.addUpdateListener(new AnimatorUpdateListener() {
3445 @Override
3446 public void onAnimationUpdate(ValueAnimator animation) {
3447 float t = 1f - (Float) animation.getAnimatedValue();
3448 dispatchOnLauncherTransitionStep(fromView, t);
3449 dispatchOnLauncherTransitionStep(toView, t);
3450 }
3451 });
3452
3453 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
3454
3455 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3456 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3457 mAppsCustomizeContent.stopScrolling();
3458
3459 mStateAnimation.playTogether(scaleAnim, alphaAnim);
3460 } else {
3461 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
3462
3463 int width = fromView.getMeasuredWidth();
3464 int height = fromView.getMeasuredHeight();
3465 float revealRadius = (float) Math.sqrt((width * width) / 4 + height * height);
3466
3467 AppsCustomizePagedView content = (AppsCustomizePagedView)
3468 fromView.findViewById(R.id.apps_customize_pane_content);
3469
3470 final View page = content.getPageAt(content.getNextPage());
3471 View revealView = page;
3472
3473 float yDrift = height / 2f - 400;
3474
3475 LauncherViewPropertyAnimator panelAlphaAndDrift =
3476 new LauncherViewPropertyAnimator(revealView);
3477 revealView.setTranslationY(0);
3478 revealView.setAlpha(1);
3479 panelAlphaAndDrift.alpha(0)
3480 .translationY(yDrift)
3481 .setDuration(revealDuration)
3482 .setInterpolator(new LogDecelerateInterpolator(100, 0));
3483
3484 mStateAnimation.play(panelAlphaAndDrift);
3485
3486 if (page instanceof CellLayout) {
3487 final CellLayout cellLayout = (CellLayout) page;
3488 cellLayout.enableHardwareLayer(true);
3489
3490 final View iconsView = cellLayout.getShortcutsAndWidgets();
3491
3492 LauncherViewPropertyAnimator iconsAlpha =
3493 new LauncherViewPropertyAnimator(iconsView);
3494 iconsAlpha.alpha(0f)
3495 .setDuration(revealDuration - 100)
3496 .setInterpolator(new LogDecelerateInterpolator(100, 0));
3497
3498 mStateAnimation.play(iconsAlpha);
3499
3500 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3501 @Override
3502 public void onAnimationEnd(Animator animation) {
3503 cellLayout.setTranslationY(0);
3504 cellLayout.setAlpha(1f);
3505 iconsView.setAlpha(1f);
3506 }
3507 });
Winson Chung70442722012-02-10 15:43:22 -08003508 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003509
Adam Cohen6c5891a2014-07-09 23:53:15 -07003510 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3511 final LauncherViewPropertyAnimator indicatorsAlpha =
3512 new LauncherViewPropertyAnimator(pageIndicators);
3513 indicatorsAlpha.alpha(0f);
3514 indicatorsAlpha.setDuration(revealDuration);
3515 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3516 mStateAnimation.play(indicatorsAlpha);
Michael Jurkabed61d22012-02-14 22:51:29 -08003517
Adam Cohen6c5891a2014-07-09 23:53:15 -07003518 width = revealView.getMeasuredWidth();
3519
3520 ValueAnimator reveal =
3521 ViewAnimationUtils.createCircularReveal(revealView, width / 2,
3522 height / 2 + 100, revealRadius, 0f);
3523 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3524 reveal.setDuration(revealDuration);
3525
3526 reveal.addListener(new AnimatorListenerAdapter() {
3527 @Override
3528 public void onAnimationEnd(Animator animation) {
3529 fromView.setVisibility(View.GONE);
3530 }
3531 });
3532
3533 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3534 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3535 mAppsCustomizeContent.stopScrolling();
3536
3537 mStateAnimation.play(reveal);
3538 }
3539 if (workspaceAnim != null) {
3540 mStateAnimation.play(workspaceAnim);
3541 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003542
3543 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003544 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003545 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003546 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003547 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3548 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003549 if (onCompleteRunnable != null) {
3550 onCompleteRunnable.run();
3551 }
Chet Haasebc2f0822012-10-26 17:59:53 -07003552 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003553 }
3554 });
3555
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003556 dispatchOnLauncherTransitionStart(fromView, animated, true);
3557 dispatchOnLauncherTransitionStart(toView, animated, true);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003558 LauncherAnimUtils.startAnimationAfterNextDraw(mStateAnimation, toView);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003559 } else {
3560 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003561 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003562 dispatchOnLauncherTransitionStart(fromView, animated, true);
3563 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003564 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003565 dispatchOnLauncherTransitionStart(toView, animated, true);
3566 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003567 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003568 }
3569
Michael Jurkae326f182011-11-21 14:05:46 -08003570 @Override
3571 public void onTrimMemory(int level) {
3572 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003573 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003574 mAppsCustomizeTabHost.onTrimMemory();
3575 }
3576 }
3577
Adam Cohened307df2013-10-02 09:37:31 -07003578 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003579 showWorkspace(animated, null);
3580 }
3581
Adam Cohened307df2013-10-02 09:37:31 -07003582 protected void showWorkspace() {
3583 showWorkspace(true);
3584 }
3585
Adam Cohened66b2b2012-01-23 17:28:51 -08003586 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003587 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003588 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003589 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003590 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003591
Winson Chungc7d2b602012-05-16 17:02:20 -07003592 // Show the search bar (only animate if we were showing the drop target bar in spring
3593 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003594 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003595 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003596 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003597
Michael Jurkab3e22d92011-10-31 15:58:33 -07003598 // Set focus to the AppsCustomize button
3599 if (mAllAppsButton != null) {
3600 mAllAppsButton.requestFocus();
3601 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003602 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003603
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003604 // Change the state *after* we've called all the transition code
3605 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003606
Winson Chung5fb63472011-02-02 17:03:37 -08003607 // Resume the auto-advance of widgets
3608 mUserPresent = true;
3609 updateRunning();
3610
alanv1d4fde62012-10-17 13:15:47 -07003611 // Send an accessibility event to announce the context change
3612 getWindow().getDecorView()
3613 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003614
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003615 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003616 }
3617
Adam Cohened307df2013-10-02 09:37:31 -07003618 void showOverviewMode(boolean animated) {
3619 mWorkspace.setVisibility(View.VISIBLE);
3620 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3621 mState = State.WORKSPACE;
3622 onWorkspaceShown(animated);
3623 }
3624
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003625 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003626 }
3627
Winson Chung82963d52013-10-09 11:20:57 -07003628 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3629 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003630 if (mState != State.WORKSPACE) return;
3631
Winson Chung82963d52013-10-09 11:20:57 -07003632 if (resetPageToZero) {
3633 mAppsCustomizeTabHost.reset();
3634 }
Winson Chungc58497e2013-09-03 17:48:37 -07003635 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003636 mAppsCustomizeTabHost.post(new Runnable() {
3637 @Override
3638 public void run() {
3639 // We post this in-case the all apps view isn't yet constructed.
3640 mAppsCustomizeTabHost.requestFocus();
3641 }
3642 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003643
Michael Jurkab3e22d92011-10-31 15:58:33 -07003644 // Change the state *after* we've called all the transition code
3645 mState = State.APPS_CUSTOMIZE;
3646
3647 // Pause the auto-advance of widgets until we are out of AllApps
3648 mUserPresent = false;
3649 updateRunning();
3650 closeFolder();
3651
3652 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003653 getWindow().getDecorView()
3654 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003655 }
3656
Adam Cohen7777d962011-08-18 18:58:38 -07003657 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003658 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003659 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003660 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003661 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003662 }
Adam Cohen7777d962011-08-18 18:58:38 -07003663
Adam Cohenad4e15c2013-10-17 16:21:35 -07003664 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003665 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003666 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3667
Winson Chunge7a03942011-08-05 15:05:12 -07003668 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003669 @Override
3670 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003671 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003672 // Before we show workspace, hide all apps again because
3673 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3674 // clean up our state transition functions
3675 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003676 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003677 } else {
3678 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003679 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003680 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003681 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003682 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003683
Michael Jurkad3ef3062010-11-23 16:23:58 -08003684 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003685 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003686 final boolean animated = true;
3687 final boolean springLoaded = true;
3688 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003689 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003690 }
3691 // Otherwise, we are not in spring loaded mode, so don't do anything.
3692 }
3693
Michael Jurkab3e22d92011-10-31 15:58:33 -07003694 void lockAllApps() {
3695 // TODO
3696 }
3697
3698 void unlockAllApps() {
3699 // TODO
3700 }
3701
Winson Chungf0ea4d32011-06-06 14:27:16 -07003702 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003703 * Shows the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003704 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003705 void showHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003706 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003707 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003708 if (mHotseat.getAlpha() != 1f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003709 int duration = 0;
3710 if (mSearchDropTargetBar != null) {
3711 duration = mSearchDropTargetBar.getTransitionInDuration();
3712 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003713 mHotseat.animate().alpha(1f).setDuration(duration);
3714 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003715 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003716 mHotseat.setAlpha(1f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003717 }
3718 }
3719 }
3720
3721 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003722 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003723 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003724 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003725 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003726 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003727 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003728 int duration = 0;
3729 if (mSearchDropTargetBar != null) {
3730 duration = mSearchDropTargetBar.getTransitionOutDuration();
3731 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003732 mHotseat.animate().alpha(0f).setDuration(duration);
3733 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003734 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003735 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003736 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003737 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003738 }
3739
Patrick Dubroy5f445422011-02-18 14:35:21 -08003740 /**
3741 * Add an item from all apps or customize onto the given workspace screen.
3742 * If layout is null, add to the current screen.
3743 */
3744 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003745 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003746 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003747 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003748 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003749
Winson Chungdff8ebb2011-09-08 17:25:31 -07003750 /** Maps the current orientation to an index for referencing orientation correct global icons */
3751 private int getCurrentOrientationIndexForGlobalIcons() {
3752 // default - 0, landscape - 1
3753 switch (getResources().getConfiguration().orientation) {
3754 case Configuration.ORIENTATION_LANDSCAPE:
3755 return 1;
3756 default:
3757 return 0;
3758 }
3759 }
3760
Michael Jurkaae65ee32012-04-30 11:45:54 -07003761 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003762 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003763 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003764 // Look for the toolbar icon specified in the activity meta-data
3765 Bundle metaData = packageManager.getActivityInfo(
3766 activityName, PackageManager.GET_META_DATA).metaData;
3767 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003768 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003769 if (iconResId != 0) {
3770 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003771 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003772 }
3773 }
3774 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07003775 // This can happen if the activity defines an invalid drawable
3776 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
3777 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01003778 } catch (Resources.NotFoundException nfe) {
3779 // This can happen if the activity defines an invalid drawable
3780 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
3781 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003782 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003783 return null;
3784 }
3785
3786 // if successful in getting icon, return it; otherwise, set button to use default drawable
3787 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003788 int buttonId, ComponentName activityName, int fallbackDrawableId,
3789 String toolbarResourceName) {
3790 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07003791 Resources r = getResources();
3792 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
3793 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003794
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003795 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003796 // If we were unable to find the icon via the meta-data, use a generic one
3797 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07003798 toolbarIcon = r.getDrawable(fallbackDrawableId);
3799 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003800 if (button != null) {
3801 button.setCompoundDrawables(toolbarIcon, null, null, null);
3802 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003803 return null;
3804 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07003805 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07003806 if (button != null) {
3807 button.setCompoundDrawables(toolbarIcon, null, null, null);
3808 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003809 return toolbarIcon.getConstantState();
3810 }
3811 }
3812
3813 // if successful in getting icon, return it; otherwise, set button to use default drawable
3814 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003815 int buttonId, ComponentName activityName, int fallbackDrawableId,
3816 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003817 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07003818 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003819
Michael Jurka19e0fc52011-07-22 18:00:21 -07003820 if (button != null) {
3821 // If we were unable to find the icon via the meta-data, use a
3822 // generic one
3823 if (toolbarIcon == null) {
3824 button.setImageResource(fallbackDrawableId);
3825 } else {
3826 button.setImageDrawable(toolbarIcon);
3827 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003828 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07003829
3830 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
3831
Michael Jurka0423dcf2010-10-05 14:56:18 -07003832 }
3833
Winson Chung1b884092012-06-08 17:13:02 -07003834 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003835 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07003836 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003837 }
3838
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003839 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08003840 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08003841 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08003842 }
3843
Winson Chungbb185bd2011-11-21 12:31:42 -08003844 private void invalidatePressedFocusedStates(View container, View button) {
3845 if (container instanceof HolographicLinearLayout) {
3846 HolographicLinearLayout layout = (HolographicLinearLayout) container;
3847 layout.invalidatePressedFocusedStates();
3848 } else if (button instanceof HolographicImageView) {
3849 HolographicImageView view = (HolographicImageView) button;
3850 view.invalidatePressedFocusedStates();
3851 }
3852 }
3853
Cristina Stancu476493b2013-08-07 17:20:14 +01003854 public View getQsbBar() {
Adam Cohen24ce0b32014-01-14 16:18:14 -08003855 if (mQsb == null) {
3856 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
3857 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01003858 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003859 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003860 }
3861
3862 protected boolean updateGlobalSearchIcon() {
Winson Chungc51db6a2011-10-05 11:44:49 -07003863 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003864 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07003865 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003866 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003867
Winson Chung1cad91e2011-05-25 17:41:01 -07003868 final SearchManager searchManager =
3869 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3870 ComponentName activityName = searchManager.getGlobalSearchActivity();
3871 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003872 int coi = getCurrentOrientationIndexForGlobalIcons();
3873 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003874 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3875 TOOLBAR_SEARCH_ICON_METADATA_NAME);
3876 if (sGlobalSearchIcon[coi] == null) {
3877 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3878 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
3879 TOOLBAR_ICON_METADATA_NAME);
3880 }
3881
Winson Chungc51db6a2011-10-05 11:44:49 -07003882 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
3883 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08003884 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003885 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003886 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07003887 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07003888 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
3889 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003890 if (searchButton != null) searchButton.setVisibility(View.GONE);
3891 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003892 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003893 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08003894 }
3895 }
3896
Cristina Stancu476493b2013-08-07 17:20:14 +01003897 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003898 final View searchButtonContainer = findViewById(R.id.search_button_container);
3899 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003900 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003901 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003902 }
3903
Cristina Stancu476493b2013-08-07 17:20:14 +01003904 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Winson Chungc51db6a2011-10-05 11:44:49 -07003905 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07003906 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07003907
Winson Chungc51db6a2011-10-05 11:44:49 -07003908 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07003909 final SearchManager searchManager =
3910 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
3911 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
3912
3913 ComponentName activityName = null;
3914 if (globalSearchActivity != null) {
3915 // Check if the global search activity handles voice search
3916 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3917 intent.setPackage(globalSearchActivity.getPackageName());
3918 activityName = intent.resolveActivity(getPackageManager());
3919 }
3920
3921 if (activityName == null) {
3922 // Fallback: check if an activity other than the global search activity
3923 // resolves this
3924 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
3925 activityName = intent.resolveActivity(getPackageManager());
3926 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003927 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07003928 int coi = getCurrentOrientationIndexForGlobalIcons();
3929 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07003930 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3931 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
3932 if (sVoiceSearchIcon[coi] == null) {
3933 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
3934 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
3935 TOOLBAR_ICON_METADATA_NAME);
3936 }
Winson Chungc51db6a2011-10-05 11:44:49 -07003937 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07003938 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07003939 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08003940 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07003941 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07003942 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07003943 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04003944 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003945 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07003946 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003947 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003948 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003949
Cristina Stancu476493b2013-08-07 17:20:14 +01003950 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Winson Chungbb185bd2011-11-21 12:31:42 -08003951 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
3952 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003953 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08003954 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08003955 }
3956
Winson Chung5841efa2013-09-30 18:06:44 -07003957 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003958 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
3959 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07003960 boolean visible = !forceDisableVoiceButtonProxy &&
3961 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003962 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07003963 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01003964 }
3965 }
Winson Chung5841efa2013-09-30 18:06:44 -07003966
3967 /**
3968 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
3969 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
3970 */
3971 public void disableVoiceButtonProxy(boolean disabled) {
3972 updateVoiceButtonProxyVisible(disabled);
3973 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003974 /**
Winson Chungeb66b142011-06-16 13:14:22 -07003975 * Sets the app market icon
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07003976 */
3977 private void updateAppMarketIcon() {
Winson Chungd64a6662013-09-30 11:06:59 -07003978 if (!DISABLE_MARKET_BUTTON) {
3979 final View marketButton = findViewById(R.id.market_button);
3980 Intent intent = new Intent(Intent.ACTION_MAIN).addCategory(Intent.CATEGORY_APP_MARKET);
3981 // Find the app market activity by resolving an intent.
3982 // (If multiple app markets are installed, it will return the ResolverActivity.)
3983 ComponentName activityName = intent.resolveActivity(getPackageManager());
3984 if (activityName != null) {
3985 int coi = getCurrentOrientationIndexForGlobalIcons();
3986 mAppMarketIntent = intent;
3987 sAppMarketIcon[coi] = updateTextButtonWithIconFromExternalActivity(
3988 R.id.market_button, activityName, R.drawable.ic_launcher_market_holo,
3989 TOOLBAR_ICON_METADATA_NAME);
3990 marketButton.setVisibility(View.VISIBLE);
3991 } else {
3992 // We should hide and disable the view so that we don't try and restore the visibility
3993 // of it when we swap between drag & normal states from IconDropTarget subclasses.
3994 marketButton.setVisibility(View.GONE);
3995 marketButton.setEnabled(false);
3996 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07003997 }
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003998 }
3999
Michael Jurkae7bf83b2010-12-14 18:02:21 -08004000 private void updateAppMarketIcon(Drawable.ConstantState d) {
Winson Chungd64a6662013-09-30 11:06:59 -07004001 if (!DISABLE_MARKET_BUTTON) {
4002 // Ensure that the new drawable we are creating has the approprate toolbar icon bounds
4003 Resources r = getResources();
4004 Drawable marketIconDrawable = d.newDrawable(r);
4005 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
4006 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
4007 marketIconDrawable.setBounds(0, 0, w, h);
Winson Chung1b884092012-06-08 17:13:02 -07004008
Winson Chungd64a6662013-09-30 11:06:59 -07004009 updateTextButtonWithDrawable(R.id.market_button, marketIconDrawable);
4010 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08004011 }
4012
Michael Jurkad7c28052012-04-27 15:43:36 -07004013 @Override
4014 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004015 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004016 final List<CharSequence> text = event.getText();
4017 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004018 // Populate event with a fake title based on the current state.
4019 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004020 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004021 } else {
4022 text.add(getString(R.string.all_apps_home_button_label));
4023 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004024 return result;
4025 }
4026
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004027 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004028 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004029 */
4030 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4031 @Override
4032 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004033 closeSystemDialogs();
4034 }
4035 }
4036
4037 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004038 * Receives notifications whenever the appwidgets are reset.
4039 */
4040 private class AppWidgetResetObserver extends ContentObserver {
4041 public AppWidgetResetObserver() {
4042 super(new Handler());
4043 }
4044
4045 @Override
4046 public void onChange(boolean selfChange) {
4047 onAppWidgetReset();
4048 }
4049 }
4050
4051 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004052 * If the activity is currently paused, signal that we need to run the passed Runnable
4053 * in onResume.
4054 *
4055 * This needs to be called from incoming places where resources might have been loaded
4056 * while we are paused. That is becaues the Configuration might be wrong
4057 * when we're not running, and if it comes back to what it was when we
4058 * were paused, we are not restarted.
4059 *
4060 * Implementation of the method from LauncherModel.Callbacks.
4061 *
4062 * @return true if we are currently paused. The caller might be able to
4063 * skip some work in that case since we will come back again.
4064 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004065 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004066 if (mPaused) {
4067 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004068 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004069 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004070 }
4071 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004072 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004073 return true;
4074 } else {
4075 return false;
4076 }
4077 }
4078
Michael Jurkac402cd92013-05-20 15:49:32 +02004079 private boolean waitUntilResume(Runnable run) {
4080 return waitUntilResume(run, false);
4081 }
4082
Michael Jurka1e2f4652013-07-08 18:03:46 -07004083 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004084 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004085 }
4086
Michael Jurka7607c2f2013-04-03 14:33:19 -07004087 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004088 * If the activity is currently paused, signal that we need to re-run the loader
4089 * in onResume.
4090 *
4091 * This needs to be called from incoming places where resources might have been loaded
4092 * while we are paused. That is becaues the Configuration might be wrong
4093 * when we're not running, and if it comes back to what it was when we
4094 * were paused, we are not restarted.
4095 *
4096 * Implementation of the method from LauncherModel.Callbacks.
4097 *
4098 * @return true if we are currently paused. The caller might be able to
4099 * skip some work in that case since we will come back again.
4100 */
4101 public boolean setLoadOnResume() {
4102 if (mPaused) {
4103 Log.i(TAG, "setLoadOnResume");
4104 mOnResumeNeedsLoad = true;
4105 return true;
4106 } else {
4107 return false;
4108 }
4109 }
4110
4111 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004112 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004113 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004114 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004115 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004116 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004117 } else {
4118 return SCREEN_COUNT / 2;
4119 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004120 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004121
Joe Onorato9c1289c2009-08-17 11:03:03 -04004122 /**
4123 * Refreshes the shortcuts shown on the workspace.
4124 *
4125 * Implementation of the method from LauncherModel.Callbacks.
4126 */
4127 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004128 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004129
Michael Jurka7607c2f2013-04-03 14:33:19 -07004130 // If we're starting binding all over again, clear any bind calls we'd postponed in
4131 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4132 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004133 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004134
Winson Chungd64d1762013-08-20 14:37:16 -07004135 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004136 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004137 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004138
Michael Jurka05bf6442011-11-30 20:28:53 -08004139 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004140 if (mHotseat != null) {
4141 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004142 }
4143 }
4144
Adam Cohendcd297f2013-06-18 13:13:40 -07004145 @Override
4146 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004147 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004148
Adam Cohen5084cba2013-09-03 12:01:16 -07004149 // If there are no screens, we need to have an empty screen
4150 if (orderedScreenIds.size() == 0) {
4151 mWorkspace.addExtraEmptyScreen();
4152 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004153
4154 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004155 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004156 if (hasCustomContentToLeft()) {
4157 mWorkspace.createCustomContentContainer();
4158 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004159 }
Winson Chung64359a52013-07-08 17:17:08 -07004160 }
4161
4162 @Override
4163 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004164 // Log to disk
4165 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4166 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4167 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004168 int count = orderedScreenIds.size();
4169 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004170 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004171 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004172 }
4173
Adam Cohen39a06042013-07-19 14:30:12 -07004174 private boolean shouldShowWeightWatcher() {
4175 String spKey = LauncherAppState.getSharedPreferencesKey();
4176 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004177 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004178
4179 return show;
4180 }
4181
4182 private void toggleShowWeightWatcher() {
4183 String spKey = LauncherAppState.getSharedPreferencesKey();
4184 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4185 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4186
4187 show = !show;
4188
4189 SharedPreferences.Editor editor = sp.edit();
4190 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4191 editor.commit();
4192
4193 if (mWeightWatcher != null) {
4194 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4195 }
4196 }
4197
Winson Chungd64d1762013-08-20 14:37:16 -07004198 public void bindAppsAdded(final ArrayList<Long> newScreens,
4199 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004200 final ArrayList<ItemInfo> addAnimated,
4201 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004202 Runnable r = new Runnable() {
4203 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004204 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004205 }
4206 };
4207 if (waitUntilResume(r)) {
4208 return;
4209 }
4210
Winson Chungd64d1762013-08-20 14:37:16 -07004211 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004212 if (newScreens != null) {
4213 bindAddScreens(newScreens);
4214 }
Winson Chungd64d1762013-08-20 14:37:16 -07004215
4216 // We add the items without animation on non-visible pages, and with
4217 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004218 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004219 bindItems(addNotAnimated, 0,
4220 addNotAnimated.size(), false);
4221 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004222 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004223 bindItems(addAnimated, 0,
4224 addAnimated.size(), true);
4225 }
Winson Chungc58497e2013-09-03 17:48:37 -07004226
Winson Chung87412982013-10-03 18:34:14 -07004227 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004228 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004229
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004230 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004231 addedApps != null && mAppsCustomizeContent != null) {
4232 mAppsCustomizeContent.addApps(addedApps);
4233 }
Winson Chungd64d1762013-08-20 14:37:16 -07004234 }
4235
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004236 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004237 * Bind the items start-end from the list.
4238 *
4239 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004240 */
Winson Chung64359a52013-07-08 17:17:08 -07004241 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4242 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004243 Runnable r = new Runnable() {
4244 public void run() {
4245 bindItems(shortcuts, start, end, forceAnimateIcons);
4246 }
4247 };
4248 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004249 return;
4250 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004251
Winson Chungf0c6ae02012-03-21 16:10:31 -07004252 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004253 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4254 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004255 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004256 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004257 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004258 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004259 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004260
4261 // Short circuit if we are loading dock items for a configuration which has no dock
4262 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4263 mHotseat == null) {
4264 continue;
4265 }
4266
Joe Onorato9c1289c2009-08-17 11:03:03 -04004267 switch (item.itemType) {
4268 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4269 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004270 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004271 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004272
Winson Chung64359a52013-07-08 17:17:08 -07004273 /*
4274 * TODO: FIX collision case
4275 */
Winson Chungce376632013-07-11 16:12:41 -07004276 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4277 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4278 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004279 View v = cl.getChildAt(item.cellX, item.cellY);
4280 Object tag = v.getTag();
4281 String desc = "Collision while binding workspace item: " + item
4282 + ". Collides with " + tag;
4283 if (LauncherAppState.isDogfoodBuild()) {
4284 throw (new RuntimeException(desc));
4285 } else {
4286 Log.d(TAG, desc);
4287 }
Winson Chungce376632013-07-11 16:12:41 -07004288 }
Winson Chung64359a52013-07-08 17:17:08 -07004289 }
4290
Adam Cohendcd297f2013-06-18 13:13:40 -07004291 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4292 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004293 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004294 // Animate all the applications up now
4295 shortcut.setAlpha(0f);
4296 shortcut.setScaleX(0f);
4297 shortcut.setScaleY(0f);
4298 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004299 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004300 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004301 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004302 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004303 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004304 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004305 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004306 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4307 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004308 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004309 default:
4310 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004311 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004312 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004313
Winson Chung997a9232013-07-24 15:33:46 -07004314 if (animateIcons) {
4315 // Animate to the correct page
4316 if (newShortcutsScreenId > -1) {
4317 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004318 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004319 final Runnable startBounceAnimRunnable = new Runnable() {
4320 public void run() {
4321 anim.playTogether(bounceAnims);
4322 anim.start();
4323 }
4324 };
Winson Chung997a9232013-07-24 15:33:46 -07004325 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004326 // We post the animation slightly delayed to prevent slowdowns
4327 // when we are loading right after we return to launcher.
4328 mWorkspace.postDelayed(new Runnable() {
4329 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004330 if (mWorkspace != null) {
4331 mWorkspace.snapToPage(newScreenIndex);
4332 mWorkspace.postDelayed(startBounceAnimRunnable,
4333 NEW_APPS_ANIMATION_DELAY);
4334 }
Winson Chung94d67682013-09-25 16:29:40 -07004335 }
4336 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004337 } else {
4338 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004339 }
4340 }
Winson Chung64359a52013-07-08 17:17:08 -07004341 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004342 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004343 }
4344
Joe Onorato9c1289c2009-08-17 11:03:03 -04004345 /**
4346 * Implementation of the method from LauncherModel.Callbacks.
4347 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004348 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004349 Runnable r = new Runnable() {
4350 public void run() {
4351 bindFolders(folders);
4352 }
4353 };
4354 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004355 return;
4356 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004357 sFolders.clear();
4358 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004359 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004360
4361 /**
4362 * Add the views for a widget to the workspace.
4363 *
4364 * Implementation of the method from LauncherModel.Callbacks.
4365 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004366 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004367 Runnable r = new Runnable() {
4368 public void run() {
4369 bindAppWidget(item);
4370 }
4371 };
4372 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004373 return;
4374 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004375
Daniel Sandler843e8602010-06-07 14:59:01 -04004376 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4377 if (DEBUG_WIDGETS) {
4378 Log.d(TAG, "bindAppWidget: " + item);
4379 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004380 final Workspace workspace = mWorkspace;
4381
4382 final int appWidgetId = item.appWidgetId;
4383 final AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
Daniel Sandler843e8602010-06-07 14:59:01 -04004384 if (DEBUG_WIDGETS) {
4385 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4386 }
4387
Joe Onorato9c1289c2009-08-17 11:03:03 -04004388 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4389
Joe Onorato9c1289c2009-08-17 11:03:03 -04004390 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004391 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004392
Adam Cohendcd297f2013-06-18 13:13:40 -07004393 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004394 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004395 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4396
Joe Onorato9c1289c2009-08-17 11:03:03 -04004397 workspace.requestLayout();
4398
Daniel Sandler843e8602010-06-07 14:59:01 -04004399 if (DEBUG_WIDGETS) {
4400 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4401 + (SystemClock.uptimeMillis()-start) + "ms");
4402 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004403 }
4404
Adam Cohen1462de32012-07-24 22:34:36 -07004405 public void onPageBoundSynchronously(int page) {
4406 mSynchronouslyBoundPages.add(page);
4407 }
4408
Joe Onorato9c1289c2009-08-17 11:03:03 -04004409 /**
4410 * Callback saying that there aren't any more items to bind.
4411 *
4412 * Implementation of the method from LauncherModel.Callbacks.
4413 */
Adam Cohene25af792013-06-06 23:08:25 -07004414 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004415 Runnable r = new Runnable() {
4416 public void run() {
4417 finishBindingItems(upgradePath);
4418 }
4419 };
4420 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004421 return;
4422 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004423 if (mSavedState != null) {
4424 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004425 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004426 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004427 mSavedState = null;
4428 }
4429
Adam Cohen1462de32012-07-24 22:34:36 -07004430 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004431
Winson Chungc51db6a2011-10-05 11:44:49 -07004432 // Update the market app icon as necessary (the other icons will be managed in response to
4433 // package changes in bindSearchablesChanged()
Winson Chungd64a6662013-09-30 11:06:59 -07004434 if (!DISABLE_MARKET_BUTTON) {
4435 updateAppMarketIcon();
4436 }
Michael Jurkac1f5d262011-09-30 19:32:27 -07004437
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004438 setWorkspaceLoading(false);
Adam Cohendb364c32014-05-20 14:23:40 -07004439
4440 // If we received the result of any pending adds while the loader was running (e.g. the
4441 // widget configuration forced an orientation change), process them now.
4442 if (sPendingAddItem != null) {
4443 final long screenId = completeAdd(sPendingAddItem);
4444
4445 // TODO: this moves the user to the page where the pending item was added. Ideally,
4446 // the screen would be guaranteed to exist after bind, and the page would be set through
4447 // the workspace restore process.
4448 mWorkspace.post(new Runnable() {
4449 @Override
4450 public void run() {
4451 mWorkspace.snapToScreenId(screenId);
4452 }
4453 });
4454 sPendingAddItem = null;
4455 }
4456
Adam Cohene25af792013-06-06 23:08:25 -07004457 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004458 mWorkspace.getUniqueComponents(true, null);
4459 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004460 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004461 }
4462
Winson Chunga0b7e862013-09-05 16:03:15 -07004463 public boolean isAllAppsButtonRank(int rank) {
4464 if (mHotseat != null) {
4465 return mHotseat.isAllAppsButtonRank(rank);
4466 }
4467 return false;
4468 }
4469
Winson Chunga2413752012-04-03 14:22:34 -07004470 private boolean canRunNewAppsAnimation() {
4471 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4472 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4473 }
4474
Winson Chungc9168342013-06-26 14:54:55 -07004475 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4476 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4477 PropertyValuesHolder.ofFloat("alpha", 1f),
4478 PropertyValuesHolder.ofFloat("scaleX", 1f),
4479 PropertyValuesHolder.ofFloat("scaleY", 1f));
4480 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4481 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4482 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4483 return bounceAnim;
4484 }
4485
Adam Cohen27772732013-11-11 14:00:56 +00004486 public boolean useVerticalBarLayout() {
4487 return LauncherAppState.getInstance().getDynamicGrid().
4488 getDeviceProfile().isVerticalBarLayout();
4489 }
4490
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004491 protected Rect getSearchBarBounds() {
4492 return LauncherAppState.getInstance().getDynamicGrid().
4493 getDeviceProfile().getSearchBarBounds();
4494 }
4495
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004496 @Override
4497 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004498 boolean searchVisible = updateGlobalSearchIcon();
4499 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004500 if (mSearchDropTargetBar != null) {
4501 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4502 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004503 }
4504
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004505 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004506 * Add the icons for all apps.
4507 *
4508 * Implementation of the method from LauncherModel.Callbacks.
4509 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004510 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004511 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004512 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4513 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4514 getHotseat().addAllAppsFolder(mIconCache, apps,
4515 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4516 }
4517 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004518 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004519 if (mAppsCustomizeContent != null) {
4520 mAppsCustomizeContent.onPackagesUpdated(
4521 LauncherModel.getSortedWidgetsAndShortcuts(this));
4522 }
Winson Chungc58497e2013-09-03 17:48:37 -07004523 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004524 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004525 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004526 mAppsCustomizeContent.onPackagesUpdated(
4527 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004528 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004529 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004530 }
4531
4532 /**
4533 * A package was updated.
4534 *
4535 * Implementation of the method from LauncherModel.Callbacks.
4536 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004537 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004538 Runnable r = new Runnable() {
4539 public void run() {
4540 bindAppsUpdated(apps);
4541 }
4542 };
4543 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004544 return;
4545 }
4546
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004547 if (mWorkspace != null) {
4548 mWorkspace.updateShortcuts(apps);
4549 }
Winson Chungc58497e2013-09-03 17:48:37 -07004550
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004551 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004552 mAppsCustomizeContent != null) {
4553 mAppsCustomizeContent.updateApps(apps);
4554 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004555 }
4556
4557 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004558 * Update the state of a package, typically related to install state.
4559 *
4560 * Implementation of the method from LauncherModel.Callbacks.
4561 */
4562 public void updatePackageState(String pkgName, int state) {
4563 if (mWorkspace != null) {
4564 mWorkspace.updatePackageState(pkgName, state);
4565 }
4566 }
4567
4568 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004569 * A package was uninstalled. We take both the super set of packageNames
4570 * in addition to specific applications to remove, the reason being that
4571 * this can be called when a package is updated as well. In that scenario,
4572 * we only remove specific components from the workspace, where as
4573 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004574 *
4575 * Implementation of the method from LauncherModel.Callbacks.
4576 */
Winson Chung83892cc2013-05-01 16:53:33 -07004577 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Kenny Guyed131872014-04-30 03:02:21 +01004578 final ArrayList<AppInfo> appInfos, final UserHandleCompat user) {
Winson Chungd64d1762013-08-20 14:37:16 -07004579 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004580 public void run() {
Kenny Guyed131872014-04-30 03:02:21 +01004581 bindComponentsRemoved(packageNames, appInfos, user);
Winson Chung83892cc2013-05-01 16:53:33 -07004582 }
Winson Chungd64d1762013-08-20 14:37:16 -07004583 };
4584 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004585 return;
4586 }
4587
Winson Chungdf95eb12013-10-16 14:57:07 -07004588 if (!packageNames.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004589 mWorkspace.removeItemsByPackageName(packageNames, user);
Winson Chungdf95eb12013-10-16 14:57:07 -07004590 }
4591 if (!appInfos.isEmpty()) {
Kenny Guyed131872014-04-30 03:02:21 +01004592 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
Joe Onorato36115782010-06-17 13:28:48 -04004593 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004594
Winson Chunga1820962011-10-03 16:31:06 -07004595 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004596 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004597
Winson Chungdf95eb12013-10-16 14:57:07 -07004598 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004599 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004600 mAppsCustomizeContent != null) {
4601 mAppsCustomizeContent.removeApps(appInfos);
4602 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004603 }
Joe Onoratobe386092009-11-17 17:32:16 -08004604
4605 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004606 * A number of packages were updated.
4607 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004608 private ArrayList<Object> mWidgetsAndShortcuts;
4609 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004610 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004611 bindPackagesUpdated(mWidgetsAndShortcuts);
4612 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004613 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004614 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004615 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4616 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4617 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004618 return;
4619 }
4620
Winson Chung64359a52013-07-08 17:17:08 -07004621 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004622 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004623 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004624 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004625 }
4626
Winson Chung400438b2011-01-16 17:53:48 -08004627 private int mapConfigurationOriActivityInfoOri(int configOri) {
4628 final Display d = getWindowManager().getDefaultDisplay();
4629 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4630 switch (d.getRotation()) {
4631 case Surface.ROTATION_0:
4632 case Surface.ROTATION_180:
4633 // We are currently in the same basic orientation as the natural orientation
4634 naturalOri = configOri;
4635 break;
4636 case Surface.ROTATION_90:
4637 case Surface.ROTATION_270:
4638 // We are currently in the other basic orientation to the natural orientation
4639 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4640 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4641 break;
4642 }
4643
4644 int[] oriMap = {
4645 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4646 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4647 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4648 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4649 };
4650 // Since the map starts at portrait, we need to offset if this device's natural orientation
4651 // is landscape.
4652 int indexOffset = 0;
4653 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4654 indexOffset = 1;
4655 }
4656 return oriMap[(d.getRotation() + indexOffset) % 4];
4657 }
Adam Cohen446e9402011-09-15 18:21:21 -07004658
Winson Chung4b919f82012-05-01 10:44:08 -07004659 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08004660 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07004661 getResources().getBoolean(R.bool.allow_rotation);
4662 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08004663 }
Winson Chung4b919f82012-05-01 10:44:08 -07004664 public void lockScreenOrientation() {
4665 if (isRotationEnabled()) {
4666 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4667 .getConfiguration().orientation));
4668 }
4669 }
4670 public void unlockScreenOrientation(boolean immediate) {
4671 if (isRotationEnabled()) {
4672 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004673 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004674 } else {
4675 mHandler.postDelayed(new Runnable() {
4676 public void run() {
4677 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4678 }
4679 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004680 }
Winson Chung4b919f82012-05-01 10:44:08 -07004681 }
Winson Chung400438b2011-01-16 17:53:48 -08004682 }
4683
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004684 /**
4685 * Called when the SearchBar hint should be changed.
4686 *
4687 * @param hint the hint to be displayed in the search bar.
4688 */
4689 protected void onSearchBarHintChanged(String hint) {
Winson Chunga6945242014-01-08 14:04:34 -08004690 mLauncherClings.updateSearchBarHint(hint);
Sandeep Siddharthad4d3d082013-12-26 15:42:52 -08004691 }
4692
Winson Chunge43a1e72014-01-15 10:33:02 -08004693 protected boolean isLauncherPreinstalled() {
4694 PackageManager pm = getPackageManager();
4695 try {
4696 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4697 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4698 return true;
4699 } else {
4700 return false;
4701 }
4702 } catch (NameNotFoundException e) {
4703 e.printStackTrace();
4704 return false;
4705 }
4706 }
4707
Adam Cohenea90f832014-05-21 15:03:34 -07004708 /**
4709 * This method indicates whether or not we should suggest default wallpaper dimensions
4710 * when our wallpaper cropper was not yet used to set a wallpaper.
4711 */
4712 protected boolean overrideWallpaperDimensions() {
4713 return true;
4714 }
4715
Adam Cohen5eed5d82014-05-19 13:12:13 -07004716 protected boolean shouldClingFocusHotseatApp() {
4717 return false;
4718 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004719 protected String getFirstRunClingSearchBarHint() {
4720 return "";
4721 }
4722 protected String getFirstRunCustomContentHint() {
4723 return "";
4724 }
Winson Chung3a6e7f32013-10-09 15:50:52 -07004725 protected int getFirstRunFocusedHotseatAppDrawableId() {
4726 return -1;
4727 }
4728 protected ComponentName getFirstRunFocusedHotseatAppComponentName() {
4729 return null;
4730 }
4731 protected int getFirstRunFocusedHotseatAppRank() {
4732 return -1;
4733 }
4734 protected String getFirstRunFocusedHotseatAppBubbleTitle() {
4735 return "";
4736 }
4737 protected String getFirstRunFocusedHotseatAppBubbleDescription() {
4738 return "";
4739 }
Winson Chunge6eabff2013-09-24 11:01:23 -07004740
Winson Chungaf40f202013-09-18 18:26:31 -07004741 public void dismissFirstRunCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004742 mLauncherClings.dismissFirstRunCling(v);
4743 }
4744 public void dismissMigrationClingCopyApps(View v) {
4745 mLauncherClings.dismissMigrationClingCopyApps(v);
4746 }
4747 public void dismissMigrationClingUseDefault(View v) {
4748 mLauncherClings.dismissMigrationClingUseDefault(v);
4749 }
4750 public void dismissMigrationWorkspaceCling(View v) {
4751 mLauncherClings.dismissMigrationWorkspaceCling(v);
Winson Chungaf40f202013-09-18 18:26:31 -07004752 }
Winson Chung82f55532011-08-09 14:14:23 -07004753 public void dismissWorkspaceCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004754 mLauncherClings.dismissWorkspaceCling(v);
Winson Chung7d7541e2011-09-16 20:14:36 -07004755 }
4756 public void dismissFolderCling(View v) {
Winson Chunga6945242014-01-08 14:04:34 -08004757 mLauncherClings.dismissFolderCling(v);
Winson Chung82f55532011-08-09 14:14:23 -07004758 }
Adam Cohen6268f2d2014-05-16 16:42:35 -07004759 public void markFolderClingDismissedIfNecessary() {
4760 mLauncherClings.markFolderClingDismissedIfNecessary();
4761 }
Adam Cohen432609a2014-03-13 17:03:22 -07004762
4763 /**
4764 * To be overridden by subclasses to indicate that there is an activity to launch
4765 * before showing the standard launcher experience.
4766 */
4767 protected boolean hasFirstRunActivity() {
4768 return false;
4769 }
4770
4771 /**
4772 * To be overridden by subclasses to launch any first run activity
4773 */
4774 protected Intent getFirstRunActivity() {
4775 return null;
4776 }
4777
Winson Chunga6945242014-01-08 14:04:34 -08004778 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004779 return !ActivityManager.isRunningInTestHarness() &&
4780 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004781 }
4782
Adam Cohen4a9423d2014-03-28 14:22:31 -07004783 protected boolean hasRunFirstRunActivity() {
4784 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4785 }
4786
Adam Cohen432609a2014-03-13 17:03:22 -07004787 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004788 if (shouldRunFirstRunActivity() &&
4789 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004790 Intent firstRunIntent = getFirstRunActivity();
4791 if (firstRunIntent != null) {
4792 startActivity(firstRunIntent);
4793 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004794 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004795 }
4796 }
Adam Cohen432609a2014-03-13 17:03:22 -07004797 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004798 }
4799
4800 private void markFirstRunActivityShown() {
4801 SharedPreferences.Editor editor = mSharedPrefs.edit();
4802 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4803 editor.apply();
4804 }
4805
Adam Cohen432609a2014-03-13 17:03:22 -07004806 /**
4807 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4808 * screen that must be displayed and dismissed.
4809 */
4810 protected boolean hasDismissableIntroScreen() {
4811 return false;
4812 }
4813
4814 /**
4815 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4816 */
4817 protected View getIntroScreen() {
4818 return null;
4819 }
4820
4821 /**
4822 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4823 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4824 */
4825 private boolean shouldShowIntroScreen() {
4826 return hasDismissableIntroScreen() &&
4827 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4828 }
4829
4830 protected void showIntroScreen() {
4831 View introScreen = getIntroScreen();
4832 changeWallpaperVisiblity(false);
4833 if (introScreen != null) {
4834 mDragLayer.showOverlayView(introScreen);
4835 }
4836 }
4837
4838 public void dismissIntroScreen() {
4839 markIntroScreenDismissed();
4840 if (showFirstRunActivity()) {
4841 // We delay hiding the intro view until the first run activity is showing. This
4842 // avoids a blip.
4843 mWorkspace.postDelayed(new Runnable() {
4844 @Override
4845 public void run() {
4846 mDragLayer.dismissOverlayView();
4847 }
4848 }, ACTIVITY_START_DELAY);
4849 } else {
4850 mDragLayer.dismissOverlayView();
4851 }
4852 changeWallpaperVisiblity(true);
4853 }
4854
4855 private void markIntroScreenDismissed() {
4856 SharedPreferences.Editor editor = mSharedPrefs.edit();
4857 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4858 editor.apply();
4859 }
4860
Winson Chunga6945242014-01-08 14:04:34 -08004861 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004862 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4863 if (mHotseat != null) mHotseat.setAlpha(1f);
4864 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4865 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004866 }
4867
4868 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004869 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4870 if (mHotseat != null) mHotseat.setAlpha(0f);
4871 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4872 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004873 }
4874
Mathew Inwood72fbec12013-11-19 15:45:07 +00004875 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004876 // Called from search suggestion, not supported in other profiles.
4877 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4878 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4879 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4880 myUser);
4881 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004882 return null;
4883 }
Kenny Guyed131872014-04-30 03:02:21 +01004884 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004885 }
4886
4887 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4888 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004889 // Called from search suggestion, not supported in other profiles.
4890 return createShortcutDragInfo(shortcutIntent, caption, icon,
4891 UserHandleCompat.myUserHandle());
4892 }
4893
4894 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4895 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004896 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
4897 String contentDescription = userManager.getBadgedLabelForUser(caption.toString(), user);
4898 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004899 }
4900
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004901 protected void moveWorkspaceToDefaultScreen() {
4902 mWorkspace.moveToDefaultScreen(false);
4903 }
4904
Mathew Inwood72fbec12013-11-19 15:45:07 +00004905 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4906 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004907 mWorkspace.onExternalDragStartedWithItem(dragView);
4908 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004909 }
4910
Anjali Koppalf5d29132014-02-28 13:33:27 -08004911 @Override
4912 public void onPageSwitch(View newPage, int newPageIndex) {
4913 }
4914
Winson Chung80baf5a2010-08-09 16:03:15 -07004915 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004916 * Prints out out state for debugging.
4917 */
4918 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004919 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004920 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004921 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4922 Log.d(TAG, "mRestoring=" + mRestoring);
4923 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4924 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004925 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004926 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004927
Winson Chung785d2eb2011-04-14 16:08:02 -07004928 if (mAppsCustomizeContent != null) {
4929 mAppsCustomizeContent.dumpState();
4930 }
Daniel Sandler325dc232013-06-05 22:57:57 -04004931 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004932 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004933
4934 @Override
4935 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4936 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004937 synchronized (sDumpLogs) {
4938 writer.println(" ");
4939 writer.println("Debug logs: ");
4940 for (int i = 0; i < sDumpLogs.size(); i++) {
4941 writer.println(" " + sDumpLogs.get(i));
4942 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004943 }
4944 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004945
4946 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004947 if (DEBUG_DUMP_LOG) {
4948 synchronized (sDumpLogs) {
4949 Log.d(TAG, "");
4950 Log.d(TAG, "*********************");
4951 Log.d(TAG, "Launcher debug logs: ");
4952 for (int i = 0; i < sDumpLogs.size(); i++) {
4953 Log.d(TAG, " " + sDumpLogs.get(i));
4954 }
4955 Log.d(TAG, "*********************");
4956 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004957 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004958 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004959 }
4960
4961 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004962 addDumpLog(tag, log, null, debugLog);
4963 }
4964
4965 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004966 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004967 if (e != null) {
4968 Log.d(tag, log, e);
4969 } else {
4970 Log.d(tag, log);
4971 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004972 }
Winson Chungede41292013-09-19 16:27:36 -07004973 if (DEBUG_DUMP_LOG) {
4974 sDateStamp.setTime(System.currentTimeMillis());
4975 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004976 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4977 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004978 }
Winson Chungede41292013-09-19 16:27:36 -07004979 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004980 }
4981
Winson Chungede41292013-09-19 16:27:36 -07004982 public void dumpLogsToLocalData() {
4983 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004984 new AsyncTask<Void, Void, Void>() {
4985 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004986 boolean success = false;
4987 sDateStamp.setTime(sRunStart);
4988 String FILENAME = sDateStamp.getMonth() + "-"
4989 + sDateStamp.getDay() + "_"
4990 + sDateStamp.getHours() + "-"
4991 + sDateStamp.getMinutes() + "_"
4992 + sDateStamp.getSeconds() + ".txt";
4993
4994 FileOutputStream fos = null;
4995 File outFile = null;
4996 try {
4997 outFile = new File(getFilesDir(), FILENAME);
4998 outFile.createNewFile();
4999 fos = new FileOutputStream(outFile);
5000 } catch (Exception e) {
5001 e.printStackTrace();
5002 }
5003 if (fos != null) {
5004 PrintWriter writer = new PrintWriter(fos);
5005
5006 writer.println(" ");
5007 writer.println("Debug logs: ");
5008 synchronized (sDumpLogs) {
5009 for (int i = 0; i < sDumpLogs.size(); i++) {
5010 writer.println(" " + sDumpLogs.get(i));
5011 }
5012 }
5013 writer.close();
5014 }
5015 try {
5016 if (fos != null) {
5017 fos.close();
5018 success = true;
5019 }
5020 } catch (IOException e) {
5021 e.printStackTrace();
5022 }
Michael Jurka43467462013-11-14 12:03:58 +01005023 return null;
Winson Chungede41292013-09-19 16:27:36 -07005024 }
Michael Jurka43467462013-11-14 12:03:58 +01005025 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005026 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005027 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005028}
Michael Jurka2763be32011-02-24 11:19:57 -08005029
Michael Jurkaabded662011-03-04 12:06:57 -08005030interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005031 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005032 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005033 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005034 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005035 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005036}
Dan Sandlerd5024042014-01-09 15:01:33 -05005037
5038interface DebugIntents {
5039 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5040 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005041}