blob: dc63a761b1525a5df3e2ecb01e6aad704dcf27df [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 Sandler6053b8002013-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;
402
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700403 FocusIndicatorView mFocusHandler;
404
Sunny Goyal316490e2015-06-02 09:38:28 -0700405 @Thunk boolean mRotationEnabled = false;
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700406 private boolean mPreferenceObserverRegistered = false;
407
408 final private SharedPreferences.OnSharedPreferenceChangeListener mSettingsObserver =
409 new SharedPreferences.OnSharedPreferenceChangeListener() {
410 @Override
411 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
412 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
413 if (mRotationEnabled = sharedPreferences.getBoolean(
414 Utilities.ALLOW_ROTATION_PREFERENCE_KEY, false)) {
415 unlockScreenOrientation(true);
416 } else {
417 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
418 }
419 }
420 }
421 };
422
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800423 @Override
424 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700425 if (DEBUG_STRICT_MODE) {
426 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
427 .detectDiskReads()
428 .detectDiskWrites()
429 .detectNetwork() // or .detectAll() for all detectable problems
430 .penaltyLog()
431 .build());
432 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
433 .detectLeakedSqlLiteObjects()
434 .detectLeakedClosableObjects()
435 .penaltyLog()
436 .penaltyDeath()
437 .build());
438 }
439
Adam Cohen9211d422014-10-07 18:14:53 -0700440 if (mLauncherCallbacks != null) {
441 mLauncherCallbacks.preOnCreate();
442 }
443
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800444 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400445
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400446 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400447 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700448 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700449
Adam Cohen2e6da152015-05-06 11:42:25 -0700450 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700451 mDeviceProfile = getResources().getConfiguration().orientation
452 == Configuration.ORIENTATION_LANDSCAPE ?
453 app.getInvariantDeviceProfile().landscapeProfile
454 : app.getInvariantDeviceProfile().portraitProfile;
455
Winson Chung5f8afe62013-08-12 16:19:28 -0700456 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400457 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700458 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700459 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800460 mModel = app.setLauncher(this);
461 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700462
Joe Onorato41a12d22009-10-31 18:30:00 -0400463 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800465 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700466
Daniel Sandlerff02d492013-08-05 02:12:05 -0400467 mStats = new Stats(this);
468
Sunny Goyalffe83f12014-08-14 17:39:34 -0700469 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700470
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700471 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
472 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700473
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700474 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
475 // this also ensures that any synchronous binding below doesn't re-trigger another
476 // LauncherModel load.
477 mPaused = false;
478
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800479 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200480 android.os.Debug.startMethodTracing(
481 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800482 }
483
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800484 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700485
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700487 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800488
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800489 registerContentObservers();
490
Joe Onorato7c312c12009-08-13 21:36:53 -0700491 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700492
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800493 mSavedState = savedInstanceState;
494 restoreState(mSavedState);
495
496 if (PROFILE_STARTUP) {
497 android.os.Debug.stopMethodTracing();
498 }
499
500 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700501 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700502 // If the user leaves launcher, then we should just load items asynchronously when
503 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700504 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700505 } else {
506 // We only load the page synchronously if the user rotates (or triggers a
507 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700508 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700509 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800510 }
511
512 // For handling default keys
513 mDefaultKeySsb = new SpannableStringBuilder();
514 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800515
516 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
517 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800518
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700519 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
520 // In case we are on a device with locked rotation, we should look at preferences to check
521 // if the user has specifically allowed rotation.
522 if (!mRotationEnabled) {
523 getSharedPreferences(LauncherFiles.ROTATION_PREF_FILE,
524 Context.MODE_MULTI_PROCESS).registerOnSharedPreferenceChangeListener(
525 mSettingsObserver);
526 mPreferenceObserverRegistered = true;
527 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
528 }
529
530 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
531 // we want the screen to auto-rotate based on the current orientation
Adam Cohenf9426d52012-06-04 17:26:21 -0700532 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700533
Adam Cohen9211d422014-10-07 18:14:53 -0700534 if (mLauncherCallbacks != null) {
535 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700536 if (mLauncherCallbacks.hasLauncherOverlay()) {
537 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700538 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
539 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
540 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700541 mWorkspace.setLauncherOverlay(mLauncherOverlay);
542 }
Adam Cohen9211d422014-10-07 18:14:53 -0700543 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700544
545 if (shouldShowIntroScreen()) {
546 showIntroScreen();
547 } else {
548 showFirstRunActivity();
549 showFirstRunClings();
550 }
Adam Cohen9211d422014-10-07 18:14:53 -0700551 }
552
553 private LauncherCallbacks mLauncherCallbacks;
554
555 public void onPostCreate(Bundle savedInstanceState) {
556 super.onPostCreate(savedInstanceState);
557 if (mLauncherCallbacks != null) {
558 mLauncherCallbacks.onPostCreate(savedInstanceState);
559 }
560 }
561
562 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
563 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700564 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
565 @Override
566 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700567 if (LOGD) {
568 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
569 }
Winson Chung94804152015-05-08 13:06:44 -0700570 mAppsView.setFixedBounds(bounds);
571 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700572 }
573
574 @Override
575 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700576 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
577 // Dismiss All Apps if we aren't already paused/invisible
578 if (!mPaused) {
579 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
580 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
581 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700582 }
Winson Chung0f785722015-04-08 10:27:49 -0700583 }
Sunny Goyal5683f872015-05-29 14:54:40 -0700584
585 @Override
586 public void setSearchManager(AppSearchManager manager) {
587 mAppsView.setSearchManager(manager);
588 }
Winson Chung0f785722015-04-08 10:27:49 -0700589 });
Jun Mukai8af0cd82015-05-12 12:32:12 -0700590 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
591 private boolean mImportanceStored = false;
592 private int mWorkspaceImportanceForAccessibility =
593 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
594 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
595
596 @Override
597 public void onSearchOverlayOpened() {
598 if (mImportanceStored) {
599 return;
600 }
601 // The underlying workspace and hotseat are temporarily suppressed by the search
602 // overlay. So they sholudn't be accessible.
603 mWorkspaceImportanceForAccessibility = mWorkspace.getImportantForAccessibility();
604 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
605 mWorkspace.setImportantForAccessibility(
606 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
607 mHotseat.setImportantForAccessibility(
608 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
609 mImportanceStored = true;
610 }
611
612 @Override
613 public void onSearchOverlayClosed() {
614 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
615 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
616 mImportanceStored = false;
617 }
618 });
Adam Cohen9211d422014-10-07 18:14:53 -0700619 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700620 }
621
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700622 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700623 public void onLauncherProviderChange() {
624 if (mLauncherCallbacks != null) {
625 mLauncherCallbacks.onLauncherProviderChange();
626 }
627 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700628
Adam Cohen9211d422014-10-07 18:14:53 -0700629 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700630 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700631 if (mLauncherCallbacks != null) {
632 return mLauncherCallbacks.hasCustomContentToLeft();
633 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700634 return false;
635 }
636
Dave Hawkeya8881582013-09-17 15:55:33 -0600637 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500638 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600639 * {@link #addToCustomContentPage}. This will only be invoked if
640 * {@link #hasCustomContentToLeft()} is {@code true}.
641 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500642 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700643 if (mLauncherCallbacks != null) {
644 mLauncherCallbacks.populateCustomContentContainer();
645 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600646 }
647
648 /**
649 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
650 * ensure the custom content page is added or removed if necessary.
651 */
652 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600653 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600654 // Not bound yet, wait for bindScreens to be called.
655 return;
656 }
657
658 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
659 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500660 mWorkspace.createCustomContentContainer();
661 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600662 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
663 mWorkspace.removeCustomContentPage();
664 }
665 }
666
Anton Hanssona2f665f2013-09-26 12:18:32 +0100667 public Stats getStats() {
668 return mStats;
669 }
670
Bjorn Bringertc459e522013-06-07 19:36:01 +0100671 public LayoutInflater getInflater() {
672 return mInflater;
673 }
674
Hyunyoung Song3f471442015-04-08 19:01:34 -0700675 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700676 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
677 // that is subsequently removed from the workspace in startBinding().
678 return !mModel.isLoadingWorkspace();
679 }
680
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800681 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000682 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100683 if (Build.VERSION.SDK_INT >= 17) {
684 return View.generateViewId();
685 } else {
686 // View.generateViewId() is not available. The following fallback logic is a copy
687 // of its implementation.
688 for (;;) {
689 final int result = sNextGeneratedId.get();
690 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
691 int newValue = result + 1;
692 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
693 if (sNextGeneratedId.compareAndSet(result, newValue)) {
694 return result;
695 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000696 }
697 }
698 }
699
700 public int getViewIdForItem(ItemInfo info) {
701 // This cast is safe given the > 2B range for int.
702 int itemId = (int) info.id;
703 if (mItemIdToViewId.containsKey(itemId)) {
704 return mItemIdToViewId.get(itemId);
705 }
706 int viewId = generateViewId();
707 mItemIdToViewId.put(itemId, viewId);
708 return viewId;
709 }
710
711 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700712 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
713 * a configuration step, this allows the proper animations to run after other transitions.
714 */
Adam Cohendb364c32014-05-20 14:23:40 -0700715 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700716 long screenId = args.screenId;
717 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
718 // When the screen id represents an actual screen (as opposed to a rank) we make sure
719 // that the drop page actually exists.
720 screenId = ensurePendingDropLayoutExists(args.screenId);
721 }
Adam Cohendb364c32014-05-20 14:23:40 -0700722
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800723 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800724 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700725 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700726 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700727 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800728 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700729 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700730 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700731 case REQUEST_RECONFIGURE_APPWIDGET:
732 completeRestoreAppWidget(args.appWidgetId);
733 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800734 }
Michael Jurka27614d22012-04-02 06:40:22 -0700735 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
736 // if you turned the screen off and then back while in All Apps, Launcher would not
737 // return to the workspace. Clearing mAddInfo.container here fixes this issue
738 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700739 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800740 }
741
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800742 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700743 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700744 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700745 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700746 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800747 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700748
Adam Cohenad4e15c2013-10-17 16:21:35 -0700749 Runnable exitSpringLoaded = new Runnable() {
750 @Override
751 public void run() {
752 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
753 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
754 }
755 };
756
Michael Jurka8b805b12012-04-18 14:23:14 -0700757 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700758 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700759 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
760 if (resultCode == RESULT_CANCELED) {
761 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700762 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700763 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700764 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800765 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700766 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700767 }
768 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200769 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
770 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700771 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200772 }
773 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700774 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200775
Adam Cohened66b2b2012-01-23 17:28:51 -0800776 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700777 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700778
Adam Cohendb364c32014-05-20 14:23:40 -0700779 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800780 // We have special handling for widgets
781 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800782 final int appWidgetId;
783 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
784 : -1;
785 if (widgetId < 0) {
786 appWidgetId = pendingAddWidgetId;
787 } else {
788 appWidgetId = widgetId;
789 }
790
Adam Cohenad4e15c2013-10-17 16:21:35 -0700791 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800792 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700793 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
794 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700795 result = RESULT_CANCELED;
796 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700797 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700798 @Override
799 public void run() {
800 exitSpringLoadedDragModeDelayed(false, 0, null);
801 }
802 };
Adam Cohendb364c32014-05-20 14:23:40 -0700803 if (workspaceLocked) {
804 // No need to remove the empty screen if we're mid-binding, as the
805 // the bind will not add the empty screen.
806 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
807 } else {
808 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
809 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
810 }
Winson Chung5aaab772012-04-27 15:24:02 -0700811 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700812 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700813 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
814 // When the screen id represents an actual screen (as opposed to a rank)
815 // we make sure that the drop page actually exists.
816 mPendingAddInfo.screenId =
817 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
818 }
Adam Cohendb364c32014-05-20 14:23:40 -0700819 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
820
821 dropLayout.setDropPending(true);
822 final Runnable onComplete = new Runnable() {
823 @Override
824 public void run() {
825 completeTwoStageWidgetDrop(resultCode, appWidgetId);
826 dropLayout.setDropPending(false);
827 }
828 };
829 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
830 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
831 } else {
832 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
833 mPendingAddInfo);
834 sPendingAddItem = args;
835 }
Winson Chung5aaab772012-04-27 15:24:02 -0700836 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800837 return;
838 }
839
Sunny Goyalff572272014-07-23 13:58:07 -0700840 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
841 if (resultCode == RESULT_OK) {
842 // Update the widget view.
843 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
844 pendingAddWidgetId, mPendingAddInfo);
845 if (workspaceLocked) {
846 sPendingAddItem = args;
847 } else {
848 completeAdd(args);
849 }
850 }
851 // Leave the widget in the pending state if the user canceled the configure.
852 return;
853 }
854
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800855 // The pattern used here is that a user PICKs a specific application,
856 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700857
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800858 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
859 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700860 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700861 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
862 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800863 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700864 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800865 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700866 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700867 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
868 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800869 }
Adam Cohen00074722013-10-11 17:46:09 -0700870 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700871 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700872 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800873 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800874 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800875
876 }
877
878 @Override
879 protected void onActivityResult(
880 final int requestCode, final int resultCode, final Intent data) {
881 handleActivityResult(requestCode, resultCode, data);
882 if (mLauncherCallbacks != null) {
883 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
884 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800885 }
886
Adam Cohendb364c32014-05-20 14:23:40 -0700887 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
888 appWidgetId, ItemInfo info) {
889 PendingAddArguments args = new PendingAddArguments();
890 args.requestCode = requestCode;
891 args.intent = data;
892 args.container = info.container;
893 args.screenId = info.screenId;
894 args.cellX = info.cellX;
895 args.cellY = info.cellY;
896 args.appWidgetId = appWidgetId;
897 return args;
898 }
899
900 /**
901 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
902 *
903 * @param screenId the screen id to check
904 * @return the new screen, or screenId if it exists
905 */
906 private long ensurePendingDropLayoutExists(long screenId) {
907 CellLayout dropLayout =
908 (CellLayout) mWorkspace.getScreenWithId(screenId);
909 if (dropLayout == null) {
910 // it's possible that the add screen was removed because it was
911 // empty and a re-bind occurred
912 mWorkspace.addExtraEmptyScreen();
913 return mWorkspace.commitExtraEmptyScreen();
914 } else {
915 return screenId;
916 }
917 }
918
Adam Cohen091440a2015-03-18 14:16:05 -0700919 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700920 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700921 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800922 Runnable onCompleteRunnable = null;
923 int animationType = 0;
924
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700925 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800926 if (resultCode == RESULT_OK) {
927 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
928 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700929 mPendingAddWidgetInfo);
930 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800931 onCompleteRunnable = new Runnable() {
932 @Override
933 public void run() {
934 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700935 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700936 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
937 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800938 }
939 };
940 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800941 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800942 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800943 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700944 if (mDragLayer.getAnimatedView() != null) {
945 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
946 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
947 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700948 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700949 // The animated view may be null in the case of a rotation during widget configuration
950 onCompleteRunnable.run();
951 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800952 }
953
954 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700955 protected void onStop() {
956 super.onStop();
957 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700958
959 if (mLauncherCallbacks != null) {
960 mLauncherCallbacks.onStop();
961 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700962 }
963
964 @Override
965 protected void onStart() {
966 super.onStart();
967 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700968
969 if (mLauncherCallbacks != null) {
970 mLauncherCallbacks.onStart();
971 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700972 }
973
974 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800975 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200976 long startTime = 0;
977 if (DEBUG_RESUME_TIME) {
978 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400979 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200980 }
Adam Cohen9211d422014-10-07 18:14:53 -0700981
982 if (mLauncherCallbacks != null) {
983 mLauncherCallbacks.preOnResume();
984 }
985
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800986 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700987
Winson Chung4a2afa32012-07-19 14:53:05 -0700988 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -0700989 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700990 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -0800991 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -0700992 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -0700993 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -0700994 // view after launching an app, as they may be depending on the UI to be static to
995 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -0700996 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -0700997 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -0800998 } else if (mOnResumeState == State.WIDGETS) {
999 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001000 }
1001 mOnResumeState = State.NONE;
1002
Winson Chungcd99cd32015-04-29 11:03:24 -07001003 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001004 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1005 // Otherwise, notify the callbacks if we are in all apps mode
1006 if (mState == State.APPS) {
1007 if (mLauncherCallbacks != null) {
1008 mLauncherCallbacks.onAllAppsShown();
1009 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001010 }
1011 }
1012
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001013 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001014 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1015 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001016
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001017 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001018 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001019 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001020 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001021 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001022 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001023 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001024 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001025 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1026 // execute them here
1027 long startTimeCallbacks = 0;
1028 if (DEBUG_RESUME_TIME) {
1029 startTimeCallbacks = System.currentTimeMillis();
1030 }
1031
Michael Jurka1e2f4652013-07-08 18:03:46 -07001032 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1033 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001034 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001035 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001036 if (DEBUG_RESUME_TIME) {
1037 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1038 (System.currentTimeMillis() - startTimeCallbacks));
1039 }
Michael Jurka447bf842013-05-15 14:52:15 +02001040 }
Michael Jurka54554252013-08-01 12:52:23 +02001041 if (mOnResumeCallbacks.size() > 0) {
1042 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1043 mOnResumeCallbacks.get(i).run();
1044 }
1045 mOnResumeCallbacks.clear();
1046 }
Winson Chunge4e50662012-01-23 14:45:13 -08001047
1048 // Reset the pressed state of icons that were locked in the press state while activities
1049 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001050 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001051 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001052 mWaitingForResume.setStayPressed(false);
1053 }
Winson Chung82963d52013-10-09 11:20:57 -07001054
Adam Cohen06dff352012-06-01 17:17:08 -07001055 // It is possible that widgets can receive updates while launcher is not in the foreground.
1056 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1057 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1058 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001059 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001060 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001061
Michael Jurka447bf842013-05-15 14:52:15 +02001062 if (DEBUG_RESUME_TIME) {
1063 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1064 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001065
1066 if (mWorkspace.getCustomContentCallbacks() != null) {
1067 // If we are resuming and the custom content is the current page, we call onShow().
1068 // It is also poassible that onShow will instead be called slightly after first layout
1069 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1070 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001071 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001072 }
1073 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001074 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001075 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001076
Sunny Goyal756adbc2015-04-16 15:20:51 -07001077 if (!isWorkspaceLoading()) {
1078 // Process any items that were added while Launcher was away.
1079 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1080 }
Adam Cohen9211d422014-10-07 18:14:53 -07001081
1082 if (mLauncherCallbacks != null) {
1083 mLauncherCallbacks.onResume();
1084 }
Adam Cohen06dff352012-06-01 17:17:08 -07001085 }
1086
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001087 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001088 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001089 // Ensure that items added to Launcher are queued until Launcher returns
1090 InstallShortcutReceiver.enableInstallQueue();
1091
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001092 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001093 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001094 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001095 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001096
1097 // We call onHide() aggressively. The custom content callbacks should be able to
1098 // debounce excess onHide calls.
1099 if (mWorkspace.getCustomContentCallbacks() != null) {
1100 mWorkspace.getCustomContentCallbacks().onHide();
1101 }
Romain Guycbb89e42009-06-08 15:52:54 -07001102
Adam Cohen9211d422014-10-07 18:14:53 -07001103 if (mLauncherCallbacks != null) {
1104 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001105 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001106 }
1107
1108 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001109 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1110 // by a onResume or by scrolling otherwise.
1111 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001112
1113 // Custom content is completely hidden
1114 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001115
1116 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1117 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001118
1119 // Indicates whether the user is allowed to scroll away from the custom content.
1120 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001121 }
1122
Adam Cohenc2d6e892014-10-16 09:49:24 -07001123 public interface LauncherOverlay {
1124
1125 /**
1126 * Touch interaction leading to overscroll has begun
1127 */
1128 public void onScrollInteractionBegin();
1129
1130 /**
1131 * Touch interaction related to overscroll has ended
1132 */
1133 public void onScrollInteractionEnd();
1134
1135 /**
1136 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1137 * screen (or in the case of RTL, the rightmost screen).
1138 */
1139 public void onScrollChange(int progress, boolean rtl);
1140
1141 /**
1142 * Screen has stopped scrolling
1143 */
1144 public void onScrollSettled();
1145
1146 /**
1147 * This method can be called by the Launcher in order to force the LauncherOverlay
1148 * to exit fully immersive mode.
1149 */
1150 public void forceExitFullImmersion();
1151 }
1152
Winson Chung0f785722015-04-08 10:27:49 -07001153 public interface LauncherAppsCallbacks {
1154 /**
1155 * Updates launcher to the available space that AllApps can take so as not to overlap with
1156 * any other views.
1157 */
1158 public void onAllAppsBoundsChanged(Rect bounds);
1159
1160 /**
1161 * Called to dismiss all apps if it is showing.
1162 */
1163 public void dismissAllApps();
Sunny Goyal5683f872015-05-29 14:54:40 -07001164
1165 /**
1166 * Sets the search manager to be used for app search.
1167 */
1168 public void setSearchManager(AppSearchManager manager);
Winson Chung0f785722015-04-08 10:27:49 -07001169 }
1170
Jun Mukai8af0cd82015-05-12 12:32:12 -07001171 public interface LauncherSearchCallbacks {
1172 /**
1173 * Called when the search overlay is shown.
1174 */
1175 public void onSearchOverlayOpened();
1176
1177 /**
1178 * Called when the search overlay is dismissed.
1179 */
1180 public void onSearchOverlayClosed();
1181 }
1182
Adam Cohenc2d6e892014-10-16 09:49:24 -07001183 public interface LauncherOverlayCallbacks {
1184 /**
1185 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1186 * however it doesn't modify any state within the launcher.
1187 */
1188 public boolean canEnterFullImmersion();
1189
1190 /**
1191 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1192 * eg. by occupying the full screen and handling all touch events.
1193 *
1194 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1195 * case, Launcher will modify any necessary state and assumes the overlay is
1196 * handling all interaction. If false, the LauncherOverlay should cancel any
1197 *
1198 */
1199 public boolean enterFullImmersion();
1200
1201 /**
1202 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1203 * full control over UI and state.
1204 */
1205 public void exitFullImmersion();
1206 }
1207
1208 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1209
1210 @Override
1211 public boolean canEnterFullImmersion() {
1212 return mState == State.WORKSPACE;
1213 }
1214
1215 @Override
1216 public boolean enterFullImmersion() {
1217 if (mState == State.WORKSPACE) {
1218 // When fully immersed, disregard any touches which fall through.
1219 mDragLayer.setBlockTouch(true);
1220 return true;
1221 }
1222 return false;
1223 }
1224
1225 @Override
1226 public void exitFullImmersion() {
1227 mDragLayer.setBlockTouch(false);
1228 }
1229 }
1230
Jorim Jaggid017f882014-01-14 17:08:48 -08001231 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001232 if (mLauncherCallbacks != null) {
1233 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001234 } else {
1235 // On devices with a locked orientation, we will at least have the allow rotation
1236 // setting.
1237 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001238 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001239 }
1240
Adam Cohen9211d422014-10-07 18:14:53 -07001241 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001242 CustomContentCallbacks callbacks, String description) {
1243 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001244 }
1245
Adam Cohenedb40762013-07-18 16:45:45 -07001246 // The custom content needs to offset its content to account for the QSB
1247 public int getTopOffsetForCustomContent() {
1248 return mWorkspace.getPaddingTop();
1249 }
1250
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001251 @Override
1252 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001253 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001254 if (mModel.isCurrentCallbacks(this)) {
1255 mModel.stopLoader();
1256 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001257 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1258
Romain Guy13c2e7b2010-03-10 19:45:00 -08001259 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001260 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001261
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001262 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001263 @Override
1264 public void onWindowFocusChanged(boolean hasFocus) {
1265 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001266 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001267
1268 if (mLauncherCallbacks != null) {
1269 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1270 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001271 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001272
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001273 private boolean acceptFilter() {
1274 final InputMethodManager inputManager = (InputMethodManager)
1275 getSystemService(Context.INPUT_METHOD_SERVICE);
1276 return !inputManager.isFullscreenMode();
1277 }
1278
1279 @Override
1280 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001281 final int uniChar = event.getUnicodeChar();
1282 final boolean handled = super.onKeyDown(keyCode, event);
1283 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1284 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001285 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1286 keyCode, event);
1287 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001288 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001289 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001290 // showSearchDialog()
1291 // If there are multiple keystrokes before the search dialog takes focus,
1292 // onSearchRequested() will be called for every keystroke,
1293 // but it is idempotent, so it's fine.
1294 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 }
1296 }
1297
Joe Onorato8a9625e2010-01-28 15:55:35 -08001298 // Eat the long press event so the keyboard doesn't come up.
1299 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1300 return true;
1301 }
1302
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001303 return handled;
1304 }
1305
Karl Rosaen138a0412009-04-23 19:00:21 -07001306 private String getTypedText() {
1307 return mDefaultKeySsb.toString();
1308 }
1309
1310 private void clearTypedText() {
1311 mDefaultKeySsb.clear();
1312 mDefaultKeySsb.clearSpans();
1313 Selection.setSelection(mDefaultKeySsb, 0);
1314 }
1315
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001316 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001317 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1318 * State
1319 */
1320 private static State intToState(int stateOrdinal) {
1321 State state = State.WORKSPACE;
1322 final State[] stateValues = State.values();
1323 for (int i = 0; i < stateValues.length; i++) {
1324 if (stateValues[i].ordinal() == stateOrdinal) {
1325 state = stateValues[i];
1326 break;
1327 }
1328 }
1329 return state;
1330 }
1331
1332 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001333 * Restores the previous state, if it exists.
1334 *
1335 * @param savedState The previous state.
1336 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001337 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001338 private void restoreState(Bundle savedState) {
1339 if (savedState == null) {
1340 return;
1341 }
1342
Michael Jurka883f55b2010-10-21 15:47:14 -07001343 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001344 if (state == State.APPS || state == State.WIDGETS) {
1345 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001346 }
1347
Adam Cohen21cd0022013-10-09 18:57:02 -07001348 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1349 PagedView.INVALID_RESTORE_PAGE);
1350 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001351 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 }
1353
Winson Chung3d503fb2011-07-13 17:25:49 -07001354 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001355 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001356
Winson Chung3d503fb2011-07-13 17:25:49 -07001357 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1358 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001359 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001360 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1361 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001362 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1363 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001364 AppWidgetProviderInfo info = savedState.getParcelable(
1365 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001366 mPendingAddWidgetInfo = info == null ?
1367 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1368
Adam Cohen4637b5a2013-11-04 18:21:24 -08001369 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001370 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001371 mRestoring = true;
1372 }
1373
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001374 mItemIdToViewId = (HashMap<Integer, Integer>)
1375 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001376 }
1377
1378 /**
1379 * Finds all the views we need and configure them properly.
1380 */
1381 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001382 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001383
Craig Mautner360310b2012-10-26 15:13:08 -07001384 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001385 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001386 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1387 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001388 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001389 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001390
John Spurlock77e1f472013-09-11 10:09:51 -04001391 mLauncherView.setSystemUiVisibility(
1392 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001393 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001394
Winson Chung4c98d922011-05-31 16:50:48 -07001395 // Setup the drag layer
1396 mDragLayer.setup(this, dragController);
1397
Winson Chung3d503fb2011-07-13 17:25:49 -07001398 // Setup the hotseat
1399 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1400 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001401 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001402 }
1403
Jorim Jaggid017f882014-01-14 17:08:48 -08001404 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001405 View widgetButton = findViewById(R.id.widget_button);
1406 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001407 @Override
1408 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001409 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001410 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001411 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001412 }
1413 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001414 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1415
1416 View wallpaperButton = findViewById(R.id.wallpaper_button);
1417 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001418 @Override
1419 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001420 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001421 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001422 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001423 }
1424 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001425 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1426
1427 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001428 if (hasSettings()) {
1429 settingsButton.setOnClickListener(new OnClickListener() {
1430 @Override
1431 public void onClick(View arg0) {
1432 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001433 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001434 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001435 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001436 });
1437 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1438 } else {
1439 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001440 }
1441
Adam Cohendbdff6b2013-09-18 19:09:15 -07001442 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001443
Winson Chung4c98d922011-05-31 16:50:48 -07001444 // Setup the workspace
1445 mWorkspace.setHapticFeedbackEnabled(false);
1446 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001447 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001448 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001449
Winson Chungf0ea4d32011-06-06 14:27:16 -07001450 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001451 mSearchDropTargetBar = (SearchDropTargetBar)
1452 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001453
Winson Chungb745afb2015-03-02 11:51:23 -08001454 // Setup Apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001455 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001456 if (isAllAppsSearchOverridden()) {
1457 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001458 }
Winson Chungb745afb2015-03-02 11:51:23 -08001459
Winson Chungf0ea4d32011-06-06 14:27:16 -07001460 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001461 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001462
Winson Chung3d503fb2011-07-13 17:25:49 -07001463 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001464 dragController.setDragScoller(mWorkspace);
1465 dragController.setScrollView(mDragLayer);
1466 dragController.setMoveTarget(mWorkspace);
1467 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001468 if (mSearchDropTargetBar != null) {
1469 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001470 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001471 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001472
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001473 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001474 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001475 mWeightWatcher = new WeightWatcher(this);
1476 mWeightWatcher.setAlpha(0.5f);
1477 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001478 new FrameLayout.LayoutParams(
1479 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001480 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001481 Gravity.BOTTOM)
1482 );
Adam Cohen39a06042013-07-19 14:30:12 -07001483
1484 boolean show = shouldShowWeightWatcher();
1485 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001486 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001487 }
1488
1489 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001490 * Sets the all apps button. This method is called from {@link Hotseat}.
1491 */
1492 public void setAllAppsButton(View allAppsButton) {
1493 mAllAppsButton = allAppsButton;
1494 }
1495
1496 public View getAllAppsButton() {
1497 return mAllAppsButton;
1498 }
1499
1500 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001501 * Creates a view representing a shortcut.
1502 *
1503 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001504 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001505 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001506 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001507 }
1508
1509 /**
1510 * Creates a view representing a shortcut inflated from the specified resource.
1511 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 * @param parent The group the shortcut belongs to.
1513 * @param info The data structure describing the shortcut.
1514 *
1515 * @return A View inflated from layoutResId.
1516 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001517 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001518 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001519 parent, false);
1520 favorite.applyFromShortcutInfo(info, mIconCache);
1521 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001523 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 return favorite;
1525 }
1526
1527 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 * Add a shortcut to the workspace.
1529 *
1530 * @param data The intent describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001532 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001533 int cellY) {
1534 int[] cellXY = mTmpAddItemCellCoordinates;
1535 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001536 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001537
Sunny Goyal5c97f512015-05-19 16:03:28 -07001538 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001539 if (info == null) {
1540 return;
1541 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001542 final View view = createShortcut(info);
1543
Sunny Goyal5c97f512015-05-19 16:03:28 -07001544 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001545 // First we check if we already know the exact location where we want to add this item.
1546 if (cellX >= 0 && cellY >= 0) {
1547 cellXY[0] = cellX;
1548 cellXY[1] = cellY;
1549 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001550
1551 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001552 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001553 true, null,null)) {
1554 return;
1555 }
1556 DragObject dragObject = new DragObject();
1557 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001558 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1559 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001560 return;
1561 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001562 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001563 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001564 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001565 foundCellSpan = (result != null);
1566 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001567 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001568 }
1569
1570 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001571 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001572 return;
1573 }
1574
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001575 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001576
1577 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001578 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001579 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001580 }
1581 }
1582
Adam Cohen2e6da152015-05-06 11:42:25 -07001583 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1584 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001585 // We want to account for the extra amount of padding that we are adding to the widget
1586 // to ensure that it gets the full amount of space that it has requested
1587 int requiredWidth = minWidth + padding.left + padding.right;
1588 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2e6da152015-05-06 11:42:25 -07001589 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001590 }
1591
Adam Cohen2e6da152015-05-06 11:42:25 -07001592 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1593 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001594 }
1595
Adam Cohen2e6da152015-05-06 11:42:25 -07001596 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1597 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001598 }
1599
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001600 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001601 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001603 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 */
Adam Cohen091440a2015-03-18 14:16:05 -07001605 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001606 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1607
1608 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001609 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001610 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1611 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001612 }
Romain Guycbb89e42009-06-08 15:52:54 -07001613
Adam Cohen59400422014-03-05 18:07:04 -08001614 if (appWidgetInfo.isCustomWidget) {
1615 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001616 }
1617
Adam Cohen59400422014-03-05 18:07:04 -08001618 LauncherAppWidgetInfo launcherInfo;
1619 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1620 launcherInfo.spanX = info.spanX;
1621 launcherInfo.spanY = info.spanY;
1622 launcherInfo.minSpanX = info.minSpanX;
1623 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001624 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001625
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001626 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001627 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628
1629 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001630 if (hostView == null) {
1631 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001632 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1633 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001634 } else {
1635 // The AppWidgetHostView has already been inflated and instantiated
1636 launcherInfo.hostView = hostView;
1637 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001638 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001639 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001640 launcherInfo.notifyWidgetSizeChanged(this);
1641
Adam Cohen59400422014-03-05 18:07:04 -08001642 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1643 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001644
1645 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001646 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001647 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001648 }
Romain Guycbb89e42009-06-08 15:52:54 -07001649
Adam Cohended9f8d2010-11-03 13:25:16 -07001650 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1651 @Override
1652 public void onReceive(Context context, Intent intent) {
1653 final String action = intent.getAction();
1654 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1655 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001656 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001657 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001658
Winson Chungc100e8e2011-08-09 16:02:43 -07001659 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001660 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001661 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001662 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001663 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001664 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001665 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1666 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001667 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001668 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1669 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001670 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001671 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1672 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1673 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001674 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001675 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1676 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001677 }
1678 }
1679 };
1680
1681 @Override
1682 public void onAttachedToWindow() {
1683 super.onAttachedToWindow();
1684
1685 // Listen for broadcasts related to user-presence
1686 final IntentFilter filter = new IntentFilter();
1687 filter.addAction(Intent.ACTION_SCREEN_OFF);
1688 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001689 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001690 if (ENABLE_DEBUG_INTENTS) {
1691 filter.addAction(DebugIntents.DELETE_DATABASE);
1692 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1693 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001694 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001695 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001696 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001697 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001698 mVisible = true;
1699 }
1700
Adam Cohen0b395352014-06-09 22:54:36 +00001701 /**
1702 * Sets up transparent navigation and status bars in LMP.
1703 * This method is a no-op for other platform versions.
1704 */
Sunny Goyald0091012015-01-20 15:55:34 -08001705 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001706 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001707 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001708 Window window = getWindow();
1709 window.getAttributes().systemUiVisibility |=
1710 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1711 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1712 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1713 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1714 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1715 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1716 window.setStatusBarColor(Color.TRANSPARENT);
1717 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001718 }
1719 }
1720
Adam Cohended9f8d2010-11-03 13:25:16 -07001721 @Override
1722 public void onDetachedFromWindow() {
1723 super.onDetachedFromWindow();
1724 mVisible = false;
1725
Adam Cohend113e0c2010-11-11 10:48:05 -08001726 if (mAttached) {
1727 unregisterReceiver(mReceiver);
1728 mAttached = false;
1729 }
Winson Chungb745afb2015-03-02 11:51:23 -08001730 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001731 }
1732
1733 public void onWindowVisibilityChanged(int visibility) {
1734 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001735 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001736 // The following code used to be in onResume, but it turns out onResume is called when
1737 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1738 // is a more appropriate event to handle
1739 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001740 if (!mWorkspaceLoading) {
1741 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001742 // We want to let Launcher draw itself at least once before we force it to build
1743 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001744 // apps is nice and speedy.
1745 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001746 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001747 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001748 if (mStarted) return;
1749 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001750 // We delay the layer building a bit in order to give
1751 // other message processing a time to run. In particular
1752 // this avoids a delay in hiding the IME if it was
1753 // currently shown, because doing that may involve
1754 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001755 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001756 final ViewTreeObserver.OnDrawListener listener = this;
1757 mWorkspace.post(new Runnable() {
1758 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001759 if (mWorkspace != null &&
1760 mWorkspace.getViewTreeObserver() != null) {
1761 mWorkspace.getViewTreeObserver().
1762 removeOnDrawListener(listener);
1763 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001764 }
1765 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001766 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001767 }
1768 });
1769 }
1770 clearTypedText();
1771 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001772 }
1773
Adam Cohen091440a2015-03-18 14:16:05 -07001774 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001775 mHandler.removeMessages(ADVANCE_MSG);
1776 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1777 mHandler.sendMessageDelayed(msg, delay);
1778 mAutoAdvanceSentTime = System.currentTimeMillis();
1779 }
1780
Adam Cohen091440a2015-03-18 14:16:05 -07001781 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001782 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1783 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1784 mAutoAdvanceRunning = autoAdvanceRunning;
1785 if (autoAdvanceRunning) {
1786 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1787 sendAdvanceMessage(delay);
1788 } else {
1789 if (!mWidgetsToAdvance.isEmpty()) {
1790 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1791 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1792 }
1793 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001794 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001795 }
1796 }
1797 }
1798
1799 private final Handler mHandler = new Handler() {
1800 @Override
1801 public void handleMessage(Message msg) {
1802 if (msg.what == ADVANCE_MSG) {
1803 int i = 0;
1804 for (View key: mWidgetsToAdvance.keySet()) {
1805 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1806 final int delay = mAdvanceStagger * i;
1807 if (v instanceof Advanceable) {
1808 postDelayed(new Runnable() {
1809 public void run() {
1810 ((Advanceable) v).advance();
1811 }
1812 }, delay);
1813 }
1814 i++;
1815 }
1816 sendAdvanceMessage(mAdvanceInterval);
1817 }
1818 }
1819 };
1820
1821 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001822 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001823 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1824 if (v instanceof Advanceable) {
1825 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001826 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001827 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001828 }
1829 }
1830
1831 void removeWidgetToAutoAdvance(View hostView) {
1832 if (mWidgetsToAdvance.containsKey(hostView)) {
1833 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001834 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001835 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001836 }
1837
Joe Onorato9c1289c2009-08-17 11:03:03 -04001838 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001839 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001840 launcherInfo.hostView = null;
1841 }
1842
Hyunyoung Song3f471442015-04-08 19:01:34 -07001843 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001844 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1845 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001846 }
1847
Winson Chunga6945242014-01-08 14:04:34 -08001848 public DragLayer getDragLayer() {
1849 return mDragLayer;
1850 }
1851
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001852 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001853 return mAppsView;
1854 }
1855
Hyunyoung Song3f471442015-04-08 19:01:34 -07001856 public WidgetsContainerView getWidgetsView() {
1857 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001858 }
1859
Winson Chunga6945242014-01-08 14:04:34 -08001860 public Workspace getWorkspace() {
1861 return mWorkspace;
1862 }
1863
1864 public Hotseat getHotseat() {
1865 return mHotseat;
1866 }
1867
Jorim Jaggid017f882014-01-14 17:08:48 -08001868 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001869 return mOverviewPanel;
1870 }
1871
1872 public SearchDropTargetBar getSearchBar() {
1873 return mSearchDropTargetBar;
1874 }
1875
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001876 public LauncherAppWidgetHost getAppWidgetHost() {
1877 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001878 }
Romain Guycbb89e42009-06-08 15:52:54 -07001879
Winson Chunga9abd0e2010-10-27 17:18:37 -07001880 public LauncherModel getModel() {
1881 return mModel;
1882 }
1883
Winson Chunga6945242014-01-08 14:04:34 -08001884 protected SharedPreferences getSharedPrefs() {
1885 return mSharedPrefs;
1886 }
1887
Adam Cohen2e6da152015-05-06 11:42:25 -07001888 public DeviceProfile getDeviceProfile() {
1889 return mDeviceProfile;
1890 }
1891
Bjorn Bringertc459e522013-06-07 19:36:01 +01001892 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001893 getWindow().closeAllPanels();
1894
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001895 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001896 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001897 }
1898
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001899 @Override
1900 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001901 long startTime = 0;
1902 if (DEBUG_RESUME_TIME) {
1903 startTime = System.currentTimeMillis();
1904 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001905 super.onNewIntent(intent);
1906
1907 // Close the menu
1908 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001909 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001910 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001911
Adam Cohened307df2013-10-02 09:37:31 -07001912 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1913 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1914 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001915
Adam Cohen6fecd412013-10-02 17:41:50 -07001916 if (mWorkspace == null) {
1917 // Can be cases where mWorkspace is null, this prevents a NPE
1918 return;
1919 }
1920 Folder openFolder = mWorkspace.getOpenFolder();
1921 // In all these cases, only animate if we're already on home
1922 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001923
1924 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1925 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001926 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001927 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001928 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001929 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001930
Adam Cohen6fecd412013-10-02 17:41:50 -07001931 closeFolder();
1932 exitSpringLoadedDragMode();
1933
1934 // If we are already on home, then just animate back to the workspace,
1935 // otherwise, just wait until onResume to set the state back to Workspace
1936 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001937 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001938 } else {
1939 mOnResumeState = State.WORKSPACE;
1940 }
1941
1942 final View v = getWindow().peekDecorView();
1943 if (v != null && v.getWindowToken() != null) {
1944 InputMethodManager imm = (InputMethodManager)getSystemService(
1945 INPUT_METHOD_SERVICE);
1946 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1947 }
1948
Winson Chungb745afb2015-03-02 11:51:23 -08001949 // Reset the apps view
1950 if (!alreadyOnHome && mAppsView != null) {
1951 mAppsView.scrollToTop();
1952 }
1953
Hyunyoung Song3f471442015-04-08 19:01:34 -07001954 // Reset the widgets view
1955 if (!alreadyOnHome && mWidgetsView != null) {
1956 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07001957 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001958
Adam Cohen9211d422014-10-07 18:14:53 -07001959 if (mLauncherCallbacks != null) {
1960 mLauncherCallbacks.onHomeIntent();
1961 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001962 }
Adam Cohened307df2013-10-02 09:37:31 -07001963
Michael Jurka447bf842013-05-15 14:52:15 +02001964 if (DEBUG_RESUME_TIME) {
1965 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1966 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967
Adam Cohen9211d422014-10-07 18:14:53 -07001968 if (mLauncherCallbacks != null) {
1969 mLauncherCallbacks.onNewIntent(intent);
1970 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001971 }
1972
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001974 public void onRestoreInstanceState(Bundle state) {
1975 super.onRestoreInstanceState(state);
1976 for (int page: mSynchronouslyBoundPages) {
1977 mWorkspace.restoreInstanceStateForChild(page);
1978 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 }
1980
1981 @Override
1982 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001983 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001984 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1985 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001986 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001987 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001988
Michael Jurka883f55b2010-10-21 15:47:14 -07001989 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001990 // We close any open folder since it will not be re-opened, and we need to make sure
1991 // this state is reflected.
1992 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001993
Adam Cohendcd297f2013-06-18 13:13:40 -07001994 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001995 mWaitingForResult) {
1996 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001997 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001998 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1999 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002000 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2001 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2002 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002003 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002004 }
2005
Hyunyoung Song3f471442015-04-08 19:01:34 -07002006 // Save the current widgets tray?
2007 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002008 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002009
2010 if (mLauncherCallbacks != null) {
2011 mLauncherCallbacks.onSaveInstanceState(outState);
2012 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 }
2014
2015 @Override
2016 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002017 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002018
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002019 if (mPreferenceObserverRegistered) {
2020 getSharedPreferences(LauncherFiles.ROTATION_PREF_FILE,
2021 Context.MODE_MULTI_PROCESS).unregisterOnSharedPreferenceChangeListener(
2022 mSettingsObserver);
2023 mPreferenceObserverRegistered = false;
2024 }
2025
Winson Chunge7a03942011-08-05 15:05:12 -07002026 // Remove all pending runnables
2027 mHandler.removeMessages(ADVANCE_MSG);
2028 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002029 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002030
Winson Chungcd2b0142011-06-08 16:02:26 -07002031 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002032 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002033
2034 // It's possible to receive onDestroy after a new Launcher activity has
2035 // been created. In this case, don't interfere with the new Launcher.
2036 if (mModel.isCurrentCallbacks(this)) {
2037 mModel.stopLoader();
2038 app.setLauncher(null);
2039 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002040
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002041 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002042 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002044 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002046 mAppWidgetHost = null;
2047
2048 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049
2050 TextKeyListener.getInstance().release();
2051
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002052 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002053 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002054
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002055 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002056 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002057 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002058 mWorkspace = null;
2059 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002060
Michael Jurka2ecf9952012-06-18 12:52:28 -07002061 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002062
2063 if (mLauncherCallbacks != null) {
2064 mLauncherCallbacks.onDestroy();
2065 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002066 }
2067
Adam Cohena9cf38f2011-05-02 15:36:58 -07002068 public DragController getDragController() {
2069 return mDragController;
2070 }
2071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 @Override
2073 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002074 onStartForResult(requestCode);
2075 super.startActivityForResult(intent, requestCode);
2076 }
2077
2078 @Override
2079 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2080 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2081 onStartForResult(requestCode);
2082 try {
2083 super.startIntentSenderForResult(intent, requestCode,
2084 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2085 } catch (IntentSender.SendIntentException e) {
2086 throw new ActivityNotFoundException();
2087 }
2088 }
2089
2090 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002091 if (requestCode >= 0) {
2092 setWaitingForResult(true);
2093 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094 }
2095
Winson Chung70d72102011-08-12 11:24:35 -07002096 /**
2097 * Indicates that we want global search for this activity by setting the globalSearch
2098 * argument for {@link #startSearch} to true.
2099 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002101 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002102 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002103
Karl Rosaen138a0412009-04-23 19:00:21 -07002104 if (initialQuery == null) {
2105 // Use any text typed in the launcher as the initial query
2106 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002107 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002108 if (appSearchData == null) {
2109 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002110 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002111 }
Winson Chungadf0c182012-08-23 14:59:07 -07002112 Rect sourceBounds = new Rect();
2113 if (mSearchDropTargetBar != null) {
2114 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2115 }
Romain Guycbb89e42009-06-08 15:52:54 -07002116
Ian Parkinson047036e2014-09-01 15:40:53 +01002117 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002118 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002119 if (clearTextImmediately) {
2120 clearTypedText();
2121 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002122
2123 // We need to show the workspace after starting the search
2124 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002125 }
2126
Ian Parkinson047036e2014-09-01 15:40:53 +01002127 /**
2128 * Start a text search.
2129 *
2130 * @return {@code true} if the search will start immediately, so any further keypresses
2131 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2132 * to buffer keypresses.
2133 */
2134 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002135 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002136 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2137 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2138 sourceBounds);
2139 }
2140
Michael Jurkaa33411c2012-06-14 16:18:21 -07002141 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002142 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002143 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002144 }
2145
2146 /**
2147 * Starts the global search activity. This code is a copied from SearchManager
2148 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002149 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002150 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002151 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002152 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2153 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2154 if (globalSearchActivity == null) {
2155 Log.w(TAG, "No global search activity found.");
2156 return;
2157 }
2158 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2159 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2160 intent.setComponent(globalSearchActivity);
2161 // Make sure that we have a Bundle to put source in
2162 if (appSearchData == null) {
2163 appSearchData = new Bundle();
2164 } else {
2165 appSearchData = new Bundle(appSearchData);
2166 }
Adam Cohen9211d422014-10-07 18:14:53 -07002167 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002168 if (!appSearchData.containsKey("source")) {
2169 appSearchData.putString("source", getPackageName());
2170 }
2171 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2172 if (!TextUtils.isEmpty(initialQuery)) {
2173 intent.putExtra(SearchManager.QUERY, initialQuery);
2174 }
2175 if (selectInitialQuery) {
2176 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2177 }
2178 intent.setSourceBounds(sourceBounds);
2179 try {
2180 startActivity(intent);
2181 } catch (ActivityNotFoundException ex) {
2182 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2183 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002184 }
2185
Yura4f93ec62014-02-04 14:15:21 +00002186 public boolean isOnCustomContent() {
2187 return mWorkspace.isOnOrMovingToCustomContent();
2188 }
2189
Winson Chung70d72102011-08-12 11:24:35 -07002190 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002191 public boolean onPrepareOptionsMenu(Menu menu) {
2192 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002193 if (!isOnCustomContent()) {
2194 // Close any open folders
2195 closeFolder();
2196 // Stop resizing any widgets
2197 mWorkspace.exitWidgetResizeMode();
2198 if (!mWorkspace.isInOverviewMode()) {
2199 // Show the overview mode
2200 showOverviewMode(true);
2201 } else {
2202 showWorkspace(true);
2203 }
Winson Chunge0298742014-01-17 12:03:00 -08002204 }
Adam Cohen9211d422014-10-07 18:14:53 -07002205 if (mLauncherCallbacks != null) {
2206 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2207 }
2208
Michael Jurkab94f3f82013-09-11 18:08:54 +02002209 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002210 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002211
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002212 @Override
2213 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002214 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002215 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002216 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002217 }
2218
Joe Onorato9c1289c2009-08-17 11:03:03 -04002219 public boolean isWorkspaceLocked() {
2220 return mWorkspaceLoading || mWaitingForResult;
2221 }
2222
Adam Cohen517a7f52014-03-01 12:12:59 -08002223 public boolean isWorkspaceLoading() {
2224 return mWorkspaceLoading;
2225 }
2226
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002227 private void setWorkspaceLoading(boolean value) {
2228 boolean isLocked = isWorkspaceLocked();
2229 mWorkspaceLoading = value;
2230 if (isLocked != isWorkspaceLocked()) {
2231 onWorkspaceLockedChanged();
2232 }
2233 }
2234
2235 private void setWaitingForResult(boolean value) {
2236 boolean isLocked = isWorkspaceLocked();
2237 mWaitingForResult = value;
2238 if (isLocked != isWorkspaceLocked()) {
2239 onWorkspaceLockedChanged();
2240 }
2241 }
2242
Adam Cohen9211d422014-10-07 18:14:53 -07002243 protected void onWorkspaceLockedChanged() {
2244 if (mLauncherCallbacks != null) {
2245 mLauncherCallbacks.onWorkspaceLockedChanged();
2246 }
2247 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002248
Michael Jurka0280c3b2010-09-17 15:00:07 -07002249 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002250 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002251 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002252 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2253 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002254 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002255 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002256 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002257
Sunny Goyale6b63a32015-01-16 16:14:29 -08002258 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002259 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002260 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2261 }
2262
Sunny Goyale6b63a32015-01-16 16:14:29 -08002263 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002264 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2265 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002266 if (appWidgetInfo.configure != null) {
2267 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002268 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002269
Bjorn Bringert7984c942009-12-09 15:38:25 +00002270 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002271 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2272 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2273
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002274 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002275 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002276 Runnable onComplete = new Runnable() {
2277 @Override
2278 public void run() {
2279 // Exit spring loaded mode if necessary after adding the widget
2280 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2281 null);
2282 }
2283 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002284 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002285 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002286 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002287 }
2288 }
Romain Guycbb89e42009-06-08 15:52:54 -07002289
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002290 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002291 // Close any folders that may be open.
2292 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002293 mWorkspace.moveToCustomContentScreen(animate);
2294 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002295
2296 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2297 int[] cell, int spanX, int spanY) {
2298 switch (info.itemType) {
2299 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2300 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2301 int span[] = new int[2];
2302 span[0] = spanX;
2303 span[1] = spanY;
2304 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2305 container, screenId, cell, span);
2306 break;
2307 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2308 processShortcutFromDrop(info.componentName, container, screenId, cell);
2309 break;
2310 default:
2311 throw new IllegalStateException("Unknown item type: " + info.itemType);
2312 }
2313 }
2314
Adam Cohenfbba09b2011-07-18 21:43:05 -07002315 /**
2316 * Process a shortcut drop.
2317 *
2318 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002319 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002320 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002321 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002322 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2323 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002324 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002325 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002326 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002327 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002328
2329 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002330 mPendingAddInfo.cellX = cell[0];
2331 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002332 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002333
2334 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2335 createShortcutIntent.setComponent(componentName);
2336 processShortcut(createShortcutIntent);
2337 }
2338
Adam Cohenfbba09b2011-07-18 21:43:05 -07002339 /**
2340 * Process a widget drop.
2341 *
2342 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002343 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002344 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002345 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002346 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2347 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002348 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002349 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002350 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002351 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002352 mPendingAddInfo.minSpanX = info.minSpanX;
2353 mPendingAddInfo.minSpanY = info.minSpanY;
2354
Adam Cohenfbba09b2011-07-18 21:43:05 -07002355 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002356 mPendingAddInfo.cellX = cell[0];
2357 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002358 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002359 if (span != null) {
2360 mPendingAddInfo.spanX = span[0];
2361 mPendingAddInfo.spanY = span[1];
2362 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002363
Adam Cohened66b2b2012-01-23 17:28:51 -08002364 AppWidgetHostView hostView = info.boundWidget;
2365 int appWidgetId;
2366 if (hostView != null) {
2367 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002368 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
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
Joe Onoratodeb98af2010-02-19 14:59:39 -08002393 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002394 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002395 }
2396
Winson Chung24ab2f12010-09-16 14:10:47 -07002397 void processWallpaper(Intent intent) {
2398 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2399 }
2400
Adam Cohendcd297f2013-06-18 13:13:40 -07002401 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002402 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002403 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002404 folderInfo.title = getText(R.string.folder_name);
2405
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002407 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2408 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002409 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002410
2411 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002412 FolderIcon newFolder =
2413 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002414 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002415 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002416 // Force measure the new folder icon
2417 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2418 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002419 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002420 }
Romain Guycbb89e42009-06-08 15:52:54 -07002421
Joe Onorato9c1289c2009-08-17 11:03:03 -04002422 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002423 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002424 }
2425
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002426 /**
2427 * Registers various content observers. The current implementation registers
2428 * only a favorites observer to keep track of the favorites applications.
2429 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002430 private void registerContentObservers() {
2431 ContentResolver resolver = getContentResolver();
2432 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2433 true, mWidgetObserver);
2434 }
2435
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002436 @Override
2437 public boolean dispatchKeyEvent(KeyEvent event) {
2438 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2439 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002440 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002441 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002442 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002443 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002444 dumpState();
2445 return true;
2446 }
2447 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002448 }
2449 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2450 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002451 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002452 return true;
2453 }
2454 }
2455
2456 return super.dispatchKeyEvent(event);
2457 }
2458
Joe Onorato88ec0992009-11-19 13:16:06 -08002459 @Override
2460 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002461 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2462 return;
2463 }
2464
Winson Chung3d9490a2015-03-24 17:38:42 -07002465 LauncherAccessibilityDelegate delegate =
2466 LauncherAppState.getInstance().getAccessibilityDelegate();
2467 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002468 return;
2469 }
2470
Winson Chungb745afb2015-03-02 11:51:23 -08002471 if (isAppsViewVisible()) {
2472 showWorkspace(true);
2473 } else if (isWidgetsViewVisible()) {
2474 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002475 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002476 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002477 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002478 Folder openFolder = mWorkspace.getOpenFolder();
2479 if (openFolder.isEditingName()) {
2480 openFolder.dismissEditingName();
2481 } else {
2482 closeFolder();
2483 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002484 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002485 mWorkspace.exitWidgetResizeMode();
2486
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002487 // Back button is a no-op here, but give at least some feedback for the button press
2488 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002489 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002490 }
2491
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002492 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002493 * Re-listen when widgets are reset.
2494 */
Adam Cohen091440a2015-03-18 14:16:05 -07002495 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002496 if (mAppWidgetHost != null) {
2497 mAppWidgetHost.startListening();
2498 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002499 }
2500
2501 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002502 * Launches the intent referred by the clicked shortcut.
2503 *
2504 * @param v The view representing the clicked shortcut.
2505 */
2506 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002507 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2508 // view has detached (it's possible for this to happen if the view is removed mid touch).
2509 if (v.getWindowToken() == null) {
2510 return;
2511 }
2512
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002513 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002514 return;
2515 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002516
Adam Cohen1697b792013-09-17 19:08:21 -07002517 if (v instanceof Workspace) {
2518 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002519 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002520 }
2521 return;
2522 }
2523
2524 if (v instanceof CellLayout) {
2525 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002526 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002527 }
2528 }
2529
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002530 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002531 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002532 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002533 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002534 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002535 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002536 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002537 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002538 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002539 } else if (tag instanceof AppInfo) {
2540 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002541 } else if (tag instanceof LauncherAppWidgetInfo) {
2542 if (v instanceof PendingAppWidgetHostView) {
2543 onClickPendingWidget((PendingAppWidgetHostView) v);
2544 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 }
2546 }
2547
Sunny Goyal70660032015-05-14 00:07:08 -07002548 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002549 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002550 return false;
2551 }
2552
Michael Jurkaaf442092010-06-10 17:01:57 -07002553 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002554 * Event handler for the app widget view which has not fully restored.
2555 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002556 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002557 if (mIsSafeModeEnabled) {
2558 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2559 return;
2560 }
2561
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002562 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002563 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002564 int widgetId = info.appWidgetId;
2565 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2566 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002567 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2568 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002569 mPendingAddInfo.copyFrom(info);
2570 mPendingAddWidgetId = widgetId;
2571
Sunny Goyalffe83f12014-08-14 17:39:34 -07002572 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2573 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002574 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002575 } else if (info.installProgress < 0) {
2576 // The install has not been queued
2577 final String packageName = info.providerName.getPackageName();
2578 showBrokenAppInstallDialog(packageName,
2579 new DialogInterface.OnClickListener() {
2580 public void onClick(DialogInterface dialog, int id) {
2581 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2582 }
2583 });
2584 } else {
2585 // Download has started.
2586 final String packageName = info.providerName.getPackageName();
2587 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002588 }
2589 }
2590
2591 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002592 * Event handler for the "grid" button that appears on the home screen, which
2593 * enters all apps mode.
2594 *
2595 * @param v The view that was clicked.
2596 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002597 protected void onClickAllAppsButton(View v) {
2598 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002599 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002600 showWorkspace(true);
2601 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002602 // Try and refresh the set of predicted apps before we enter launcher
2603 showAppsView(true /* animated */, false /* resetListToTop */,
2604 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002605 }
2606 }
2607
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002608 private void showBrokenAppInstallDialog(final String packageName,
2609 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002610 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002611 .setTitle(R.string.abandoned_promises_title)
2612 .setMessage(R.string.abandoned_promise_explanation)
2613 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2614 .setNeutralButton(R.string.abandoned_clean_this,
2615 new DialogInterface.OnClickListener() {
2616 public void onClick(DialogInterface dialog, int id) {
2617 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2618 mWorkspace.removeAbandonedPromise(packageName, user);
2619 }
2620 })
2621 .create().show();
2622 return;
2623 }
2624
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002625 /**
2626 * Event handler for an app shortcut click.
2627 *
2628 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2629 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002630 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002631 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2632 Object tag = v.getTag();
2633 if (!(tag instanceof ShortcutInfo)) {
2634 throw new IllegalArgumentException("Input must be a Shortcut");
2635 }
2636
2637 // Open shortcut
2638 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002639
2640 if (shortcut.isDisabled != 0) {
2641 int error = R.string.activity_not_available;
2642 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2643 error = R.string.safemode_shortcut_error;
2644 }
2645 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2646 return;
2647 }
2648
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002649 final Intent intent = shortcut.intent;
2650
2651 // Check for special shortcuts
2652 if (intent.getComponent() != null) {
2653 final String shortcutClass = intent.getComponent().getClassName();
2654
2655 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2656 MemoryDumpActivity.startDump(this);
2657 return;
2658 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2659 toggleShowWeightWatcher();
2660 return;
2661 }
2662 }
2663
Chris Wren40c5ed32014-06-24 18:24:23 -04002664 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002665 if ((v instanceof BubbleTextView)
2666 && shortcut.isPromise()
2667 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002668 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002669 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002670 new DialogInterface.OnClickListener() {
2671 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002672 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002673 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002674 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002675 return;
2676 }
2677
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002678 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002679 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002680
2681 if (mLauncherCallbacks != null) {
2682 mLauncherCallbacks.onClickAppShortcut(v);
2683 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002684 }
2685
Adam Cohen091440a2015-03-18 14:16:05 -07002686 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002687 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002688 final ShortcutInfo shortcut;
2689 final Intent intent;
2690 if (tag instanceof ShortcutInfo) {
2691 shortcut = (ShortcutInfo) tag;
2692 intent = shortcut.intent;
2693 int[] pos = new int[2];
2694 v.getLocationOnScreen(pos);
2695 intent.setSourceBounds(new Rect(pos[0], pos[1],
2696 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002697
Sunny Goyal508da152014-08-14 10:53:27 -07002698 } else if (tag instanceof AppInfo) {
2699 shortcut = null;
2700 intent = ((AppInfo) tag).intent;
2701 } else {
2702 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2703 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002704
2705 boolean success = startActivitySafely(v, intent, tag);
Winson Chung8f1eff72015-05-28 17:33:40 -07002706 mStats.recordLaunch(v, intent, shortcut);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002707
2708 if (success && v instanceof BubbleTextView) {
2709 mWaitingForResume = (BubbleTextView) v;
2710 mWaitingForResume.setStayPressed(true);
2711 }
2712 }
2713
2714 /**
2715 * Event handler for a folder icon click.
2716 *
2717 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2718 */
2719 protected void onClickFolderIcon(View v) {
2720 if (LOGD) Log.d(TAG, "onClickFolder");
2721 if (!(v instanceof FolderIcon)){
2722 throw new IllegalArgumentException("Input must be a FolderIcon");
2723 }
2724
2725 FolderIcon folderIcon = (FolderIcon) v;
2726 final FolderInfo info = folderIcon.getFolderInfo();
2727 Folder openFolder = mWorkspace.getFolderForTag(info);
2728
2729 // If the folder info reports that the associated folder is open, then verify that
2730 // it is actually opened. There have been a few instances where this gets out of sync.
2731 if (info.opened && openFolder == null) {
2732 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2733 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2734 info.opened = false;
2735 }
2736
2737 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2738 // Close any open folder
2739 closeFolder();
2740 // Open the requested folder
2741 openFolder(folderIcon);
2742 } else {
2743 // Find the open folder...
2744 int folderScreen;
2745 if (openFolder != null) {
2746 folderScreen = mWorkspace.getPageForView(openFolder);
2747 // .. and close it
2748 closeFolder(openFolder);
2749 if (folderScreen != mWorkspace.getCurrentPage()) {
2750 // Close any folder open on the current screen
2751 closeFolder();
2752 // Pull the folder onto this screen
2753 openFolder(folderIcon);
2754 }
2755 }
2756 }
Adam Cohen9211d422014-10-07 18:14:53 -07002757
2758 if (mLauncherCallbacks != null) {
2759 mLauncherCallbacks.onClickFolderIcon(v);
2760 }
Michael Jurka5130e402011-10-13 04:55:35 -07002761 }
2762
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002763 /**
2764 * Event handler for the (Add) Widgets button that appears after a long press
2765 * on the home screen.
2766 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002767 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002768 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002769 if (mIsSafeModeEnabled) {
2770 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2771 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002772 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002773 if (mLauncherCallbacks != null) {
2774 mLauncherCallbacks.onClickAddWidgetButton(view);
2775 }
Adam Cohen9211d422014-10-07 18:14:53 -07002776 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002777 }
2778
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002779 /**
2780 * Event handler for the wallpaper picker button that appears after a long press
2781 * on the home screen.
2782 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002783 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002784 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002785 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2786 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002787
2788 if (mLauncherCallbacks != null) {
2789 mLauncherCallbacks.onClickWallpaperPicker(v);
2790 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002791 }
2792
2793 /**
2794 * Event handler for a click on the settings button that appears after a long press
2795 * on the home screen.
2796 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002797 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002798 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002799 if (mLauncherCallbacks != null) {
2800 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002801 } else {
2802 showSettingsActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002803 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002804 }
2805
Michael Jurka5130e402011-10-13 04:55:35 -07002806 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002807 // Provide the same haptic feedback that the system offers for virtual keys.
2808 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002809 }
2810
Adam Cohen61f560d2013-09-30 15:58:20 -07002811 public void performHapticFeedbackOnTouchDown(View v) {
2812 // Provide the same haptic feedback that the system offers for virtual keys.
2813 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2814 }
2815
2816 public View.OnTouchListener getHapticFeedbackTouchListener() {
2817 if (mHapticFeedbackTouchListener == null) {
2818 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002819 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002820 @Override
2821 public boolean onTouch(View v, MotionEvent event) {
2822 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2823 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2824 }
2825 return false;
2826 }
2827 };
2828 }
2829 return mHapticFeedbackTouchListener;
2830 }
2831
Adam Cohen9211d422014-10-07 18:14:53 -07002832 public void onDragStarted(View view) {
2833 if (isOnCustomContent()) {
2834 // Custom content screen doesn't participate in drag and drop. If on custom
2835 // content screen, move to default.
2836 moveWorkspaceToDefaultScreen();
2837 }
2838
2839 if (mLauncherCallbacks != null) {
2840 mLauncherCallbacks.onDragStarted(view);
2841 }
2842 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002843
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002844 /**
2845 * Called when the user stops interacting with the launcher.
2846 * This implies that the user is now on the homescreen and is not doing housekeeping.
2847 */
Adam Cohen9211d422014-10-07 18:14:53 -07002848 protected void onInteractionEnd() {
2849 if (mLauncherCallbacks != null) {
2850 mLauncherCallbacks.onInteractionEnd();
2851 }
2852 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002853
2854 /**
2855 * Called when the user starts interacting with the launcher.
2856 * The possible interactions are:
2857 * - open all apps
2858 * - reorder an app shortcut, or a widget
2859 * - open the overview mode.
2860 * This is a good time to stop doing things that only make sense
2861 * when the user is on the homescreen and not doing housekeeping.
2862 */
Adam Cohen9211d422014-10-07 18:14:53 -07002863 protected void onInteractionBegin() {
2864 if (mLauncherCallbacks != null) {
2865 mLauncherCallbacks.onInteractionBegin();
2866 }
2867 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002868
Winson Chungcd99cd32015-04-29 11:03:24 -07002869 /** Updates the interaction state. */
2870 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002871 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002872 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002873 boolean fromStateWithOverlay;
2874 boolean toStateWithOverlay;
2875 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2876 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2877 toStateWithOverlay = toState != Workspace.State.NORMAL;
2878 } else {
2879 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2880 fromState != Workspace.State.NORMAL_HIDDEN;
2881 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2882 toState != Workspace.State.NORMAL_HIDDEN;
2883 }
2884 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002885 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002886 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002887 onInteractionEnd();
2888 }
2889 }
2890
Kenny Guyf07af7b2014-07-31 11:39:16 +01002891 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002892 try {
2893 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002894 launcherApps.showAppDetailsForProfile(componentName, user);
2895 } catch (SecurityException e) {
2896 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2897 Log.e(TAG, "Launcher does not have permission to launch settings");
2898 } catch (ActivityNotFoundException e) {
2899 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2900 Log.e(TAG, "Unable to launch settings");
2901 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002902 }
2903
Michael Jurka1e2f4652013-07-08 18:03:46 -07002904 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002905 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2906 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002907 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002908 // System applications cannot be installed. For now, show a toast explaining that.
2909 // We may give them the option of disabling apps this way.
2910 int messageId = R.string.uninstall_system_app_text;
2911 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002912 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002913 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002914 String packageName = componentName.getPackageName();
2915 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002916 Intent intent = new Intent(
2917 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002918 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2919 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002920 if (user != null) {
2921 user.addToIntent(intent, Intent.EXTRA_USER);
2922 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002923 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002924 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002925 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002926 }
2927
Winson Chung8f1eff72015-05-28 17:33:40 -07002928 private boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002929 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002930 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002931 // Only launch using the new animation if the shortcut has not opted out (this is a
2932 // private contract between launcher and may be ignored in the future).
2933 boolean useLaunchAnimation = (v != null) &&
2934 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002935 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2936 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002937
Kenny Guy1317e2d2014-05-08 18:52:50 +01002938 UserHandleCompat user = null;
2939 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2940 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2941 user = userManager.getUserForSerialNumber(serialNumber);
2942 }
2943
2944 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002945 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08002946 ActivityOptions opts = null;
2947 if (sClipRevealMethod != null) {
2948 // TODO: call method directly when Launcher3 can depend on M APIs
2949 int left = 0, top = 0;
2950 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
2951 if (v instanceof TextView) {
2952 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08002953 Drawable icon = Workspace.getTextViewIcon((TextView) v);
2954 if (icon != null) {
2955 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08002956 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08002957 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08002958 width = bounds.width();
2959 height = bounds.height();
2960 }
2961 }
2962 try {
2963 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
2964 left, top, width, height);
2965 } catch (IllegalAccessException e) {
2966 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2967 sClipRevealMethod = null;
2968 } catch (InvocationTargetException e) {
2969 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
2970 sClipRevealMethod = null;
2971 }
2972 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002973 if (opts == null && !Utilities.isLmpOrAbove()) {
2974 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08002975 v.getMeasuredWidth(), v.getMeasuredHeight());
2976 }
Adam Cohen2e52c902015-04-06 13:11:28 -07002977 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07002978 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002979
2980 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2981 // Could be launching some bookkeeping activity
2982 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002983 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002984 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002985 launcherApps.startActivityForProfile(intent.getComponent(), user,
2986 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002987 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002988 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002989 } catch (SecurityException e) {
2990 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002991 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002992 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002993 "or use the exported attribute for this activity. "
2994 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002995 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002996 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002997 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002998
Winson Chung8f1eff72015-05-28 17:33:40 -07002999 private boolean startActivitySafely(View v, Intent intent, Object tag) {
Michael Jurka86a720e2012-05-09 11:23:23 -07003000 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003001 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3002 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3003 return false;
3004 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003005 try {
3006 success = startActivity(v, intent, tag);
3007 } catch (ActivityNotFoundException e) {
3008 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3009 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3010 }
3011 return success;
3012 }
3013
Adam Cohen268c4752012-06-06 17:47:33 -07003014 /**
3015 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3016 * in the DragLayer in the exact absolute location of the original FolderIcon.
3017 */
3018 private void copyFolderIconToImage(FolderIcon fi) {
3019 final int width = fi.getMeasuredWidth();
3020 final int height = fi.getMeasuredHeight();
3021
3022 // Lazy load ImageView, Bitmap and Canvas
3023 if (mFolderIconImageView == null) {
3024 mFolderIconImageView = new ImageView(this);
3025 }
3026 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3027 mFolderIconBitmap.getHeight() != height) {
3028 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3029 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3030 }
3031
3032 DragLayer.LayoutParams lp;
3033 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3034 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3035 } else {
3036 lp = new DragLayer.LayoutParams(width, height);
3037 }
3038
Adam Cohen307fe232012-08-16 17:55:58 -07003039 // The layout from which the folder is being opened may be scaled, adjust the starting
3040 // view size by this scale factor.
3041 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003042 lp.customPosition = true;
3043 lp.x = mRectForFolderAnimation.left;
3044 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003045 lp.width = (int) (scale * width);
3046 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003047
3048 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3049 fi.draw(mFolderIconCanvas);
3050 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003051 if (fi.getFolder() != null) {
3052 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3053 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003054 }
Adam Cohen268c4752012-06-06 17:47:33 -07003055 // Just in case this image view is still in the drag layer from a previous animation,
3056 // we remove it and re-add it.
3057 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3058 mDragLayer.removeView(mFolderIconImageView);
3059 }
3060 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003061 if (fi.getFolder() != null) {
3062 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003063 }
Adam Cohen268c4752012-06-06 17:47:33 -07003064 }
3065
Adam Cohen2801caf2011-05-13 20:57:39 -07003066 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003067 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003068 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3069 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3070 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3071
Adam Cohenc51934b2011-07-26 21:07:43 -07003072 FolderInfo info = (FolderInfo) fi.getTag();
3073 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3074 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003075 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3076 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003077 }
3078
Adam Cohen268c4752012-06-06 17:47:33 -07003079 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3080 copyFolderIconToImage(fi);
3081 fi.setVisibility(View.INVISIBLE);
3082
Michael Jurka2ecf9952012-06-18 12:52:28 -07003083 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003084 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003085 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003086 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3087 }
3088 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003089 oa.start();
3090 }
3091
Adam Cohen268c4752012-06-06 17:47:33 -07003092 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003093 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003094 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3095 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3096 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3097
Adam Cohen268c4752012-06-06 17:47:33 -07003098 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003099
Adam Cohen268c4752012-06-06 17:47:33 -07003100 // We remove and re-draw the FolderIcon in-case it has changed
3101 mDragLayer.removeView(mFolderIconImageView);
3102 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003103 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003104 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003105 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003106 oa.addListener(new AnimatorListenerAdapter() {
3107 @Override
3108 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003109 if (cl != null) {
3110 cl.clearFolderLeaveBehind();
3111 // Remove the ImageView copy of the FolderIcon and make the original visible.
3112 mDragLayer.removeView(mFolderIconImageView);
3113 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003114 }
3115 }
3116 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003117 oa.start();
3118 }
3119
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003120 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003121 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003122 * is animated relative to the specified View. If the View is null, no animation
3123 * is played.
3124 *
3125 * @param folderInfo The FolderInfo describing the folder to open.
3126 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003127 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003128 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003129 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3130 if (openFolder != null && openFolder != folder) {
3131 // Close any open folder before opening a folder.
3132 closeFolder();
3133 }
3134
Adam Cohena9cf38f2011-05-02 15:36:58 -07003135 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003136
Adam Cohena9cf38f2011-05-02 15:36:58 -07003137 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003138
Sunny Goyalf4066152015-04-15 09:42:19 -07003139 // While the folder is open, the position of the icon cannot change.
3140 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3141
Adam Cohen4554ee12011-08-03 16:13:21 -07003142 // Just verify that the folder hasn't already been added to the DragLayer.
3143 // There was a one-off crash where the folder had a parent already.
3144 if (folder.getParent() == null) {
3145 mDragLayer.addView(folder);
3146 mDragController.addDropTarget((DropTarget) folder);
3147 } else {
3148 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3149 folder.getParent() + ").");
3150 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003151 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003152 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003153
3154 // Notify the accessibility manager that this folder "window" has appeared and occluded
3155 // the workspace items
3156 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3157 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003158 }
3159
3160 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003161 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003162 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003163 if (folder.isEditingName()) {
3164 folder.dismissEditingName();
3165 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003166 closeFolder(folder);
3167 }
3168 }
3169
Sunny Goyal83a8f042015-05-19 12:52:12 -07003170 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003171 folder.getInfo().opened = false;
3172
3173 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3174 if (parent != null) {
3175 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3176 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003177 if (fi != null) {
3178 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3179 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003180 }
3181 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003182
3183 // Notify the accessibility manager that this folder "window" has disappeard and no
3184 // longer occludeds the workspace items
3185 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003186 }
3187
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003188 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003189 if (!isDraggingEnabled()) return false;
3190 if (isWorkspaceLocked()) return false;
3191 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003192
Adam Cohen1697b792013-09-17 19:08:21 -07003193 if (v instanceof Workspace) {
3194 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003195 if (!mWorkspace.isTouchActive()) {
3196 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003197 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3198 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3199 return true;
3200 } else {
3201 return false;
3202 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003203 } else {
3204 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003205 }
Adam Cohen1697b792013-09-17 19:08:21 -07003206 }
3207
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003208 CellLayout.CellInfo longClickCellInfo = null;
3209 View itemUnderLongClick = null;
3210 if (v.getTag() instanceof ItemInfo) {
3211 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003212 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003213 itemUnderLongClick = longClickCellInfo.cell;
3214 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003215 }
3216
Winson Chung3d503fb2011-07-13 17:25:49 -07003217 // The hotseat touch handling does not go through Workspace, and we always allow long press
3218 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003219 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003220 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003221 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003222 // User long pressed on empty space
3223 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3224 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003225 if (mWorkspace.isInOverviewMode()) {
3226 mWorkspace.startReordering(v);
3227 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003228 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003229 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003230 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003231 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3232 mHotseat.getOrderInHotseat(
3233 longClickCellInfo.cellX,
3234 longClickCellInfo.cellY));
3235 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003236 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003237 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003238 }
3239 }
3240 }
3241 return true;
3242 }
3243
Winson Chung3d503fb2011-07-13 17:25:49 -07003244 boolean isHotseatLayout(View layout) {
3245 return mHotseat != null && layout != null &&
3246 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3247 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003248
Winson Chung3d503fb2011-07-13 17:25:49 -07003249 /**
3250 * Returns the CellLayout of the specified container at the specified screen.
3251 */
Sunny Goyal92820592015-03-02 11:31:35 -08003252 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003253 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3254 if (mHotseat != null) {
3255 return mHotseat.getLayout();
3256 } else {
3257 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003258 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003259 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003260 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003261 }
3262 }
3263
Winson Chungb745afb2015-03-02 11:51:23 -08003264 /**
3265 * For overridden classes.
3266 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003267 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003268 return isAppsViewVisible();
3269 }
3270
3271 public boolean isAppsViewVisible() {
3272 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3273 }
3274
3275 public boolean isWidgetsViewVisible() {
3276 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003277 }
3278
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003279 private void setWorkspaceBackground(int background) {
3280 switch (background) {
3281 case WORKSPACE_BACKGROUND_TRANSPARENT:
3282 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3283 break;
3284 case WORKSPACE_BACKGROUND_BLACK:
3285 getWindow().setBackgroundDrawable(null);
3286 break;
3287 default:
3288 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3289 }
Craig Mautner360310b2012-10-26 15:13:08 -07003290 }
3291
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003292 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003293 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3294 int curflags = getWindow().getAttributes().flags
3295 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3296 if (wpflags != curflags) {
3297 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3298 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003299 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003300 }
3301
Michael Jurkae326f182011-11-21 14:05:46 -08003302 @Override
3303 public void onTrimMemory(int level) {
3304 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003305 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3306 // The widget preview db can result in holding onto over
3307 // 3MB of memory for caching which isn't necessary.
3308 SQLiteDatabase.releaseMemory();
3309
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003310 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003311 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003312 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003313 if (mLauncherCallbacks != null) {
3314 mLauncherCallbacks.onTrimMemory(level);
3315 }
Michael Jurkae326f182011-11-21 14:05:46 -08003316 }
3317
Winson Chungb745afb2015-03-02 11:51:23 -08003318 @Override
3319 public void onStateTransitionHideSearchBar() {
3320 // Hide the search bar
3321 if (mSearchDropTargetBar != null) {
3322 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3323 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003324 }
3325
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003326 public void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003327 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3328 true);
Adam Cohened307df2013-10-02 09:37:31 -07003329 }
3330
Sunny Goyal83a8f042015-05-19 12:52:12 -07003331 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003332 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3333 onCompleteRunnable, true);
3334 }
3335
3336 protected void showWorkspace(int snapToPage, boolean animated) {
3337 showWorkspace(snapToPage, animated, null, true);
3338 }
3339
3340 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3341 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003342 boolean changed = mState != State.WORKSPACE ||
3343 mWorkspace.getState() != Workspace.State.NORMAL;
3344 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003345 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003346 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003347 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003348 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003349
Winson Chungc7d2b602012-05-16 17:02:20 -07003350 // Show the search bar (only animate if we were showing the drop target bar in spring
3351 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003352 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003353 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003354 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003355
Michael Jurkab3e22d92011-10-31 15:58:33 -07003356 // Set focus to the AppsCustomize button
3357 if (mAllAppsButton != null) {
3358 mAllAppsButton.requestFocus();
3359 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003360 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003361
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003362 // Change the state *after* we've called all the transition code
3363 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003364
Winson Chung5fb63472011-02-02 17:03:37 -08003365 // Resume the auto-advance of widgets
3366 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003367 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003368
Winson Chung0f785722015-04-08 10:27:49 -07003369 if (changed) {
3370 // Send an accessibility event to announce the context change
3371 getWindow().getDecorView()
3372 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003373 if (notifyLauncherCallbacks) {
3374 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003375 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003376 mLauncherCallbacks.onAllAppsHidden();
3377 }
3378 }
Winson Chung0f785722015-04-08 10:27:49 -07003379 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003380 }
3381
Adam Cohened307df2013-10-02 09:37:31 -07003382 void showOverviewMode(boolean animated) {
3383 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003384 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003385 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3386 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003387 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003388 }
3389
Winson Chungb745afb2015-03-02 11:51:23 -08003390 /**
3391 * Shows the apps view.
3392 */
Winson Chung4ac30062015-05-08 17:34:17 -07003393 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003394 if (resetListToTop) {
3395 mAppsView.scrollToTop();
3396 }
Winson Chung4ac30062015-05-08 17:34:17 -07003397 if (updatePredictedApps) {
3398 tryAndUpdatePredictedApps();
3399 }
Winson Chungb745afb2015-03-02 11:51:23 -08003400 showAppsOrWidgets(animated, State.APPS);
3401 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003402
Winson Chungb745afb2015-03-02 11:51:23 -08003403 /**
3404 * Shows the widgets view.
3405 */
3406 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003407 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003408 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003409 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003410 }
Winson Chungb745afb2015-03-02 11:51:23 -08003411 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003412
3413 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003414 @Override
3415 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003416 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003417 }
3418 });
Winson Chungb745afb2015-03-02 11:51:23 -08003419 }
3420
3421 /**
3422 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003423 *
3424 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003425 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003426 // TODO: calling method should use the return value so that when {@code false} is returned
3427 // the workspace transition doesn't fall into invalid state.
3428 private boolean showAppsOrWidgets(boolean animated, State toState) {
3429 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3430 mState != State.WIDGETS_SPRING_LOADED) {
3431 return false;
3432 }
3433 if (toState != State.APPS && toState != State.WIDGETS) {
3434 return false;
3435 }
Winson Chungb745afb2015-03-02 11:51:23 -08003436
3437 if (toState == State.APPS) {
3438 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003439 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003440 mLauncherCallbacks.onAllAppsShown();
3441 }
Winson Chungb745afb2015-03-02 11:51:23 -08003442 } else {
3443 mStateTransitionAnimation.startAnimationToWidgets(animated);
3444 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003445
Michael Jurkab3e22d92011-10-31 15:58:33 -07003446 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003447 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003448
3449 // Pause the auto-advance of widgets until we are out of AllApps
3450 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003451 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003452 closeFolder();
3453
3454 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003455 getWindow().getDecorView()
3456 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003457 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003458 }
3459
Winson Chungcd99cd32015-04-29 11:03:24 -07003460 /**
3461 * Updates the workspace and interaction state on state change, and return the animation to this
3462 * new state.
3463 */
3464 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3465 boolean animated, HashMap<View, Integer> layerViews) {
3466 Workspace.State fromState = mWorkspace.getState();
3467 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3468 updateInteraction(fromState, toState);
3469 return anim;
3470 }
3471
Hyunyoung Song3f471442015-04-08 19:01:34 -07003472 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003473 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003474 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3475 mState == State.WIDGETS_SPRING_LOADED) {
3476 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003477 }
Winson Chungb745afb2015-03-02 11:51:23 -08003478
3479 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003480 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3481 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003482 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003483 }
Adam Cohen7777d962011-08-18 18:58:38 -07003484
Hyunyoung Song3f471442015-04-08 19:01:34 -07003485 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003486 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003487 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003488
Winson Chungcd99cd32015-04-29 11:03:24 -07003489 if (successfulDrop) {
3490 // We need to trigger all apps hidden to notify search to update itself before the
3491 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003492 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003493 mLauncherCallbacks.onAllAppsHidden();
3494 }
3495 }
3496
Winson Chunge7a03942011-08-05 15:05:12 -07003497 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003498 @Override
3499 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003500 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003501 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3502 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003503 // Before we show workspace, hide all apps again because
3504 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3505 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003506 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003507 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003508 } else {
3509 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003510 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003511 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003512 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003513 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003514
Michael Jurkad3ef3062010-11-23 16:23:58 -08003515 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003516 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003517 showAppsView(true /* animated */, false /* resetListToTop */,
3518 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003519 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003520 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003521 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003522 }
3523
Winson Chung4ac30062015-05-08 17:34:17 -07003524 /**
3525 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3526 * resumed.
3527 */
3528 private void tryAndUpdatePredictedApps() {
3529 if (mLauncherCallbacks != null) {
3530 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3531 if (!apps.isEmpty()) {
3532 mAppsView.setPredictedApps(apps);
3533 }
3534 }
3535 }
3536
Michael Jurkab3e22d92011-10-31 15:58:33 -07003537 void lockAllApps() {
3538 // TODO
3539 }
3540
3541 void unlockAllApps() {
3542 // TODO
3543 }
3544
Sunny Goyal594d76d2014-11-06 10:12:54 -08003545 protected void disableVoiceButtonProxy(boolean disable) {
3546 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003547 }
3548
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003549 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003550 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3551 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003552 }
3553
Adam Cohen24ce0b32014-01-14 16:18:14 -08003554 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003555 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3556 if (searchProvider == null) {
3557 return null;
3558 }
3559
3560 Bundle opts = new Bundle();
3561 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003562 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003563
3564 SharedPreferences sp = getSharedPreferences(
3565 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3566 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003567 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003568 if (!searchProvider.provider.flattenToString().equals(
3569 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003570 || (widgetInfo == null)
3571 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003572 // A valid widget is not already bound.
3573 if (widgetId > -1) {
3574 mAppWidgetHost.deleteAppWidgetId(widgetId);
3575 widgetId = -1;
3576 }
3577
3578 // Try to bind a new widget
3579 widgetId = mAppWidgetHost.allocateAppWidgetId();
3580
3581 if (!AppWidgetManagerCompat.getInstance(this)
3582 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3583 mAppWidgetHost.deleteAppWidgetId(widgetId);
3584 widgetId = -1;
3585 }
3586
3587 sp.edit()
3588 .putInt(QSB_WIDGET_ID, widgetId)
3589 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3590 .commit();
3591 }
3592
3593 if (widgetId != -1) {
3594 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3595 mQsb.updateAppWidgetOptions(opts);
3596 mQsb.setPadding(0, 0, 0, 0);
3597 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003598 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003599 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003600 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003601 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003602 }
3603
Sunny Goyal22235bc2015-04-03 09:23:43 -07003604 private void reinflateQSBIfNecessary() {
3605 if (mQsb instanceof LauncherAppWidgetHostView &&
3606 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3607 mSearchDropTargetBar.removeView(mQsb);
3608 mQsb = null;
3609 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3610 }
3611 }
3612
Michael Jurkad7c28052012-04-27 15:43:36 -07003613 @Override
3614 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003615 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003616 final List<CharSequence> text = event.getText();
3617 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003618 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003619 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003620 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003621 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003622 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003623 } else {
3624 text.add(getString(R.string.all_apps_home_button_label));
3625 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003626 return result;
3627 }
3628
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003629 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003630 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003631 */
Adam Cohen091440a2015-03-18 14:16:05 -07003632 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003633 @Override
3634 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003635 closeSystemDialogs();
3636 }
3637 }
3638
3639 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003640 * Receives notifications whenever the appwidgets are reset.
3641 */
3642 private class AppWidgetResetObserver extends ContentObserver {
3643 public AppWidgetResetObserver() {
3644 super(new Handler());
3645 }
3646
3647 @Override
3648 public void onChange(boolean selfChange) {
3649 onAppWidgetReset();
3650 }
3651 }
3652
3653 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003654 * If the activity is currently paused, signal that we need to run the passed Runnable
3655 * in onResume.
3656 *
3657 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003658 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3659 * wrong when we're not running, and if the activity comes back to what the configuration was
3660 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003661 *
3662 * Implementation of the method from LauncherModel.Callbacks.
3663 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003664 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003665 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003666 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003667 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003668 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003669 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003670 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003671 }
3672 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003673 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003674 return true;
3675 } else {
3676 return false;
3677 }
3678 }
3679
Michael Jurkac402cd92013-05-20 15:49:32 +02003680 private boolean waitUntilResume(Runnable run) {
3681 return waitUntilResume(run, false);
3682 }
3683
Michael Jurka1e2f4652013-07-08 18:03:46 -07003684 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003685 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003686 }
3687
Michael Jurka7607c2f2013-04-03 14:33:19 -07003688 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003689 * If the activity is currently paused, signal that we need to re-run the loader
3690 * in onResume.
3691 *
3692 * This needs to be called from incoming places where resources might have been loaded
3693 * while we are paused. That is becaues the Configuration might be wrong
3694 * when we're not running, and if it comes back to what it was when we
3695 * were paused, we are not restarted.
3696 *
3697 * Implementation of the method from LauncherModel.Callbacks.
3698 *
3699 * @return true if we are currently paused. The caller might be able to
3700 * skip some work in that case since we will come back again.
3701 */
3702 public boolean setLoadOnResume() {
3703 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003704 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003705 mOnResumeNeedsLoad = true;
3706 return true;
3707 } else {
3708 return false;
3709 }
3710 }
3711
3712 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003713 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003714 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003715 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003716 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003717 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003718 } else {
3719 return SCREEN_COUNT / 2;
3720 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003721 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003722
Joe Onorato9c1289c2009-08-17 11:03:03 -04003723 /**
3724 * Refreshes the shortcuts shown on the workspace.
3725 *
3726 * Implementation of the method from LauncherModel.Callbacks.
3727 */
3728 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003729 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003730
Michael Jurka7607c2f2013-04-03 14:33:19 -07003731 // If we're starting binding all over again, clear any bind calls we'd postponed in
3732 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3733 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003734 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003735
Winson Chungd64d1762013-08-20 14:37:16 -07003736 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003737 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003738 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003739
Michael Jurka05bf644e2011-11-30 20:28:53 -08003740 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003741 if (mHotseat != null) {
3742 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003743 }
3744 }
3745
Adam Cohendcd297f2013-06-18 13:13:40 -07003746 @Override
3747 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003748 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003749
Adam Cohen5084cba2013-09-03 12:01:16 -07003750 // If there are no screens, we need to have an empty screen
3751 if (orderedScreenIds.size() == 0) {
3752 mWorkspace.addExtraEmptyScreen();
3753 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003754
3755 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003756 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003757 if (hasCustomContentToLeft()) {
3758 mWorkspace.createCustomContentContainer();
3759 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003760 }
Winson Chung64359a52013-07-08 17:17:08 -07003761 }
3762
3763 @Override
3764 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003765 // Log to disk
3766 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3767 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3768 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003769 int count = orderedScreenIds.size();
3770 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003771 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003772 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003773 }
3774
Adam Cohen39a06042013-07-19 14:30:12 -07003775 private boolean shouldShowWeightWatcher() {
3776 String spKey = LauncherAppState.getSharedPreferencesKey();
3777 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07003778 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003779
3780 return show;
3781 }
3782
3783 private void toggleShowWeightWatcher() {
3784 String spKey = LauncherAppState.getSharedPreferencesKey();
3785 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3786 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3787
3788 show = !show;
3789
3790 SharedPreferences.Editor editor = sp.edit();
3791 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3792 editor.commit();
3793
3794 if (mWeightWatcher != null) {
3795 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3796 }
3797 }
3798
Winson Chungd64d1762013-08-20 14:37:16 -07003799 public void bindAppsAdded(final ArrayList<Long> newScreens,
3800 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003801 final ArrayList<ItemInfo> addAnimated,
3802 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003803 Runnable r = new Runnable() {
3804 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003805 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003806 }
3807 };
3808 if (waitUntilResume(r)) {
3809 return;
3810 }
3811
Winson Chungd64d1762013-08-20 14:37:16 -07003812 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003813 if (newScreens != null) {
3814 bindAddScreens(newScreens);
3815 }
Winson Chungd64d1762013-08-20 14:37:16 -07003816
3817 // We add the items without animation on non-visible pages, and with
3818 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003819 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003820 bindItems(addNotAnimated, 0,
3821 addNotAnimated.size(), false);
3822 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003823 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003824 bindItems(addAnimated, 0,
3825 addAnimated.size(), true);
3826 }
Winson Chungc58497e2013-09-03 17:48:37 -07003827
Winson Chung87412982013-10-03 18:34:14 -07003828 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003829 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003830
Winson Chungb745afb2015-03-02 11:51:23 -08003831 if (addedApps != null && mAppsView != null) {
3832 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003833 }
Winson Chungd64d1762013-08-20 14:37:16 -07003834 }
3835
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003836 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003837 * Bind the items start-end from the list.
3838 *
3839 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003840 */
Winson Chung64359a52013-07-08 17:17:08 -07003841 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3842 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003843 Runnable r = new Runnable() {
3844 public void run() {
3845 bindItems(shortcuts, start, end, forceAnimateIcons);
3846 }
3847 };
3848 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003849 return;
3850 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003851
Winson Chungf0c6ae02012-03-21 16:10:31 -07003852 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003853 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3854 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003855 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003856 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003857 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003858 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003859 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003860
3861 // Short circuit if we are loading dock items for a configuration which has no dock
3862 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3863 mHotseat == null) {
3864 continue;
3865 }
3866
Joe Onorato9c1289c2009-08-17 11:03:03 -04003867 switch (item.itemType) {
3868 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3869 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003870 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003871 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003872
Winson Chung64359a52013-07-08 17:17:08 -07003873 /*
3874 * TODO: FIX collision case
3875 */
Winson Chungce376632013-07-11 16:12:41 -07003876 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3877 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3878 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003879 View v = cl.getChildAt(item.cellX, item.cellY);
3880 Object tag = v.getTag();
3881 String desc = "Collision while binding workspace item: " + item
3882 + ". Collides with " + tag;
3883 if (LauncherAppState.isDogfoodBuild()) {
3884 throw (new RuntimeException(desc));
3885 } else {
3886 Log.d(TAG, desc);
3887 }
Winson Chungce376632013-07-11 16:12:41 -07003888 }
Winson Chung64359a52013-07-08 17:17:08 -07003889 }
3890
Adam Cohendcd297f2013-06-18 13:13:40 -07003891 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
3892 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07003893 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07003894 // Animate all the applications up now
3895 shortcut.setAlpha(0f);
3896 shortcut.setScaleX(0f);
3897 shortcut.setScaleY(0f);
3898 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07003899 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003900 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003901 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07003902 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07003903 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07003904 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07003905 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07003906 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
3907 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04003908 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07003909 default:
3910 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04003911 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003912 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07003913
Winson Chung997a9232013-07-24 15:33:46 -07003914 if (animateIcons) {
3915 // Animate to the correct page
3916 if (newShortcutsScreenId > -1) {
3917 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07003918 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07003919 final Runnable startBounceAnimRunnable = new Runnable() {
3920 public void run() {
3921 anim.playTogether(bounceAnims);
3922 anim.start();
3923 }
3924 };
Winson Chung997a9232013-07-24 15:33:46 -07003925 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07003926 // We post the animation slightly delayed to prevent slowdowns
3927 // when we are loading right after we return to launcher.
3928 mWorkspace.postDelayed(new Runnable() {
3929 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00003930 if (mWorkspace != null) {
3931 mWorkspace.snapToPage(newScreenIndex);
3932 mWorkspace.postDelayed(startBounceAnimRunnable,
3933 NEW_APPS_ANIMATION_DELAY);
3934 }
Winson Chung94d67682013-09-25 16:29:40 -07003935 }
3936 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07003937 } else {
3938 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07003939 }
3940 }
Winson Chung64359a52013-07-08 17:17:08 -07003941 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003942 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003943 }
3944
Joe Onorato9c1289c2009-08-17 11:03:03 -04003945 /**
3946 * Implementation of the method from LauncherModel.Callbacks.
3947 */
Sunny Goyale2df0622015-04-24 11:27:00 -07003948 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07003949 Runnable r = new Runnable() {
3950 public void run() {
3951 bindFolders(folders);
3952 }
3953 };
3954 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003955 return;
3956 }
Sunny Goyale2df0622015-04-24 11:27:00 -07003957 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003958 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003959
3960 /**
3961 * Add the views for a widget to the workspace.
3962 *
3963 * Implementation of the method from LauncherModel.Callbacks.
3964 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07003965 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07003966 Runnable r = new Runnable() {
3967 public void run() {
3968 bindAppWidget(item);
3969 }
3970 };
3971 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003972 return;
3973 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003974
Daniel Sandler843e8602010-06-07 14:59:01 -04003975 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
3976 if (DEBUG_WIDGETS) {
3977 Log.d(TAG, "bindAppWidget: " + item);
3978 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003979 final Workspace workspace = mWorkspace;
3980
Adam Cohen59400422014-03-05 18:07:04 -08003981 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00003982 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08003983
Sunny Goyal9b4b0812014-10-08 10:47:28 -07003984 if (!mIsSafeModeEnabled
3985 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
3986 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07003987 if (appWidgetInfo == null) {
3988 if (DEBUG_WIDGETS) {
3989 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
3990 + " belongs to component " + item.providerName
3991 + ", as the povider is null");
3992 }
3993 LauncherModel.deleteItemFromDatabase(this, item);
3994 return;
3995 }
3996 // Note: This assumes that the id remap broadcast is received before this step.
3997 // If that is not the case, the id remap will be ignored and user may see the
3998 // click to setup view.
Adam Cohen2e6da152015-05-06 11:42:25 -07003999 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004000 pendingInfo.spanX = item.spanX;
4001 pendingInfo.spanY = item.spanY;
4002 pendingInfo.minSpanX = item.minSpanX;
4003 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004004 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004005 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004006
Sunny Goyalff572272014-07-23 13:58:07 -07004007 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004008 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4009 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004010
4011 // TODO consider showing a permission dialog when the widget is clicked.
4012 if (!success) {
4013 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4014 if (DEBUG_WIDGETS) {
4015 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4016 + " belongs to component " + item.providerName
4017 + ", as the launcher is unable to bing a new widget id");
4018 }
4019 LauncherModel.deleteItemFromDatabase(this, item);
4020 return;
4021 }
4022
4023 item.appWidgetId = newWidgetId;
4024
4025 // If the widget has a configure activity, it is still needs to set it up, otherwise
4026 // the widget is ready to go.
4027 item.restoreStatus = (appWidgetInfo.configure == null)
4028 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4029 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4030
4031 LauncherModel.updateItemInDatabase(this, item);
4032 }
4033
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004034 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004035 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004036 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004037 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4038 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004039 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004040
Sunny Goyal651077b2014-06-30 14:15:31 -07004041 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4042 } else {
4043 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004044 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4045 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004046 view.updateIcon(mIconCache);
4047 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004048 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004049 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004050 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004051
Joe Onorato9c1289c2009-08-17 11:03:03 -04004052 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004053 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004054
Adam Cohendcd297f2013-06-18 13:13:40 -07004055 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004056 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004057 if (!item.isCustomWidget()) {
4058 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4059 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004060
Joe Onorato9c1289c2009-08-17 11:03:03 -04004061 workspace.requestLayout();
4062
Daniel Sandler843e8602010-06-07 14:59:01 -04004063 if (DEBUG_WIDGETS) {
4064 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4065 + (SystemClock.uptimeMillis()-start) + "ms");
4066 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004067 }
4068
Sunny Goyalff572272014-07-23 13:58:07 -07004069 /**
4070 * Restores a pending widget.
4071 *
4072 * @param appWidgetId The app widget id
4073 * @param cellInfo The position on screen where to create the widget.
4074 */
4075 private void completeRestoreAppWidget(final int appWidgetId) {
4076 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4077 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4078 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4079 return;
4080 }
4081
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004082 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004083 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4084
4085 mWorkspace.reinflateWidgetsIfNecessary();
4086 LauncherModel.updateItemInDatabase(this, info);
4087 }
4088
Adam Cohen1462de32012-07-24 22:34:36 -07004089 public void onPageBoundSynchronously(int page) {
4090 mSynchronouslyBoundPages.add(page);
4091 }
4092
Joe Onorato9c1289c2009-08-17 11:03:03 -04004093 /**
4094 * Callback saying that there aren't any more items to bind.
4095 *
4096 * Implementation of the method from LauncherModel.Callbacks.
4097 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004098 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004099 Runnable r = new Runnable() {
4100 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004101 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004102 }
4103 };
4104 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004105 return;
4106 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004107 if (mSavedState != null) {
4108 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004109 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004110 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004111 mSavedState = null;
4112 }
4113
Adam Cohen1462de32012-07-24 22:34:36 -07004114 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004115
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004116 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004117 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004118
4119 // If we received the result of any pending adds while the loader was running (e.g. the
4120 // widget configuration forced an orientation change), process them now.
4121 if (sPendingAddItem != null) {
4122 final long screenId = completeAdd(sPendingAddItem);
4123
4124 // TODO: this moves the user to the page where the pending item was added. Ideally,
4125 // the screen would be guaranteed to exist after bind, and the page would be set through
4126 // the workspace restore process.
4127 mWorkspace.post(new Runnable() {
4128 @Override
4129 public void run() {
4130 mWorkspace.snapToScreenId(screenId);
4131 }
4132 });
4133 sPendingAddItem = null;
4134 }
4135
Sunny Goyal756adbc2015-04-16 15:20:51 -07004136 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004137
4138 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004139 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004140 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004141 }
4142
Adam Cohen3ed316a2014-07-23 18:21:20 -07004143 private void sendLoadingCompleteBroadcastIfNecessary() {
4144 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4145 String permission =
4146 getResources().getString(R.string.receive_first_load_broadcast_permission);
4147 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4148 sendBroadcast(intent, permission);
4149 SharedPreferences.Editor editor = mSharedPrefs.edit();
4150 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4151 editor.apply();
4152 }
4153 }
4154
Winson Chunga0b7e862013-09-05 16:03:15 -07004155 public boolean isAllAppsButtonRank(int rank) {
4156 if (mHotseat != null) {
4157 return mHotseat.isAllAppsButtonRank(rank);
4158 }
4159 return false;
4160 }
4161
Winson Chunga2413752012-04-03 14:22:34 -07004162 private boolean canRunNewAppsAnimation() {
4163 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4164 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4165 }
4166
Winson Chungc9168342013-06-26 14:54:55 -07004167 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4168 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4169 PropertyValuesHolder.ofFloat("alpha", 1f),
4170 PropertyValuesHolder.ofFloat("scaleX", 1f),
4171 PropertyValuesHolder.ofFloat("scaleY", 1f));
4172 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4173 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004174 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004175 return bounceAnim;
4176 }
4177
Adam Cohen27772732013-11-11 14:00:56 +00004178 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004179 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004180 }
4181
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004182 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004183 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004184 }
4185
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004186 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004187 if (mSearchDropTargetBar == null) {
4188 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004189 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004190 if (mQsb != null) {
4191 mSearchDropTargetBar.removeView(mQsb);
4192 mQsb = null;
4193 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004194 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004195 }
4196
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004197 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004198 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4199 * multiple calls to bind the same list.)
4200 */
4201 @Thunk ArrayList<AppInfo> mTmpAppsList;
4202 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4203 public void run() {
4204 bindAllApplications(mTmpAppsList);
4205 mTmpAppsList = null;
4206 }
4207 };
4208
4209 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004210 * Add the icons for all apps.
4211 *
4212 * Implementation of the method from LauncherModel.Callbacks.
4213 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004214 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004215 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4216 mTmpAppsList = apps;
4217 return;
4218 }
4219
Winson Chungb745afb2015-03-02 11:51:23 -08004220 if (mAppsView != null) {
4221 mAppsView.setApps(apps);
4222 }
Adam Cohen9211d422014-10-07 18:14:53 -07004223 if (mLauncherCallbacks != null) {
4224 mLauncherCallbacks.bindAllApplications(apps);
4225 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004226 }
4227
4228 /**
4229 * A package was updated.
4230 *
4231 * Implementation of the method from LauncherModel.Callbacks.
4232 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004233 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004234 Runnable r = new Runnable() {
4235 public void run() {
4236 bindAppsUpdated(apps);
4237 }
4238 };
4239 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004240 return;
4241 }
4242
Winson Chungb745afb2015-03-02 11:51:23 -08004243 if (mAppsView != null) {
4244 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004245 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004246 }
4247
Sunny Goyal4390ace2014-10-13 11:33:11 -07004248 @Override
4249 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4250 Runnable r = new Runnable() {
4251 public void run() {
4252 bindWidgetsRestored(widgets);
4253 }
4254 };
4255 if (waitUntilResume(r)) {
4256 return;
4257 }
4258 mWorkspace.widgetsRestored(widgets);
4259 }
4260
Joe Onorato9c1289c2009-08-17 11:03:03 -04004261 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004262 * Some shortcuts were updated in the background.
4263 *
4264 * Implementation of the method from LauncherModel.Callbacks.
4265 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004266 @Override
4267 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4268 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004269 Runnable r = new Runnable() {
4270 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004271 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004272 }
4273 };
4274 if (waitUntilResume(r)) {
4275 return;
4276 }
4277
Sunny Goyal4390ace2014-10-13 11:33:11 -07004278 if (!updated.isEmpty()) {
4279 mWorkspace.updateShortcuts(updated);
4280 }
4281
4282 if (!removed.isEmpty()) {
4283 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4284 for (ShortcutInfo si : removed) {
4285 removedComponents.add(si.getTargetComponent());
4286 }
4287 mWorkspace.removeItemsByComponentName(removedComponents, user);
4288 // Notify the drag controller
4289 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004290 }
4291 }
4292
4293 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004294 * Update the state of a package, typically related to install state.
4295 *
4296 * Implementation of the method from LauncherModel.Callbacks.
4297 */
Sunny Goyale755d462014-07-22 13:48:29 -07004298 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004299 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4300 Runnable r = new Runnable() {
4301 public void run() {
4302 bindRestoreItemsChange(updates);
4303 }
4304 };
4305 if (waitUntilResume(r)) {
4306 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004307 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004308
Sunny Goyal756adbc2015-04-16 15:20:51 -07004309 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004310 }
4311
4312 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004313 * A package was uninstalled. We take both the super set of packageNames
4314 * in addition to specific applications to remove, the reason being that
4315 * this can be called when a package is updated as well. In that scenario,
4316 * we only remove specific components from the workspace, where as
4317 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004318 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004319 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004320 * Implementation of the method from LauncherModel.Callbacks.
4321 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004322 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004323 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004324 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004325 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004326 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004327 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004328 }
Winson Chungd64d1762013-08-20 14:37:16 -07004329 };
4330 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004331 return;
4332 }
4333
Sunny Goyal1a745e82014-10-02 15:58:31 -07004334 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004335 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4336 for (AppInfo info : appInfos) {
4337 removedComponents.add(info.componentName);
4338 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004339 if (!packageNames.isEmpty()) {
4340 mWorkspace.removeItemsByPackageName(packageNames, user);
4341 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004342 if (!removedComponents.isEmpty()) {
4343 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004344 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004345 // Notify the drag controller
4346 mDragController.onAppsRemoved(packageNames, removedComponents);
4347
Sunny Goyal1a745e82014-10-02 15:58:31 -07004348 } else {
4349 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004350 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004351
Winson Chungdf95eb12013-10-16 14:57:07 -07004352 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004353 if (mAppsView != null) {
4354 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004355 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004356 }
Joe Onoratobe386092009-11-17 17:32:16 -08004357
Michael Jurkac402cd92013-05-20 15:49:32 +02004358 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004359 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004360 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004361 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004362 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004363
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004364 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004365 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004366 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004367 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004368 return;
4369 }
4370
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004371 if (mWidgetsView != null && model != null) {
4372 mWidgetsView.addWidgets(model);
4373 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004374 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004375 }
4376
Winson Chung400438b2011-01-16 17:53:48 -08004377 private int mapConfigurationOriActivityInfoOri(int configOri) {
4378 final Display d = getWindowManager().getDefaultDisplay();
4379 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4380 switch (d.getRotation()) {
4381 case Surface.ROTATION_0:
4382 case Surface.ROTATION_180:
4383 // We are currently in the same basic orientation as the natural orientation
4384 naturalOri = configOri;
4385 break;
4386 case Surface.ROTATION_90:
4387 case Surface.ROTATION_270:
4388 // We are currently in the other basic orientation to the natural orientation
4389 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4390 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4391 break;
4392 }
4393
4394 int[] oriMap = {
4395 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4396 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4397 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4398 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4399 };
4400 // Since the map starts at portrait, we need to offset if this device's natural orientation
4401 // is landscape.
4402 int indexOffset = 0;
4403 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4404 indexOffset = 1;
4405 }
4406 return oriMap[(d.getRotation() + indexOffset) % 4];
4407 }
Adam Cohen446e9402011-09-15 18:21:21 -07004408
Winson Chung4b919f82012-05-01 10:44:08 -07004409 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004410 if (mRotationEnabled) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004411 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4412 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4413 .getConfiguration().orientation));
4414 } else {
4415 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4416 }
Winson Chung4b919f82012-05-01 10:44:08 -07004417 }
4418 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004419
Winson Chung4b919f82012-05-01 10:44:08 -07004420 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004421 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004422 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004423 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004424 } else {
4425 mHandler.postDelayed(new Runnable() {
4426 public void run() {
4427 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4428 }
4429 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004430 }
Winson Chung4b919f82012-05-01 10:44:08 -07004431 }
Winson Chung400438b2011-01-16 17:53:48 -08004432 }
4433
Winson Chunge43a1e72014-01-15 10:33:02 -08004434 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004435 if (mLauncherCallbacks != null) {
4436 return mLauncherCallbacks.isLauncherPreinstalled();
4437 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004438 PackageManager pm = getPackageManager();
4439 try {
4440 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4441 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4442 return true;
4443 } else {
4444 return false;
4445 }
4446 } catch (NameNotFoundException e) {
4447 e.printStackTrace();
4448 return false;
4449 }
4450 }
4451
Adam Cohenea90f832014-05-21 15:03:34 -07004452 /**
4453 * This method indicates whether or not we should suggest default wallpaper dimensions
4454 * when our wallpaper cropper was not yet used to set a wallpaper.
4455 */
4456 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004457 if (mLauncherCallbacks != null) {
4458 return mLauncherCallbacks.overrideWallpaperDimensions();
4459 }
Adam Cohenea90f832014-05-21 15:03:34 -07004460 return true;
4461 }
4462
Adam Cohen432609a2014-03-13 17:03:22 -07004463 /**
4464 * To be overridden by subclasses to indicate that there is an activity to launch
4465 * before showing the standard launcher experience.
4466 */
4467 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004468 if (mLauncherCallbacks != null) {
4469 return mLauncherCallbacks.hasFirstRunActivity();
4470 }
Adam Cohen432609a2014-03-13 17:03:22 -07004471 return false;
4472 }
4473
4474 /**
4475 * To be overridden by subclasses to launch any first run activity
4476 */
4477 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004478 if (mLauncherCallbacks != null) {
4479 return mLauncherCallbacks.getFirstRunActivity();
4480 }
Adam Cohen432609a2014-03-13 17:03:22 -07004481 return null;
4482 }
4483
Winson Chung2826a402015-04-17 16:18:53 -07004484 /**
4485 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004486 */
4487 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004488 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4489 return false;
4490 }
4491
Winson Chung2826a402015-04-17 16:18:53 -07004492 if (mLauncherCallbacks != null) {
4493 return mLauncherCallbacks.overrideAllAppsSearch();
4494 }
4495 return false;
4496 }
4497
Winson Chunga6945242014-01-08 14:04:34 -08004498 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004499 return !ActivityManager.isRunningInTestHarness() &&
4500 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004501 }
4502
Adam Cohen4a9423d2014-03-28 14:22:31 -07004503 protected boolean hasRunFirstRunActivity() {
4504 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4505 }
4506
Adam Cohen432609a2014-03-13 17:03:22 -07004507 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004508 if (shouldRunFirstRunActivity() &&
4509 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004510 Intent firstRunIntent = getFirstRunActivity();
4511 if (firstRunIntent != null) {
4512 startActivity(firstRunIntent);
4513 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004514 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004515 }
4516 }
Adam Cohen432609a2014-03-13 17:03:22 -07004517 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004518 }
4519
4520 private void markFirstRunActivityShown() {
4521 SharedPreferences.Editor editor = mSharedPrefs.edit();
4522 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4523 editor.apply();
4524 }
4525
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004526 private void showSettingsActivity() {
4527 startActivity(new Intent(this, SettingsActivity.class));
4528 }
4529
Adam Cohen432609a2014-03-13 17:03:22 -07004530 /**
4531 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4532 * screen that must be displayed and dismissed.
4533 */
4534 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004535 if (mLauncherCallbacks != null) {
4536 return mLauncherCallbacks.hasDismissableIntroScreen();
4537 }
Adam Cohen432609a2014-03-13 17:03:22 -07004538 return false;
4539 }
4540
4541 /**
4542 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4543 */
4544 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004545 if (mLauncherCallbacks != null) {
4546 return mLauncherCallbacks.getIntroScreen();
4547 }
Adam Cohen432609a2014-03-13 17:03:22 -07004548 return null;
4549 }
4550
4551 /**
4552 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4553 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4554 */
4555 private boolean shouldShowIntroScreen() {
4556 return hasDismissableIntroScreen() &&
4557 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4558 }
4559
4560 protected void showIntroScreen() {
4561 View introScreen = getIntroScreen();
4562 changeWallpaperVisiblity(false);
4563 if (introScreen != null) {
4564 mDragLayer.showOverlayView(introScreen);
4565 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004566 if (mLauncherOverlayContainer != null) {
4567 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4568 }
Adam Cohen432609a2014-03-13 17:03:22 -07004569 }
4570
4571 public void dismissIntroScreen() {
4572 markIntroScreenDismissed();
4573 if (showFirstRunActivity()) {
4574 // We delay hiding the intro view until the first run activity is showing. This
4575 // avoids a blip.
4576 mWorkspace.postDelayed(new Runnable() {
4577 @Override
4578 public void run() {
4579 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004580 if (mLauncherOverlayContainer != null) {
4581 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4582 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004583 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004584 }
4585 }, ACTIVITY_START_DELAY);
4586 } else {
4587 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004588 if (mLauncherOverlayContainer != null) {
4589 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4590 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004591 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004592 }
4593 changeWallpaperVisiblity(true);
4594 }
4595
4596 private void markIntroScreenDismissed() {
4597 SharedPreferences.Editor editor = mSharedPrefs.edit();
4598 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4599 editor.apply();
4600 }
4601
Adam Cohen091440a2015-03-18 14:16:05 -07004602 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004603 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4604 // on the device, then we always show the first run cling experience (or if there is no
4605 // launcher2). Otherwise, we prompt the user upon started for migration
4606 LauncherClings launcherClings = new LauncherClings(this);
4607 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4608 if (mModel.canMigrateFromOldLauncherDb(this)) {
4609 launcherClings.showMigrationCling();
4610 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004611 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004612 }
4613 }
4614 }
4615
Winson Chunga6945242014-01-08 14:04:34 -08004616 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004617 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4618 if (mHotseat != null) mHotseat.setAlpha(1f);
4619 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4620 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004621 }
4622
4623 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004624 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4625 if (mHotseat != null) mHotseat.setAlpha(0f);
4626 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4627 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004628 }
4629
Mathew Inwood72fbec12013-11-19 15:45:07 +00004630 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004631 // Called from search suggestion, not supported in other profiles.
4632 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4633 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4634 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4635 myUser);
4636 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004637 return null;
4638 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004639 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004640 }
4641
4642 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4643 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004644 // Called from search suggestion, not supported in other profiles.
4645 return createShortcutDragInfo(shortcutIntent, caption, icon,
4646 UserHandleCompat.myUserHandle());
4647 }
4648
4649 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4650 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004651 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004652 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004653 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004654 }
4655
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004656 protected void moveWorkspaceToDefaultScreen() {
4657 mWorkspace.moveToDefaultScreen(false);
4658 }
4659
Mathew Inwood72fbec12013-11-19 15:45:07 +00004660 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4661 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004662 mWorkspace.onExternalDragStartedWithItem(dragView);
4663 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004664 }
4665
Anjali Koppalf5d29132014-02-28 13:33:27 -08004666 @Override
4667 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004668 if (mLauncherCallbacks != null) {
4669 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4670 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004671 }
4672
Winson Chung80baf5a2010-08-09 16:03:15 -07004673 /**
Sunny Goyal53d7ee42015-05-22 12:25:45 -07004674 * Returns a FastBitmapDrawable with the icon, accurately sized.
4675 */
4676 public FastBitmapDrawable createIconDrawable(Bitmap icon) {
4677 FastBitmapDrawable d = new FastBitmapDrawable(icon);
4678 d.setFilterBitmap(true);
4679 resizeIconDrawable(d);
4680 return d;
4681 }
4682
4683 /**
4684 * Resizes an icon drawable to the correct icon size.
4685 */
4686 public void resizeIconDrawable(Drawable icon) {
4687 icon.setBounds(0, 0, mDeviceProfile.iconSizePx, mDeviceProfile.iconSizePx);
4688 }
4689
4690 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004691 * Prints out out state for debugging.
4692 */
4693 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004694 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004695 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004696 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4697 Log.d(TAG, "mRestoring=" + mRestoring);
4698 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4699 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004700 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004701 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004702 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004703
Daniel Sandler325dc232013-06-05 22:57:57 -04004704 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004705 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004706
4707 @Override
4708 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4709 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004710 synchronized (sDumpLogs) {
4711 writer.println(" ");
4712 writer.println("Debug logs: ");
4713 for (int i = 0; i < sDumpLogs.size(); i++) {
4714 writer.println(" " + sDumpLogs.get(i));
4715 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004716 }
Adam Cohen9211d422014-10-07 18:14:53 -07004717 if (mLauncherCallbacks != null) {
4718 mLauncherCallbacks.dump(prefix, fd, writer, args);
4719 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004720 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004721
4722 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004723 if (DEBUG_DUMP_LOG) {
4724 synchronized (sDumpLogs) {
4725 Log.d(TAG, "");
4726 Log.d(TAG, "*********************");
4727 Log.d(TAG, "Launcher debug logs: ");
4728 for (int i = 0; i < sDumpLogs.size(); i++) {
4729 Log.d(TAG, " " + sDumpLogs.get(i));
4730 }
4731 Log.d(TAG, "*********************");
4732 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004733 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004734 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004735 }
4736
4737 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004738 addDumpLog(tag, log, null, debugLog);
4739 }
4740
4741 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004742 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004743 if (e != null) {
4744 Log.d(tag, log, e);
4745 } else {
4746 Log.d(tag, log);
4747 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004748 }
Winson Chungede41292013-09-19 16:27:36 -07004749 if (DEBUG_DUMP_LOG) {
4750 sDateStamp.setTime(System.currentTimeMillis());
4751 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004752 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4753 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004754 }
Winson Chungede41292013-09-19 16:27:36 -07004755 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004756 }
4757
Adam Cohen59400422014-03-05 18:07:04 -08004758 public static CustomAppWidget getCustomAppWidget(String name) {
4759 return sCustomAppWidgets.get(name);
4760 }
4761
4762 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4763 return sCustomAppWidgets;
4764 }
4765
Winson Chungede41292013-09-19 16:27:36 -07004766 public void dumpLogsToLocalData() {
4767 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004768 new AsyncTask<Void, Void, Void>() {
4769 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004770 boolean success = false;
4771 sDateStamp.setTime(sRunStart);
4772 String FILENAME = sDateStamp.getMonth() + "-"
4773 + sDateStamp.getDay() + "_"
4774 + sDateStamp.getHours() + "-"
4775 + sDateStamp.getMinutes() + "_"
4776 + sDateStamp.getSeconds() + ".txt";
4777
4778 FileOutputStream fos = null;
4779 File outFile = null;
4780 try {
4781 outFile = new File(getFilesDir(), FILENAME);
4782 outFile.createNewFile();
4783 fos = new FileOutputStream(outFile);
4784 } catch (Exception e) {
4785 e.printStackTrace();
4786 }
4787 if (fos != null) {
4788 PrintWriter writer = new PrintWriter(fos);
4789
4790 writer.println(" ");
4791 writer.println("Debug logs: ");
4792 synchronized (sDumpLogs) {
4793 for (int i = 0; i < sDumpLogs.size(); i++) {
4794 writer.println(" " + sDumpLogs.get(i));
4795 }
4796 }
4797 writer.close();
4798 }
4799 try {
4800 if (fos != null) {
4801 fos.close();
4802 success = true;
4803 }
4804 } catch (IOException e) {
4805 e.printStackTrace();
4806 }
Michael Jurka43467462013-11-14 12:03:58 +01004807 return null;
Winson Chungede41292013-09-19 16:27:36 -07004808 }
Michael Jurka43467462013-11-14 12:03:58 +01004809 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004810 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004811 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004812}
Michael Jurka2763be32011-02-24 11:19:57 -08004813
Dan Sandlerd5024042014-01-09 15:01:33 -05004814interface DebugIntents {
4815 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4816 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004817}