blob: ba9a96e77e17d37cd1e5f2640e9828268bbf871a [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Gilles Debunnedd6c9922010-10-25 11:23:41 -070019import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080020import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070056import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070086import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070088import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070091import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Advanceable;
94import android.widget.FrameLayout;
95import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080096import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070097import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070098
Sunny Goyal651077b2014-06-30 14:15:31 -070099import com.android.launcher3.DropTarget.DragObject;
100import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyal83a8f042015-05-19 12:52:12 -0700101import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700102import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyal5683f872015-05-29 14:54:40 -0700103import com.android.launcher3.allapps.AppSearchManager;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700104import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100105import com.android.launcher3.compat.LauncherActivityInfoCompat;
106import com.android.launcher3.compat.LauncherAppsCompat;
107import com.android.launcher3.compat.UserHandleCompat;
108import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700109import com.android.launcher3.model.WidgetsModel;
Sunny Goyale2df0622015-04-24 11:27:00 -0700110import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700111import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700112import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700113import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700114import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700115
Adam Cohen4caf2982013-08-20 18:54:31 -0700116import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700117import java.io.FileDescriptor;
Adam Cohen4caf2982013-08-20 18:54:31 -0700118import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700120import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800121import java.lang.reflect.InvocationTargetException;
122import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700125import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700126import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700128import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700129import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000130import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132/**
133 * Default launcher application.
134 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100135public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700136 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800137 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
138 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700139 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700140 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800141
Winson Chung83f59ab2015-05-05 17:21:58 -0700142 // Temporary flag
143 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
144
Daniel Sandlerf061f822013-06-27 13:59:36 -0400145 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700146 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700147 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400148 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700149 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700150
Dan Sandlerd5024042014-01-09 15:01:33 -0500151 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
152
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700154 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700155 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700156 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800157
Michael Jurka8b805b12012-04-18 14:23:14 -0700158 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700159 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700160
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700161 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
162 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
163 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
164
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700165 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
166
Mathew Inwood876a8462013-06-14 14:12:41 +0100167 /**
168 * IntentStarter uses request codes starting with this. This must be greater than all activity
169 * request codes used internally.
170 */
171 protected static final int REQUEST_LAST = 100;
172
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800173 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Michael Jurka0a457bf2012-11-19 14:05:05 -0800175 // To turn on these properties, type
176 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800177 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178
Winson Chung2672ff92012-05-04 16:22:30 -0700179 // The Intent extra that defines whether to ignore the launch animation
180 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400181 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700182
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
184 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700185 // Type: int
186 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800187 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700188 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
189 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
191 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700192 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800193 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700194 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700195 // Type: int
196 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
197 // Type: int
198 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
199 // Type: parcelable
200 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000201 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800202 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000203 // Type: int[]
204 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Adam Cohen432609a2014-03-13 17:03:22 -0700206 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800207 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
208
Adam Cohen3ed316a2014-07-23 18:21:20 -0700209 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
210 static final String ACTION_FIRST_LOAD_COMPLETE =
211 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
212
Adam Cohen39a06042013-07-19 14:30:12 -0700213 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700214 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700215
Sunny Goyal594d76d2014-11-06 10:12:54 -0800216 private static final String QSB_WIDGET_ID = "qsb_widget_id";
217 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
218
Winson Chunga6945242014-01-08 14:04:34 -0800219 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
220
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700221 /** The different states that Launcher can be in. */
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700222 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED }
223
Adam Cohen091440a2015-03-18 14:16:05 -0700224 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700225 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700226
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700227 private boolean mIsSafeModeEnabled;
228
Adam Cohenc2d6e892014-10-16 09:49:24 -0700229 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
230 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700231 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700232
Joe Onorato9c1289c2009-08-17 11:03:03 -0400233 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700234 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
235 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700236 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000238 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
239 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
240
Winson Chunga2413752012-04-03 14:22:34 -0700241 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700242 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
243 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700244 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700245
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800246 private final BroadcastReceiver mCloseSystemDialogsReceiver
247 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800248 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private LayoutInflater mInflater;
251
Adam Cohen091440a2015-03-18 14:16:05 -0700252 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700253 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800254 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700255 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800256 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700257 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700258
Sunny Goyalffe83f12014-08-14 17:39:34 -0700259 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700260 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700261
Adam Cohen091440a2015-03-18 14:16:05 -0700262 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800263 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800264 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700265
Michael Jurka0280c3b2010-09-17 15:00:07 -0700266 private int[] mTmpAddItemCellCoordinates = new int[2];
267
Sunny Goyal316490e2015-06-02 09:38:28 -0700268 @Thunk Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800269 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700270
Michael Jurka838a4ca2011-02-07 13:33:06 -0800271 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700272
Winson Chungc51db6a2011-10-05 11:44:49 -0700273 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700274
275 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700276 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700277
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700278 // Main container view and the model for the widget tray screen.
279 @Thunk WidgetsContainerView mWidgetsView;
280 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700281
Winson Chungf0ea4d32011-06-06 14:27:16 -0700282 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800283 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800284
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700286 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
287 // scroll issues (because the workspace may not have been measured yet) and extra work.
288 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
289 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800290
291 private SpannableStringBuilder mDefaultKeySsb = null;
292
Adam Cohen091440a2015-03-18 14:16:05 -0700293 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800295 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296 private boolean mRestoring;
297 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700298 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299
Michael Jurka1e2f4652013-07-08 18:03:46 -0700300 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700301 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
302
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800303 private Bundle mSavedInstanceState;
304
Joe Onorato9c1289c2009-08-17 11:03:03 -0400305 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800306 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700307 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800308 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700309 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800310 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400311
Sunny Goyale2df0622015-04-24 11:27:00 -0700312 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700313
Adam Cohen61f560d2013-09-30 15:58:20 -0700314 private View.OnTouchListener mHapticFeedbackTouchListener;
315
Adam Cohended9f8d2010-11-03 13:25:16 -0700316 // Related to the auto-advancing of widgets
317 private final int ADVANCE_MSG = 1;
318 private final int mAdvanceInterval = 20000;
319 private final int mAdvanceStagger = 250;
320 private long mAutoAdvanceSentTime;
321 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700322 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700323 new HashMap<View, AppWidgetProviderInfo>();
324
Winson Chung400438b2011-01-16 17:53:48 -0800325 // Determines how long to wait after a rotation before restoring the screen orientation to
326 // match the sensor state.
327 private final int mRestoreScreenOrientationDelay = 500;
328
Adam Cohen091440a2015-03-18 14:16:05 -0700329 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700330
Adam Cohen1462de32012-07-24 22:34:36 -0700331 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700332 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700333
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700334 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700335 static Date sDateStamp = new Date();
336 static DateFormat sDateFormat =
337 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
338 static long sRunStart = System.currentTimeMillis();
339 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700340
Winson Chung46353de2012-02-16 14:05:10 -0800341 // We only want to get the SharedPreferences once since it does an FS stat each time we get
342 // it from the context.
343 private SharedPreferences mSharedPrefs;
344
Winson Chungf0c6ae02012-03-21 16:10:31 -0700345 // Holds the page that we need to animate to, and the icon views that we need to animate up
346 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700347 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700348 private Bitmap mFolderIconBitmap;
349 private Canvas mFolderIconCanvas;
350 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700351
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700352 private DeviceProfile mDeviceProfile;
353
Winson Chung13eb5272015-05-11 16:30:13 -0700354 // This is set to the view that launched the activity that navigated the user away from
355 // launcher. Since there is no callback for when the activity has finished launching, enable
356 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800357 private BubbleTextView mWaitingForResume;
358
Adam Cohen59400422014-03-05 18:07:04 -0800359 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
360 new HashMap<String, CustomAppWidget>();
361
362 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
363 static {
364 if (ENABLE_CUSTOM_WIDGET_TEST) {
365 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
366 }
367 }
368
Chet Haasea8f996d2015-02-17 12:56:04 -0800369 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
370 private static Method sClipRevealMethod = null;
371 static {
372 Class<?> activityOptionsClass = ActivityOptions.class;
373 try {
374 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
375 View.class, int.class, int.class, int.class, int.class);
376 } catch (Exception e) {
377 // Earlier version
378 }
379 }
380
Adam Cohen091440a2015-03-18 14:16:05 -0700381 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700382 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700383 if (mWorkspace != null) {
384 mWorkspace.buildPageHardwareLayers();
385 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700386 }
387 };
388
Adam Cohendb364c32014-05-20 14:23:40 -0700389 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800390
Adam Cohen091440a2015-03-18 14:16:05 -0700391 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800392 int requestCode;
393 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700394 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700395 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800396 int cellX;
397 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700398 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800399 }
400
Daniel Sandlerff02d492013-08-05 02:12:05 -0400401 private Stats mStats;
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700402 FocusIndicatorView mFocusHandler;
Sunny Goyal7779d622015-06-11 16:18:39 -0700403 private boolean mRotationEnabled = false;
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400404
405 @Thunk void setOrientation() {
406 if (mRotationEnabled) {
407 unlockScreenOrientation(true);
408 } else {
409 setRequestedOrientation(
410 ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700411 }
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400412 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700413
Sunny Goyal7779d622015-06-11 16:18:39 -0700414 private Runnable mUpdateOrientationRunnable = new Runnable() {
415 public void run() {
416 setOrientation();
417 }
418 };
419
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800420 @Override
421 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700422 if (DEBUG_STRICT_MODE) {
423 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
424 .detectDiskReads()
425 .detectDiskWrites()
426 .detectNetwork() // or .detectAll() for all detectable problems
427 .penaltyLog()
428 .build());
429 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
430 .detectLeakedSqlLiteObjects()
431 .detectLeakedClosableObjects()
432 .penaltyLog()
433 .penaltyDeath()
434 .build());
435 }
436
Adam Cohen9211d422014-10-07 18:14:53 -0700437 if (mLauncherCallbacks != null) {
438 mLauncherCallbacks.preOnCreate();
439 }
440
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800441 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400442
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400443 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400444 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700445 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700446
Adam Cohen2e6da152015-05-06 11:42:25 -0700447 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700448 mDeviceProfile = getResources().getConfiguration().orientation
449 == Configuration.ORIENTATION_LANDSCAPE ?
450 app.getInvariantDeviceProfile().landscapeProfile
451 : app.getInvariantDeviceProfile().portraitProfile;
452
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400453 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700454 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700455 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800456 mModel = app.setLauncher(this);
457 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700458
Joe Onorato41a12d22009-10-31 18:30:00 -0400459 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800461 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700462
Daniel Sandlerff02d492013-08-05 02:12:05 -0400463 mStats = new Stats(this);
464
Sunny Goyalffe83f12014-08-14 17:39:34 -0700465 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700466
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700467 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
468 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700469
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700470 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
471 // this also ensures that any synchronous binding below doesn't re-trigger another
472 // LauncherModel load.
473 mPaused = false;
474
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800475 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200476 android.os.Debug.startMethodTracing(
477 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 }
479
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800480 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700481
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700483 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800485 registerContentObservers();
486
Joe Onorato7c312c12009-08-13 21:36:53 -0700487 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700488
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 mSavedState = savedInstanceState;
490 restoreState(mSavedState);
491
492 if (PROFILE_STARTUP) {
493 android.os.Debug.stopMethodTracing();
494 }
495
496 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700497 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700498 // If the user leaves launcher, then we should just load items asynchronously when
499 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700500 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700501 } else {
502 // We only load the page synchronously if the user rotates (or triggers a
503 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700504 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700505 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800506 }
507
508 // For handling default keys
509 mDefaultKeySsb = new SpannableStringBuilder();
510 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800511
512 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
513 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800514
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700515 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
516 // In case we are on a device with locked rotation, we should look at preferences to check
517 // if the user has specifically allowed rotation.
518 if (!mRotationEnabled) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700519 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
520 }
521
522 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
523 // we want the screen to auto-rotate based on the current orientation
Rahul Chaturvedi799aa042015-06-01 21:26:41 -0400524 setOrientation();
Winson Chungaf40f202013-09-18 18:26:31 -0700525
Adam Cohen9211d422014-10-07 18:14:53 -0700526 if (mLauncherCallbacks != null) {
527 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700528 if (mLauncherCallbacks.hasLauncherOverlay()) {
529 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700530 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
531 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
532 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700533 mWorkspace.setLauncherOverlay(mLauncherOverlay);
534 }
Adam Cohen9211d422014-10-07 18:14:53 -0700535 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700536
537 if (shouldShowIntroScreen()) {
538 showIntroScreen();
539 } else {
540 showFirstRunActivity();
541 showFirstRunClings();
542 }
Adam Cohen9211d422014-10-07 18:14:53 -0700543 }
544
Sunny Goyal7779d622015-06-11 16:18:39 -0700545 @Override
546 public void onSettingsChanged(String settings, boolean value) {
547 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(settings)) {
548 mRotationEnabled = value;
549 if (!waitUntilResume(mUpdateOrientationRunnable, true)) {
550 mUpdateOrientationRunnable.run();
551 }
552 }
553 }
554
Adam Cohen9211d422014-10-07 18:14:53 -0700555 private LauncherCallbacks mLauncherCallbacks;
556
557 public void onPostCreate(Bundle savedInstanceState) {
558 super.onPostCreate(savedInstanceState);
559 if (mLauncherCallbacks != null) {
560 mLauncherCallbacks.onPostCreate(savedInstanceState);
561 }
562 }
563
564 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
565 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700566 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
567 @Override
568 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700569 if (LOGD) {
570 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
571 }
Winson Chung94804152015-05-08 13:06:44 -0700572 mAppsView.setFixedBounds(bounds);
573 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700574 }
575
576 @Override
577 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700578 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
579 // Dismiss All Apps if we aren't already paused/invisible
580 if (!mPaused) {
581 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
582 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
583 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700584 }
Winson Chung0f785722015-04-08 10:27:49 -0700585 }
Sunny Goyal5683f872015-05-29 14:54:40 -0700586
587 @Override
588 public void setSearchManager(AppSearchManager manager) {
589 mAppsView.setSearchManager(manager);
590 }
Winson Chung0f785722015-04-08 10:27:49 -0700591 });
Jun Mukai8af0cd82015-05-12 12:32:12 -0700592 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
593 private boolean mImportanceStored = false;
594 private int mWorkspaceImportanceForAccessibility =
595 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
596 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
597
598 @Override
599 public void onSearchOverlayOpened() {
600 if (mImportanceStored) {
601 return;
602 }
603 // The underlying workspace and hotseat are temporarily suppressed by the search
604 // overlay. So they sholudn't be accessible.
605 mWorkspaceImportanceForAccessibility = mWorkspace.getImportantForAccessibility();
606 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
607 mWorkspace.setImportantForAccessibility(
608 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
609 mHotseat.setImportantForAccessibility(
610 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
611 mImportanceStored = true;
612 }
613
614 @Override
615 public void onSearchOverlayClosed() {
616 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
617 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
618 mImportanceStored = false;
619 }
620 });
Adam Cohen9211d422014-10-07 18:14:53 -0700621 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700622 }
623
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700624 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700625 public void onLauncherProviderChange() {
626 if (mLauncherCallbacks != null) {
627 mLauncherCallbacks.onLauncherProviderChange();
628 }
629 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700630
Adam Cohen9211d422014-10-07 18:14:53 -0700631 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700632 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700633 if (mLauncherCallbacks != null) {
634 return mLauncherCallbacks.hasCustomContentToLeft();
635 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700636 return false;
637 }
638
Dave Hawkeya8881582013-09-17 15:55:33 -0600639 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500640 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600641 * {@link #addToCustomContentPage}. This will only be invoked if
642 * {@link #hasCustomContentToLeft()} is {@code true}.
643 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500644 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700645 if (mLauncherCallbacks != null) {
646 mLauncherCallbacks.populateCustomContentContainer();
647 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600648 }
649
650 /**
651 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
652 * ensure the custom content page is added or removed if necessary.
653 */
654 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600655 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600656 // Not bound yet, wait for bindScreens to be called.
657 return;
658 }
659
660 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
661 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500662 mWorkspace.createCustomContentContainer();
663 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600664 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
665 mWorkspace.removeCustomContentPage();
666 }
667 }
668
Anton Hanssona2f665f2013-09-26 12:18:32 +0100669 public Stats getStats() {
670 return mStats;
671 }
672
Bjorn Bringertc459e522013-06-07 19:36:01 +0100673 public LayoutInflater getInflater() {
674 return mInflater;
675 }
676
Hyunyoung Song3f471442015-04-08 19:01:34 -0700677 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700678 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
679 // that is subsequently removed from the workspace in startBinding().
680 return !mModel.isLoadingWorkspace();
681 }
682
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800683 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000684 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100685 if (Build.VERSION.SDK_INT >= 17) {
686 return View.generateViewId();
687 } else {
688 // View.generateViewId() is not available. The following fallback logic is a copy
689 // of its implementation.
690 for (;;) {
691 final int result = sNextGeneratedId.get();
692 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
693 int newValue = result + 1;
694 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
695 if (sNextGeneratedId.compareAndSet(result, newValue)) {
696 return result;
697 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000698 }
699 }
700 }
701
702 public int getViewIdForItem(ItemInfo info) {
703 // This cast is safe given the > 2B range for int.
704 int itemId = (int) info.id;
705 if (mItemIdToViewId.containsKey(itemId)) {
706 return mItemIdToViewId.get(itemId);
707 }
708 int viewId = generateViewId();
709 mItemIdToViewId.put(itemId, viewId);
710 return viewId;
711 }
712
713 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700714 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
715 * a configuration step, this allows the proper animations to run after other transitions.
716 */
Adam Cohendb364c32014-05-20 14:23:40 -0700717 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700718 long screenId = args.screenId;
719 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
720 // When the screen id represents an actual screen (as opposed to a rank) we make sure
721 // that the drop page actually exists.
722 screenId = ensurePendingDropLayoutExists(args.screenId);
723 }
Adam Cohendb364c32014-05-20 14:23:40 -0700724
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800725 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800726 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700727 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700728 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700729 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800730 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700731 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700732 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700733 case REQUEST_RECONFIGURE_APPWIDGET:
734 completeRestoreAppWidget(args.appWidgetId);
735 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800736 }
Michael Jurka27614d22012-04-02 06:40:22 -0700737 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
738 // if you turned the screen off and then back while in All Apps, Launcher would not
739 // return to the workspace. Clearing mAddInfo.container here fixes this issue
740 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700741 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800742 }
743
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800744 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700745 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700746 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700747 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700748 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800749 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700750
Adam Cohenad4e15c2013-10-17 16:21:35 -0700751 Runnable exitSpringLoaded = new Runnable() {
752 @Override
753 public void run() {
754 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
755 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
756 }
757 };
758
Michael Jurka8b805b12012-04-18 14:23:14 -0700759 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700760 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700761 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
762 if (resultCode == RESULT_CANCELED) {
763 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700764 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700765 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700766 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800767 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700768 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700769 }
770 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200771 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
772 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700773 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200774 }
775 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700776 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200777
Adam Cohened66b2b2012-01-23 17:28:51 -0800778 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700779 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700780
Adam Cohendb364c32014-05-20 14:23:40 -0700781 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800782 // We have special handling for widgets
783 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800784 final int appWidgetId;
785 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
786 : -1;
787 if (widgetId < 0) {
788 appWidgetId = pendingAddWidgetId;
789 } else {
790 appWidgetId = widgetId;
791 }
792
Adam Cohenad4e15c2013-10-17 16:21:35 -0700793 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800794 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700795 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
796 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700797 result = RESULT_CANCELED;
798 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700799 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700800 @Override
801 public void run() {
802 exitSpringLoadedDragModeDelayed(false, 0, null);
803 }
804 };
Adam Cohendb364c32014-05-20 14:23:40 -0700805 if (workspaceLocked) {
806 // No need to remove the empty screen if we're mid-binding, as the
807 // the bind will not add the empty screen.
808 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
809 } else {
810 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
811 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
812 }
Winson Chung5aaab772012-04-27 15:24:02 -0700813 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700814 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700815 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
816 // When the screen id represents an actual screen (as opposed to a rank)
817 // we make sure that the drop page actually exists.
818 mPendingAddInfo.screenId =
819 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
820 }
Adam Cohendb364c32014-05-20 14:23:40 -0700821 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
822
823 dropLayout.setDropPending(true);
824 final Runnable onComplete = new Runnable() {
825 @Override
826 public void run() {
827 completeTwoStageWidgetDrop(resultCode, appWidgetId);
828 dropLayout.setDropPending(false);
829 }
830 };
831 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
832 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
833 } else {
834 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
835 mPendingAddInfo);
836 sPendingAddItem = args;
837 }
Winson Chung5aaab772012-04-27 15:24:02 -0700838 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800839 return;
840 }
841
Sunny Goyalff572272014-07-23 13:58:07 -0700842 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
843 if (resultCode == RESULT_OK) {
844 // Update the widget view.
845 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
846 pendingAddWidgetId, mPendingAddInfo);
847 if (workspaceLocked) {
848 sPendingAddItem = args;
849 } else {
850 completeAdd(args);
851 }
852 }
853 // Leave the widget in the pending state if the user canceled the configure.
854 return;
855 }
856
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800857 // The pattern used here is that a user PICKs a specific application,
858 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700859
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800860 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
861 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700862 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700863 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
864 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800865 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700866 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800867 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700868 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700869 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
870 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 }
Adam Cohen00074722013-10-11 17:46:09 -0700872 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700873 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700874 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800875 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800876 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800877
878 }
879
880 @Override
881 protected void onActivityResult(
882 final int requestCode, final int resultCode, final Intent data) {
883 handleActivityResult(requestCode, resultCode, data);
884 if (mLauncherCallbacks != null) {
885 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
886 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800887 }
888
Adam Cohendb364c32014-05-20 14:23:40 -0700889 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
890 appWidgetId, ItemInfo info) {
891 PendingAddArguments args = new PendingAddArguments();
892 args.requestCode = requestCode;
893 args.intent = data;
894 args.container = info.container;
895 args.screenId = info.screenId;
896 args.cellX = info.cellX;
897 args.cellY = info.cellY;
898 args.appWidgetId = appWidgetId;
899 return args;
900 }
901
902 /**
903 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
904 *
905 * @param screenId the screen id to check
906 * @return the new screen, or screenId if it exists
907 */
908 private long ensurePendingDropLayoutExists(long screenId) {
909 CellLayout dropLayout =
910 (CellLayout) mWorkspace.getScreenWithId(screenId);
911 if (dropLayout == null) {
912 // it's possible that the add screen was removed because it was
913 // empty and a re-bind occurred
914 mWorkspace.addExtraEmptyScreen();
915 return mWorkspace.commitExtraEmptyScreen();
916 } else {
917 return screenId;
918 }
919 }
920
Adam Cohen091440a2015-03-18 14:16:05 -0700921 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700922 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700923 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800924 Runnable onCompleteRunnable = null;
925 int animationType = 0;
926
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700927 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800928 if (resultCode == RESULT_OK) {
929 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
930 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700931 mPendingAddWidgetInfo);
932 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800933 onCompleteRunnable = new Runnable() {
934 @Override
935 public void run() {
936 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700937 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700938 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
939 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800940 }
941 };
942 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800943 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800944 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800945 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700946 if (mDragLayer.getAnimatedView() != null) {
947 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
948 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
949 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700950 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700951 // The animated view may be null in the case of a rotation during widget configuration
952 onCompleteRunnable.run();
953 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800954 }
955
956 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700957 protected void onStop() {
958 super.onStop();
959 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700960
961 if (mLauncherCallbacks != null) {
962 mLauncherCallbacks.onStop();
963 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700964 }
965
966 @Override
967 protected void onStart() {
968 super.onStart();
969 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700970
971 if (mLauncherCallbacks != null) {
972 mLauncherCallbacks.onStart();
973 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700974 }
975
976 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800977 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200978 long startTime = 0;
979 if (DEBUG_RESUME_TIME) {
980 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400981 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200982 }
Adam Cohen9211d422014-10-07 18:14:53 -0700983
984 if (mLauncherCallbacks != null) {
985 mLauncherCallbacks.preOnResume();
986 }
987
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800988 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700989
Winson Chung4a2afa32012-07-19 14:53:05 -0700990 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700991 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700992 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800993 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700994 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700995 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700996 // view after launching an app, as they may be depending on the UI to be static to
997 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700998 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -0700999 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08001000 } else if (mOnResumeState == State.WIDGETS) {
1001 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001002 }
1003 mOnResumeState = State.NONE;
1004
Winson Chungcd99cd32015-04-29 11:03:24 -07001005 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001006 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1007 // Otherwise, notify the callbacks if we are in all apps mode
1008 if (mState == State.APPS) {
1009 if (mLauncherCallbacks != null) {
1010 mLauncherCallbacks.onAllAppsShown();
1011 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001012 }
1013 }
1014
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001015 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001016 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1017 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001018
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001019 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001020 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001021 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001022 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001023 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001024 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001025 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001026 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001027 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1028 // execute them here
1029 long startTimeCallbacks = 0;
1030 if (DEBUG_RESUME_TIME) {
1031 startTimeCallbacks = System.currentTimeMillis();
1032 }
1033
Michael Jurka1e2f4652013-07-08 18:03:46 -07001034 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1035 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001036 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001037 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001038 if (DEBUG_RESUME_TIME) {
1039 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1040 (System.currentTimeMillis() - startTimeCallbacks));
1041 }
Michael Jurka447bf842013-05-15 14:52:15 +02001042 }
Michael Jurka54554252013-08-01 12:52:23 +02001043 if (mOnResumeCallbacks.size() > 0) {
1044 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1045 mOnResumeCallbacks.get(i).run();
1046 }
1047 mOnResumeCallbacks.clear();
1048 }
Winson Chunge4e50662012-01-23 14:45:13 -08001049
1050 // Reset the pressed state of icons that were locked in the press state while activities
1051 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001052 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001053 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001054 mWaitingForResume.setStayPressed(false);
1055 }
Winson Chung82963d52013-10-09 11:20:57 -07001056
Adam Cohen06dff352012-06-01 17:17:08 -07001057 // It is possible that widgets can receive updates while launcher is not in the foreground.
1058 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1059 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1060 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001061 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001062 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001063
Michael Jurka447bf842013-05-15 14:52:15 +02001064 if (DEBUG_RESUME_TIME) {
1065 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1066 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001067
1068 if (mWorkspace.getCustomContentCallbacks() != null) {
1069 // If we are resuming and the custom content is the current page, we call onShow().
1070 // It is also poassible that onShow will instead be called slightly after first layout
1071 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1072 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001073 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001074 }
1075 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001076 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001077 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001078
Sunny Goyal756adbc2015-04-16 15:20:51 -07001079 if (!isWorkspaceLoading()) {
1080 // Process any items that were added while Launcher was away.
1081 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1082 }
Adam Cohen9211d422014-10-07 18:14:53 -07001083
1084 if (mLauncherCallbacks != null) {
1085 mLauncherCallbacks.onResume();
1086 }
Adam Cohen06dff352012-06-01 17:17:08 -07001087 }
1088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001089 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001090 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001091 // Ensure that items added to Launcher are queued until Launcher returns
1092 InstallShortcutReceiver.enableInstallQueue();
1093
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001094 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001095 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001096 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001097 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001098
1099 // We call onHide() aggressively. The custom content callbacks should be able to
1100 // debounce excess onHide calls.
1101 if (mWorkspace.getCustomContentCallbacks() != null) {
1102 mWorkspace.getCustomContentCallbacks().onHide();
1103 }
Romain Guycbb89e42009-06-08 15:52:54 -07001104
Adam Cohen9211d422014-10-07 18:14:53 -07001105 if (mLauncherCallbacks != null) {
1106 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001107 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001108 }
1109
1110 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001111 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1112 // by a onResume or by scrolling otherwise.
1113 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001114
1115 // Custom content is completely hidden
1116 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001117
1118 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1119 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001120
1121 // Indicates whether the user is allowed to scroll away from the custom content.
1122 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001123 }
1124
Adam Cohenc2d6e892014-10-16 09:49:24 -07001125 public interface LauncherOverlay {
1126
1127 /**
1128 * Touch interaction leading to overscroll has begun
1129 */
1130 public void onScrollInteractionBegin();
1131
1132 /**
1133 * Touch interaction related to overscroll has ended
1134 */
1135 public void onScrollInteractionEnd();
1136
1137 /**
1138 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1139 * screen (or in the case of RTL, the rightmost screen).
1140 */
1141 public void onScrollChange(int progress, boolean rtl);
1142
1143 /**
1144 * Screen has stopped scrolling
1145 */
1146 public void onScrollSettled();
1147
1148 /**
1149 * This method can be called by the Launcher in order to force the LauncherOverlay
1150 * to exit fully immersive mode.
1151 */
1152 public void forceExitFullImmersion();
1153 }
1154
Winson Chung0f785722015-04-08 10:27:49 -07001155 public interface LauncherAppsCallbacks {
1156 /**
1157 * Updates launcher to the available space that AllApps can take so as not to overlap with
1158 * any other views.
1159 */
1160 public void onAllAppsBoundsChanged(Rect bounds);
1161
1162 /**
1163 * Called to dismiss all apps if it is showing.
1164 */
1165 public void dismissAllApps();
Sunny Goyal5683f872015-05-29 14:54:40 -07001166
1167 /**
1168 * Sets the search manager to be used for app search.
1169 */
1170 public void setSearchManager(AppSearchManager manager);
Winson Chung0f785722015-04-08 10:27:49 -07001171 }
1172
Jun Mukai8af0cd82015-05-12 12:32:12 -07001173 public interface LauncherSearchCallbacks {
1174 /**
1175 * Called when the search overlay is shown.
1176 */
1177 public void onSearchOverlayOpened();
1178
1179 /**
1180 * Called when the search overlay is dismissed.
1181 */
1182 public void onSearchOverlayClosed();
1183 }
1184
Adam Cohenc2d6e892014-10-16 09:49:24 -07001185 public interface LauncherOverlayCallbacks {
1186 /**
1187 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1188 * however it doesn't modify any state within the launcher.
1189 */
1190 public boolean canEnterFullImmersion();
1191
1192 /**
1193 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1194 * eg. by occupying the full screen and handling all touch events.
1195 *
1196 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1197 * case, Launcher will modify any necessary state and assumes the overlay is
1198 * handling all interaction. If false, the LauncherOverlay should cancel any
1199 *
1200 */
1201 public boolean enterFullImmersion();
1202
1203 /**
1204 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1205 * full control over UI and state.
1206 */
1207 public void exitFullImmersion();
1208 }
1209
1210 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1211
1212 @Override
1213 public boolean canEnterFullImmersion() {
1214 return mState == State.WORKSPACE;
1215 }
1216
1217 @Override
1218 public boolean enterFullImmersion() {
1219 if (mState == State.WORKSPACE) {
1220 // When fully immersed, disregard any touches which fall through.
1221 mDragLayer.setBlockTouch(true);
1222 return true;
1223 }
1224 return false;
1225 }
1226
1227 @Override
1228 public void exitFullImmersion() {
1229 mDragLayer.setBlockTouch(false);
1230 }
1231 }
1232
Jorim Jaggid017f882014-01-14 17:08:48 -08001233 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001234 if (mLauncherCallbacks != null) {
1235 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001236 } else {
1237 // On devices with a locked orientation, we will at least have the allow rotation
1238 // setting.
1239 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001240 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001241 }
1242
Adam Cohen9211d422014-10-07 18:14:53 -07001243 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001244 CustomContentCallbacks callbacks, String description) {
1245 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001246 }
1247
Adam Cohenedb40762013-07-18 16:45:45 -07001248 // The custom content needs to offset its content to account for the QSB
1249 public int getTopOffsetForCustomContent() {
1250 return mWorkspace.getPaddingTop();
1251 }
1252
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001253 @Override
1254 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001255 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001256 if (mModel.isCurrentCallbacks(this)) {
1257 mModel.stopLoader();
1258 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001259 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1260
Romain Guy13c2e7b2010-03-10 19:45:00 -08001261 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001262 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001263
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001264 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001265 @Override
1266 public void onWindowFocusChanged(boolean hasFocus) {
1267 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001268 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001269
1270 if (mLauncherCallbacks != null) {
1271 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1272 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001273 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001274
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001275 private boolean acceptFilter() {
1276 final InputMethodManager inputManager = (InputMethodManager)
1277 getSystemService(Context.INPUT_METHOD_SERVICE);
1278 return !inputManager.isFullscreenMode();
1279 }
1280
1281 @Override
1282 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001283 final int uniChar = event.getUnicodeChar();
1284 final boolean handled = super.onKeyDown(keyCode, event);
1285 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1286 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1288 keyCode, event);
1289 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001290 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001291 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001292 // showSearchDialog()
1293 // If there are multiple keystrokes before the search dialog takes focus,
1294 // onSearchRequested() will be called for every keystroke,
1295 // but it is idempotent, so it's fine.
1296 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001297 }
1298 }
1299
Joe Onorato8a9625e2010-01-28 15:55:35 -08001300 // Eat the long press event so the keyboard doesn't come up.
1301 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1302 return true;
1303 }
1304
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001305 return handled;
1306 }
1307
Karl Rosaen138a0412009-04-23 19:00:21 -07001308 private String getTypedText() {
1309 return mDefaultKeySsb.toString();
1310 }
1311
1312 private void clearTypedText() {
1313 mDefaultKeySsb.clear();
1314 mDefaultKeySsb.clearSpans();
1315 Selection.setSelection(mDefaultKeySsb, 0);
1316 }
1317
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001319 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1320 * State
1321 */
1322 private static State intToState(int stateOrdinal) {
1323 State state = State.WORKSPACE;
1324 final State[] stateValues = State.values();
1325 for (int i = 0; i < stateValues.length; i++) {
1326 if (stateValues[i].ordinal() == stateOrdinal) {
1327 state = stateValues[i];
1328 break;
1329 }
1330 }
1331 return state;
1332 }
1333
1334 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001335 * Restores the previous state, if it exists.
1336 *
1337 * @param savedState The previous state.
1338 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001339 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001340 private void restoreState(Bundle savedState) {
1341 if (savedState == null) {
1342 return;
1343 }
1344
Michael Jurka883f55b2010-10-21 15:47:14 -07001345 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001346 if (state == State.APPS || state == State.WIDGETS) {
1347 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001348 }
1349
Adam Cohen21cd0022013-10-09 18:57:02 -07001350 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1351 PagedView.INVALID_RESTORE_PAGE);
1352 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001353 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001354 }
1355
Winson Chung3d503fb2011-07-13 17:25:49 -07001356 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001357 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001358
Winson Chung3d503fb2011-07-13 17:25:49 -07001359 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1360 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001361 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001362 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1363 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001364 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1365 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001366 AppWidgetProviderInfo info = savedState.getParcelable(
1367 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001368 mPendingAddWidgetInfo = info == null ?
1369 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1370
Adam Cohen4637b5a2013-11-04 18:21:24 -08001371 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001372 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 mRestoring = true;
1374 }
1375
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001376 mItemIdToViewId = (HashMap<Integer, Integer>)
1377 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001378 }
1379
1380 /**
1381 * Finds all the views we need and configure them properly.
1382 */
1383 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001384 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001385
Craig Mautner360310b2012-10-26 15:13:08 -07001386 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001387 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001388 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1389 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001390 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001391 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001392
John Spurlock77e1f472013-09-11 10:09:51 -04001393 mLauncherView.setSystemUiVisibility(
1394 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001395 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001396
Winson Chung4c98d922011-05-31 16:50:48 -07001397 // Setup the drag layer
1398 mDragLayer.setup(this, dragController);
1399
Winson Chung3d503fb2011-07-13 17:25:49 -07001400 // Setup the hotseat
1401 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1402 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001403 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001404 }
1405
Jorim Jaggid017f882014-01-14 17:08:48 -08001406 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001407 View widgetButton = findViewById(R.id.widget_button);
1408 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001409 @Override
1410 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001411 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001412 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001413 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001414 }
1415 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001416 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1417
1418 View wallpaperButton = findViewById(R.id.wallpaper_button);
1419 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001420 @Override
1421 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001422 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001423 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001424 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001425 }
1426 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001427 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1428
1429 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001430 if (hasSettings()) {
1431 settingsButton.setOnClickListener(new OnClickListener() {
1432 @Override
1433 public void onClick(View arg0) {
1434 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001435 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001436 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001437 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001438 });
1439 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1440 } else {
1441 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001442 }
1443
Adam Cohendbdff6b2013-09-18 19:09:15 -07001444 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001445
Winson Chung4c98d922011-05-31 16:50:48 -07001446 // Setup the workspace
1447 mWorkspace.setHapticFeedbackEnabled(false);
1448 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001449 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001450 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001451
Winson Chungf0ea4d32011-06-06 14:27:16 -07001452 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001453 mSearchDropTargetBar = (SearchDropTargetBar)
1454 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001455
Winson Chungb745afb2015-03-02 11:51:23 -08001456 // Setup Apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001457 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001458 if (isAllAppsSearchOverridden()) {
1459 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001460 }
Winson Chungb745afb2015-03-02 11:51:23 -08001461
Winson Chungf0ea4d32011-06-06 14:27:16 -07001462 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001463 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001464
Winson Chung3d503fb2011-07-13 17:25:49 -07001465 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001466 dragController.setDragScoller(mWorkspace);
1467 dragController.setScrollView(mDragLayer);
1468 dragController.setMoveTarget(mWorkspace);
1469 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001470 if (mSearchDropTargetBar != null) {
1471 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001472 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001473 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001474
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001475 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001476 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001477 mWeightWatcher = new WeightWatcher(this);
1478 mWeightWatcher.setAlpha(0.5f);
1479 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001480 new FrameLayout.LayoutParams(
1481 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001482 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001483 Gravity.BOTTOM)
1484 );
Adam Cohen39a06042013-07-19 14:30:12 -07001485
1486 boolean show = shouldShowWeightWatcher();
1487 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001488 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001489 }
1490
1491 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001492 * Sets the all apps button. This method is called from {@link Hotseat}.
1493 */
1494 public void setAllAppsButton(View allAppsButton) {
1495 mAllAppsButton = allAppsButton;
1496 }
1497
1498 public View getAllAppsButton() {
1499 return mAllAppsButton;
1500 }
1501
1502 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 * Creates a view representing a shortcut.
1504 *
1505 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001507 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001508 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001509 }
1510
1511 /**
1512 * Creates a view representing a shortcut inflated from the specified resource.
1513 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001514 * @param parent The group the shortcut belongs to.
1515 * @param info The data structure describing the shortcut.
1516 *
1517 * @return A View inflated from layoutResId.
1518 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001519 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001520 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001521 parent, false);
1522 favorite.applyFromShortcutInfo(info, mIconCache);
1523 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001525 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001526 return favorite;
1527 }
1528
1529 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001530 * Add a shortcut to the workspace.
1531 *
1532 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001533 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001534 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001535 int cellY) {
1536 int[] cellXY = mTmpAddItemCellCoordinates;
1537 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001538 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001539
Sunny Goyal5c97f512015-05-19 16:03:28 -07001540 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001541 if (info == null) {
1542 return;
1543 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001544 final View view = createShortcut(info);
1545
Sunny Goyal5c97f512015-05-19 16:03:28 -07001546 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001547 // First we check if we already know the exact location where we want to add this item.
1548 if (cellX >= 0 && cellY >= 0) {
1549 cellXY[0] = cellX;
1550 cellXY[1] = cellY;
1551 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001552
1553 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001554 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001555 true, null,null)) {
1556 return;
1557 }
1558 DragObject dragObject = new DragObject();
1559 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001560 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1561 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001562 return;
1563 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001564 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001565 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001566 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001567 foundCellSpan = (result != null);
1568 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001569 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001570 }
1571
1572 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001573 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001574 return;
1575 }
1576
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001577 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578
1579 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001580 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001581 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 }
1583 }
1584
Adam Cohen2e6da152015-05-06 11:42:25 -07001585 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1586 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001587 // We want to account for the extra amount of padding that we are adding to the widget
1588 // to ensure that it gets the full amount of space that it has requested
1589 int requiredWidth = minWidth + padding.left + padding.right;
1590 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2e6da152015-05-06 11:42:25 -07001591 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001592 }
1593
Adam Cohen2e6da152015-05-06 11:42:25 -07001594 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1595 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001596 }
1597
Adam Cohen2e6da152015-05-06 11:42:25 -07001598 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1599 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001600 }
1601
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001603 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001605 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001606 */
Adam Cohen091440a2015-03-18 14:16:05 -07001607 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001608 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1609
1610 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001611 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001612 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1613 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001614 }
Romain Guycbb89e42009-06-08 15:52:54 -07001615
Adam Cohen59400422014-03-05 18:07:04 -08001616 if (appWidgetInfo.isCustomWidget) {
1617 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001618 }
1619
Adam Cohen59400422014-03-05 18:07:04 -08001620 LauncherAppWidgetInfo launcherInfo;
1621 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1622 launcherInfo.spanX = info.spanX;
1623 launcherInfo.spanY = info.spanY;
1624 launcherInfo.minSpanX = info.minSpanX;
1625 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001626 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001627
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001629 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630
1631 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001632 if (hostView == null) {
1633 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001634 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1635 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001636 } else {
1637 // The AppWidgetHostView has already been inflated and instantiated
1638 launcherInfo.hostView = hostView;
1639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001641 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001642 launcherInfo.notifyWidgetSizeChanged(this);
1643
Adam Cohen59400422014-03-05 18:07:04 -08001644 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1645 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001646
1647 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001649 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 }
Romain Guycbb89e42009-06-08 15:52:54 -07001651
Adam Cohended9f8d2010-11-03 13:25:16 -07001652 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1653 @Override
1654 public void onReceive(Context context, Intent intent) {
1655 final String action = intent.getAction();
1656 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1657 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001658 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001659 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001660
Winson Chungc100e8e2011-08-09 16:02:43 -07001661 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001662 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001663 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001664 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001665 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001666 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001667 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1668 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001669 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001670 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1671 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001672 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001673 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1674 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1675 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001676 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001677 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1678 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001679 }
1680 }
1681 };
1682
1683 @Override
1684 public void onAttachedToWindow() {
1685 super.onAttachedToWindow();
1686
1687 // Listen for broadcasts related to user-presence
1688 final IntentFilter filter = new IntentFilter();
1689 filter.addAction(Intent.ACTION_SCREEN_OFF);
1690 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001691 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001692 if (ENABLE_DEBUG_INTENTS) {
1693 filter.addAction(DebugIntents.DELETE_DATABASE);
1694 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1695 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001696 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001697 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001698 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001699 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001700 mVisible = true;
1701 }
1702
Adam Cohen0b395352014-06-09 22:54:36 +00001703 /**
1704 * Sets up transparent navigation and status bars in LMP.
1705 * This method is a no-op for other platform versions.
1706 */
Sunny Goyald0091012015-01-20 15:55:34 -08001707 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001708 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001709 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001710 Window window = getWindow();
1711 window.getAttributes().systemUiVisibility |=
1712 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1713 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1714 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1715 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1716 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1717 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1718 window.setStatusBarColor(Color.TRANSPARENT);
1719 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001720 }
1721 }
1722
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 @Override
1724 public void onDetachedFromWindow() {
1725 super.onDetachedFromWindow();
1726 mVisible = false;
1727
Adam Cohend113e0c2010-11-11 10:48:05 -08001728 if (mAttached) {
1729 unregisterReceiver(mReceiver);
1730 mAttached = false;
1731 }
Winson Chungb745afb2015-03-02 11:51:23 -08001732 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 }
1734
1735 public void onWindowVisibilityChanged(int visibility) {
1736 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001737 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001738 // The following code used to be in onResume, but it turns out onResume is called when
1739 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1740 // is a more appropriate event to handle
1741 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001742 if (!mWorkspaceLoading) {
1743 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001744 // We want to let Launcher draw itself at least once before we force it to build
1745 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001746 // apps is nice and speedy.
1747 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001748 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001749 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001750 if (mStarted) return;
1751 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001752 // We delay the layer building a bit in order to give
1753 // other message processing a time to run. In particular
1754 // this avoids a delay in hiding the IME if it was
1755 // currently shown, because doing that may involve
1756 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001757 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001758 final ViewTreeObserver.OnDrawListener listener = this;
1759 mWorkspace.post(new Runnable() {
1760 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001761 if (mWorkspace != null &&
1762 mWorkspace.getViewTreeObserver() != null) {
1763 mWorkspace.getViewTreeObserver().
1764 removeOnDrawListener(listener);
1765 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001766 }
1767 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001768 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001769 }
1770 });
1771 }
1772 clearTypedText();
1773 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001774 }
1775
Adam Cohen091440a2015-03-18 14:16:05 -07001776 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001777 mHandler.removeMessages(ADVANCE_MSG);
1778 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1779 mHandler.sendMessageDelayed(msg, delay);
1780 mAutoAdvanceSentTime = System.currentTimeMillis();
1781 }
1782
Adam Cohen091440a2015-03-18 14:16:05 -07001783 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001784 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1785 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1786 mAutoAdvanceRunning = autoAdvanceRunning;
1787 if (autoAdvanceRunning) {
1788 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1789 sendAdvanceMessage(delay);
1790 } else {
1791 if (!mWidgetsToAdvance.isEmpty()) {
1792 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1793 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1794 }
1795 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001796 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001797 }
1798 }
1799 }
1800
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001801 @Thunk final Handler mHandler = new Handler(new Handler.Callback() {
1802
Adam Cohended9f8d2010-11-03 13:25:16 -07001803 @Override
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001804 public boolean handleMessage(Message msg) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001805 if (msg.what == ADVANCE_MSG) {
1806 int i = 0;
1807 for (View key: mWidgetsToAdvance.keySet()) {
1808 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1809 final int delay = mAdvanceStagger * i;
1810 if (v instanceof Advanceable) {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001811 mHandler.postDelayed(new Runnable() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001812 public void run() {
1813 ((Advanceable) v).advance();
1814 }
1815 }, delay);
1816 }
1817 i++;
1818 }
1819 sendAdvanceMessage(mAdvanceInterval);
1820 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001821 return true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001822 }
Sunny Goyal4f3e9382015-06-05 00:13:25 -07001823 });
Adam Cohended9f8d2010-11-03 13:25:16 -07001824
1825 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001826 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001827 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1828 if (v instanceof Advanceable) {
1829 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001830 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001831 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001832 }
1833 }
1834
1835 void removeWidgetToAutoAdvance(View hostView) {
1836 if (mWidgetsToAdvance.containsKey(hostView)) {
1837 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001838 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001839 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001840 }
1841
Joe Onorato9c1289c2009-08-17 11:03:03 -04001842 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001843 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001844 launcherInfo.hostView = null;
1845 }
1846
Hyunyoung Song3f471442015-04-08 19:01:34 -07001847 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001848 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1849 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001850 }
1851
Winson Chunga6945242014-01-08 14:04:34 -08001852 public DragLayer getDragLayer() {
1853 return mDragLayer;
1854 }
1855
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001856 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001857 return mAppsView;
1858 }
1859
Hyunyoung Song3f471442015-04-08 19:01:34 -07001860 public WidgetsContainerView getWidgetsView() {
1861 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001862 }
1863
Winson Chunga6945242014-01-08 14:04:34 -08001864 public Workspace getWorkspace() {
1865 return mWorkspace;
1866 }
1867
1868 public Hotseat getHotseat() {
1869 return mHotseat;
1870 }
1871
Jorim Jaggid017f882014-01-14 17:08:48 -08001872 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001873 return mOverviewPanel;
1874 }
1875
1876 public SearchDropTargetBar getSearchBar() {
1877 return mSearchDropTargetBar;
1878 }
1879
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001880 public LauncherAppWidgetHost getAppWidgetHost() {
1881 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001882 }
Romain Guycbb89e42009-06-08 15:52:54 -07001883
Winson Chunga9abd0e2010-10-27 17:18:37 -07001884 public LauncherModel getModel() {
1885 return mModel;
1886 }
1887
Winson Chunga6945242014-01-08 14:04:34 -08001888 protected SharedPreferences getSharedPrefs() {
1889 return mSharedPrefs;
1890 }
1891
Adam Cohen2e6da152015-05-06 11:42:25 -07001892 public DeviceProfile getDeviceProfile() {
1893 return mDeviceProfile;
1894 }
1895
Bjorn Bringertc459e522013-06-07 19:36:01 +01001896 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001897 getWindow().closeAllPanels();
1898
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001899 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001900 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001901 }
1902
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 @Override
1904 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001905 long startTime = 0;
1906 if (DEBUG_RESUME_TIME) {
1907 startTime = System.currentTimeMillis();
1908 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001909 super.onNewIntent(intent);
1910
1911 // Close the menu
1912 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001913 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001914 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001915
Adam Cohened307df2013-10-02 09:37:31 -07001916 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1917 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1918 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001919
Adam Cohen6fecd412013-10-02 17:41:50 -07001920 if (mWorkspace == null) {
1921 // Can be cases where mWorkspace is null, this prevents a NPE
1922 return;
1923 }
1924 Folder openFolder = mWorkspace.getOpenFolder();
1925 // In all these cases, only animate if we're already on home
1926 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001927
1928 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1929 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001930 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001931 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001932 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001933 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001934
Adam Cohen6fecd412013-10-02 17:41:50 -07001935 closeFolder();
1936 exitSpringLoadedDragMode();
1937
1938 // If we are already on home, then just animate back to the workspace,
1939 // otherwise, just wait until onResume to set the state back to Workspace
1940 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001941 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001942 } else {
1943 mOnResumeState = State.WORKSPACE;
1944 }
1945
1946 final View v = getWindow().peekDecorView();
1947 if (v != null && v.getWindowToken() != null) {
1948 InputMethodManager imm = (InputMethodManager)getSystemService(
1949 INPUT_METHOD_SERVICE);
1950 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1951 }
1952
Winson Chungb745afb2015-03-02 11:51:23 -08001953 // Reset the apps view
1954 if (!alreadyOnHome && mAppsView != null) {
1955 mAppsView.scrollToTop();
1956 }
1957
Hyunyoung Song3f471442015-04-08 19:01:34 -07001958 // Reset the widgets view
1959 if (!alreadyOnHome && mWidgetsView != null) {
1960 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001961 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001962
Adam Cohen9211d422014-10-07 18:14:53 -07001963 if (mLauncherCallbacks != null) {
1964 mLauncherCallbacks.onHomeIntent();
1965 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 }
Adam Cohened307df2013-10-02 09:37:31 -07001967
Michael Jurka447bf842013-05-15 14:52:15 +02001968 if (DEBUG_RESUME_TIME) {
1969 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1970 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001971
Adam Cohen9211d422014-10-07 18:14:53 -07001972 if (mLauncherCallbacks != null) {
1973 mLauncherCallbacks.onNewIntent(intent);
1974 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001975 }
1976
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001977 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001978 public void onRestoreInstanceState(Bundle state) {
1979 super.onRestoreInstanceState(state);
1980 for (int page: mSynchronouslyBoundPages) {
1981 mWorkspace.restoreInstanceStateForChild(page);
1982 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001983 }
1984
1985 @Override
1986 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001987 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001988 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1989 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001990 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001991 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001992
Michael Jurka883f55b2010-10-21 15:47:14 -07001993 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001994 // We close any open folder since it will not be re-opened, and we need to make sure
1995 // this state is reflected.
1996 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997
Adam Cohendcd297f2013-06-18 13:13:40 -07001998 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001999 mWaitingForResult) {
2000 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002001 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002002 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2003 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002004 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2005 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2006 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002007 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002008 }
2009
Hyunyoung Song3f471442015-04-08 19:01:34 -07002010 // Save the current widgets tray?
2011 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002012 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002013
2014 if (mLauncherCallbacks != null) {
2015 mLauncherCallbacks.onSaveInstanceState(outState);
2016 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 }
2018
2019 @Override
2020 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002021 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002022
Winson Chunge7a03942011-08-05 15:05:12 -07002023 // Remove all pending runnables
2024 mHandler.removeMessages(ADVANCE_MSG);
2025 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002026 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002027
Winson Chungcd2b0142011-06-08 16:02:26 -07002028 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002029 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002030
2031 // It's possible to receive onDestroy after a new Launcher activity has
2032 // been created. In this case, don't interfere with the new Launcher.
2033 if (mModel.isCurrentCallbacks(this)) {
2034 mModel.stopLoader();
2035 app.setLauncher(null);
2036 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002037
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002039 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002041 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002043 mAppWidgetHost = null;
2044
2045 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002046
2047 TextKeyListener.getInstance().release();
2048
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002049 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002050 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002051
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002052 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002053 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002054 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002055 mWorkspace = null;
2056 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002057
Michael Jurka2ecf9952012-06-18 12:52:28 -07002058 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002059
2060 if (mLauncherCallbacks != null) {
2061 mLauncherCallbacks.onDestroy();
2062 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 }
2064
Adam Cohena9cf38f2011-05-02 15:36:58 -07002065 public DragController getDragController() {
2066 return mDragController;
2067 }
2068
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002069 @Override
2070 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002071 onStartForResult(requestCode);
2072 super.startActivityForResult(intent, requestCode);
2073 }
2074
2075 @Override
2076 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2077 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2078 onStartForResult(requestCode);
2079 try {
2080 super.startIntentSenderForResult(intent, requestCode,
2081 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2082 } catch (IntentSender.SendIntentException e) {
2083 throw new ActivityNotFoundException();
2084 }
2085 }
2086
2087 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002088 if (requestCode >= 0) {
2089 setWaitingForResult(true);
2090 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002091 }
2092
Winson Chung70d72102011-08-12 11:24:35 -07002093 /**
2094 * Indicates that we want global search for this activity by setting the globalSearch
2095 * argument for {@link #startSearch} to true.
2096 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002097 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002098 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002099 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002100
Karl Rosaen138a0412009-04-23 19:00:21 -07002101 if (initialQuery == null) {
2102 // Use any text typed in the launcher as the initial query
2103 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002104 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 if (appSearchData == null) {
2106 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002107 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 }
Winson Chungadf0c182012-08-23 14:59:07 -07002109 Rect sourceBounds = new Rect();
2110 if (mSearchDropTargetBar != null) {
2111 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2112 }
Romain Guycbb89e42009-06-08 15:52:54 -07002113
Ian Parkinson047036e2014-09-01 15:40:53 +01002114 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002115 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002116 if (clearTextImmediately) {
2117 clearTypedText();
2118 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002119
2120 // We need to show the workspace after starting the search
2121 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002122 }
2123
Ian Parkinson047036e2014-09-01 15:40:53 +01002124 /**
2125 * Start a text search.
2126 *
2127 * @return {@code true} if the search will start immediately, so any further keypresses
2128 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2129 * to buffer keypresses.
2130 */
2131 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002132 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002133 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2134 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2135 sourceBounds);
2136 }
2137
Michael Jurkaa33411c2012-06-14 16:18:21 -07002138 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002139 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002140 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002141 }
2142
2143 /**
2144 * Starts the global search activity. This code is a copied from SearchManager
2145 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002146 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002147 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002148 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002149 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2150 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2151 if (globalSearchActivity == null) {
2152 Log.w(TAG, "No global search activity found.");
2153 return;
2154 }
2155 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2156 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2157 intent.setComponent(globalSearchActivity);
2158 // Make sure that we have a Bundle to put source in
2159 if (appSearchData == null) {
2160 appSearchData = new Bundle();
2161 } else {
2162 appSearchData = new Bundle(appSearchData);
2163 }
Adam Cohen9211d422014-10-07 18:14:53 -07002164 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002165 if (!appSearchData.containsKey("source")) {
2166 appSearchData.putString("source", getPackageName());
2167 }
2168 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2169 if (!TextUtils.isEmpty(initialQuery)) {
2170 intent.putExtra(SearchManager.QUERY, initialQuery);
2171 }
2172 if (selectInitialQuery) {
2173 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2174 }
2175 intent.setSourceBounds(sourceBounds);
2176 try {
2177 startActivity(intent);
2178 } catch (ActivityNotFoundException ex) {
2179 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2180 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002181 }
2182
Yura4f93ec62014-02-04 14:15:21 +00002183 public boolean isOnCustomContent() {
2184 return mWorkspace.isOnOrMovingToCustomContent();
2185 }
2186
Winson Chung70d72102011-08-12 11:24:35 -07002187 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002188 public boolean onPrepareOptionsMenu(Menu menu) {
2189 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002190 if (!isOnCustomContent()) {
2191 // Close any open folders
2192 closeFolder();
2193 // Stop resizing any widgets
2194 mWorkspace.exitWidgetResizeMode();
2195 if (!mWorkspace.isInOverviewMode()) {
2196 // Show the overview mode
2197 showOverviewMode(true);
2198 } else {
2199 showWorkspace(true);
2200 }
Winson Chunge0298742014-01-17 12:03:00 -08002201 }
Adam Cohen9211d422014-10-07 18:14:53 -07002202 if (mLauncherCallbacks != null) {
2203 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2204 }
2205
Michael Jurkab94f3f82013-09-11 18:08:54 +02002206 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002207 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002208
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002209 @Override
2210 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002211 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002212 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002213 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002214 }
2215
Joe Onorato9c1289c2009-08-17 11:03:03 -04002216 public boolean isWorkspaceLocked() {
2217 return mWorkspaceLoading || mWaitingForResult;
2218 }
2219
Adam Cohen517a7f52014-03-01 12:12:59 -08002220 public boolean isWorkspaceLoading() {
2221 return mWorkspaceLoading;
2222 }
2223
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002224 private void setWorkspaceLoading(boolean value) {
2225 boolean isLocked = isWorkspaceLocked();
2226 mWorkspaceLoading = value;
2227 if (isLocked != isWorkspaceLocked()) {
2228 onWorkspaceLockedChanged();
2229 }
2230 }
2231
2232 private void setWaitingForResult(boolean value) {
2233 boolean isLocked = isWorkspaceLocked();
2234 mWaitingForResult = value;
2235 if (isLocked != isWorkspaceLocked()) {
2236 onWorkspaceLockedChanged();
2237 }
2238 }
2239
Adam Cohen9211d422014-10-07 18:14:53 -07002240 protected void onWorkspaceLockedChanged() {
2241 if (mLauncherCallbacks != null) {
2242 mLauncherCallbacks.onWorkspaceLockedChanged();
2243 }
2244 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002245
Michael Jurka0280c3b2010-09-17 15:00:07 -07002246 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002247 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002248 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002249 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2250 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002251 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002252 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002253 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002254
Sunny Goyale6b63a32015-01-16 16:14:29 -08002255 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002256 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002257 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2258 }
2259
Sunny Goyale6b63a32015-01-16 16:14:29 -08002260 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002261 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2262 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002263 if (appWidgetInfo.configure != null) {
2264 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002265 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002266
Bjorn Bringert7984c942009-12-09 15:38:25 +00002267 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002268 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2269 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002271 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002272 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002273 Runnable onComplete = new Runnable() {
2274 @Override
2275 public void run() {
2276 // Exit spring loaded mode if necessary after adding the widget
2277 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2278 null);
2279 }
2280 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002281 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002282 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002283 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002284 }
2285 }
Romain Guycbb89e42009-06-08 15:52:54 -07002286
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002287 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002288 // Close any folders that may be open.
2289 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002290 mWorkspace.moveToCustomContentScreen(animate);
2291 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002292
2293 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2294 int[] cell, int spanX, int spanY) {
2295 switch (info.itemType) {
2296 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2297 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2298 int span[] = new int[2];
2299 span[0] = spanX;
2300 span[1] = spanY;
2301 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2302 container, screenId, cell, span);
2303 break;
2304 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2305 processShortcutFromDrop(info.componentName, container, screenId, cell);
2306 break;
2307 default:
2308 throw new IllegalStateException("Unknown item type: " + info.itemType);
2309 }
2310 }
2311
Adam Cohenfbba09b2011-07-18 21:43:05 -07002312 /**
2313 * Process a shortcut drop.
2314 *
2315 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002316 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002317 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002318 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002319 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2320 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002321 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002322 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002323 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002324 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002325
2326 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002327 mPendingAddInfo.cellX = cell[0];
2328 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002330
2331 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2332 createShortcutIntent.setComponent(componentName);
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002333 Utilities.startActivityForResultSafely(this, createShortcutIntent, REQUEST_CREATE_SHORTCUT);
Michael Jurka0280c3b2010-09-17 15:00:07 -07002334 }
2335
Adam Cohenfbba09b2011-07-18 21:43:05 -07002336 /**
2337 * Process a widget drop.
2338 *
2339 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002340 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002341 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002342 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002343 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2344 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002345 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002346 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002347 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002348 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002349 mPendingAddInfo.minSpanX = info.minSpanX;
2350 mPendingAddInfo.minSpanY = info.minSpanY;
2351
Adam Cohenfbba09b2011-07-18 21:43:05 -07002352 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002353 mPendingAddInfo.cellX = cell[0];
2354 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002355 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002356 if (span != null) {
2357 mPendingAddInfo.spanX = span[0];
2358 mPendingAddInfo.spanY = span[1];
2359 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002360
Adam Cohened66b2b2012-01-23 17:28:51 -08002361 AppWidgetHostView hostView = info.boundWidget;
2362 int appWidgetId;
2363 if (hostView != null) {
2364 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002365 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Sunny Goyal5b9ebca2015-06-01 18:37:32 -07002366
2367 // Clear the boundWidget so that it doesn't get destroyed.
2368 info.boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08002369 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002370 // In this case, we either need to start an activity to get permission to bind
2371 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002372 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002373 Bundle options = info.bindOptions;
2374
Sunny Goyalffe83f12014-08-14 17:39:34 -07002375 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2376 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002377 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002378 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002379 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002380 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002381 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2382 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2383 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002384 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2385 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002386 // TODO: we need to make sure that this accounts for the options bundle.
2387 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002388 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2389 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002390 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002391 }
2392
Adam Cohendcd297f2013-06-18 13:13:40 -07002393 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002394 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002395 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002396 folderInfo.title = getText(R.string.folder_name);
2397
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002398 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002399 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2400 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002401 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002402
2403 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002404 FolderIcon newFolder =
2405 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002406 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002407 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002408 // Force measure the new folder icon
2409 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2410 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002411 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002412 }
Romain Guycbb89e42009-06-08 15:52:54 -07002413
Joe Onorato9c1289c2009-08-17 11:03:03 -04002414 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002415 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002416 }
2417
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002418 /**
2419 * Registers various content observers. The current implementation registers
2420 * only a favorites observer to keep track of the favorites applications.
2421 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002422 private void registerContentObservers() {
2423 ContentResolver resolver = getContentResolver();
2424 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2425 true, mWidgetObserver);
2426 }
2427
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002428 @Override
2429 public boolean dispatchKeyEvent(KeyEvent event) {
2430 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2431 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002432 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002433 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002434 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002435 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002436 dumpState();
2437 return true;
2438 }
2439 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002440 }
2441 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2442 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002443 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 return true;
2445 }
2446 }
2447
2448 return super.dispatchKeyEvent(event);
2449 }
2450
Joe Onorato88ec0992009-11-19 13:16:06 -08002451 @Override
2452 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002453 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2454 return;
2455 }
2456
Winson Chung3d9490a2015-03-24 17:38:42 -07002457 LauncherAccessibilityDelegate delegate =
2458 LauncherAppState.getInstance().getAccessibilityDelegate();
2459 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002460 return;
2461 }
2462
Winson Chungb745afb2015-03-02 11:51:23 -08002463 if (isAppsViewVisible()) {
2464 showWorkspace(true);
2465 } else if (isWidgetsViewVisible()) {
2466 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002467 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002468 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002469 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002470 Folder openFolder = mWorkspace.getOpenFolder();
2471 if (openFolder.isEditingName()) {
2472 openFolder.dismissEditingName();
2473 } else {
2474 closeFolder();
2475 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002476 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002477 mWorkspace.exitWidgetResizeMode();
2478
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002479 // Back button is a no-op here, but give at least some feedback for the button press
2480 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002481 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002482 }
2483
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002484 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002485 * Re-listen when widgets are reset.
2486 */
Adam Cohen091440a2015-03-18 14:16:05 -07002487 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002488 if (mAppWidgetHost != null) {
2489 mAppWidgetHost.startListening();
2490 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002491 }
2492
2493 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494 * Launches the intent referred by the clicked shortcut.
2495 *
2496 * @param v The view representing the clicked shortcut.
2497 */
2498 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002499 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2500 // view has detached (it's possible for this to happen if the view is removed mid touch).
2501 if (v.getWindowToken() == null) {
2502 return;
2503 }
2504
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002505 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002506 return;
2507 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002508
Adam Cohen1697b792013-09-17 19:08:21 -07002509 if (v instanceof Workspace) {
2510 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002511 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002512 }
2513 return;
2514 }
2515
2516 if (v instanceof CellLayout) {
2517 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002518 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002519 }
2520 }
2521
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002522 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002523 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002524 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002525 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002526 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002527 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002528 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002529 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002530 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002531 } else if (tag instanceof AppInfo) {
2532 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002533 } else if (tag instanceof LauncherAppWidgetInfo) {
2534 if (v instanceof PendingAppWidgetHostView) {
2535 onClickPendingWidget((PendingAppWidgetHostView) v);
2536 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002537 }
2538 }
2539
Sunny Goyal70660032015-05-14 00:07:08 -07002540 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002541 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002542 return false;
2543 }
2544
Michael Jurkaaf442092010-06-10 17:01:57 -07002545 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002546 * Event handler for the app widget view which has not fully restored.
2547 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002548 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002549 if (mIsSafeModeEnabled) {
2550 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2551 return;
2552 }
2553
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002554 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002555 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002556 int widgetId = info.appWidgetId;
2557 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2558 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002559 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2560 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002561 mPendingAddInfo.copyFrom(info);
2562 mPendingAddWidgetId = widgetId;
2563
Sunny Goyalffe83f12014-08-14 17:39:34 -07002564 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2565 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002566 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002567 } else if (info.installProgress < 0) {
2568 // The install has not been queued
2569 final String packageName = info.providerName.getPackageName();
2570 showBrokenAppInstallDialog(packageName,
2571 new DialogInterface.OnClickListener() {
2572 public void onClick(DialogInterface dialog, int id) {
2573 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2574 }
2575 });
2576 } else {
2577 // Download has started.
2578 final String packageName = info.providerName.getPackageName();
2579 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002580 }
2581 }
2582
2583 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002584 * Event handler for the "grid" button that appears on the home screen, which
2585 * enters all apps mode.
2586 *
2587 * @param v The view that was clicked.
2588 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002589 protected void onClickAllAppsButton(View v) {
2590 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002591 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002592 showWorkspace(true);
2593 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002594 // Try and refresh the set of predicted apps before we enter launcher
2595 showAppsView(true /* animated */, false /* resetListToTop */,
2596 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002597 }
2598 }
2599
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002600 private void showBrokenAppInstallDialog(final String packageName,
2601 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002602 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002603 .setTitle(R.string.abandoned_promises_title)
2604 .setMessage(R.string.abandoned_promise_explanation)
2605 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2606 .setNeutralButton(R.string.abandoned_clean_this,
2607 new DialogInterface.OnClickListener() {
2608 public void onClick(DialogInterface dialog, int id) {
2609 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2610 mWorkspace.removeAbandonedPromise(packageName, user);
2611 }
2612 })
2613 .create().show();
2614 return;
2615 }
2616
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002617 /**
2618 * Event handler for an app shortcut click.
2619 *
2620 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2621 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002622 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002623 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2624 Object tag = v.getTag();
2625 if (!(tag instanceof ShortcutInfo)) {
2626 throw new IllegalArgumentException("Input must be a Shortcut");
2627 }
2628
2629 // Open shortcut
2630 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002631
2632 if (shortcut.isDisabled != 0) {
2633 int error = R.string.activity_not_available;
2634 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2635 error = R.string.safemode_shortcut_error;
2636 }
2637 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2638 return;
2639 }
2640
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002641 final Intent intent = shortcut.intent;
2642
2643 // Check for special shortcuts
2644 if (intent.getComponent() != null) {
2645 final String shortcutClass = intent.getComponent().getClassName();
2646
2647 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2648 MemoryDumpActivity.startDump(this);
2649 return;
2650 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2651 toggleShowWeightWatcher();
2652 return;
2653 }
2654 }
2655
Chris Wren40c5ed32014-06-24 18:24:23 -04002656 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002657 if ((v instanceof BubbleTextView)
2658 && shortcut.isPromise()
2659 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002660 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002661 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002662 new DialogInterface.OnClickListener() {
2663 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002664 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002665 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002666 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002667 return;
2668 }
2669
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002670 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002671 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002672
2673 if (mLauncherCallbacks != null) {
2674 mLauncherCallbacks.onClickAppShortcut(v);
2675 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002676 }
2677
Adam Cohen091440a2015-03-18 14:16:05 -07002678 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002679 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002680 final ShortcutInfo shortcut;
2681 final Intent intent;
2682 if (tag instanceof ShortcutInfo) {
2683 shortcut = (ShortcutInfo) tag;
2684 intent = shortcut.intent;
2685 int[] pos = new int[2];
2686 v.getLocationOnScreen(pos);
2687 intent.setSourceBounds(new Rect(pos[0], pos[1],
2688 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002689
Sunny Goyal508da152014-08-14 10:53:27 -07002690 } else if (tag instanceof AppInfo) {
2691 shortcut = null;
2692 intent = ((AppInfo) tag).intent;
2693 } else {
2694 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2695 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002696
2697 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002698 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002699
2700 if (success && v instanceof BubbleTextView) {
2701 mWaitingForResume = (BubbleTextView) v;
2702 mWaitingForResume.setStayPressed(true);
2703 }
2704 }
2705
2706 /**
2707 * Event handler for a folder icon click.
2708 *
2709 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2710 */
2711 protected void onClickFolderIcon(View v) {
2712 if (LOGD) Log.d(TAG, "onClickFolder");
2713 if (!(v instanceof FolderIcon)){
2714 throw new IllegalArgumentException("Input must be a FolderIcon");
2715 }
2716
2717 FolderIcon folderIcon = (FolderIcon) v;
2718 final FolderInfo info = folderIcon.getFolderInfo();
2719 Folder openFolder = mWorkspace.getFolderForTag(info);
2720
2721 // If the folder info reports that the associated folder is open, then verify that
2722 // it is actually opened. There have been a few instances where this gets out of sync.
2723 if (info.opened && openFolder == null) {
2724 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2725 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2726 info.opened = false;
2727 }
2728
2729 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2730 // Close any open folder
2731 closeFolder();
2732 // Open the requested folder
2733 openFolder(folderIcon);
2734 } else {
2735 // Find the open folder...
2736 int folderScreen;
2737 if (openFolder != null) {
2738 folderScreen = mWorkspace.getPageForView(openFolder);
2739 // .. and close it
2740 closeFolder(openFolder);
2741 if (folderScreen != mWorkspace.getCurrentPage()) {
2742 // Close any folder open on the current screen
2743 closeFolder();
2744 // Pull the folder onto this screen
2745 openFolder(folderIcon);
2746 }
2747 }
2748 }
Adam Cohen9211d422014-10-07 18:14:53 -07002749
2750 if (mLauncherCallbacks != null) {
2751 mLauncherCallbacks.onClickFolderIcon(v);
2752 }
Michael Jurka5130e402011-10-13 04:55:35 -07002753 }
2754
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002755 /**
2756 * Event handler for the (Add) Widgets button that appears after a long press
2757 * on the home screen.
2758 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002759 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002760 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002761 if (mIsSafeModeEnabled) {
2762 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2763 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002764 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002765 if (mLauncherCallbacks != null) {
2766 mLauncherCallbacks.onClickAddWidgetButton(view);
2767 }
Adam Cohen9211d422014-10-07 18:14:53 -07002768 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002769 }
2770
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002771 /**
2772 * Event handler for the wallpaper picker button that appears after a long press
2773 * on the home screen.
2774 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002775 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002776 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002777 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2778 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002779
2780 if (mLauncherCallbacks != null) {
2781 mLauncherCallbacks.onClickWallpaperPicker(v);
2782 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002783 }
2784
2785 /**
2786 * Event handler for a click on the settings button that appears after a long press
2787 * on the home screen.
2788 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002789 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002790 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002791 if (mLauncherCallbacks != null) {
2792 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002793 } else {
Sunny Goyal4f3e9382015-06-05 00:13:25 -07002794 startActivity(new Intent(this, SettingsActivity.class));
Adam Cohen9211d422014-10-07 18:14:53 -07002795 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002796 }
2797
Adam Cohen61f560d2013-09-30 15:58:20 -07002798 public View.OnTouchListener getHapticFeedbackTouchListener() {
2799 if (mHapticFeedbackTouchListener == null) {
2800 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002801 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002802 @Override
2803 public boolean onTouch(View v, MotionEvent event) {
2804 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2805 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2806 }
2807 return false;
2808 }
2809 };
2810 }
2811 return mHapticFeedbackTouchListener;
2812 }
2813
Adam Cohen9211d422014-10-07 18:14:53 -07002814 public void onDragStarted(View view) {
2815 if (isOnCustomContent()) {
2816 // Custom content screen doesn't participate in drag and drop. If on custom
2817 // content screen, move to default.
2818 moveWorkspaceToDefaultScreen();
2819 }
2820
2821 if (mLauncherCallbacks != null) {
2822 mLauncherCallbacks.onDragStarted(view);
2823 }
2824 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002825
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002826 /**
2827 * Called when the user stops interacting with the launcher.
2828 * This implies that the user is now on the homescreen and is not doing housekeeping.
2829 */
Adam Cohen9211d422014-10-07 18:14:53 -07002830 protected void onInteractionEnd() {
2831 if (mLauncherCallbacks != null) {
2832 mLauncherCallbacks.onInteractionEnd();
2833 }
2834 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002835
2836 /**
2837 * Called when the user starts interacting with the launcher.
2838 * The possible interactions are:
2839 * - open all apps
2840 * - reorder an app shortcut, or a widget
2841 * - open the overview mode.
2842 * This is a good time to stop doing things that only make sense
2843 * when the user is on the homescreen and not doing housekeeping.
2844 */
Adam Cohen9211d422014-10-07 18:14:53 -07002845 protected void onInteractionBegin() {
2846 if (mLauncherCallbacks != null) {
2847 mLauncherCallbacks.onInteractionBegin();
2848 }
2849 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002850
Winson Chungcd99cd32015-04-29 11:03:24 -07002851 /** Updates the interaction state. */
2852 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002853 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002854 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002855 boolean fromStateWithOverlay;
2856 boolean toStateWithOverlay;
2857 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2858 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2859 toStateWithOverlay = toState != Workspace.State.NORMAL;
2860 } else {
2861 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2862 fromState != Workspace.State.NORMAL_HIDDEN;
2863 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2864 toState != Workspace.State.NORMAL_HIDDEN;
2865 }
2866 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002867 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002868 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002869 onInteractionEnd();
2870 }
2871 }
2872
Kenny Guyf07af7b2014-07-31 11:39:16 +01002873 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002874 try {
2875 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002876 launcherApps.showAppDetailsForProfile(componentName, user);
2877 } catch (SecurityException e) {
2878 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2879 Log.e(TAG, "Launcher does not have permission to launch settings");
2880 } catch (ActivityNotFoundException e) {
2881 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2882 Log.e(TAG, "Unable to launch settings");
2883 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002884 }
2885
Michael Jurka1e2f4652013-07-08 18:03:46 -07002886 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002887 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2888 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002889 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002890 // System applications cannot be installed. For now, show a toast explaining that.
2891 // We may give them the option of disabling apps this way.
2892 int messageId = R.string.uninstall_system_app_text;
2893 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002894 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002895 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002896 String packageName = componentName.getPackageName();
2897 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002898 Intent intent = new Intent(
2899 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002900 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2901 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002902 if (user != null) {
2903 user.addToIntent(intent, Intent.EXTRA_USER);
2904 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002905 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002906 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002907 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002908 }
2909
Winson Chung8f1eff72015-05-28 17:33:40 -07002910 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002911 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002912 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002913 // Only launch using the new animation if the shortcut has not opted out (this is a
2914 // private contract between launcher and may be ignored in the future).
2915 boolean useLaunchAnimation = (v != null) &&
2916 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002917 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2918 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002919
Kenny Guy1317e2d2014-05-08 18:52:50 +01002920 UserHandleCompat user = null;
2921 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2922 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2923 user = userManager.getUserForSerialNumber(serialNumber);
2924 }
2925
2926 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002927 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002928 ActivityOptions opts = null;
2929 if (sClipRevealMethod != null) {
2930 // TODO: call method directly when Launcher3 can depend on M APIs
2931 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 }
2944 try {
2945 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2946 left, top, width, height);
2947 } catch (IllegalAccessException e) {
2948 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2949 sClipRevealMethod = null;
2950 } catch (InvocationTargetException e) {
2951 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2952 sClipRevealMethod = null;
2953 }
2954 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002955 if (opts == null && !Utilities.isLmpOrAbove()) {
2956 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002957 v.getMeasuredWidth(), v.getMeasuredHeight());
2958 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002959 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002960 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002961
2962 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2963 // Could be launching some bookkeeping activity
2964 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002965 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002966 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002967 launcherApps.startActivityForProfile(intent.getComponent(), user,
2968 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002969 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002970 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 } catch (SecurityException e) {
2972 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002973 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002975 "or use the exported attribute for this activity. "
2976 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002977 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002978 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002979 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002980
Winson Chung8f1eff72015-05-28 17:33:40 -07002981 private boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07002982 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002983 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2984 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2985 return false;
2986 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002987 try {
2988 success = startActivity(v, intent, tag);
2989 } catch (ActivityNotFoundException e) {
2990 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2991 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2992 }
2993 return success;
2994 }
2995
Adam Cohen268c4752012-06-06 17:47:33 -07002996 /**
2997 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2998 * in the DragLayer in the exact absolute location of the original FolderIcon.
2999 */
3000 private void copyFolderIconToImage(FolderIcon fi) {
3001 final int width = fi.getMeasuredWidth();
3002 final int height = fi.getMeasuredHeight();
3003
3004 // Lazy load ImageView, Bitmap and Canvas
3005 if (mFolderIconImageView == null) {
3006 mFolderIconImageView = new ImageView(this);
3007 }
3008 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3009 mFolderIconBitmap.getHeight() != height) {
3010 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3011 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3012 }
3013
3014 DragLayer.LayoutParams lp;
3015 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3016 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3017 } else {
3018 lp = new DragLayer.LayoutParams(width, height);
3019 }
3020
Adam Cohen307fe232012-08-16 17:55:58 -07003021 // The layout from which the folder is being opened may be scaled, adjust the starting
3022 // view size by this scale factor.
3023 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003024 lp.customPosition = true;
3025 lp.x = mRectForFolderAnimation.left;
3026 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003027 lp.width = (int) (scale * width);
3028 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003029
3030 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3031 fi.draw(mFolderIconCanvas);
3032 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003033 if (fi.getFolder() != null) {
3034 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3035 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003036 }
Adam Cohen268c4752012-06-06 17:47:33 -07003037 // Just in case this image view is still in the drag layer from a previous animation,
3038 // we remove it and re-add it.
3039 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3040 mDragLayer.removeView(mFolderIconImageView);
3041 }
3042 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003043 if (fi.getFolder() != null) {
3044 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003045 }
Adam Cohen268c4752012-06-06 17:47:33 -07003046 }
3047
Adam Cohen2801caf2011-05-13 20:57:39 -07003048 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003049 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003050 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3051 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3052 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3053
Adam Cohenc51934b2011-07-26 21:07:43 -07003054 FolderInfo info = (FolderInfo) fi.getTag();
3055 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3056 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003057 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3058 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003059 }
3060
Adam Cohen268c4752012-06-06 17:47:33 -07003061 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3062 copyFolderIconToImage(fi);
3063 fi.setVisibility(View.INVISIBLE);
3064
Michael Jurka2ecf9952012-06-18 12:52:28 -07003065 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003066 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003067 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003068 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3069 }
3070 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003071 oa.start();
3072 }
3073
Adam Cohen268c4752012-06-06 17:47:33 -07003074 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003075 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003076 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3077 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3078 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3079
Adam Cohen268c4752012-06-06 17:47:33 -07003080 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003081
Adam Cohen268c4752012-06-06 17:47:33 -07003082 // We remove and re-draw the FolderIcon in-case it has changed
3083 mDragLayer.removeView(mFolderIconImageView);
3084 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003085 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003086 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003087 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003088 oa.addListener(new AnimatorListenerAdapter() {
3089 @Override
3090 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003091 if (cl != null) {
3092 cl.clearFolderLeaveBehind();
3093 // Remove the ImageView copy of the FolderIcon and make the original visible.
3094 mDragLayer.removeView(mFolderIconImageView);
3095 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003096 }
3097 }
3098 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003099 oa.start();
3100 }
3101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003102 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003103 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003104 * is animated relative to the specified View. If the View is null, no animation
3105 * is played.
3106 *
3107 * @param folderInfo The FolderInfo describing the folder to open.
3108 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003109 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003110 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003111 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3112 if (openFolder != null && openFolder != folder) {
3113 // Close any open folder before opening a folder.
3114 closeFolder();
3115 }
3116
Adam Cohena9cf38f2011-05-02 15:36:58 -07003117 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003118
Adam Cohena9cf38f2011-05-02 15:36:58 -07003119 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120
Sunny Goyalf4066152015-04-15 09:42:19 -07003121 // While the folder is open, the position of the icon cannot change.
3122 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3123
Adam Cohen4554ee12011-08-03 16:13:21 -07003124 // Just verify that the folder hasn't already been added to the DragLayer.
3125 // There was a one-off crash where the folder had a parent already.
3126 if (folder.getParent() == null) {
3127 mDragLayer.addView(folder);
3128 mDragController.addDropTarget((DropTarget) folder);
3129 } else {
3130 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3131 folder.getParent() + ").");
3132 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003133 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003134 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003135
3136 // Notify the accessibility manager that this folder "window" has appeared and occluded
3137 // the workspace items
3138 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3139 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003140 }
3141
3142 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003143 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003144 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003145 if (folder.isEditingName()) {
3146 folder.dismissEditingName();
3147 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003148 closeFolder(folder);
3149 }
3150 }
3151
Sunny Goyal83a8f042015-05-19 12:52:12 -07003152 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003153 folder.getInfo().opened = false;
3154
3155 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3156 if (parent != null) {
3157 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3158 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003159 if (fi != null) {
3160 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3161 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003162 }
3163 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003164
3165 // Notify the accessibility manager that this folder "window" has disappeard and no
3166 // longer occludeds the workspace items
3167 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003168 }
3169
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003170 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003171 if (!isDraggingEnabled()) return false;
3172 if (isWorkspaceLocked()) return false;
3173 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174
Adam Cohen1697b792013-09-17 19:08:21 -07003175 if (v instanceof Workspace) {
3176 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003177 if (!mWorkspace.isTouchActive()) {
3178 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003179 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3180 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3181 return true;
3182 } else {
3183 return false;
3184 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003185 } else {
3186 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003187 }
Adam Cohen1697b792013-09-17 19:08:21 -07003188 }
3189
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003190 CellLayout.CellInfo longClickCellInfo = null;
3191 View itemUnderLongClick = null;
3192 if (v.getTag() instanceof ItemInfo) {
3193 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003194 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003195 itemUnderLongClick = longClickCellInfo.cell;
3196 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003197 }
3198
Winson Chung3d503fb2011-07-13 17:25:49 -07003199 // The hotseat touch handling does not go through Workspace, and we always allow long press
3200 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003201 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003202 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003203 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003204 // User long pressed on empty space
3205 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3206 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003207 if (mWorkspace.isInOverviewMode()) {
3208 mWorkspace.startReordering(v);
3209 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003210 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003211 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003212 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003213 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3214 mHotseat.getOrderInHotseat(
3215 longClickCellInfo.cellX,
3216 longClickCellInfo.cellY));
3217 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003218 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003219 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003220 }
3221 }
3222 }
3223 return true;
3224 }
3225
Winson Chung3d503fb2011-07-13 17:25:49 -07003226 boolean isHotseatLayout(View layout) {
3227 return mHotseat != null && layout != null &&
3228 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3229 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003230
Winson Chung3d503fb2011-07-13 17:25:49 -07003231 /**
3232 * Returns the CellLayout of the specified container at the specified screen.
3233 */
Sunny Goyal92820592015-03-02 11:31:35 -08003234 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003235 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3236 if (mHotseat != null) {
3237 return mHotseat.getLayout();
3238 } else {
3239 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003240 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003241 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003242 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003243 }
3244 }
3245
Winson Chungb745afb2015-03-02 11:51:23 -08003246 /**
3247 * For overridden classes.
3248 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003249 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003250 return isAppsViewVisible();
3251 }
3252
3253 public boolean isAppsViewVisible() {
3254 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3255 }
3256
3257 public boolean isWidgetsViewVisible() {
3258 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003259 }
3260
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003261 private void setWorkspaceBackground(int background) {
3262 switch (background) {
3263 case WORKSPACE_BACKGROUND_TRANSPARENT:
3264 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3265 break;
3266 case WORKSPACE_BACKGROUND_BLACK:
3267 getWindow().setBackgroundDrawable(null);
3268 break;
3269 default:
3270 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3271 }
Craig Mautner360310b2012-10-26 15:13:08 -07003272 }
3273
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003274 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003275 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3276 int curflags = getWindow().getAttributes().flags
3277 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3278 if (wpflags != curflags) {
3279 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3280 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003281 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003282 }
3283
Michael Jurkae326f182011-11-21 14:05:46 -08003284 @Override
3285 public void onTrimMemory(int level) {
3286 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003287 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3288 // The widget preview db can result in holding onto over
3289 // 3MB of memory for caching which isn't necessary.
3290 SQLiteDatabase.releaseMemory();
3291
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003292 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003293 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003294 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003295 if (mLauncherCallbacks != null) {
3296 mLauncherCallbacks.onTrimMemory(level);
3297 }
Michael Jurkae326f182011-11-21 14:05:46 -08003298 }
3299
Winson Chungb745afb2015-03-02 11:51:23 -08003300 @Override
3301 public void onStateTransitionHideSearchBar() {
3302 // Hide the search bar
3303 if (mSearchDropTargetBar != null) {
3304 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3305 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003306 }
3307
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003308 public void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003309 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3310 true);
Adam Cohened307df2013-10-02 09:37:31 -07003311 }
3312
Sunny Goyal83a8f042015-05-19 12:52:12 -07003313 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003314 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3315 onCompleteRunnable, true);
3316 }
3317
3318 protected void showWorkspace(int snapToPage, boolean animated) {
3319 showWorkspace(snapToPage, animated, null, true);
3320 }
3321
3322 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3323 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003324 boolean changed = mState != State.WORKSPACE ||
3325 mWorkspace.getState() != Workspace.State.NORMAL;
3326 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003327 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003328 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003329 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003330 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003331
Winson Chungc7d2b602012-05-16 17:02:20 -07003332 // Show the search bar (only animate if we were showing the drop target bar in spring
3333 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003334 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003335 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003336 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003337
Michael Jurkab3e22d92011-10-31 15:58:33 -07003338 // Set focus to the AppsCustomize button
3339 if (mAllAppsButton != null) {
3340 mAllAppsButton.requestFocus();
3341 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003342 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003343
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003344 // Change the state *after* we've called all the transition code
3345 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003346
Winson Chung5fb63472011-02-02 17:03:37 -08003347 // Resume the auto-advance of widgets
3348 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003349 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003350
Winson Chung0f785722015-04-08 10:27:49 -07003351 if (changed) {
3352 // Send an accessibility event to announce the context change
3353 getWindow().getDecorView()
3354 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003355 if (notifyLauncherCallbacks) {
3356 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003357 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003358 mLauncherCallbacks.onAllAppsHidden();
3359 }
3360 }
Winson Chung0f785722015-04-08 10:27:49 -07003361 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003362 }
3363
Adam Cohened307df2013-10-02 09:37:31 -07003364 void showOverviewMode(boolean animated) {
3365 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003366 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003367 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3368 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003369 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003370 }
3371
Winson Chungb745afb2015-03-02 11:51:23 -08003372 /**
3373 * Shows the apps view.
3374 */
Winson Chung4ac30062015-05-08 17:34:17 -07003375 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003376 if (resetListToTop) {
3377 mAppsView.scrollToTop();
3378 }
Winson Chung4ac30062015-05-08 17:34:17 -07003379 if (updatePredictedApps) {
3380 tryAndUpdatePredictedApps();
3381 }
Winson Chungb745afb2015-03-02 11:51:23 -08003382 showAppsOrWidgets(animated, State.APPS);
3383 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003384
Winson Chungb745afb2015-03-02 11:51:23 -08003385 /**
3386 * Shows the widgets view.
3387 */
3388 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003389 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003390 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003391 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003392 }
Winson Chungb745afb2015-03-02 11:51:23 -08003393 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003394
3395 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003396 @Override
3397 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003398 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003399 }
3400 });
Winson Chungb745afb2015-03-02 11:51:23 -08003401 }
3402
3403 /**
3404 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003405 *
3406 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003407 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003408 // TODO: calling method should use the return value so that when {@code false} is returned
3409 // the workspace transition doesn't fall into invalid state.
3410 private boolean showAppsOrWidgets(boolean animated, State toState) {
3411 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3412 mState != State.WIDGETS_SPRING_LOADED) {
3413 return false;
3414 }
3415 if (toState != State.APPS && toState != State.WIDGETS) {
3416 return false;
3417 }
Winson Chungb745afb2015-03-02 11:51:23 -08003418
3419 if (toState == State.APPS) {
3420 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003421 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003422 mLauncherCallbacks.onAllAppsShown();
3423 }
Winson Chungb745afb2015-03-02 11:51:23 -08003424 } else {
3425 mStateTransitionAnimation.startAnimationToWidgets(animated);
3426 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003427
Michael Jurkab3e22d92011-10-31 15:58:33 -07003428 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003429 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003430
3431 // Pause the auto-advance of widgets until we are out of AllApps
3432 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003433 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003434 closeFolder();
3435
3436 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003437 getWindow().getDecorView()
3438 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003439 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003440 }
3441
Winson Chungcd99cd32015-04-29 11:03:24 -07003442 /**
3443 * Updates the workspace and interaction state on state change, and return the animation to this
3444 * new state.
3445 */
3446 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3447 boolean animated, HashMap<View, Integer> layerViews) {
3448 Workspace.State fromState = mWorkspace.getState();
3449 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3450 updateInteraction(fromState, toState);
3451 return anim;
3452 }
3453
Hyunyoung Song3f471442015-04-08 19:01:34 -07003454 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003455 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003456 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3457 mState == State.WIDGETS_SPRING_LOADED) {
3458 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003459 }
Winson Chungb745afb2015-03-02 11:51:23 -08003460
3461 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003462 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3463 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003464 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003465 }
Adam Cohen7777d962011-08-18 18:58:38 -07003466
Hyunyoung Song3f471442015-04-08 19:01:34 -07003467 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003468 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003469 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003470
Winson Chungcd99cd32015-04-29 11:03:24 -07003471 if (successfulDrop) {
3472 // We need to trigger all apps hidden to notify search to update itself before the
3473 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003474 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003475 mLauncherCallbacks.onAllAppsHidden();
3476 }
3477 }
3478
Winson Chunge7a03942011-08-05 15:05:12 -07003479 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003480 @Override
3481 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003482 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003483 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3484 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003485 // Before we show workspace, hide all apps again because
3486 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3487 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003488 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003489 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003490 } else {
3491 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003492 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003493 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003494 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003495 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003496
Michael Jurkad3ef3062010-11-23 16:23:58 -08003497 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003498 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003499 showAppsView(true /* animated */, false /* resetListToTop */,
3500 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003501 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003502 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003503 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003504 }
3505
Winson Chung4ac30062015-05-08 17:34:17 -07003506 /**
3507 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3508 * resumed.
3509 */
3510 private void tryAndUpdatePredictedApps() {
3511 if (mLauncherCallbacks != null) {
3512 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3513 if (!apps.isEmpty()) {
3514 mAppsView.setPredictedApps(apps);
3515 }
3516 }
3517 }
3518
Michael Jurkab3e22d92011-10-31 15:58:33 -07003519 void lockAllApps() {
3520 // TODO
3521 }
3522
3523 void unlockAllApps() {
3524 // TODO
3525 }
3526
Sunny Goyal594d76d2014-11-06 10:12:54 -08003527 protected void disableVoiceButtonProxy(boolean disable) {
3528 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003529 }
3530
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003531 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003532 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3533 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003534 }
3535
Adam Cohen24ce0b32014-01-14 16:18:14 -08003536 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003537 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3538 if (searchProvider == null) {
3539 return null;
3540 }
3541
3542 Bundle opts = new Bundle();
3543 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003544 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003545
3546 SharedPreferences sp = getSharedPreferences(
3547 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3548 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003549 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003550 if (!searchProvider.provider.flattenToString().equals(
3551 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003552 || (widgetInfo == null)
3553 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003554 // A valid widget is not already bound.
3555 if (widgetId > -1) {
3556 mAppWidgetHost.deleteAppWidgetId(widgetId);
3557 widgetId = -1;
3558 }
3559
3560 // Try to bind a new widget
3561 widgetId = mAppWidgetHost.allocateAppWidgetId();
3562
3563 if (!AppWidgetManagerCompat.getInstance(this)
3564 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3565 mAppWidgetHost.deleteAppWidgetId(widgetId);
3566 widgetId = -1;
3567 }
3568
3569 sp.edit()
3570 .putInt(QSB_WIDGET_ID, widgetId)
3571 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3572 .commit();
3573 }
3574
3575 if (widgetId != -1) {
3576 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3577 mQsb.updateAppWidgetOptions(opts);
3578 mQsb.setPadding(0, 0, 0, 0);
3579 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003580 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003581 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003582 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003583 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003584 }
3585
Sunny Goyal22235bc2015-04-03 09:23:43 -07003586 private void reinflateQSBIfNecessary() {
3587 if (mQsb instanceof LauncherAppWidgetHostView &&
3588 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3589 mSearchDropTargetBar.removeView(mQsb);
3590 mQsb = null;
3591 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3592 }
3593 }
3594
Michael Jurkad7c28052012-04-27 15:43:36 -07003595 @Override
3596 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003597 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003598 final List<CharSequence> text = event.getText();
3599 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003600 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003601 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003602 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003603 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003604 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003605 } else {
3606 text.add(getString(R.string.all_apps_home_button_label));
3607 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003608 return result;
3609 }
3610
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003611 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003612 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003613 */
Adam Cohen091440a2015-03-18 14:16:05 -07003614 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003615 @Override
3616 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003617 closeSystemDialogs();
3618 }
3619 }
3620
3621 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003622 * Receives notifications whenever the appwidgets are reset.
3623 */
3624 private class AppWidgetResetObserver extends ContentObserver {
3625 public AppWidgetResetObserver() {
3626 super(new Handler());
3627 }
3628
3629 @Override
3630 public void onChange(boolean selfChange) {
3631 onAppWidgetReset();
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 Jurka05bf6442011-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
Joe Onorato9c1289c2009-08-17 11:03:03 -04003845 switch (item.itemType) {
3846 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3847 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003848 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003849 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003850
Winson Chung64359a52013-07-08 17:17:08 -07003851 /*
3852 * TODO: FIX collision case
3853 */
Winson Chungce376632013-07-11 16:12:41 -07003854 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3855 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3856 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003857 View v = cl.getChildAt(item.cellX, item.cellY);
3858 Object tag = v.getTag();
3859 String desc = "Collision while binding workspace item: " + item
3860 + ". Collides with " + tag;
3861 if (LauncherAppState.isDogfoodBuild()) {
3862 throw (new RuntimeException(desc));
3863 } else {
3864 Log.d(TAG, desc);
3865 }
Winson Chungce376632013-07-11 16:12:41 -07003866 }
Winson Chung64359a52013-07-08 17:17:08 -07003867 }
3868
Adam Cohendcd297f2013-06-18 13:13:40 -07003869 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3870 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003871 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003872 // Animate all the applications up now
3873 shortcut.setAlpha(0f);
3874 shortcut.setScaleX(0f);
3875 shortcut.setScaleY(0f);
3876 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003877 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003878 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003879 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003880 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003881 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003882 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003883 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003884 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3885 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003886 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003887 default:
3888 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003889 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003890 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003891
Winson Chung997a9232013-07-24 15:33:46 -07003892 if (animateIcons) {
3893 // Animate to the correct page
3894 if (newShortcutsScreenId > -1) {
3895 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003896 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003897 final Runnable startBounceAnimRunnable = new Runnable() {
3898 public void run() {
3899 anim.playTogether(bounceAnims);
3900 anim.start();
3901 }
3902 };
Winson Chung997a9232013-07-24 15:33:46 -07003903 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003904 // We post the animation slightly delayed to prevent slowdowns
3905 // when we are loading right after we return to launcher.
3906 mWorkspace.postDelayed(new Runnable() {
3907 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003908 if (mWorkspace != null) {
3909 mWorkspace.snapToPage(newScreenIndex);
3910 mWorkspace.postDelayed(startBounceAnimRunnable,
3911 NEW_APPS_ANIMATION_DELAY);
3912 }
Winson Chung94d67682013-09-25 16:29:40 -07003913 }
3914 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003915 } else {
3916 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003917 }
3918 }
Winson Chung64359a52013-07-08 17:17:08 -07003919 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003920 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003921 }
3922
Joe Onorato9c1289c2009-08-17 11:03:03 -04003923 /**
3924 * Implementation of the method from LauncherModel.Callbacks.
3925 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003926 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003927 Runnable r = new Runnable() {
3928 public void run() {
3929 bindFolders(folders);
3930 }
3931 };
3932 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003933 return;
3934 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003935 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003936 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003937
3938 /**
3939 * Add the views for a widget to the workspace.
3940 *
3941 * Implementation of the method from LauncherModel.Callbacks.
3942 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003943 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003944 Runnable r = new Runnable() {
3945 public void run() {
3946 bindAppWidget(item);
3947 }
3948 };
3949 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003950 return;
3951 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003952
Daniel Sandler843e8602010-06-07 14:59:01 -04003953 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3954 if (DEBUG_WIDGETS) {
3955 Log.d(TAG, "bindAppWidget: " + item);
3956 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003957 final Workspace workspace = mWorkspace;
3958
Adam Cohen59400422014-03-05 18:07:04 -08003959 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003960 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003961
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003962 if (!mIsSafeModeEnabled
3963 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3964 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
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 }
3974 // Note: This assumes that the id remap broadcast is received before this step.
3975 // If that is not the case, the id remap will be ignored and user may see the
3976 // click to setup view.
Adam Cohen2e6da152015-05-06 11:42:25 -07003977 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07003978 pendingInfo.spanX = item.spanX;
3979 pendingInfo.spanY = item.spanY;
3980 pendingInfo.minSpanX = item.minSpanX;
3981 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07003982 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07003983 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07003984
Sunny Goyalff572272014-07-23 13:58:07 -07003985 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07003986 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
3987 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07003988
3989 // TODO consider showing a permission dialog when the widget is clicked.
3990 if (!success) {
3991 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
3992 if (DEBUG_WIDGETS) {
3993 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3994 + " belongs to component " + item.providerName
3995 + ", as the launcher is unable to bing a new widget id");
3996 }
3997 LauncherModel.deleteItemFromDatabase(this, item);
3998 return;
3999 }
4000
4001 item.appWidgetId = newWidgetId;
4002
4003 // If the widget has a configure activity, it is still needs to set it up, otherwise
4004 // the widget is ready to go.
4005 item.restoreStatus = (appWidgetInfo.configure == null)
4006 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4007 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4008
4009 LauncherModel.updateItemInDatabase(this, item);
4010 }
4011
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004012 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004013 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004014 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004015 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4016 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004017 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004018
Sunny Goyal651077b2014-06-30 14:15:31 -07004019 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4020 } else {
4021 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004022 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4023 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004024 view.updateIcon(mIconCache);
4025 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004026 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004027 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004028 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004029
Joe Onorato9c1289c2009-08-17 11:03:03 -04004030 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004031 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004032
Adam Cohendcd297f2013-06-18 13:13:40 -07004033 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004034 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004035 if (!item.isCustomWidget()) {
4036 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4037 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004038
Joe Onorato9c1289c2009-08-17 11:03:03 -04004039 workspace.requestLayout();
4040
Daniel Sandler843e8602010-06-07 14:59:01 -04004041 if (DEBUG_WIDGETS) {
4042 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4043 + (SystemClock.uptimeMillis()-start) + "ms");
4044 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004045 }
4046
Sunny Goyalff572272014-07-23 13:58:07 -07004047 /**
4048 * Restores a pending widget.
4049 *
4050 * @param appWidgetId The app widget id
4051 * @param cellInfo The position on screen where to create the widget.
4052 */
4053 private void completeRestoreAppWidget(final int appWidgetId) {
4054 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4055 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4056 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4057 return;
4058 }
4059
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004060 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004061 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4062
4063 mWorkspace.reinflateWidgetsIfNecessary();
4064 LauncherModel.updateItemInDatabase(this, info);
4065 }
4066
Adam Cohen1462de32012-07-24 22:34:36 -07004067 public void onPageBoundSynchronously(int page) {
4068 mSynchronouslyBoundPages.add(page);
4069 }
4070
Joe Onorato9c1289c2009-08-17 11:03:03 -04004071 /**
4072 * Callback saying that there aren't any more items to bind.
4073 *
4074 * Implementation of the method from LauncherModel.Callbacks.
4075 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004076 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004077 Runnable r = new Runnable() {
4078 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004079 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004080 }
4081 };
4082 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004083 return;
4084 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004085 if (mSavedState != null) {
4086 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004087 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004088 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004089 mSavedState = null;
4090 }
4091
Adam Cohen1462de32012-07-24 22:34:36 -07004092 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004093
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004094 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004095 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004096
4097 // If we received the result of any pending adds while the loader was running (e.g. the
4098 // widget configuration forced an orientation change), process them now.
4099 if (sPendingAddItem != null) {
4100 final long screenId = completeAdd(sPendingAddItem);
4101
4102 // TODO: this moves the user to the page where the pending item was added. Ideally,
4103 // the screen would be guaranteed to exist after bind, and the page would be set through
4104 // the workspace restore process.
4105 mWorkspace.post(new Runnable() {
4106 @Override
4107 public void run() {
4108 mWorkspace.snapToScreenId(screenId);
4109 }
4110 });
4111 sPendingAddItem = null;
4112 }
4113
Sunny Goyal756adbc2015-04-16 15:20:51 -07004114 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004115
4116 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004117 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004118 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004119 }
4120
Adam Cohen3ed316a2014-07-23 18:21:20 -07004121 private void sendLoadingCompleteBroadcastIfNecessary() {
4122 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4123 String permission =
4124 getResources().getString(R.string.receive_first_load_broadcast_permission);
4125 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4126 sendBroadcast(intent, permission);
4127 SharedPreferences.Editor editor = mSharedPrefs.edit();
4128 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4129 editor.apply();
4130 }
4131 }
4132
Winson Chunga0b7e862013-09-05 16:03:15 -07004133 public boolean isAllAppsButtonRank(int rank) {
4134 if (mHotseat != null) {
4135 return mHotseat.isAllAppsButtonRank(rank);
4136 }
4137 return false;
4138 }
4139
Winson Chunga2413752012-04-03 14:22:34 -07004140 private boolean canRunNewAppsAnimation() {
4141 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4142 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4143 }
4144
Winson Chungc9168342013-06-26 14:54:55 -07004145 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4146 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4147 PropertyValuesHolder.ofFloat("alpha", 1f),
4148 PropertyValuesHolder.ofFloat("scaleX", 1f),
4149 PropertyValuesHolder.ofFloat("scaleY", 1f));
4150 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4151 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004152 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004153 return bounceAnim;
4154 }
4155
Adam Cohen27772732013-11-11 14:00:56 +00004156 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004157 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004158 }
4159
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004160 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004161 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004162 }
4163
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004164 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004165 if (mSearchDropTargetBar == null) {
4166 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004167 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004168 if (mQsb != null) {
4169 mSearchDropTargetBar.removeView(mQsb);
4170 mQsb = null;
4171 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004172 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004173 }
4174
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004175 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004176 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4177 * multiple calls to bind the same list.)
4178 */
4179 @Thunk ArrayList<AppInfo> mTmpAppsList;
4180 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4181 public void run() {
4182 bindAllApplications(mTmpAppsList);
4183 mTmpAppsList = null;
4184 }
4185 };
4186
4187 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004188 * Add the icons for all apps.
4189 *
4190 * Implementation of the method from LauncherModel.Callbacks.
4191 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004192 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004193 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4194 mTmpAppsList = apps;
4195 return;
4196 }
4197
Winson Chungb745afb2015-03-02 11:51:23 -08004198 if (mAppsView != null) {
4199 mAppsView.setApps(apps);
4200 }
Adam Cohen9211d422014-10-07 18:14:53 -07004201 if (mLauncherCallbacks != null) {
4202 mLauncherCallbacks.bindAllApplications(apps);
4203 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004204 }
4205
4206 /**
4207 * A package was updated.
4208 *
4209 * Implementation of the method from LauncherModel.Callbacks.
4210 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004211 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004212 Runnable r = new Runnable() {
4213 public void run() {
4214 bindAppsUpdated(apps);
4215 }
4216 };
4217 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004218 return;
4219 }
4220
Winson Chungb745afb2015-03-02 11:51:23 -08004221 if (mAppsView != null) {
4222 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004223 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004224 }
4225
Sunny Goyal4390ace2014-10-13 11:33:11 -07004226 @Override
4227 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4228 Runnable r = new Runnable() {
4229 public void run() {
4230 bindWidgetsRestored(widgets);
4231 }
4232 };
4233 if (waitUntilResume(r)) {
4234 return;
4235 }
4236 mWorkspace.widgetsRestored(widgets);
4237 }
4238
Joe Onorato9c1289c2009-08-17 11:03:03 -04004239 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004240 * Some shortcuts were updated in the background.
4241 *
4242 * Implementation of the method from LauncherModel.Callbacks.
4243 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004244 @Override
4245 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4246 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004247 Runnable r = new Runnable() {
4248 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004249 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004250 }
4251 };
4252 if (waitUntilResume(r)) {
4253 return;
4254 }
4255
Sunny Goyal4390ace2014-10-13 11:33:11 -07004256 if (!updated.isEmpty()) {
4257 mWorkspace.updateShortcuts(updated);
4258 }
4259
4260 if (!removed.isEmpty()) {
4261 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4262 for (ShortcutInfo si : removed) {
4263 removedComponents.add(si.getTargetComponent());
4264 }
4265 mWorkspace.removeItemsByComponentName(removedComponents, user);
4266 // Notify the drag controller
4267 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004268 }
4269 }
4270
4271 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004272 * Update the state of a package, typically related to install state.
4273 *
4274 * Implementation of the method from LauncherModel.Callbacks.
4275 */
Sunny Goyale755d462014-07-22 13:48:29 -07004276 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004277 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4278 Runnable r = new Runnable() {
4279 public void run() {
4280 bindRestoreItemsChange(updates);
4281 }
4282 };
4283 if (waitUntilResume(r)) {
4284 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004285 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004286
Sunny Goyal756adbc2015-04-16 15:20:51 -07004287 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004288 }
4289
4290 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004291 * A package was uninstalled. We take both the super set of packageNames
4292 * in addition to specific applications to remove, the reason being that
4293 * this can be called when a package is updated as well. In that scenario,
4294 * we only remove specific components from the workspace, where as
4295 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004296 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004297 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004298 * Implementation of the method from LauncherModel.Callbacks.
4299 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004300 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004301 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004302 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004303 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004304 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004305 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004306 }
Winson Chungd64d1762013-08-20 14:37:16 -07004307 };
4308 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004309 return;
4310 }
4311
Sunny Goyal1a745e82014-10-02 15:58:31 -07004312 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004313 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4314 for (AppInfo info : appInfos) {
4315 removedComponents.add(info.componentName);
4316 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004317 if (!packageNames.isEmpty()) {
4318 mWorkspace.removeItemsByPackageName(packageNames, user);
4319 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004320 if (!removedComponents.isEmpty()) {
4321 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004322 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004323 // Notify the drag controller
4324 mDragController.onAppsRemoved(packageNames, removedComponents);
4325
Sunny Goyal1a745e82014-10-02 15:58:31 -07004326 } else {
4327 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004328 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004329
Winson Chungdf95eb12013-10-16 14:57:07 -07004330 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004331 if (mAppsView != null) {
4332 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004333 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004334 }
Joe Onoratobe386092009-11-17 17:32:16 -08004335
Michael Jurkac402cd92013-05-20 15:49:32 +02004336 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004337 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004338 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004339 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004340 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004341
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004342 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004343 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004344 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004345 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004346 return;
4347 }
4348
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004349 if (mWidgetsView != null && model != null) {
4350 mWidgetsView.addWidgets(model);
4351 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004352 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004353 }
4354
Winson Chung400438b2011-01-16 17:53:48 -08004355 private int mapConfigurationOriActivityInfoOri(int configOri) {
4356 final Display d = getWindowManager().getDefaultDisplay();
4357 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4358 switch (d.getRotation()) {
4359 case Surface.ROTATION_0:
4360 case Surface.ROTATION_180:
4361 // We are currently in the same basic orientation as the natural orientation
4362 naturalOri = configOri;
4363 break;
4364 case Surface.ROTATION_90:
4365 case Surface.ROTATION_270:
4366 // We are currently in the other basic orientation to the natural orientation
4367 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4368 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4369 break;
4370 }
4371
4372 int[] oriMap = {
4373 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4374 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4375 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4376 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4377 };
4378 // Since the map starts at portrait, we need to offset if this device's natural orientation
4379 // is landscape.
4380 int indexOffset = 0;
4381 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4382 indexOffset = 1;
4383 }
4384 return oriMap[(d.getRotation() + indexOffset) % 4];
4385 }
Adam Cohen446e9402011-09-15 18:21:21 -07004386
Winson Chung4b919f82012-05-01 10:44:08 -07004387 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004388 if (mRotationEnabled) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004389 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4390 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4391 .getConfiguration().orientation));
4392 } else {
4393 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4394 }
Winson Chung4b919f82012-05-01 10:44:08 -07004395 }
4396 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004397
Winson Chung4b919f82012-05-01 10:44:08 -07004398 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004399 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004400 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004401 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004402 } else {
4403 mHandler.postDelayed(new Runnable() {
4404 public void run() {
4405 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4406 }
4407 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004408 }
Winson Chung4b919f82012-05-01 10:44:08 -07004409 }
Winson Chung400438b2011-01-16 17:53:48 -08004410 }
4411
Winson Chunge43a1e72014-01-15 10:33:02 -08004412 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004413 if (mLauncherCallbacks != null) {
4414 return mLauncherCallbacks.isLauncherPreinstalled();
4415 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004416 PackageManager pm = getPackageManager();
4417 try {
4418 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4419 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4420 return true;
4421 } else {
4422 return false;
4423 }
4424 } catch (NameNotFoundException e) {
4425 e.printStackTrace();
4426 return false;
4427 }
4428 }
4429
Adam Cohenea90f832014-05-21 15:03:34 -07004430 /**
4431 * This method indicates whether or not we should suggest default wallpaper dimensions
4432 * when our wallpaper cropper was not yet used to set a wallpaper.
4433 */
4434 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004435 if (mLauncherCallbacks != null) {
4436 return mLauncherCallbacks.overrideWallpaperDimensions();
4437 }
Adam Cohenea90f832014-05-21 15:03:34 -07004438 return true;
4439 }
4440
Adam Cohen432609a2014-03-13 17:03:22 -07004441 /**
4442 * To be overridden by subclasses to indicate that there is an activity to launch
4443 * before showing the standard launcher experience.
4444 */
4445 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004446 if (mLauncherCallbacks != null) {
4447 return mLauncherCallbacks.hasFirstRunActivity();
4448 }
Adam Cohen432609a2014-03-13 17:03:22 -07004449 return false;
4450 }
4451
4452 /**
4453 * To be overridden by subclasses to launch any first run activity
4454 */
4455 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004456 if (mLauncherCallbacks != null) {
4457 return mLauncherCallbacks.getFirstRunActivity();
4458 }
Adam Cohen432609a2014-03-13 17:03:22 -07004459 return null;
4460 }
4461
Winson Chung2826a402015-04-17 16:18:53 -07004462 /**
4463 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004464 */
4465 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004466 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4467 return false;
4468 }
4469
Winson Chung2826a402015-04-17 16:18:53 -07004470 if (mLauncherCallbacks != null) {
4471 return mLauncherCallbacks.overrideAllAppsSearch();
4472 }
4473 return false;
4474 }
4475
Winson Chunga6945242014-01-08 14:04:34 -08004476 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004477 return !ActivityManager.isRunningInTestHarness() &&
4478 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004479 }
4480
Adam Cohen4a9423d2014-03-28 14:22:31 -07004481 protected boolean hasRunFirstRunActivity() {
4482 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4483 }
4484
Adam Cohen432609a2014-03-13 17:03:22 -07004485 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004486 if (shouldRunFirstRunActivity() &&
4487 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004488 Intent firstRunIntent = getFirstRunActivity();
4489 if (firstRunIntent != null) {
4490 startActivity(firstRunIntent);
4491 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004492 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004493 }
4494 }
Adam Cohen432609a2014-03-13 17:03:22 -07004495 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004496 }
4497
4498 private void markFirstRunActivityShown() {
4499 SharedPreferences.Editor editor = mSharedPrefs.edit();
4500 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4501 editor.apply();
4502 }
4503
Adam Cohen432609a2014-03-13 17:03:22 -07004504 /**
4505 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4506 * screen that must be displayed and dismissed.
4507 */
4508 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004509 if (mLauncherCallbacks != null) {
4510 return mLauncherCallbacks.hasDismissableIntroScreen();
4511 }
Adam Cohen432609a2014-03-13 17:03:22 -07004512 return false;
4513 }
4514
4515 /**
4516 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4517 */
4518 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004519 if (mLauncherCallbacks != null) {
4520 return mLauncherCallbacks.getIntroScreen();
4521 }
Adam Cohen432609a2014-03-13 17:03:22 -07004522 return null;
4523 }
4524
4525 /**
4526 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4527 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4528 */
4529 private boolean shouldShowIntroScreen() {
4530 return hasDismissableIntroScreen() &&
4531 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4532 }
4533
4534 protected void showIntroScreen() {
4535 View introScreen = getIntroScreen();
4536 changeWallpaperVisiblity(false);
4537 if (introScreen != null) {
4538 mDragLayer.showOverlayView(introScreen);
4539 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004540 if (mLauncherOverlayContainer != null) {
4541 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4542 }
Adam Cohen432609a2014-03-13 17:03:22 -07004543 }
4544
4545 public void dismissIntroScreen() {
4546 markIntroScreenDismissed();
4547 if (showFirstRunActivity()) {
4548 // We delay hiding the intro view until the first run activity is showing. This
4549 // avoids a blip.
4550 mWorkspace.postDelayed(new Runnable() {
4551 @Override
4552 public void run() {
4553 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004554 if (mLauncherOverlayContainer != null) {
4555 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4556 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004557 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004558 }
4559 }, ACTIVITY_START_DELAY);
4560 } else {
4561 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004562 if (mLauncherOverlayContainer != null) {
4563 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4564 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004565 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004566 }
4567 changeWallpaperVisiblity(true);
4568 }
4569
4570 private void markIntroScreenDismissed() {
4571 SharedPreferences.Editor editor = mSharedPrefs.edit();
4572 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4573 editor.apply();
4574 }
4575
Adam Cohen091440a2015-03-18 14:16:05 -07004576 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004577 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4578 // on the device, then we always show the first run cling experience (or if there is no
4579 // launcher2). Otherwise, we prompt the user upon started for migration
4580 LauncherClings launcherClings = new LauncherClings(this);
4581 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4582 if (mModel.canMigrateFromOldLauncherDb(this)) {
4583 launcherClings.showMigrationCling();
4584 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004585 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004586 }
4587 }
4588 }
4589
Winson Chunga6945242014-01-08 14:04:34 -08004590 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004591 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4592 if (mHotseat != null) mHotseat.setAlpha(1f);
4593 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4594 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004595 }
4596
4597 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004598 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4599 if (mHotseat != null) mHotseat.setAlpha(0f);
4600 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4601 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004602 }
4603
Mathew Inwood72fbec12013-11-19 15:45:07 +00004604 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004605 // Called from search suggestion, not supported in other profiles.
4606 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4607 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4608 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4609 myUser);
4610 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004611 return null;
4612 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004613 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004614 }
4615
4616 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4617 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004618 // Called from search suggestion, not supported in other profiles.
4619 return createShortcutDragInfo(shortcutIntent, caption, icon,
4620 UserHandleCompat.myUserHandle());
4621 }
4622
4623 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4624 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004625 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004626 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004627 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004628 }
4629
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004630 protected void moveWorkspaceToDefaultScreen() {
4631 mWorkspace.moveToDefaultScreen(false);
4632 }
4633
Mathew Inwood72fbec12013-11-19 15:45:07 +00004634 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4635 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004636 mWorkspace.onExternalDragStartedWithItem(dragView);
4637 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004638 }
4639
Anjali Koppalf5d29132014-02-28 13:33:27 -08004640 @Override
4641 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004642 if (mLauncherCallbacks != null) {
4643 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4644 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004645 }
4646
Winson Chung80baf5a2010-08-09 16:03:15 -07004647 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004648 * Returns a FastBitmapDrawable with the icon, accurately sized.
4649 */
4650 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4651 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4652 d.setFilterBitmap(true);
4653 resizeIconDrawable(d);
4654 return d;
4655 }
4656
4657 /**
4658 * Resizes an icon drawable to the correct icon size.
4659 */
4660 public void resizeIconDrawable(Drawable icon) {
4661 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4662 }
4663
4664 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004665 * Prints out out state for debugging.
4666 */
4667 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004668 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004669 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004670 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4671 Log.d(TAG, "mRestoring=" + mRestoring);
4672 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4673 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004674 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004675 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004676 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004677
Daniel Sandler325dc232013-06-05 22:57:57 -04004678 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004679 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004680
4681 @Override
4682 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4683 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004684 synchronized (sDumpLogs) {
4685 writer.println(" ");
4686 writer.println("Debug logs: ");
4687 for (int i = 0; i < sDumpLogs.size(); i++) {
4688 writer.println(" " + sDumpLogs.get(i));
4689 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004690 }
Adam Cohen9211d422014-10-07 18:14:53 -07004691 if (mLauncherCallbacks != null) {
4692 mLauncherCallbacks.dump(prefix, fd, writer, args);
4693 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004694 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004695
4696 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004697 if (DEBUG_DUMP_LOG) {
4698 synchronized (sDumpLogs) {
4699 Log.d(TAG, "");
4700 Log.d(TAG, "*********************");
4701 Log.d(TAG, "Launcher debug logs: ");
4702 for (int i = 0; i < sDumpLogs.size(); i++) {
4703 Log.d(TAG, " " + sDumpLogs.get(i));
4704 }
4705 Log.d(TAG, "*********************");
4706 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004707 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004708 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004709 }
4710
4711 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004712 addDumpLog(tag, log, null, debugLog);
4713 }
4714
4715 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004716 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004717 if (e != null) {
4718 Log.d(tag, log, e);
4719 } else {
4720 Log.d(tag, log);
4721 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004722 }
Winson Chungede41292013-09-19 16:27:36 -07004723 if (DEBUG_DUMP_LOG) {
4724 sDateStamp.setTime(System.currentTimeMillis());
4725 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004726 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4727 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004728 }
Winson Chungede41292013-09-19 16:27:36 -07004729 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004730 }
4731
Adam Cohen59400422014-03-05 18:07:04 -08004732 public static CustomAppWidget getCustomAppWidget(String name) {
4733 return sCustomAppWidgets.get(name);
4734 }
4735
4736 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4737 return sCustomAppWidgets;
4738 }
4739
Winson Chungede41292013-09-19 16:27:36 -07004740 public void dumpLogsToLocalData() {
4741 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004742 new AsyncTask<Void, Void, Void>() {
4743 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004744 boolean success = false;
4745 sDateStamp.setTime(sRunStart);
4746 String FILENAME = sDateStamp.getMonth() + "-"
4747 + sDateStamp.getDay() + "_"
4748 + sDateStamp.getHours() + "-"
4749 + sDateStamp.getMinutes() + "_"
4750 + sDateStamp.getSeconds() + ".txt";
4751
4752 FileOutputStream fos = null;
4753 File outFile = null;
4754 try {
4755 outFile = new File(getFilesDir(), FILENAME);
4756 outFile.createNewFile();
4757 fos = new FileOutputStream(outFile);
4758 } catch (Exception e) {
4759 e.printStackTrace();
4760 }
4761 if (fos != null) {
4762 PrintWriter writer = new PrintWriter(fos);
4763
4764 writer.println(" ");
4765 writer.println("Debug logs: ");
4766 synchronized (sDumpLogs) {
4767 for (int i = 0; i < sDumpLogs.size(); i++) {
4768 writer.println(" " + sDumpLogs.get(i));
4769 }
4770 }
4771 writer.close();
4772 }
4773 try {
4774 if (fos != null) {
4775 fos.close();
4776 success = true;
4777 }
4778 } catch (IOException e) {
4779 e.printStackTrace();
4780 }
Michael Jurka43467462013-11-14 12:03:58 +01004781 return null;
Winson Chungede41292013-09-19 16:27:36 -07004782 }
Michael Jurka43467462013-11-14 12:03:58 +01004783 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004784 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004785 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004786}
Michael Jurka2763be32011-02-24 11:19:57 -08004787
Dan Sandlerd5024042014-01-09 15:01:33 -05004788interface DebugIntents {
4789 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4790 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004791}