blob: 3b1ee2abb12d1ab27f96089c68b0127f58e3e688 [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
Sunny Goyal28c6b962015-10-12 11:42:05 -070019import android.Manifest;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070025import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070026import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000027import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070029import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070030import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
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;
Adam Cohen3f9c9712014-10-31 11:48:25 -070051import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070052import android.graphics.Bitmap;
53import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070054import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070055import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070056import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070057import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040059import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070060import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020061import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080062import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020063import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080064import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070065import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070066import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040067import android.os.SystemClock;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070068import android.os.UserHandle;
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;
Sunny Goyala1d1bf32015-06-26 13:24:53 -070098
Sunny Goyal651077b2014-06-30 14:15:31 -070099import com.android.launcher3.DropTarget.DragObject;
100import com.android.launcher3.PagedView.PageSwitchListener;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700101import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700102import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.LauncherActivityInfoCompat;
104import com.android.launcher3.compat.LauncherAppsCompat;
105import com.android.launcher3.compat.UserHandleCompat;
106import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700107import com.android.launcher3.model.WidgetsModel;
Winson Chung6b1c73f2015-06-18 11:38:42 -0700108import com.android.launcher3.util.ComponentKey;
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 Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700116import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700119import java.io.PrintWriter;
Adam Cohen4caf2982013-08-20 18:54:31 -0700120import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700121import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700122import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700125import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700126import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000127import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700128
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800129/**
130 * Default launcher application.
131 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100132public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700133 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chung006ee262015-08-03 14:40:11 -0700134 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700135 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700136 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800137
Daniel Sandlerf061f822013-06-27 13:59:36 -0400138 static final boolean PROFILE_STARTUP = false;
Sunny Goyalc84e6e12015-10-02 12:48:37 -0700139 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700140 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400141 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700142 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700143
Dan Sandlerd5024042014-01-09 15:01:33 -0500144 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
145
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800146 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700147 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700148 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700149 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150
Michael Jurka8b805b12012-04-18 14:23:14 -0700151 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700152 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700153
Sunny Goyal28c6b962015-10-12 11:42:05 -0700154 private static final int REQUEST_PERMISSION_CALL_PHONE = 13;
155
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700156 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
157 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
158 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
159
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700160 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
161
Mathew Inwood876a8462013-06-14 14:12:41 +0100162 /**
163 * IntentStarter uses request codes starting with this. This must be greater than all activity
164 * request codes used internally.
165 */
166 protected static final int REQUEST_LAST = 100;
167
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800168 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800169
Michael Jurka0a457bf2012-11-19 14:05:05 -0800170 // To turn on these properties, type
171 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800172 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Winson Chung2672ff92012-05-04 16:22:30 -0700174 // The Intent extra that defines whether to ignore the launch animation
175 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400176 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: int
179 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700180 // Type: int
181 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
184 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
186 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700189 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700190 // Type: int
191 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
192 // Type: int
193 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
194 // Type: parcelable
195 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000196 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800197 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000198 // Type: int[]
199 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800200
Adam Cohen432609a2014-03-13 17:03:22 -0700201 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800202 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
203
Adam Cohen3ed316a2014-07-23 18:21:20 -0700204 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
205 static final String ACTION_FIRST_LOAD_COMPLETE =
206 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
207
Adam Cohen39a06042013-07-19 14:30:12 -0700208 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700209 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700210
Sunny Goyal594d76d2014-11-06 10:12:54 -0800211 private static final String QSB_WIDGET_ID = "qsb_widget_id";
212 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
213
Winson Chunga6945242014-01-08 14:04:34 -0800214 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
215
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700216 /** The different states that Launcher can be in. */
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700217 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED }
218
Adam Cohen091440a2015-03-18 14:16:05 -0700219 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700220 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700221
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700222 private boolean mIsSafeModeEnabled;
223
Adam Cohenc2d6e892014-10-16 09:49:24 -0700224 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
225 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700226 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700227
Joe Onorato9c1289c2009-08-17 11:03:03 -0400228 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700229 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
230 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700231 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800232
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000233 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
234 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
235
Winson Chunga2413752012-04-03 14:22:34 -0700236 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700237 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
238 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700239 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700240
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800241 private final BroadcastReceiver mCloseSystemDialogsReceiver
242 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800243
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800244 private LayoutInflater mInflater;
245
Adam Cohen091440a2015-03-18 14:16:05 -0700246 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700247 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800248 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700249 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800250 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700251 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700252
Sunny Goyalffe83f12014-08-14 17:39:34 -0700253 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700254 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700255
Adam Cohen091440a2015-03-18 14:16:05 -0700256 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800257 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800258 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700259
Michael Jurka0280c3b2010-09-17 15:00:07 -0700260 private int[] mTmpAddItemCellCoordinates = new int[2];
261
Sunny Goyal316490e2015-06-02 09:38:28 -0700262 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800263 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700264
Michael Jurka838a4ca2011-02-07 13:33:06 -0800265 private View mAllAppsButton;
Hyunyoung Song98ff38a2015-07-10 17:50:13 -0700266 private View mWidgetsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700267
Winson Chungc51db6a2011-10-05 11:44:49 -0700268 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700269
270 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700271 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700272
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700273 // Main container view and the model for the widget tray screen.
274 @Thunk WidgetsContainerView mWidgetsView;
275 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700276
Winson Chungf0ea4d32011-06-06 14:27:16 -0700277 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800278 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800279
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700281 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
282 // scroll issues (because the workspace may not have been measured yet) and extra work.
283 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
284 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285
286 private SpannableStringBuilder mDefaultKeySsb = null;
287
Adam Cohen091440a2015-03-18 14:16:05 -0700288 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400289
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800290 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291 private boolean mRestoring;
292 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700293 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800294
Michael Jurka1e2f4652013-07-08 18:03:46 -0700295 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700296 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
297
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 private Bundle mSavedInstanceState;
299
Joe Onorato9c1289c2009-08-17 11:03:03 -0400300 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800301 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700302 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800303 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700304 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800305 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400306
Sunny Goyal639e9062015-08-19 19:17:06 -0700307 private LauncherClings mClings;
308
Sunny Goyale2df0622015-04-24 11:27:00 -0700309 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700310
Adam Cohen61f560d2013-09-30 15:58:20 -0700311 private View.OnTouchListener mHapticFeedbackTouchListener;
312
Adam Cohended9f8d2010-11-03 13:25:16 -0700313 // Related to the auto-advancing of widgets
314 private final int ADVANCE_MSG = 1;
315 private final int mAdvanceInterval = 20000;
316 private final int mAdvanceStagger = 250;
317 private long mAutoAdvanceSentTime;
318 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700319 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700320 new HashMap<View, AppWidgetProviderInfo>();
321
Winson Chung400438b2011-01-16 17:53:48 -0800322 // Determines how long to wait after a rotation before restoring the screen orientation to
323 // match the sensor state.
324 private final int mRestoreScreenOrientationDelay = 500;
325
Adam Cohen091440a2015-03-18 14:16:05 -0700326 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700327
Adam Cohen1462de32012-07-24 22:34:36 -0700328 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700329 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700330
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700331 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700332 static Date sDateStamp = new Date();
333 static DateFormat sDateFormat =
334 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
335 static long sRunStart = System.currentTimeMillis();
336 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700337
Winson Chung46353de2012-02-16 14:05:10 -0800338 // We only want to get the SharedPreferences once since it does an FS stat each time we get
339 // it from the context.
340 private SharedPreferences mSharedPrefs;
341
Winson Chungf0c6ae02012-03-21 16:10:31 -0700342 // Holds the page that we need to animate to, and the icon views that we need to animate up
343 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700344 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700345 private Bitmap mFolderIconBitmap;
346 private Canvas mFolderIconCanvas;
347 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700348
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700349 private DeviceProfile mDeviceProfile;
350
Adam Cohen4b66bf32015-09-18 12:15:19 -0700351 private boolean mMoveToDefaultScreenFromNewIntent;
352
Winson Chung13eb5272015-05-11 16:30:13 -0700353 // This is set to the view that launched the activity that navigated the user away from
354 // launcher. Since there is no callback for when the activity has finished launching, enable
355 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800356 private BubbleTextView mWaitingForResume;
357
Adam Cohen59400422014-03-05 18:07:04 -0800358 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
359 new HashMap<String, CustomAppWidget>();
360
361 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
362 static {
363 if (ENABLE_CUSTOM_WIDGET_TEST) {
364 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
365 }
366 }
367
Adam Cohen091440a2015-03-18 14:16:05 -0700368 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700369 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700370 if (mWorkspace != null) {
371 mWorkspace.buildPageHardwareLayers();
372 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700373 }
374 };
375
Adam Cohendb364c32014-05-20 14:23:40 -0700376 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800377
Adam Cohen091440a2015-03-18 14:16:05 -0700378 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800379 int requestCode;
380 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700381 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700382 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800383 int cellX;
384 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700385 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800386 }
387
Daniel Sandlerff02d492013-08-05 02:12:05 -0400388 private Stats mStats;
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700389 FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700390 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400391
392 @Thunk void setOrientation() {
393 if (mRotationEnabled) {
394 unlockScreenOrientation(true);
395 } else {
396 setRequestedOrientation(
397 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700398 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400399 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700400
Sunny Goyal7779d622015-06-11 16:18:39 -0700401 private Runnable mUpdateOrientationRunnable = new Runnable() {
402 public void run() {
403 setOrientation();
404 }
405 };
406
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800407 @Override
408 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700409 if (DEBUG_STRICT_MODE) {
410 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
411 .detectDiskReads()
412 .detectDiskWrites()
413 .detectNetwork() // or .detectAll() for all detectable problems
414 .penaltyLog()
415 .build());
416 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
417 .detectLeakedSqlLiteObjects()
418 .detectLeakedClosableObjects()
419 .penaltyLog()
420 .penaltyDeath()
421 .build());
422 }
423
Adam Cohen9211d422014-10-07 18:14:53 -0700424 if (mLauncherCallbacks != null) {
425 mLauncherCallbacks.preOnCreate();
426 }
427
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800428 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400429
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400430 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400431 LauncherAppState app = LauncherAppState.getInstance();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700432
Adam Cohen2e6da152015-05-06 11:42:25 -0700433 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700434 mDeviceProfile = getResources().getConfiguration().orientation
435 == Configuration.ORIENTATION_LANDSCAPE ?
Tony Wickham3a3517f2015-10-06 11:27:04 -0700436 app.getInvariantDeviceProfile().landscapeProfile
437 : app.getInvariantDeviceProfile().portraitProfile;
Sunny Goyalc6205602015-05-21 20:46:33 -0700438
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400439 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700440 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700441 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800442 mModel = app.setLauncher(this);
443 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700444
Joe Onorato41a12d22009-10-31 18:30:00 -0400445 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 mInflater = getLayoutInflater();
Winson Chung006ee262015-08-03 14:40:11 -0700447 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this);
Romain Guycbb89e42009-06-08 15:52:54 -0700448
Daniel Sandlerff02d492013-08-05 02:12:05 -0400449 mStats = new Stats(this);
450
Sunny Goyalffe83f12014-08-14 17:39:34 -0700451 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700452
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700453 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
454 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700455
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700456 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
457 // this also ensures that any synchronous binding below doesn't re-trigger another
458 // LauncherModel load.
459 mPaused = false;
460
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200462 android.os.Debug.startMethodTracing(
463 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 }
465
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800466 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700467
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800468 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700469 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800470
Joe Onorato7c312c12009-08-13 21:36:53 -0700471 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700472
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800473 mSavedState = savedInstanceState;
474 restoreState(mSavedState);
475
476 if (PROFILE_STARTUP) {
477 android.os.Debug.stopMethodTracing();
478 }
479
480 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700481 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700482 // If the user leaves launcher, then we should just load items asynchronously when
483 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700484 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700485 } else {
486 // We only load the page synchronously if the user rotates (or triggers a
487 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700488 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700489 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800490 }
491
492 // For handling default keys
493 mDefaultKeySsb = new SpannableStringBuilder();
494 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800495
496 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
497 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800498
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700499 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
500 // In case we are on a device with locked rotation, we should look at preferences to check
501 // if the user has specifically allowed rotation.
502 if (!mRotationEnabled) {
Rahul Chaturvedie863fed2015-06-15 14:09:42 -0400503 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext(), false);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700504 }
505
506 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
507 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400508 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700509
Adam Cohen9211d422014-10-07 18:14:53 -0700510 if (mLauncherCallbacks != null) {
511 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700512 if (mLauncherCallbacks.hasLauncherOverlay()) {
513 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700514 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
515 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
516 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700517 mWorkspace.setLauncherOverlay(mLauncherOverlay);
518 }
Adam Cohen9211d422014-10-07 18:14:53 -0700519 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700520
521 if (shouldShowIntroScreen()) {
522 showIntroScreen();
523 } else {
524 showFirstRunActivity();
525 showFirstRunClings();
526 }
Adam Cohen9211d422014-10-07 18:14:53 -0700527 }
528
Sunny Goyal7779d622015-06-11 16:18:39 -0700529 @Override
530 public void onSettingsChanged(String settings, boolean value) {
531 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
532 mRotationEnabled = value;
533 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
534 mUpdateOrientationRunnable.run();
535 }
536 }
537 }
538
Adam Cohen9211d422014-10-07 18:14:53 -0700539 private LauncherCallbacks mLauncherCallbacks;
540
541 public void onPostCreate(Bundle savedInstanceState) {
542 super.onPostCreate(savedInstanceState);
543 if (mLauncherCallbacks != null) {
544 mLauncherCallbacks.onPostCreate(savedInstanceState);
545 }
546 }
547
548 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
549 mLauncherCallbacks = callbacks;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700550 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700551 private boolean mWorkspaceImportanceStored = false;
552 private boolean mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700553 private int mWorkspaceImportanceForAccessibility =
554 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
555 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
556
557 @Override
558 public void onSearchOverlayOpened() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700559 if (mWorkspaceImportanceStored || mHotseatImportanceStored) {
Jun Mukai8af0cd82015-05-12 12:32:12 -0700560 return;
561 }
562 // The underlying workspace and hotseat are temporarily suppressed by the search
563 // overlay. So they sholudn't be accessible.
Jun Mukaie9819e62015-06-17 10:58:59 -0700564 if (mWorkspace != null) {
565 mWorkspaceImportanceForAccessibility =
566 mWorkspace.getImportantForAccessibility();
567 mWorkspace.setImportantForAccessibility(
568 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
569 mWorkspaceImportanceStored = true;
570 }
571 if (mHotseat != null) {
572 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
573 mHotseat.setImportantForAccessibility(
574 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
575 mHotseatImportanceStored = true;
576 }
Jun Mukai8af0cd82015-05-12 12:32:12 -0700577 }
578
579 @Override
580 public void onSearchOverlayClosed() {
Jun Mukaie9819e62015-06-17 10:58:59 -0700581 if (mWorkspaceImportanceStored && mWorkspace != null) {
582 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
583 }
584 if (mHotseatImportanceStored && mHotseat != null) {
585 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
586 }
587 mWorkspaceImportanceStored = false;
588 mHotseatImportanceStored = false;
Jun Mukai8af0cd82015-05-12 12:32:12 -0700589 }
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
Winson Chungef7f8742015-06-04 17:18:17 -0700601 /**
602 * Updates the bounds of all the overlays to match the new fixed bounds.
603 */
604 public void updateOverlayBounds(Rect newBounds) {
605 mAppsView.setSearchBarBounds(newBounds);
606 mWidgetsView.setSearchBarBounds(newBounds);
607 }
608
Adam Cohen9211d422014-10-07 18:14:53 -0700609 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700610 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700611 if (mLauncherCallbacks != null) {
612 return mLauncherCallbacks.hasCustomContentToLeft();
613 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700614 return false;
615 }
616
Dave Hawkeya8881582013-09-17 15:55:33 -0600617 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500618 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600619 * {@link #addToCustomContentPage}. This will only be invoked if
620 * {@link #hasCustomContentToLeft()} is {@code true}.
621 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500622 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700623 if (mLauncherCallbacks != null) {
624 mLauncherCallbacks.populateCustomContentContainer();
625 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600626 }
627
628 /**
629 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
630 * ensure the custom content page is added or removed if necessary.
631 */
632 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600633 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600634 // Not bound yet, wait for bindScreens to be called.
635 return;
636 }
637
638 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
639 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500640 mWorkspace.createCustomContentContainer();
641 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600642 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
643 mWorkspace.removeCustomContentPage();
644 }
645 }
646
Anton Hanssona2f665f2013-09-26 12:18:32 +0100647 public Stats getStats() {
648 return mStats;
649 }
650
Bjorn Bringertc459e522013-06-07 19:36:01 +0100651 public LayoutInflater getInflater() {
652 return mInflater;
653 }
654
Hyunyoung Song3f471442015-04-08 19:01:34 -0700655 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700656 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
657 // that is subsequently removed from the workspace in startBinding().
Sunny Goyal639e9062015-08-19 19:17:06 -0700658 return !isWorkspaceLoading();
Winson Chung36a62fe2012-05-06 18:04:42 -0700659 }
660
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800661 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000662 public static int generateViewId() {
Sunny Goyal9fc953b2015-08-17 12:24:25 -0700663 if (Utilities.ATLEAST_JB_MR1) {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100664 return View.generateViewId();
665 } else {
666 // View.generateViewId() is not available. The following fallback logic is a copy
667 // of its implementation.
668 for (;;) {
669 final int result = sNextGeneratedId.get();
670 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
671 int newValue = result + 1;
672 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
673 if (sNextGeneratedId.compareAndSet(result, newValue)) {
674 return result;
675 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000676 }
677 }
678 }
679
680 public int getViewIdForItem(ItemInfo info) {
681 // This cast is safe given the > 2B range for int.
Sunny Goyalebfae6e2015-08-27 15:30:25 -0700682 int itemId = (int) info.id;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000683 if (mItemIdToViewId.containsKey(itemId)) {
684 return mItemIdToViewId.get(itemId);
685 }
686 int viewId = generateViewId();
687 mItemIdToViewId.put(itemId, viewId);
688 return viewId;
689 }
690
691 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700692 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
693 * a configuration step, this allows the proper animations to run after other transitions.
694 */
Adam Cohendb364c32014-05-20 14:23:40 -0700695 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700696 long screenId = args.screenId;
697 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
698 // When the screen id represents an actual screen (as opposed to a rank) we make sure
699 // that the drop page actually exists.
700 screenId = ensurePendingDropLayoutExists(args.screenId);
701 }
Adam Cohendb364c32014-05-20 14:23:40 -0700702
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800703 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800704 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700705 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700706 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700707 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800708 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700709 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700710 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700711 case REQUEST_RECONFIGURE_APPWIDGET:
712 completeRestoreAppWidget(args.appWidgetId);
713 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800714 }
Michael Jurka27614d22012-04-02 06:40:22 -0700715 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
716 // if you turned the screen off and then back while in All Apps, Launcher would not
717 // return to the workspace. Clearing mAddInfo.container here fixes this issue
718 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700719 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800720 }
721
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800722 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700723 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700724 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700725 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700726 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800727 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700728
Adam Cohenad4e15c2013-10-17 16:21:35 -0700729 Runnable exitSpringLoaded = new Runnable() {
730 @Override
731 public void run() {
732 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
733 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
734 }
735 };
736
Michael Jurka8b805b12012-04-18 14:23:14 -0700737 if (requestCode == REQUEST_BIND_APPWIDGET) {
Winsona1f79d32015-08-05 14:00:45 -0700738 // This is called only if the user did not previously have permissions to bind widgets
Adam Cohenad4e15c2013-10-17 16:21:35 -0700739 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700740 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
741 if (resultCode == RESULT_CANCELED) {
742 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700743 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700744 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700745 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800746 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700747 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Winsona1f79d32015-08-05 14:00:45 -0700748
749 // When the user has granted permission to bind widgets, we should check to see if
750 // we can inflate the default search bar widget.
751 getOrCreateQsbBar();
Michael Jurka8b805b12012-04-18 14:23:14 -0700752 }
753 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200754 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
755 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Tony Wickham6ed058c2015-09-09 18:21:49 -0700756 // User could have free-scrolled between pages before picking a wallpaper; make sure
757 // we move to the closest one now to avoid visual jump.
758 mWorkspace.setCurrentPage(mWorkspace.getPageNearestToCenterOfScreen());
Winson Chungdc61c4d2015-04-20 18:26:57 -0700759 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200760 }
761 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700762 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200763
Adam Cohened66b2b2012-01-23 17:28:51 -0800764 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700765 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700766
Adam Cohendb364c32014-05-20 14:23:40 -0700767 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800768 // We have special handling for widgets
769 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800770 final int appWidgetId;
771 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
772 : -1;
773 if (widgetId < 0) {
774 appWidgetId = pendingAddWidgetId;
775 } else {
776 appWidgetId = widgetId;
777 }
778
Adam Cohenad4e15c2013-10-17 16:21:35 -0700779 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800780 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700781 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
782 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700783 result = RESULT_CANCELED;
784 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700785 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700786 @Override
787 public void run() {
788 exitSpringLoadedDragModeDelayed(false, 0, null);
789 }
790 };
Adam Cohendb364c32014-05-20 14:23:40 -0700791 if (workspaceLocked) {
792 // No need to remove the empty screen if we're mid-binding, as the
793 // the bind will not add the empty screen.
794 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
795 } else {
796 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
797 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
798 }
Winson Chung5aaab772012-04-27 15:24:02 -0700799 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700800 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700801 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
802 // When the screen id represents an actual screen (as opposed to a rank)
803 // we make sure that the drop page actually exists.
804 mPendingAddInfo.screenId =
805 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
806 }
Adam Cohendb364c32014-05-20 14:23:40 -0700807 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
808
809 dropLayout.setDropPending(true);
810 final Runnable onComplete = new Runnable() {
811 @Override
812 public void run() {
813 completeTwoStageWidgetDrop(resultCode, appWidgetId);
814 dropLayout.setDropPending(false);
815 }
816 };
817 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
818 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
819 } else {
820 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
821 mPendingAddInfo);
822 sPendingAddItem = args;
823 }
Winson Chung5aaab772012-04-27 15:24:02 -0700824 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800825 return;
826 }
827
Sunny Goyalff572272014-07-23 13:58:07 -0700828 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
829 if (resultCode == RESULT_OK) {
830 // Update the widget view.
831 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
832 pendingAddWidgetId, mPendingAddInfo);
833 if (workspaceLocked) {
834 sPendingAddItem = args;
835 } else {
836 completeAdd(args);
837 }
838 }
839 // Leave the widget in the pending state if the user canceled the configure.
840 return;
841 }
842
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800843 // The pattern used here is that a user PICKs a specific application,
844 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700845
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800846 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
847 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700848 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700849 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
850 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800851 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700852 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800853 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700854 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700855 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
856 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 }
Adam Cohen00074722013-10-11 17:46:09 -0700858 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700859 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700860 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800861 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800862 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800863
864 }
865
866 @Override
867 protected void onActivityResult(
868 final int requestCode, final int resultCode, final Intent data) {
869 handleActivityResult(requestCode, resultCode, data);
870 if (mLauncherCallbacks != null) {
871 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
872 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800873 }
874
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600875 /** @Override for MNC */
Sunny Goyaldd2e6df2015-07-07 10:57:57 -0700876 public void onRequestPermissionsResult(int requestCode, String[] permissions,
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600877 int[] grantResults) {
Sunny Goyal28c6b962015-10-12 11:42:05 -0700878 if (requestCode == REQUEST_PERMISSION_CALL_PHONE && sPendingAddItem != null
879 && sPendingAddItem.requestCode == REQUEST_PERMISSION_CALL_PHONE) {
880 View v = null;
881 CellLayout layout = getCellLayout(sPendingAddItem.container, sPendingAddItem.screenId);
882 if (layout != null) {
883 v = layout.getChildAt(sPendingAddItem.cellX, sPendingAddItem.cellY);
884 }
885 Intent intent = sPendingAddItem.intent;
886 sPendingAddItem = null;
887 if (grantResults.length > 0
888 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
889 startActivity(v, intent, null);
890 } else {
891 // TODO: Show a snack bar with link to settings
892 Toast.makeText(this, getString(R.string.msg_no_phone_permission,
893 getString(R.string.app_name)), Toast.LENGTH_SHORT).show();
894 }
895 }
Dave Hawkey3a43ed62015-06-26 10:27:47 -0600896 if (mLauncherCallbacks != null) {
897 mLauncherCallbacks.onRequestPermissionsResult(requestCode, permissions,
898 grantResults);
899 }
900 }
901
Adam Cohendb364c32014-05-20 14:23:40 -0700902 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
903 appWidgetId, ItemInfo info) {
904 PendingAddArguments args = new PendingAddArguments();
905 args.requestCode = requestCode;
906 args.intent = data;
907 args.container = info.container;
908 args.screenId = info.screenId;
909 args.cellX = info.cellX;
910 args.cellY = info.cellY;
911 args.appWidgetId = appWidgetId;
912 return args;
913 }
914
915 /**
916 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
917 *
918 * @param screenId the screen id to check
919 * @return the new screen, or screenId if it exists
920 */
921 private long ensurePendingDropLayoutExists(long screenId) {
922 CellLayout dropLayout =
923 (CellLayout) mWorkspace.getScreenWithId(screenId);
924 if (dropLayout == null) {
925 // it's possible that the add screen was removed because it was
926 // empty and a re-bind occurred
927 mWorkspace.addExtraEmptyScreen();
928 return mWorkspace.commitExtraEmptyScreen();
929 } else {
930 return screenId;
931 }
932 }
933
Adam Cohen091440a2015-03-18 14:16:05 -0700934 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700935 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700936 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800937 Runnable onCompleteRunnable = null;
938 int animationType = 0;
939
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700940 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 if (resultCode == RESULT_OK) {
942 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
943 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700944 mPendingAddWidgetInfo);
945 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800946 onCompleteRunnable = new Runnable() {
947 @Override
948 public void run() {
949 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700950 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700951 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
952 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800953 }
954 };
955 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800956 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800957 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800958 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700959 if (mDragLayer.getAnimatedView() != null) {
960 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
961 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
962 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700963 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700964 // The animated view may be null in the case of a rotation during widget configuration
965 onCompleteRunnable.run();
966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800967 }
968
969 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700970 protected void onStop() {
971 super.onStop();
972 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700973
974 if (mLauncherCallbacks != null) {
975 mLauncherCallbacks.onStop();
976 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700977 }
978
979 @Override
980 protected void onStart() {
981 super.onStart();
982 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700983
984 if (mLauncherCallbacks != null) {
985 mLauncherCallbacks.onStart();
986 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700987 }
988
989 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800990 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200991 long startTime = 0;
992 if (DEBUG_RESUME_TIME) {
993 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400994 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200995 }
Adam Cohen9211d422014-10-07 18:14:53 -0700996
997 if (mLauncherCallbacks != null) {
998 mLauncherCallbacks.preOnResume();
999 }
1000
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001001 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001002
Winson Chung4a2afa32012-07-19 14:53:05 -07001003 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001004 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001005 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001006 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001007 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001008 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001009 // view after launching an app, as they may be depending on the UI to be static to
1010 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001011 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07001012 !launchedFromApp /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08001013 } else if (mOnResumeState == State.WIDGETS) {
1014 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001015 }
1016 mOnResumeState = State.NONE;
1017
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001018 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001019 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1020 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001021
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001022 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001023 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001024 setWorkspaceLoading(true);
Sunny Goyal22aa3812015-08-19 16:24:27 -07001025
1026 // If we're starting binding all over again, clear any bind calls we'd postponed in
1027 // the past (see waitUntilResume) -- we don't need them since we're starting binding
1028 // from scratch again
1029 mBindOnResumeCallbacks.clear();
1030
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001031 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001032 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001033 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001035 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001036 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1037 // execute them here
1038 long startTimeCallbacks = 0;
1039 if (DEBUG_RESUME_TIME) {
1040 startTimeCallbacks = System.currentTimeMillis();
1041 }
1042
Michael Jurka1e2f4652013-07-08 18:03:46 -07001043 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1044 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001045 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001046 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001047 if (DEBUG_RESUME_TIME) {
1048 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1049 (System.currentTimeMillis() - startTimeCallbacks));
1050 }
Michael Jurka447bf842013-05-15 14:52:15 +02001051 }
Michael Jurka54554252013-08-01 12:52:23 +02001052 if (mOnResumeCallbacks.size() > 0) {
1053 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1054 mOnResumeCallbacks.get(i).run();
1055 }
1056 mOnResumeCallbacks.clear();
1057 }
Winson Chunge4e50662012-01-23 14:45:13 -08001058
1059 // Reset the pressed state of icons that were locked in the press state while activities
1060 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001061 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001062 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001063 mWaitingForResume.setStayPressed(false);
1064 }
Winson Chung82963d52013-10-09 11:20:57 -07001065
Adam Cohen06dff352012-06-01 17:17:08 -07001066 // It is possible that widgets can receive updates while launcher is not in the foreground.
1067 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1068 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1069 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001070 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001071 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001072
Michael Jurka447bf842013-05-15 14:52:15 +02001073 if (DEBUG_RESUME_TIME) {
1074 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1075 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001076
Adam Cohen4b66bf32015-09-18 12:15:19 -07001077 // We want to suppress callbacks about CustomContent being shown if we have just received
1078 // onNewIntent while the user was present within launcher. In that case, we post a call
1079 // to move the user to the main screen (which will occur after onResume). We don't want to
1080 // have onHide (from onPause), then onShow, then onHide again, which we get if we don't
1081 // suppress here.
1082 if (mWorkspace.getCustomContentCallbacks() != null
1083 && !mMoveToDefaultScreenFromNewIntent) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001084 // If we are resuming and the custom content is the current page, we call onShow().
Adam Cohen4b66bf32015-09-18 12:15:19 -07001085 // It is also possible that onShow will instead be called slightly after first layout
Adam Cohen6fecd412013-10-02 17:41:50 -07001086 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1087 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001088 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001089 }
1090 }
Adam Cohen4b66bf32015-09-18 12:15:19 -07001091 mMoveToDefaultScreenFromNewIntent = false;
Winson Chungcd99cd32015-04-29 11:03:24 -07001092 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001093 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001094
Sunny Goyal756adbc2015-04-16 15:20:51 -07001095 if (!isWorkspaceLoading()) {
1096 // Process any items that were added while Launcher was away.
1097 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1098 }
Adam Cohen9211d422014-10-07 18:14:53 -07001099
1100 if (mLauncherCallbacks != null) {
1101 mLauncherCallbacks.onResume();
1102 }
Adam Cohen06dff352012-06-01 17:17:08 -07001103 }
1104
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001105 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001106 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001107 // Ensure that items added to Launcher are queued until Launcher returns
1108 InstallShortcutReceiver.enableInstallQueue();
1109
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001110 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001111 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001112 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001113 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001114
1115 // We call onHide() aggressively. The custom content callbacks should be able to
1116 // debounce excess onHide calls.
1117 if (mWorkspace.getCustomContentCallbacks() != null) {
1118 mWorkspace.getCustomContentCallbacks().onHide();
1119 }
Romain Guycbb89e42009-06-08 15:52:54 -07001120
Adam Cohen9211d422014-10-07 18:14:53 -07001121 if (mLauncherCallbacks != null) {
1122 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001123 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001124 }
1125
1126 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001127 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1128 // by a onResume or by scrolling otherwise.
1129 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001130
1131 // Custom content is completely hidden
1132 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001133
1134 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1135 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001136
1137 // Indicates whether the user is allowed to scroll away from the custom content.
1138 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001139 }
1140
Adam Cohenc2d6e892014-10-16 09:49:24 -07001141 public interface LauncherOverlay {
1142
1143 /**
1144 * Touch interaction leading to overscroll has begun
1145 */
1146 public void onScrollInteractionBegin();
1147
1148 /**
1149 * Touch interaction related to overscroll has ended
1150 */
1151 public void onScrollInteractionEnd();
1152
1153 /**
1154 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1155 * screen (or in the case of RTL, the rightmost screen).
1156 */
1157 public void onScrollChange(int progress, boolean rtl);
1158
1159 /**
1160 * Screen has stopped scrolling
1161 */
1162 public void onScrollSettled();
1163
1164 /**
1165 * This method can be called by the Launcher in order to force the LauncherOverlay
1166 * to exit fully immersive mode.
1167 */
1168 public void forceExitFullImmersion();
1169 }
1170
Jun Mukai8af0cd82015-05-12 12:32:12 -07001171 public interface LauncherSearchCallbacks {
1172 /**
1173 * Called when the search overlay is shown.
1174 */
1175 public void onSearchOverlayOpened();
1176
1177 /**
1178 * Called when the search overlay is dismissed.
1179 */
1180 public void onSearchOverlayClosed();
1181 }
1182
Adam Cohenc2d6e892014-10-16 09:49:24 -07001183 public interface LauncherOverlayCallbacks {
1184 /**
1185 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1186 * however it doesn't modify any state within the launcher.
1187 */
1188 public boolean canEnterFullImmersion();
1189
1190 /**
1191 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1192 * eg. by occupying the full screen and handling all touch events.
1193 *
1194 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1195 * case, Launcher will modify any necessary state and assumes the overlay is
1196 * handling all interaction. If false, the LauncherOverlay should cancel any
1197 *
1198 */
1199 public boolean enterFullImmersion();
1200
1201 /**
1202 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1203 * full control over UI and state.
1204 */
1205 public void exitFullImmersion();
1206 }
1207
1208 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1209
1210 @Override
1211 public boolean canEnterFullImmersion() {
1212 return mState == State.WORKSPACE;
1213 }
1214
1215 @Override
1216 public boolean enterFullImmersion() {
1217 if (mState == State.WORKSPACE) {
1218 // When fully immersed, disregard any touches which fall through.
1219 mDragLayer.setBlockTouch(true);
1220 return true;
1221 }
1222 return false;
1223 }
1224
1225 @Override
1226 public void exitFullImmersion() {
1227 mDragLayer.setBlockTouch(false);
1228 }
1229 }
1230
Jorim Jaggid017f882014-01-14 17:08:48 -08001231 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001232 if (mLauncherCallbacks != null) {
1233 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001234 } else {
1235 // On devices with a locked orientation, we will at least have the allow rotation
1236 // setting.
1237 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001238 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001239 }
1240
Adam Cohen9211d422014-10-07 18:14:53 -07001241 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001242 CustomContentCallbacks callbacks, String description) {
1243 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001244 }
1245
Adam Cohenedb40762013-07-18 16:45:45 -07001246 // The custom content needs to offset its content to account for the QSB
1247 public int getTopOffsetForCustomContent() {
1248 return mWorkspace.getPaddingTop();
1249 }
1250
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001251 @Override
1252 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001253 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001254 if (mModel.isCurrentCallbacks(this)) {
1255 mModel.stopLoader();
1256 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001257 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1258
Romain Guy13c2e7b2010-03-10 19:45:00 -08001259 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001260 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001261
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001262 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001263 @Override
1264 public void onWindowFocusChanged(boolean hasFocus) {
1265 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001266 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001267
1268 if (mLauncherCallbacks != null) {
1269 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1270 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001271 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 private boolean acceptFilter() {
1274 final InputMethodManager inputManager = (InputMethodManager)
1275 getSystemService(Context.INPUT_METHOD_SERVICE);
1276 return !inputManager.isFullscreenMode();
1277 }
1278
1279 @Override
1280 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001281 final int uniChar = event.getUnicodeChar();
1282 final boolean handled = super.onKeyDown(keyCode, event);
1283 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1284 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1286 keyCode, event);
1287 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001288 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001289 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001290 // showSearchDialog()
1291 // If there are multiple keystrokes before the search dialog takes focus,
1292 // onSearchRequested() will be called for every keystroke,
1293 // but it is idempotent, so it's fine.
1294 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 }
1296 }
1297
Joe Onorato8a9625e2010-01-28 15:55:35 -08001298 // Eat the long press event so the keyboard doesn't come up.
1299 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1300 return true;
1301 }
1302
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 return handled;
1304 }
1305
Karl Rosaen138a0412009-04-23 19:00:21 -07001306 private String getTypedText() {
1307 return mDefaultKeySsb.toString();
1308 }
1309
1310 private void clearTypedText() {
1311 mDefaultKeySsb.clear();
1312 mDefaultKeySsb.clearSpans();
1313 Selection.setSelection(mDefaultKeySsb, 0);
1314 }
1315
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001317 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1318 * State
1319 */
1320 private static State intToState(int stateOrdinal) {
1321 State state = State.WORKSPACE;
1322 final State[] stateValues = State.values();
1323 for (int i = 0; i < stateValues.length; i++) {
1324 if (stateValues[i].ordinal() == stateOrdinal) {
1325 state = stateValues[i];
1326 break;
1327 }
1328 }
1329 return state;
1330 }
1331
1332 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 * Restores the previous state, if it exists.
1334 *
1335 * @param savedState The previous state.
1336 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001337 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 private void restoreState(Bundle savedState) {
1339 if (savedState == null) {
1340 return;
1341 }
1342
Michael Jurka883f55b2010-10-21 15:47:14 -07001343 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001344 if (state == State.APPS || state == State.WIDGETS) {
1345 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001346 }
1347
Adam Cohen21cd0022013-10-09 18:57:02 -07001348 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1349 PagedView.INVALID_RESTORE_PAGE);
1350 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001351 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 }
1353
Winson Chung3d503fb2011-07-13 17:25:49 -07001354 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001355 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001356
Winson Chung3d503fb2011-07-13 17:25:49 -07001357 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1358 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001359 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001360 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1361 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001362 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1363 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001364 AppWidgetProviderInfo info = savedState.getParcelable(
1365 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001366 mPendingAddWidgetInfo = info == null ?
1367 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1368
Adam Cohen4637b5a2013-11-04 18:21:24 -08001369 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001370 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 mRestoring = true;
1372 }
1373
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001374 mItemIdToViewId = (HashMap<Integer, Integer>)
1375 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 }
1377
1378 /**
1379 * Finds all the views we need and configure them properly.
1380 */
1381 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001382 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001383
Craig Mautner360310b2012-10-26 15:13:08 -07001384 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001385 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001386 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1387 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001388 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001389 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001390
John Spurlock77e1f472013-09-11 10:09:51 -04001391 mLauncherView.setSystemUiVisibility(
1392 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001393 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394
Winson Chung4c98d922011-05-31 16:50:48 -07001395 // Setup the drag layer
1396 mDragLayer.setup(this, dragController);
1397
Winson Chung3d503fb2011-07-13 17:25:49 -07001398 // Setup the hotseat
1399 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1400 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001401 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001402 }
1403
Jorim Jaggid017f882014-01-14 17:08:48 -08001404 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001405 mWidgetsButton = findViewById(R.id.widget_button);
1406 mWidgetsButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001407 @Override
1408 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001409 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001410 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001411 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001412 }
1413 });
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001414 mWidgetsButton.setOnTouchListener(getHapticFeedbackTouchListener());
Adam Cohen61f560d2013-09-30 15:58:20 -07001415
1416 View wallpaperButton = findViewById(R.id.wallpaper_button);
1417 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001418 @Override
1419 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001420 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001421 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001422 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001423 }
1424 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001425 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1426
1427 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001428 if (hasSettings()) {
1429 settingsButton.setOnClickListener(new OnClickListener() {
1430 @Override
1431 public void onClick(View arg0) {
1432 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001433 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001434 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001435 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001436 });
1437 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1438 } else {
1439 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001440 }
1441
Adam Cohendbdff6b2013-09-18 19:09:15 -07001442 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001443
Winson Chung4c98d922011-05-31 16:50:48 -07001444 // Setup the workspace
1445 mWorkspace.setHapticFeedbackEnabled(false);
1446 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001447 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001448 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001449
Winson Chungf0ea4d32011-06-06 14:27:16 -07001450 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001451 mSearchDropTargetBar = (SearchDropTargetBar)
1452 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001453
Winson Chungef7f8742015-06-04 17:18:17 -07001454 // Setup Apps and Widgets
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001455 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Hyunyoung Song3f471442015-04-08 19:01:34 -07001456 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Winson Chungef7f8742015-06-04 17:18:17 -07001457 if (mLauncherCallbacks != null && mLauncherCallbacks.getAllAppsSearchBarController() != null) {
1458 mAppsView.setSearchBarController(mLauncherCallbacks.getAllAppsSearchBarController());
1459 } else {
1460 mAppsView.setSearchBarController(mAppsView.newDefaultAppSearchController());
1461 }
Craig Mautner360310b2012-10-26 15:13:08 -07001462
Winson Chung3d503fb2011-07-13 17:25:49 -07001463 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001464 dragController.setDragScoller(mWorkspace);
1465 dragController.setScrollView(mDragLayer);
1466 dragController.setMoveTarget(mWorkspace);
1467 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001468 if (mSearchDropTargetBar != null) {
1469 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001470 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001471 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001472
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001473 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001474 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001475 mWeightWatcher = new WeightWatcher(this);
1476 mWeightWatcher.setAlpha(0.5f);
1477 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001478 new FrameLayout.LayoutParams(
1479 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001480 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001481 Gravity.BOTTOM)
1482 );
Adam Cohen39a06042013-07-19 14:30:12 -07001483
1484 boolean show = shouldShowWeightWatcher();
1485 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001486 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 }
1488
1489 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001490 * Sets the all apps button. This method is called from {@link Hotseat}.
1491 */
1492 public void setAllAppsButton(View allAppsButton) {
1493 mAllAppsButton = allAppsButton;
1494 }
1495
1496 public View getAllAppsButton() {
1497 return mAllAppsButton;
1498 }
1499
Hyunyoung Song98ff38a2015-07-10 17:50:13 -07001500 public View getWidgetsButton() {
1501 return mWidgetsButton;
1502 }
1503
Anjali Koppal5ad44842014-03-10 20:34:39 -07001504 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001505 * Creates a view representing a shortcut.
1506 *
1507 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001509 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001510 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511 }
1512
1513 /**
1514 * Creates a view representing a shortcut inflated from the specified resource.
1515 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001516 * @param parent The group the shortcut belongs to.
1517 * @param info The data structure describing the shortcut.
1518 *
1519 * @return A View inflated from layoutResId.
1520 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001521 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001522 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001523 parent, false);
1524 favorite.applyFromShortcutInfo(info, mIconCache);
1525 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001527 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 return favorite;
1529 }
1530
1531 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 * Add a shortcut to the workspace.
1533 *
1534 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001536 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001537 int cellY) {
1538 int[] cellXY = mTmpAddItemCellCoordinates;
1539 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001540 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001541
Sunny Goyal5c97f512015-05-19 16:03:28 -07001542 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001543 if (info == null) {
1544 return;
1545 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001546 final View view = createShortcut(info);
1547
Sunny Goyal5c97f512015-05-19 16:03:28 -07001548 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001549 // First we check if we already know the exact location where we want to add this item.
1550 if (cellX >= 0 && cellY >= 0) {
1551 cellXY[0] = cellX;
1552 cellXY[1] = cellY;
1553 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001554
1555 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001556 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001557 true, null,null)) {
1558 return;
1559 }
1560 DragObject dragObject = new DragObject();
1561 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001562 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1563 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001564 return;
1565 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001566 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001567 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001568 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001569 foundCellSpan = (result != null);
1570 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001571 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001572 }
1573
1574 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001575 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001576 return;
1577 }
1578
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001579 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580
1581 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001582 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001583 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001584 }
1585 }
1586
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001588 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001590 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001591 */
Adam Cohen091440a2015-03-18 14:16:05 -07001592 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001593 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1594
1595 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001596 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001597 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1598 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001599 }
Romain Guycbb89e42009-06-08 15:52:54 -07001600
Adam Cohen59400422014-03-05 18:07:04 -08001601 if (appWidgetInfo.isCustomWidget) {
1602 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001603 }
1604
Adam Cohen59400422014-03-05 18:07:04 -08001605 LauncherAppWidgetInfo launcherInfo;
1606 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1607 launcherInfo.spanX = info.spanX;
1608 launcherInfo.spanY = info.spanY;
1609 launcherInfo.minSpanX = info.minSpanX;
1610 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001611 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001612
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001613 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001614 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001615
1616 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001617 if (hostView == null) {
1618 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001619 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1620 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001621 } else {
1622 // The AppWidgetHostView has already been inflated and instantiated
1623 launcherInfo.hostView = hostView;
1624 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001626 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001627 launcherInfo.notifyWidgetSizeChanged(this);
1628
Adam Cohen59400422014-03-05 18:07:04 -08001629 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1630 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001631
1632 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001634 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 }
Romain Guycbb89e42009-06-08 15:52:54 -07001636
Adam Cohended9f8d2010-11-03 13:25:16 -07001637 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1638 @Override
1639 public void onReceive(Context context, Intent intent) {
1640 final String action = intent.getAction();
1641 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1642 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001643 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001644 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001645
Winson Chungc100e8e2011-08-09 16:02:43 -07001646 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001647 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001648 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001649 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001650 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001651 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001652 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1653 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001654 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001655 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1656 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001657 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001658 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1659 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1660 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001661 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001662 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1663 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001664 }
1665 }
1666 };
1667
1668 @Override
1669 public void onAttachedToWindow() {
1670 super.onAttachedToWindow();
1671
1672 // Listen for broadcasts related to user-presence
1673 final IntentFilter filter = new IntentFilter();
1674 filter.addAction(Intent.ACTION_SCREEN_OFF);
1675 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001676 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001677 if (ENABLE_DEBUG_INTENTS) {
1678 filter.addAction(DebugIntents.DELETE_DATABASE);
1679 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1680 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001681 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001682 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001683 setupTransparentSystemBarsForLollipop();
Adam Cohend113e0c2010-11-11 10:48:05 -08001684 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001685 mVisible = true;
1686 }
1687
Adam Cohen0b395352014-06-09 22:54:36 +00001688 /**
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001689 * Sets up transparent navigation and status bars in Lollipop.
Adam Cohen0b395352014-06-09 22:54:36 +00001690 * This method is a no-op for other platform versions.
1691 */
Sunny Goyald0091012015-01-20 15:55:34 -08001692 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Sunny Goyal9fc953b2015-08-17 12:24:25 -07001693 private void setupTransparentSystemBarsForLollipop() {
1694 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyald0091012015-01-20 15:55:34 -08001695 Window window = getWindow();
1696 window.getAttributes().systemUiVisibility |=
1697 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1698 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1699 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1700 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1701 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1702 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1703 window.setStatusBarColor(Color.TRANSPARENT);
1704 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001705 }
1706 }
1707
Adam Cohended9f8d2010-11-03 13:25:16 -07001708 @Override
1709 public void onDetachedFromWindow() {
1710 super.onDetachedFromWindow();
1711 mVisible = false;
1712
Adam Cohend113e0c2010-11-11 10:48:05 -08001713 if (mAttached) {
1714 unregisterReceiver(mReceiver);
1715 mAttached = false;
1716 }
Winson Chungb745afb2015-03-02 11:51:23 -08001717 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001718 }
1719
1720 public void onWindowVisibilityChanged(int visibility) {
1721 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001722 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001723 // The following code used to be in onResume, but it turns out onResume is called when
1724 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1725 // is a more appropriate event to handle
1726 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001727 if (!mWorkspaceLoading) {
1728 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001729 // We want to let Launcher draw itself at least once before we force it to build
1730 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001731 // apps is nice and speedy.
1732 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001733 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001734 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001735 if (mStarted) return;
1736 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001737 // We delay the layer building a bit in order to give
1738 // other message processing a time to run. In particular
1739 // this avoids a delay in hiding the IME if it was
1740 // currently shown, because doing that may involve
1741 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001742 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001743 final ViewTreeObserver.OnDrawListener listener = this;
1744 mWorkspace.post(new Runnable() {
1745 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001746 if (mWorkspace != null &&
1747 mWorkspace.getViewTreeObserver() != null) {
1748 mWorkspace.getViewTreeObserver().
1749 removeOnDrawListener(listener);
1750 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001751 }
1752 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001753 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001754 }
1755 });
1756 }
1757 clearTypedText();
1758 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 }
1760
Adam Cohen091440a2015-03-18 14:16:05 -07001761 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001762 mHandler.removeMessages(ADVANCE_MSG);
1763 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1764 mHandler.sendMessageDelayed(msg, delay);
1765 mAutoAdvanceSentTime = System.currentTimeMillis();
1766 }
1767
Adam Cohen091440a2015-03-18 14:16:05 -07001768 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001769 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1770 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1771 mAutoAdvanceRunning = autoAdvanceRunning;
1772 if (autoAdvanceRunning) {
1773 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1774 sendAdvanceMessage(delay);
1775 } else {
1776 if (!mWidgetsToAdvance.isEmpty()) {
1777 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1778 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1779 }
1780 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001781 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001782 }
1783 }
1784 }
1785
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001786 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1787
Adam Cohended9f8d2010-11-03 13:25:16 -07001788 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001789 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001790 if (msg.what == ADVANCE_MSG) {
1791 int i = 0;
1792 for (View key: mWidgetsToAdvance.keySet()) {
1793 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1794 final int delay = mAdvanceStagger * i;
1795 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001796 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001797 public void run() {
1798 ((Advanceable) v).advance();
1799 }
1800 }, delay);
1801 }
1802 i++;
1803 }
1804 sendAdvanceMessage(mAdvanceInterval);
1805 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001806 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001807 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001808 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001809
1810 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001811 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001812 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1813 if (v instanceof Advanceable) {
1814 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001815 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001816 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001817 }
1818 }
1819
1820 void removeWidgetToAutoAdvance(View hostView) {
1821 if (mWidgetsToAdvance.containsKey(hostView)) {
1822 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001823 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001824 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001825 }
1826
Joe Onorato9c1289c2009-08-17 11:03:03 -04001827 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001828 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001829 launcherInfo.hostView = null;
1830 }
1831
Hyunyoung Song3f471442015-04-08 19:01:34 -07001832 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001833 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1834 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001835 }
1836
Winson Chunga6945242014-01-08 14:04:34 -08001837 public DragLayer getDragLayer() {
1838 return mDragLayer;
1839 }
1840
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001841 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001842 return mAppsView;
1843 }
1844
Hyunyoung Song3f471442015-04-08 19:01:34 -07001845 public WidgetsContainerView getWidgetsView() {
1846 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001847 }
1848
Winson Chunga6945242014-01-08 14:04:34 -08001849 public Workspace getWorkspace() {
1850 return mWorkspace;
1851 }
1852
1853 public Hotseat getHotseat() {
1854 return mHotseat;
1855 }
1856
Jorim Jaggid017f882014-01-14 17:08:48 -08001857 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001858 return mOverviewPanel;
1859 }
1860
Winson Chung006ee262015-08-03 14:40:11 -07001861 public SearchDropTargetBar getSearchDropTargetBar() {
Winson Chunga6945242014-01-08 14:04:34 -08001862 return mSearchDropTargetBar;
1863 }
1864
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001865 public LauncherAppWidgetHost getAppWidgetHost() {
1866 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001867 }
Romain Guycbb89e42009-06-08 15:52:54 -07001868
Winson Chunga9abd0e2010-10-27 17:18:37 -07001869 public LauncherModel getModel() {
1870 return mModel;
1871 }
1872
Winson Chunga6945242014-01-08 14:04:34 -08001873 protected SharedPreferences getSharedPrefs() {
1874 return mSharedPrefs;
1875 }
1876
Adam Cohen2e6da152015-05-06 11:42:25 -07001877 public DeviceProfile getDeviceProfile() {
1878 return mDeviceProfile;
1879 }
1880
Bjorn Bringertc459e522013-06-07 19:36:01 +01001881 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001882 getWindow().closeAllPanels();
1883
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001884 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001885 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001886 }
1887
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001888 @Override
1889 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001890 long startTime = 0;
1891 if (DEBUG_RESUME_TIME) {
1892 startTime = System.currentTimeMillis();
1893 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001894 super.onNewIntent(intent);
1895
1896 // Close the menu
Winson15f8b172015-08-19 11:02:39 -07001897 Folder openFolder = mWorkspace.getOpenFolder();
1898 boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1899 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1900 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
1901 boolean isActionMain = Intent.ACTION_MAIN.equals(intent.getAction());
1902 if (isActionMain) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001903 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001904 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001905
Adam Cohen6fecd412013-10-02 17:41:50 -07001906 if (mWorkspace == null) {
1907 // Can be cases where mWorkspace is null, this prevents a NPE
1908 return;
1909 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001910 // In all these cases, only animate if we're already on home
1911 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001912
Adam Cohen6fecd412013-10-02 17:41:50 -07001913 closeFolder();
1914 exitSpringLoadedDragMode();
1915
1916 // If we are already on home, then just animate back to the workspace,
1917 // otherwise, just wait until onResume to set the state back to Workspace
1918 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001919 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001920 } else {
1921 mOnResumeState = State.WORKSPACE;
1922 }
1923
1924 final View v = getWindow().peekDecorView();
1925 if (v != null && v.getWindowToken() != null) {
1926 InputMethodManager imm = (InputMethodManager)getSystemService(
1927 INPUT_METHOD_SERVICE);
1928 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1929 }
1930
Winson Chungb745afb2015-03-02 11:51:23 -08001931 // Reset the apps view
1932 if (!alreadyOnHome && mAppsView != null) {
1933 mAppsView.scrollToTop();
1934 }
1935
Hyunyoung Song3f471442015-04-08 19:01:34 -07001936 // Reset the widgets view
1937 if (!alreadyOnHome && mWidgetsView != null) {
1938 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001939 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001940
Adam Cohen9211d422014-10-07 18:14:53 -07001941 if (mLauncherCallbacks != null) {
1942 mLauncherCallbacks.onHomeIntent();
1943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 }
Adam Cohened307df2013-10-02 09:37:31 -07001945
Adam Cohen9211d422014-10-07 18:14:53 -07001946 if (mLauncherCallbacks != null) {
1947 mLauncherCallbacks.onNewIntent(intent);
1948 }
Winson15f8b172015-08-19 11:02:39 -07001949
1950 // Defer moving to the default screen until after we callback to the LauncherCallbacks
1951 // as slow logic in the callbacks eat into the time the scroller expects for the snapToPage
1952 // animation.
1953 if (isActionMain) {
Ivan Lee667d6882015-09-03 10:16:07 -06001954 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1955 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Winson15f8b172015-08-19 11:02:39 -07001956 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
1957 openFolder == null && moveToDefaultScreen) {
Adam Cohen4b66bf32015-09-18 12:15:19 -07001958
1959 // We use this flag to suppress noisy callbacks above custom content state
1960 // from onResume.
1961 mMoveToDefaultScreenFromNewIntent = true;
Winson15f8b172015-08-19 11:02:39 -07001962 mWorkspace.post(new Runnable() {
1963 @Override
1964 public void run() {
1965 mWorkspace.moveToDefaultScreen(true);
1966 }
1967 });
1968 }
1969 }
1970
1971 if (DEBUG_RESUME_TIME) {
1972 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1973 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001974 }
1975
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001976 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001977 public void onRestoreInstanceState(Bundle state) {
1978 super.onRestoreInstanceState(state);
1979 for (int page: mSynchronouslyBoundPages) {
1980 mWorkspace.restoreInstanceStateForChild(page);
1981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001982 }
1983
1984 @Override
1985 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001986 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001987 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1988 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001989 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001990 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001991
Michael Jurka883f55b2010-10-21 15:47:14 -07001992 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001993 // We close any open folder since it will not be re-opened, and we need to make sure
1994 // this state is reflected.
1995 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996
Adam Cohendcd297f2013-06-18 13:13:40 -07001997 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001998 mWaitingForResult) {
1999 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002000 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002001 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2002 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002003 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2004 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2005 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002006 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 }
2008
Hyunyoung Song3f471442015-04-08 19:01:34 -07002009 // Save the current widgets tray?
2010 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002011 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002012
2013 if (mLauncherCallbacks != null) {
2014 mLauncherCallbacks.onSaveInstanceState(outState);
2015 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002016 }
2017
2018 @Override
2019 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002020 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002021
Winson Chunge7a03942011-08-05 15:05:12 -07002022 // Remove all pending runnables
2023 mHandler.removeMessages(ADVANCE_MSG);
2024 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002025 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002026
Winson Chungcd2b0142011-06-08 16:02:26 -07002027 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002028 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002029
2030 // It's possible to receive onDestroy after a new Launcher activity has
2031 // been created. In this case, don't interfere with the new Launcher.
2032 if (mModel.isCurrentCallbacks(this)) {
2033 mModel.stopLoader();
2034 app.setLauncher(null);
2035 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002036
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002038 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002039 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002040 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002042 mAppWidgetHost = null;
2043
2044 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045
2046 TextKeyListener.getInstance().release();
2047
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002048 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002049
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002050 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002051 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002052 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002053 mWorkspace = null;
2054 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002055
Michael Jurka2ecf9952012-06-18 12:52:28 -07002056 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002057
2058 if (mLauncherCallbacks != null) {
2059 mLauncherCallbacks.onDestroy();
2060 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002061 }
2062
Adam Cohena9cf38f2011-05-02 15:36:58 -07002063 public DragController getDragController() {
2064 return mDragController;
2065 }
2066
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 @Override
2068 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002069 onStartForResult(requestCode);
2070 super.startActivityForResult(intent, requestCode);
2071 }
2072
2073 @Override
2074 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2075 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2076 onStartForResult(requestCode);
2077 try {
2078 super.startIntentSenderForResult(intent, requestCode,
2079 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2080 } catch (IntentSender.SendIntentException e) {
2081 throw new ActivityNotFoundException();
2082 }
2083 }
2084
2085 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002086 if (requestCode >= 0) {
2087 setWaitingForResult(true);
2088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 }
2090
Winson Chung70d72102011-08-12 11:24:35 -07002091 /**
2092 * Indicates that we want global search for this activity by setting the globalSearch
2093 * argument for {@link #startSearch} to true.
2094 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002096 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002098
Karl Rosaen138a0412009-04-23 19:00:21 -07002099 if (initialQuery == null) {
2100 // Use any text typed in the launcher as the initial query
2101 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002102 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 if (appSearchData == null) {
2104 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002105 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 }
Winson Chungadf0c182012-08-23 14:59:07 -07002107 Rect sourceBounds = new Rect();
2108 if (mSearchDropTargetBar != null) {
2109 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2110 }
Romain Guycbb89e42009-06-08 15:52:54 -07002111
Ian Parkinson047036e2014-09-01 15:40:53 +01002112 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002113 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002114 if (clearTextImmediately) {
2115 clearTypedText();
2116 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002117
2118 // We need to show the workspace after starting the search
2119 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002120 }
2121
Ian Parkinson047036e2014-09-01 15:40:53 +01002122 /**
2123 * Start a text search.
2124 *
2125 * @return {@code true} if the search will start immediately, so any further keypresses
2126 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2127 * to buffer keypresses.
2128 */
2129 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002130 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002131 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2132 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2133 sourceBounds);
2134 }
2135
Michael Jurkaa33411c2012-06-14 16:18:21 -07002136 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002137 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002138 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002139 }
2140
2141 /**
2142 * Starts the global search activity. This code is a copied from SearchManager
2143 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002144 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002145 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002146 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002147 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2148 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2149 if (globalSearchActivity == null) {
2150 Log.w(TAG, "No global search activity found.");
2151 return;
2152 }
2153 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2154 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2155 intent.setComponent(globalSearchActivity);
2156 // Make sure that we have a Bundle to put source in
2157 if (appSearchData == null) {
2158 appSearchData = new Bundle();
2159 } else {
2160 appSearchData = new Bundle(appSearchData);
2161 }
Adam Cohen9211d422014-10-07 18:14:53 -07002162 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002163 if (!appSearchData.containsKey("source")) {
2164 appSearchData.putString("source", getPackageName());
2165 }
2166 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2167 if (!TextUtils.isEmpty(initialQuery)) {
2168 intent.putExtra(SearchManager.QUERY, initialQuery);
2169 }
2170 if (selectInitialQuery) {
2171 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2172 }
2173 intent.setSourceBounds(sourceBounds);
2174 try {
2175 startActivity(intent);
2176 } catch (ActivityNotFoundException ex) {
2177 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2178 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179 }
2180
Winson Chungbedf9232015-07-10 12:38:30 -07002181 public void startSearchFromAllApps(View v, Intent searchIntent, String searchQuery) {
2182 if (mLauncherCallbacks != null && mLauncherCallbacks.startSearchFromAllApps(searchQuery)) {
2183 return;
2184 }
2185
2186 // If not handled, then just start the provided search intent
2187 startActivitySafely(v, searchIntent, null);
2188 }
2189
Yura4f93ec62014-02-04 14:15:21 +00002190 public boolean isOnCustomContent() {
2191 return mWorkspace.isOnOrMovingToCustomContent();
2192 }
2193
Winson Chung70d72102011-08-12 11:24:35 -07002194 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002195 public boolean onPrepareOptionsMenu(Menu menu) {
2196 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002197 if (!isOnCustomContent()) {
2198 // Close any open folders
2199 closeFolder();
2200 // Stop resizing any widgets
2201 mWorkspace.exitWidgetResizeMode();
2202 if (!mWorkspace.isInOverviewMode()) {
2203 // Show the overview mode
2204 showOverviewMode(true);
2205 } else {
2206 showWorkspace(true);
2207 }
Winson Chunge0298742014-01-17 12:03:00 -08002208 }
Adam Cohen9211d422014-10-07 18:14:53 -07002209 if (mLauncherCallbacks != null) {
2210 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2211 }
2212
Michael Jurkab94f3f82013-09-11 18:08:54 +02002213 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002214 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002215
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002216 @Override
2217 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002218 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002219 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002220 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002221 }
2222
Joe Onorato9c1289c2009-08-17 11:03:03 -04002223 public boolean isWorkspaceLocked() {
2224 return mWorkspaceLoading || mWaitingForResult;
2225 }
2226
Adam Cohen517a7f52014-03-01 12:12:59 -08002227 public boolean isWorkspaceLoading() {
2228 return mWorkspaceLoading;
2229 }
2230
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002231 private void setWorkspaceLoading(boolean value) {
2232 boolean isLocked = isWorkspaceLocked();
2233 mWorkspaceLoading = value;
2234 if (isLocked != isWorkspaceLocked()) {
2235 onWorkspaceLockedChanged();
2236 }
2237 }
2238
2239 private void setWaitingForResult(boolean value) {
2240 boolean isLocked = isWorkspaceLocked();
2241 mWaitingForResult = value;
2242 if (isLocked != isWorkspaceLocked()) {
2243 onWorkspaceLockedChanged();
2244 }
2245 }
2246
Adam Cohen9211d422014-10-07 18:14:53 -07002247 protected void onWorkspaceLockedChanged() {
2248 if (mLauncherCallbacks != null) {
2249 mLauncherCallbacks.onWorkspaceLockedChanged();
2250 }
2251 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002252
Michael Jurka0280c3b2010-09-17 15:00:07 -07002253 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002254 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002255 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002256 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2257 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Sunny Goyal233ee962015-08-03 13:05:01 -07002258 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = 1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002259 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002260 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002261
Sunny Goyale6b63a32015-01-16 16:14:29 -08002262 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002263 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002264 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2265 }
2266
Sunny Goyale6b63a32015-01-16 16:14:29 -08002267 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002268 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2269 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002270 if (appWidgetInfo.configure != null) {
2271 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002272 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002273
Bjorn Bringert7984c942009-12-09 15:38:25 +00002274 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002275 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2276 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2277
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002278 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002279 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002280 Runnable onComplete = new Runnable() {
2281 @Override
2282 public void run() {
2283 // Exit spring loaded mode if necessary after adding the widget
2284 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2285 null);
2286 }
2287 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002288 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002289 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002290 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002291 }
2292 }
Romain Guycbb89e42009-06-08 15:52:54 -07002293
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002294 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002295 // Close any folders that may be open.
2296 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002297 mWorkspace.moveToCustomContentScreen(animate);
2298 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002299
2300 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2301 int[] cell, int spanX, int spanY) {
2302 switch (info.itemType) {
2303 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2304 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2305 int span[] = new int[2];
2306 span[0] = spanX;
2307 span[1] = spanY;
2308 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2309 container, screenId, cell, span);
2310 break;
2311 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2312 processShortcutFromDrop(info.componentName, container, screenId, cell);
2313 break;
2314 default:
2315 throw new IllegalStateException("Unknown item type: " + info.itemType);
2316 }
2317 }
2318
Adam Cohenfbba09b2011-07-18 21:43:05 -07002319 /**
2320 * Process a shortcut drop.
2321 *
2322 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002323 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002324 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002325 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002326 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2327 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002328 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002329 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002330 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002331 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002332
2333 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002334 mPendingAddInfo.cellX = cell[0];
2335 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002336 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002337
2338 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2339 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002340 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002341 }
2342
Adam Cohenfbba09b2011-07-18 21:43:05 -07002343 /**
2344 * Process a widget drop.
2345 *
2346 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002347 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002348 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002349 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002350 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2351 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002352 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002353 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002354 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002355 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002356 mPendingAddInfo.minSpanX = info.minSpanX;
2357 mPendingAddInfo.minSpanY = info.minSpanY;
2358
Adam Cohenfbba09b2011-07-18 21:43:05 -07002359 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002360 mPendingAddInfo.cellX = cell[0];
2361 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002362 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002363 if (span != null) {
2364 mPendingAddInfo.spanX = span[0];
2365 mPendingAddInfo.spanY = span[1];
2366 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002367
Adam Cohened66b2b2012-01-23 17:28:51 -08002368 AppWidgetHostView hostView = info.boundWidget;
2369 int appWidgetId;
2370 if (hostView != null) {
2371 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002372 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002373
2374 // Clear the boundWidget so that it doesn't get destroyed.
2375 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002376 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002377 // In this case, we either need to start an activity to get permission to bind
2378 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002379 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002380 Bundle options = info.bindOptions;
2381
Sunny Goyalffe83f12014-08-14 17:39:34 -07002382 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2383 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002384 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002385 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002386 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002387 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002388 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2389 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2390 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002391 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2392 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002393 // TODO: we need to make sure that this accounts for the options bundle.
2394 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002395 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2396 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002397 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002398 }
2399
Adam Cohendcd297f2013-06-18 13:13:40 -07002400 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002401 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002402 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002403 folderInfo.title = getText(R.string.folder_name);
2404
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002405 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002406 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2407 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002408 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002409
2410 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002411 FolderIcon newFolder =
2412 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002413 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002414 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002415 // Force measure the new folder icon
2416 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2417 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002418 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002419 }
Romain Guycbb89e42009-06-08 15:52:54 -07002420
Joe Onorato9c1289c2009-08-17 11:03:03 -04002421 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002422 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002423 }
2424
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002425 @Override
2426 public boolean dispatchKeyEvent(KeyEvent event) {
2427 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2428 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002429 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002430 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002431 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002432 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002433 dumpState();
2434 return true;
2435 }
2436 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002437 }
2438 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2439 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002440 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002441 return true;
2442 }
2443 }
2444
2445 return super.dispatchKeyEvent(event);
2446 }
2447
Joe Onorato88ec0992009-11-19 13:16:06 -08002448 @Override
2449 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002450 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2451 return;
2452 }
2453
Sunny Goyal45478022015-06-08 16:52:41 -07002454 if (mDragController.isDragging()) {
2455 mDragController.cancelDrag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08002456 return;
2457 }
2458
Winson Chungb745afb2015-03-02 11:51:23 -08002459 if (isAppsViewVisible()) {
2460 showWorkspace(true);
2461 } else if (isWidgetsViewVisible()) {
2462 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002463 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002464 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002465 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002466 Folder openFolder = mWorkspace.getOpenFolder();
2467 if (openFolder.isEditingName()) {
2468 openFolder.dismissEditingName();
2469 } else {
2470 closeFolder();
2471 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002472 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002473 mWorkspace.exitWidgetResizeMode();
2474
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002475 // Back button is a no-op here, but give at least some feedback for the button press
2476 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002477 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002478 }
2479
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 /**
Sunny Goyal383c5072015-06-12 21:18:53 -07002481 * Re-listen when widget host is reset.
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002482 */
Sunny Goyal383c5072015-06-12 21:18:53 -07002483 @Override
2484 public void onAppWidgetHostReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002485 if (mAppWidgetHost != null) {
2486 mAppWidgetHost.startListening();
2487 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002488 }
2489
2490 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002491 * Launches the intent referred by the clicked shortcut.
2492 *
2493 * @param v The view representing the clicked shortcut.
2494 */
2495 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002496 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2497 // view has detached (it's possible for this to happen if the view is removed mid touch).
2498 if (v.getWindowToken() == null) {
2499 return;
2500 }
2501
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002502 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002503 return;
2504 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002505
Adam Cohen1697b792013-09-17 19:08:21 -07002506 if (v instanceof Workspace) {
2507 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002508 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002509 }
2510 return;
2511 }
2512
2513 if (v instanceof CellLayout) {
2514 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002515 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002516 }
2517 }
2518
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002519 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002520 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002521 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002523 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002524 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002525 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002526 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002527 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002528 } else if (tag instanceof AppInfo) {
2529 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002530 } else if (tag instanceof LauncherAppWidgetInfo) {
2531 if (v instanceof PendingAppWidgetHostView) {
2532 onClickPendingWidget((PendingAppWidgetHostView) v);
2533 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002534 }
2535 }
2536
Sunny Goyal70660032015-05-14 00:07:08 -07002537 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002538 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002539 return false;
2540 }
2541
Michael Jurkaaf442092010-06-10 17:01:57 -07002542 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002543 * Event handler for the app widget view which has not fully restored.
2544 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002545 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002546 if (mIsSafeModeEnabled) {
2547 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2548 return;
2549 }
2550
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002551 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002552 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002553 int widgetId = info.appWidgetId;
2554 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2555 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002556 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2557 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002558 mPendingAddInfo.copyFrom(info);
2559 mPendingAddWidgetId = widgetId;
2560
Sunny Goyalffe83f12014-08-14 17:39:34 -07002561 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2562 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002563 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002564 } else if (info.installProgress < 0) {
2565 // The install has not been queued
2566 final String packageName = info.providerName.getPackageName();
2567 showBrokenAppInstallDialog(packageName,
2568 new DialogInterface.OnClickListener() {
2569 public void onClick(DialogInterface dialog, int id) {
2570 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2571 }
2572 });
2573 } else {
2574 // Download has started.
2575 final String packageName = info.providerName.getPackageName();
2576 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002577 }
2578 }
2579
2580 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002581 * Event handler for the "grid" button that appears on the home screen, which
2582 * enters all apps mode.
2583 *
2584 * @param v The view that was clicked.
2585 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002586 protected void onClickAllAppsButton(View v) {
2587 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chung76648c52015-07-10 14:33:23 -07002588 if (!isAppsViewVisible()) {
Winson Chung4ac30062015-05-08 17:34:17 -07002589 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07002590 true /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungbedf9232015-07-10 12:38:30 -07002591
2592 if (mLauncherCallbacks != null) {
2593 mLauncherCallbacks.onClickAllAppsButton(v);
2594 }
Winson Chung76648c52015-07-10 14:33:23 -07002595 }
2596 }
2597
2598 protected void onLongClickAllAppsButton(View v) {
2599 if (LOGD) Log.d(TAG, "onLongClickAllAppsButton");
2600 if (!isAppsViewVisible()) {
2601 showAppsView(true /* animated */, false /* resetListToTop */,
2602 true /* updatePredictedApps */, true /* focusSearchBar */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002603 }
2604 }
2605
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002606 private void showBrokenAppInstallDialog(final String packageName,
2607 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002608 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002609 .setTitle(R.string.abandoned_promises_title)
2610 .setMessage(R.string.abandoned_promise_explanation)
2611 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2612 .setNeutralButton(R.string.abandoned_clean_this,
2613 new DialogInterface.OnClickListener() {
2614 public void onClick(DialogInterface dialog, int id) {
2615 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2616 mWorkspace.removeAbandonedPromise(packageName, user);
2617 }
2618 })
2619 .create().show();
2620 return;
2621 }
2622
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002623 /**
2624 * Event handler for an app shortcut click.
2625 *
2626 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2627 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002628 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002629 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2630 Object tag = v.getTag();
2631 if (!(tag instanceof ShortcutInfo)) {
2632 throw new IllegalArgumentException("Input must be a Shortcut");
2633 }
2634
2635 // Open shortcut
2636 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002637
2638 if (shortcut.isDisabled != 0) {
2639 int error = R.string.activity_not_available;
2640 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2641 error = R.string.safemode_shortcut_error;
2642 }
2643 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2644 return;
2645 }
2646
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002647 final Intent intent = shortcut.intent;
2648
2649 // Check for special shortcuts
2650 if (intent.getComponent() != null) {
2651 final String shortcutClass = intent.getComponent().getClassName();
2652
2653 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2654 MemoryDumpActivity.startDump(this);
2655 return;
2656 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2657 toggleShowWeightWatcher();
2658 return;
2659 }
2660 }
2661
Chris Wren40c5ed32014-06-24 18:24:23 -04002662 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002663 if ((v instanceof BubbleTextView)
2664 && shortcut.isPromise()
2665 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002666 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002667 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002668 new DialogInterface.OnClickListener() {
2669 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002670 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002671 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002672 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002673 return;
2674 }
2675
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002676 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002677 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002678
2679 if (mLauncherCallbacks != null) {
2680 mLauncherCallbacks.onClickAppShortcut(v);
2681 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002682 }
2683
Adam Cohen091440a2015-03-18 14:16:05 -07002684 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002685 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002686 final ShortcutInfo shortcut;
2687 final Intent intent;
2688 if (tag instanceof ShortcutInfo) {
2689 shortcut = (ShortcutInfo) tag;
2690 intent = shortcut.intent;
2691 int[] pos = new int[2];
2692 v.getLocationOnScreen(pos);
2693 intent.setSourceBounds(new Rect(pos[0], pos[1],
2694 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002695
Sunny Goyal508da152014-08-14 10:53:27 -07002696 } else if (tag instanceof AppInfo) {
2697 shortcut = null;
2698 intent = ((AppInfo) tag).intent;
2699 } else {
2700 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2701 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002702
2703 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002704 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002705
2706 if (success && v instanceof BubbleTextView) {
2707 mWaitingForResume = (BubbleTextView) v;
2708 mWaitingForResume.setStayPressed(true);
2709 }
2710 }
2711
2712 /**
2713 * Event handler for a folder icon click.
2714 *
2715 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2716 */
2717 protected void onClickFolderIcon(View v) {
2718 if (LOGD) Log.d(TAG, "onClickFolder");
2719 if (!(v instanceof FolderIcon)){
2720 throw new IllegalArgumentException("Input must be a FolderIcon");
2721 }
2722
Sunny Goyal317698b2015-07-29 11:45:41 -07002723 // TODO(sunnygoyal): Re-evaluate this code.
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002724 FolderIcon folderIcon = (FolderIcon) v;
2725 final FolderInfo info = folderIcon.getFolderInfo();
2726 Folder openFolder = mWorkspace.getFolderForTag(info);
2727
2728 // If the folder info reports that the associated folder is open, then verify that
2729 // it is actually opened. There have been a few instances where this gets out of sync.
2730 if (info.opened && openFolder == null) {
2731 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2732 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2733 info.opened = false;
2734 }
2735
2736 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2737 // Close any open folder
2738 closeFolder();
2739 // Open the requested folder
2740 openFolder(folderIcon);
2741 } else {
2742 // Find the open folder...
2743 int folderScreen;
2744 if (openFolder != null) {
2745 folderScreen = mWorkspace.getPageForView(openFolder);
2746 // .. and close it
2747 closeFolder(openFolder);
2748 if (folderScreen != mWorkspace.getCurrentPage()) {
2749 // Close any folder open on the current screen
2750 closeFolder();
2751 // Pull the folder onto this screen
2752 openFolder(folderIcon);
2753 }
2754 }
2755 }
Adam Cohen9211d422014-10-07 18:14:53 -07002756
2757 if (mLauncherCallbacks != null) {
2758 mLauncherCallbacks.onClickFolderIcon(v);
2759 }
Michael Jurka5130e402011-10-13 04:55:35 -07002760 }
2761
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002762 /**
2763 * Event handler for the (Add) Widgets button that appears after a long press
2764 * on the home screen.
2765 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002766 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002767 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002768 if (mIsSafeModeEnabled) {
2769 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2770 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002771 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002772 if (mLauncherCallbacks != null) {
2773 mLauncherCallbacks.onClickAddWidgetButton(view);
2774 }
Adam Cohen9211d422014-10-07 18:14:53 -07002775 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002776 }
2777
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002778 /**
2779 * Event handler for the wallpaper picker button that appears after a long press
2780 * on the home screen.
2781 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002782 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002783 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Tony Wickham6ed058c2015-09-09 18:21:49 -07002784 int pageScroll = mWorkspace.getScrollForPage(mWorkspace.getPageNearestToCenterOfScreen());
2785 float offset = mWorkspace.mWallpaperOffset.wallpaperOffsetForScroll(pageScroll);
2786 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName())
2787 .putExtra(WallpaperPickerActivity.EXTRA_WALLPAPER_OFFSET, offset),
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002788 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002789
2790 if (mLauncherCallbacks != null) {
2791 mLauncherCallbacks.onClickWallpaperPicker(v);
2792 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002793 }
2794
2795 /**
2796 * Event handler for a click on the settings button that appears after a long press
2797 * on the home screen.
2798 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002799 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002800 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002801 if (mLauncherCallbacks != null) {
2802 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002803 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002804 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002805 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002806 }
2807
Adam Cohen61f560d2013-09-30 15:58:20 -07002808 public View.OnTouchListener getHapticFeedbackTouchListener() {
2809 if (mHapticFeedbackTouchListener == null) {
2810 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002811 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002812 @Override
2813 public boolean onTouch(View v, MotionEvent event) {
2814 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2815 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2816 }
2817 return false;
2818 }
2819 };
2820 }
2821 return mHapticFeedbackTouchListener;
2822 }
2823
Adam Cohen9211d422014-10-07 18:14:53 -07002824 public void onDragStarted(View view) {
2825 if (isOnCustomContent()) {
2826 // Custom content screen doesn't participate in drag and drop. If on custom
2827 // content screen, move to default.
2828 moveWorkspaceToDefaultScreen();
2829 }
2830
2831 if (mLauncherCallbacks != null) {
2832 mLauncherCallbacks.onDragStarted(view);
2833 }
2834 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002835
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002836 /**
2837 * Called when the user stops interacting with the launcher.
2838 * This implies that the user is now on the homescreen and is not doing housekeeping.
2839 */
Adam Cohen9211d422014-10-07 18:14:53 -07002840 protected void onInteractionEnd() {
2841 if (mLauncherCallbacks != null) {
2842 mLauncherCallbacks.onInteractionEnd();
2843 }
2844 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002845
2846 /**
2847 * Called when the user starts interacting with the launcher.
2848 * The possible interactions are:
2849 * - open all apps
2850 * - reorder an app shortcut, or a widget
2851 * - open the overview mode.
2852 * This is a good time to stop doing things that only make sense
2853 * when the user is on the homescreen and not doing housekeeping.
2854 */
Adam Cohen9211d422014-10-07 18:14:53 -07002855 protected void onInteractionBegin() {
2856 if (mLauncherCallbacks != null) {
2857 mLauncherCallbacks.onInteractionBegin();
2858 }
2859 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002860
Winson Chungcd99cd32015-04-29 11:03:24 -07002861 /** Updates the interaction state. */
2862 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002863 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002864 // overlay
Winson Chungef7f8742015-06-04 17:18:17 -07002865 boolean fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2866 boolean toStateWithOverlay = toState != Workspace.State.NORMAL;
Winson Chung83f59ab2015-05-05 17:21:58 -07002867 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002868 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002869 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002870 onInteractionEnd();
2871 }
2872 }
2873
Kenny Guyf07af7b2014-07-31 11:39:16 +01002874 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002875 try {
2876 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002877 launcherApps.showAppDetailsForProfile(componentName, user);
2878 } catch (SecurityException e) {
2879 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2880 Log.e(TAG, "Launcher does not have permission to launch settings");
2881 } catch (ActivityNotFoundException e) {
2882 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2883 Log.e(TAG, "Unable to launch settings");
2884 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002885 }
2886
Michael Jurka1e2f4652013-07-08 18:03:46 -07002887 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002888 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2889 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002890 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002891 // System applications cannot be installed. For now, show a toast explaining that.
2892 // We may give them the option of disabling apps this way.
2893 int messageId = R.string.uninstall_system_app_text;
2894 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002895 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002896 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002897 String packageName = componentName.getPackageName();
2898 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002899 Intent intent = new Intent(
2900 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002901 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2902 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002903 if (user != null) {
2904 user.addToIntent(intent, Intent.EXTRA_USER);
2905 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002906 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002907 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002908 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002909 }
2910
Winson Chung8f1eff72015-05-28 17:33:40 -07002911 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002912 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002913 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002914 // Only launch using the new animation if the shortcut has not opted out (this is a
2915 // private contract between launcher and may be ignored in the future).
2916 boolean useLaunchAnimation = (v != null) &&
2917 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002918 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2919 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002920
Kenny Guy1317e2d2014-05-08 18:52:50 +01002921 UserHandleCompat user = null;
2922 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2923 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2924 user = userManager.getUserForSerialNumber(serialNumber);
2925 }
2926
2927 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002928 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002929 ActivityOptions opts = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002930 if (Utilities.ATLEAST_MARSHMALLOW) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002931 int left = 0, top = 0;
2932 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2933 if (v instanceof TextView) {
2934 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002935 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2936 if (icon != null) {
2937 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002938 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002939 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002940 width = bounds.width();
2941 height = bounds.height();
2942 }
2943 }
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002944 opts = ActivityOptions.makeClipRevealAnimation(v, left, top, width, height);
2945 } else if (!Utilities.ATLEAST_LOLLIPOP) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002946 // Below L, we use a scale up animation
Adam Cohen2e52c902015-04-06 13:11:28 -07002947 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002948 v.getMeasuredWidth(), v.getMeasuredHeight());
Sunny Goyal9fc953b2015-08-17 12:24:25 -07002949 } else if (Utilities.ATLEAST_LOLLIPOP_MR1) {
Adam Cohen947e02b2015-05-22 19:23:34 -07002950 // On L devices, we use the device default slide-up transition.
2951 // On L MR1 devices, we a custom version of the slide-up transition which
2952 // doesn't have the delay present in the device default.
2953 opts = ActivityOptions.makeCustomAnimation(this,
2954 R.anim.task_open_enter, R.anim.no_anim);
Chet Haasea8f996d2015-02-17 12:56:04 -08002955 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002956 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002957 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002958
2959 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2960 // Could be launching some bookkeeping activity
2961 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002962 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002963 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002964 launcherApps.startActivityForProfile(intent.getComponent(), user,
2965 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002966 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002967 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 } catch (SecurityException e) {
Sunny Goyal28c6b962015-10-12 11:42:05 -07002969 if (Utilities.ATLEAST_MARSHMALLOW && tag instanceof ItemInfo) {
2970 // Due to legacy reasons, direct call shortcuts require Launchers to have the
2971 // corresponding permission. Show the appropriate permission prompt if that
2972 // is the case.
2973 if (intent.getComponent() == null
2974 && Intent.ACTION_CALL.equals(intent.getAction())
2975 && checkSelfPermission(Manifest.permission.CALL_PHONE) !=
2976 PackageManager.PERMISSION_GRANTED) {
2977 // TODO: Rename sPendingAddItem to a generic name.
2978 sPendingAddItem = preparePendingAddArgs(REQUEST_PERMISSION_CALL_PHONE, intent,
2979 0, (ItemInfo) tag);
2980 requestPermissions(new String[]{Manifest.permission.CALL_PHONE},
2981 REQUEST_PERMISSION_CALL_PHONE);
2982 return false;
2983 }
2984 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002985 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002986 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002987 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002988 "or use the exported attribute for this activity. "
2989 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002990 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002991 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002992 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002993
Winson Chungbedf9232015-07-10 12:38:30 -07002994 public boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002995 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002996 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2997 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2998 return false;
2999 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003000 try {
3001 success = startActivity(v, intent, tag);
3002 } catch (ActivityNotFoundException e) {
3003 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3004 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3005 }
3006 return success;
3007 }
3008
Adam Cohen268c4752012-06-06 17:47:33 -07003009 /**
3010 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3011 * in the DragLayer in the exact absolute location of the original FolderIcon.
3012 */
3013 private void copyFolderIconToImage(FolderIcon fi) {
3014 final int width = fi.getMeasuredWidth();
3015 final int height = fi.getMeasuredHeight();
3016
3017 // Lazy load ImageView, Bitmap and Canvas
3018 if (mFolderIconImageView == null) {
3019 mFolderIconImageView = new ImageView(this);
3020 }
3021 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3022 mFolderIconBitmap.getHeight() != height) {
3023 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3024 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3025 }
3026
3027 DragLayer.LayoutParams lp;
3028 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3029 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3030 } else {
3031 lp = new DragLayer.LayoutParams(width, height);
3032 }
3033
Adam Cohen307fe232012-08-16 17:55:58 -07003034 // The layout from which the folder is being opened may be scaled, adjust the starting
3035 // view size by this scale factor.
3036 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003037 lp.customPosition = true;
3038 lp.x = mRectForFolderAnimation.left;
3039 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003040 lp.width = (int) (scale * width);
3041 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003042
3043 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3044 fi.draw(mFolderIconCanvas);
3045 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003046 if (fi.getFolder() != null) {
3047 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3048 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003049 }
Adam Cohen268c4752012-06-06 17:47:33 -07003050 // Just in case this image view is still in the drag layer from a previous animation,
3051 // we remove it and re-add it.
3052 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3053 mDragLayer.removeView(mFolderIconImageView);
3054 }
3055 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003056 if (fi.getFolder() != null) {
3057 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003058 }
Adam Cohen268c4752012-06-06 17:47:33 -07003059 }
3060
Adam Cohen2801caf2011-05-13 20:57:39 -07003061 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003062 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003063 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3064 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3065 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3066
Adam Cohenc51934b2011-07-26 21:07:43 -07003067 FolderInfo info = (FolderInfo) fi.getTag();
3068 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3069 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003070 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3071 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003072 }
3073
Adam Cohen268c4752012-06-06 17:47:33 -07003074 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3075 copyFolderIconToImage(fi);
3076 fi.setVisibility(View.INVISIBLE);
3077
Michael Jurka2ecf9952012-06-18 12:52:28 -07003078 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003079 scaleX, scaleY);
Sunny Goyal9fc953b2015-08-17 12:24:25 -07003080 if (Utilities.ATLEAST_LOLLIPOP) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003081 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3082 }
3083 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003084 oa.start();
3085 }
3086
Adam Cohen268c4752012-06-06 17:47:33 -07003087 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003088 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003089 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3090 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3091 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3092
Adam Cohen268c4752012-06-06 17:47:33 -07003093 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003094
Adam Cohen268c4752012-06-06 17:47:33 -07003095 // We remove and re-draw the FolderIcon in-case it has changed
3096 mDragLayer.removeView(mFolderIconImageView);
3097 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003098 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003099 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003100 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003101 oa.addListener(new AnimatorListenerAdapter() {
3102 @Override
3103 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003104 if (cl != null) {
3105 cl.clearFolderLeaveBehind();
3106 // Remove the ImageView copy of the FolderIcon and make the original visible.
3107 mDragLayer.removeView(mFolderIconImageView);
3108 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003109 }
3110 }
3111 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003112 oa.start();
3113 }
3114
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003115 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003116 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003117 * is animated relative to the specified View. If the View is null, no animation
3118 * is played.
3119 *
3120 * @param folderInfo The FolderInfo describing the folder to open.
3121 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003122 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003123 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003124 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3125 if (openFolder != null && openFolder != folder) {
3126 // Close any open folder before opening a folder.
3127 closeFolder();
3128 }
3129
Adam Cohena9cf38f2011-05-02 15:36:58 -07003130 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003131
Adam Cohena9cf38f2011-05-02 15:36:58 -07003132 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003133
Sunny Goyalf4066152015-04-15 09:42:19 -07003134 // While the folder is open, the position of the icon cannot change.
3135 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3136
Adam Cohen4554ee12011-08-03 16:13:21 -07003137 // Just verify that the folder hasn't already been added to the DragLayer.
3138 // There was a one-off crash where the folder had a parent already.
3139 if (folder.getParent() == null) {
3140 mDragLayer.addView(folder);
3141 mDragController.addDropTarget((DropTarget) folder);
3142 } else {
3143 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3144 folder.getParent() + ").");
3145 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003146 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003147 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003148
3149 // Notify the accessibility manager that this folder "window" has appeared and occluded
3150 // the workspace items
3151 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3152 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003153 }
3154
3155 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003156 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003157 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003158 if (folder.isEditingName()) {
3159 folder.dismissEditingName();
3160 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003161 closeFolder(folder);
3162 }
3163 }
3164
Sunny Goyal83a8f042015-05-19 12:52:12 -07003165 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003166 folder.getInfo().opened = false;
3167
3168 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3169 if (parent != null) {
3170 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3171 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003172 if (fi != null) {
3173 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3174 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003175 }
3176 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003177
3178 // Notify the accessibility manager that this folder "window" has disappeard and no
3179 // longer occludeds the workspace items
3180 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003181 }
3182
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003183 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003184 if (!isDraggingEnabled()) return false;
3185 if (isWorkspaceLocked()) return false;
3186 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003187
Winson Chung76648c52015-07-10 14:33:23 -07003188 if (v == mAllAppsButton) {
3189 onLongClickAllAppsButton(v);
3190 return true;
3191 }
3192
Adam Cohen1697b792013-09-17 19:08:21 -07003193 if (v instanceof Workspace) {
3194 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003195 if (!mWorkspace.isTouchActive()) {
3196 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003197 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3198 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3199 return true;
3200 } else {
3201 return false;
3202 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003203 } else {
3204 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003205 }
Adam Cohen1697b792013-09-17 19:08:21 -07003206 }
3207
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003208 CellLayout.CellInfo longClickCellInfo = null;
3209 View itemUnderLongClick = null;
3210 if (v.getTag() instanceof ItemInfo) {
3211 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003212 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003213 itemUnderLongClick = longClickCellInfo.cell;
3214 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003215 }
3216
Winson Chung3d503fb2011-07-13 17:25:49 -07003217 // The hotseat touch handling does not go through Workspace, and we always allow long press
3218 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003219 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003220 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003221 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003222 // User long pressed on empty space
3223 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3224 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003225 if (mWorkspace.isInOverviewMode()) {
3226 mWorkspace.startReordering(v);
3227 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003228 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003230 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003231 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3232 mHotseat.getOrderInHotseat(
3233 longClickCellInfo.cellX,
3234 longClickCellInfo.cellY));
3235 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003236 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003237 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238 }
3239 }
3240 }
3241 return true;
3242 }
3243
Winson Chung3d503fb2011-07-13 17:25:49 -07003244 boolean isHotseatLayout(View layout) {
3245 return mHotseat != null && layout != null &&
3246 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3247 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003248
Winson Chung3d503fb2011-07-13 17:25:49 -07003249 /**
3250 * Returns the CellLayout of the specified container at the specified screen.
3251 */
Sunny Goyal92820592015-03-02 11:31:35 -08003252 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003253 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3254 if (mHotseat != null) {
3255 return mHotseat.getLayout();
3256 } else {
3257 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003258 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003259 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003260 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003261 }
3262 }
3263
Winson Chungb745afb2015-03-02 11:51:23 -08003264 /**
3265 * For overridden classes.
3266 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003267 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003268 return isAppsViewVisible();
3269 }
3270
3271 public boolean isAppsViewVisible() {
3272 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3273 }
3274
3275 public boolean isWidgetsViewVisible() {
3276 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003277 }
3278
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003279 private void setWorkspaceBackground(int background) {
3280 switch (background) {
3281 case WORKSPACE_BACKGROUND_TRANSPARENT:
3282 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3283 break;
3284 case WORKSPACE_BACKGROUND_BLACK:
3285 getWindow().setBackgroundDrawable(null);
3286 break;
3287 default:
3288 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3289 }
Craig Mautner360310b2012-10-26 15:13:08 -07003290 }
3291
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003292 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003293 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3294 int curflags = getWindow().getAttributes().flags
3295 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3296 if (wpflags != curflags) {
3297 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3298 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003299 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003300 }
3301
Michael Jurkae326f182011-11-21 14:05:46 -08003302 @Override
3303 public void onTrimMemory(int level) {
3304 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003305 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3306 // The widget preview db can result in holding onto over
3307 // 3MB of memory for caching which isn't necessary.
3308 SQLiteDatabase.releaseMemory();
3309
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003310 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003311 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003312 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003313 if (mLauncherCallbacks != null) {
3314 mLauncherCallbacks.onTrimMemory(level);
3315 }
Michael Jurkae326f182011-11-21 14:05:46 -08003316 }
3317
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003318 public void showWorkspace(boolean animated) {
Winson Chungef7f8742015-06-04 17:18:17 -07003319 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null);
Adam Cohened307df2013-10-02 09:37:31 -07003320 }
3321
Sunny Goyal83a8f042015-05-19 12:52:12 -07003322 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003323 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
Winson Chungef7f8742015-06-04 17:18:17 -07003324 onCompleteRunnable);
Winson Chungdc61c4d2015-04-20 18:26:57 -07003325 }
3326
3327 protected void showWorkspace(int snapToPage, boolean animated) {
Winson Chungef7f8742015-06-04 17:18:17 -07003328 showWorkspace(snapToPage, animated, null);
Winson Chungdc61c4d2015-04-20 18:26:57 -07003329 }
3330
Winson Chungef7f8742015-06-04 17:18:17 -07003331 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable) {
Winson Chung0f785722015-04-08 10:27:49 -07003332 boolean changed = mState != State.WORKSPACE ||
3333 mWorkspace.getState() != Workspace.State.NORMAL;
3334 if (changed) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003335 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003336 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
3337 Workspace.State.NORMAL, snapToPage, animated, onCompleteRunnable);
Winson Chungc7d2b602012-05-16 17:02:20 -07003338
Michael Jurkab3e22d92011-10-31 15:58:33 -07003339 // Set focus to the AppsCustomize button
3340 if (mAllAppsButton != null) {
3341 mAllAppsButton.requestFocus();
3342 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003343 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003344
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003345 // Change the state *after* we've called all the transition code
3346 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003347
Winson Chung5fb63472011-02-02 17:03:37 -08003348 // Resume the auto-advance of widgets
3349 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003350 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003351
Winson Chung0f785722015-04-08 10:27:49 -07003352 if (changed) {
3353 // Send an accessibility event to announce the context change
3354 getWindow().getDecorView()
3355 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chung0f785722015-04-08 10:27:49 -07003356 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003357 }
3358
Adam Cohened307df2013-10-02 09:37:31 -07003359 void showOverviewMode(boolean animated) {
3360 mWorkspace.setVisibility(View.VISIBLE);
Winson Chung006ee262015-08-03 14:40:11 -07003361 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
3362 Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003363 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3364 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003365 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003366 }
3367
Winson Chungb745afb2015-03-02 11:51:23 -08003368 /**
3369 * Shows the apps view.
3370 */
Winson Chung76648c52015-07-10 14:33:23 -07003371 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps,
3372 boolean focusSearchBar) {
Winson Chungb745afb2015-03-02 11:51:23 -08003373 if (resetListToTop) {
3374 mAppsView.scrollToTop();
3375 }
Winson Chung4ac30062015-05-08 17:34:17 -07003376 if (updatePredictedApps) {
3377 tryAndUpdatePredictedApps();
3378 }
Winson Chung76648c52015-07-10 14:33:23 -07003379 showAppsOrWidgets(State.APPS, animated, focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003380 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003381
Winson Chungb745afb2015-03-02 11:51:23 -08003382 /**
3383 * Shows the widgets view.
3384 */
3385 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003386 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003387 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003388 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003389 }
Winson Chung76648c52015-07-10 14:33:23 -07003390 showAppsOrWidgets(State.WIDGETS, animated, false);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003391
3392 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003393 @Override
3394 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003395 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003396 }
3397 });
Winson Chungb745afb2015-03-02 11:51:23 -08003398 }
3399
3400 /**
3401 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003402 *
3403 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003404 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003405 // TODO: calling method should use the return value so that when {@code false} is returned
3406 // the workspace transition doesn't fall into invalid state.
Winson Chung76648c52015-07-10 14:33:23 -07003407 private boolean showAppsOrWidgets(State toState, boolean animated, boolean focusSearchBar) {
Hyunyoung Songb7900832015-05-15 16:29:45 -07003408 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3409 mState != State.WIDGETS_SPRING_LOADED) {
3410 return false;
3411 }
3412 if (toState != State.APPS && toState != State.WIDGETS) {
3413 return false;
3414 }
Winson Chungb745afb2015-03-02 11:51:23 -08003415
3416 if (toState == State.APPS) {
Winson Chung006ee262015-08-03 14:40:11 -07003417 mStateTransitionAnimation.startAnimationToAllApps(mWorkspace.getState(), animated,
3418 focusSearchBar);
Winson Chungb745afb2015-03-02 11:51:23 -08003419 } else {
Winson Chung006ee262015-08-03 14:40:11 -07003420 mStateTransitionAnimation.startAnimationToWidgets(mWorkspace.getState(), animated);
Winson Chungb745afb2015-03-02 11:51:23 -08003421 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003422
Michael Jurkab3e22d92011-10-31 15:58:33 -07003423 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003424 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003425
3426 // Pause the auto-advance of widgets until we are out of AllApps
3427 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003428 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003429 closeFolder();
3430
3431 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003432 getWindow().getDecorView()
3433 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003434 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003435 }
3436
Winson Chungcd99cd32015-04-29 11:03:24 -07003437 /**
3438 * Updates the workspace and interaction state on state change, and return the animation to this
3439 * new state.
3440 */
3441 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
Winson Chung006ee262015-08-03 14:40:11 -07003442 boolean animated, HashMap<View, Integer> layerViews) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003443 Workspace.State fromState = mWorkspace.getState();
Winson Chung006ee262015-08-03 14:40:11 -07003444 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
Winson Chungcd99cd32015-04-29 11:03:24 -07003445 updateInteraction(fromState, toState);
3446 return anim;
3447 }
3448
Hyunyoung Song3f471442015-04-08 19:01:34 -07003449 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003450 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003451 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3452 mState == State.WIDGETS_SPRING_LOADED) {
3453 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003454 }
Winson Chungb745afb2015-03-02 11:51:23 -08003455
Winson Chung006ee262015-08-03 14:40:11 -07003456 mStateTransitionAnimation.startAnimationToWorkspace(mState, mWorkspace.getState(),
3457 Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003458 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3459 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003460 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003461 }
Adam Cohen7777d962011-08-18 18:58:38 -07003462
Hyunyoung Song3f471442015-04-08 19:01:34 -07003463 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003464 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003465 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003466
Winson Chunge7a03942011-08-05 15:05:12 -07003467 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003468 @Override
3469 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003470 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003471 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3472 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003473 // Before we show workspace, hide all apps again because
3474 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3475 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003476 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003477 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003478 } else {
3479 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003480 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003481 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003482 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003483 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003484
Michael Jurkad3ef3062010-11-23 16:23:58 -08003485 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003486 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003487 showAppsView(true /* animated */, false /* resetListToTop */,
Winson Chung76648c52015-07-10 14:33:23 -07003488 false /* updatePredictedApps */, false /* focusSearchBar */);
Winson Chungb745afb2015-03-02 11:51:23 -08003489 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003490 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003491 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003492 }
3493
Winson Chung4ac30062015-05-08 17:34:17 -07003494 /**
3495 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3496 * resumed.
3497 */
3498 private void tryAndUpdatePredictedApps() {
3499 if (mLauncherCallbacks != null) {
Winson Chung6b1c73f2015-06-18 11:38:42 -07003500 List<ComponentKey> apps = mLauncherCallbacks.getPredictedApps();
Winson Chung91c05952015-06-25 12:17:30 -07003501 if (apps != null) {
Winson Chung4ac30062015-05-08 17:34:17 -07003502 mAppsView.setPredictedApps(apps);
3503 }
3504 }
3505 }
3506
Michael Jurkab3e22d92011-10-31 15:58:33 -07003507 void lockAllApps() {
3508 // TODO
3509 }
3510
3511 void unlockAllApps() {
3512 // TODO
3513 }
3514
Sunny Goyal594d76d2014-11-06 10:12:54 -08003515 protected void disableVoiceButtonProxy(boolean disable) {
3516 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003517 }
3518
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003519 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003520 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3521 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003522 }
3523
Adam Cohen24ce0b32014-01-14 16:18:14 -08003524 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003525 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3526 if (searchProvider == null) {
3527 return null;
3528 }
3529
3530 Bundle opts = new Bundle();
3531 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003532 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003533
Tony Wickham3a3517f2015-10-06 11:27:04 -07003534 // Determine the min and max dimensions of the widget.
3535 LauncherAppState app = LauncherAppState.getInstance();
3536 DeviceProfile portraitProfile = app.getInvariantDeviceProfile().portraitProfile;
3537 DeviceProfile landscapeProfile = app.getInvariantDeviceProfile().landscapeProfile;
3538 float density = getResources().getDisplayMetrics().density;
3539 Rect searchBounds = portraitProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
3540 int maxHeight = (int) (searchBounds.height() / density);
3541 int minHeight = maxHeight;
3542 int maxWidth = (int) (searchBounds.width() / density);
3543 int minWidth = maxWidth;
3544 if (!landscapeProfile.isVerticalBarLayout()) {
3545 searchBounds = landscapeProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
3546 maxHeight = (int) Math.max(maxHeight, searchBounds.height() / density);
3547 minHeight = (int) Math.min(minHeight, searchBounds.height() / density);
3548 maxWidth = (int) Math.max(maxWidth, searchBounds.width() / density);
3549 minWidth = (int) Math.min(minWidth, searchBounds.width() / density);
3550 }
3551 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_HEIGHT, maxHeight);
3552 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT, minHeight);
3553 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MAX_WIDTH, maxWidth);
3554 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_MIN_WIDTH, minWidth);
3555
Sunny Goyal594d76d2014-11-06 10:12:54 -08003556 SharedPreferences sp = getSharedPreferences(
3557 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3558 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003559 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003560 if (!searchProvider.provider.flattenToString().equals(
3561 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003562 || (widgetInfo == null)
3563 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003564 // A valid widget is not already bound.
3565 if (widgetId > -1) {
3566 mAppWidgetHost.deleteAppWidgetId(widgetId);
3567 widgetId = -1;
3568 }
3569
3570 // Try to bind a new widget
3571 widgetId = mAppWidgetHost.allocateAppWidgetId();
3572
3573 if (!AppWidgetManagerCompat.getInstance(this)
3574 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3575 mAppWidgetHost.deleteAppWidgetId(widgetId);
3576 widgetId = -1;
3577 }
3578
3579 sp.edit()
3580 .putInt(QSB_WIDGET_ID, widgetId)
3581 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3582 .commit();
3583 }
3584
Sunny Goyal8d595092015-07-06 12:22:14 -07003585 mAppWidgetHost.setQsbWidgetId(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003586 if (widgetId != -1) {
3587 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
Sunny Goyalebfae6e2015-08-27 15:30:25 -07003588 mQsb.setId(R.id.qsb_widget);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003589 mQsb.updateAppWidgetOptions(opts);
3590 mQsb.setPadding(0, 0, 0, 0);
3591 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003592 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003593 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003594 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003595 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003596 }
3597
Sunny Goyal22235bc2015-04-03 09:23:43 -07003598 private void reinflateQSBIfNecessary() {
3599 if (mQsb instanceof LauncherAppWidgetHostView &&
3600 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3601 mSearchDropTargetBar.removeView(mQsb);
3602 mQsb = null;
3603 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3604 }
3605 }
3606
Michael Jurkad7c28052012-04-27 15:43:36 -07003607 @Override
3608 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003609 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003610 final List<CharSequence> text = event.getText();
3611 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003612 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003613 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003614 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003615 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003616 text.add(getString(R.string.widget_button_text));
Hyunyoung Song166e6482015-06-25 21:06:07 -07003617 } else if (mWorkspace != null) {
3618 text.add(mWorkspace.getCurrentPageDescription());
alanv1d4fde62012-10-17 13:15:47 -07003619 } else {
3620 text.add(getString(R.string.all_apps_home_button_label));
3621 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003622 return result;
3623 }
3624
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003625 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003626 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003627 */
Adam Cohen091440a2015-03-18 14:16:05 -07003628 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003629 @Override
3630 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003631 closeSystemDialogs();
3632 }
3633 }
3634
3635 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003636 * If the activity is currently paused, signal that we need to run the passed Runnable
3637 * in onResume.
3638 *
3639 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003640 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3641 * wrong when we're not running, and if the activity comes back to what the configuration was
3642 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003643 *
3644 * Implementation of the method from LauncherModel.Callbacks.
3645 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003646 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003647 */
Rahul Chaturvedi799aa042015-06-01 21:26:41 -04003648 @Thunk boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003649 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003650 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003651 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003652 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003653 }
3654 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003655 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003656 return true;
3657 } else {
3658 return false;
3659 }
3660 }
3661
Michael Jurkac402cd92013-05-20 15:49:32 +02003662 private boolean waitUntilResume(Runnable run) {
3663 return waitUntilResume(run, false);
3664 }
3665
Michael Jurka1e2f4652013-07-08 18:03:46 -07003666 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003667 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003668 }
3669
Michael Jurka7607c2f2013-04-03 14:33:19 -07003670 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003671 * If the activity is currently paused, signal that we need to re-run the loader
3672 * in onResume.
3673 *
3674 * This needs to be called from incoming places where resources might have been loaded
3675 * while we are paused. That is becaues the Configuration might be wrong
3676 * when we're not running, and if it comes back to what it was when we
3677 * were paused, we are not restarted.
3678 *
3679 * Implementation of the method from LauncherModel.Callbacks.
3680 *
3681 * @return true if we are currently paused. The caller might be able to
3682 * skip some work in that case since we will come back again.
3683 */
3684 public boolean setLoadOnResume() {
3685 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003686 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003687 mOnResumeNeedsLoad = true;
3688 return true;
3689 } else {
3690 return false;
3691 }
3692 }
3693
3694 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003695 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003696 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003697 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003698 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003699 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003700 } else {
3701 return SCREEN_COUNT / 2;
3702 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003703 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003704
Joe Onorato9c1289c2009-08-17 11:03:03 -04003705 /**
3706 * Refreshes the shortcuts shown on the workspace.
3707 *
3708 * Implementation of the method from LauncherModel.Callbacks.
3709 */
3710 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003711 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003712
Michael Jurka7607c2f2013-04-03 14:33:19 -07003713 // If we're starting binding all over again, clear any bind calls we'd postponed in
3714 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3715 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003716 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003717
Winson Chungd64d1762013-08-20 14:37:16 -07003718 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003719 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003720 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003721
Michael Jurka05bf644e2011-11-30 20:28:53 -08003722 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003723 if (mHotseat != null) {
3724 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003725 }
3726 }
3727
Adam Cohendcd297f2013-06-18 13:13:40 -07003728 @Override
3729 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003730 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003731
Adam Cohen5084cba2013-09-03 12:01:16 -07003732 // If there are no screens, we need to have an empty screen
3733 if (orderedScreenIds.size() == 0) {
3734 mWorkspace.addExtraEmptyScreen();
3735 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003736
3737 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003738 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003739 if (hasCustomContentToLeft()) {
3740 mWorkspace.createCustomContentContainer();
3741 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003742 }
Winson Chung64359a52013-07-08 17:17:08 -07003743 }
3744
3745 @Override
3746 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Adam Cohendcd297f2013-06-18 13:13:40 -07003747 int count = orderedScreenIds.size();
3748 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003749 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003750 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003751 }
3752
Adam Cohen39a06042013-07-19 14:30:12 -07003753 private boolean shouldShowWeightWatcher() {
3754 String spKey = LauncherAppState.getSharedPreferencesKey();
3755 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003756 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003757
3758 return show;
3759 }
3760
3761 private void toggleShowWeightWatcher() {
3762 String spKey = LauncherAppState.getSharedPreferencesKey();
3763 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3764 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3765
3766 show = !show;
3767
3768 SharedPreferences.Editor editor = sp.edit();
3769 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3770 editor.commit();
3771
3772 if (mWeightWatcher != null) {
3773 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3774 }
3775 }
3776
Winson Chungd64d1762013-08-20 14:37:16 -07003777 public void bindAppsAdded(final ArrayList<Long> newScreens,
3778 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003779 final ArrayList<ItemInfo> addAnimated,
3780 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003781 Runnable r = new Runnable() {
3782 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003783 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003784 }
3785 };
3786 if (waitUntilResume(r)) {
3787 return;
3788 }
3789
Winson Chungd64d1762013-08-20 14:37:16 -07003790 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003791 if (newScreens != null) {
3792 bindAddScreens(newScreens);
3793 }
Winson Chungd64d1762013-08-20 14:37:16 -07003794
3795 // We add the items without animation on non-visible pages, and with
3796 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003797 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003798 bindItems(addNotAnimated, 0,
3799 addNotAnimated.size(), false);
3800 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003801 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003802 bindItems(addAnimated, 0,
3803 addAnimated.size(), true);
3804 }
Winson Chungc58497e2013-09-03 17:48:37 -07003805
Winson Chung87412982013-10-03 18:34:14 -07003806 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003807 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003808
Winson Chungb745afb2015-03-02 11:51:23 -08003809 if (addedApps != null && mAppsView != null) {
3810 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003811 }
Winson Chungd64d1762013-08-20 14:37:16 -07003812 }
3813
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003814 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003815 * Bind the items start-end from the list.
3816 *
3817 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003818 */
Winson Chung64359a52013-07-08 17:17:08 -07003819 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3820 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003821 Runnable r = new Runnable() {
3822 public void run() {
3823 bindItems(shortcuts, start, end, forceAnimateIcons);
3824 }
3825 };
3826 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003827 return;
3828 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003829
Winson Chungf0c6ae02012-03-21 16:10:31 -07003830 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003831 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3832 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003833 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003834 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003835 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003836 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003837 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003838
3839 // Short circuit if we are loading dock items for a configuration which has no dock
3840 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3841 mHotseat == null) {
3842 continue;
3843 }
3844
Sunny Goyal639e9062015-08-19 19:17:06 -07003845 final View view;
Joe Onorato9c1289c2009-08-17 11:03:03 -04003846 switch (item.itemType) {
3847 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3848 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003849 ShortcutInfo info = (ShortcutInfo) item;
Sunny Goyal639e9062015-08-19 19:17:06 -07003850 view = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003851
Winson Chung64359a52013-07-08 17:17:08 -07003852 /*
3853 * TODO: FIX collision case
3854 */
Winson Chungce376632013-07-11 16:12:41 -07003855 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3856 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3857 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003858 View v = cl.getChildAt(item.cellX, item.cellY);
3859 Object tag = v.getTag();
3860 String desc = "Collision while binding workspace item: " + item
3861 + ". Collides with " + tag;
3862 if (LauncherAppState.isDogfoodBuild()) {
3863 throw (new RuntimeException(desc));
3864 } else {
3865 Log.d(TAG, desc);
3866 }
Winson Chungce376632013-07-11 16:12:41 -07003867 }
Winson Chung64359a52013-07-08 17:17:08 -07003868 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003869 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003870 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Sunny Goyal639e9062015-08-19 19:17:06 -07003871 view = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003872 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003873 (FolderInfo) item, mIconCache);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003874 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003875 default:
3876 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003877 }
Sunny Goyal639e9062015-08-19 19:17:06 -07003878
3879 workspace.addInScreenFromBind(view, item.container, item.screenId, item.cellX,
3880 item.cellY, 1, 1);
3881 if (animateIcons) {
3882 // Animate all the applications up now
3883 view.setAlpha(0f);
3884 view.setScaleX(0f);
3885 view.setScaleY(0f);
3886 bounceAnims.add(createNewAppBounceAnimation(view, i));
3887 newShortcutsScreenId = item.screenId;
3888 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003889 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003890
Winson Chung997a9232013-07-24 15:33:46 -07003891 if (animateIcons) {
3892 // Animate to the correct page
3893 if (newShortcutsScreenId > -1) {
3894 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003895 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003896 final Runnable startBounceAnimRunnable = new Runnable() {
3897 public void run() {
3898 anim.playTogether(bounceAnims);
3899 anim.start();
3900 }
3901 };
Winson Chung997a9232013-07-24 15:33:46 -07003902 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003903 // We post the animation slightly delayed to prevent slowdowns
3904 // when we are loading right after we return to launcher.
3905 mWorkspace.postDelayed(new Runnable() {
3906 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003907 if (mWorkspace != null) {
3908 mWorkspace.snapToPage(newScreenIndex);
3909 mWorkspace.postDelayed(startBounceAnimRunnable,
3910 NEW_APPS_ANIMATION_DELAY);
3911 }
Winson Chung94d67682013-09-25 16:29:40 -07003912 }
3913 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003914 } else {
3915 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003916 }
3917 }
Winson Chung64359a52013-07-08 17:17:08 -07003918 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003919 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003920 }
3921
Joe Onorato9c1289c2009-08-17 11:03:03 -04003922 /**
3923 * Implementation of the method from LauncherModel.Callbacks.
3924 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003925 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003926 Runnable r = new Runnable() {
3927 public void run() {
3928 bindFolders(folders);
3929 }
3930 };
3931 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003932 return;
3933 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003934 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003935 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003936
3937 /**
3938 * Add the views for a widget to the workspace.
3939 *
3940 * Implementation of the method from LauncherModel.Callbacks.
3941 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003942 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003943 Runnable r = new Runnable() {
3944 public void run() {
3945 bindAppWidget(item);
3946 }
3947 };
3948 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003949 return;
3950 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003951
Daniel Sandler843e8602010-06-07 14:59:01 -04003952 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3953 if (DEBUG_WIDGETS) {
3954 Log.d(TAG, "bindAppWidget: " + item);
3955 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003956 final Workspace workspace = mWorkspace;
3957
Adam Cohen59400422014-03-05 18:07:04 -08003958 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003959 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003960
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003961 if (!mIsSafeModeEnabled
3962 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003963 && (item.restoreStatus != LauncherAppWidgetInfo.RESTORE_COMPLETED)) {
3964
Sunny Goyalff572272014-07-23 13:58:07 -07003965 if (appWidgetInfo == null) {
3966 if (DEBUG_WIDGETS) {
3967 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3968 + " belongs to component " + item.providerName
3969 + ", as the povider is null");
3970 }
3971 LauncherModel.deleteItemFromDatabase(this, item);
3972 return;
3973 }
Sunny Goyalff572272014-07-23 13:58:07 -07003974
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003975 // If we do not have a valid id, try to bind an id.
3976 if ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0) {
3977 // Note: This assumes that the id remap broadcast is received before this step.
3978 // If that is not the case, the id remap will be ignored and user may see the
3979 // click to setup view.
3980 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
3981 pendingInfo.spanX = item.spanX;
3982 pendingInfo.spanY = item.spanY;
3983 pendingInfo.minSpanX = item.minSpanX;
3984 pendingInfo.minSpanY = item.minSpanY;
3985 Bundle options = null;
3986 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003987
Sunny Goyal84b4adc2015-08-12 15:12:16 -07003988 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
3989 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3990 newWidgetId, appWidgetInfo, options);
3991
3992 // TODO consider showing a permission dialog when the widget is clicked.
3993 if (!success) {
3994 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3995 if (DEBUG_WIDGETS) {
3996 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3997 + " belongs to component " + item.providerName
3998 + ", as the launcher is unable to bing a new widget id");
3999 }
4000 LauncherModel.deleteItemFromDatabase(this, item);
4001 return;
Sunny Goyalff572272014-07-23 13:58:07 -07004002 }
Sunny Goyal84b4adc2015-08-12 15:12:16 -07004003
4004 item.appWidgetId = newWidgetId;
4005
4006 // If the widget has a configure activity, it is still needs to set it up, otherwise
4007 // the widget is ready to go.
4008 item.restoreStatus = (appWidgetInfo.configure == null)
4009 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4010 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4011
4012 LauncherModel.updateItemInDatabase(this, item);
4013 } else if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_UI_NOT_READY) != 0)
4014 && (appWidgetInfo.configure == null)) {
4015 // If the ID is already valid, verify if we need to configure or not.
4016 item.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4017 LauncherModel.updateItemInDatabase(this, item);
Sunny Goyalff572272014-07-23 13:58:07 -07004018 }
Sunny Goyalff572272014-07-23 13:58:07 -07004019 }
4020
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004021 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004022 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004023 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004024 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4025 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004026 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004027
Sunny Goyal651077b2014-06-30 14:15:31 -07004028 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
Sunny Goyal233ee962015-08-03 13:05:01 -07004029 item.minSpanX = appWidgetInfo.minSpanX;
4030 item.minSpanY = appWidgetInfo.minSpanY;
Sunny Goyal651077b2014-06-30 14:15:31 -07004031 } else {
4032 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004033 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4034 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004035 view.updateIcon(mIconCache);
4036 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004037 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004038 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004039 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004040
Joe Onorato9c1289c2009-08-17 11:03:03 -04004041 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004042 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004043
Adam Cohendcd297f2013-06-18 13:13:40 -07004044 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004045 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004046 if (!item.isCustomWidget()) {
4047 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4048 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004049
Joe Onorato9c1289c2009-08-17 11:03:03 -04004050 workspace.requestLayout();
4051
Daniel Sandler843e8602010-06-07 14:59:01 -04004052 if (DEBUG_WIDGETS) {
4053 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4054 + (SystemClock.uptimeMillis()-start) + "ms");
4055 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004056 }
4057
Sunny Goyalff572272014-07-23 13:58:07 -07004058 /**
4059 * Restores a pending widget.
4060 *
4061 * @param appWidgetId The app widget id
4062 * @param cellInfo The position on screen where to create the widget.
4063 */
4064 private void completeRestoreAppWidget(final int appWidgetId) {
4065 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4066 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4067 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4068 return;
4069 }
4070
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004071 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004072 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4073
4074 mWorkspace.reinflateWidgetsIfNecessary();
4075 LauncherModel.updateItemInDatabase(this, info);
4076 }
4077
Adam Cohen1462de32012-07-24 22:34:36 -07004078 public void onPageBoundSynchronously(int page) {
4079 mSynchronouslyBoundPages.add(page);
4080 }
4081
Joe Onorato9c1289c2009-08-17 11:03:03 -04004082 /**
4083 * Callback saying that there aren't any more items to bind.
4084 *
4085 * Implementation of the method from LauncherModel.Callbacks.
4086 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004087 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004088 Runnable r = new Runnable() {
4089 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004090 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004091 }
4092 };
4093 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004094 return;
4095 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004096 if (mSavedState != null) {
4097 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004098 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004099 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004100 mSavedState = null;
4101 }
4102
Adam Cohen1462de32012-07-24 22:34:36 -07004103 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004104
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004105 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004106 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004107
4108 // If we received the result of any pending adds while the loader was running (e.g. the
4109 // widget configuration forced an orientation change), process them now.
4110 if (sPendingAddItem != null) {
4111 final long screenId = completeAdd(sPendingAddItem);
4112
4113 // TODO: this moves the user to the page where the pending item was added. Ideally,
4114 // the screen would be guaranteed to exist after bind, and the page would be set through
4115 // the workspace restore process.
4116 mWorkspace.post(new Runnable() {
4117 @Override
4118 public void run() {
4119 mWorkspace.snapToScreenId(screenId);
4120 }
4121 });
4122 sPendingAddItem = null;
4123 }
4124
Sunny Goyal756adbc2015-04-16 15:20:51 -07004125 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004126
4127 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004128 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004129 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004130 }
4131
Adam Cohen3ed316a2014-07-23 18:21:20 -07004132 private void sendLoadingCompleteBroadcastIfNecessary() {
4133 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4134 String permission =
4135 getResources().getString(R.string.receive_first_load_broadcast_permission);
4136 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4137 sendBroadcast(intent, permission);
4138 SharedPreferences.Editor editor = mSharedPrefs.edit();
4139 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4140 editor.apply();
4141 }
4142 }
4143
Winson Chunga0b7e862013-09-05 16:03:15 -07004144 public boolean isAllAppsButtonRank(int rank) {
4145 if (mHotseat != null) {
4146 return mHotseat.isAllAppsButtonRank(rank);
4147 }
4148 return false;
4149 }
4150
Winson Chunga2413752012-04-03 14:22:34 -07004151 private boolean canRunNewAppsAnimation() {
4152 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
Sunny Goyal639e9062015-08-19 19:17:06 -07004153 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000)
4154 && (mClings == null || !mClings.isVisible());
Winson Chunga2413752012-04-03 14:22:34 -07004155 }
4156
Winson Chungc9168342013-06-26 14:54:55 -07004157 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4158 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4159 PropertyValuesHolder.ofFloat("alpha", 1f),
4160 PropertyValuesHolder.ofFloat("scaleX", 1f),
4161 PropertyValuesHolder.ofFloat("scaleY", 1f));
4162 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4163 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004164 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004165 return bounceAnim;
4166 }
4167
Adam Cohen27772732013-11-11 14:00:56 +00004168 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004169 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004170 }
4171
Tony Wickham3a3517f2015-10-06 11:27:04 -07004172 /** Returns the search bar bounds in pixels. */
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004173 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004174 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004175 }
4176
Tony Wickham55616cd2015-09-23 14:55:17 -07004177 public int getSearchBarHeight() {
4178 if (mLauncherCallbacks != null) {
4179 return mLauncherCallbacks.getSearchBarHeight();
4180 }
4181 return LauncherCallbacks.SEARCH_BAR_HEIGHT_NORMAL;
4182 }
4183
Winson Chung88fa7412015-08-03 14:25:28 -07004184 public void bindSearchProviderChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004185 if (mSearchDropTargetBar == null) {
4186 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004187 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004188 if (mQsb != null) {
4189 mSearchDropTargetBar.removeView(mQsb);
4190 mQsb = null;
4191 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004192 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004193 }
4194
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004195 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004196 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4197 * multiple calls to bind the same list.)
4198 */
4199 @Thunk ArrayList<AppInfo> mTmpAppsList;
4200 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4201 public void run() {
4202 bindAllApplications(mTmpAppsList);
4203 mTmpAppsList = null;
4204 }
4205 };
4206
4207 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004208 * Add the icons for all apps.
4209 *
4210 * Implementation of the method from LauncherModel.Callbacks.
4211 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004212 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004213 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4214 mTmpAppsList = apps;
4215 return;
4216 }
4217
Winson Chungb745afb2015-03-02 11:51:23 -08004218 if (mAppsView != null) {
4219 mAppsView.setApps(apps);
4220 }
Adam Cohen9211d422014-10-07 18:14:53 -07004221 if (mLauncherCallbacks != null) {
4222 mLauncherCallbacks.bindAllApplications(apps);
4223 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004224 }
4225
4226 /**
4227 * A package was updated.
4228 *
4229 * Implementation of the method from LauncherModel.Callbacks.
4230 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004231 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004232 Runnable r = new Runnable() {
4233 public void run() {
4234 bindAppsUpdated(apps);
4235 }
4236 };
4237 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004238 return;
4239 }
4240
Winson Chungb745afb2015-03-02 11:51:23 -08004241 if (mAppsView != null) {
4242 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004243 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004244 }
4245
Sunny Goyal4390ace2014-10-13 11:33:11 -07004246 @Override
4247 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4248 Runnable r = new Runnable() {
4249 public void run() {
4250 bindWidgetsRestored(widgets);
4251 }
4252 };
4253 if (waitUntilResume(r)) {
4254 return;
4255 }
4256 mWorkspace.widgetsRestored(widgets);
4257 }
4258
Joe Onorato9c1289c2009-08-17 11:03:03 -04004259 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004260 * Some shortcuts were updated in the background.
4261 *
4262 * Implementation of the method from LauncherModel.Callbacks.
4263 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004264 @Override
4265 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4266 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004267 Runnable r = new Runnable() {
4268 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004269 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004270 }
4271 };
4272 if (waitUntilResume(r)) {
4273 return;
4274 }
4275
Sunny Goyal4390ace2014-10-13 11:33:11 -07004276 if (!updated.isEmpty()) {
4277 mWorkspace.updateShortcuts(updated);
4278 }
4279
4280 if (!removed.isEmpty()) {
4281 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4282 for (ShortcutInfo si : removed) {
4283 removedComponents.add(si.getTargetComponent());
4284 }
4285 mWorkspace.removeItemsByComponentName(removedComponents, user);
4286 // Notify the drag controller
4287 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004288 }
4289 }
4290
4291 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004292 * Update the state of a package, typically related to install state.
4293 *
4294 * Implementation of the method from LauncherModel.Callbacks.
4295 */
Sunny Goyale755d462014-07-22 13:48:29 -07004296 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004297 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4298 Runnable r = new Runnable() {
4299 public void run() {
4300 bindRestoreItemsChange(updates);
4301 }
4302 };
4303 if (waitUntilResume(r)) {
4304 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004305 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004306
Sunny Goyal756adbc2015-04-16 15:20:51 -07004307 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004308 }
4309
4310 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004311 * A package was uninstalled. We take both the super set of packageNames
4312 * in addition to specific applications to remove, the reason being that
4313 * this can be called when a package is updated as well. In that scenario,
4314 * we only remove specific components from the workspace, where as
4315 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004316 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004317 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004318 * Implementation of the method from LauncherModel.Callbacks.
4319 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004320 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004321 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004322 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004323 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004324 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004325 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004326 }
Winson Chungd64d1762013-08-20 14:37:16 -07004327 };
4328 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004329 return;
4330 }
4331
Sunny Goyal1a745e82014-10-02 15:58:31 -07004332 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004333 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4334 for (AppInfo info : appInfos) {
4335 removedComponents.add(info.componentName);
4336 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004337 if (!packageNames.isEmpty()) {
4338 mWorkspace.removeItemsByPackageName(packageNames, user);
4339 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004340 if (!removedComponents.isEmpty()) {
4341 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004342 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004343 // Notify the drag controller
4344 mDragController.onAppsRemoved(packageNames, removedComponents);
4345
Sunny Goyal1a745e82014-10-02 15:58:31 -07004346 } else {
4347 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004348 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004349
Winson Chungdf95eb12013-10-16 14:57:07 -07004350 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004351 if (mAppsView != null) {
4352 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004353 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004354 }
Joe Onoratobe386092009-11-17 17:32:16 -08004355
Michael Jurkac402cd92013-05-20 15:49:32 +02004356 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004357 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004358 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004359 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004360 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004361
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004362 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004363 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004364 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004365 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004366 return;
4367 }
4368
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004369 if (mWidgetsView != null && model != null) {
4370 mWidgetsView.addWidgets(model);
4371 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004372 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004373 }
4374
Winson Chung400438b2011-01-16 17:53:48 -08004375 private int mapConfigurationOriActivityInfoOri(int configOri) {
4376 final Display d = getWindowManager().getDefaultDisplay();
4377 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4378 switch (d.getRotation()) {
4379 case Surface.ROTATION_0:
4380 case Surface.ROTATION_180:
4381 // We are currently in the same basic orientation as the natural orientation
4382 naturalOri = configOri;
4383 break;
4384 case Surface.ROTATION_90:
4385 case Surface.ROTATION_270:
4386 // We are currently in the other basic orientation to the natural orientation
4387 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4388 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4389 break;
4390 }
4391
4392 int[] oriMap = {
4393 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4394 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4395 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4396 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4397 };
4398 // Since the map starts at portrait, we need to offset if this device's natural orientation
4399 // is landscape.
4400 int indexOffset = 0;
4401 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4402 indexOffset = 1;
4403 }
4404 return oriMap[(d.getRotation() + indexOffset) % 4];
4405 }
Adam Cohen446e9402011-09-15 18:21:21 -07004406
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004407 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
Winson Chung4b919f82012-05-01 10:44:08 -07004408 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004409 if (mRotationEnabled) {
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004410 if (Utilities.ATLEAST_JB_MR2) {
4411 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4412 } else {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004413 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4414 .getConfiguration().orientation));
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004415 }
Winson Chung4b919f82012-05-01 10:44:08 -07004416 }
4417 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004418
Winson Chung4b919f82012-05-01 10:44:08 -07004419 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004420 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004421 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004422 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004423 } else {
4424 mHandler.postDelayed(new Runnable() {
4425 public void run() {
4426 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4427 }
4428 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004429 }
Winson Chung4b919f82012-05-01 10:44:08 -07004430 }
Winson Chung400438b2011-01-16 17:53:48 -08004431 }
4432
Winson Chunge43a1e72014-01-15 10:33:02 -08004433 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004434 if (mLauncherCallbacks != null) {
4435 return mLauncherCallbacks.isLauncherPreinstalled();
4436 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004437 PackageManager pm = getPackageManager();
4438 try {
4439 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4440 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4441 return true;
4442 } else {
4443 return false;
4444 }
4445 } catch (NameNotFoundException e) {
4446 e.printStackTrace();
4447 return false;
4448 }
4449 }
4450
Adam Cohenea90f832014-05-21 15:03:34 -07004451 /**
4452 * This method indicates whether or not we should suggest default wallpaper dimensions
4453 * when our wallpaper cropper was not yet used to set a wallpaper.
4454 */
4455 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004456 if (mLauncherCallbacks != null) {
4457 return mLauncherCallbacks.overrideWallpaperDimensions();
4458 }
Adam Cohenea90f832014-05-21 15:03:34 -07004459 return true;
4460 }
4461
Adam Cohen432609a2014-03-13 17:03:22 -07004462 /**
4463 * To be overridden by subclasses to indicate that there is an activity to launch
4464 * before showing the standard launcher experience.
4465 */
4466 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004467 if (mLauncherCallbacks != null) {
4468 return mLauncherCallbacks.hasFirstRunActivity();
4469 }
Adam Cohen432609a2014-03-13 17:03:22 -07004470 return false;
4471 }
4472
4473 /**
4474 * To be overridden by subclasses to launch any first run activity
4475 */
4476 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004477 if (mLauncherCallbacks != null) {
4478 return mLauncherCallbacks.getFirstRunActivity();
4479 }
Adam Cohen432609a2014-03-13 17:03:22 -07004480 return null;
4481 }
4482
Winson Chunga6945242014-01-08 14:04:34 -08004483 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004484 return !ActivityManager.isRunningInTestHarness() &&
4485 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004486 }
4487
Adam Cohen4a9423d2014-03-28 14:22:31 -07004488 protected boolean hasRunFirstRunActivity() {
4489 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4490 }
4491
Adam Cohen432609a2014-03-13 17:03:22 -07004492 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004493 if (shouldRunFirstRunActivity() &&
4494 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004495 Intent firstRunIntent = getFirstRunActivity();
4496 if (firstRunIntent != null) {
4497 startActivity(firstRunIntent);
4498 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004499 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004500 }
4501 }
Adam Cohen432609a2014-03-13 17:03:22 -07004502 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004503 }
4504
4505 private void markFirstRunActivityShown() {
4506 SharedPreferences.Editor editor = mSharedPrefs.edit();
4507 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4508 editor.apply();
4509 }
4510
Adam Cohen432609a2014-03-13 17:03:22 -07004511 /**
4512 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4513 * screen that must be displayed and dismissed.
4514 */
4515 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004516 if (mLauncherCallbacks != null) {
4517 return mLauncherCallbacks.hasDismissableIntroScreen();
4518 }
Adam Cohen432609a2014-03-13 17:03:22 -07004519 return false;
4520 }
4521
4522 /**
4523 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4524 */
4525 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004526 if (mLauncherCallbacks != null) {
4527 return mLauncherCallbacks.getIntroScreen();
4528 }
Adam Cohen432609a2014-03-13 17:03:22 -07004529 return null;
4530 }
4531
4532 /**
4533 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4534 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4535 */
4536 private boolean shouldShowIntroScreen() {
4537 return hasDismissableIntroScreen() &&
4538 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4539 }
4540
4541 protected void showIntroScreen() {
4542 View introScreen = getIntroScreen();
4543 changeWallpaperVisiblity(false);
4544 if (introScreen != null) {
4545 mDragLayer.showOverlayView(introScreen);
4546 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004547 if (mLauncherOverlayContainer != null) {
4548 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4549 }
Adam Cohen432609a2014-03-13 17:03:22 -07004550 }
4551
4552 public void dismissIntroScreen() {
4553 markIntroScreenDismissed();
4554 if (showFirstRunActivity()) {
4555 // We delay hiding the intro view until the first run activity is showing. This
4556 // avoids a blip.
4557 mWorkspace.postDelayed(new Runnable() {
4558 @Override
4559 public void run() {
4560 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004561 if (mLauncherOverlayContainer != null) {
4562 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4563 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004564 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004565 }
4566 }, ACTIVITY_START_DELAY);
4567 } else {
4568 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004569 if (mLauncherOverlayContainer != null) {
4570 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4571 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004572 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004573 }
4574 changeWallpaperVisiblity(true);
4575 }
4576
4577 private void markIntroScreenDismissed() {
4578 SharedPreferences.Editor editor = mSharedPrefs.edit();
4579 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4580 editor.apply();
4581 }
4582
Adam Cohen091440a2015-03-18 14:16:05 -07004583 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004584 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4585 // on the device, then we always show the first run cling experience (or if there is no
4586 // launcher2). Otherwise, we prompt the user upon started for migration
4587 LauncherClings launcherClings = new LauncherClings(this);
4588 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
Sunny Goyal639e9062015-08-19 19:17:06 -07004589 mClings = launcherClings;
Sunny Goyal88d60f12014-09-08 03:47:29 -07004590 if (mModel.canMigrateFromOldLauncherDb(this)) {
4591 launcherClings.showMigrationCling();
4592 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004593 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004594 }
4595 }
4596 }
4597
Winson Chunga6945242014-01-08 14:04:34 -08004598 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004599 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4600 if (mHotseat != null) mHotseat.setAlpha(1f);
4601 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
Winson Chung006ee262015-08-03 14:40:11 -07004602 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4603 SearchDropTargetBar.State.SEARCH_BAR, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004604 }
4605
4606 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004607 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4608 if (mHotseat != null) mHotseat.setAlpha(0f);
4609 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
Winson Chung006ee262015-08-03 14:40:11 -07004610 if (mSearchDropTargetBar != null) mSearchDropTargetBar.animateToState(
4611 SearchDropTargetBar.State.INVISIBLE, 0);
Winson Chunga6945242014-01-08 14:04:34 -08004612 }
4613
Winson Chung5ffd51d2015-06-25 11:36:50 -07004614 // TODO: These method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004615 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Mathew Inwood72fbec12013-11-19 15:45:07 +00004616 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004617 // Called from search suggestion
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004618 UserHandleCompat user = null;
Sunny Goyal9fc953b2015-08-17 12:24:25 -07004619 if (Utilities.ATLEAST_LOLLIPOP) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004620 UserHandle userHandle = appLaunchIntent.getParcelableExtra(Intent.EXTRA_USER);
4621 if (userHandle != null) {
4622 user = UserHandleCompat.fromUser(userHandle);
4623 }
4624 }
4625 return createAppDragInfo(appLaunchIntent, user);
Winson Chung5ffd51d2015-06-25 11:36:50 -07004626 }
4627
4628 // TODO: This method should be a part of LauncherSearchCallback
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004629 public ItemInfo createAppDragInfo(Intent intent, UserHandleCompat user) {
Winson Chung5ffd51d2015-06-25 11:36:50 -07004630 if (user == null) {
4631 user = UserHandleCompat.myUserHandle();
4632 }
4633
4634 // Called from search suggestion, add the profile extra to the intent to ensure that we
4635 // can launch it correctly
Kenny Guyed131872014-04-30 03:02:21 +01004636 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004637 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(intent, user);
Kenny Guyed131872014-04-30 03:02:21 +01004638 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004639 return null;
4640 }
Winson Chung5ffd51d2015-06-25 11:36:50 -07004641 return new AppInfo(this, activityInfo, user, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004642 }
4643
Winson Chung5ffd51d2015-06-25 11:36:50 -07004644 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004645 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4646 Bitmap icon) {
Sunny Goyala1d1bf32015-06-26 13:24:53 -07004647 return new ShortcutInfo(shortcutIntent, caption, caption, icon,
Kenny Guyed131872014-04-30 03:02:21 +01004648 UserHandleCompat.myUserHandle());
4649 }
4650
Winson Chung5ffd51d2015-06-25 11:36:50 -07004651 // TODO: This method should be a part of LauncherSearchCallback
Mathew Inwood72fbec12013-11-19 15:45:07 +00004652 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4653 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004654 mWorkspace.onExternalDragStartedWithItem(dragView);
4655 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004656 }
4657
Winson Chung5ffd51d2015-06-25 11:36:50 -07004658 protected void moveWorkspaceToDefaultScreen() {
4659 mWorkspace.moveToDefaultScreen(false);
4660 }
4661
Anjali Koppalf5d29132014-02-28 13:33:27 -08004662 @Override
4663 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004664 if (mLauncherCallbacks != null) {
4665 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4666 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004667 }
4668
Winson Chung80baf5a2010-08-09 16:03:15 -07004669 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004670 * Returns a FastBitmapDrawable with the icon, accurately sized.
4671 */
4672 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4673 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4674 d.setFilterBitmap(true);
4675 resizeIconDrawable(d);
4676 return d;
4677 }
4678
4679 /**
4680 * Resizes an icon drawable to the correct icon size.
4681 */
4682 public void resizeIconDrawable(Drawable icon) {
4683 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4684 }
4685
4686 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004687 * Prints out out state for debugging.
4688 */
4689 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004690 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004691 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004692 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4693 Log.d(TAG, "mRestoring=" + mRestoring);
4694 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4695 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004696 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004697 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004698 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004699
Daniel Sandler325dc232013-06-05 22:57:57 -04004700 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004701 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004702
4703 @Override
4704 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4705 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004706 synchronized (sDumpLogs) {
4707 writer.println(" ");
4708 writer.println("Debug logs: ");
4709 for (int i = 0; i < sDumpLogs.size(); i++) {
4710 writer.println(" " + sDumpLogs.get(i));
4711 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004712 }
Adam Cohen9211d422014-10-07 18:14:53 -07004713 if (mLauncherCallbacks != null) {
4714 mLauncherCallbacks.dump(prefix, fd, writer, args);
4715 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004716 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004717
4718 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004719 if (DEBUG_DUMP_LOG) {
4720 synchronized (sDumpLogs) {
4721 Log.d(TAG, "");
4722 Log.d(TAG, "*********************");
4723 Log.d(TAG, "Launcher debug logs: ");
4724 for (int i = 0; i < sDumpLogs.size(); i++) {
4725 Log.d(TAG, " " + sDumpLogs.get(i));
4726 }
4727 Log.d(TAG, "*********************");
4728 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004729 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004730 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004731 }
4732
4733 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004734 addDumpLog(tag, log, null, debugLog);
4735 }
4736
4737 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004738 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004739 if (e != null) {
4740 Log.d(tag, log, e);
4741 } else {
4742 Log.d(tag, log);
4743 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004744 }
Winson Chungede41292013-09-19 16:27:36 -07004745 if (DEBUG_DUMP_LOG) {
4746 sDateStamp.setTime(System.currentTimeMillis());
4747 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004748 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4749 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004750 }
Winson Chungede41292013-09-19 16:27:36 -07004751 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004752 }
4753
Adam Cohen59400422014-03-05 18:07:04 -08004754 public static CustomAppWidget getCustomAppWidget(String name) {
4755 return sCustomAppWidgets.get(name);
4756 }
4757
4758 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4759 return sCustomAppWidgets;
4760 }
4761
Winson Chungede41292013-09-19 16:27:36 -07004762 public void dumpLogsToLocalData() {
4763 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004764 new AsyncTask<Void, Void, Void>() {
4765 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004766 boolean success = false;
4767 sDateStamp.setTime(sRunStart);
4768 String FILENAME = sDateStamp.getMonth() + "-"
4769 + sDateStamp.getDay() + "_"
4770 + sDateStamp.getHours() + "-"
4771 + sDateStamp.getMinutes() + "_"
4772 + sDateStamp.getSeconds() + ".txt";
4773
4774 FileOutputStream fos = null;
4775 File outFile = null;
4776 try {
4777 outFile = new File(getFilesDir(), FILENAME);
4778 outFile.createNewFile();
4779 fos = new FileOutputStream(outFile);
4780 } catch (Exception e) {
4781 e.printStackTrace();
4782 }
4783 if (fos != null) {
4784 PrintWriter writer = new PrintWriter(fos);
4785
4786 writer.println(" ");
4787 writer.println("Debug logs: ");
4788 synchronized (sDumpLogs) {
4789 for (int i = 0; i < sDumpLogs.size(); i++) {
4790 writer.println(" " + sDumpLogs.get(i));
4791 }
4792 }
4793 writer.close();
4794 }
4795 try {
4796 if (fos != null) {
4797 fos.close();
4798 success = true;
4799 }
4800 } catch (IOException e) {
4801 e.printStackTrace();
4802 }
Michael Jurka43467462013-11-14 12:03:58 +01004803 return null;
Winson Chungede41292013-09-19 16:27:36 -07004804 }
Sunny Goyal8ac727b2015-09-23 15:38:09 -07004805 }.executeOnExecutor(Utilities.THREAD_POOL_EXECUTOR);
Adam Cohen4caf2982013-08-20 18:54:31 -07004806 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004807 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004808}
Michael Jurka2763be32011-02-24 11:19:57 -08004809
Dan Sandlerd5024042014-01-09 15:01:33 -05004810interface DebugIntents {
4811 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4812 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004813}