blob: 1aa446e314ee5adf7565acd8837009bc72e5a242 [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;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070091import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Advanceable;
94import android.widget.FrameLayout;
95import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080096import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070097import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070098
Sunny Goyal651077b2014-06-30 14:15:31 -070099import com.android.launcher3.DropTarget.DragObject;
100import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyal83a8f042015-05-19 12:52:12 -0700101import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700102import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700103import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100104import com.android.launcher3.compat.LauncherActivityInfoCompat;
105import com.android.launcher3.compat.LauncherAppsCompat;
106import com.android.launcher3.compat.UserHandleCompat;
107import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700108import com.android.launcher3.model.WidgetsModel;
Sunny Goyale2df0622015-04-24 11:27:00 -0700109import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700110import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700111import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700112import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700113import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700114
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115import java.io.DataInputStream;
116import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700118import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700120import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700121import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700122import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800123import java.lang.reflect.InvocationTargetException;
124import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700125import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700127import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700128import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700129import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700130import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700131import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000132import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134/**
135 * Default launcher application.
136 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100137public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700138 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800139 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
140 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700141 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700142 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Winson Chung83f59ab2015-05-05 17:21:58 -0700144 // Temporary flag
145 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
146
Daniel Sandlerf061f822013-06-27 13:59:36 -0400147 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700148 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700149 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400150 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700151 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700152
Dan Sandlerd5024042014-01-09 15:01:33 -0500153 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700157 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700158 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Michael Jurka8b805b12012-04-18 14:23:14 -0700160 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700161 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700162
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700163 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
164 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
165 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
166
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700167 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
168
Mathew Inwood876a8462013-06-14 14:12:41 +0100169 /**
170 * IntentStarter uses request codes starting with this. This must be greater than all activity
171 * request codes used internally.
172 */
173 protected static final int REQUEST_LAST = 100;
174
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800175 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Michael Jurka0a457bf2012-11-19 14:05:05 -0800177 // To turn on these properties, type
178 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800179 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Winson Chung2672ff92012-05-04 16:22:30 -0700181 // The Intent extra that defines whether to ignore the launch animation
182 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400183 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 // Type: int
186 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700187 // Type: int
188 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700190 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
191 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
193 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700194 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700196 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700197 // Type: int
198 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
199 // Type: int
200 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
201 // Type: parcelable
202 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000203 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800204 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000205 // Type: int[]
206 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
Adam Cohen432609a2014-03-13 17:03:22 -0700208 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800209 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
210
Adam Cohen3ed316a2014-07-23 18:21:20 -0700211 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
212 static final String ACTION_FIRST_LOAD_COMPLETE =
213 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
214
Adam Cohen39a06042013-07-19 14:30:12 -0700215 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700216 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700217
Sunny Goyal594d76d2014-11-06 10:12:54 -0800218 private static final String QSB_WIDGET_ID = "qsb_widget_id";
219 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
220
Winson Chunga6945242014-01-08 14:04:34 -0800221 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
222
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700223 /** The different states that Launcher can be in. */
Winson Chungb745afb2015-03-02 11:51:23 -0800224 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED };
Adam Cohen091440a2015-03-18 14:16:05 -0700225 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700226 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700227
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700228 private boolean mIsSafeModeEnabled;
229
Adam Cohenc2d6e892014-10-16 09:49:24 -0700230 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
231 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700232 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700233
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700235 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
236 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700237 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000239 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
240 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
241
Winson Chunga2413752012-04-03 14:22:34 -0700242 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700243 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
244 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700245 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700246
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800247 private final BroadcastReceiver mCloseSystemDialogsReceiver
248 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800249 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private LayoutInflater mInflater;
252
Adam Cohen091440a2015-03-18 14:16:05 -0700253 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700254 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800255 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700256 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800257 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700258 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700259
Sunny Goyalffe83f12014-08-14 17:39:34 -0700260 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700261 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700262
Adam Cohen091440a2015-03-18 14:16:05 -0700263 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800264 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800265 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700266
Michael Jurka0280c3b2010-09-17 15:00:07 -0700267 private int[] mTmpAddItemCellCoordinates = new int[2];
268
Winson Chung3d503fb2011-07-13 17:25:49 -0700269 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800270 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700271
Michael Jurka838a4ca2011-02-07 13:33:06 -0800272 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700273
Winson Chungc51db6a2011-10-05 11:44:49 -0700274 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700275
276 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700277 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700278
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700279 // Main container view and the model for the widget tray screen.
280 @Thunk WidgetsContainerView mWidgetsView;
281 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700282
Winson Chungf0ea4d32011-06-06 14:27:16 -0700283 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800284 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700287 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
288 // scroll issues (because the workspace may not have been measured yet) and extra work.
289 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
290 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291
292 private SpannableStringBuilder mDefaultKeySsb = null;
293
Adam Cohen091440a2015-03-18 14:16:05 -0700294 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400295
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800296 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 private boolean mRestoring;
298 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700299 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300
Michael Jurka1e2f4652013-07-08 18:03:46 -0700301 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700302 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 private Bundle mSavedInstanceState;
305
Joe Onorato9c1289c2009-08-17 11:03:03 -0400306 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800307 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700308 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800309 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700310 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800311 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400312
Adam Cohen091440a2015-03-18 14:16:05 -0700313 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700314
Sunny Goyale2df0622015-04-24 11:27:00 -0700315 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700316
Adam Cohen61f560d2013-09-30 15:58:20 -0700317 private View.OnTouchListener mHapticFeedbackTouchListener;
318
Adam Cohended9f8d2010-11-03 13:25:16 -0700319 // Related to the auto-advancing of widgets
320 private final int ADVANCE_MSG = 1;
321 private final int mAdvanceInterval = 20000;
322 private final int mAdvanceStagger = 250;
323 private long mAutoAdvanceSentTime;
324 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700325 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700326 new HashMap<View, AppWidgetProviderInfo>();
327
Winson Chung400438b2011-01-16 17:53:48 -0800328 // Determines how long to wait after a rotation before restoring the screen orientation to
329 // match the sensor state.
330 private final int mRestoreScreenOrientationDelay = 500;
331
Adam Cohen091440a2015-03-18 14:16:05 -0700332 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700333
Adam Cohen1462de32012-07-24 22:34:36 -0700334 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700335 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700336
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700337 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700338 static Date sDateStamp = new Date();
339 static DateFormat sDateFormat =
340 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
341 static long sRunStart = System.currentTimeMillis();
342 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700343
Winson Chung46353de2012-02-16 14:05:10 -0800344 // We only want to get the SharedPreferences once since it does an FS stat each time we get
345 // it from the context.
346 private SharedPreferences mSharedPrefs;
347
Winson Chungf0c6ae02012-03-21 16:10:31 -0700348 // Holds the page that we need to animate to, and the icon views that we need to animate up
349 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700350 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700351 private Bitmap mFolderIconBitmap;
352 private Canvas mFolderIconCanvas;
353 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700354
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700355 private DeviceProfile mDeviceProfile;
356
Winson Chung13eb5272015-05-11 16:30:13 -0700357 // This is set to the view that launched the activity that navigated the user away from
358 // launcher. Since there is no callback for when the activity has finished launching, enable
359 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800360 private BubbleTextView mWaitingForResume;
361
Adam Cohen59400422014-03-05 18:07:04 -0800362 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
363 new HashMap<String, CustomAppWidget>();
364
365 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
366 static {
367 if (ENABLE_CUSTOM_WIDGET_TEST) {
368 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
369 }
370 }
371
Chet Haasea8f996d2015-02-17 12:56:04 -0800372 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
373 private static Method sClipRevealMethod = null;
374 static {
375 Class<?> activityOptionsClass = ActivityOptions.class;
376 try {
377 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
378 View.class, int.class, int.class, int.class, int.class);
379 } catch (Exception e) {
380 // Earlier version
381 }
382 }
383
Adam Cohen091440a2015-03-18 14:16:05 -0700384 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700385 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700386 if (mWorkspace != null) {
387 mWorkspace.buildPageHardwareLayers();
388 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700389 }
390 };
391
Adam Cohendb364c32014-05-20 14:23:40 -0700392 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800393
Adam Cohen091440a2015-03-18 14:16:05 -0700394 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800395 int requestCode;
396 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700397 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700398 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800399 int cellX;
400 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700401 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800402 }
403
Daniel Sandlerff02d492013-08-05 02:12:05 -0400404 private Stats mStats;
405
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700406 FocusIndicatorView mFocusHandler;
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 @Override
409 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700410 if (DEBUG_STRICT_MODE) {
411 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
412 .detectDiskReads()
413 .detectDiskWrites()
414 .detectNetwork() // or .detectAll() for all detectable problems
415 .penaltyLog()
416 .build());
417 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
418 .detectLeakedSqlLiteObjects()
419 .detectLeakedClosableObjects()
420 .penaltyLog()
421 .penaltyDeath()
422 .build());
423 }
424
Adam Cohen9211d422014-10-07 18:14:53 -0700425 if (mLauncherCallbacks != null) {
426 mLauncherCallbacks.preOnCreate();
427 }
428
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800429 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400430
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400431 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400432 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700433 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700434
Adam Cohen2e6da152015-05-06 11:42:25 -0700435 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700436 mDeviceProfile = getResources().getConfiguration().orientation
437 == Configuration.ORIENTATION_LANDSCAPE ?
438 app.getInvariantDeviceProfile().landscapeProfile
439 : app.getInvariantDeviceProfile().portraitProfile;
440
441 // TODO: Move this to icon cache.
442 Utilities.setIconSize(mDeviceProfile.iconSizePx);
Winson Chung5f8afe62013-08-12 16:19:28 -0700443
444 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400445 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700446 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700447 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800448 mModel = app.setLauncher(this);
449 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700450
Joe Onorato41a12d22009-10-31 18:30:00 -0400451 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800453 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700454
Daniel Sandlerff02d492013-08-05 02:12:05 -0400455 mStats = new Stats(this);
456
Sunny Goyalffe83f12014-08-14 17:39:34 -0700457 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700458
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700459 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
460 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700461
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700462 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
463 // this also ensures that any synchronous binding below doesn't re-trigger another
464 // LauncherModel load.
465 mPaused = false;
466
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800467 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200468 android.os.Debug.startMethodTracing(
469 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470 }
471
472 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700474
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700476 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800477
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800478 registerContentObservers();
479
Joe Onorato7c312c12009-08-13 21:36:53 -0700480 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700481
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 mSavedState = savedInstanceState;
483 restoreState(mSavedState);
484
485 if (PROFILE_STARTUP) {
486 android.os.Debug.stopMethodTracing();
487 }
488
489 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700490 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700491 // If the user leaves launcher, then we should just load items asynchronously when
492 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700493 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700494 } else {
495 // We only load the page synchronously if the user rotates (or triggers a
496 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700497 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700498 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800499 }
500
501 // For handling default keys
502 mDefaultKeySsb = new SpannableStringBuilder();
503 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800504
505 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
506 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800507
Adam Cohenf9426d52012-06-04 17:26:21 -0700508 // On large interfaces, we want the screen to auto-rotate based on the current orientation
509 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700510
Adam Cohen9211d422014-10-07 18:14:53 -0700511 if (mLauncherCallbacks != null) {
512 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700513 if (mLauncherCallbacks.hasLauncherOverlay()) {
514 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700515 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
516 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
517 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700518 mWorkspace.setLauncherOverlay(mLauncherOverlay);
519 }
Adam Cohen9211d422014-10-07 18:14:53 -0700520 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700521
522 if (shouldShowIntroScreen()) {
523 showIntroScreen();
524 } else {
525 showFirstRunActivity();
526 showFirstRunClings();
527 }
Adam Cohen9211d422014-10-07 18:14:53 -0700528 }
529
530 private LauncherCallbacks mLauncherCallbacks;
531
532 public void onPostCreate(Bundle savedInstanceState) {
533 super.onPostCreate(savedInstanceState);
534 if (mLauncherCallbacks != null) {
535 mLauncherCallbacks.onPostCreate(savedInstanceState);
536 }
537 }
538
539 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
540 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700541 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
542 @Override
543 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700544 if (LOGD) {
545 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
546 }
Winson Chung94804152015-05-08 13:06:44 -0700547 mAppsView.setFixedBounds(bounds);
548 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700549 }
550
551 @Override
552 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700553 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
554 // Dismiss All Apps if we aren't already paused/invisible
555 if (!mPaused) {
556 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
557 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
558 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700559 }
Winson Chung0f785722015-04-08 10:27:49 -0700560 }
561 });
Jun Mukai8af0cd82015-05-12 12:32:12 -0700562 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
563 private boolean mImportanceStored = false;
564 private int mWorkspaceImportanceForAccessibility =
565 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
566 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
567
568 @Override
569 public void onSearchOverlayOpened() {
570 if (mImportanceStored) {
571 return;
572 }
573 // The underlying workspace and hotseat are temporarily suppressed by the search
574 // overlay. So they sholudn't be accessible.
575 mWorkspaceImportanceForAccessibility = mWorkspace.getImportantForAccessibility();
576 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
577 mWorkspace.setImportantForAccessibility(
578 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
579 mHotseat.setImportantForAccessibility(
580 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
581 mImportanceStored = true;
582 }
583
584 @Override
585 public void onSearchOverlayClosed() {
586 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
587 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
588 mImportanceStored = false;
589 }
590 });
Adam Cohen9211d422014-10-07 18:14:53 -0700591 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700592 }
593
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700594 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700595 public void onLauncherProviderChange() {
596 if (mLauncherCallbacks != null) {
597 mLauncherCallbacks.onLauncherProviderChange();
598 }
599 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700600
Adam Cohen9211d422014-10-07 18:14:53 -0700601 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700602 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700603 if (mLauncherCallbacks != null) {
604 return mLauncherCallbacks.hasCustomContentToLeft();
605 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700606 return false;
607 }
608
Dave Hawkeya8881582013-09-17 15:55:33 -0600609 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500610 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600611 * {@link #addToCustomContentPage}. This will only be invoked if
612 * {@link #hasCustomContentToLeft()} is {@code true}.
613 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500614 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700615 if (mLauncherCallbacks != null) {
616 mLauncherCallbacks.populateCustomContentContainer();
617 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600618 }
619
620 /**
621 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
622 * ensure the custom content page is added or removed if necessary.
623 */
624 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600625 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600626 // Not bound yet, wait for bindScreens to be called.
627 return;
628 }
629
630 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
631 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500632 mWorkspace.createCustomContentContainer();
633 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600634 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
635 mWorkspace.removeCustomContentPage();
636 }
637 }
638
Adam Cohen091440a2015-03-18 14:16:05 -0700639 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700640 if (sLocaleConfiguration == null) {
641 new AsyncTask<Void, Void, LocaleConfiguration>() {
642 @Override
643 protected LocaleConfiguration doInBackground(Void... unused) {
644 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
645 readConfiguration(Launcher.this, localeConfiguration);
646 return localeConfiguration;
647 }
648
649 @Override
650 protected void onPostExecute(LocaleConfiguration result) {
651 sLocaleConfiguration = result;
652 checkForLocaleChange(); // recursive, but now with a locale configuration
653 }
654 }.execute();
655 return;
656 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700657
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800658 final Configuration configuration = getResources().getConfiguration();
659
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700660 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800661 final String locale = configuration.locale.toString();
662
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700663 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800664 final int mcc = configuration.mcc;
665
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700666 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800667 final int mnc = configuration.mnc;
668
Romain Guy84f296c2009-11-04 15:00:44 -0800669 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800670
Romain Guy84f296c2009-11-04 15:00:44 -0800671 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700672 sLocaleConfiguration.locale = locale;
673 sLocaleConfiguration.mcc = mcc;
674 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700675
Joe Onorato0589f0f2010-02-08 13:44:00 -0800676 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700677
678 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100679 new AsyncTask<Void, Void, Void>() {
680 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700681 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100682 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700683 }
Michael Jurka43467462013-11-14 12:03:58 +0100684 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700685 }
686 }
687
Adam Cohen091440a2015-03-18 14:16:05 -0700688 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700689 public String locale;
690 public int mcc = -1;
691 public int mnc = -1;
692 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700693
Adam Cohen091440a2015-03-18 14:16:05 -0700694 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700695 DataInputStream in = null;
696 try {
Helena Josol28db2802014-10-09 17:04:09 +0100697 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700698 configuration.locale = in.readUTF();
699 configuration.mcc = in.readInt();
700 configuration.mnc = in.readInt();
701 } catch (FileNotFoundException e) {
702 // Ignore
703 } catch (IOException e) {
704 // Ignore
705 } finally {
706 if (in != null) {
707 try {
708 in.close();
709 } catch (IOException e) {
710 // Ignore
711 }
712 }
713 }
714 }
715
Adam Cohen091440a2015-03-18 14:16:05 -0700716 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700717 DataOutputStream out = null;
718 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100719 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100720 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700721 out.writeUTF(configuration.locale);
722 out.writeInt(configuration.mcc);
723 out.writeInt(configuration.mnc);
724 out.flush();
725 } catch (FileNotFoundException e) {
726 // Ignore
727 } catch (IOException e) {
728 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100729 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700730 } finally {
731 if (out != null) {
732 try {
733 out.close();
734 } catch (IOException e) {
735 // Ignore
736 }
737 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800738 }
739 }
740
Anton Hanssona2f665f2013-09-26 12:18:32 +0100741 public Stats getStats() {
742 return mStats;
743 }
744
Bjorn Bringertc459e522013-06-07 19:36:01 +0100745 public LayoutInflater getInflater() {
746 return mInflater;
747 }
748
Hyunyoung Song3f471442015-04-08 19:01:34 -0700749 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700750 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
751 // that is subsequently removed from the workspace in startBinding().
752 return !mModel.isLoadingWorkspace();
753 }
754
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800755 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000756 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100757 if (Build.VERSION.SDK_INT >= 17) {
758 return View.generateViewId();
759 } else {
760 // View.generateViewId() is not available. The following fallback logic is a copy
761 // of its implementation.
762 for (;;) {
763 final int result = sNextGeneratedId.get();
764 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
765 int newValue = result + 1;
766 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
767 if (sNextGeneratedId.compareAndSet(result, newValue)) {
768 return result;
769 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000770 }
771 }
772 }
773
774 public int getViewIdForItem(ItemInfo info) {
775 // This cast is safe given the > 2B range for int.
776 int itemId = (int) info.id;
777 if (mItemIdToViewId.containsKey(itemId)) {
778 return mItemIdToViewId.get(itemId);
779 }
780 int viewId = generateViewId();
781 mItemIdToViewId.put(itemId, viewId);
782 return viewId;
783 }
784
785 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700786 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
787 * a configuration step, this allows the proper animations to run after other transitions.
788 */
Adam Cohendb364c32014-05-20 14:23:40 -0700789 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700790 long screenId = args.screenId;
791 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
792 // When the screen id represents an actual screen (as opposed to a rank) we make sure
793 // that the drop page actually exists.
794 screenId = ensurePendingDropLayoutExists(args.screenId);
795 }
Adam Cohendb364c32014-05-20 14:23:40 -0700796
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800797 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800798 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700799 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700800 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700801 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800802 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700803 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700804 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700805 case REQUEST_RECONFIGURE_APPWIDGET:
806 completeRestoreAppWidget(args.appWidgetId);
807 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800808 }
Michael Jurka27614d22012-04-02 06:40:22 -0700809 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
810 // if you turned the screen off and then back while in All Apps, Launcher would not
811 // return to the workspace. Clearing mAddInfo.container here fixes this issue
812 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700813 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800814 }
815
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800816 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700817 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700818 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700819 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700820 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800821 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700822
Adam Cohenad4e15c2013-10-17 16:21:35 -0700823 Runnable exitSpringLoaded = new Runnable() {
824 @Override
825 public void run() {
826 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
827 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
828 }
829 };
830
Michael Jurka8b805b12012-04-18 14:23:14 -0700831 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700832 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700833 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
834 if (resultCode == RESULT_CANCELED) {
835 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700836 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700837 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700838 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800839 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700840 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700841 }
842 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200843 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
844 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700845 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200846 }
847 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700848 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200849
Adam Cohened66b2b2012-01-23 17:28:51 -0800850 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700851 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700852
Adam Cohendb364c32014-05-20 14:23:40 -0700853 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800854 // We have special handling for widgets
855 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800856 final int appWidgetId;
857 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
858 : -1;
859 if (widgetId < 0) {
860 appWidgetId = pendingAddWidgetId;
861 } else {
862 appWidgetId = widgetId;
863 }
864
Adam Cohenad4e15c2013-10-17 16:21:35 -0700865 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800866 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700867 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
868 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700869 result = RESULT_CANCELED;
870 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700871 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700872 @Override
873 public void run() {
874 exitSpringLoadedDragModeDelayed(false, 0, null);
875 }
876 };
Adam Cohendb364c32014-05-20 14:23:40 -0700877 if (workspaceLocked) {
878 // No need to remove the empty screen if we're mid-binding, as the
879 // the bind will not add the empty screen.
880 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
881 } else {
882 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
883 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
884 }
Winson Chung5aaab772012-04-27 15:24:02 -0700885 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700886 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700887 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
888 // When the screen id represents an actual screen (as opposed to a rank)
889 // we make sure that the drop page actually exists.
890 mPendingAddInfo.screenId =
891 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
892 }
Adam Cohendb364c32014-05-20 14:23:40 -0700893 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
894
895 dropLayout.setDropPending(true);
896 final Runnable onComplete = new Runnable() {
897 @Override
898 public void run() {
899 completeTwoStageWidgetDrop(resultCode, appWidgetId);
900 dropLayout.setDropPending(false);
901 }
902 };
903 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
904 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
905 } else {
906 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
907 mPendingAddInfo);
908 sPendingAddItem = args;
909 }
Winson Chung5aaab772012-04-27 15:24:02 -0700910 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800911 return;
912 }
913
Sunny Goyalff572272014-07-23 13:58:07 -0700914 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
915 if (resultCode == RESULT_OK) {
916 // Update the widget view.
917 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
918 pendingAddWidgetId, mPendingAddInfo);
919 if (workspaceLocked) {
920 sPendingAddItem = args;
921 } else {
922 completeAdd(args);
923 }
924 }
925 // Leave the widget in the pending state if the user canceled the configure.
926 return;
927 }
928
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800929 // The pattern used here is that a user PICKs a specific application,
930 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700931
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800932 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
933 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700934 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700935 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
936 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800937 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700938 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800939 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700940 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700941 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
942 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800943 }
Adam Cohen00074722013-10-11 17:46:09 -0700944 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700945 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700946 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800947 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800948 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800949
950 }
951
952 @Override
953 protected void onActivityResult(
954 final int requestCode, final int resultCode, final Intent data) {
955 handleActivityResult(requestCode, resultCode, data);
956 if (mLauncherCallbacks != null) {
957 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
958 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800959 }
960
Adam Cohendb364c32014-05-20 14:23:40 -0700961 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
962 appWidgetId, ItemInfo info) {
963 PendingAddArguments args = new PendingAddArguments();
964 args.requestCode = requestCode;
965 args.intent = data;
966 args.container = info.container;
967 args.screenId = info.screenId;
968 args.cellX = info.cellX;
969 args.cellY = info.cellY;
970 args.appWidgetId = appWidgetId;
971 return args;
972 }
973
974 /**
975 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
976 *
977 * @param screenId the screen id to check
978 * @return the new screen, or screenId if it exists
979 */
980 private long ensurePendingDropLayoutExists(long screenId) {
981 CellLayout dropLayout =
982 (CellLayout) mWorkspace.getScreenWithId(screenId);
983 if (dropLayout == null) {
984 // it's possible that the add screen was removed because it was
985 // empty and a re-bind occurred
986 mWorkspace.addExtraEmptyScreen();
987 return mWorkspace.commitExtraEmptyScreen();
988 } else {
989 return screenId;
990 }
991 }
992
Adam Cohen091440a2015-03-18 14:16:05 -0700993 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700994 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700995 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800996 Runnable onCompleteRunnable = null;
997 int animationType = 0;
998
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700999 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001000 if (resultCode == RESULT_OK) {
1001 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
1002 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001003 mPendingAddWidgetInfo);
1004 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -08001005 onCompleteRunnable = new Runnable() {
1006 @Override
1007 public void run() {
1008 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001009 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001010 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
1011 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -08001012 }
1013 };
1014 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -08001015 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001016 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -08001017 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001018 if (mDragLayer.getAnimatedView() != null) {
1019 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
1020 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
1021 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001022 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001023 // The animated view may be null in the case of a rotation during widget configuration
1024 onCompleteRunnable.run();
1025 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001026 }
1027
1028 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -07001029 protected void onStop() {
1030 super.onStop();
1031 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -07001032
1033 if (mLauncherCallbacks != null) {
1034 mLauncherCallbacks.onStop();
1035 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001036 }
1037
1038 @Override
1039 protected void onStart() {
1040 super.onStart();
1041 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -07001042
1043 if (mLauncherCallbacks != null) {
1044 mLauncherCallbacks.onStart();
1045 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001046 }
1047
1048 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001049 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001050 long startTime = 0;
1051 if (DEBUG_RESUME_TIME) {
1052 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001053 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001054 }
Adam Cohen9211d422014-10-07 18:14:53 -07001055
1056 if (mLauncherCallbacks != null) {
1057 mLauncherCallbacks.preOnResume();
1058 }
1059
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001060 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001061
Winson Chung4a2afa32012-07-19 14:53:05 -07001062 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001063 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001064 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001065 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001066 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001067 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001068 // view after launching an app, as they may be depending on the UI to be static to
1069 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001070 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -07001071 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08001072 } else if (mOnResumeState == State.WIDGETS) {
1073 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001074 }
1075 mOnResumeState = State.NONE;
1076
Winson Chungcd99cd32015-04-29 11:03:24 -07001077 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001078 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1079 // Otherwise, notify the callbacks if we are in all apps mode
1080 if (mState == State.APPS) {
1081 if (mLauncherCallbacks != null) {
1082 mLauncherCallbacks.onAllAppsShown();
1083 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001084 }
1085 }
1086
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001087 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001088 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1089 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001090
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001091 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001092 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001093 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001094 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001095 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001096 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001097 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001098 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001099 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1100 // execute them here
1101 long startTimeCallbacks = 0;
1102 if (DEBUG_RESUME_TIME) {
1103 startTimeCallbacks = System.currentTimeMillis();
1104 }
1105
Michael Jurka1e2f4652013-07-08 18:03:46 -07001106 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1107 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001108 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001109 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001110 if (DEBUG_RESUME_TIME) {
1111 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1112 (System.currentTimeMillis() - startTimeCallbacks));
1113 }
Michael Jurka447bf842013-05-15 14:52:15 +02001114 }
Michael Jurka54554252013-08-01 12:52:23 +02001115 if (mOnResumeCallbacks.size() > 0) {
1116 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1117 mOnResumeCallbacks.get(i).run();
1118 }
1119 mOnResumeCallbacks.clear();
1120 }
Winson Chunge4e50662012-01-23 14:45:13 -08001121
1122 // Reset the pressed state of icons that were locked in the press state while activities
1123 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001124 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001125 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001126 mWaitingForResume.setStayPressed(false);
1127 }
Winson Chung82963d52013-10-09 11:20:57 -07001128
Adam Cohen06dff352012-06-01 17:17:08 -07001129 // It is possible that widgets can receive updates while launcher is not in the foreground.
1130 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1131 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1132 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001133 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001134 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001135
Michael Jurka447bf842013-05-15 14:52:15 +02001136 if (DEBUG_RESUME_TIME) {
1137 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1138 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001139
1140 if (mWorkspace.getCustomContentCallbacks() != null) {
1141 // If we are resuming and the custom content is the current page, we call onShow().
1142 // It is also poassible that onShow will instead be called slightly after first layout
1143 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1144 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001145 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001146 }
1147 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001148 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001149 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001150
Sunny Goyal756adbc2015-04-16 15:20:51 -07001151 if (!isWorkspaceLoading()) {
1152 // Process any items that were added while Launcher was away.
1153 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1154 }
Adam Cohen9211d422014-10-07 18:14:53 -07001155
1156 if (mLauncherCallbacks != null) {
1157 mLauncherCallbacks.onResume();
1158 }
Adam Cohen06dff352012-06-01 17:17:08 -07001159 }
1160
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001161 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001162 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001163 // Ensure that items added to Launcher are queued until Launcher returns
1164 InstallShortcutReceiver.enableInstallQueue();
1165
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001166 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001167 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001168 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001169 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001170
1171 // We call onHide() aggressively. The custom content callbacks should be able to
1172 // debounce excess onHide calls.
1173 if (mWorkspace.getCustomContentCallbacks() != null) {
1174 mWorkspace.getCustomContentCallbacks().onHide();
1175 }
Romain Guycbb89e42009-06-08 15:52:54 -07001176
Adam Cohen9211d422014-10-07 18:14:53 -07001177 if (mLauncherCallbacks != null) {
1178 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001179 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001180 }
1181
1182 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001183 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1184 // by a onResume or by scrolling otherwise.
1185 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001186
1187 // Custom content is completely hidden
1188 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001189
1190 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1191 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001192
1193 // Indicates whether the user is allowed to scroll away from the custom content.
1194 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001195 }
1196
Adam Cohenc2d6e892014-10-16 09:49:24 -07001197 public interface LauncherOverlay {
1198
1199 /**
1200 * Touch interaction leading to overscroll has begun
1201 */
1202 public void onScrollInteractionBegin();
1203
1204 /**
1205 * Touch interaction related to overscroll has ended
1206 */
1207 public void onScrollInteractionEnd();
1208
1209 /**
1210 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1211 * screen (or in the case of RTL, the rightmost screen).
1212 */
1213 public void onScrollChange(int progress, boolean rtl);
1214
1215 /**
1216 * Screen has stopped scrolling
1217 */
1218 public void onScrollSettled();
1219
1220 /**
1221 * This method can be called by the Launcher in order to force the LauncherOverlay
1222 * to exit fully immersive mode.
1223 */
1224 public void forceExitFullImmersion();
1225 }
1226
Winson Chung0f785722015-04-08 10:27:49 -07001227 public interface LauncherAppsCallbacks {
1228 /**
1229 * Updates launcher to the available space that AllApps can take so as not to overlap with
1230 * any other views.
1231 */
1232 public void onAllAppsBoundsChanged(Rect bounds);
1233
1234 /**
1235 * Called to dismiss all apps if it is showing.
1236 */
1237 public void dismissAllApps();
1238 }
1239
Jun Mukai8af0cd82015-05-12 12:32:12 -07001240 public interface LauncherSearchCallbacks {
1241 /**
1242 * Called when the search overlay is shown.
1243 */
1244 public void onSearchOverlayOpened();
1245
1246 /**
1247 * Called when the search overlay is dismissed.
1248 */
1249 public void onSearchOverlayClosed();
1250 }
1251
Adam Cohenc2d6e892014-10-16 09:49:24 -07001252 public interface LauncherOverlayCallbacks {
1253 /**
1254 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1255 * however it doesn't modify any state within the launcher.
1256 */
1257 public boolean canEnterFullImmersion();
1258
1259 /**
1260 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1261 * eg. by occupying the full screen and handling all touch events.
1262 *
1263 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1264 * case, Launcher will modify any necessary state and assumes the overlay is
1265 * handling all interaction. If false, the LauncherOverlay should cancel any
1266 *
1267 */
1268 public boolean enterFullImmersion();
1269
1270 /**
1271 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1272 * full control over UI and state.
1273 */
1274 public void exitFullImmersion();
1275 }
1276
1277 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1278
1279 @Override
1280 public boolean canEnterFullImmersion() {
1281 return mState == State.WORKSPACE;
1282 }
1283
1284 @Override
1285 public boolean enterFullImmersion() {
1286 if (mState == State.WORKSPACE) {
1287 // When fully immersed, disregard any touches which fall through.
1288 mDragLayer.setBlockTouch(true);
1289 return true;
1290 }
1291 return false;
1292 }
1293
1294 @Override
1295 public void exitFullImmersion() {
1296 mDragLayer.setBlockTouch(false);
1297 }
1298 }
1299
Jorim Jaggid017f882014-01-14 17:08:48 -08001300 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001301 if (mLauncherCallbacks != null) {
1302 return mLauncherCallbacks.hasSettings();
1303 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001304 return false;
1305 }
1306
Adam Cohen9211d422014-10-07 18:14:53 -07001307 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001308 CustomContentCallbacks callbacks, String description) {
1309 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001310 }
1311
Adam Cohenedb40762013-07-18 16:45:45 -07001312 // The custom content needs to offset its content to account for the QSB
1313 public int getTopOffsetForCustomContent() {
1314 return mWorkspace.getPaddingTop();
1315 }
1316
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001317 @Override
1318 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001319 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001320 if (mModel.isCurrentCallbacks(this)) {
1321 mModel.stopLoader();
1322 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001323 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1324
Romain Guy13c2e7b2010-03-10 19:45:00 -08001325 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001326 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001327
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001328 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001329 @Override
1330 public void onWindowFocusChanged(boolean hasFocus) {
1331 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001332 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001333
1334 if (mLauncherCallbacks != null) {
1335 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1336 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001337 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001338
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001339 private boolean acceptFilter() {
1340 final InputMethodManager inputManager = (InputMethodManager)
1341 getSystemService(Context.INPUT_METHOD_SERVICE);
1342 return !inputManager.isFullscreenMode();
1343 }
1344
1345 @Override
1346 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001347 final int uniChar = event.getUnicodeChar();
1348 final boolean handled = super.onKeyDown(keyCode, event);
1349 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1350 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001351 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1352 keyCode, event);
1353 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001354 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001355 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001356 // showSearchDialog()
1357 // If there are multiple keystrokes before the search dialog takes focus,
1358 // onSearchRequested() will be called for every keystroke,
1359 // but it is idempotent, so it's fine.
1360 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001361 }
1362 }
1363
Joe Onorato8a9625e2010-01-28 15:55:35 -08001364 // Eat the long press event so the keyboard doesn't come up.
1365 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1366 return true;
1367 }
1368
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001369 return handled;
1370 }
1371
Karl Rosaen138a0412009-04-23 19:00:21 -07001372 private String getTypedText() {
1373 return mDefaultKeySsb.toString();
1374 }
1375
1376 private void clearTypedText() {
1377 mDefaultKeySsb.clear();
1378 mDefaultKeySsb.clearSpans();
1379 Selection.setSelection(mDefaultKeySsb, 0);
1380 }
1381
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001382 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001383 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1384 * State
1385 */
1386 private static State intToState(int stateOrdinal) {
1387 State state = State.WORKSPACE;
1388 final State[] stateValues = State.values();
1389 for (int i = 0; i < stateValues.length; i++) {
1390 if (stateValues[i].ordinal() == stateOrdinal) {
1391 state = stateValues[i];
1392 break;
1393 }
1394 }
1395 return state;
1396 }
1397
1398 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001399 * Restores the previous state, if it exists.
1400 *
1401 * @param savedState The previous state.
1402 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001403 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001404 private void restoreState(Bundle savedState) {
1405 if (savedState == null) {
1406 return;
1407 }
1408
Michael Jurka883f55b2010-10-21 15:47:14 -07001409 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001410 if (state == State.APPS || state == State.WIDGETS) {
1411 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001412 }
1413
Adam Cohen21cd0022013-10-09 18:57:02 -07001414 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1415 PagedView.INVALID_RESTORE_PAGE);
1416 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001417 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
1419
Winson Chung3d503fb2011-07-13 17:25:49 -07001420 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001421 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001422
Winson Chung3d503fb2011-07-13 17:25:49 -07001423 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1424 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001425 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001426 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1427 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001428 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1429 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001430 AppWidgetProviderInfo info = savedState.getParcelable(
1431 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001432 mPendingAddWidgetInfo = info == null ?
1433 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1434
Adam Cohen4637b5a2013-11-04 18:21:24 -08001435 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001436 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001437 mRestoring = true;
1438 }
1439
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001440 mItemIdToViewId = (HashMap<Integer, Integer>)
1441 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001442 }
1443
1444 /**
1445 * Finds all the views we need and configure them properly.
1446 */
1447 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001448 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001449
Craig Mautner360310b2012-10-26 15:13:08 -07001450 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001451 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001452 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1453 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001454 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001455 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001456
John Spurlock77e1f472013-09-11 10:09:51 -04001457 mLauncherView.setSystemUiVisibility(
1458 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001459 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001460
Winson Chung4c98d922011-05-31 16:50:48 -07001461 // Setup the drag layer
1462 mDragLayer.setup(this, dragController);
1463
Winson Chung3d503fb2011-07-13 17:25:49 -07001464 // Setup the hotseat
1465 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1466 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001467 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001468 }
1469
Jorim Jaggid017f882014-01-14 17:08:48 -08001470 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001471 View widgetButton = findViewById(R.id.widget_button);
1472 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001473 @Override
1474 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001475 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001476 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001477 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001478 }
1479 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001480 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1481
1482 View wallpaperButton = findViewById(R.id.wallpaper_button);
1483 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001484 @Override
1485 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001486 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001487 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001488 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001489 }
1490 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001491 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1492
1493 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001494 if (hasSettings()) {
1495 settingsButton.setOnClickListener(new OnClickListener() {
1496 @Override
1497 public void onClick(View arg0) {
1498 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001499 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001500 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001501 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001502 });
1503 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1504 } else {
1505 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001506 }
1507
Adam Cohendbdff6b2013-09-18 19:09:15 -07001508 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001509
Winson Chung4c98d922011-05-31 16:50:48 -07001510 // Setup the workspace
1511 mWorkspace.setHapticFeedbackEnabled(false);
1512 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001513 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001514 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001515
Winson Chungf0ea4d32011-06-06 14:27:16 -07001516 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001517 mSearchDropTargetBar = (SearchDropTargetBar)
1518 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001519
Winson Chungb745afb2015-03-02 11:51:23 -08001520 // Setup Apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001521 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001522 if (isAllAppsSearchOverridden()) {
1523 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001524 }
Winson Chungb745afb2015-03-02 11:51:23 -08001525
Winson Chungf0ea4d32011-06-06 14:27:16 -07001526 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001527 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001528
Winson Chung3d503fb2011-07-13 17:25:49 -07001529 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001530 dragController.setDragScoller(mWorkspace);
1531 dragController.setScrollView(mDragLayer);
1532 dragController.setMoveTarget(mWorkspace);
1533 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001534 if (mSearchDropTargetBar != null) {
1535 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001536 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001537 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001538
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001539 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001540 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001541 mWeightWatcher = new WeightWatcher(this);
1542 mWeightWatcher.setAlpha(0.5f);
1543 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001544 new FrameLayout.LayoutParams(
1545 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001546 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001547 Gravity.BOTTOM)
1548 );
Adam Cohen39a06042013-07-19 14:30:12 -07001549
1550 boolean show = shouldShowWeightWatcher();
1551 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001552 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 }
1554
1555 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001556 * Sets the all apps button. This method is called from {@link Hotseat}.
1557 */
1558 public void setAllAppsButton(View allAppsButton) {
1559 mAllAppsButton = allAppsButton;
1560 }
1561
1562 public View getAllAppsButton() {
1563 return mAllAppsButton;
1564 }
1565
1566 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001567 * Creates a view representing a shortcut.
1568 *
1569 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001570 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001571 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001572 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001573 }
1574
1575 /**
1576 * Creates a view representing a shortcut inflated from the specified resource.
1577 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578 * @param parent The group the shortcut belongs to.
1579 * @param info The data structure describing the shortcut.
1580 *
1581 * @return A View inflated from layoutResId.
1582 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001583 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001584 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001585 parent, false);
1586 favorite.applyFromShortcutInfo(info, mIconCache);
1587 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001589 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001590 return favorite;
1591 }
1592
1593 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001594 * Add a shortcut to the workspace.
1595 *
1596 * @param data The intent describing the shortcut.
1597 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001598 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001599 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001600 int cellY) {
1601 int[] cellXY = mTmpAddItemCellCoordinates;
1602 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001603 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001604
Sunny Goyal5c97f512015-05-19 16:03:28 -07001605 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001606 if (info == null) {
1607 return;
1608 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001609 final View view = createShortcut(info);
1610
Sunny Goyal5c97f512015-05-19 16:03:28 -07001611 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001612 // First we check if we already know the exact location where we want to add this item.
1613 if (cellX >= 0 && cellY >= 0) {
1614 cellXY[0] = cellX;
1615 cellXY[1] = cellY;
1616 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001617
1618 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001619 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001620 true, null,null)) {
1621 return;
1622 }
1623 DragObject dragObject = new DragObject();
1624 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001625 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1626 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001627 return;
1628 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001629 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001630 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001631 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001632 foundCellSpan = (result != null);
1633 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001634 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001635 }
1636
1637 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001638 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001639 return;
1640 }
1641
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001642 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001643
1644 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001645 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001646 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001647 }
1648 }
1649
Adam Cohen2e6da152015-05-06 11:42:25 -07001650 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1651 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001652 // We want to account for the extra amount of padding that we are adding to the widget
1653 // to ensure that it gets the full amount of space that it has requested
1654 int requiredWidth = minWidth + padding.left + padding.right;
1655 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2e6da152015-05-06 11:42:25 -07001656 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001657 }
1658
Adam Cohen2e6da152015-05-06 11:42:25 -07001659 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1660 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001661 }
1662
Adam Cohen2e6da152015-05-06 11:42:25 -07001663 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1664 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001665 }
1666
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001667 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001668 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001670 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 */
Adam Cohen091440a2015-03-18 14:16:05 -07001672 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001673 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1674
1675 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001676 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001677 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1678 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001679 }
Romain Guycbb89e42009-06-08 15:52:54 -07001680
Adam Cohen59400422014-03-05 18:07:04 -08001681 if (appWidgetInfo.isCustomWidget) {
1682 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001683 }
1684
Adam Cohen59400422014-03-05 18:07:04 -08001685 LauncherAppWidgetInfo launcherInfo;
1686 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1687 launcherInfo.spanX = info.spanX;
1688 launcherInfo.spanY = info.spanY;
1689 launcherInfo.minSpanX = info.minSpanX;
1690 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001691 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001692
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001693 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001694 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001695
1696 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001697 if (hostView == null) {
1698 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001699 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1700 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001701 } else {
1702 // The AppWidgetHostView has already been inflated and instantiated
1703 launcherInfo.hostView = hostView;
1704 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001706 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001707 launcherInfo.notifyWidgetSizeChanged(this);
1708
Adam Cohen59400422014-03-05 18:07:04 -08001709 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1710 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001711
1712 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001713 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001714 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001715 }
Romain Guycbb89e42009-06-08 15:52:54 -07001716
Adam Cohended9f8d2010-11-03 13:25:16 -07001717 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1718 @Override
1719 public void onReceive(Context context, Intent intent) {
1720 final String action = intent.getAction();
1721 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1722 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001723 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001724 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001725
Winson Chungc100e8e2011-08-09 16:02:43 -07001726 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001727 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001728 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001729 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001730 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001731 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001732 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1733 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001734 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001735 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1736 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001737 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001738 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1739 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1740 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001741 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001742 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1743 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001744 }
1745 }
1746 };
1747
1748 @Override
1749 public void onAttachedToWindow() {
1750 super.onAttachedToWindow();
1751
1752 // Listen for broadcasts related to user-presence
1753 final IntentFilter filter = new IntentFilter();
1754 filter.addAction(Intent.ACTION_SCREEN_OFF);
1755 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001756 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001757 if (ENABLE_DEBUG_INTENTS) {
1758 filter.addAction(DebugIntents.DELETE_DATABASE);
1759 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1760 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001761 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001762 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001763 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001764 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001765 mVisible = true;
1766 }
1767
Adam Cohen0b395352014-06-09 22:54:36 +00001768 /**
1769 * Sets up transparent navigation and status bars in LMP.
1770 * This method is a no-op for other platform versions.
1771 */
Sunny Goyald0091012015-01-20 15:55:34 -08001772 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001773 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001774 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001775 Window window = getWindow();
1776 window.getAttributes().systemUiVisibility |=
1777 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1778 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1779 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1780 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1781 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1782 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1783 window.setStatusBarColor(Color.TRANSPARENT);
1784 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001785 }
1786 }
1787
Adam Cohended9f8d2010-11-03 13:25:16 -07001788 @Override
1789 public void onDetachedFromWindow() {
1790 super.onDetachedFromWindow();
1791 mVisible = false;
1792
Adam Cohend113e0c2010-11-11 10:48:05 -08001793 if (mAttached) {
1794 unregisterReceiver(mReceiver);
1795 mAttached = false;
1796 }
Winson Chungb745afb2015-03-02 11:51:23 -08001797 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001798 }
1799
1800 public void onWindowVisibilityChanged(int visibility) {
1801 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001802 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001803 // The following code used to be in onResume, but it turns out onResume is called when
1804 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1805 // is a more appropriate event to handle
1806 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001807 if (!mWorkspaceLoading) {
1808 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001809 // We want to let Launcher draw itself at least once before we force it to build
1810 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001811 // apps is nice and speedy.
1812 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001813 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001814 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001815 if (mStarted) return;
1816 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001817 // We delay the layer building a bit in order to give
1818 // other message processing a time to run. In particular
1819 // this avoids a delay in hiding the IME if it was
1820 // currently shown, because doing that may involve
1821 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001822 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001823 final ViewTreeObserver.OnDrawListener listener = this;
1824 mWorkspace.post(new Runnable() {
1825 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001826 if (mWorkspace != null &&
1827 mWorkspace.getViewTreeObserver() != null) {
1828 mWorkspace.getViewTreeObserver().
1829 removeOnDrawListener(listener);
1830 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001831 }
1832 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001833 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001834 }
1835 });
1836 }
1837 clearTypedText();
1838 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001839 }
1840
Adam Cohen091440a2015-03-18 14:16:05 -07001841 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001842 mHandler.removeMessages(ADVANCE_MSG);
1843 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1844 mHandler.sendMessageDelayed(msg, delay);
1845 mAutoAdvanceSentTime = System.currentTimeMillis();
1846 }
1847
Adam Cohen091440a2015-03-18 14:16:05 -07001848 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001849 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1850 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1851 mAutoAdvanceRunning = autoAdvanceRunning;
1852 if (autoAdvanceRunning) {
1853 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1854 sendAdvanceMessage(delay);
1855 } else {
1856 if (!mWidgetsToAdvance.isEmpty()) {
1857 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1858 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1859 }
1860 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001861 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001862 }
1863 }
1864 }
1865
1866 private final Handler mHandler = new Handler() {
1867 @Override
1868 public void handleMessage(Message msg) {
1869 if (msg.what == ADVANCE_MSG) {
1870 int i = 0;
1871 for (View key: mWidgetsToAdvance.keySet()) {
1872 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1873 final int delay = mAdvanceStagger * i;
1874 if (v instanceof Advanceable) {
1875 postDelayed(new Runnable() {
1876 public void run() {
1877 ((Advanceable) v).advance();
1878 }
1879 }, delay);
1880 }
1881 i++;
1882 }
1883 sendAdvanceMessage(mAdvanceInterval);
1884 }
1885 }
1886 };
1887
1888 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001889 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001890 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1891 if (v instanceof Advanceable) {
1892 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001893 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001894 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001895 }
1896 }
1897
1898 void removeWidgetToAutoAdvance(View hostView) {
1899 if (mWidgetsToAdvance.containsKey(hostView)) {
1900 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001901 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001902 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001903 }
1904
Joe Onorato9c1289c2009-08-17 11:03:03 -04001905 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001906 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001907 launcherInfo.hostView = null;
1908 }
1909
Hyunyoung Song3f471442015-04-08 19:01:34 -07001910 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001911 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1912 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001913 }
1914
Winson Chunga6945242014-01-08 14:04:34 -08001915 public DragLayer getDragLayer() {
1916 return mDragLayer;
1917 }
1918
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001919 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001920 return mAppsView;
1921 }
1922
Hyunyoung Song3f471442015-04-08 19:01:34 -07001923 public WidgetsContainerView getWidgetsView() {
1924 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001925 }
1926
Winson Chunga6945242014-01-08 14:04:34 -08001927 public Workspace getWorkspace() {
1928 return mWorkspace;
1929 }
1930
1931 public Hotseat getHotseat() {
1932 return mHotseat;
1933 }
1934
Jorim Jaggid017f882014-01-14 17:08:48 -08001935 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001936 return mOverviewPanel;
1937 }
1938
1939 public SearchDropTargetBar getSearchBar() {
1940 return mSearchDropTargetBar;
1941 }
1942
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001943 public LauncherAppWidgetHost getAppWidgetHost() {
1944 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001945 }
Romain Guycbb89e42009-06-08 15:52:54 -07001946
Winson Chunga9abd0e2010-10-27 17:18:37 -07001947 public LauncherModel getModel() {
1948 return mModel;
1949 }
1950
Winson Chunga6945242014-01-08 14:04:34 -08001951 protected SharedPreferences getSharedPrefs() {
1952 return mSharedPrefs;
1953 }
1954
Adam Cohen2e6da152015-05-06 11:42:25 -07001955 public DeviceProfile getDeviceProfile() {
1956 return mDeviceProfile;
1957 }
1958
Bjorn Bringertc459e522013-06-07 19:36:01 +01001959 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001960 getWindow().closeAllPanels();
1961
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001962 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001963 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001964 }
1965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 @Override
1967 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001968 long startTime = 0;
1969 if (DEBUG_RESUME_TIME) {
1970 startTime = System.currentTimeMillis();
1971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 super.onNewIntent(intent);
1973
1974 // Close the menu
1975 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001976 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001977 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001978
Adam Cohened307df2013-10-02 09:37:31 -07001979 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1980 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1981 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001982
Adam Cohen6fecd412013-10-02 17:41:50 -07001983 if (mWorkspace == null) {
1984 // Can be cases where mWorkspace is null, this prevents a NPE
1985 return;
1986 }
1987 Folder openFolder = mWorkspace.getOpenFolder();
1988 // In all these cases, only animate if we're already on home
1989 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001990
1991 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1992 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001993 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001994 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001995 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001996 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001997
Adam Cohen6fecd412013-10-02 17:41:50 -07001998 closeFolder();
1999 exitSpringLoadedDragMode();
2000
2001 // If we are already on home, then just animate back to the workspace,
2002 // otherwise, just wait until onResume to set the state back to Workspace
2003 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07002004 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07002005 } else {
2006 mOnResumeState = State.WORKSPACE;
2007 }
2008
2009 final View v = getWindow().peekDecorView();
2010 if (v != null && v.getWindowToken() != null) {
2011 InputMethodManager imm = (InputMethodManager)getSystemService(
2012 INPUT_METHOD_SERVICE);
2013 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
2014 }
2015
Winson Chungb745afb2015-03-02 11:51:23 -08002016 // Reset the apps view
2017 if (!alreadyOnHome && mAppsView != null) {
2018 mAppsView.scrollToTop();
2019 }
2020
Hyunyoung Song3f471442015-04-08 19:01:34 -07002021 // Reset the widgets view
2022 if (!alreadyOnHome && mWidgetsView != null) {
2023 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07002024 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002025
Adam Cohen9211d422014-10-07 18:14:53 -07002026 if (mLauncherCallbacks != null) {
2027 mLauncherCallbacks.onHomeIntent();
2028 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002029 }
Adam Cohened307df2013-10-02 09:37:31 -07002030
Michael Jurka447bf842013-05-15 14:52:15 +02002031 if (DEBUG_RESUME_TIME) {
2032 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
2033 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002034
Adam Cohen9211d422014-10-07 18:14:53 -07002035 if (mLauncherCallbacks != null) {
2036 mLauncherCallbacks.onNewIntent(intent);
2037 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002038 }
2039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07002041 public void onRestoreInstanceState(Bundle state) {
2042 super.onRestoreInstanceState(state);
2043 for (int page: mSynchronouslyBoundPages) {
2044 mWorkspace.restoreInstanceStateForChild(page);
2045 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046 }
2047
2048 @Override
2049 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002050 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002051 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2052 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002053 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002054 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002055
Michael Jurka883f55b2010-10-21 15:47:14 -07002056 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002057 // We close any open folder since it will not be re-opened, and we need to make sure
2058 // this state is reflected.
2059 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060
Adam Cohendcd297f2013-06-18 13:13:40 -07002061 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002062 mWaitingForResult) {
2063 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002064 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002065 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2066 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002067 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2068 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2069 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002070 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002071 }
2072
Hyunyoung Song3f471442015-04-08 19:01:34 -07002073 // Save the current widgets tray?
2074 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002075 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002076
2077 if (mLauncherCallbacks != null) {
2078 mLauncherCallbacks.onSaveInstanceState(outState);
2079 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 }
2081
2082 @Override
2083 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002084 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002085
Winson Chunge7a03942011-08-05 15:05:12 -07002086 // Remove all pending runnables
2087 mHandler.removeMessages(ADVANCE_MSG);
2088 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002089 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002090
Winson Chungcd2b0142011-06-08 16:02:26 -07002091 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002092 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002093
2094 // It's possible to receive onDestroy after a new Launcher activity has
2095 // been created. In this case, don't interfere with the new Launcher.
2096 if (mModel.isCurrentCallbacks(this)) {
2097 mModel.stopLoader();
2098 app.setLauncher(null);
2099 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002100
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002101 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002102 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002104 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002106 mAppWidgetHost = null;
2107
2108 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002109
2110 TextKeyListener.getInstance().release();
2111
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002112 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002113 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002114
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002115 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002116 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002117 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002118 mWorkspace = null;
2119 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002120
Michael Jurka2ecf9952012-06-18 12:52:28 -07002121 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002122
2123 if (mLauncherCallbacks != null) {
2124 mLauncherCallbacks.onDestroy();
2125 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002126 }
2127
Adam Cohena9cf38f2011-05-02 15:36:58 -07002128 public DragController getDragController() {
2129 return mDragController;
2130 }
2131
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 @Override
2133 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002134 onStartForResult(requestCode);
2135 super.startActivityForResult(intent, requestCode);
2136 }
2137
2138 @Override
2139 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2140 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2141 onStartForResult(requestCode);
2142 try {
2143 super.startIntentSenderForResult(intent, requestCode,
2144 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2145 } catch (IntentSender.SendIntentException e) {
2146 throw new ActivityNotFoundException();
2147 }
2148 }
2149
2150 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002151 if (requestCode >= 0) {
2152 setWaitingForResult(true);
2153 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 }
2155
Winson Chung70d72102011-08-12 11:24:35 -07002156 /**
2157 * Indicates that we want global search for this activity by setting the globalSearch
2158 * argument for {@link #startSearch} to true.
2159 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002160 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002161 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002162 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002163
Karl Rosaen138a0412009-04-23 19:00:21 -07002164 if (initialQuery == null) {
2165 // Use any text typed in the launcher as the initial query
2166 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002167 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002168 if (appSearchData == null) {
2169 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002170 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002171 }
Winson Chungadf0c182012-08-23 14:59:07 -07002172 Rect sourceBounds = new Rect();
2173 if (mSearchDropTargetBar != null) {
2174 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2175 }
Romain Guycbb89e42009-06-08 15:52:54 -07002176
Ian Parkinson047036e2014-09-01 15:40:53 +01002177 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002178 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002179 if (clearTextImmediately) {
2180 clearTypedText();
2181 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002182
2183 // We need to show the workspace after starting the search
2184 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002185 }
2186
Ian Parkinson047036e2014-09-01 15:40:53 +01002187 /**
2188 * Start a text search.
2189 *
2190 * @return {@code true} if the search will start immediately, so any further keypresses
2191 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2192 * to buffer keypresses.
2193 */
2194 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002195 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002196 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2197 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2198 sourceBounds);
2199 }
2200
Michael Jurkaa33411c2012-06-14 16:18:21 -07002201 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002202 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002203 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002204 }
2205
2206 /**
2207 * Starts the global search activity. This code is a copied from SearchManager
2208 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002209 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002210 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002211 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002212 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2213 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2214 if (globalSearchActivity == null) {
2215 Log.w(TAG, "No global search activity found.");
2216 return;
2217 }
2218 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2219 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2220 intent.setComponent(globalSearchActivity);
2221 // Make sure that we have a Bundle to put source in
2222 if (appSearchData == null) {
2223 appSearchData = new Bundle();
2224 } else {
2225 appSearchData = new Bundle(appSearchData);
2226 }
Adam Cohen9211d422014-10-07 18:14:53 -07002227 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002228 if (!appSearchData.containsKey("source")) {
2229 appSearchData.putString("source", getPackageName());
2230 }
2231 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2232 if (!TextUtils.isEmpty(initialQuery)) {
2233 intent.putExtra(SearchManager.QUERY, initialQuery);
2234 }
2235 if (selectInitialQuery) {
2236 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2237 }
2238 intent.setSourceBounds(sourceBounds);
2239 try {
2240 startActivity(intent);
2241 } catch (ActivityNotFoundException ex) {
2242 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2243 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002244 }
2245
Yura4f93ec62014-02-04 14:15:21 +00002246 public boolean isOnCustomContent() {
2247 return mWorkspace.isOnOrMovingToCustomContent();
2248 }
2249
Winson Chung70d72102011-08-12 11:24:35 -07002250 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002251 public boolean onPrepareOptionsMenu(Menu menu) {
2252 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002253 if (!isOnCustomContent()) {
2254 // Close any open folders
2255 closeFolder();
2256 // Stop resizing any widgets
2257 mWorkspace.exitWidgetResizeMode();
2258 if (!mWorkspace.isInOverviewMode()) {
2259 // Show the overview mode
2260 showOverviewMode(true);
2261 } else {
2262 showWorkspace(true);
2263 }
Winson Chunge0298742014-01-17 12:03:00 -08002264 }
Adam Cohen9211d422014-10-07 18:14:53 -07002265 if (mLauncherCallbacks != null) {
2266 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2267 }
2268
Michael Jurkab94f3f82013-09-11 18:08:54 +02002269 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002270 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002272 @Override
2273 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002274 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002275 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002276 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002277 }
2278
Joe Onorato9c1289c2009-08-17 11:03:03 -04002279 public boolean isWorkspaceLocked() {
2280 return mWorkspaceLoading || mWaitingForResult;
2281 }
2282
Adam Cohen517a7f52014-03-01 12:12:59 -08002283 public boolean isWorkspaceLoading() {
2284 return mWorkspaceLoading;
2285 }
2286
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002287 private void setWorkspaceLoading(boolean value) {
2288 boolean isLocked = isWorkspaceLocked();
2289 mWorkspaceLoading = value;
2290 if (isLocked != isWorkspaceLocked()) {
2291 onWorkspaceLockedChanged();
2292 }
2293 }
2294
2295 private void setWaitingForResult(boolean value) {
2296 boolean isLocked = isWorkspaceLocked();
2297 mWaitingForResult = value;
2298 if (isLocked != isWorkspaceLocked()) {
2299 onWorkspaceLockedChanged();
2300 }
2301 }
2302
Adam Cohen9211d422014-10-07 18:14:53 -07002303 protected void onWorkspaceLockedChanged() {
2304 if (mLauncherCallbacks != null) {
2305 mLauncherCallbacks.onWorkspaceLockedChanged();
2306 }
2307 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002308
Michael Jurka0280c3b2010-09-17 15:00:07 -07002309 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002310 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002311 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002312 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2313 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002314 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002315 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002316 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002317
Sunny Goyale6b63a32015-01-16 16:14:29 -08002318 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002319 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002320 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2321 }
2322
Sunny Goyale6b63a32015-01-16 16:14:29 -08002323 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002324 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2325 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002326 if (appWidgetInfo.configure != null) {
2327 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002328 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002329
Bjorn Bringert7984c942009-12-09 15:38:25 +00002330 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002331 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2332 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2333
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002334 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002335 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002336 Runnable onComplete = new Runnable() {
2337 @Override
2338 public void run() {
2339 // Exit spring loaded mode if necessary after adding the widget
2340 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2341 null);
2342 }
2343 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002344 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002345 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002346 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002347 }
2348 }
Romain Guycbb89e42009-06-08 15:52:54 -07002349
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002350 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002351 // Close any folders that may be open.
2352 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002353 mWorkspace.moveToCustomContentScreen(animate);
2354 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002355
2356 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2357 int[] cell, int spanX, int spanY) {
2358 switch (info.itemType) {
2359 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2360 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2361 int span[] = new int[2];
2362 span[0] = spanX;
2363 span[1] = spanY;
2364 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2365 container, screenId, cell, span);
2366 break;
2367 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2368 processShortcutFromDrop(info.componentName, container, screenId, cell);
2369 break;
2370 default:
2371 throw new IllegalStateException("Unknown item type: " + info.itemType);
2372 }
2373 }
2374
Adam Cohenfbba09b2011-07-18 21:43:05 -07002375 /**
2376 * Process a shortcut drop.
2377 *
2378 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002379 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002380 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002381 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002382 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2383 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002384 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002385 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002386 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002387 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002388
2389 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002390 mPendingAddInfo.cellX = cell[0];
2391 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002392 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002393
2394 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2395 createShortcutIntent.setComponent(componentName);
2396 processShortcut(createShortcutIntent);
2397 }
2398
Adam Cohenfbba09b2011-07-18 21:43:05 -07002399 /**
2400 * Process a widget drop.
2401 *
2402 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002403 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002404 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002405 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002406 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2407 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002408 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002409 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002410 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002411 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002412 mPendingAddInfo.minSpanX = info.minSpanX;
2413 mPendingAddInfo.minSpanY = info.minSpanY;
2414
Adam Cohenfbba09b2011-07-18 21:43:05 -07002415 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002416 mPendingAddInfo.cellX = cell[0];
2417 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002418 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002419 if (span != null) {
2420 mPendingAddInfo.spanX = span[0];
2421 mPendingAddInfo.spanY = span[1];
2422 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002423
Adam Cohened66b2b2012-01-23 17:28:51 -08002424 AppWidgetHostView hostView = info.boundWidget;
2425 int appWidgetId;
2426 if (hostView != null) {
2427 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002428 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002429 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002430 // In this case, we either need to start an activity to get permission to bind
2431 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002432 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002433 Bundle options = info.bindOptions;
2434
Sunny Goyalffe83f12014-08-14 17:39:34 -07002435 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2436 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002437 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002438 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002439 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002440 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002441 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2442 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2443 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002444 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2445 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002446 // TODO: we need to make sure that this accounts for the options bundle.
2447 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002448 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2449 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002450 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002451 }
2452
Joe Onoratodeb98af2010-02-19 14:59:39 -08002453 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002454 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002455 }
2456
Winson Chung24ab2f12010-09-16 14:10:47 -07002457 void processWallpaper(Intent intent) {
2458 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2459 }
2460
Adam Cohendcd297f2013-06-18 13:13:40 -07002461 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002462 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002463 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002464 folderInfo.title = getText(R.string.folder_name);
2465
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002466 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002467 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2468 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002469 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470
2471 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002472 FolderIcon newFolder =
2473 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002474 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002475 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002476 // Force measure the new folder icon
2477 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2478 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002479 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 }
Romain Guycbb89e42009-06-08 15:52:54 -07002481
Joe Onorato9c1289c2009-08-17 11:03:03 -04002482 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002483 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002484 }
2485
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002486 /**
2487 * Registers various content observers. The current implementation registers
2488 * only a favorites observer to keep track of the favorites applications.
2489 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002490 private void registerContentObservers() {
2491 ContentResolver resolver = getContentResolver();
2492 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2493 true, mWidgetObserver);
2494 }
2495
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002496 @Override
2497 public boolean dispatchKeyEvent(KeyEvent event) {
2498 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2499 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002500 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002501 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002502 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002503 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002504 dumpState();
2505 return true;
2506 }
2507 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002508 }
2509 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2510 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002511 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002512 return true;
2513 }
2514 }
2515
2516 return super.dispatchKeyEvent(event);
2517 }
2518
Joe Onorato88ec0992009-11-19 13:16:06 -08002519 @Override
2520 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002521 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2522 return;
2523 }
2524
Winson Chung3d9490a2015-03-24 17:38:42 -07002525 LauncherAccessibilityDelegate delegate =
2526 LauncherAppState.getInstance().getAccessibilityDelegate();
2527 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002528 return;
2529 }
2530
Winson Chungb745afb2015-03-02 11:51:23 -08002531 if (isAppsViewVisible()) {
2532 showWorkspace(true);
2533 } else if (isWidgetsViewVisible()) {
2534 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002535 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002536 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002537 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002538 Folder openFolder = mWorkspace.getOpenFolder();
2539 if (openFolder.isEditingName()) {
2540 openFolder.dismissEditingName();
2541 } else {
2542 closeFolder();
2543 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002544 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002545 mWorkspace.exitWidgetResizeMode();
2546
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002547 // Back button is a no-op here, but give at least some feedback for the button press
2548 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002549 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002550 }
2551
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002552 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002553 * Re-listen when widgets are reset.
2554 */
Adam Cohen091440a2015-03-18 14:16:05 -07002555 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002556 if (mAppWidgetHost != null) {
2557 mAppWidgetHost.startListening();
2558 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002559 }
2560
2561 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002562 * Launches the intent referred by the clicked shortcut.
2563 *
2564 * @param v The view representing the clicked shortcut.
2565 */
2566 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002567 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2568 // view has detached (it's possible for this to happen if the view is removed mid touch).
2569 if (v.getWindowToken() == null) {
2570 return;
2571 }
2572
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002573 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002574 return;
2575 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002576
Adam Cohen1697b792013-09-17 19:08:21 -07002577 if (v instanceof Workspace) {
2578 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002579 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002580 }
2581 return;
2582 }
2583
2584 if (v instanceof CellLayout) {
2585 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002586 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002587 }
2588 }
2589
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002590 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002591 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002592 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002593 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002594 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002595 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002596 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002597 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002598 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002599 } else if (tag instanceof AppInfo) {
2600 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002601 } else if (tag instanceof LauncherAppWidgetInfo) {
2602 if (v instanceof PendingAppWidgetHostView) {
2603 onClickPendingWidget((PendingAppWidgetHostView) v);
2604 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002605 }
2606 }
2607
Sunny Goyal508da152014-08-14 10:53:27 -07002608 public void onClickPagedViewIcon(View v) {
2609 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002610 if (mLauncherCallbacks != null) {
2611 mLauncherCallbacks.onClickPagedViewIcon(v);
2612 }
Sunny Goyal508da152014-08-14 10:53:27 -07002613 }
2614
Sunny Goyal70660032015-05-14 00:07:08 -07002615 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002616 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002617 return false;
2618 }
2619
Michael Jurkaaf442092010-06-10 17:01:57 -07002620 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002621 * Event handler for the app widget view which has not fully restored.
2622 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002623 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002624 if (mIsSafeModeEnabled) {
2625 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2626 return;
2627 }
2628
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002629 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002630 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002631 int widgetId = info.appWidgetId;
2632 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2633 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002634 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2635 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002636 mPendingAddInfo.copyFrom(info);
2637 mPendingAddWidgetId = widgetId;
2638
Sunny Goyalffe83f12014-08-14 17:39:34 -07002639 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2640 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002641 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002642 } else if (info.installProgress < 0) {
2643 // The install has not been queued
2644 final String packageName = info.providerName.getPackageName();
2645 showBrokenAppInstallDialog(packageName,
2646 new DialogInterface.OnClickListener() {
2647 public void onClick(DialogInterface dialog, int id) {
2648 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2649 }
2650 });
2651 } else {
2652 // Download has started.
2653 final String packageName = info.providerName.getPackageName();
2654 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002655 }
2656 }
2657
2658 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002659 * Event handler for the "grid" button that appears on the home screen, which
2660 * enters all apps mode.
2661 *
2662 * @param v The view that was clicked.
2663 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002664 protected void onClickAllAppsButton(View v) {
2665 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002666 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002667 showWorkspace(true);
2668 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002669 // Try and refresh the set of predicted apps before we enter launcher
2670 showAppsView(true /* animated */, false /* resetListToTop */,
2671 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002672 }
2673 }
2674
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002675 private void showBrokenAppInstallDialog(final String packageName,
2676 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002677 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002678 .setTitle(R.string.abandoned_promises_title)
2679 .setMessage(R.string.abandoned_promise_explanation)
2680 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2681 .setNeutralButton(R.string.abandoned_clean_this,
2682 new DialogInterface.OnClickListener() {
2683 public void onClick(DialogInterface dialog, int id) {
2684 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2685 mWorkspace.removeAbandonedPromise(packageName, user);
2686 }
2687 })
2688 .create().show();
2689 return;
2690 }
2691
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002692 /**
2693 * Event handler for an app shortcut click.
2694 *
2695 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2696 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002697 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002698 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2699 Object tag = v.getTag();
2700 if (!(tag instanceof ShortcutInfo)) {
2701 throw new IllegalArgumentException("Input must be a Shortcut");
2702 }
2703
2704 // Open shortcut
2705 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002706
2707 if (shortcut.isDisabled != 0) {
2708 int error = R.string.activity_not_available;
2709 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2710 error = R.string.safemode_shortcut_error;
2711 }
2712 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2713 return;
2714 }
2715
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002716 final Intent intent = shortcut.intent;
2717
2718 // Check for special shortcuts
2719 if (intent.getComponent() != null) {
2720 final String shortcutClass = intent.getComponent().getClassName();
2721
2722 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2723 MemoryDumpActivity.startDump(this);
2724 return;
2725 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2726 toggleShowWeightWatcher();
2727 return;
2728 }
2729 }
2730
Chris Wren40c5ed32014-06-24 18:24:23 -04002731 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002732 if ((v instanceof BubbleTextView)
2733 && shortcut.isPromise()
2734 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002735 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002736 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002737 new DialogInterface.OnClickListener() {
2738 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002739 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002740 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002741 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002742 return;
2743 }
2744
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002745 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002746 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002747
2748 if (mLauncherCallbacks != null) {
2749 mLauncherCallbacks.onClickAppShortcut(v);
2750 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002751 }
2752
Adam Cohen091440a2015-03-18 14:16:05 -07002753 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002754 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002755 final ShortcutInfo shortcut;
2756 final Intent intent;
2757 if (tag instanceof ShortcutInfo) {
2758 shortcut = (ShortcutInfo) tag;
2759 intent = shortcut.intent;
2760 int[] pos = new int[2];
2761 v.getLocationOnScreen(pos);
2762 intent.setSourceBounds(new Rect(pos[0], pos[1],
2763 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002764
Sunny Goyal508da152014-08-14 10:53:27 -07002765 } else if (tag instanceof AppInfo) {
2766 shortcut = null;
2767 intent = ((AppInfo) tag).intent;
2768 } else {
2769 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2770 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002771
2772 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002773 mStats.recordLaunch(intent, shortcut);
2774
2775 if (success && v instanceof BubbleTextView) {
2776 mWaitingForResume = (BubbleTextView) v;
2777 mWaitingForResume.setStayPressed(true);
2778 }
2779 }
2780
2781 /**
2782 * Event handler for a folder icon click.
2783 *
2784 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2785 */
2786 protected void onClickFolderIcon(View v) {
2787 if (LOGD) Log.d(TAG, "onClickFolder");
2788 if (!(v instanceof FolderIcon)){
2789 throw new IllegalArgumentException("Input must be a FolderIcon");
2790 }
2791
2792 FolderIcon folderIcon = (FolderIcon) v;
2793 final FolderInfo info = folderIcon.getFolderInfo();
2794 Folder openFolder = mWorkspace.getFolderForTag(info);
2795
2796 // If the folder info reports that the associated folder is open, then verify that
2797 // it is actually opened. There have been a few instances where this gets out of sync.
2798 if (info.opened && openFolder == null) {
2799 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2800 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2801 info.opened = false;
2802 }
2803
2804 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2805 // Close any open folder
2806 closeFolder();
2807 // Open the requested folder
2808 openFolder(folderIcon);
2809 } else {
2810 // Find the open folder...
2811 int folderScreen;
2812 if (openFolder != null) {
2813 folderScreen = mWorkspace.getPageForView(openFolder);
2814 // .. and close it
2815 closeFolder(openFolder);
2816 if (folderScreen != mWorkspace.getCurrentPage()) {
2817 // Close any folder open on the current screen
2818 closeFolder();
2819 // Pull the folder onto this screen
2820 openFolder(folderIcon);
2821 }
2822 }
2823 }
Adam Cohen9211d422014-10-07 18:14:53 -07002824
2825 if (mLauncherCallbacks != null) {
2826 mLauncherCallbacks.onClickFolderIcon(v);
2827 }
Michael Jurka5130e402011-10-13 04:55:35 -07002828 }
2829
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002830 /**
2831 * Event handler for the (Add) Widgets button that appears after a long press
2832 * on the home screen.
2833 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002834 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002835 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002836 if (mIsSafeModeEnabled) {
2837 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2838 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002839 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002840 if (mLauncherCallbacks != null) {
2841 mLauncherCallbacks.onClickAddWidgetButton(view);
2842 }
Adam Cohen9211d422014-10-07 18:14:53 -07002843 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002844 }
2845
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002846 /**
2847 * Event handler for the wallpaper picker button that appears after a long press
2848 * on the home screen.
2849 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002850 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002851 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002852 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2853 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002854
2855 if (mLauncherCallbacks != null) {
2856 mLauncherCallbacks.onClickWallpaperPicker(v);
2857 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002858 }
2859
2860 /**
2861 * Event handler for a click on the settings button that appears after a long press
2862 * on the home screen.
2863 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002864 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002865 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002866 if (mLauncherCallbacks != null) {
2867 mLauncherCallbacks.onClickSettingsButton(v);
2868 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002869 }
2870
Michael Jurka5130e402011-10-13 04:55:35 -07002871 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002872 // Provide the same haptic feedback that the system offers for virtual keys.
2873 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002874 }
2875
Adam Cohen61f560d2013-09-30 15:58:20 -07002876 public void performHapticFeedbackOnTouchDown(View v) {
2877 // Provide the same haptic feedback that the system offers for virtual keys.
2878 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2879 }
2880
2881 public View.OnTouchListener getHapticFeedbackTouchListener() {
2882 if (mHapticFeedbackTouchListener == null) {
2883 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002884 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002885 @Override
2886 public boolean onTouch(View v, MotionEvent event) {
2887 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2888 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2889 }
2890 return false;
2891 }
2892 };
2893 }
2894 return mHapticFeedbackTouchListener;
2895 }
2896
Adam Cohen9211d422014-10-07 18:14:53 -07002897 public void onDragStarted(View view) {
2898 if (isOnCustomContent()) {
2899 // Custom content screen doesn't participate in drag and drop. If on custom
2900 // content screen, move to default.
2901 moveWorkspaceToDefaultScreen();
2902 }
2903
2904 if (mLauncherCallbacks != null) {
2905 mLauncherCallbacks.onDragStarted(view);
2906 }
2907 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002908
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002909 /**
2910 * Called when the user stops interacting with the launcher.
2911 * This implies that the user is now on the homescreen and is not doing housekeeping.
2912 */
Adam Cohen9211d422014-10-07 18:14:53 -07002913 protected void onInteractionEnd() {
2914 if (mLauncherCallbacks != null) {
2915 mLauncherCallbacks.onInteractionEnd();
2916 }
2917 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002918
2919 /**
2920 * Called when the user starts interacting with the launcher.
2921 * The possible interactions are:
2922 * - open all apps
2923 * - reorder an app shortcut, or a widget
2924 * - open the overview mode.
2925 * This is a good time to stop doing things that only make sense
2926 * when the user is on the homescreen and not doing housekeeping.
2927 */
Adam Cohen9211d422014-10-07 18:14:53 -07002928 protected void onInteractionBegin() {
2929 if (mLauncherCallbacks != null) {
2930 mLauncherCallbacks.onInteractionBegin();
2931 }
2932 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002933
Winson Chungcd99cd32015-04-29 11:03:24 -07002934 /** Updates the interaction state. */
2935 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002936 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002937 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002938 boolean fromStateWithOverlay;
2939 boolean toStateWithOverlay;
2940 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2941 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2942 toStateWithOverlay = toState != Workspace.State.NORMAL;
2943 } else {
2944 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2945 fromState != Workspace.State.NORMAL_HIDDEN;
2946 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2947 toState != Workspace.State.NORMAL_HIDDEN;
2948 }
2949 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002950 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002951 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002952 onInteractionEnd();
2953 }
2954 }
2955
Kenny Guyf07af7b2014-07-31 11:39:16 +01002956 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002957 try {
2958 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002959 launcherApps.showAppDetailsForProfile(componentName, user);
2960 } catch (SecurityException e) {
2961 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2962 Log.e(TAG, "Launcher does not have permission to launch settings");
2963 } catch (ActivityNotFoundException e) {
2964 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2965 Log.e(TAG, "Unable to launch settings");
2966 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002967 }
2968
Michael Jurka1e2f4652013-07-08 18:03:46 -07002969 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002970 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2971 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002972 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002973 // System applications cannot be installed. For now, show a toast explaining that.
2974 // We may give them the option of disabling apps this way.
2975 int messageId = R.string.uninstall_system_app_text;
2976 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002977 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002978 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002979 String packageName = componentName.getPackageName();
2980 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002981 Intent intent = new Intent(
2982 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002983 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2984 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002985 if (user != null) {
2986 user.addToIntent(intent, Intent.EXTRA_USER);
2987 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002988 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002989 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002990 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002991 }
2992
Michael Jurka86a720e2012-05-09 11:23:23 -07002993 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002994 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002995 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002996 // Only launch using the new animation if the shortcut has not opted out (this is a
2997 // private contract between launcher and may be ignored in the future).
2998 boolean useLaunchAnimation = (v != null) &&
2999 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01003000 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
3001 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01003002
Kenny Guy1317e2d2014-05-08 18:52:50 +01003003 UserHandleCompat user = null;
3004 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
3005 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
3006 user = userManager.getUserForSerialNumber(serialNumber);
3007 }
3008
3009 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07003010 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08003011 ActivityOptions opts = null;
3012 if (sClipRevealMethod != null) {
3013 // TODO: call method directly when Launcher3 can depend on M APIs
3014 int left = 0, top = 0;
3015 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
3016 if (v instanceof TextView) {
3017 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08003018 Drawable icon = Workspace.getTextViewIcon((TextView) v);
3019 if (icon != null) {
3020 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08003021 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08003022 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08003023 width = bounds.width();
3024 height = bounds.height();
3025 }
3026 }
3027 try {
3028 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
3029 left, top, width, height);
3030 } catch (IllegalAccessException e) {
3031 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
3032 sClipRevealMethod = null;
3033 } catch (InvocationTargetException e) {
3034 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
3035 sClipRevealMethod = null;
3036 }
3037 }
Adam Cohen2e52c902015-04-06 13:11:28 -07003038 if (opts == null && !Utilities.isLmpOrAbove()) {
3039 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08003040 v.getMeasuredWidth(), v.getMeasuredHeight());
3041 }
Adam Cohen2e52c902015-04-06 13:11:28 -07003042 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07003043 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01003044
3045 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
3046 // Could be launching some bookkeeping activity
3047 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003048 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003049 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003050 launcherApps.startActivityForProfile(intent.getComponent(), user,
3051 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003052 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003053 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003054 } catch (SecurityException e) {
3055 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003056 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003057 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003058 "or use the exported attribute for this activity. "
3059 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003060 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003061 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003062 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003063
Michael Jurka86a720e2012-05-09 11:23:23 -07003064 boolean startActivitySafely(View v, Intent intent, Object tag) {
3065 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003066 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3067 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3068 return false;
3069 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003070 try {
3071 success = startActivity(v, intent, tag);
3072 } catch (ActivityNotFoundException e) {
3073 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3074 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3075 }
3076 return success;
3077 }
3078
Adam Cohen268c4752012-06-06 17:47:33 -07003079 /**
3080 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3081 * in the DragLayer in the exact absolute location of the original FolderIcon.
3082 */
3083 private void copyFolderIconToImage(FolderIcon fi) {
3084 final int width = fi.getMeasuredWidth();
3085 final int height = fi.getMeasuredHeight();
3086
3087 // Lazy load ImageView, Bitmap and Canvas
3088 if (mFolderIconImageView == null) {
3089 mFolderIconImageView = new ImageView(this);
3090 }
3091 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3092 mFolderIconBitmap.getHeight() != height) {
3093 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3094 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3095 }
3096
3097 DragLayer.LayoutParams lp;
3098 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3099 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3100 } else {
3101 lp = new DragLayer.LayoutParams(width, height);
3102 }
3103
Adam Cohen307fe232012-08-16 17:55:58 -07003104 // The layout from which the folder is being opened may be scaled, adjust the starting
3105 // view size by this scale factor.
3106 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003107 lp.customPosition = true;
3108 lp.x = mRectForFolderAnimation.left;
3109 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003110 lp.width = (int) (scale * width);
3111 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003112
3113 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3114 fi.draw(mFolderIconCanvas);
3115 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003116 if (fi.getFolder() != null) {
3117 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3118 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003119 }
Adam Cohen268c4752012-06-06 17:47:33 -07003120 // Just in case this image view is still in the drag layer from a previous animation,
3121 // we remove it and re-add it.
3122 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3123 mDragLayer.removeView(mFolderIconImageView);
3124 }
3125 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003126 if (fi.getFolder() != null) {
3127 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003128 }
Adam Cohen268c4752012-06-06 17:47:33 -07003129 }
3130
Adam Cohen2801caf2011-05-13 20:57:39 -07003131 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003132 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003133 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3134 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3135 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3136
Adam Cohenc51934b2011-07-26 21:07:43 -07003137 FolderInfo info = (FolderInfo) fi.getTag();
3138 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3139 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003140 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3141 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003142 }
3143
Adam Cohen268c4752012-06-06 17:47:33 -07003144 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3145 copyFolderIconToImage(fi);
3146 fi.setVisibility(View.INVISIBLE);
3147
Michael Jurka2ecf9952012-06-18 12:52:28 -07003148 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003149 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003150 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003151 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3152 }
3153 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003154 oa.start();
3155 }
3156
Adam Cohen268c4752012-06-06 17:47:33 -07003157 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003158 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003159 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3160 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3161 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3162
Adam Cohen268c4752012-06-06 17:47:33 -07003163 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003164
Adam Cohen268c4752012-06-06 17:47:33 -07003165 // We remove and re-draw the FolderIcon in-case it has changed
3166 mDragLayer.removeView(mFolderIconImageView);
3167 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003168 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003169 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003170 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003171 oa.addListener(new AnimatorListenerAdapter() {
3172 @Override
3173 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003174 if (cl != null) {
3175 cl.clearFolderLeaveBehind();
3176 // Remove the ImageView copy of the FolderIcon and make the original visible.
3177 mDragLayer.removeView(mFolderIconImageView);
3178 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003179 }
3180 }
3181 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003182 oa.start();
3183 }
3184
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003185 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003186 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003187 * is animated relative to the specified View. If the View is null, no animation
3188 * is played.
3189 *
3190 * @param folderInfo The FolderInfo describing the folder to open.
3191 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003192 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003193 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003194 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3195 if (openFolder != null && openFolder != folder) {
3196 // Close any open folder before opening a folder.
3197 closeFolder();
3198 }
3199
Adam Cohena9cf38f2011-05-02 15:36:58 -07003200 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003201
Adam Cohena9cf38f2011-05-02 15:36:58 -07003202 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203
Sunny Goyalf4066152015-04-15 09:42:19 -07003204 // While the folder is open, the position of the icon cannot change.
3205 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3206
Adam Cohen4554ee12011-08-03 16:13:21 -07003207 // Just verify that the folder hasn't already been added to the DragLayer.
3208 // There was a one-off crash where the folder had a parent already.
3209 if (folder.getParent() == null) {
3210 mDragLayer.addView(folder);
3211 mDragController.addDropTarget((DropTarget) folder);
3212 } else {
3213 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3214 folder.getParent() + ").");
3215 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003216 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003217 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003218
3219 // Notify the accessibility manager that this folder "window" has appeared and occluded
3220 // the workspace items
3221 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3222 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003223 }
3224
3225 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003226 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003227 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003228 if (folder.isEditingName()) {
3229 folder.dismissEditingName();
3230 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003231 closeFolder(folder);
3232 }
3233 }
3234
Sunny Goyal83a8f042015-05-19 12:52:12 -07003235 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003236 folder.getInfo().opened = false;
3237
3238 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3239 if (parent != null) {
3240 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3241 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003242 if (fi != null) {
3243 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3244 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003245 }
3246 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003247
3248 // Notify the accessibility manager that this folder "window" has disappeard and no
3249 // longer occludeds the workspace items
3250 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003251 }
3252
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003253 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003254 if (!isDraggingEnabled()) return false;
3255 if (isWorkspaceLocked()) return false;
3256 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003257
Adam Cohen1697b792013-09-17 19:08:21 -07003258 if (v instanceof Workspace) {
3259 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003260 if (!mWorkspace.isTouchActive()) {
3261 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003262 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3263 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3264 return true;
3265 } else {
3266 return false;
3267 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003268 } else {
3269 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003270 }
Adam Cohen1697b792013-09-17 19:08:21 -07003271 }
3272
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003273 CellLayout.CellInfo longClickCellInfo = null;
3274 View itemUnderLongClick = null;
3275 if (v.getTag() instanceof ItemInfo) {
3276 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003277 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003278 itemUnderLongClick = longClickCellInfo.cell;
3279 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003280 }
3281
Winson Chung3d503fb2011-07-13 17:25:49 -07003282 // The hotseat touch handling does not go through Workspace, and we always allow long press
3283 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003284 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003285 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003286 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003287 // User long pressed on empty space
3288 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3289 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003290 if (mWorkspace.isInOverviewMode()) {
3291 mWorkspace.startReordering(v);
3292 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003293 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003294 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003295 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003296 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3297 mHotseat.getOrderInHotseat(
3298 longClickCellInfo.cellX,
3299 longClickCellInfo.cellY));
3300 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003301 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003302 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003303 }
3304 }
3305 }
3306 return true;
3307 }
3308
Winson Chung3d503fb2011-07-13 17:25:49 -07003309 boolean isHotseatLayout(View layout) {
3310 return mHotseat != null && layout != null &&
3311 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3312 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003313
Winson Chung3d503fb2011-07-13 17:25:49 -07003314 /**
3315 * Returns the CellLayout of the specified container at the specified screen.
3316 */
Sunny Goyal92820592015-03-02 11:31:35 -08003317 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003318 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3319 if (mHotseat != null) {
3320 return mHotseat.getLayout();
3321 } else {
3322 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003323 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003324 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003325 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003326 }
3327 }
3328
Winson Chungb745afb2015-03-02 11:51:23 -08003329 /**
3330 * For overridden classes.
3331 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003332 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003333 return isAppsViewVisible();
3334 }
3335
3336 public boolean isAppsViewVisible() {
3337 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3338 }
3339
3340 public boolean isWidgetsViewVisible() {
3341 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003342 }
3343
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003344 private void setWorkspaceBackground(int background) {
3345 switch (background) {
3346 case WORKSPACE_BACKGROUND_TRANSPARENT:
3347 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3348 break;
3349 case WORKSPACE_BACKGROUND_BLACK:
3350 getWindow().setBackgroundDrawable(null);
3351 break;
3352 default:
3353 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3354 }
Craig Mautner360310b2012-10-26 15:13:08 -07003355 }
3356
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003357 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003358 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3359 int curflags = getWindow().getAttributes().flags
3360 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3361 if (wpflags != curflags) {
3362 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3363 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003364 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003365 }
3366
Michael Jurkae326f182011-11-21 14:05:46 -08003367 @Override
3368 public void onTrimMemory(int level) {
3369 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003370 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3371 // The widget preview db can result in holding onto over
3372 // 3MB of memory for caching which isn't necessary.
3373 SQLiteDatabase.releaseMemory();
3374
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003375 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003376 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003377 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003378 if (mLauncherCallbacks != null) {
3379 mLauncherCallbacks.onTrimMemory(level);
3380 }
Michael Jurkae326f182011-11-21 14:05:46 -08003381 }
3382
Winson Chungb745afb2015-03-02 11:51:23 -08003383 @Override
3384 public void onStateTransitionHideSearchBar() {
3385 // Hide the search bar
3386 if (mSearchDropTargetBar != null) {
3387 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3388 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003389 }
3390
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003391 public void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003392 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3393 true);
Adam Cohened307df2013-10-02 09:37:31 -07003394 }
3395
Sunny Goyal83a8f042015-05-19 12:52:12 -07003396 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003397 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3398 onCompleteRunnable, true);
3399 }
3400
3401 protected void showWorkspace(int snapToPage, boolean animated) {
3402 showWorkspace(snapToPage, animated, null, true);
3403 }
3404
3405 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3406 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003407 boolean changed = mState != State.WORKSPACE ||
3408 mWorkspace.getState() != Workspace.State.NORMAL;
3409 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003410 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003411 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003412 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003413 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003414
Winson Chungc7d2b602012-05-16 17:02:20 -07003415 // Show the search bar (only animate if we were showing the drop target bar in spring
3416 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003417 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003418 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003419 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003420
Michael Jurkab3e22d92011-10-31 15:58:33 -07003421 // Set focus to the AppsCustomize button
3422 if (mAllAppsButton != null) {
3423 mAllAppsButton.requestFocus();
3424 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003425 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003426
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003427 // Change the state *after* we've called all the transition code
3428 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003429
Winson Chung5fb63472011-02-02 17:03:37 -08003430 // Resume the auto-advance of widgets
3431 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003432 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003433
Winson Chung0f785722015-04-08 10:27:49 -07003434 if (changed) {
3435 // Send an accessibility event to announce the context change
3436 getWindow().getDecorView()
3437 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003438 if (notifyLauncherCallbacks) {
3439 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003440 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003441 mLauncherCallbacks.onAllAppsHidden();
3442 }
3443 }
Winson Chung0f785722015-04-08 10:27:49 -07003444 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003445 }
3446
Adam Cohened307df2013-10-02 09:37:31 -07003447 void showOverviewMode(boolean animated) {
3448 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003449 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003450 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3451 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003452 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003453 }
3454
Winson Chungb745afb2015-03-02 11:51:23 -08003455 /**
3456 * Shows the apps view.
3457 */
Winson Chung4ac30062015-05-08 17:34:17 -07003458 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003459 if (resetListToTop) {
3460 mAppsView.scrollToTop();
3461 }
Winson Chung4ac30062015-05-08 17:34:17 -07003462 if (updatePredictedApps) {
3463 tryAndUpdatePredictedApps();
3464 }
Winson Chungb745afb2015-03-02 11:51:23 -08003465 showAppsOrWidgets(animated, State.APPS);
3466 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003467
Winson Chungb745afb2015-03-02 11:51:23 -08003468 /**
3469 * Shows the widgets view.
3470 */
3471 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003472 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003473 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003474 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003475 }
Winson Chungb745afb2015-03-02 11:51:23 -08003476 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003477
3478 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003479 @Override
3480 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003481 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003482 }
3483 });
Winson Chungb745afb2015-03-02 11:51:23 -08003484 }
3485
3486 /**
3487 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003488 *
3489 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003490 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003491 // TODO: calling method should use the return value so that when {@code false} is returned
3492 // the workspace transition doesn't fall into invalid state.
3493 private boolean showAppsOrWidgets(boolean animated, State toState) {
3494 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3495 mState != State.WIDGETS_SPRING_LOADED) {
3496 return false;
3497 }
3498 if (toState != State.APPS && toState != State.WIDGETS) {
3499 return false;
3500 }
Winson Chungb745afb2015-03-02 11:51:23 -08003501
3502 if (toState == State.APPS) {
3503 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003504 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003505 mLauncherCallbacks.onAllAppsShown();
3506 }
Winson Chungb745afb2015-03-02 11:51:23 -08003507 } else {
3508 mStateTransitionAnimation.startAnimationToWidgets(animated);
3509 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003510
Michael Jurkab3e22d92011-10-31 15:58:33 -07003511 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003512 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003513
3514 // Pause the auto-advance of widgets until we are out of AllApps
3515 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003516 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003517 closeFolder();
3518
3519 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003520 getWindow().getDecorView()
3521 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003522 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003523 }
3524
Winson Chungcd99cd32015-04-29 11:03:24 -07003525 /**
3526 * Updates the workspace and interaction state on state change, and return the animation to this
3527 * new state.
3528 */
3529 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3530 boolean animated, HashMap<View, Integer> layerViews) {
3531 Workspace.State fromState = mWorkspace.getState();
3532 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3533 updateInteraction(fromState, toState);
3534 return anim;
3535 }
3536
Hyunyoung Song3f471442015-04-08 19:01:34 -07003537 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003538 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003539 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3540 mState == State.WIDGETS_SPRING_LOADED) {
3541 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003542 }
Winson Chungb745afb2015-03-02 11:51:23 -08003543
3544 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003545 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3546 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003547 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003548 }
Adam Cohen7777d962011-08-18 18:58:38 -07003549
Hyunyoung Song3f471442015-04-08 19:01:34 -07003550 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003551 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003552 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003553
Winson Chungcd99cd32015-04-29 11:03:24 -07003554 if (successfulDrop) {
3555 // We need to trigger all apps hidden to notify search to update itself before the
3556 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003557 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003558 mLauncherCallbacks.onAllAppsHidden();
3559 }
3560 }
3561
Winson Chunge7a03942011-08-05 15:05:12 -07003562 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003563 @Override
3564 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003565 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003566 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3567 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003568 // Before we show workspace, hide all apps again because
3569 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3570 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003571 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003572 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003573 } else {
3574 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003575 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003576 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003577 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003578 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003579
Michael Jurkad3ef3062010-11-23 16:23:58 -08003580 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003581 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003582 showAppsView(true /* animated */, false /* resetListToTop */,
3583 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003584 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003585 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003586 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003587 }
3588
Winson Chung4ac30062015-05-08 17:34:17 -07003589 /**
3590 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3591 * resumed.
3592 */
3593 private void tryAndUpdatePredictedApps() {
3594 if (mLauncherCallbacks != null) {
3595 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3596 if (!apps.isEmpty()) {
3597 mAppsView.setPredictedApps(apps);
3598 }
3599 }
3600 }
3601
Michael Jurkab3e22d92011-10-31 15:58:33 -07003602 void lockAllApps() {
3603 // TODO
3604 }
3605
3606 void unlockAllApps() {
3607 // TODO
3608 }
3609
Sunny Goyal594d76d2014-11-06 10:12:54 -08003610 protected void disableVoiceButtonProxy(boolean disable) {
3611 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003612 }
3613
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003614 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003615 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3616 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003617 }
3618
Adam Cohen24ce0b32014-01-14 16:18:14 -08003619 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003620 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3621 if (searchProvider == null) {
3622 return null;
3623 }
3624
3625 Bundle opts = new Bundle();
3626 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003627 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003628
3629 SharedPreferences sp = getSharedPreferences(
3630 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3631 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003632 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003633 if (!searchProvider.provider.flattenToString().equals(
3634 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003635 || (widgetInfo == null)
3636 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003637 // A valid widget is not already bound.
3638 if (widgetId > -1) {
3639 mAppWidgetHost.deleteAppWidgetId(widgetId);
3640 widgetId = -1;
3641 }
3642
3643 // Try to bind a new widget
3644 widgetId = mAppWidgetHost.allocateAppWidgetId();
3645
3646 if (!AppWidgetManagerCompat.getInstance(this)
3647 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3648 mAppWidgetHost.deleteAppWidgetId(widgetId);
3649 widgetId = -1;
3650 }
3651
3652 sp.edit()
3653 .putInt(QSB_WIDGET_ID, widgetId)
3654 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3655 .commit();
3656 }
3657
3658 if (widgetId != -1) {
3659 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3660 mQsb.updateAppWidgetOptions(opts);
3661 mQsb.setPadding(0, 0, 0, 0);
3662 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003663 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003664 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003665 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003666 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003667 }
3668
Sunny Goyal22235bc2015-04-03 09:23:43 -07003669 private void reinflateQSBIfNecessary() {
3670 if (mQsb instanceof LauncherAppWidgetHostView &&
3671 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3672 mSearchDropTargetBar.removeView(mQsb);
3673 mQsb = null;
3674 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3675 }
3676 }
3677
Michael Jurkad7c28052012-04-27 15:43:36 -07003678 @Override
3679 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003680 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003681 final List<CharSequence> text = event.getText();
3682 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003683 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003684 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003685 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003686 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003687 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003688 } else {
3689 text.add(getString(R.string.all_apps_home_button_label));
3690 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003691 return result;
3692 }
3693
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003694 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003695 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003696 */
Adam Cohen091440a2015-03-18 14:16:05 -07003697 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003698 @Override
3699 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003700 closeSystemDialogs();
3701 }
3702 }
3703
3704 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003705 * Receives notifications whenever the appwidgets are reset.
3706 */
3707 private class AppWidgetResetObserver extends ContentObserver {
3708 public AppWidgetResetObserver() {
3709 super(new Handler());
3710 }
3711
3712 @Override
3713 public void onChange(boolean selfChange) {
3714 onAppWidgetReset();
3715 }
3716 }
3717
3718 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003719 * If the activity is currently paused, signal that we need to run the passed Runnable
3720 * in onResume.
3721 *
3722 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003723 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3724 * wrong when we're not running, and if the activity comes back to what the configuration was
3725 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003726 *
3727 * Implementation of the method from LauncherModel.Callbacks.
3728 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003729 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003730 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003731 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003732 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003733 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003734 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003735 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003736 }
3737 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003738 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003739 return true;
3740 } else {
3741 return false;
3742 }
3743 }
3744
Michael Jurkac402cd92013-05-20 15:49:32 +02003745 private boolean waitUntilResume(Runnable run) {
3746 return waitUntilResume(run, false);
3747 }
3748
Michael Jurka1e2f4652013-07-08 18:03:46 -07003749 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003750 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003751 }
3752
Michael Jurka7607c2f2013-04-03 14:33:19 -07003753 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003754 * If the activity is currently paused, signal that we need to re-run the loader
3755 * in onResume.
3756 *
3757 * This needs to be called from incoming places where resources might have been loaded
3758 * while we are paused. That is becaues the Configuration might be wrong
3759 * when we're not running, and if it comes back to what it was when we
3760 * were paused, we are not restarted.
3761 *
3762 * Implementation of the method from LauncherModel.Callbacks.
3763 *
3764 * @return true if we are currently paused. The caller might be able to
3765 * skip some work in that case since we will come back again.
3766 */
3767 public boolean setLoadOnResume() {
3768 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003769 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003770 mOnResumeNeedsLoad = true;
3771 return true;
3772 } else {
3773 return false;
3774 }
3775 }
3776
3777 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003778 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003779 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003780 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003781 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003782 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003783 } else {
3784 return SCREEN_COUNT / 2;
3785 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003786 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003787
Joe Onorato9c1289c2009-08-17 11:03:03 -04003788 /**
3789 * Refreshes the shortcuts shown on the workspace.
3790 *
3791 * Implementation of the method from LauncherModel.Callbacks.
3792 */
3793 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003794 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003795
Michael Jurka7607c2f2013-04-03 14:33:19 -07003796 // If we're starting binding all over again, clear any bind calls we'd postponed in
3797 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3798 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003799 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003800
Winson Chungd64d1762013-08-20 14:37:16 -07003801 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003802 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003803 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003804
Michael Jurka05bf6442011-11-30 20:28:53 -08003805 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003806 if (mHotseat != null) {
3807 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003808 }
3809 }
3810
Adam Cohendcd297f2013-06-18 13:13:40 -07003811 @Override
3812 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003813 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003814
Adam Cohen5084cba2013-09-03 12:01:16 -07003815 // If there are no screens, we need to have an empty screen
3816 if (orderedScreenIds.size() == 0) {
3817 mWorkspace.addExtraEmptyScreen();
3818 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003819
3820 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003821 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003822 if (hasCustomContentToLeft()) {
3823 mWorkspace.createCustomContentContainer();
3824 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003825 }
Winson Chung64359a52013-07-08 17:17:08 -07003826 }
3827
3828 @Override
3829 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003830 // Log to disk
3831 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3832 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3833 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003834 int count = orderedScreenIds.size();
3835 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003836 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003837 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003838 }
3839
Adam Cohen39a06042013-07-19 14:30:12 -07003840 private boolean shouldShowWeightWatcher() {
3841 String spKey = LauncherAppState.getSharedPreferencesKey();
3842 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003843 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003844
3845 return show;
3846 }
3847
3848 private void toggleShowWeightWatcher() {
3849 String spKey = LauncherAppState.getSharedPreferencesKey();
3850 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3851 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3852
3853 show = !show;
3854
3855 SharedPreferences.Editor editor = sp.edit();
3856 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3857 editor.commit();
3858
3859 if (mWeightWatcher != null) {
3860 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3861 }
3862 }
3863
Winson Chungd64d1762013-08-20 14:37:16 -07003864 public void bindAppsAdded(final ArrayList<Long> newScreens,
3865 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003866 final ArrayList<ItemInfo> addAnimated,
3867 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003868 Runnable r = new Runnable() {
3869 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003870 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003871 }
3872 };
3873 if (waitUntilResume(r)) {
3874 return;
3875 }
3876
Winson Chungd64d1762013-08-20 14:37:16 -07003877 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003878 if (newScreens != null) {
3879 bindAddScreens(newScreens);
3880 }
Winson Chungd64d1762013-08-20 14:37:16 -07003881
3882 // We add the items without animation on non-visible pages, and with
3883 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003884 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003885 bindItems(addNotAnimated, 0,
3886 addNotAnimated.size(), false);
3887 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003888 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003889 bindItems(addAnimated, 0,
3890 addAnimated.size(), true);
3891 }
Winson Chungc58497e2013-09-03 17:48:37 -07003892
Winson Chung87412982013-10-03 18:34:14 -07003893 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003894 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003895
Winson Chungb745afb2015-03-02 11:51:23 -08003896 if (addedApps != null && mAppsView != null) {
3897 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003898 }
Winson Chungd64d1762013-08-20 14:37:16 -07003899 }
3900
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003901 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003902 * Bind the items start-end from the list.
3903 *
3904 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003905 */
Winson Chung64359a52013-07-08 17:17:08 -07003906 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3907 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003908 Runnable r = new Runnable() {
3909 public void run() {
3910 bindItems(shortcuts, start, end, forceAnimateIcons);
3911 }
3912 };
3913 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003914 return;
3915 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003916
Winson Chungf0c6ae02012-03-21 16:10:31 -07003917 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003918 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3919 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003920 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003921 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003922 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003923 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003924 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003925
3926 // Short circuit if we are loading dock items for a configuration which has no dock
3927 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3928 mHotseat == null) {
3929 continue;
3930 }
3931
Joe Onorato9c1289c2009-08-17 11:03:03 -04003932 switch (item.itemType) {
3933 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3934 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003935 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003936 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003937
Winson Chung64359a52013-07-08 17:17:08 -07003938 /*
3939 * TODO: FIX collision case
3940 */
Winson Chungce376632013-07-11 16:12:41 -07003941 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3942 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3943 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003944 View v = cl.getChildAt(item.cellX, item.cellY);
3945 Object tag = v.getTag();
3946 String desc = "Collision while binding workspace item: " + item
3947 + ". Collides with " + tag;
3948 if (LauncherAppState.isDogfoodBuild()) {
3949 throw (new RuntimeException(desc));
3950 } else {
3951 Log.d(TAG, desc);
3952 }
Winson Chungce376632013-07-11 16:12:41 -07003953 }
Winson Chung64359a52013-07-08 17:17:08 -07003954 }
3955
Adam Cohendcd297f2013-06-18 13:13:40 -07003956 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3957 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003958 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003959 // Animate all the applications up now
3960 shortcut.setAlpha(0f);
3961 shortcut.setScaleX(0f);
3962 shortcut.setScaleY(0f);
3963 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003964 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003965 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003966 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003967 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003968 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003969 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003970 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003971 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3972 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003973 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003974 default:
3975 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003976 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003977 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003978
Winson Chung997a9232013-07-24 15:33:46 -07003979 if (animateIcons) {
3980 // Animate to the correct page
3981 if (newShortcutsScreenId > -1) {
3982 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003983 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003984 final Runnable startBounceAnimRunnable = new Runnable() {
3985 public void run() {
3986 anim.playTogether(bounceAnims);
3987 anim.start();
3988 }
3989 };
Winson Chung997a9232013-07-24 15:33:46 -07003990 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003991 // We post the animation slightly delayed to prevent slowdowns
3992 // when we are loading right after we return to launcher.
3993 mWorkspace.postDelayed(new Runnable() {
3994 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003995 if (mWorkspace != null) {
3996 mWorkspace.snapToPage(newScreenIndex);
3997 mWorkspace.postDelayed(startBounceAnimRunnable,
3998 NEW_APPS_ANIMATION_DELAY);
3999 }
Winson Chung94d67682013-09-25 16:29:40 -07004000 }
4001 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004002 } else {
4003 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004004 }
4005 }
Winson Chung64359a52013-07-08 17:17:08 -07004006 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004007 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004008 }
4009
Joe Onorato9c1289c2009-08-17 11:03:03 -04004010 /**
4011 * Implementation of the method from LauncherModel.Callbacks.
4012 */
Sunny Goyale2df0622015-04-24 11:27:00 -07004013 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004014 Runnable r = new Runnable() {
4015 public void run() {
4016 bindFolders(folders);
4017 }
4018 };
4019 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004020 return;
4021 }
Sunny Goyale2df0622015-04-24 11:27:00 -07004022 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004023 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004024
4025 /**
4026 * Add the views for a widget to the workspace.
4027 *
4028 * Implementation of the method from LauncherModel.Callbacks.
4029 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004030 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004031 Runnable r = new Runnable() {
4032 public void run() {
4033 bindAppWidget(item);
4034 }
4035 };
4036 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004037 return;
4038 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004039
Daniel Sandler843e8602010-06-07 14:59:01 -04004040 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4041 if (DEBUG_WIDGETS) {
4042 Log.d(TAG, "bindAppWidget: " + item);
4043 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004044 final Workspace workspace = mWorkspace;
4045
Adam Cohen59400422014-03-05 18:07:04 -08004046 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00004047 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08004048
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004049 if (!mIsSafeModeEnabled
4050 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4051 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004052 if (appWidgetInfo == null) {
4053 if (DEBUG_WIDGETS) {
4054 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4055 + " belongs to component " + item.providerName
4056 + ", as the povider is null");
4057 }
4058 LauncherModel.deleteItemFromDatabase(this, item);
4059 return;
4060 }
4061 // Note: This assumes that the id remap broadcast is received before this step.
4062 // If that is not the case, the id remap will be ignored and user may see the
4063 // click to setup view.
Adam Cohen2e6da152015-05-06 11:42:25 -07004064 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004065 pendingInfo.spanX = item.spanX;
4066 pendingInfo.spanY = item.spanY;
4067 pendingInfo.minSpanX = item.minSpanX;
4068 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004069 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004070 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004071
Sunny Goyalff572272014-07-23 13:58:07 -07004072 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004073 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4074 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004075
4076 // TODO consider showing a permission dialog when the widget is clicked.
4077 if (!success) {
4078 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4079 if (DEBUG_WIDGETS) {
4080 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4081 + " belongs to component " + item.providerName
4082 + ", as the launcher is unable to bing a new widget id");
4083 }
4084 LauncherModel.deleteItemFromDatabase(this, item);
4085 return;
4086 }
4087
4088 item.appWidgetId = newWidgetId;
4089
4090 // If the widget has a configure activity, it is still needs to set it up, otherwise
4091 // the widget is ready to go.
4092 item.restoreStatus = (appWidgetInfo.configure == null)
4093 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4094 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4095
4096 LauncherModel.updateItemInDatabase(this, item);
4097 }
4098
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004099 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004100 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004101 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004102 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4103 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004104 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004105
Sunny Goyal651077b2014-06-30 14:15:31 -07004106 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4107 } else {
4108 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004109 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4110 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004111 view.updateIcon(mIconCache);
4112 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004113 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004114 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004115 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004116
Joe Onorato9c1289c2009-08-17 11:03:03 -04004117 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004118 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004119
Adam Cohendcd297f2013-06-18 13:13:40 -07004120 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004121 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004122 if (!item.isCustomWidget()) {
4123 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4124 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004125
Joe Onorato9c1289c2009-08-17 11:03:03 -04004126 workspace.requestLayout();
4127
Daniel Sandler843e8602010-06-07 14:59:01 -04004128 if (DEBUG_WIDGETS) {
4129 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4130 + (SystemClock.uptimeMillis()-start) + "ms");
4131 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004132 }
4133
Sunny Goyalff572272014-07-23 13:58:07 -07004134 /**
4135 * Restores a pending widget.
4136 *
4137 * @param appWidgetId The app widget id
4138 * @param cellInfo The position on screen where to create the widget.
4139 */
4140 private void completeRestoreAppWidget(final int appWidgetId) {
4141 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4142 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4143 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4144 return;
4145 }
4146
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004147 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004148 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4149
4150 mWorkspace.reinflateWidgetsIfNecessary();
4151 LauncherModel.updateItemInDatabase(this, info);
4152 }
4153
Adam Cohen1462de32012-07-24 22:34:36 -07004154 public void onPageBoundSynchronously(int page) {
4155 mSynchronouslyBoundPages.add(page);
4156 }
4157
Joe Onorato9c1289c2009-08-17 11:03:03 -04004158 /**
4159 * Callback saying that there aren't any more items to bind.
4160 *
4161 * Implementation of the method from LauncherModel.Callbacks.
4162 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004163 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004164 Runnable r = new Runnable() {
4165 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004166 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004167 }
4168 };
4169 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004170 return;
4171 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004172 if (mSavedState != null) {
4173 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004174 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004175 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004176 mSavedState = null;
4177 }
4178
Adam Cohen1462de32012-07-24 22:34:36 -07004179 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004180
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004181 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004182 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004183
4184 // If we received the result of any pending adds while the loader was running (e.g. the
4185 // widget configuration forced an orientation change), process them now.
4186 if (sPendingAddItem != null) {
4187 final long screenId = completeAdd(sPendingAddItem);
4188
4189 // TODO: this moves the user to the page where the pending item was added. Ideally,
4190 // the screen would be guaranteed to exist after bind, and the page would be set through
4191 // the workspace restore process.
4192 mWorkspace.post(new Runnable() {
4193 @Override
4194 public void run() {
4195 mWorkspace.snapToScreenId(screenId);
4196 }
4197 });
4198 sPendingAddItem = null;
4199 }
4200
Sunny Goyal756adbc2015-04-16 15:20:51 -07004201 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004202
4203 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004204 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004205 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004206 }
4207
Adam Cohen3ed316a2014-07-23 18:21:20 -07004208 private void sendLoadingCompleteBroadcastIfNecessary() {
4209 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4210 String permission =
4211 getResources().getString(R.string.receive_first_load_broadcast_permission);
4212 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4213 sendBroadcast(intent, permission);
4214 SharedPreferences.Editor editor = mSharedPrefs.edit();
4215 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4216 editor.apply();
4217 }
4218 }
4219
Winson Chunga0b7e862013-09-05 16:03:15 -07004220 public boolean isAllAppsButtonRank(int rank) {
4221 if (mHotseat != null) {
4222 return mHotseat.isAllAppsButtonRank(rank);
4223 }
4224 return false;
4225 }
4226
Winson Chunga2413752012-04-03 14:22:34 -07004227 private boolean canRunNewAppsAnimation() {
4228 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4229 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4230 }
4231
Winson Chungc9168342013-06-26 14:54:55 -07004232 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4233 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4234 PropertyValuesHolder.ofFloat("alpha", 1f),
4235 PropertyValuesHolder.ofFloat("scaleX", 1f),
4236 PropertyValuesHolder.ofFloat("scaleY", 1f));
4237 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4238 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004239 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004240 return bounceAnim;
4241 }
4242
Adam Cohen27772732013-11-11 14:00:56 +00004243 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004244 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004245 }
4246
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004247 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004248 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004249 }
4250
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004251 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004252 if (mSearchDropTargetBar == null) {
4253 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004254 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004255 if (mQsb != null) {
4256 mSearchDropTargetBar.removeView(mQsb);
4257 mQsb = null;
4258 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004259 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004260 }
4261
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004262 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004263 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4264 * multiple calls to bind the same list.)
4265 */
4266 @Thunk ArrayList<AppInfo> mTmpAppsList;
4267 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4268 public void run() {
4269 bindAllApplications(mTmpAppsList);
4270 mTmpAppsList = null;
4271 }
4272 };
4273
4274 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004275 * Add the icons for all apps.
4276 *
4277 * Implementation of the method from LauncherModel.Callbacks.
4278 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004279 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004280 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4281 mTmpAppsList = apps;
4282 return;
4283 }
4284
Winson Chungb745afb2015-03-02 11:51:23 -08004285 if (mAppsView != null) {
4286 mAppsView.setApps(apps);
4287 }
Adam Cohen9211d422014-10-07 18:14:53 -07004288 if (mLauncherCallbacks != null) {
4289 mLauncherCallbacks.bindAllApplications(apps);
4290 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004291 }
4292
4293 /**
4294 * A package was updated.
4295 *
4296 * Implementation of the method from LauncherModel.Callbacks.
4297 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004298 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004299 Runnable r = new Runnable() {
4300 public void run() {
4301 bindAppsUpdated(apps);
4302 }
4303 };
4304 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004305 return;
4306 }
4307
Winson Chungb745afb2015-03-02 11:51:23 -08004308 if (mAppsView != null) {
4309 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004310 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004311 }
4312
Sunny Goyal4390ace2014-10-13 11:33:11 -07004313 @Override
4314 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4315 Runnable r = new Runnable() {
4316 public void run() {
4317 bindWidgetsRestored(widgets);
4318 }
4319 };
4320 if (waitUntilResume(r)) {
4321 return;
4322 }
4323 mWorkspace.widgetsRestored(widgets);
4324 }
4325
Joe Onorato9c1289c2009-08-17 11:03:03 -04004326 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004327 * Some shortcuts were updated in the background.
4328 *
4329 * Implementation of the method from LauncherModel.Callbacks.
4330 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004331 @Override
4332 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4333 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004334 Runnable r = new Runnable() {
4335 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004336 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004337 }
4338 };
4339 if (waitUntilResume(r)) {
4340 return;
4341 }
4342
Sunny Goyal4390ace2014-10-13 11:33:11 -07004343 if (!updated.isEmpty()) {
4344 mWorkspace.updateShortcuts(updated);
4345 }
4346
4347 if (!removed.isEmpty()) {
4348 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4349 for (ShortcutInfo si : removed) {
4350 removedComponents.add(si.getTargetComponent());
4351 }
4352 mWorkspace.removeItemsByComponentName(removedComponents, user);
4353 // Notify the drag controller
4354 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004355 }
4356 }
4357
4358 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004359 * Update the state of a package, typically related to install state.
4360 *
4361 * Implementation of the method from LauncherModel.Callbacks.
4362 */
Sunny Goyale755d462014-07-22 13:48:29 -07004363 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004364 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4365 Runnable r = new Runnable() {
4366 public void run() {
4367 bindRestoreItemsChange(updates);
4368 }
4369 };
4370 if (waitUntilResume(r)) {
4371 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004372 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004373
Sunny Goyal756adbc2015-04-16 15:20:51 -07004374 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004375 }
4376
4377 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004378 * A package was uninstalled. We take both the super set of packageNames
4379 * in addition to specific applications to remove, the reason being that
4380 * this can be called when a package is updated as well. In that scenario,
4381 * we only remove specific components from the workspace, where as
4382 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004383 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004384 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004385 * Implementation of the method from LauncherModel.Callbacks.
4386 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004387 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004388 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004389 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004390 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004391 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004392 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004393 }
Winson Chungd64d1762013-08-20 14:37:16 -07004394 };
4395 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004396 return;
4397 }
4398
Sunny Goyal1a745e82014-10-02 15:58:31 -07004399 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004400 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4401 for (AppInfo info : appInfos) {
4402 removedComponents.add(info.componentName);
4403 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004404 if (!packageNames.isEmpty()) {
4405 mWorkspace.removeItemsByPackageName(packageNames, user);
4406 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004407 if (!removedComponents.isEmpty()) {
4408 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004409 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004410 // Notify the drag controller
4411 mDragController.onAppsRemoved(packageNames, removedComponents);
4412
Sunny Goyal1a745e82014-10-02 15:58:31 -07004413 } else {
4414 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004415 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004416
Winson Chungdf95eb12013-10-16 14:57:07 -07004417 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004418 if (mAppsView != null) {
4419 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004420 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004421 }
Joe Onoratobe386092009-11-17 17:32:16 -08004422
Michael Jurkac402cd92013-05-20 15:49:32 +02004423 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004424 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004425 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004426 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004427 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004428
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004429 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004430 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004431 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004432 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004433 return;
4434 }
4435
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004436 if (mWidgetsView != null && model != null) {
4437 mWidgetsView.addWidgets(model);
4438 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004439 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004440 }
4441
Winson Chung400438b2011-01-16 17:53:48 -08004442 private int mapConfigurationOriActivityInfoOri(int configOri) {
4443 final Display d = getWindowManager().getDefaultDisplay();
4444 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4445 switch (d.getRotation()) {
4446 case Surface.ROTATION_0:
4447 case Surface.ROTATION_180:
4448 // We are currently in the same basic orientation as the natural orientation
4449 naturalOri = configOri;
4450 break;
4451 case Surface.ROTATION_90:
4452 case Surface.ROTATION_270:
4453 // We are currently in the other basic orientation to the natural orientation
4454 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4455 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4456 break;
4457 }
4458
4459 int[] oriMap = {
4460 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4461 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4462 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4463 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4464 };
4465 // Since the map starts at portrait, we need to offset if this device's natural orientation
4466 // is landscape.
4467 int indexOffset = 0;
4468 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4469 indexOffset = 1;
4470 }
4471 return oriMap[(d.getRotation() + indexOffset) % 4];
4472 }
Adam Cohen446e9402011-09-15 18:21:21 -07004473
Winson Chung4b919f82012-05-01 10:44:08 -07004474 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004475 if (Utilities.isRotationEnabled(this)) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004476 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4477 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4478 .getConfiguration().orientation));
4479 } else {
4480 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4481 }
Winson Chung4b919f82012-05-01 10:44:08 -07004482 }
4483 }
4484 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004485 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004486 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004487 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004488 } else {
4489 mHandler.postDelayed(new Runnable() {
4490 public void run() {
4491 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4492 }
4493 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004494 }
Winson Chung4b919f82012-05-01 10:44:08 -07004495 }
Winson Chung400438b2011-01-16 17:53:48 -08004496 }
4497
Winson Chunge43a1e72014-01-15 10:33:02 -08004498 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004499 if (mLauncherCallbacks != null) {
4500 return mLauncherCallbacks.isLauncherPreinstalled();
4501 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004502 PackageManager pm = getPackageManager();
4503 try {
4504 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4505 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4506 return true;
4507 } else {
4508 return false;
4509 }
4510 } catch (NameNotFoundException e) {
4511 e.printStackTrace();
4512 return false;
4513 }
4514 }
4515
Adam Cohenea90f832014-05-21 15:03:34 -07004516 /**
4517 * This method indicates whether or not we should suggest default wallpaper dimensions
4518 * when our wallpaper cropper was not yet used to set a wallpaper.
4519 */
4520 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004521 if (mLauncherCallbacks != null) {
4522 return mLauncherCallbacks.overrideWallpaperDimensions();
4523 }
Adam Cohenea90f832014-05-21 15:03:34 -07004524 return true;
4525 }
4526
Adam Cohen432609a2014-03-13 17:03:22 -07004527 /**
4528 * To be overridden by subclasses to indicate that there is an activity to launch
4529 * before showing the standard launcher experience.
4530 */
4531 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004532 if (mLauncherCallbacks != null) {
4533 return mLauncherCallbacks.hasFirstRunActivity();
4534 }
Adam Cohen432609a2014-03-13 17:03:22 -07004535 return false;
4536 }
4537
4538 /**
4539 * To be overridden by subclasses to launch any first run activity
4540 */
4541 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004542 if (mLauncherCallbacks != null) {
4543 return mLauncherCallbacks.getFirstRunActivity();
4544 }
Adam Cohen432609a2014-03-13 17:03:22 -07004545 return null;
4546 }
4547
Winson Chung2826a402015-04-17 16:18:53 -07004548 /**
4549 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004550 */
4551 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004552 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4553 return false;
4554 }
4555
Winson Chung2826a402015-04-17 16:18:53 -07004556 if (mLauncherCallbacks != null) {
4557 return mLauncherCallbacks.overrideAllAppsSearch();
4558 }
4559 return false;
4560 }
4561
Winson Chunga6945242014-01-08 14:04:34 -08004562 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004563 return !ActivityManager.isRunningInTestHarness() &&
4564 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004565 }
4566
Adam Cohen4a9423d2014-03-28 14:22:31 -07004567 protected boolean hasRunFirstRunActivity() {
4568 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4569 }
4570
Adam Cohen432609a2014-03-13 17:03:22 -07004571 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004572 if (shouldRunFirstRunActivity() &&
4573 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004574 Intent firstRunIntent = getFirstRunActivity();
4575 if (firstRunIntent != null) {
4576 startActivity(firstRunIntent);
4577 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004578 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004579 }
4580 }
Adam Cohen432609a2014-03-13 17:03:22 -07004581 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004582 }
4583
4584 private void markFirstRunActivityShown() {
4585 SharedPreferences.Editor editor = mSharedPrefs.edit();
4586 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4587 editor.apply();
4588 }
4589
Adam Cohen432609a2014-03-13 17:03:22 -07004590 /**
4591 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4592 * screen that must be displayed and dismissed.
4593 */
4594 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004595 if (mLauncherCallbacks != null) {
4596 return mLauncherCallbacks.hasDismissableIntroScreen();
4597 }
Adam Cohen432609a2014-03-13 17:03:22 -07004598 return false;
4599 }
4600
4601 /**
4602 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4603 */
4604 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004605 if (mLauncherCallbacks != null) {
4606 return mLauncherCallbacks.getIntroScreen();
4607 }
Adam Cohen432609a2014-03-13 17:03:22 -07004608 return null;
4609 }
4610
4611 /**
4612 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4613 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4614 */
4615 private boolean shouldShowIntroScreen() {
4616 return hasDismissableIntroScreen() &&
4617 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4618 }
4619
4620 protected void showIntroScreen() {
4621 View introScreen = getIntroScreen();
4622 changeWallpaperVisiblity(false);
4623 if (introScreen != null) {
4624 mDragLayer.showOverlayView(introScreen);
4625 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004626 if (mLauncherOverlayContainer != null) {
4627 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4628 }
Adam Cohen432609a2014-03-13 17:03:22 -07004629 }
4630
4631 public void dismissIntroScreen() {
4632 markIntroScreenDismissed();
4633 if (showFirstRunActivity()) {
4634 // We delay hiding the intro view until the first run activity is showing. This
4635 // avoids a blip.
4636 mWorkspace.postDelayed(new Runnable() {
4637 @Override
4638 public void run() {
4639 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004640 if (mLauncherOverlayContainer != null) {
4641 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4642 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004643 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004644 }
4645 }, ACTIVITY_START_DELAY);
4646 } else {
4647 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004648 if (mLauncherOverlayContainer != null) {
4649 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4650 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004651 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004652 }
4653 changeWallpaperVisiblity(true);
4654 }
4655
4656 private void markIntroScreenDismissed() {
4657 SharedPreferences.Editor editor = mSharedPrefs.edit();
4658 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4659 editor.apply();
4660 }
4661
Adam Cohen091440a2015-03-18 14:16:05 -07004662 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004663 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4664 // on the device, then we always show the first run cling experience (or if there is no
4665 // launcher2). Otherwise, we prompt the user upon started for migration
4666 LauncherClings launcherClings = new LauncherClings(this);
4667 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4668 if (mModel.canMigrateFromOldLauncherDb(this)) {
4669 launcherClings.showMigrationCling();
4670 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004671 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004672 }
4673 }
4674 }
4675
Winson Chunga6945242014-01-08 14:04:34 -08004676 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004677 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4678 if (mHotseat != null) mHotseat.setAlpha(1f);
4679 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4680 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004681 }
4682
4683 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004684 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4685 if (mHotseat != null) mHotseat.setAlpha(0f);
4686 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4687 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004688 }
4689
Mathew Inwood72fbec12013-11-19 15:45:07 +00004690 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004691 // Called from search suggestion, not supported in other profiles.
4692 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4693 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4694 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4695 myUser);
4696 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004697 return null;
4698 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004699 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004700 }
4701
4702 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4703 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004704 // Called from search suggestion, not supported in other profiles.
4705 return createShortcutDragInfo(shortcutIntent, caption, icon,
4706 UserHandleCompat.myUserHandle());
4707 }
4708
4709 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4710 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004711 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004712 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004713 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004714 }
4715
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004716 protected void moveWorkspaceToDefaultScreen() {
4717 mWorkspace.moveToDefaultScreen(false);
4718 }
4719
Mathew Inwood72fbec12013-11-19 15:45:07 +00004720 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4721 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004722 mWorkspace.onExternalDragStartedWithItem(dragView);
4723 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004724 }
4725
Anjali Koppalf5d29132014-02-28 13:33:27 -08004726 @Override
4727 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004728 if (mLauncherCallbacks != null) {
4729 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4730 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004731 }
4732
Winson Chung80baf5a2010-08-09 16:03:15 -07004733 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004734 * Prints out out state for debugging.
4735 */
4736 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004737 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004738 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004739 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4740 Log.d(TAG, "mRestoring=" + mRestoring);
4741 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4742 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004743 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004744 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004745 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004746
Daniel Sandler325dc232013-06-05 22:57:57 -04004747 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004748 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004749
4750 @Override
4751 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4752 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004753 synchronized (sDumpLogs) {
4754 writer.println(" ");
4755 writer.println("Debug logs: ");
4756 for (int i = 0; i < sDumpLogs.size(); i++) {
4757 writer.println(" " + sDumpLogs.get(i));
4758 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004759 }
Adam Cohen9211d422014-10-07 18:14:53 -07004760 if (mLauncherCallbacks != null) {
4761 mLauncherCallbacks.dump(prefix, fd, writer, args);
4762 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004763 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004764
4765 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004766 if (DEBUG_DUMP_LOG) {
4767 synchronized (sDumpLogs) {
4768 Log.d(TAG, "");
4769 Log.d(TAG, "*********************");
4770 Log.d(TAG, "Launcher debug logs: ");
4771 for (int i = 0; i < sDumpLogs.size(); i++) {
4772 Log.d(TAG, " " + sDumpLogs.get(i));
4773 }
4774 Log.d(TAG, "*********************");
4775 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004776 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004777 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004778 }
4779
4780 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004781 addDumpLog(tag, log, null, debugLog);
4782 }
4783
4784 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004785 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004786 if (e != null) {
4787 Log.d(tag, log, e);
4788 } else {
4789 Log.d(tag, log);
4790 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004791 }
Winson Chungede41292013-09-19 16:27:36 -07004792 if (DEBUG_DUMP_LOG) {
4793 sDateStamp.setTime(System.currentTimeMillis());
4794 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004795 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4796 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004797 }
Winson Chungede41292013-09-19 16:27:36 -07004798 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004799 }
4800
Adam Cohen59400422014-03-05 18:07:04 -08004801 public static CustomAppWidget getCustomAppWidget(String name) {
4802 return sCustomAppWidgets.get(name);
4803 }
4804
4805 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4806 return sCustomAppWidgets;
4807 }
4808
Winson Chungede41292013-09-19 16:27:36 -07004809 public void dumpLogsToLocalData() {
4810 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004811 new AsyncTask<Void, Void, Void>() {
4812 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004813 boolean success = false;
4814 sDateStamp.setTime(sRunStart);
4815 String FILENAME = sDateStamp.getMonth() + "-"
4816 + sDateStamp.getDay() + "_"
4817 + sDateStamp.getHours() + "-"
4818 + sDateStamp.getMinutes() + "_"
4819 + sDateStamp.getSeconds() + ".txt";
4820
4821 FileOutputStream fos = null;
4822 File outFile = null;
4823 try {
4824 outFile = new File(getFilesDir(), FILENAME);
4825 outFile.createNewFile();
4826 fos = new FileOutputStream(outFile);
4827 } catch (Exception e) {
4828 e.printStackTrace();
4829 }
4830 if (fos != null) {
4831 PrintWriter writer = new PrintWriter(fos);
4832
4833 writer.println(" ");
4834 writer.println("Debug logs: ");
4835 synchronized (sDumpLogs) {
4836 for (int i = 0; i < sDumpLogs.size(); i++) {
4837 writer.println(" " + sDumpLogs.get(i));
4838 }
4839 }
4840 writer.close();
4841 }
4842 try {
4843 if (fos != null) {
4844 fos.close();
4845 success = true;
4846 }
4847 } catch (IOException e) {
4848 e.printStackTrace();
4849 }
Michael Jurka43467462013-11-14 12:03:58 +01004850 return null;
Winson Chungede41292013-09-19 16:27:36 -07004851 }
Michael Jurka43467462013-11-14 12:03:58 +01004852 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004853 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004854 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004855}
Michael Jurka2763be32011-02-24 11:19:57 -08004856
Dan Sandlerd5024042014-01-09 15:01:33 -05004857interface DebugIntents {
4858 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4859 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004860}