blob: 105dde6e3ef3207daf5c863231d61b4ac6aaa19d [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Gilles Debunnedd6c9922010-10-25 11:23:41 -070019import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080020import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
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;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
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;
Adam Cohen268c4752012-06-06 17:47:33 -070056import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070058import android.graphics.drawable.ColorDrawable;
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;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070086import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070088import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080091import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070092import android.widget.Advanceable;
93import android.widget.FrameLayout;
94import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080095import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070096import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070097
Sunny Goyal651077b2014-06-30 14:15:31 -070098import com.android.launcher3.DropTarget.DragObject;
99import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyal83a8f042015-05-19 12:52:12 -0700100import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700101import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700102import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.LauncherActivityInfoCompat;
104import com.android.launcher3.compat.LauncherAppsCompat;
105import com.android.launcher3.compat.UserHandleCompat;
106import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700107import com.android.launcher3.model.WidgetsModel;
Sunny Goyale2df0622015-04-24 11:27:00 -0700108import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700109import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700110import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700111import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700112import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700113
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.io.DataInputStream;
115import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700117import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700119import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700120import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700121import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800122import java.lang.reflect.InvocationTargetException;
123import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700124import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700125import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700126import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700127import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700129import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700130import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000131import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700132
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800133/**
134 * Default launcher application.
135 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100136public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700137 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800138 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
139 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700140 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700141 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800142
Winson Chung83f59ab2015-05-05 17:21:58 -0700143 // Temporary flag
144 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
145
Daniel Sandlerf061f822013-06-27 13:59:36 -0400146 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700147 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700148 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400149 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700150 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700151
Dan Sandlerd5024042014-01-09 15:01:33 -0500152 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
153
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700157 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800158
Michael Jurka8b805b12012-04-18 14:23:14 -0700159 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700160 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700161
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700162 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
163 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
164 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
165
Mathew Inwood876a8462013-06-14 14:12:41 +0100166 /**
167 * IntentStarter uses request codes starting with this. This must be greater than all activity
168 * request codes used internally.
169 */
170 protected static final int REQUEST_LAST = 100;
171
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800172 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
173
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800174 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800175
Michael Jurka0a457bf2012-11-19 14:05:05 -0800176 // To turn on these properties, type
177 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800178 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179
Winson Chung2672ff92012-05-04 16:22:30 -0700180 // The Intent extra that defines whether to ignore the launch animation
181 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400182 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700183
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800184 // Type: int
185 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700186 // Type: int
187 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700189 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
190 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
192 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700193 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800194 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700195 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700196 // Type: int
197 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
198 // Type: int
199 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
200 // Type: parcelable
201 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000202 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800203 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000204 // Type: int[]
205 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800206
Adam Cohen432609a2014-03-13 17:03:22 -0700207 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800208 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
209
Adam Cohen3ed316a2014-07-23 18:21:20 -0700210 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
211 static final String ACTION_FIRST_LOAD_COMPLETE =
212 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
213
Adam Cohen39a06042013-07-19 14:30:12 -0700214 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700215 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700216
Sunny Goyal594d76d2014-11-06 10:12:54 -0800217 private static final String QSB_WIDGET_ID = "qsb_widget_id";
218 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
219
Winson Chunga6945242014-01-08 14:04:34 -0800220 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
221
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700222 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800223 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700225 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700226
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700227 private boolean mIsSafeModeEnabled;
228
Adam Cohenc2d6e892014-10-16 09:49:24 -0700229 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
230 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700231 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700232
Joe Onorato9c1289c2009-08-17 11:03:03 -0400233 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700234 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
235 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700236 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000238 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
239 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
240
Winson Chunga2413752012-04-03 14:22:34 -0700241 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700242 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
243 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700244 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700245
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800246 private final BroadcastReceiver mCloseSystemDialogsReceiver
247 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800248 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private LayoutInflater mInflater;
251
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700253 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800254 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700255 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800256 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700257 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700258
Sunny Goyalffe83f12014-08-14 17:39:34 -0700259 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700260 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700261
Adam Cohen091440a2015-03-18 14:16:05 -0700262 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800263 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800264 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700265
Michael Jurka0280c3b2010-09-17 15:00:07 -0700266 private int[] mTmpAddItemCellCoordinates = new int[2];
267
Winson Chung3d503fb2011-07-13 17:25:49 -0700268 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800269 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700270
Michael Jurka838a4ca2011-02-07 13:33:06 -0800271 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700272
Winson Chungc51db6a2011-10-05 11:44:49 -0700273 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700274
275 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700276 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700277
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700278 // Main container view and the model for the widget tray screen.
279 @Thunk WidgetsContainerView mWidgetsView;
280 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700281
Winson Chungf0ea4d32011-06-06 14:27:16 -0700282 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800283 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700286 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
287 // scroll issues (because the workspace may not have been measured yet) and extra work.
288 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
289 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290
291 private SpannableStringBuilder mDefaultKeySsb = null;
292
Adam Cohen091440a2015-03-18 14:16:05 -0700293 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800295 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 private boolean mRestoring;
297 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700298 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299
Michael Jurka1e2f4652013-07-08 18:03:46 -0700300 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700301 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
302
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 private Bundle mSavedInstanceState;
304
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800306 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700307 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800308 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700309 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800310 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400311
Adam Cohen091440a2015-03-18 14:16:05 -0700312 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700313
Sunny Goyale2df0622015-04-24 11:27:00 -0700314 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700315
Adam Cohen61f560d2013-09-30 15:58:20 -0700316 private View.OnTouchListener mHapticFeedbackTouchListener;
317
Adam Cohended9f8d2010-11-03 13:25:16 -0700318 // Related to the auto-advancing of widgets
319 private final int ADVANCE_MSG = 1;
320 private final int mAdvanceInterval = 20000;
321 private final int mAdvanceStagger = 250;
322 private long mAutoAdvanceSentTime;
323 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700324 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700325 new HashMap<View, AppWidgetProviderInfo>();
326
Winson Chung400438b2011-01-16 17:53:48 -0800327 // Determines how long to wait after a rotation before restoring the screen orientation to
328 // match the sensor state.
329 private final int mRestoreScreenOrientationDelay = 500;
330
Adam Cohen091440a2015-03-18 14:16:05 -0700331 @Thunk 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
Winson Chungf0c6ae02012-03-21 16:10:31 -0700347 // Holds the page that we need to animate to, and the icon views that we need to animate up
348 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700349 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700350 private Bitmap mFolderIconBitmap;
351 private Canvas mFolderIconCanvas;
352 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700353
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700354 private DeviceProfile mDeviceProfile;
355
Winson Chung13eb5272015-05-11 16:30:13 -0700356 // This is set to the view that launched the activity that navigated the user away from
357 // launcher. Since there is no callback for when the activity has finished launching, enable
358 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800359 private BubbleTextView mWaitingForResume;
360
Adam Cohen59400422014-03-05 18:07:04 -0800361 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
362 new HashMap<String, CustomAppWidget>();
363
364 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
365 static {
366 if (ENABLE_CUSTOM_WIDGET_TEST) {
367 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
368 }
369 }
370
Chet Haasea8f996d2015-02-17 12:56:04 -0800371 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
372 private static Method sClipRevealMethod = null;
373 static {
374 Class<?> activityOptionsClass = ActivityOptions.class;
375 try {
376 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
377 View.class, int.class, int.class, int.class, int.class);
378 } catch (Exception e) {
379 // Earlier version
380 }
381 }
382
Adam Cohen091440a2015-03-18 14:16:05 -0700383 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700384 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700385 if (mWorkspace != null) {
386 mWorkspace.buildPageHardwareLayers();
387 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700388 }
389 };
390
Adam Cohendb364c32014-05-20 14:23:40 -0700391 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800392
Adam Cohen091440a2015-03-18 14:16:05 -0700393 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800394 int requestCode;
395 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700396 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700397 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800398 int cellX;
399 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700400 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800401 }
402
Daniel Sandlerff02d492013-08-05 02:12:05 -0400403 private Stats mStats;
404
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700405 FocusIndicatorView mFocusHandler;
406
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 @Override
408 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700409 if (DEBUG_STRICT_MODE) {
410 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
411 .detectDiskReads()
412 .detectDiskWrites()
413 .detectNetwork() // or .detectAll() for all detectable problems
414 .penaltyLog()
415 .build());
416 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
417 .detectLeakedSqlLiteObjects()
418 .detectLeakedClosableObjects()
419 .penaltyLog()
420 .penaltyDeath()
421 .build());
422 }
423
Adam Cohen9211d422014-10-07 18:14:53 -0700424 if (mLauncherCallbacks != null) {
425 mLauncherCallbacks.preOnCreate();
426 }
427
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400429
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400430 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400431 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700432 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700433
Adam Cohen2e6da152015-05-06 11:42:25 -0700434 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700435 mDeviceProfile = getResources().getConfiguration().orientation
436 == Configuration.ORIENTATION_LANDSCAPE ?
437 app.getInvariantDeviceProfile().landscapeProfile
438 : app.getInvariantDeviceProfile().portraitProfile;
439
440 // TODO: Move this to icon cache.
441 Utilities.setIconSize(mDeviceProfile.iconSizePx);
Winson Chung5f8afe62013-08-12 16:19:28 -0700442
443 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400444 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700445 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700446 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800447 mModel = app.setLauncher(this);
448 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700449
Joe Onorato41a12d22009-10-31 18:30:00 -0400450 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800451 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800452 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700453
Daniel Sandlerff02d492013-08-05 02:12:05 -0400454 mStats = new Stats(this);
455
Sunny Goyalffe83f12014-08-14 17:39:34 -0700456 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700457
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700458 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
459 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700460
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700461 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
462 // this also ensures that any synchronous binding below doesn't re-trigger another
463 // LauncherModel load.
464 mPaused = false;
465
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200467 android.os.Debug.startMethodTracing(
468 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 }
470
471 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800472 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700473
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800474 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700475 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800476
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800477 registerContentObservers();
478
Joe Onorato7c312c12009-08-13 21:36:53 -0700479 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700480
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800481 mSavedState = savedInstanceState;
482 restoreState(mSavedState);
483
484 if (PROFILE_STARTUP) {
485 android.os.Debug.stopMethodTracing();
486 }
487
488 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700489 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700490 // If the user leaves launcher, then we should just load items asynchronously when
491 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700492 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700493 } else {
494 // We only load the page synchronously if the user rotates (or triggers a
495 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700496 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700497 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800498 }
499
500 // For handling default keys
501 mDefaultKeySsb = new SpannableStringBuilder();
502 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800503
504 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
505 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800506
Adam Cohenf9426d52012-06-04 17:26:21 -0700507 // On large interfaces, we want the screen to auto-rotate based on the current orientation
508 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700509
Adam Cohen9211d422014-10-07 18:14:53 -0700510 if (mLauncherCallbacks != null) {
511 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700512 if (mLauncherCallbacks.hasLauncherOverlay()) {
513 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700514 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
515 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
516 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700517 mWorkspace.setLauncherOverlay(mLauncherOverlay);
518 }
Adam Cohen9211d422014-10-07 18:14:53 -0700519 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700520
521 if (shouldShowIntroScreen()) {
522 showIntroScreen();
523 } else {
524 showFirstRunActivity();
525 showFirstRunClings();
526 }
Adam Cohen9211d422014-10-07 18:14:53 -0700527 }
528
529 private LauncherCallbacks mLauncherCallbacks;
530
531 public void onPostCreate(Bundle savedInstanceState) {
532 super.onPostCreate(savedInstanceState);
533 if (mLauncherCallbacks != null) {
534 mLauncherCallbacks.onPostCreate(savedInstanceState);
535 }
536 }
537
538 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
539 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700540 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
541 @Override
542 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700543 if (LOGD) {
544 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
545 }
Winson Chung94804152015-05-08 13:06:44 -0700546 mAppsView.setFixedBounds(bounds);
547 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700548 }
549
550 @Override
551 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700552 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
553 // Dismiss All Apps if we aren't already paused/invisible
554 if (!mPaused) {
555 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
556 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
557 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700558 }
Winson Chung0f785722015-04-08 10:27:49 -0700559 }
560 });
Jun Mukai8af0cd82015-05-12 12:32:12 -0700561 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
562 private boolean mImportanceStored = false;
563 private int mWorkspaceImportanceForAccessibility =
564 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
565 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
566
567 @Override
568 public void onSearchOverlayOpened() {
569 if (mImportanceStored) {
570 return;
571 }
572 // The underlying workspace and hotseat are temporarily suppressed by the search
573 // overlay. So they sholudn't be accessible.
574 mWorkspaceImportanceForAccessibility = mWorkspace.getImportantForAccessibility();
575 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
576 mWorkspace.setImportantForAccessibility(
577 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
578 mHotseat.setImportantForAccessibility(
579 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
580 mImportanceStored = true;
581 }
582
583 @Override
584 public void onSearchOverlayClosed() {
585 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
586 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
587 mImportanceStored = false;
588 }
589 });
Adam Cohen9211d422014-10-07 18:14:53 -0700590 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700591 }
592
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700593 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700594 public void onLauncherProviderChange() {
595 if (mLauncherCallbacks != null) {
596 mLauncherCallbacks.onLauncherProviderChange();
597 }
598 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700599
Adam Cohen9211d422014-10-07 18:14:53 -0700600 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700601 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700602 if (mLauncherCallbacks != null) {
603 return mLauncherCallbacks.hasCustomContentToLeft();
604 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700605 return false;
606 }
607
Dave Hawkeya8881582013-09-17 15:55:33 -0600608 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500609 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600610 * {@link #addToCustomContentPage}. This will only be invoked if
611 * {@link #hasCustomContentToLeft()} is {@code true}.
612 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500613 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700614 if (mLauncherCallbacks != null) {
615 mLauncherCallbacks.populateCustomContentContainer();
616 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600617 }
618
619 /**
620 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
621 * ensure the custom content page is added or removed if necessary.
622 */
623 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600624 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600625 // Not bound yet, wait for bindScreens to be called.
626 return;
627 }
628
629 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
630 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500631 mWorkspace.createCustomContentContainer();
632 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600633 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
634 mWorkspace.removeCustomContentPage();
635 }
636 }
637
Adam Cohen091440a2015-03-18 14:16:05 -0700638 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700639 if (sLocaleConfiguration == null) {
640 new AsyncTask<Void, Void, LocaleConfiguration>() {
641 @Override
642 protected LocaleConfiguration doInBackground(Void... unused) {
643 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
644 readConfiguration(Launcher.this, localeConfiguration);
645 return localeConfiguration;
646 }
647
648 @Override
649 protected void onPostExecute(LocaleConfiguration result) {
650 sLocaleConfiguration = result;
651 checkForLocaleChange(); // recursive, but now with a locale configuration
652 }
653 }.execute();
654 return;
655 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700656
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800657 final Configuration configuration = getResources().getConfiguration();
658
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700659 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800660 final String locale = configuration.locale.toString();
661
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700662 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800663 final int mcc = configuration.mcc;
664
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700665 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800666 final int mnc = configuration.mnc;
667
Romain Guy84f296c2009-11-04 15:00:44 -0800668 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800669
Romain Guy84f296c2009-11-04 15:00:44 -0800670 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700671 sLocaleConfiguration.locale = locale;
672 sLocaleConfiguration.mcc = mcc;
673 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700674
Joe Onorato0589f0f2010-02-08 13:44:00 -0800675 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700676
677 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100678 new AsyncTask<Void, Void, Void>() {
679 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700680 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100681 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700682 }
Michael Jurka43467462013-11-14 12:03:58 +0100683 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700684 }
685 }
686
Adam Cohen091440a2015-03-18 14:16:05 -0700687 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700688 public String locale;
689 public int mcc = -1;
690 public int mnc = -1;
691 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700692
Adam Cohen091440a2015-03-18 14:16:05 -0700693 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700694 DataInputStream in = null;
695 try {
Helena Josol28db2802014-10-09 17:04:09 +0100696 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700697 configuration.locale = in.readUTF();
698 configuration.mcc = in.readInt();
699 configuration.mnc = in.readInt();
700 } catch (FileNotFoundException e) {
701 // Ignore
702 } catch (IOException e) {
703 // Ignore
704 } finally {
705 if (in != null) {
706 try {
707 in.close();
708 } catch (IOException e) {
709 // Ignore
710 }
711 }
712 }
713 }
714
Adam Cohen091440a2015-03-18 14:16:05 -0700715 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700716 DataOutputStream out = null;
717 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100718 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100719 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700720 out.writeUTF(configuration.locale);
721 out.writeInt(configuration.mcc);
722 out.writeInt(configuration.mnc);
723 out.flush();
724 } catch (FileNotFoundException e) {
725 // Ignore
726 } catch (IOException e) {
727 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100728 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700729 } finally {
730 if (out != null) {
731 try {
732 out.close();
733 } catch (IOException e) {
734 // Ignore
735 }
736 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800737 }
738 }
739
Anton Hanssona2f665f2013-09-26 12:18:32 +0100740 public Stats getStats() {
741 return mStats;
742 }
743
Bjorn Bringertc459e522013-06-07 19:36:01 +0100744 public LayoutInflater getInflater() {
745 return mInflater;
746 }
747
Hyunyoung Song3f471442015-04-08 19:01:34 -0700748 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700749 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
750 // that is subsequently removed from the workspace in startBinding().
751 return !mModel.isLoadingWorkspace();
752 }
753
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800754 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000755 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100756 if (Build.VERSION.SDK_INT >= 17) {
757 return View.generateViewId();
758 } else {
759 // View.generateViewId() is not available. The following fallback logic is a copy
760 // of its implementation.
761 for (;;) {
762 final int result = sNextGeneratedId.get();
763 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
764 int newValue = result + 1;
765 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
766 if (sNextGeneratedId.compareAndSet(result, newValue)) {
767 return result;
768 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000769 }
770 }
771 }
772
773 public int getViewIdForItem(ItemInfo info) {
774 // This cast is safe given the > 2B range for int.
775 int itemId = (int) info.id;
776 if (mItemIdToViewId.containsKey(itemId)) {
777 return mItemIdToViewId.get(itemId);
778 }
779 int viewId = generateViewId();
780 mItemIdToViewId.put(itemId, viewId);
781 return viewId;
782 }
783
784 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700785 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
786 * a configuration step, this allows the proper animations to run after other transitions.
787 */
Adam Cohendb364c32014-05-20 14:23:40 -0700788 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700789 long screenId = args.screenId;
790 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
791 // When the screen id represents an actual screen (as opposed to a rank) we make sure
792 // that the drop page actually exists.
793 screenId = ensurePendingDropLayoutExists(args.screenId);
794 }
Adam Cohendb364c32014-05-20 14:23:40 -0700795
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800796 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800797 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700798 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700799 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700800 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800801 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700802 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700803 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700804 case REQUEST_RECONFIGURE_APPWIDGET:
805 completeRestoreAppWidget(args.appWidgetId);
806 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800807 }
Michael Jurka27614d22012-04-02 06:40:22 -0700808 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
809 // if you turned the screen off and then back while in All Apps, Launcher would not
810 // return to the workspace. Clearing mAddInfo.container here fixes this issue
811 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700812 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800813 }
814
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800815 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700816 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700817 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700818 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700819 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800820 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700821
Adam Cohenad4e15c2013-10-17 16:21:35 -0700822 Runnable exitSpringLoaded = new Runnable() {
823 @Override
824 public void run() {
825 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
826 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
827 }
828 };
829
Michael Jurka8b805b12012-04-18 14:23:14 -0700830 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700831 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700832 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
833 if (resultCode == RESULT_CANCELED) {
834 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700835 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700836 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700837 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800838 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700839 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700840 }
841 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200842 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
843 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700844 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200845 }
846 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700847 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200848
Adam Cohened66b2b2012-01-23 17:28:51 -0800849 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700850 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700851
Adam Cohendb364c32014-05-20 14:23:40 -0700852 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800853 // We have special handling for widgets
854 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800855 final int appWidgetId;
856 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
857 : -1;
858 if (widgetId < 0) {
859 appWidgetId = pendingAddWidgetId;
860 } else {
861 appWidgetId = widgetId;
862 }
863
Adam Cohenad4e15c2013-10-17 16:21:35 -0700864 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800865 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700866 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
867 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700868 result = RESULT_CANCELED;
869 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700870 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700871 @Override
872 public void run() {
873 exitSpringLoadedDragModeDelayed(false, 0, null);
874 }
875 };
Adam Cohendb364c32014-05-20 14:23:40 -0700876 if (workspaceLocked) {
877 // No need to remove the empty screen if we're mid-binding, as the
878 // the bind will not add the empty screen.
879 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
880 } else {
881 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
882 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
883 }
Winson Chung5aaab772012-04-27 15:24:02 -0700884 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700885 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700886 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
887 // When the screen id represents an actual screen (as opposed to a rank)
888 // we make sure that the drop page actually exists.
889 mPendingAddInfo.screenId =
890 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
891 }
Adam Cohendb364c32014-05-20 14:23:40 -0700892 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
893
894 dropLayout.setDropPending(true);
895 final Runnable onComplete = new Runnable() {
896 @Override
897 public void run() {
898 completeTwoStageWidgetDrop(resultCode, appWidgetId);
899 dropLayout.setDropPending(false);
900 }
901 };
902 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
903 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
904 } else {
905 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
906 mPendingAddInfo);
907 sPendingAddItem = args;
908 }
Winson Chung5aaab772012-04-27 15:24:02 -0700909 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800910 return;
911 }
912
Sunny Goyalff572272014-07-23 13:58:07 -0700913 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
914 if (resultCode == RESULT_OK) {
915 // Update the widget view.
916 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
917 pendingAddWidgetId, mPendingAddInfo);
918 if (workspaceLocked) {
919 sPendingAddItem = args;
920 } else {
921 completeAdd(args);
922 }
923 }
924 // Leave the widget in the pending state if the user canceled the configure.
925 return;
926 }
927
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800928 // The pattern used here is that a user PICKs a specific application,
929 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700930
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800931 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
932 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700933 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700934 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
935 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800936 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700937 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800938 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700939 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700940 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
941 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800942 }
Adam Cohen00074722013-10-11 17:46:09 -0700943 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700944 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700945 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800946 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800947 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800948
949 }
950
951 @Override
952 protected void onActivityResult(
953 final int requestCode, final int resultCode, final Intent data) {
954 handleActivityResult(requestCode, resultCode, data);
955 if (mLauncherCallbacks != null) {
956 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
957 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800958 }
959
Adam Cohendb364c32014-05-20 14:23:40 -0700960 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
961 appWidgetId, ItemInfo info) {
962 PendingAddArguments args = new PendingAddArguments();
963 args.requestCode = requestCode;
964 args.intent = data;
965 args.container = info.container;
966 args.screenId = info.screenId;
967 args.cellX = info.cellX;
968 args.cellY = info.cellY;
969 args.appWidgetId = appWidgetId;
970 return args;
971 }
972
973 /**
974 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
975 *
976 * @param screenId the screen id to check
977 * @return the new screen, or screenId if it exists
978 */
979 private long ensurePendingDropLayoutExists(long screenId) {
980 CellLayout dropLayout =
981 (CellLayout) mWorkspace.getScreenWithId(screenId);
982 if (dropLayout == null) {
983 // it's possible that the add screen was removed because it was
984 // empty and a re-bind occurred
985 mWorkspace.addExtraEmptyScreen();
986 return mWorkspace.commitExtraEmptyScreen();
987 } else {
988 return screenId;
989 }
990 }
991
Adam Cohen091440a2015-03-18 14:16:05 -0700992 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700993 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700994 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800995 Runnable onCompleteRunnable = null;
996 int animationType = 0;
997
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700998 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800999 if (resultCode == RESULT_OK) {
1000 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
1001 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001002 mPendingAddWidgetInfo);
1003 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -08001004 onCompleteRunnable = new Runnable() {
1005 @Override
1006 public void run() {
1007 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001008 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001009 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
1010 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -08001011 }
1012 };
1013 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -08001014 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001015 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -08001016 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001017 if (mDragLayer.getAnimatedView() != null) {
1018 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
1019 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
1020 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001021 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001022 // The animated view may be null in the case of a rotation during widget configuration
1023 onCompleteRunnable.run();
1024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 }
1026
1027 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -07001028 protected void onStop() {
1029 super.onStop();
1030 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -07001031
1032 if (mLauncherCallbacks != null) {
1033 mLauncherCallbacks.onStop();
1034 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001035 }
1036
1037 @Override
1038 protected void onStart() {
1039 super.onStart();
1040 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -07001041
1042 if (mLauncherCallbacks != null) {
1043 mLauncherCallbacks.onStart();
1044 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001045 }
1046
1047 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001048 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001049 long startTime = 0;
1050 if (DEBUG_RESUME_TIME) {
1051 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001052 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001053 }
Adam Cohen9211d422014-10-07 18:14:53 -07001054
1055 if (mLauncherCallbacks != null) {
1056 mLauncherCallbacks.preOnResume();
1057 }
1058
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001059 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001060
Winson Chung4a2afa32012-07-19 14:53:05 -07001061 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001062 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001063 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001064 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001065 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001066 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001067 // view after launching an app, as they may be depending on the UI to be static to
1068 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001069 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -07001070 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08001071 } else if (mOnResumeState == State.WIDGETS) {
1072 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001073 }
1074 mOnResumeState = State.NONE;
1075
Winson Chungcd99cd32015-04-29 11:03:24 -07001076 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001077 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1078 // Otherwise, notify the callbacks if we are in all apps mode
1079 if (mState == State.APPS) {
1080 if (mLauncherCallbacks != null) {
1081 mLauncherCallbacks.onAllAppsShown();
1082 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001083 }
1084 }
1085
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001086 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001087 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1088 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001089
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001090 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001091 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001092 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001093 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001094 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001095 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001096 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001097 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001098 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1099 // execute them here
1100 long startTimeCallbacks = 0;
1101 if (DEBUG_RESUME_TIME) {
1102 startTimeCallbacks = System.currentTimeMillis();
1103 }
1104
Michael Jurka1e2f4652013-07-08 18:03:46 -07001105 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1106 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001107 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001108 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001109 if (DEBUG_RESUME_TIME) {
1110 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1111 (System.currentTimeMillis() - startTimeCallbacks));
1112 }
Michael Jurka447bf842013-05-15 14:52:15 +02001113 }
Michael Jurka54554252013-08-01 12:52:23 +02001114 if (mOnResumeCallbacks.size() > 0) {
1115 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1116 mOnResumeCallbacks.get(i).run();
1117 }
1118 mOnResumeCallbacks.clear();
1119 }
Winson Chunge4e50662012-01-23 14:45:13 -08001120
1121 // Reset the pressed state of icons that were locked in the press state while activities
1122 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001123 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001124 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001125 mWaitingForResume.setStayPressed(false);
1126 }
Winson Chung82963d52013-10-09 11:20:57 -07001127
Adam Cohen06dff352012-06-01 17:17:08 -07001128 // It is possible that widgets can receive updates while launcher is not in the foreground.
1129 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1130 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1131 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001132 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001133 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001134
Michael Jurka447bf842013-05-15 14:52:15 +02001135 if (DEBUG_RESUME_TIME) {
1136 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1137 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001138
1139 if (mWorkspace.getCustomContentCallbacks() != null) {
1140 // If we are resuming and the custom content is the current page, we call onShow().
1141 // It is also poassible that onShow will instead be called slightly after first layout
1142 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1143 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001144 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001145 }
1146 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001147 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001148 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001149
Sunny Goyal756adbc2015-04-16 15:20:51 -07001150 if (!isWorkspaceLoading()) {
1151 // Process any items that were added while Launcher was away.
1152 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1153 }
Adam Cohen9211d422014-10-07 18:14:53 -07001154
1155 if (mLauncherCallbacks != null) {
1156 mLauncherCallbacks.onResume();
1157 }
Adam Cohen06dff352012-06-01 17:17:08 -07001158 }
1159
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001160 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001161 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001162 // Ensure that items added to Launcher are queued until Launcher returns
1163 InstallShortcutReceiver.enableInstallQueue();
1164
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001165 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001166 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001167 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001168 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001169
1170 // We call onHide() aggressively. The custom content callbacks should be able to
1171 // debounce excess onHide calls.
1172 if (mWorkspace.getCustomContentCallbacks() != null) {
1173 mWorkspace.getCustomContentCallbacks().onHide();
1174 }
Romain Guycbb89e42009-06-08 15:52:54 -07001175
Adam Cohen9211d422014-10-07 18:14:53 -07001176 if (mLauncherCallbacks != null) {
1177 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001178 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001179 }
1180
1181 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001182 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1183 // by a onResume or by scrolling otherwise.
1184 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001185
1186 // Custom content is completely hidden
1187 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001188
1189 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1190 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001191
1192 // Indicates whether the user is allowed to scroll away from the custom content.
1193 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001194 }
1195
Adam Cohenc2d6e892014-10-16 09:49:24 -07001196 public interface LauncherOverlay {
1197
1198 /**
1199 * Touch interaction leading to overscroll has begun
1200 */
1201 public void onScrollInteractionBegin();
1202
1203 /**
1204 * Touch interaction related to overscroll has ended
1205 */
1206 public void onScrollInteractionEnd();
1207
1208 /**
1209 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1210 * screen (or in the case of RTL, the rightmost screen).
1211 */
1212 public void onScrollChange(int progress, boolean rtl);
1213
1214 /**
1215 * Screen has stopped scrolling
1216 */
1217 public void onScrollSettled();
1218
1219 /**
1220 * This method can be called by the Launcher in order to force the LauncherOverlay
1221 * to exit fully immersive mode.
1222 */
1223 public void forceExitFullImmersion();
1224 }
1225
Winson Chung0f785722015-04-08 10:27:49 -07001226 public interface LauncherAppsCallbacks {
1227 /**
1228 * Updates launcher to the available space that AllApps can take so as not to overlap with
1229 * any other views.
1230 */
1231 public void onAllAppsBoundsChanged(Rect bounds);
1232
1233 /**
1234 * Called to dismiss all apps if it is showing.
1235 */
1236 public void dismissAllApps();
1237 }
1238
Jun Mukai8af0cd82015-05-12 12:32:12 -07001239 public interface LauncherSearchCallbacks {
1240 /**
1241 * Called when the search overlay is shown.
1242 */
1243 public void onSearchOverlayOpened();
1244
1245 /**
1246 * Called when the search overlay is dismissed.
1247 */
1248 public void onSearchOverlayClosed();
1249 }
1250
Adam Cohenc2d6e892014-10-16 09:49:24 -07001251 public interface LauncherOverlayCallbacks {
1252 /**
1253 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1254 * however it doesn't modify any state within the launcher.
1255 */
1256 public boolean canEnterFullImmersion();
1257
1258 /**
1259 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1260 * eg. by occupying the full screen and handling all touch events.
1261 *
1262 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1263 * case, Launcher will modify any necessary state and assumes the overlay is
1264 * handling all interaction. If false, the LauncherOverlay should cancel any
1265 *
1266 */
1267 public boolean enterFullImmersion();
1268
1269 /**
1270 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1271 * full control over UI and state.
1272 */
1273 public void exitFullImmersion();
1274 }
1275
1276 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1277
1278 @Override
1279 public boolean canEnterFullImmersion() {
1280 return mState == State.WORKSPACE;
1281 }
1282
1283 @Override
1284 public boolean enterFullImmersion() {
1285 if (mState == State.WORKSPACE) {
1286 // When fully immersed, disregard any touches which fall through.
1287 mDragLayer.setBlockTouch(true);
1288 return true;
1289 }
1290 return false;
1291 }
1292
1293 @Override
1294 public void exitFullImmersion() {
1295 mDragLayer.setBlockTouch(false);
1296 }
1297 }
1298
Jorim Jaggid017f882014-01-14 17:08:48 -08001299 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001300 if (mLauncherCallbacks != null) {
1301 return mLauncherCallbacks.hasSettings();
1302 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001303 return false;
1304 }
1305
Adam Cohen9211d422014-10-07 18:14:53 -07001306 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001307 CustomContentCallbacks callbacks, String description) {
1308 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001309 }
1310
Adam Cohenedb40762013-07-18 16:45:45 -07001311 // The custom content needs to offset its content to account for the QSB
1312 public int getTopOffsetForCustomContent() {
1313 return mWorkspace.getPaddingTop();
1314 }
1315
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001316 @Override
1317 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001318 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001319 if (mModel.isCurrentCallbacks(this)) {
1320 mModel.stopLoader();
1321 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001322 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1323
Romain Guy13c2e7b2010-03-10 19:45:00 -08001324 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001325 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001326
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001327 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001328 @Override
1329 public void onWindowFocusChanged(boolean hasFocus) {
1330 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001331 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001332
1333 if (mLauncherCallbacks != null) {
1334 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1335 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001336 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001337
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 private boolean acceptFilter() {
1339 final InputMethodManager inputManager = (InputMethodManager)
1340 getSystemService(Context.INPUT_METHOD_SERVICE);
1341 return !inputManager.isFullscreenMode();
1342 }
1343
1344 @Override
1345 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001346 final int uniChar = event.getUnicodeChar();
1347 final boolean handled = super.onKeyDown(keyCode, event);
1348 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1349 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001350 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1351 keyCode, event);
1352 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001353 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001354 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001355 // showSearchDialog()
1356 // If there are multiple keystrokes before the search dialog takes focus,
1357 // onSearchRequested() will be called for every keystroke,
1358 // but it is idempotent, so it's fine.
1359 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001360 }
1361 }
1362
Joe Onorato8a9625e2010-01-28 15:55:35 -08001363 // Eat the long press event so the keyboard doesn't come up.
1364 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1365 return true;
1366 }
1367
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001368 return handled;
1369 }
1370
Karl Rosaen138a0412009-04-23 19:00:21 -07001371 private String getTypedText() {
1372 return mDefaultKeySsb.toString();
1373 }
1374
1375 private void clearTypedText() {
1376 mDefaultKeySsb.clear();
1377 mDefaultKeySsb.clearSpans();
1378 Selection.setSelection(mDefaultKeySsb, 0);
1379 }
1380
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001381 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001382 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1383 * State
1384 */
1385 private static State intToState(int stateOrdinal) {
1386 State state = State.WORKSPACE;
1387 final State[] stateValues = State.values();
1388 for (int i = 0; i < stateValues.length; i++) {
1389 if (stateValues[i].ordinal() == stateOrdinal) {
1390 state = stateValues[i];
1391 break;
1392 }
1393 }
1394 return state;
1395 }
1396
1397 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001398 * Restores the previous state, if it exists.
1399 *
1400 * @param savedState The previous state.
1401 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001402 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 private void restoreState(Bundle savedState) {
1404 if (savedState == null) {
1405 return;
1406 }
1407
Michael Jurka883f55b2010-10-21 15:47:14 -07001408 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001409 if (state == State.APPS || state == State.WIDGETS) {
1410 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001411 }
1412
Adam Cohen21cd0022013-10-09 18:57:02 -07001413 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1414 PagedView.INVALID_RESTORE_PAGE);
1415 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001416 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001417 }
1418
Winson Chung3d503fb2011-07-13 17:25:49 -07001419 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001420 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001421
Winson Chung3d503fb2011-07-13 17:25:49 -07001422 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1423 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001424 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001425 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1426 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001427 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1428 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001429 AppWidgetProviderInfo info = savedState.getParcelable(
1430 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001431 mPendingAddWidgetInfo = info == null ?
1432 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1433
Adam Cohen4637b5a2013-11-04 18:21:24 -08001434 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001435 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 mRestoring = true;
1437 }
1438
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001439 mItemIdToViewId = (HashMap<Integer, Integer>)
1440 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 }
1442
1443 /**
1444 * Finds all the views we need and configure them properly.
1445 */
1446 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001447 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001448
Craig Mautner360310b2012-10-26 15:13:08 -07001449 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001450 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001451 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1452 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001453 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001454 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001455
John Spurlock77e1f472013-09-11 10:09:51 -04001456 mLauncherView.setSystemUiVisibility(
1457 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001458 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001459
Winson Chung4c98d922011-05-31 16:50:48 -07001460 // Setup the drag layer
1461 mDragLayer.setup(this, dragController);
1462
Winson Chung3d503fb2011-07-13 17:25:49 -07001463 // Setup the hotseat
1464 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1465 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001466 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001467 }
1468
Jorim Jaggid017f882014-01-14 17:08:48 -08001469 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001470 View widgetButton = findViewById(R.id.widget_button);
1471 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001472 @Override
1473 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001474 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001475 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001476 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001477 }
1478 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001479 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1480
1481 View wallpaperButton = findViewById(R.id.wallpaper_button);
1482 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001483 @Override
1484 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001485 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001486 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001487 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001488 }
1489 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001490 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1491
1492 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001493 if (hasSettings()) {
1494 settingsButton.setOnClickListener(new OnClickListener() {
1495 @Override
1496 public void onClick(View arg0) {
1497 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001498 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001499 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001500 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001501 });
1502 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1503 } else {
1504 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001505 }
1506
Adam Cohendbdff6b2013-09-18 19:09:15 -07001507 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001508
Winson Chung4c98d922011-05-31 16:50:48 -07001509 // Setup the workspace
1510 mWorkspace.setHapticFeedbackEnabled(false);
1511 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001512 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001513 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001514
Winson Chungf0ea4d32011-06-06 14:27:16 -07001515 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001516 mSearchDropTargetBar = (SearchDropTargetBar)
1517 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001518
Winson Chungb745afb2015-03-02 11:51:23 -08001519 // Setup Apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001520 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001521 if (isAllAppsSearchOverridden()) {
1522 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001523 }
Winson Chungb745afb2015-03-02 11:51:23 -08001524
Winson Chungf0ea4d32011-06-06 14:27:16 -07001525 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001526 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001527
Winson Chung3d503fb2011-07-13 17:25:49 -07001528 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001529 dragController.setDragScoller(mWorkspace);
1530 dragController.setScrollView(mDragLayer);
1531 dragController.setMoveTarget(mWorkspace);
1532 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001533 if (mSearchDropTargetBar != null) {
1534 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001535 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001536 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001537
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001538 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001539 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001540 mWeightWatcher = new WeightWatcher(this);
1541 mWeightWatcher.setAlpha(0.5f);
1542 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001543 new FrameLayout.LayoutParams(
1544 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001545 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001546 Gravity.BOTTOM)
1547 );
Adam Cohen39a06042013-07-19 14:30:12 -07001548
1549 boolean show = shouldShowWeightWatcher();
1550 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001551 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001552 }
1553
1554 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001555 * Sets the all apps button. This method is called from {@link Hotseat}.
1556 */
1557 public void setAllAppsButton(View allAppsButton) {
1558 mAllAppsButton = allAppsButton;
1559 }
1560
1561 public View getAllAppsButton() {
1562 return mAllAppsButton;
1563 }
1564
1565 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001566 * Creates a view representing a shortcut.
1567 *
1568 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001569 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001570 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001571 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001572 }
1573
1574 /**
1575 * Creates a view representing a shortcut inflated from the specified resource.
1576 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001577 * @param parent The group the shortcut belongs to.
1578 * @param info The data structure describing the shortcut.
1579 *
1580 * @return A View inflated from layoutResId.
1581 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001582 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001583 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001584 parent, false);
1585 favorite.applyFromShortcutInfo(info, mIconCache);
1586 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001588 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 return favorite;
1590 }
1591
1592 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001593 * Add a shortcut to the workspace.
1594 *
1595 * @param data The intent describing the shortcut.
1596 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001597 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001598 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001599 int cellY) {
1600 int[] cellXY = mTmpAddItemCellCoordinates;
1601 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001602 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001603
Sunny Goyal5c97f512015-05-19 16:03:28 -07001604 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001605 if (info == null) {
1606 return;
1607 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001608 final View view = createShortcut(info);
1609
Sunny Goyal5c97f512015-05-19 16:03:28 -07001610 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001611 // First we check if we already know the exact location where we want to add this item.
1612 if (cellX >= 0 && cellY >= 0) {
1613 cellXY[0] = cellX;
1614 cellXY[1] = cellY;
1615 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001616
1617 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001618 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001619 true, null,null)) {
1620 return;
1621 }
1622 DragObject dragObject = new DragObject();
1623 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001624 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1625 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001626 return;
1627 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001628 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001629 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001630 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001631 foundCellSpan = (result != null);
1632 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001633 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001634 }
1635
1636 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001637 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001638 return;
1639 }
1640
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001641 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001642
1643 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001644 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001645 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 }
1647 }
1648
Adam Cohen2e6da152015-05-06 11:42:25 -07001649 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1650 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001651 // We want to account for the extra amount of padding that we are adding to the widget
1652 // to ensure that it gets the full amount of space that it has requested
1653 int requiredWidth = minWidth + padding.left + padding.right;
1654 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2e6da152015-05-06 11:42:25 -07001655 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001656 }
1657
Adam Cohen2e6da152015-05-06 11:42:25 -07001658 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1659 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001660 }
1661
Adam Cohen2e6da152015-05-06 11:42:25 -07001662 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1663 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001664 }
1665
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001666 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001667 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001668 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001669 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001670 */
Adam Cohen091440a2015-03-18 14:16:05 -07001671 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001672 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1673
1674 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001675 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001676 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1677 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001678 }
Romain Guycbb89e42009-06-08 15:52:54 -07001679
Adam Cohen59400422014-03-05 18:07:04 -08001680 if (appWidgetInfo.isCustomWidget) {
1681 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001682 }
1683
Adam Cohen59400422014-03-05 18:07:04 -08001684 LauncherAppWidgetInfo launcherInfo;
1685 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1686 launcherInfo.spanX = info.spanX;
1687 launcherInfo.spanY = info.spanY;
1688 launcherInfo.minSpanX = info.minSpanX;
1689 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001690 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001691
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001693 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001694
1695 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001696 if (hostView == null) {
1697 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001698 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1699 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001700 } else {
1701 // The AppWidgetHostView has already been inflated and instantiated
1702 launcherInfo.hostView = hostView;
1703 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001704 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001705 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001706 launcherInfo.notifyWidgetSizeChanged(this);
1707
Adam Cohen59400422014-03-05 18:07:04 -08001708 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1709 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001710
1711 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001712 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001713 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001714 }
Romain Guycbb89e42009-06-08 15:52:54 -07001715
Adam Cohended9f8d2010-11-03 13:25:16 -07001716 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1717 @Override
1718 public void onReceive(Context context, Intent intent) {
1719 final String action = intent.getAction();
1720 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1721 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001722 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001723 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001724
Winson Chungc100e8e2011-08-09 16:02:43 -07001725 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001726 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001727 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001728 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001729 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001730 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1732 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001733 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001734 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1735 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001736 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001737 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1738 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1739 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001740 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001741 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1742 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001743 }
1744 }
1745 };
1746
1747 @Override
1748 public void onAttachedToWindow() {
1749 super.onAttachedToWindow();
1750
1751 // Listen for broadcasts related to user-presence
1752 final IntentFilter filter = new IntentFilter();
1753 filter.addAction(Intent.ACTION_SCREEN_OFF);
1754 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001755 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001756 if (ENABLE_DEBUG_INTENTS) {
1757 filter.addAction(DebugIntents.DELETE_DATABASE);
1758 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1759 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001760 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001761 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001762 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001763 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001764 mVisible = true;
1765 }
1766
Adam Cohen0b395352014-06-09 22:54:36 +00001767 /**
1768 * Sets up transparent navigation and status bars in LMP.
1769 * This method is a no-op for other platform versions.
1770 */
Sunny Goyald0091012015-01-20 15:55:34 -08001771 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001772 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001773 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001774 Window window = getWindow();
1775 window.getAttributes().systemUiVisibility |=
1776 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1777 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1778 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1779 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1780 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1781 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1782 window.setStatusBarColor(Color.TRANSPARENT);
1783 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001784 }
1785 }
1786
Adam Cohended9f8d2010-11-03 13:25:16 -07001787 @Override
1788 public void onDetachedFromWindow() {
1789 super.onDetachedFromWindow();
1790 mVisible = false;
1791
Adam Cohend113e0c2010-11-11 10:48:05 -08001792 if (mAttached) {
1793 unregisterReceiver(mReceiver);
1794 mAttached = false;
1795 }
Winson Chungb745afb2015-03-02 11:51:23 -08001796 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001797 }
1798
1799 public void onWindowVisibilityChanged(int visibility) {
1800 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001801 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001802 // The following code used to be in onResume, but it turns out onResume is called when
1803 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1804 // is a more appropriate event to handle
1805 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001806 if (!mWorkspaceLoading) {
1807 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001808 // We want to let Launcher draw itself at least once before we force it to build
1809 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001810 // apps is nice and speedy.
1811 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001812 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001813 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001814 if (mStarted) return;
1815 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001816 // We delay the layer building a bit in order to give
1817 // other message processing a time to run. In particular
1818 // this avoids a delay in hiding the IME if it was
1819 // currently shown, because doing that may involve
1820 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001821 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001822 final ViewTreeObserver.OnDrawListener listener = this;
1823 mWorkspace.post(new Runnable() {
1824 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001825 if (mWorkspace != null &&
1826 mWorkspace.getViewTreeObserver() != null) {
1827 mWorkspace.getViewTreeObserver().
1828 removeOnDrawListener(listener);
1829 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001830 }
1831 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001832 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001833 }
1834 });
1835 }
1836 clearTypedText();
1837 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001838 }
1839
Adam Cohen091440a2015-03-18 14:16:05 -07001840 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001841 mHandler.removeMessages(ADVANCE_MSG);
1842 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1843 mHandler.sendMessageDelayed(msg, delay);
1844 mAutoAdvanceSentTime = System.currentTimeMillis();
1845 }
1846
Adam Cohen091440a2015-03-18 14:16:05 -07001847 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001848 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1849 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1850 mAutoAdvanceRunning = autoAdvanceRunning;
1851 if (autoAdvanceRunning) {
1852 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1853 sendAdvanceMessage(delay);
1854 } else {
1855 if (!mWidgetsToAdvance.isEmpty()) {
1856 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1857 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1858 }
1859 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001860 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001861 }
1862 }
1863 }
1864
1865 private final Handler mHandler = new Handler() {
1866 @Override
1867 public void handleMessage(Message msg) {
1868 if (msg.what == ADVANCE_MSG) {
1869 int i = 0;
1870 for (View key: mWidgetsToAdvance.keySet()) {
1871 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1872 final int delay = mAdvanceStagger * i;
1873 if (v instanceof Advanceable) {
1874 postDelayed(new Runnable() {
1875 public void run() {
1876 ((Advanceable) v).advance();
1877 }
1878 }, delay);
1879 }
1880 i++;
1881 }
1882 sendAdvanceMessage(mAdvanceInterval);
1883 }
1884 }
1885 };
1886
1887 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001888 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001889 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1890 if (v instanceof Advanceable) {
1891 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001892 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001893 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001894 }
1895 }
1896
1897 void removeWidgetToAutoAdvance(View hostView) {
1898 if (mWidgetsToAdvance.containsKey(hostView)) {
1899 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001900 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001901 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001902 }
1903
Joe Onorato9c1289c2009-08-17 11:03:03 -04001904 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001905 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001906 launcherInfo.hostView = null;
1907 }
1908
Hyunyoung Song3f471442015-04-08 19:01:34 -07001909 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001910 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1911 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001912 }
1913
Winson Chunga6945242014-01-08 14:04:34 -08001914 public DragLayer getDragLayer() {
1915 return mDragLayer;
1916 }
1917
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001918 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001919 return mAppsView;
1920 }
1921
Hyunyoung Song3f471442015-04-08 19:01:34 -07001922 public WidgetsContainerView getWidgetsView() {
1923 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001924 }
1925
Winson Chunga6945242014-01-08 14:04:34 -08001926 public Workspace getWorkspace() {
1927 return mWorkspace;
1928 }
1929
1930 public Hotseat getHotseat() {
1931 return mHotseat;
1932 }
1933
Jorim Jaggid017f882014-01-14 17:08:48 -08001934 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001935 return mOverviewPanel;
1936 }
1937
1938 public SearchDropTargetBar getSearchBar() {
1939 return mSearchDropTargetBar;
1940 }
1941
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001942 public LauncherAppWidgetHost getAppWidgetHost() {
1943 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 }
Romain Guycbb89e42009-06-08 15:52:54 -07001945
Winson Chunga9abd0e2010-10-27 17:18:37 -07001946 public LauncherModel getModel() {
1947 return mModel;
1948 }
1949
Winson Chunga6945242014-01-08 14:04:34 -08001950 protected SharedPreferences getSharedPrefs() {
1951 return mSharedPrefs;
1952 }
1953
Adam Cohen2e6da152015-05-06 11:42:25 -07001954 public DeviceProfile getDeviceProfile() {
1955 return mDeviceProfile;
1956 }
1957
Bjorn Bringertc459e522013-06-07 19:36:01 +01001958 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001959 getWindow().closeAllPanels();
1960
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001961 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001962 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001963 }
1964
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001965 @Override
1966 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001967 long startTime = 0;
1968 if (DEBUG_RESUME_TIME) {
1969 startTime = System.currentTimeMillis();
1970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971 super.onNewIntent(intent);
1972
1973 // Close the menu
1974 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001975 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001976 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001977
Adam Cohened307df2013-10-02 09:37:31 -07001978 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1979 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1980 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001981
Adam Cohen6fecd412013-10-02 17:41:50 -07001982 if (mWorkspace == null) {
1983 // Can be cases where mWorkspace is null, this prevents a NPE
1984 return;
1985 }
1986 Folder openFolder = mWorkspace.getOpenFolder();
1987 // In all these cases, only animate if we're already on home
1988 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001989
1990 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1991 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001992 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001993 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001994 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001995 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001996
Adam Cohen6fecd412013-10-02 17:41:50 -07001997 closeFolder();
1998 exitSpringLoadedDragMode();
1999
2000 // If we are already on home, then just animate back to the workspace,
2001 // otherwise, just wait until onResume to set the state back to Workspace
2002 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07002003 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07002004 } else {
2005 mOnResumeState = State.WORKSPACE;
2006 }
2007
2008 final View v = getWindow().peekDecorView();
2009 if (v != null && v.getWindowToken() != null) {
2010 InputMethodManager imm = (InputMethodManager)getSystemService(
2011 INPUT_METHOD_SERVICE);
2012 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
2013 }
2014
Winson Chungb745afb2015-03-02 11:51:23 -08002015 // Reset the apps view
2016 if (!alreadyOnHome && mAppsView != null) {
2017 mAppsView.scrollToTop();
2018 }
2019
Hyunyoung Song3f471442015-04-08 19:01:34 -07002020 // Reset the widgets view
2021 if (!alreadyOnHome && mWidgetsView != null) {
2022 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07002023 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002024
Adam Cohen9211d422014-10-07 18:14:53 -07002025 if (mLauncherCallbacks != null) {
2026 mLauncherCallbacks.onHomeIntent();
2027 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002028 }
Adam Cohened307df2013-10-02 09:37:31 -07002029
Michael Jurka447bf842013-05-15 14:52:15 +02002030 if (DEBUG_RESUME_TIME) {
2031 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
2032 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002033
Adam Cohen9211d422014-10-07 18:14:53 -07002034 if (mLauncherCallbacks != null) {
2035 mLauncherCallbacks.onNewIntent(intent);
2036 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002037 }
2038
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07002040 public void onRestoreInstanceState(Bundle state) {
2041 super.onRestoreInstanceState(state);
2042 for (int page: mSynchronouslyBoundPages) {
2043 mWorkspace.restoreInstanceStateForChild(page);
2044 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
2046
2047 @Override
2048 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002049 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002050 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2051 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002052 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002053 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002054
Michael Jurka883f55b2010-10-21 15:47:14 -07002055 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002056 // We close any open folder since it will not be re-opened, and we need to make sure
2057 // this state is reflected.
2058 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002059
Adam Cohendcd297f2013-06-18 13:13:40 -07002060 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002061 mWaitingForResult) {
2062 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002063 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002064 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2065 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002066 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2067 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2068 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002069 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002070 }
2071
Hyunyoung Song3f471442015-04-08 19:01:34 -07002072 // Save the current widgets tray?
2073 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002074 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002075
2076 if (mLauncherCallbacks != null) {
2077 mLauncherCallbacks.onSaveInstanceState(outState);
2078 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002079 }
2080
2081 @Override
2082 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002084
Winson Chunge7a03942011-08-05 15:05:12 -07002085 // Remove all pending runnables
2086 mHandler.removeMessages(ADVANCE_MSG);
2087 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002088 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002089
Winson Chungcd2b0142011-06-08 16:02:26 -07002090 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002091 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002092
2093 // It's possible to receive onDestroy after a new Launcher activity has
2094 // been created. In this case, don't interfere with the new Launcher.
2095 if (mModel.isCurrentCallbacks(this)) {
2096 mModel.stopLoader();
2097 app.setLauncher(null);
2098 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002099
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002101 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002103 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002104 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002105 mAppWidgetHost = null;
2106
2107 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108
2109 TextKeyListener.getInstance().release();
2110
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002111 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002112 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002113
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002114 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002115 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002116 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002117 mWorkspace = null;
2118 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002119
Michael Jurka2ecf9952012-06-18 12:52:28 -07002120 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002121
2122 if (mLauncherCallbacks != null) {
2123 mLauncherCallbacks.onDestroy();
2124 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125 }
2126
Adam Cohena9cf38f2011-05-02 15:36:58 -07002127 public DragController getDragController() {
2128 return mDragController;
2129 }
2130
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002131 @Override
2132 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002133 onStartForResult(requestCode);
2134 super.startActivityForResult(intent, requestCode);
2135 }
2136
2137 @Override
2138 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2139 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2140 onStartForResult(requestCode);
2141 try {
2142 super.startIntentSenderForResult(intent, requestCode,
2143 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2144 } catch (IntentSender.SendIntentException e) {
2145 throw new ActivityNotFoundException();
2146 }
2147 }
2148
2149 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002150 if (requestCode >= 0) {
2151 setWaitingForResult(true);
2152 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002153 }
2154
Winson Chung70d72102011-08-12 11:24:35 -07002155 /**
2156 * Indicates that we want global search for this activity by setting the globalSearch
2157 * argument for {@link #startSearch} to true.
2158 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002160 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002162
Karl Rosaen138a0412009-04-23 19:00:21 -07002163 if (initialQuery == null) {
2164 // Use any text typed in the launcher as the initial query
2165 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002166 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 if (appSearchData == null) {
2168 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002169 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002170 }
Winson Chungadf0c182012-08-23 14:59:07 -07002171 Rect sourceBounds = new Rect();
2172 if (mSearchDropTargetBar != null) {
2173 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2174 }
Romain Guycbb89e42009-06-08 15:52:54 -07002175
Ian Parkinson047036e2014-09-01 15:40:53 +01002176 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002177 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002178 if (clearTextImmediately) {
2179 clearTypedText();
2180 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002181
2182 // We need to show the workspace after starting the search
2183 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002184 }
2185
Ian Parkinson047036e2014-09-01 15:40:53 +01002186 /**
2187 * Start a text search.
2188 *
2189 * @return {@code true} if the search will start immediately, so any further keypresses
2190 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2191 * to buffer keypresses.
2192 */
2193 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002194 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002195 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2196 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2197 sourceBounds);
2198 }
2199
Michael Jurkaa33411c2012-06-14 16:18:21 -07002200 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002201 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002202 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002203 }
2204
2205 /**
2206 * Starts the global search activity. This code is a copied from SearchManager
2207 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002208 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002209 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002210 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002211 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2212 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2213 if (globalSearchActivity == null) {
2214 Log.w(TAG, "No global search activity found.");
2215 return;
2216 }
2217 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2218 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2219 intent.setComponent(globalSearchActivity);
2220 // Make sure that we have a Bundle to put source in
2221 if (appSearchData == null) {
2222 appSearchData = new Bundle();
2223 } else {
2224 appSearchData = new Bundle(appSearchData);
2225 }
Adam Cohen9211d422014-10-07 18:14:53 -07002226 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002227 if (!appSearchData.containsKey("source")) {
2228 appSearchData.putString("source", getPackageName());
2229 }
2230 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2231 if (!TextUtils.isEmpty(initialQuery)) {
2232 intent.putExtra(SearchManager.QUERY, initialQuery);
2233 }
2234 if (selectInitialQuery) {
2235 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2236 }
2237 intent.setSourceBounds(sourceBounds);
2238 try {
2239 startActivity(intent);
2240 } catch (ActivityNotFoundException ex) {
2241 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2242 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002243 }
2244
Yura4f93ec62014-02-04 14:15:21 +00002245 public boolean isOnCustomContent() {
2246 return mWorkspace.isOnOrMovingToCustomContent();
2247 }
2248
Winson Chung70d72102011-08-12 11:24:35 -07002249 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002250 public boolean onPrepareOptionsMenu(Menu menu) {
2251 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002252 if (!isOnCustomContent()) {
2253 // Close any open folders
2254 closeFolder();
2255 // Stop resizing any widgets
2256 mWorkspace.exitWidgetResizeMode();
2257 if (!mWorkspace.isInOverviewMode()) {
2258 // Show the overview mode
2259 showOverviewMode(true);
2260 } else {
2261 showWorkspace(true);
2262 }
Winson Chunge0298742014-01-17 12:03:00 -08002263 }
Adam Cohen9211d422014-10-07 18:14:53 -07002264 if (mLauncherCallbacks != null) {
2265 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2266 }
2267
Michael Jurkab94f3f82013-09-11 18:08:54 +02002268 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002269 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002270
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002271 @Override
2272 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002273 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002274 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002275 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002276 }
2277
Joe Onorato9c1289c2009-08-17 11:03:03 -04002278 public boolean isWorkspaceLocked() {
2279 return mWorkspaceLoading || mWaitingForResult;
2280 }
2281
Adam Cohen517a7f52014-03-01 12:12:59 -08002282 public boolean isWorkspaceLoading() {
2283 return mWorkspaceLoading;
2284 }
2285
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002286 private void setWorkspaceLoading(boolean value) {
2287 boolean isLocked = isWorkspaceLocked();
2288 mWorkspaceLoading = value;
2289 if (isLocked != isWorkspaceLocked()) {
2290 onWorkspaceLockedChanged();
2291 }
2292 }
2293
2294 private void setWaitingForResult(boolean value) {
2295 boolean isLocked = isWorkspaceLocked();
2296 mWaitingForResult = value;
2297 if (isLocked != isWorkspaceLocked()) {
2298 onWorkspaceLockedChanged();
2299 }
2300 }
2301
Adam Cohen9211d422014-10-07 18:14:53 -07002302 protected void onWorkspaceLockedChanged() {
2303 if (mLauncherCallbacks != null) {
2304 mLauncherCallbacks.onWorkspaceLockedChanged();
2305 }
2306 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002307
Michael Jurka0280c3b2010-09-17 15:00:07 -07002308 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002309 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002310 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002311 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2312 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002313 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002314 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002315 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002316
Sunny Goyale6b63a32015-01-16 16:14:29 -08002317 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002318 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002319 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2320 }
2321
Sunny Goyale6b63a32015-01-16 16:14:29 -08002322 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002323 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2324 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002325 if (appWidgetInfo.configure != null) {
2326 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002327 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002328
Bjorn Bringert7984c942009-12-09 15:38:25 +00002329 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002330 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2331 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2332
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002333 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002334 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002335 Runnable onComplete = new Runnable() {
2336 @Override
2337 public void run() {
2338 // Exit spring loaded mode if necessary after adding the widget
2339 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2340 null);
2341 }
2342 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002343 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002344 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002345 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002346 }
2347 }
Romain Guycbb89e42009-06-08 15:52:54 -07002348
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002349 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002350 // Close any folders that may be open.
2351 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002352 mWorkspace.moveToCustomContentScreen(animate);
2353 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002354
2355 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2356 int[] cell, int spanX, int spanY) {
2357 switch (info.itemType) {
2358 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2359 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2360 int span[] = new int[2];
2361 span[0] = spanX;
2362 span[1] = spanY;
2363 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2364 container, screenId, cell, span);
2365 break;
2366 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2367 processShortcutFromDrop(info.componentName, container, screenId, cell);
2368 break;
2369 default:
2370 throw new IllegalStateException("Unknown item type: " + info.itemType);
2371 }
2372 }
2373
Adam Cohenfbba09b2011-07-18 21:43:05 -07002374 /**
2375 * Process a shortcut drop.
2376 *
2377 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002378 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002379 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002380 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002381 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2382 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002383 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002384 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002385 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002386 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002387
2388 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002389 mPendingAddInfo.cellX = cell[0];
2390 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002391 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002392
2393 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2394 createShortcutIntent.setComponent(componentName);
2395 processShortcut(createShortcutIntent);
2396 }
2397
Adam Cohenfbba09b2011-07-18 21:43:05 -07002398 /**
2399 * Process a widget drop.
2400 *
2401 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002402 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002403 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002404 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002405 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2406 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002407 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002408 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002409 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002410 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002411 mPendingAddInfo.minSpanX = info.minSpanX;
2412 mPendingAddInfo.minSpanY = info.minSpanY;
2413
Adam Cohenfbba09b2011-07-18 21:43:05 -07002414 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002415 mPendingAddInfo.cellX = cell[0];
2416 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002417 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002418 if (span != null) {
2419 mPendingAddInfo.spanX = span[0];
2420 mPendingAddInfo.spanY = span[1];
2421 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002422
Adam Cohened66b2b2012-01-23 17:28:51 -08002423 AppWidgetHostView hostView = info.boundWidget;
2424 int appWidgetId;
2425 if (hostView != null) {
2426 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002427 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002428 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002429 // In this case, we either need to start an activity to get permission to bind
2430 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002431 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002432 Bundle options = info.bindOptions;
2433
Sunny Goyalffe83f12014-08-14 17:39:34 -07002434 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2435 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002436 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002437 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002438 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002439 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002440 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2441 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2442 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002443 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2444 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002445 // TODO: we need to make sure that this accounts for the options bundle.
2446 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002447 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2448 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002449 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002450 }
2451
Joe Onoratodeb98af2010-02-19 14:59:39 -08002452 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002453 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002454 }
2455
Winson Chung24ab2f12010-09-16 14:10:47 -07002456 void processWallpaper(Intent intent) {
2457 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2458 }
2459
Adam Cohendcd297f2013-06-18 13:13:40 -07002460 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002461 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002462 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002463 folderInfo.title = getText(R.string.folder_name);
2464
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002465 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002466 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2467 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002468 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002469
2470 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002471 FolderIcon newFolder =
2472 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002473 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002474 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002475 // Force measure the new folder icon
2476 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2477 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002478 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002479 }
Romain Guycbb89e42009-06-08 15:52:54 -07002480
Joe Onorato9c1289c2009-08-17 11:03:03 -04002481 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002482 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002483 }
2484
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002485 /**
2486 * Registers various content observers. The current implementation registers
2487 * only a favorites observer to keep track of the favorites applications.
2488 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002489 private void registerContentObservers() {
2490 ContentResolver resolver = getContentResolver();
2491 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2492 true, mWidgetObserver);
2493 }
2494
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002495 @Override
2496 public boolean dispatchKeyEvent(KeyEvent event) {
2497 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2498 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002500 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002501 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002502 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002503 dumpState();
2504 return true;
2505 }
2506 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002507 }
2508 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2509 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002510 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 return true;
2512 }
2513 }
2514
2515 return super.dispatchKeyEvent(event);
2516 }
2517
Joe Onorato88ec0992009-11-19 13:16:06 -08002518 @Override
2519 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002520 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2521 return;
2522 }
2523
Winson Chung3d9490a2015-03-24 17:38:42 -07002524 LauncherAccessibilityDelegate delegate =
2525 LauncherAppState.getInstance().getAccessibilityDelegate();
2526 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002527 return;
2528 }
2529
Winson Chungb745afb2015-03-02 11:51:23 -08002530 if (isAppsViewVisible()) {
2531 showWorkspace(true);
2532 } else if (isWidgetsViewVisible()) {
2533 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002534 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002535 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002536 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002537 Folder openFolder = mWorkspace.getOpenFolder();
2538 if (openFolder.isEditingName()) {
2539 openFolder.dismissEditingName();
2540 } else {
2541 closeFolder();
2542 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002543 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002544 mWorkspace.exitWidgetResizeMode();
2545
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002546 // Back button is a no-op here, but give at least some feedback for the button press
2547 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002548 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002549 }
2550
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002551 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002552 * Re-listen when widgets are reset.
2553 */
Adam Cohen091440a2015-03-18 14:16:05 -07002554 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002555 if (mAppWidgetHost != null) {
2556 mAppWidgetHost.startListening();
2557 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002558 }
2559
2560 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002561 * Launches the intent referred by the clicked shortcut.
2562 *
2563 * @param v The view representing the clicked shortcut.
2564 */
2565 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002566 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2567 // view has detached (it's possible for this to happen if the view is removed mid touch).
2568 if (v.getWindowToken() == null) {
2569 return;
2570 }
2571
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002572 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002573 return;
2574 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002575
Adam Cohen1697b792013-09-17 19:08:21 -07002576 if (v instanceof Workspace) {
2577 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002578 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002579 }
2580 return;
2581 }
2582
2583 if (v instanceof CellLayout) {
2584 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002585 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002586 }
2587 }
2588
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002590 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002591 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002592 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002593 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002594 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002595 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002596 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002597 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002598 } else if (tag instanceof AppInfo) {
2599 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002600 } else if (tag instanceof LauncherAppWidgetInfo) {
2601 if (v instanceof PendingAppWidgetHostView) {
2602 onClickPendingWidget((PendingAppWidgetHostView) v);
2603 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002604 }
2605 }
2606
Sunny Goyal508da152014-08-14 10:53:27 -07002607 public void onClickPagedViewIcon(View v) {
2608 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002609 if (mLauncherCallbacks != null) {
2610 mLauncherCallbacks.onClickPagedViewIcon(v);
2611 }
Sunny Goyal508da152014-08-14 10:53:27 -07002612 }
2613
Sunny Goyal70660032015-05-14 00:07:08 -07002614 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002615 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002616 return false;
2617 }
2618
Michael Jurkaaf442092010-06-10 17:01:57 -07002619 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002620 * Event handler for the app widget view which has not fully restored.
2621 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002622 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002623 if (mIsSafeModeEnabled) {
2624 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2625 return;
2626 }
2627
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002628 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002629 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002630 int widgetId = info.appWidgetId;
2631 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2632 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002633 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2634 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002635 mPendingAddInfo.copyFrom(info);
2636 mPendingAddWidgetId = widgetId;
2637
Sunny Goyalffe83f12014-08-14 17:39:34 -07002638 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2639 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002640 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002641 } else if (info.installProgress < 0) {
2642 // The install has not been queued
2643 final String packageName = info.providerName.getPackageName();
2644 showBrokenAppInstallDialog(packageName,
2645 new DialogInterface.OnClickListener() {
2646 public void onClick(DialogInterface dialog, int id) {
2647 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2648 }
2649 });
2650 } else {
2651 // Download has started.
2652 final String packageName = info.providerName.getPackageName();
2653 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002654 }
2655 }
2656
2657 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002658 * Event handler for the "grid" button that appears on the home screen, which
2659 * enters all apps mode.
2660 *
2661 * @param v The view that was clicked.
2662 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002663 protected void onClickAllAppsButton(View v) {
2664 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002665 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002666 showWorkspace(true);
2667 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002668 // Try and refresh the set of predicted apps before we enter launcher
2669 showAppsView(true /* animated */, false /* resetListToTop */,
2670 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002671 }
2672 }
2673
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002674 private void showBrokenAppInstallDialog(final String packageName,
2675 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002676 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002677 .setTitle(R.string.abandoned_promises_title)
2678 .setMessage(R.string.abandoned_promise_explanation)
2679 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2680 .setNeutralButton(R.string.abandoned_clean_this,
2681 new DialogInterface.OnClickListener() {
2682 public void onClick(DialogInterface dialog, int id) {
2683 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2684 mWorkspace.removeAbandonedPromise(packageName, user);
2685 }
2686 })
2687 .create().show();
2688 return;
2689 }
2690
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002691 /**
2692 * Event handler for an app shortcut click.
2693 *
2694 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2695 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002696 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002697 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2698 Object tag = v.getTag();
2699 if (!(tag instanceof ShortcutInfo)) {
2700 throw new IllegalArgumentException("Input must be a Shortcut");
2701 }
2702
2703 // Open shortcut
2704 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002705
2706 if (shortcut.isDisabled != 0) {
2707 int error = R.string.activity_not_available;
2708 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2709 error = R.string.safemode_shortcut_error;
2710 }
2711 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2712 return;
2713 }
2714
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002715 final Intent intent = shortcut.intent;
2716
2717 // Check for special shortcuts
2718 if (intent.getComponent() != null) {
2719 final String shortcutClass = intent.getComponent().getClassName();
2720
2721 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2722 MemoryDumpActivity.startDump(this);
2723 return;
2724 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2725 toggleShowWeightWatcher();
2726 return;
2727 }
2728 }
2729
Chris Wren40c5ed32014-06-24 18:24:23 -04002730 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002731 if ((v instanceof BubbleTextView)
2732 && shortcut.isPromise()
2733 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002734 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002735 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002736 new DialogInterface.OnClickListener() {
2737 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002738 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002739 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002740 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002741 return;
2742 }
2743
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002744 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002745 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002746
2747 if (mLauncherCallbacks != null) {
2748 mLauncherCallbacks.onClickAppShortcut(v);
2749 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002750 }
2751
Adam Cohen091440a2015-03-18 14:16:05 -07002752 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002753 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002754 final ShortcutInfo shortcut;
2755 final Intent intent;
2756 if (tag instanceof ShortcutInfo) {
2757 shortcut = (ShortcutInfo) tag;
2758 intent = shortcut.intent;
2759 int[] pos = new int[2];
2760 v.getLocationOnScreen(pos);
2761 intent.setSourceBounds(new Rect(pos[0], pos[1],
2762 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002763
Sunny Goyal508da152014-08-14 10:53:27 -07002764 } else if (tag instanceof AppInfo) {
2765 shortcut = null;
2766 intent = ((AppInfo) tag).intent;
2767 } else {
2768 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2769 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002770
2771 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002772 mStats.recordLaunch(intent, shortcut);
2773
2774 if (success && v instanceof BubbleTextView) {
2775 mWaitingForResume = (BubbleTextView) v;
2776 mWaitingForResume.setStayPressed(true);
2777 }
2778 }
2779
2780 /**
2781 * Event handler for a folder icon click.
2782 *
2783 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2784 */
2785 protected void onClickFolderIcon(View v) {
2786 if (LOGD) Log.d(TAG, "onClickFolder");
2787 if (!(v instanceof FolderIcon)){
2788 throw new IllegalArgumentException("Input must be a FolderIcon");
2789 }
2790
2791 FolderIcon folderIcon = (FolderIcon) v;
2792 final FolderInfo info = folderIcon.getFolderInfo();
2793 Folder openFolder = mWorkspace.getFolderForTag(info);
2794
2795 // If the folder info reports that the associated folder is open, then verify that
2796 // it is actually opened. There have been a few instances where this gets out of sync.
2797 if (info.opened && openFolder == null) {
2798 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2799 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2800 info.opened = false;
2801 }
2802
2803 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2804 // Close any open folder
2805 closeFolder();
2806 // Open the requested folder
2807 openFolder(folderIcon);
2808 } else {
2809 // Find the open folder...
2810 int folderScreen;
2811 if (openFolder != null) {
2812 folderScreen = mWorkspace.getPageForView(openFolder);
2813 // .. and close it
2814 closeFolder(openFolder);
2815 if (folderScreen != mWorkspace.getCurrentPage()) {
2816 // Close any folder open on the current screen
2817 closeFolder();
2818 // Pull the folder onto this screen
2819 openFolder(folderIcon);
2820 }
2821 }
2822 }
Adam Cohen9211d422014-10-07 18:14:53 -07002823
2824 if (mLauncherCallbacks != null) {
2825 mLauncherCallbacks.onClickFolderIcon(v);
2826 }
Michael Jurka5130e402011-10-13 04:55:35 -07002827 }
2828
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002829 /**
2830 * Event handler for the (Add) Widgets button that appears after a long press
2831 * on the home screen.
2832 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002833 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002834 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002835 if (mIsSafeModeEnabled) {
2836 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2837 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002838 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002839 if (mLauncherCallbacks != null) {
2840 mLauncherCallbacks.onClickAddWidgetButton(view);
2841 }
Adam Cohen9211d422014-10-07 18:14:53 -07002842 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002843 }
2844
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002845 /**
2846 * Event handler for the wallpaper picker button that appears after a long press
2847 * on the home screen.
2848 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002849 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002850 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002851 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2852 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002853
2854 if (mLauncherCallbacks != null) {
2855 mLauncherCallbacks.onClickWallpaperPicker(v);
2856 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002857 }
2858
2859 /**
2860 * Event handler for a click on the settings button that appears after a long press
2861 * on the home screen.
2862 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002863 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002864 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002865 if (mLauncherCallbacks != null) {
2866 mLauncherCallbacks.onClickSettingsButton(v);
2867 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002868 }
2869
Michael Jurka5130e402011-10-13 04:55:35 -07002870 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002871 // Provide the same haptic feedback that the system offers for virtual keys.
2872 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002873 }
2874
Adam Cohen61f560d2013-09-30 15:58:20 -07002875 public void performHapticFeedbackOnTouchDown(View v) {
2876 // Provide the same haptic feedback that the system offers for virtual keys.
2877 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2878 }
2879
2880 public View.OnTouchListener getHapticFeedbackTouchListener() {
2881 if (mHapticFeedbackTouchListener == null) {
2882 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002883 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002884 @Override
2885 public boolean onTouch(View v, MotionEvent event) {
2886 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2887 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2888 }
2889 return false;
2890 }
2891 };
2892 }
2893 return mHapticFeedbackTouchListener;
2894 }
2895
Adam Cohen9211d422014-10-07 18:14:53 -07002896 public void onDragStarted(View view) {
2897 if (isOnCustomContent()) {
2898 // Custom content screen doesn't participate in drag and drop. If on custom
2899 // content screen, move to default.
2900 moveWorkspaceToDefaultScreen();
2901 }
2902
2903 if (mLauncherCallbacks != null) {
2904 mLauncherCallbacks.onDragStarted(view);
2905 }
2906 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002907
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002908 /**
2909 * Called when the user stops interacting with the launcher.
2910 * This implies that the user is now on the homescreen and is not doing housekeeping.
2911 */
Adam Cohen9211d422014-10-07 18:14:53 -07002912 protected void onInteractionEnd() {
2913 if (mLauncherCallbacks != null) {
2914 mLauncherCallbacks.onInteractionEnd();
2915 }
2916 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002917
2918 /**
2919 * Called when the user starts interacting with the launcher.
2920 * The possible interactions are:
2921 * - open all apps
2922 * - reorder an app shortcut, or a widget
2923 * - open the overview mode.
2924 * This is a good time to stop doing things that only make sense
2925 * when the user is on the homescreen and not doing housekeeping.
2926 */
Adam Cohen9211d422014-10-07 18:14:53 -07002927 protected void onInteractionBegin() {
2928 if (mLauncherCallbacks != null) {
2929 mLauncherCallbacks.onInteractionBegin();
2930 }
2931 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002932
Winson Chungcd99cd32015-04-29 11:03:24 -07002933 /** Updates the interaction state. */
2934 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002935 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002936 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002937 boolean fromStateWithOverlay;
2938 boolean toStateWithOverlay;
2939 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2940 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2941 toStateWithOverlay = toState != Workspace.State.NORMAL;
2942 } else {
2943 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2944 fromState != Workspace.State.NORMAL_HIDDEN;
2945 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2946 toState != Workspace.State.NORMAL_HIDDEN;
2947 }
2948 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002949 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002950 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002951 onInteractionEnd();
2952 }
2953 }
2954
Kenny Guyf07af7b2014-07-31 11:39:16 +01002955 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002956 try {
2957 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002958 launcherApps.showAppDetailsForProfile(componentName, user);
2959 } catch (SecurityException e) {
2960 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2961 Log.e(TAG, "Launcher does not have permission to launch settings");
2962 } catch (ActivityNotFoundException e) {
2963 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2964 Log.e(TAG, "Unable to launch settings");
2965 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002966 }
2967
Michael Jurka1e2f4652013-07-08 18:03:46 -07002968 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002969 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2970 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002971 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002972 // System applications cannot be installed. For now, show a toast explaining that.
2973 // We may give them the option of disabling apps this way.
2974 int messageId = R.string.uninstall_system_app_text;
2975 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002976 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002977 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002978 String packageName = componentName.getPackageName();
2979 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002980 Intent intent = new Intent(
2981 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002982 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2983 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002984 if (user != null) {
2985 user.addToIntent(intent, Intent.EXTRA_USER);
2986 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002987 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002988 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002989 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002990 }
2991
Michael Jurka86a720e2012-05-09 11:23:23 -07002992 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002993 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002994 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002995 // Only launch using the new animation if the shortcut has not opted out (this is a
2996 // private contract between launcher and may be ignored in the future).
2997 boolean useLaunchAnimation = (v != null) &&
2998 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002999 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
3000 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01003001
Kenny Guy1317e2d2014-05-08 18:52:50 +01003002 UserHandleCompat user = null;
3003 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
3004 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
3005 user = userManager.getUserForSerialNumber(serialNumber);
3006 }
3007
3008 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07003009 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08003010 ActivityOptions opts = null;
3011 if (sClipRevealMethod != null) {
3012 // TODO: call method directly when Launcher3 can depend on M APIs
3013 int left = 0, top = 0;
3014 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
3015 if (v instanceof TextView) {
3016 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08003017 Drawable icon = Workspace.getTextViewIcon((TextView) v);
3018 if (icon != null) {
3019 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08003020 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08003021 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08003022 width = bounds.width();
3023 height = bounds.height();
3024 }
3025 }
3026 try {
3027 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
3028 left, top, width, height);
3029 } catch (IllegalAccessException e) {
3030 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
3031 sClipRevealMethod = null;
3032 } catch (InvocationTargetException e) {
3033 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
3034 sClipRevealMethod = null;
3035 }
3036 }
Adam Cohen2e52c902015-04-06 13:11:28 -07003037 if (opts == null && !Utilities.isLmpOrAbove()) {
3038 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08003039 v.getMeasuredWidth(), v.getMeasuredHeight());
3040 }
Adam Cohen2e52c902015-04-06 13:11:28 -07003041 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07003042 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01003043
3044 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
3045 // Could be launching some bookkeeping activity
3046 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003047 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003048 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003049 launcherApps.startActivityForProfile(intent.getComponent(), user,
3050 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003051 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003052 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003053 } catch (SecurityException e) {
3054 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003055 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003056 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003057 "or use the exported attribute for this activity. "
3058 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003059 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003060 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003061 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003062
Michael Jurka86a720e2012-05-09 11:23:23 -07003063 boolean startActivitySafely(View v, Intent intent, Object tag) {
3064 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003065 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3066 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3067 return false;
3068 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003069 try {
3070 success = startActivity(v, intent, tag);
3071 } catch (ActivityNotFoundException e) {
3072 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3073 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3074 }
3075 return success;
3076 }
3077
Adam Cohen268c4752012-06-06 17:47:33 -07003078 /**
3079 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3080 * in the DragLayer in the exact absolute location of the original FolderIcon.
3081 */
3082 private void copyFolderIconToImage(FolderIcon fi) {
3083 final int width = fi.getMeasuredWidth();
3084 final int height = fi.getMeasuredHeight();
3085
3086 // Lazy load ImageView, Bitmap and Canvas
3087 if (mFolderIconImageView == null) {
3088 mFolderIconImageView = new ImageView(this);
3089 }
3090 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3091 mFolderIconBitmap.getHeight() != height) {
3092 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3093 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3094 }
3095
3096 DragLayer.LayoutParams lp;
3097 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3098 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3099 } else {
3100 lp = new DragLayer.LayoutParams(width, height);
3101 }
3102
Adam Cohen307fe232012-08-16 17:55:58 -07003103 // The layout from which the folder is being opened may be scaled, adjust the starting
3104 // view size by this scale factor.
3105 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003106 lp.customPosition = true;
3107 lp.x = mRectForFolderAnimation.left;
3108 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003109 lp.width = (int) (scale * width);
3110 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003111
3112 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3113 fi.draw(mFolderIconCanvas);
3114 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003115 if (fi.getFolder() != null) {
3116 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3117 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003118 }
Adam Cohen268c4752012-06-06 17:47:33 -07003119 // Just in case this image view is still in the drag layer from a previous animation,
3120 // we remove it and re-add it.
3121 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3122 mDragLayer.removeView(mFolderIconImageView);
3123 }
3124 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003125 if (fi.getFolder() != null) {
3126 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003127 }
Adam Cohen268c4752012-06-06 17:47:33 -07003128 }
3129
Adam Cohen2801caf2011-05-13 20:57:39 -07003130 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003131 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003132 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3133 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3134 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3135
Adam Cohenc51934b2011-07-26 21:07:43 -07003136 FolderInfo info = (FolderInfo) fi.getTag();
3137 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3138 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003139 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3140 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003141 }
3142
Adam Cohen268c4752012-06-06 17:47:33 -07003143 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3144 copyFolderIconToImage(fi);
3145 fi.setVisibility(View.INVISIBLE);
3146
Michael Jurka2ecf9952012-06-18 12:52:28 -07003147 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003148 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003149 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003150 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3151 }
3152 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003153 oa.start();
3154 }
3155
Adam Cohen268c4752012-06-06 17:47:33 -07003156 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003157 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003158 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3159 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3160 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3161
Adam Cohen268c4752012-06-06 17:47:33 -07003162 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003163
Adam Cohen268c4752012-06-06 17:47:33 -07003164 // We remove and re-draw the FolderIcon in-case it has changed
3165 mDragLayer.removeView(mFolderIconImageView);
3166 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003167 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003168 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003169 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003170 oa.addListener(new AnimatorListenerAdapter() {
3171 @Override
3172 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003173 if (cl != null) {
3174 cl.clearFolderLeaveBehind();
3175 // Remove the ImageView copy of the FolderIcon and make the original visible.
3176 mDragLayer.removeView(mFolderIconImageView);
3177 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003178 }
3179 }
3180 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003181 oa.start();
3182 }
3183
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003184 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003185 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003186 * is animated relative to the specified View. If the View is null, no animation
3187 * is played.
3188 *
3189 * @param folderInfo The FolderInfo describing the folder to open.
3190 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003191 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003192 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003193 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3194 if (openFolder != null && openFolder != folder) {
3195 // Close any open folder before opening a folder.
3196 closeFolder();
3197 }
3198
Adam Cohena9cf38f2011-05-02 15:36:58 -07003199 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003200
Adam Cohena9cf38f2011-05-02 15:36:58 -07003201 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003202
Sunny Goyalf4066152015-04-15 09:42:19 -07003203 // While the folder is open, the position of the icon cannot change.
3204 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3205
Adam Cohen4554ee12011-08-03 16:13:21 -07003206 // Just verify that the folder hasn't already been added to the DragLayer.
3207 // There was a one-off crash where the folder had a parent already.
3208 if (folder.getParent() == null) {
3209 mDragLayer.addView(folder);
3210 mDragController.addDropTarget((DropTarget) folder);
3211 } else {
3212 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3213 folder.getParent() + ").");
3214 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003215 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003216 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003217
3218 // Notify the accessibility manager that this folder "window" has appeared and occluded
3219 // the workspace items
3220 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3221 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003222 }
3223
3224 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003225 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003226 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003227 if (folder.isEditingName()) {
3228 folder.dismissEditingName();
3229 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003230 closeFolder(folder);
3231 }
3232 }
3233
Sunny Goyal83a8f042015-05-19 12:52:12 -07003234 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003235 folder.getInfo().opened = false;
3236
3237 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3238 if (parent != null) {
3239 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3240 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003241 if (fi != null) {
3242 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3243 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003244 }
3245 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003246
3247 // Notify the accessibility manager that this folder "window" has disappeard and no
3248 // longer occludeds the workspace items
3249 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003250 }
3251
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003252 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003253 if (!isDraggingEnabled()) return false;
3254 if (isWorkspaceLocked()) return false;
3255 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003256
Adam Cohen1697b792013-09-17 19:08:21 -07003257 if (v instanceof Workspace) {
3258 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003259 if (!mWorkspace.isTouchActive()) {
3260 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003261 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3262 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3263 return true;
3264 } else {
3265 return false;
3266 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003267 } else {
3268 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003269 }
Adam Cohen1697b792013-09-17 19:08:21 -07003270 }
3271
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003272 CellLayout.CellInfo longClickCellInfo = null;
3273 View itemUnderLongClick = null;
3274 if (v.getTag() instanceof ItemInfo) {
3275 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003276 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003277 itemUnderLongClick = longClickCellInfo.cell;
3278 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003279 }
3280
Winson Chung3d503fb2011-07-13 17:25:49 -07003281 // The hotseat touch handling does not go through Workspace, and we always allow long press
3282 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003283 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003284 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003285 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003286 // User long pressed on empty space
3287 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3288 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003289 if (mWorkspace.isInOverviewMode()) {
3290 mWorkspace.startReordering(v);
3291 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003292 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003293 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003294 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003295 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3296 mHotseat.getOrderInHotseat(
3297 longClickCellInfo.cellX,
3298 longClickCellInfo.cellY));
3299 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003300 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003301 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003302 }
3303 }
3304 }
3305 return true;
3306 }
3307
Winson Chung3d503fb2011-07-13 17:25:49 -07003308 boolean isHotseatLayout(View layout) {
3309 return mHotseat != null && layout != null &&
3310 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3311 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003312
Winson Chung3d503fb2011-07-13 17:25:49 -07003313 /**
3314 * Returns the CellLayout of the specified container at the specified screen.
3315 */
Sunny Goyal92820592015-03-02 11:31:35 -08003316 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003317 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3318 if (mHotseat != null) {
3319 return mHotseat.getLayout();
3320 } else {
3321 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003322 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003323 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003324 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003325 }
3326 }
3327
Winson Chungb745afb2015-03-02 11:51:23 -08003328 /**
3329 * For overridden classes.
3330 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003331 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003332 return isAppsViewVisible();
3333 }
3334
3335 public boolean isAppsViewVisible() {
3336 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3337 }
3338
3339 public boolean isWidgetsViewVisible() {
3340 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003341 }
3342
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003343 private void setWorkspaceBackground(int background) {
3344 switch (background) {
3345 case WORKSPACE_BACKGROUND_TRANSPARENT:
3346 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3347 break;
3348 case WORKSPACE_BACKGROUND_BLACK:
3349 getWindow().setBackgroundDrawable(null);
3350 break;
3351 default:
3352 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3353 }
Craig Mautner360310b2012-10-26 15:13:08 -07003354 }
3355
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003356 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003357 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3358 int curflags = getWindow().getAttributes().flags
3359 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3360 if (wpflags != curflags) {
3361 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3362 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003363 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003364 }
3365
Michael Jurkae326f182011-11-21 14:05:46 -08003366 @Override
3367 public void onTrimMemory(int level) {
3368 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003369 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3370 // The widget preview db can result in holding onto over
3371 // 3MB of memory for caching which isn't necessary.
3372 SQLiteDatabase.releaseMemory();
3373
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003374 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003375 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003376 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003377 if (mLauncherCallbacks != null) {
3378 mLauncherCallbacks.onTrimMemory(level);
3379 }
Michael Jurkae326f182011-11-21 14:05:46 -08003380 }
3381
Winson Chungb745afb2015-03-02 11:51:23 -08003382 @Override
3383 public void onStateTransitionHideSearchBar() {
3384 // Hide the search bar
3385 if (mSearchDropTargetBar != null) {
3386 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3387 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003388 }
3389
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003390 public void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003391 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3392 true);
Adam Cohened307df2013-10-02 09:37:31 -07003393 }
3394
Sunny Goyal83a8f042015-05-19 12:52:12 -07003395 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003396 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3397 onCompleteRunnable, true);
3398 }
3399
3400 protected void showWorkspace(int snapToPage, boolean animated) {
3401 showWorkspace(snapToPage, animated, null, true);
3402 }
3403
3404 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3405 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003406 boolean changed = mState != State.WORKSPACE ||
3407 mWorkspace.getState() != Workspace.State.NORMAL;
3408 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003409 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003410 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003411 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003412 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003413
Winson Chungc7d2b602012-05-16 17:02:20 -07003414 // Show the search bar (only animate if we were showing the drop target bar in spring
3415 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003416 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003417 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003418 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003419
Michael Jurkab3e22d92011-10-31 15:58:33 -07003420 // Set focus to the AppsCustomize button
3421 if (mAllAppsButton != null) {
3422 mAllAppsButton.requestFocus();
3423 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003424 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003425
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003426 // Change the state *after* we've called all the transition code
3427 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003428
Winson Chung5fb63472011-02-02 17:03:37 -08003429 // Resume the auto-advance of widgets
3430 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003431 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003432
Winson Chung0f785722015-04-08 10:27:49 -07003433 if (changed) {
3434 // Send an accessibility event to announce the context change
3435 getWindow().getDecorView()
3436 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003437 if (notifyLauncherCallbacks) {
3438 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003439 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003440 mLauncherCallbacks.onAllAppsHidden();
3441 }
3442 }
Winson Chung0f785722015-04-08 10:27:49 -07003443 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003444 }
3445
Adam Cohened307df2013-10-02 09:37:31 -07003446 void showOverviewMode(boolean animated) {
3447 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003448 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003449 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3450 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003451 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003452 }
3453
Winson Chungb745afb2015-03-02 11:51:23 -08003454 /**
3455 * Shows the apps view.
3456 */
Winson Chung4ac30062015-05-08 17:34:17 -07003457 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003458 if (resetListToTop) {
3459 mAppsView.scrollToTop();
3460 }
Winson Chung4ac30062015-05-08 17:34:17 -07003461 if (updatePredictedApps) {
3462 tryAndUpdatePredictedApps();
3463 }
Winson Chungb745afb2015-03-02 11:51:23 -08003464 showAppsOrWidgets(animated, State.APPS);
3465 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003466
Winson Chungb745afb2015-03-02 11:51:23 -08003467 /**
3468 * Shows the widgets view.
3469 */
3470 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003471 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003472 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003473 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003474 }
Winson Chungb745afb2015-03-02 11:51:23 -08003475 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003476
3477 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003478 @Override
3479 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003480 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003481 }
3482 });
Winson Chungb745afb2015-03-02 11:51:23 -08003483 }
3484
3485 /**
3486 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003487 *
3488 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003489 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003490 // TODO: calling method should use the return value so that when {@code false} is returned
3491 // the workspace transition doesn't fall into invalid state.
3492 private boolean showAppsOrWidgets(boolean animated, State toState) {
3493 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3494 mState != State.WIDGETS_SPRING_LOADED) {
3495 return false;
3496 }
3497 if (toState != State.APPS && toState != State.WIDGETS) {
3498 return false;
3499 }
Winson Chungb745afb2015-03-02 11:51:23 -08003500
3501 if (toState == State.APPS) {
3502 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003503 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003504 mLauncherCallbacks.onAllAppsShown();
3505 }
Winson Chungb745afb2015-03-02 11:51:23 -08003506 } else {
3507 mStateTransitionAnimation.startAnimationToWidgets(animated);
3508 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003509
Michael Jurkab3e22d92011-10-31 15:58:33 -07003510 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003511 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003512
3513 // Pause the auto-advance of widgets until we are out of AllApps
3514 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003515 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003516 closeFolder();
3517
3518 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003519 getWindow().getDecorView()
3520 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003521 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003522 }
3523
Winson Chungcd99cd32015-04-29 11:03:24 -07003524 /**
3525 * Updates the workspace and interaction state on state change, and return the animation to this
3526 * new state.
3527 */
3528 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3529 boolean animated, HashMap<View, Integer> layerViews) {
3530 Workspace.State fromState = mWorkspace.getState();
3531 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3532 updateInteraction(fromState, toState);
3533 return anim;
3534 }
3535
Hyunyoung Song3f471442015-04-08 19:01:34 -07003536 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003537 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003538 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3539 mState == State.WIDGETS_SPRING_LOADED) {
3540 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003541 }
Winson Chungb745afb2015-03-02 11:51:23 -08003542
3543 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003544 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3545 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003546 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003547 }
Adam Cohen7777d962011-08-18 18:58:38 -07003548
Hyunyoung Song3f471442015-04-08 19:01:34 -07003549 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003550 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003551 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003552
Winson Chungcd99cd32015-04-29 11:03:24 -07003553 if (successfulDrop) {
3554 // We need to trigger all apps hidden to notify search to update itself before the
3555 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003556 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003557 mLauncherCallbacks.onAllAppsHidden();
3558 }
3559 }
3560
Winson Chunge7a03942011-08-05 15:05:12 -07003561 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003562 @Override
3563 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003564 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003565 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3566 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003567 // Before we show workspace, hide all apps again because
3568 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3569 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003570 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003571 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003572 } else {
3573 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003574 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003575 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003576 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003577 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003578
Michael Jurkad3ef3062010-11-23 16:23:58 -08003579 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003580 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003581 showAppsView(true /* animated */, false /* resetListToTop */,
3582 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003583 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003584 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003585 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003586 }
3587
Winson Chung4ac30062015-05-08 17:34:17 -07003588 /**
3589 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3590 * resumed.
3591 */
3592 private void tryAndUpdatePredictedApps() {
3593 if (mLauncherCallbacks != null) {
3594 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3595 if (!apps.isEmpty()) {
3596 mAppsView.setPredictedApps(apps);
3597 }
3598 }
3599 }
3600
Michael Jurkab3e22d92011-10-31 15:58:33 -07003601 void lockAllApps() {
3602 // TODO
3603 }
3604
3605 void unlockAllApps() {
3606 // TODO
3607 }
3608
Sunny Goyal594d76d2014-11-06 10:12:54 -08003609 protected void disableVoiceButtonProxy(boolean disable) {
3610 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003611 }
3612
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003613 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003614 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3615 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003616 }
3617
Adam Cohen24ce0b32014-01-14 16:18:14 -08003618 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003619 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3620 if (searchProvider == null) {
3621 return null;
3622 }
3623
3624 Bundle opts = new Bundle();
3625 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003626 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003627
3628 SharedPreferences sp = getSharedPreferences(
3629 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3630 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003631 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003632 if (!searchProvider.provider.flattenToString().equals(
3633 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003634 || (widgetInfo == null)
3635 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003636 // A valid widget is not already bound.
3637 if (widgetId > -1) {
3638 mAppWidgetHost.deleteAppWidgetId(widgetId);
3639 widgetId = -1;
3640 }
3641
3642 // Try to bind a new widget
3643 widgetId = mAppWidgetHost.allocateAppWidgetId();
3644
3645 if (!AppWidgetManagerCompat.getInstance(this)
3646 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3647 mAppWidgetHost.deleteAppWidgetId(widgetId);
3648 widgetId = -1;
3649 }
3650
3651 sp.edit()
3652 .putInt(QSB_WIDGET_ID, widgetId)
3653 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3654 .commit();
3655 }
3656
3657 if (widgetId != -1) {
3658 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3659 mQsb.updateAppWidgetOptions(opts);
3660 mQsb.setPadding(0, 0, 0, 0);
3661 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003662 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003663 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003664 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003665 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003666 }
3667
Sunny Goyal22235bc2015-04-03 09:23:43 -07003668 private void reinflateQSBIfNecessary() {
3669 if (mQsb instanceof LauncherAppWidgetHostView &&
3670 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3671 mSearchDropTargetBar.removeView(mQsb);
3672 mQsb = null;
3673 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3674 }
3675 }
3676
Michael Jurkad7c28052012-04-27 15:43:36 -07003677 @Override
3678 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003679 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003680 final List<CharSequence> text = event.getText();
3681 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003682 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003683 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003684 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003685 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003686 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003687 } else {
3688 text.add(getString(R.string.all_apps_home_button_label));
3689 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003690 return result;
3691 }
3692
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003693 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003694 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003695 */
Adam Cohen091440a2015-03-18 14:16:05 -07003696 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003697 @Override
3698 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003699 closeSystemDialogs();
3700 }
3701 }
3702
3703 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003704 * Receives notifications whenever the appwidgets are reset.
3705 */
3706 private class AppWidgetResetObserver extends ContentObserver {
3707 public AppWidgetResetObserver() {
3708 super(new Handler());
3709 }
3710
3711 @Override
3712 public void onChange(boolean selfChange) {
3713 onAppWidgetReset();
3714 }
3715 }
3716
3717 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003718 * If the activity is currently paused, signal that we need to run the passed Runnable
3719 * in onResume.
3720 *
3721 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003722 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3723 * wrong when we're not running, and if the activity comes back to what the configuration was
3724 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003725 *
3726 * Implementation of the method from LauncherModel.Callbacks.
3727 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003728 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003729 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003730 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003731 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003732 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003733 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003734 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003735 }
3736 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003737 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003738 return true;
3739 } else {
3740 return false;
3741 }
3742 }
3743
Michael Jurkac402cd92013-05-20 15:49:32 +02003744 private boolean waitUntilResume(Runnable run) {
3745 return waitUntilResume(run, false);
3746 }
3747
Michael Jurka1e2f4652013-07-08 18:03:46 -07003748 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003749 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003750 }
3751
Michael Jurka7607c2f2013-04-03 14:33:19 -07003752 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003753 * If the activity is currently paused, signal that we need to re-run the loader
3754 * in onResume.
3755 *
3756 * This needs to be called from incoming places where resources might have been loaded
3757 * while we are paused. That is becaues the Configuration might be wrong
3758 * when we're not running, and if it comes back to what it was when we
3759 * were paused, we are not restarted.
3760 *
3761 * Implementation of the method from LauncherModel.Callbacks.
3762 *
3763 * @return true if we are currently paused. The caller might be able to
3764 * skip some work in that case since we will come back again.
3765 */
3766 public boolean setLoadOnResume() {
3767 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003768 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003769 mOnResumeNeedsLoad = true;
3770 return true;
3771 } else {
3772 return false;
3773 }
3774 }
3775
3776 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003777 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003778 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003779 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003780 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003781 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003782 } else {
3783 return SCREEN_COUNT / 2;
3784 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003785 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003786
Joe Onorato9c1289c2009-08-17 11:03:03 -04003787 /**
3788 * Refreshes the shortcuts shown on the workspace.
3789 *
3790 * Implementation of the method from LauncherModel.Callbacks.
3791 */
3792 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003793 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003794
Michael Jurka7607c2f2013-04-03 14:33:19 -07003795 // If we're starting binding all over again, clear any bind calls we'd postponed in
3796 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3797 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003798 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003799
Winson Chungd64d1762013-08-20 14:37:16 -07003800 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003801 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003802 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003803
Michael Jurka05bf644e2011-11-30 20:28:53 -08003804 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003805 if (mHotseat != null) {
3806 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003807 }
3808 }
3809
Adam Cohendcd297f2013-06-18 13:13:40 -07003810 @Override
3811 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003812 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003813
Adam Cohen5084cba2013-09-03 12:01:16 -07003814 // If there are no screens, we need to have an empty screen
3815 if (orderedScreenIds.size() == 0) {
3816 mWorkspace.addExtraEmptyScreen();
3817 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003818
3819 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003820 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003821 if (hasCustomContentToLeft()) {
3822 mWorkspace.createCustomContentContainer();
3823 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003824 }
Winson Chung64359a52013-07-08 17:17:08 -07003825 }
3826
3827 @Override
3828 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003829 // Log to disk
3830 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3831 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3832 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003833 int count = orderedScreenIds.size();
3834 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003835 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003836 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003837 }
3838
Adam Cohen39a06042013-07-19 14:30:12 -07003839 private boolean shouldShowWeightWatcher() {
3840 String spKey = LauncherAppState.getSharedPreferencesKey();
3841 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003842 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003843
3844 return show;
3845 }
3846
3847 private void toggleShowWeightWatcher() {
3848 String spKey = LauncherAppState.getSharedPreferencesKey();
3849 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3850 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3851
3852 show = !show;
3853
3854 SharedPreferences.Editor editor = sp.edit();
3855 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3856 editor.commit();
3857
3858 if (mWeightWatcher != null) {
3859 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3860 }
3861 }
3862
Winson Chungd64d1762013-08-20 14:37:16 -07003863 public void bindAppsAdded(final ArrayList<Long> newScreens,
3864 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003865 final ArrayList<ItemInfo> addAnimated,
3866 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003867 Runnable r = new Runnable() {
3868 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003869 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003870 }
3871 };
3872 if (waitUntilResume(r)) {
3873 return;
3874 }
3875
Winson Chungd64d1762013-08-20 14:37:16 -07003876 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003877 if (newScreens != null) {
3878 bindAddScreens(newScreens);
3879 }
Winson Chungd64d1762013-08-20 14:37:16 -07003880
3881 // We add the items without animation on non-visible pages, and with
3882 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003883 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003884 bindItems(addNotAnimated, 0,
3885 addNotAnimated.size(), false);
3886 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003887 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003888 bindItems(addAnimated, 0,
3889 addAnimated.size(), true);
3890 }
Winson Chungc58497e2013-09-03 17:48:37 -07003891
Winson Chung87412982013-10-03 18:34:14 -07003892 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003893 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003894
Winson Chungb745afb2015-03-02 11:51:23 -08003895 if (addedApps != null && mAppsView != null) {
3896 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003897 }
Winson Chungd64d1762013-08-20 14:37:16 -07003898 }
3899
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003900 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003901 * Bind the items start-end from the list.
3902 *
3903 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003904 */
Winson Chung64359a52013-07-08 17:17:08 -07003905 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3906 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003907 Runnable r = new Runnable() {
3908 public void run() {
3909 bindItems(shortcuts, start, end, forceAnimateIcons);
3910 }
3911 };
3912 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003913 return;
3914 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003915
Winson Chungf0c6ae02012-03-21 16:10:31 -07003916 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003917 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3918 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003919 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003920 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003921 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003922 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003923 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003924
3925 // Short circuit if we are loading dock items for a configuration which has no dock
3926 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3927 mHotseat == null) {
3928 continue;
3929 }
3930
Joe Onorato9c1289c2009-08-17 11:03:03 -04003931 switch (item.itemType) {
3932 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3933 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003934 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003935 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003936
Winson Chung64359a52013-07-08 17:17:08 -07003937 /*
3938 * TODO: FIX collision case
3939 */
Winson Chungce376632013-07-11 16:12:41 -07003940 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3941 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3942 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003943 View v = cl.getChildAt(item.cellX, item.cellY);
3944 Object tag = v.getTag();
3945 String desc = "Collision while binding workspace item: " + item
3946 + ". Collides with " + tag;
3947 if (LauncherAppState.isDogfoodBuild()) {
3948 throw (new RuntimeException(desc));
3949 } else {
3950 Log.d(TAG, desc);
3951 }
Winson Chungce376632013-07-11 16:12:41 -07003952 }
Winson Chung64359a52013-07-08 17:17:08 -07003953 }
3954
Adam Cohendcd297f2013-06-18 13:13:40 -07003955 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3956 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003957 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003958 // Animate all the applications up now
3959 shortcut.setAlpha(0f);
3960 shortcut.setScaleX(0f);
3961 shortcut.setScaleY(0f);
3962 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003963 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003964 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003965 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003966 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003967 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003968 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003969 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003970 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3971 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003972 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003973 default:
3974 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003975 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003976 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003977
Winson Chung997a9232013-07-24 15:33:46 -07003978 if (animateIcons) {
3979 // Animate to the correct page
3980 if (newShortcutsScreenId > -1) {
3981 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003982 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003983 final Runnable startBounceAnimRunnable = new Runnable() {
3984 public void run() {
3985 anim.playTogether(bounceAnims);
3986 anim.start();
3987 }
3988 };
Winson Chung997a9232013-07-24 15:33:46 -07003989 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003990 // We post the animation slightly delayed to prevent slowdowns
3991 // when we are loading right after we return to launcher.
3992 mWorkspace.postDelayed(new Runnable() {
3993 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003994 if (mWorkspace != null) {
3995 mWorkspace.snapToPage(newScreenIndex);
3996 mWorkspace.postDelayed(startBounceAnimRunnable,
3997 NEW_APPS_ANIMATION_DELAY);
3998 }
Winson Chung94d67682013-09-25 16:29:40 -07003999 }
4000 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004001 } else {
4002 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004003 }
4004 }
Winson Chung64359a52013-07-08 17:17:08 -07004005 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004006 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004007 }
4008
Joe Onorato9c1289c2009-08-17 11:03:03 -04004009 /**
4010 * Implementation of the method from LauncherModel.Callbacks.
4011 */
Sunny Goyale2df0622015-04-24 11:27:00 -07004012 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004013 Runnable r = new Runnable() {
4014 public void run() {
4015 bindFolders(folders);
4016 }
4017 };
4018 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004019 return;
4020 }
Sunny Goyale2df0622015-04-24 11:27:00 -07004021 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004022 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004023
4024 /**
4025 * Add the views for a widget to the workspace.
4026 *
4027 * Implementation of the method from LauncherModel.Callbacks.
4028 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004029 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004030 Runnable r = new Runnable() {
4031 public void run() {
4032 bindAppWidget(item);
4033 }
4034 };
4035 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004036 return;
4037 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004038
Daniel Sandler843e8602010-06-07 14:59:01 -04004039 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4040 if (DEBUG_WIDGETS) {
4041 Log.d(TAG, "bindAppWidget: " + item);
4042 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004043 final Workspace workspace = mWorkspace;
4044
Adam Cohen59400422014-03-05 18:07:04 -08004045 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00004046 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08004047
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004048 if (!mIsSafeModeEnabled
4049 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4050 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004051 if (appWidgetInfo == null) {
4052 if (DEBUG_WIDGETS) {
4053 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4054 + " belongs to component " + item.providerName
4055 + ", as the povider is null");
4056 }
4057 LauncherModel.deleteItemFromDatabase(this, item);
4058 return;
4059 }
4060 // Note: This assumes that the id remap broadcast is received before this step.
4061 // If that is not the case, the id remap will be ignored and user may see the
4062 // click to setup view.
Adam Cohen2e6da152015-05-06 11:42:25 -07004063 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004064 pendingInfo.spanX = item.spanX;
4065 pendingInfo.spanY = item.spanY;
4066 pendingInfo.minSpanX = item.minSpanX;
4067 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004068 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004069 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004070
Sunny Goyalff572272014-07-23 13:58:07 -07004071 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004072 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4073 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004074
4075 // TODO consider showing a permission dialog when the widget is clicked.
4076 if (!success) {
4077 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4078 if (DEBUG_WIDGETS) {
4079 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4080 + " belongs to component " + item.providerName
4081 + ", as the launcher is unable to bing a new widget id");
4082 }
4083 LauncherModel.deleteItemFromDatabase(this, item);
4084 return;
4085 }
4086
4087 item.appWidgetId = newWidgetId;
4088
4089 // If the widget has a configure activity, it is still needs to set it up, otherwise
4090 // the widget is ready to go.
4091 item.restoreStatus = (appWidgetInfo.configure == null)
4092 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4093 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4094
4095 LauncherModel.updateItemInDatabase(this, item);
4096 }
4097
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004098 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004099 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004100 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004101 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4102 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004103 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004104
Sunny Goyal651077b2014-06-30 14:15:31 -07004105 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4106 } else {
4107 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004108 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4109 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004110 view.updateIcon(mIconCache);
4111 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004112 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004113 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004114 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004115
Joe Onorato9c1289c2009-08-17 11:03:03 -04004116 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004117 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004118
Adam Cohendcd297f2013-06-18 13:13:40 -07004119 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004120 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004121 if (!item.isCustomWidget()) {
4122 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4123 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004124
Joe Onorato9c1289c2009-08-17 11:03:03 -04004125 workspace.requestLayout();
4126
Daniel Sandler843e8602010-06-07 14:59:01 -04004127 if (DEBUG_WIDGETS) {
4128 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4129 + (SystemClock.uptimeMillis()-start) + "ms");
4130 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004131 }
4132
Sunny Goyalff572272014-07-23 13:58:07 -07004133 /**
4134 * Restores a pending widget.
4135 *
4136 * @param appWidgetId The app widget id
4137 * @param cellInfo The position on screen where to create the widget.
4138 */
4139 private void completeRestoreAppWidget(final int appWidgetId) {
4140 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4141 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4142 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4143 return;
4144 }
4145
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004146 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004147 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4148
4149 mWorkspace.reinflateWidgetsIfNecessary();
4150 LauncherModel.updateItemInDatabase(this, info);
4151 }
4152
Adam Cohen1462de32012-07-24 22:34:36 -07004153 public void onPageBoundSynchronously(int page) {
4154 mSynchronouslyBoundPages.add(page);
4155 }
4156
Joe Onorato9c1289c2009-08-17 11:03:03 -04004157 /**
4158 * Callback saying that there aren't any more items to bind.
4159 *
4160 * Implementation of the method from LauncherModel.Callbacks.
4161 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004162 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004163 Runnable r = new Runnable() {
4164 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004165 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004166 }
4167 };
4168 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004169 return;
4170 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004171 if (mSavedState != null) {
4172 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004173 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004174 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004175 mSavedState = null;
4176 }
4177
Adam Cohen1462de32012-07-24 22:34:36 -07004178 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004179
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004180 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004181 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004182
4183 // If we received the result of any pending adds while the loader was running (e.g. the
4184 // widget configuration forced an orientation change), process them now.
4185 if (sPendingAddItem != null) {
4186 final long screenId = completeAdd(sPendingAddItem);
4187
4188 // TODO: this moves the user to the page where the pending item was added. Ideally,
4189 // the screen would be guaranteed to exist after bind, and the page would be set through
4190 // the workspace restore process.
4191 mWorkspace.post(new Runnable() {
4192 @Override
4193 public void run() {
4194 mWorkspace.snapToScreenId(screenId);
4195 }
4196 });
4197 sPendingAddItem = null;
4198 }
4199
Sunny Goyal756adbc2015-04-16 15:20:51 -07004200 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004201
4202 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004203 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004204 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004205 }
4206
Adam Cohen3ed316a2014-07-23 18:21:20 -07004207 private void sendLoadingCompleteBroadcastIfNecessary() {
4208 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4209 String permission =
4210 getResources().getString(R.string.receive_first_load_broadcast_permission);
4211 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4212 sendBroadcast(intent, permission);
4213 SharedPreferences.Editor editor = mSharedPrefs.edit();
4214 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4215 editor.apply();
4216 }
4217 }
4218
Winson Chunga0b7e862013-09-05 16:03:15 -07004219 public boolean isAllAppsButtonRank(int rank) {
4220 if (mHotseat != null) {
4221 return mHotseat.isAllAppsButtonRank(rank);
4222 }
4223 return false;
4224 }
4225
Winson Chunga2413752012-04-03 14:22:34 -07004226 private boolean canRunNewAppsAnimation() {
4227 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4228 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4229 }
4230
Winson Chungc9168342013-06-26 14:54:55 -07004231 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4232 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4233 PropertyValuesHolder.ofFloat("alpha", 1f),
4234 PropertyValuesHolder.ofFloat("scaleX", 1f),
4235 PropertyValuesHolder.ofFloat("scaleY", 1f));
4236 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4237 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4238 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4239 return bounceAnim;
4240 }
4241
Adam Cohen27772732013-11-11 14:00:56 +00004242 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004243 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004244 }
4245
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004246 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004247 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004248 }
4249
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004250 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004251 if (mSearchDropTargetBar == null) {
4252 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004253 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004254 if (mQsb != null) {
4255 mSearchDropTargetBar.removeView(mQsb);
4256 mQsb = null;
4257 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004258 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004259 }
4260
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004261 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004262 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4263 * multiple calls to bind the same list.)
4264 */
4265 @Thunk ArrayList<AppInfo> mTmpAppsList;
4266 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4267 public void run() {
4268 bindAllApplications(mTmpAppsList);
4269 mTmpAppsList = null;
4270 }
4271 };
4272
4273 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004274 * Add the icons for all apps.
4275 *
4276 * Implementation of the method from LauncherModel.Callbacks.
4277 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004278 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004279 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4280 mTmpAppsList = apps;
4281 return;
4282 }
4283
Winson Chungb745afb2015-03-02 11:51:23 -08004284 if (mAppsView != null) {
4285 mAppsView.setApps(apps);
4286 }
Adam Cohen9211d422014-10-07 18:14:53 -07004287 if (mLauncherCallbacks != null) {
4288 mLauncherCallbacks.bindAllApplications(apps);
4289 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004290 }
4291
4292 /**
4293 * A package was updated.
4294 *
4295 * Implementation of the method from LauncherModel.Callbacks.
4296 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004297 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004298 Runnable r = new Runnable() {
4299 public void run() {
4300 bindAppsUpdated(apps);
4301 }
4302 };
4303 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004304 return;
4305 }
4306
Winson Chungb745afb2015-03-02 11:51:23 -08004307 if (mAppsView != null) {
4308 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004309 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004310 }
4311
Sunny Goyal4390ace2014-10-13 11:33:11 -07004312 @Override
4313 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4314 Runnable r = new Runnable() {
4315 public void run() {
4316 bindWidgetsRestored(widgets);
4317 }
4318 };
4319 if (waitUntilResume(r)) {
4320 return;
4321 }
4322 mWorkspace.widgetsRestored(widgets);
4323 }
4324
Joe Onorato9c1289c2009-08-17 11:03:03 -04004325 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004326 * Some shortcuts were updated in the background.
4327 *
4328 * Implementation of the method from LauncherModel.Callbacks.
4329 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004330 @Override
4331 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4332 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004333 Runnable r = new Runnable() {
4334 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004335 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004336 }
4337 };
4338 if (waitUntilResume(r)) {
4339 return;
4340 }
4341
Sunny Goyal4390ace2014-10-13 11:33:11 -07004342 if (!updated.isEmpty()) {
4343 mWorkspace.updateShortcuts(updated);
4344 }
4345
4346 if (!removed.isEmpty()) {
4347 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4348 for (ShortcutInfo si : removed) {
4349 removedComponents.add(si.getTargetComponent());
4350 }
4351 mWorkspace.removeItemsByComponentName(removedComponents, user);
4352 // Notify the drag controller
4353 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004354 }
4355 }
4356
4357 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004358 * Update the state of a package, typically related to install state.
4359 *
4360 * Implementation of the method from LauncherModel.Callbacks.
4361 */
Sunny Goyale755d462014-07-22 13:48:29 -07004362 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004363 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4364 Runnable r = new Runnable() {
4365 public void run() {
4366 bindRestoreItemsChange(updates);
4367 }
4368 };
4369 if (waitUntilResume(r)) {
4370 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004371 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004372
Sunny Goyal756adbc2015-04-16 15:20:51 -07004373 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004374 }
4375
4376 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004377 * A package was uninstalled. We take both the super set of packageNames
4378 * in addition to specific applications to remove, the reason being that
4379 * this can be called when a package is updated as well. In that scenario,
4380 * we only remove specific components from the workspace, where as
4381 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004382 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004383 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004384 * Implementation of the method from LauncherModel.Callbacks.
4385 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004386 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004387 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004388 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004389 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004390 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004391 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004392 }
Winson Chungd64d1762013-08-20 14:37:16 -07004393 };
4394 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004395 return;
4396 }
4397
Sunny Goyal1a745e82014-10-02 15:58:31 -07004398 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004399 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4400 for (AppInfo info : appInfos) {
4401 removedComponents.add(info.componentName);
4402 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004403 if (!packageNames.isEmpty()) {
4404 mWorkspace.removeItemsByPackageName(packageNames, user);
4405 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004406 if (!removedComponents.isEmpty()) {
4407 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004408 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004409 // Notify the drag controller
4410 mDragController.onAppsRemoved(packageNames, removedComponents);
4411
Sunny Goyal1a745e82014-10-02 15:58:31 -07004412 } else {
4413 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004414 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004415
Winson Chungdf95eb12013-10-16 14:57:07 -07004416 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004417 if (mAppsView != null) {
4418 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004419 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004420 }
Joe Onoratobe386092009-11-17 17:32:16 -08004421
Michael Jurkac402cd92013-05-20 15:49:32 +02004422 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004423 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004424 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004425 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004426 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004427
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004428 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004429 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004430 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004431 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004432 return;
4433 }
4434
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004435 if (mWidgetsView != null && model != null) {
4436 mWidgetsView.addWidgets(model);
4437 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004438 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004439 }
4440
Winson Chung400438b2011-01-16 17:53:48 -08004441 private int mapConfigurationOriActivityInfoOri(int configOri) {
4442 final Display d = getWindowManager().getDefaultDisplay();
4443 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4444 switch (d.getRotation()) {
4445 case Surface.ROTATION_0:
4446 case Surface.ROTATION_180:
4447 // We are currently in the same basic orientation as the natural orientation
4448 naturalOri = configOri;
4449 break;
4450 case Surface.ROTATION_90:
4451 case Surface.ROTATION_270:
4452 // We are currently in the other basic orientation to the natural orientation
4453 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4454 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4455 break;
4456 }
4457
4458 int[] oriMap = {
4459 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4460 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4461 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4462 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4463 };
4464 // Since the map starts at portrait, we need to offset if this device's natural orientation
4465 // is landscape.
4466 int indexOffset = 0;
4467 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4468 indexOffset = 1;
4469 }
4470 return oriMap[(d.getRotation() + indexOffset) % 4];
4471 }
Adam Cohen446e9402011-09-15 18:21:21 -07004472
Winson Chung4b919f82012-05-01 10:44:08 -07004473 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004474 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004475 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4476 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4477 .getConfiguration().orientation));
4478 } else {
4479 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4480 }
Winson Chung4b919f82012-05-01 10:44:08 -07004481 }
4482 }
4483 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004484 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004485 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004486 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004487 } else {
4488 mHandler.postDelayed(new Runnable() {
4489 public void run() {
4490 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4491 }
4492 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004493 }
Winson Chung4b919f82012-05-01 10:44:08 -07004494 }
Winson Chung400438b2011-01-16 17:53:48 -08004495 }
4496
Winson Chunge43a1e72014-01-15 10:33:02 -08004497 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004498 if (mLauncherCallbacks != null) {
4499 return mLauncherCallbacks.isLauncherPreinstalled();
4500 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004501 PackageManager pm = getPackageManager();
4502 try {
4503 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4504 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4505 return true;
4506 } else {
4507 return false;
4508 }
4509 } catch (NameNotFoundException e) {
4510 e.printStackTrace();
4511 return false;
4512 }
4513 }
4514
Adam Cohenea90f832014-05-21 15:03:34 -07004515 /**
4516 * This method indicates whether or not we should suggest default wallpaper dimensions
4517 * when our wallpaper cropper was not yet used to set a wallpaper.
4518 */
4519 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004520 if (mLauncherCallbacks != null) {
4521 return mLauncherCallbacks.overrideWallpaperDimensions();
4522 }
Adam Cohenea90f832014-05-21 15:03:34 -07004523 return true;
4524 }
4525
Adam Cohen432609a2014-03-13 17:03:22 -07004526 /**
4527 * To be overridden by subclasses to indicate that there is an activity to launch
4528 * before showing the standard launcher experience.
4529 */
4530 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004531 if (mLauncherCallbacks != null) {
4532 return mLauncherCallbacks.hasFirstRunActivity();
4533 }
Adam Cohen432609a2014-03-13 17:03:22 -07004534 return false;
4535 }
4536
4537 /**
4538 * To be overridden by subclasses to launch any first run activity
4539 */
4540 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004541 if (mLauncherCallbacks != null) {
4542 return mLauncherCallbacks.getFirstRunActivity();
4543 }
Adam Cohen432609a2014-03-13 17:03:22 -07004544 return null;
4545 }
4546
Winson Chung2826a402015-04-17 16:18:53 -07004547 /**
4548 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004549 */
4550 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004551 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4552 return false;
4553 }
4554
Winson Chung2826a402015-04-17 16:18:53 -07004555 if (mLauncherCallbacks != null) {
4556 return mLauncherCallbacks.overrideAllAppsSearch();
4557 }
4558 return false;
4559 }
4560
Winson Chunga6945242014-01-08 14:04:34 -08004561 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004562 return !ActivityManager.isRunningInTestHarness() &&
4563 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004564 }
4565
Adam Cohen4a9423d2014-03-28 14:22:31 -07004566 protected boolean hasRunFirstRunActivity() {
4567 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4568 }
4569
Adam Cohen432609a2014-03-13 17:03:22 -07004570 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004571 if (shouldRunFirstRunActivity() &&
4572 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004573 Intent firstRunIntent = getFirstRunActivity();
4574 if (firstRunIntent != null) {
4575 startActivity(firstRunIntent);
4576 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004577 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004578 }
4579 }
Adam Cohen432609a2014-03-13 17:03:22 -07004580 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004581 }
4582
4583 private void markFirstRunActivityShown() {
4584 SharedPreferences.Editor editor = mSharedPrefs.edit();
4585 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4586 editor.apply();
4587 }
4588
Adam Cohen432609a2014-03-13 17:03:22 -07004589 /**
4590 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4591 * screen that must be displayed and dismissed.
4592 */
4593 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004594 if (mLauncherCallbacks != null) {
4595 return mLauncherCallbacks.hasDismissableIntroScreen();
4596 }
Adam Cohen432609a2014-03-13 17:03:22 -07004597 return false;
4598 }
4599
4600 /**
4601 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4602 */
4603 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004604 if (mLauncherCallbacks != null) {
4605 return mLauncherCallbacks.getIntroScreen();
4606 }
Adam Cohen432609a2014-03-13 17:03:22 -07004607 return null;
4608 }
4609
4610 /**
4611 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4612 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4613 */
4614 private boolean shouldShowIntroScreen() {
4615 return hasDismissableIntroScreen() &&
4616 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4617 }
4618
4619 protected void showIntroScreen() {
4620 View introScreen = getIntroScreen();
4621 changeWallpaperVisiblity(false);
4622 if (introScreen != null) {
4623 mDragLayer.showOverlayView(introScreen);
4624 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004625 if (mLauncherOverlayContainer != null) {
4626 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4627 }
Adam Cohen432609a2014-03-13 17:03:22 -07004628 }
4629
4630 public void dismissIntroScreen() {
4631 markIntroScreenDismissed();
4632 if (showFirstRunActivity()) {
4633 // We delay hiding the intro view until the first run activity is showing. This
4634 // avoids a blip.
4635 mWorkspace.postDelayed(new Runnable() {
4636 @Override
4637 public void run() {
4638 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004639 if (mLauncherOverlayContainer != null) {
4640 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4641 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004642 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004643 }
4644 }, ACTIVITY_START_DELAY);
4645 } else {
4646 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004647 if (mLauncherOverlayContainer != null) {
4648 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4649 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004650 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004651 }
4652 changeWallpaperVisiblity(true);
4653 }
4654
4655 private void markIntroScreenDismissed() {
4656 SharedPreferences.Editor editor = mSharedPrefs.edit();
4657 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4658 editor.apply();
4659 }
4660
Adam Cohen091440a2015-03-18 14:16:05 -07004661 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004662 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4663 // on the device, then we always show the first run cling experience (or if there is no
4664 // launcher2). Otherwise, we prompt the user upon started for migration
4665 LauncherClings launcherClings = new LauncherClings(this);
4666 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4667 if (mModel.canMigrateFromOldLauncherDb(this)) {
4668 launcherClings.showMigrationCling();
4669 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004670 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004671 }
4672 }
4673 }
4674
Winson Chunga6945242014-01-08 14:04:34 -08004675 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004676 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4677 if (mHotseat != null) mHotseat.setAlpha(1f);
4678 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4679 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004680 }
4681
4682 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004683 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4684 if (mHotseat != null) mHotseat.setAlpha(0f);
4685 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4686 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004687 }
4688
Mathew Inwood72fbec12013-11-19 15:45:07 +00004689 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004690 // Called from search suggestion, not supported in other profiles.
4691 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4692 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4693 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4694 myUser);
4695 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004696 return null;
4697 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004698 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004699 }
4700
4701 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4702 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004703 // Called from search suggestion, not supported in other profiles.
4704 return createShortcutDragInfo(shortcutIntent, caption, icon,
4705 UserHandleCompat.myUserHandle());
4706 }
4707
4708 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4709 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004710 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004711 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004712 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004713 }
4714
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004715 protected void moveWorkspaceToDefaultScreen() {
4716 mWorkspace.moveToDefaultScreen(false);
4717 }
4718
Mathew Inwood72fbec12013-11-19 15:45:07 +00004719 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4720 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004721 mWorkspace.onExternalDragStartedWithItem(dragView);
4722 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004723 }
4724
Anjali Koppalf5d29132014-02-28 13:33:27 -08004725 @Override
4726 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004727 if (mLauncherCallbacks != null) {
4728 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4729 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004730 }
4731
Winson Chung80baf5a2010-08-09 16:03:15 -07004732 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004733 * Prints out out state for debugging.
4734 */
4735 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004736 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004737 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004738 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4739 Log.d(TAG, "mRestoring=" + mRestoring);
4740 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4741 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004742 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004743 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004744 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004745
Daniel Sandler325dc232013-06-05 22:57:57 -04004746 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004747 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004748
4749 @Override
4750 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4751 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004752 synchronized (sDumpLogs) {
4753 writer.println(" ");
4754 writer.println("Debug logs: ");
4755 for (int i = 0; i < sDumpLogs.size(); i++) {
4756 writer.println(" " + sDumpLogs.get(i));
4757 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004758 }
Adam Cohen9211d422014-10-07 18:14:53 -07004759 if (mLauncherCallbacks != null) {
4760 mLauncherCallbacks.dump(prefix, fd, writer, args);
4761 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004762 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004763
4764 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004765 if (DEBUG_DUMP_LOG) {
4766 synchronized (sDumpLogs) {
4767 Log.d(TAG, "");
4768 Log.d(TAG, "*********************");
4769 Log.d(TAG, "Launcher debug logs: ");
4770 for (int i = 0; i < sDumpLogs.size(); i++) {
4771 Log.d(TAG, " " + sDumpLogs.get(i));
4772 }
4773 Log.d(TAG, "*********************");
4774 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004775 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004776 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004777 }
4778
4779 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004780 addDumpLog(tag, log, null, debugLog);
4781 }
4782
4783 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004784 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004785 if (e != null) {
4786 Log.d(tag, log, e);
4787 } else {
4788 Log.d(tag, log);
4789 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004790 }
Winson Chungede41292013-09-19 16:27:36 -07004791 if (DEBUG_DUMP_LOG) {
4792 sDateStamp.setTime(System.currentTimeMillis());
4793 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004794 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4795 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004796 }
Winson Chungede41292013-09-19 16:27:36 -07004797 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004798 }
4799
Adam Cohen59400422014-03-05 18:07:04 -08004800 public static CustomAppWidget getCustomAppWidget(String name) {
4801 return sCustomAppWidgets.get(name);
4802 }
4803
4804 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4805 return sCustomAppWidgets;
4806 }
4807
Winson Chungede41292013-09-19 16:27:36 -07004808 public void dumpLogsToLocalData() {
4809 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004810 new AsyncTask<Void, Void, Void>() {
4811 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004812 boolean success = false;
4813 sDateStamp.setTime(sRunStart);
4814 String FILENAME = sDateStamp.getMonth() + "-"
4815 + sDateStamp.getDay() + "_"
4816 + sDateStamp.getHours() + "-"
4817 + sDateStamp.getMinutes() + "_"
4818 + sDateStamp.getSeconds() + ".txt";
4819
4820 FileOutputStream fos = null;
4821 File outFile = null;
4822 try {
4823 outFile = new File(getFilesDir(), FILENAME);
4824 outFile.createNewFile();
4825 fos = new FileOutputStream(outFile);
4826 } catch (Exception e) {
4827 e.printStackTrace();
4828 }
4829 if (fos != null) {
4830 PrintWriter writer = new PrintWriter(fos);
4831
4832 writer.println(" ");
4833 writer.println("Debug logs: ");
4834 synchronized (sDumpLogs) {
4835 for (int i = 0; i < sDumpLogs.size(); i++) {
4836 writer.println(" " + sDumpLogs.get(i));
4837 }
4838 }
4839 writer.close();
4840 }
4841 try {
4842 if (fos != null) {
4843 fos.close();
4844 success = true;
4845 }
4846 } catch (IOException e) {
4847 e.printStackTrace();
4848 }
Michael Jurka43467462013-11-14 12:03:58 +01004849 return null;
Winson Chungede41292013-09-19 16:27:36 -07004850 }
Michael Jurka43467462013-11-14 12:03:58 +01004851 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004852 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004853 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004854}
Michael Jurka2763be32011-02-24 11:19:57 -08004855
Dan Sandlerd5024042014-01-09 15:01:33 -05004856interface DebugIntents {
4857 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4858 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004859}