blob: da6430a735f6a92021c7d8be227d298c44c55ab3 [file] [log] [blame]
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001/*
2 * Copyright (C) 2008 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080018
Gilles Debunnedd6c9922010-10-25 11:23:41 -070019import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080020import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070021import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070022import android.animation.ObjectAnimator;
23import android.animation.PropertyValuesHolder;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070024import android.animation.ValueAnimator;
Sunny Goyal70660032015-05-14 00:07:08 -070025import android.annotation.SuppressLint;
Adam Cohen0b395352014-06-09 22:54:36 +000026import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070027import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070028import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070029import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040030import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080031import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080032import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070033import android.appwidget.AppWidgetManager;
34import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080035import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080036import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080037import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040038import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080039import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080040import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040041import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080042import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070043import android.content.IntentFilter;
Adam Cohen173f7112015-03-27 15:14:00 -070044import android.content.IntentSender;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080051import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070052import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070056import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070057import android.graphics.Rect;
Sunny Goyal4a6c6f32015-05-19 12:36:46 -070058import android.graphics.drawable.ColorDrawable;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080069import android.text.Selection;
70import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070071import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080072import android.text.method.TextKeyListener;
Joe Onorato7c312c12009-08-13 21:36:53 -070073import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070074import android.view.Display;
75import android.view.Gravity;
76import android.view.HapticFeedbackConstants;
77import android.view.KeyEvent;
78import android.view.LayoutInflater;
79import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070080import android.view.MotionEvent;
81import android.view.Surface;
82import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070083import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080084import android.view.View.OnLongClickListener;
Adam Cohen96d30a12013-07-16 18:13:21 -070085import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070086import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070088import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080090import android.view.accessibility.AccessibilityEvent;
Sunny Goyal5a1f53b2015-05-27 10:24:24 -070091import android.view.animation.OvershootInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080092import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070093import android.widget.Advanceable;
94import android.widget.FrameLayout;
95import android.widget.ImageView;
Chet Haasea8f996d2015-02-17 12:56:04 -080096import android.widget.TextView;
Adam Cohen96d30a12013-07-16 18:13:21 -070097import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070098
Sunny Goyal651077b2014-06-30 14:15:31 -070099import com.android.launcher3.DropTarget.DragObject;
100import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyal83a8f042015-05-19 12:52:12 -0700101import com.android.launcher3.accessibility.LauncherAccessibilityDelegate;
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700102import com.android.launcher3.allapps.AllAppsContainerView;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700103import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100104import com.android.launcher3.compat.LauncherActivityInfoCompat;
105import com.android.launcher3.compat.LauncherAppsCompat;
106import com.android.launcher3.compat.UserHandleCompat;
107import com.android.launcher3.compat.UserManagerCompat;
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700108import com.android.launcher3.model.WidgetsModel;
Sunny Goyale2df0622015-04-24 11:27:00 -0700109import com.android.launcher3.util.LongArrayMap;
Adam Cohen091440a2015-03-18 14:16:05 -0700110import com.android.launcher3.util.Thunk;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700111import com.android.launcher3.widget.PendingAddWidgetInfo;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -0700112import com.android.launcher3.widget.WidgetHostViewLoader;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700113import com.android.launcher3.widget.WidgetsContainerView;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700114
Adam Cohenc0dcf592011-06-01 15:30:43 -0700115import java.io.DataInputStream;
116import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700118import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700119import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700120import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700121import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700122import java.io.PrintWriter;
Chet Haasea8f996d2015-02-17 12:56:04 -0800123import java.lang.reflect.InvocationTargetException;
124import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700125import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700126import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700127import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700128import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700129import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700130import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700131import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000132import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700133
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800134/**
135 * Default launcher application.
136 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100137public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700138 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Winson Chungb745afb2015-03-02 11:51:23 -0800139 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener,
140 LauncherStateTransitionAnimation.Callbacks {
Hyunyoung Song4e8fb912015-04-11 15:44:32 -0700141 static final String TAG = "Launcher";
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700142 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800143
Winson Chung83f59ab2015-05-05 17:21:58 -0700144 // Temporary flag
145 static final boolean DISABLE_ALL_APPS_SEARCH_INTEGRATION = true;
146
Daniel Sandlerf061f822013-06-27 13:59:36 -0400147 static final boolean PROFILE_STARTUP = false;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700148 static final boolean DEBUG_WIDGETS = true;
Winson Chunga2413752012-04-03 14:22:34 -0700149 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400150 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700151 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700152
Dan Sandlerd5024042014-01-09 15:01:33 -0500153 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
154
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800155 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700156 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700157 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700158 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800159
Michael Jurka8b805b12012-04-18 14:23:14 -0700160 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700161 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700162
Sunny Goyal4a6c6f32015-05-19 12:36:46 -0700163 private static final int WORKSPACE_BACKGROUND_GRADIENT = 0;
164 private static final int WORKSPACE_BACKGROUND_TRANSPARENT = 1;
165 private static final int WORKSPACE_BACKGROUND_BLACK = 2;
166
Sunny Goyal5a1f53b2015-05-27 10:24:24 -0700167 private static final float BOUNCE_ANIMATION_TENSION = 1.3f;
168
Mathew Inwood876a8462013-06-14 14:12:41 +0100169 /**
170 * IntentStarter uses request codes starting with this. This must be greater than all activity
171 * request codes used internally.
172 */
173 protected static final int REQUEST_LAST = 100;
174
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800175 static final int SCREEN_COUNT = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176
Michael Jurka0a457bf2012-11-19 14:05:05 -0800177 // To turn on these properties, type
178 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800179 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180
Winson Chung2672ff92012-05-04 16:22:30 -0700181 // The Intent extra that defines whether to ignore the launch animation
182 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400183 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700184
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 // Type: int
186 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700187 // Type: int
188 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700190 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
191 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800192 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
193 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700194 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800195 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700196 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700197 // Type: int
198 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
199 // Type: int
200 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
201 // Type: parcelable
202 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000203 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800204 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000205 // Type: int[]
206 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800207
Adam Cohen432609a2014-03-13 17:03:22 -0700208 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800209 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
210
Adam Cohen3ed316a2014-07-23 18:21:20 -0700211 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
212 static final String ACTION_FIRST_LOAD_COMPLETE =
213 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
214
Adam Cohen39a06042013-07-19 14:30:12 -0700215 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700216 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700217
Sunny Goyal594d76d2014-11-06 10:12:54 -0800218 private static final String QSB_WIDGET_ID = "qsb_widget_id";
219 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
220
Winson Chunga6945242014-01-08 14:04:34 -0800221 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
222
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700223 /** The different states that Launcher can be in. */
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700224 enum State { NONE, WORKSPACE, APPS, APPS_SPRING_LOADED, WIDGETS, WIDGETS_SPRING_LOADED }
225
Adam Cohen091440a2015-03-18 14:16:05 -0700226 @Thunk State mState = State.WORKSPACE;
Adam Cohen091440a2015-03-18 14:16:05 -0700227 @Thunk LauncherStateTransitionAnimation mStateTransitionAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700228
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700229 private boolean mIsSafeModeEnabled;
230
Adam Cohenc2d6e892014-10-16 09:49:24 -0700231 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
232 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700233 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700234
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700236 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
237 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700238 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000240 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
241 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
242
Winson Chunga2413752012-04-03 14:22:34 -0700243 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700244 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
245 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Adam Cohen091440a2015-03-18 14:16:05 -0700246 @Thunk static int NEW_APPS_ANIMATION_DELAY = 500;
Winson Chunga2413752012-04-03 14:22:34 -0700247
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800248 private final BroadcastReceiver mCloseSystemDialogsReceiver
249 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800250 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 private LayoutInflater mInflater;
253
Adam Cohen091440a2015-03-18 14:16:05 -0700254 @Thunk Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700255 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800256 private View mPageIndicators;
Adam Cohen091440a2015-03-18 14:16:05 -0700257 @Thunk DragLayer mDragLayer;
Michael Jurkab737ee62011-11-15 15:57:22 -0800258 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700259 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700260
Sunny Goyalffe83f12014-08-14 17:39:34 -0700261 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700262 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700263
Adam Cohen091440a2015-03-18 14:16:05 -0700264 @Thunk ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen59400422014-03-05 18:07:04 -0800265 private LauncherAppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800266 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700267
Michael Jurka0280c3b2010-09-17 15:00:07 -0700268 private int[] mTmpAddItemCellCoordinates = new int[2];
269
Winson Chung3d503fb2011-07-13 17:25:49 -0700270 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800271 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700272
Michael Jurka838a4ca2011-02-07 13:33:06 -0800273 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700274
Winson Chungc51db6a2011-10-05 11:44:49 -0700275 private SearchDropTargetBar mSearchDropTargetBar;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700276
277 // Main container view for the all apps screen.
Winson Chung5f4e0fd2015-05-22 11:12:27 -0700278 @Thunk AllAppsContainerView mAppsView;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700279
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -0700280 // Main container view and the model for the widget tray screen.
281 @Thunk WidgetsContainerView mWidgetsView;
282 @Thunk WidgetsModel mWidgetsModel;
Hyunyoung Song3f471442015-04-08 19:01:34 -0700283
Winson Chungf0ea4d32011-06-06 14:27:16 -0700284 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800285 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700288 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
289 // scroll issues (because the workspace may not have been measured yet) and extra work.
290 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
291 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292
293 private SpannableStringBuilder mDefaultKeySsb = null;
294
Adam Cohen091440a2015-03-18 14:16:05 -0700295 @Thunk boolean mWorkspaceLoading = true;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400296
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800297 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 private boolean mRestoring;
299 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700300 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301
Michael Jurka1e2f4652013-07-08 18:03:46 -0700302 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700303 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 private Bundle mSavedInstanceState;
306
Joe Onorato9c1289c2009-08-17 11:03:03 -0400307 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800308 private IconCache mIconCache;
Adam Cohen091440a2015-03-18 14:16:05 -0700309 @Thunk boolean mUserPresent = true;
Adam Cohend113e0c2010-11-11 10:48:05 -0800310 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700311 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800312 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400313
Adam Cohen091440a2015-03-18 14:16:05 -0700314 @Thunk static LocaleConfiguration sLocaleConfiguration = null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700315
Sunny Goyale2df0622015-04-24 11:27:00 -0700316 private static LongArrayMap<FolderInfo> sFolders = new LongArrayMap<>();
Romain Guycbb89e42009-06-08 15:52:54 -0700317
Adam Cohen61f560d2013-09-30 15:58:20 -0700318 private View.OnTouchListener mHapticFeedbackTouchListener;
319
Adam Cohended9f8d2010-11-03 13:25:16 -0700320 // Related to the auto-advancing of widgets
321 private final int ADVANCE_MSG = 1;
322 private final int mAdvanceInterval = 20000;
323 private final int mAdvanceStagger = 250;
324 private long mAutoAdvanceSentTime;
325 private long mAutoAdvanceTimeLeft = -1;
Adam Cohen091440a2015-03-18 14:16:05 -0700326 @Thunk HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
Adam Cohended9f8d2010-11-03 13:25:16 -0700327 new HashMap<View, AppWidgetProviderInfo>();
328
Winson Chung400438b2011-01-16 17:53:48 -0800329 // Determines how long to wait after a rotation before restoring the screen orientation to
330 // match the sensor state.
331 private final int mRestoreScreenOrientationDelay = 500;
332
Adam Cohen091440a2015-03-18 14:16:05 -0700333 @Thunk Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700334
Adam Cohen1462de32012-07-24 22:34:36 -0700335 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700336 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700337
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700338 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700339 static Date sDateStamp = new Date();
340 static DateFormat sDateFormat =
341 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
342 static long sRunStart = System.currentTimeMillis();
343 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700344
Winson Chung46353de2012-02-16 14:05:10 -0800345 // We only want to get the SharedPreferences once since it does an FS stat each time we get
346 // it from the context.
347 private SharedPreferences mSharedPrefs;
348
Winson Chungf0c6ae02012-03-21 16:10:31 -0700349 // Holds the page that we need to animate to, and the icon views that we need to animate up
350 // when we scroll to that page on resume.
Adam Cohen091440a2015-03-18 14:16:05 -0700351 @Thunk ImageView mFolderIconImageView;
Adam Cohen268c4752012-06-06 17:47:33 -0700352 private Bitmap mFolderIconBitmap;
353 private Canvas mFolderIconCanvas;
354 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700355
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700356 private DeviceProfile mDeviceProfile;
357
Winson Chung13eb5272015-05-11 16:30:13 -0700358 // This is set to the view that launched the activity that navigated the user away from
359 // launcher. Since there is no callback for when the activity has finished launching, enable
360 // the press state and keep this reference to reset the press state when we return to launcher.
Michael Jurkaddd62e92011-02-16 17:49:14 -0800361 private BubbleTextView mWaitingForResume;
362
Adam Cohen59400422014-03-05 18:07:04 -0800363 protected static HashMap<String, CustomAppWidget> sCustomAppWidgets =
364 new HashMap<String, CustomAppWidget>();
365
366 private static final boolean ENABLE_CUSTOM_WIDGET_TEST = false;
367 static {
368 if (ENABLE_CUSTOM_WIDGET_TEST) {
369 sCustomAppWidgets.put(DummyWidget.class.getName(), new DummyWidget());
370 }
371 }
372
Chet Haasea8f996d2015-02-17 12:56:04 -0800373 // TODO: remove this field and call method directly when Launcher3 can depend on M APIs
374 private static Method sClipRevealMethod = null;
375 static {
376 Class<?> activityOptionsClass = ActivityOptions.class;
377 try {
378 sClipRevealMethod = activityOptionsClass.getDeclaredMethod("makeClipRevealAnimation",
379 View.class, int.class, int.class, int.class, int.class);
380 } catch (Exception e) {
381 // Earlier version
382 }
383 }
384
Adam Cohen091440a2015-03-18 14:16:05 -0700385 @Thunk Runnable mBuildLayersRunnable = new Runnable() {
Michael Jurkac1f5d262011-09-30 19:32:27 -0700386 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700387 if (mWorkspace != null) {
388 mWorkspace.buildPageHardwareLayers();
389 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700390 }
391 };
392
Adam Cohendb364c32014-05-20 14:23:40 -0700393 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800394
Adam Cohen091440a2015-03-18 14:16:05 -0700395 @Thunk static class PendingAddArguments {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800396 int requestCode;
397 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700398 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700399 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800400 int cellX;
401 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700402 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800403 }
404
Daniel Sandlerff02d492013-08-05 02:12:05 -0400405 private Stats mStats;
406
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700407 FocusIndicatorView mFocusHandler;
408
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700409 private boolean mRotationEnabled = false;
410 private boolean mPreferenceObserverRegistered = false;
411
412 final private SharedPreferences.OnSharedPreferenceChangeListener mSettingsObserver =
413 new SharedPreferences.OnSharedPreferenceChangeListener() {
414 @Override
415 public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
416 if (Utilities.ALLOW_ROTATION_PREFERENCE_KEY.equals(key)) {
417 if (mRotationEnabled = sharedPreferences.getBoolean(
418 Utilities.ALLOW_ROTATION_PREFERENCE_KEY, false)) {
419 unlockScreenOrientation(true);
420 } else {
421 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_NOSENSOR);
422 }
423 }
424 }
425 };
426
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800427 @Override
428 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700429 if (DEBUG_STRICT_MODE) {
430 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
431 .detectDiskReads()
432 .detectDiskWrites()
433 .detectNetwork() // or .detectAll() for all detectable problems
434 .penaltyLog()
435 .build());
436 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
437 .detectLeakedSqlLiteObjects()
438 .detectLeakedClosableObjects()
439 .penaltyLog()
440 .penaltyDeath()
441 .build());
442 }
443
Adam Cohen9211d422014-10-07 18:14:53 -0700444 if (mLauncherCallbacks != null) {
445 mLauncherCallbacks.preOnCreate();
446 }
447
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800448 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400449
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400450 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400451 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700452 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700453
Adam Cohen2e6da152015-05-06 11:42:25 -0700454 // Load configuration-specific DeviceProfile
Sunny Goyalc6205602015-05-21 20:46:33 -0700455 mDeviceProfile = getResources().getConfiguration().orientation
456 == Configuration.ORIENTATION_LANDSCAPE ?
457 app.getInvariantDeviceProfile().landscapeProfile
458 : app.getInvariantDeviceProfile().portraitProfile;
459
460 // TODO: Move this to icon cache.
461 Utilities.setIconSize(mDeviceProfile.iconSizePx);
Winson Chung5f8afe62013-08-12 16:19:28 -0700462
463 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400464 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700465 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700466 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800467 mModel = app.setLauncher(this);
468 mIconCache = app.getIconCache();
Adam Cohen2e6da152015-05-06 11:42:25 -0700469
Joe Onorato41a12d22009-10-31 18:30:00 -0400470 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 mInflater = getLayoutInflater();
Winson Chungb745afb2015-03-02 11:51:23 -0800472 mStateTransitionAnimation = new LauncherStateTransitionAnimation(this, this);
Romain Guycbb89e42009-06-08 15:52:54 -0700473
Daniel Sandlerff02d492013-08-05 02:12:05 -0400474 mStats = new Stats(this);
475
Sunny Goyalffe83f12014-08-14 17:39:34 -0700476 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700477
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700478 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
479 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700480
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700481 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
482 // this also ensures that any synchronous binding below doesn't re-trigger another
483 // LauncherModel load.
484 mPaused = false;
485
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200487 android.os.Debug.startMethodTracing(
488 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800489 }
490
491 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800492 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700493
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800494 setupViews();
Sunny Goyaldfaccf62015-05-11 16:30:44 -0700495 mDeviceProfile.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800496
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800497 registerContentObservers();
498
Joe Onorato7c312c12009-08-13 21:36:53 -0700499 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700500
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800501 mSavedState = savedInstanceState;
502 restoreState(mSavedState);
503
504 if (PROFILE_STARTUP) {
505 android.os.Debug.stopMethodTracing();
506 }
507
508 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700509 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700510 // If the user leaves launcher, then we should just load items asynchronously when
511 // they return.
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700512 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700513 } else {
514 // We only load the page synchronously if the user rotates (or triggers a
515 // configuration change) while launcher is in the foreground
Sunny Goyal2bba4c32015-05-18 15:42:48 -0700516 mModel.startLoader(mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700517 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800518 }
519
520 // For handling default keys
521 mDefaultKeySsb = new SpannableStringBuilder();
522 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800523
524 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
525 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800526
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -0700527 mRotationEnabled = Utilities.isRotationAllowedForDevice(getApplicationContext());
528 // In case we are on a device with locked rotation, we should look at preferences to check
529 // if the user has specifically allowed rotation.
530 if (!mRotationEnabled) {
531 getSharedPreferences(LauncherFiles.ROTATION_PREF_FILE,
532 Context.MODE_MULTI_PROCESS).registerOnSharedPreferenceChangeListener(
533 mSettingsObserver);
534 mPreferenceObserverRegistered = true;
535 mRotationEnabled = Utilities.isAllowRotationPrefEnabled(getApplicationContext());
536 }
537
538 // On large interfaces, or on devices that a user has specifically enabled screen rotation,
539 // we want the screen to auto-rotate based on the current orientation
Adam Cohenf9426d52012-06-04 17:26:21 -0700540 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700541
Adam Cohen9211d422014-10-07 18:14:53 -0700542 if (mLauncherCallbacks != null) {
543 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700544 if (mLauncherCallbacks.hasLauncherOverlay()) {
545 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700546 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
547 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
548 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700549 mWorkspace.setLauncherOverlay(mLauncherOverlay);
550 }
Adam Cohen9211d422014-10-07 18:14:53 -0700551 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700552
553 if (shouldShowIntroScreen()) {
554 showIntroScreen();
555 } else {
556 showFirstRunActivity();
557 showFirstRunClings();
558 }
Adam Cohen9211d422014-10-07 18:14:53 -0700559 }
560
561 private LauncherCallbacks mLauncherCallbacks;
562
563 public void onPostCreate(Bundle savedInstanceState) {
564 super.onPostCreate(savedInstanceState);
565 if (mLauncherCallbacks != null) {
566 mLauncherCallbacks.onPostCreate(savedInstanceState);
567 }
568 }
569
570 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
571 mLauncherCallbacks = callbacks;
Winson Chung0f785722015-04-08 10:27:49 -0700572 mLauncherCallbacks.setLauncherAppsCallback(new Launcher.LauncherAppsCallbacks() {
573 @Override
574 public void onAllAppsBoundsChanged(Rect bounds) {
Winson Chungcd99cd32015-04-29 11:03:24 -0700575 if (LOGD) {
576 Log.d(TAG, "onAllAppsBoundsChanged(Rect): " + bounds);
577 }
Winson Chung94804152015-05-08 13:06:44 -0700578 mAppsView.setFixedBounds(bounds);
579 mWidgetsView.setFixedBounds(bounds);
Winson Chung0f785722015-04-08 10:27:49 -0700580 }
581
582 @Override
583 public void dismissAllApps() {
Winson Chung83f59ab2015-05-05 17:21:58 -0700584 if (!DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
585 // Dismiss All Apps if we aren't already paused/invisible
586 if (!mPaused) {
587 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true,
588 null /* onCompleteRunnable */, false /* notifyLauncherCallbacks */);
589 }
Winson Chungcd99cd32015-04-29 11:03:24 -0700590 }
Winson Chung0f785722015-04-08 10:27:49 -0700591 }
592 });
Jun Mukai8af0cd82015-05-12 12:32:12 -0700593 mLauncherCallbacks.setLauncherSearchCallback(new Launcher.LauncherSearchCallbacks() {
594 private boolean mImportanceStored = false;
595 private int mWorkspaceImportanceForAccessibility =
596 View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
597 private int mHotseatImportanceForAccessibility = View.IMPORTANT_FOR_ACCESSIBILITY_AUTO;
598
599 @Override
600 public void onSearchOverlayOpened() {
601 if (mImportanceStored) {
602 return;
603 }
604 // The underlying workspace and hotseat are temporarily suppressed by the search
605 // overlay. So they sholudn't be accessible.
606 mWorkspaceImportanceForAccessibility = mWorkspace.getImportantForAccessibility();
607 mHotseatImportanceForAccessibility = mHotseat.getImportantForAccessibility();
608 mWorkspace.setImportantForAccessibility(
609 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
610 mHotseat.setImportantForAccessibility(
611 View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS);
612 mImportanceStored = true;
613 }
614
615 @Override
616 public void onSearchOverlayClosed() {
617 mWorkspace.setImportantForAccessibility(mWorkspaceImportanceForAccessibility);
618 mHotseat.setImportantForAccessibility(mHotseatImportanceForAccessibility);
619 mImportanceStored = false;
620 }
621 });
Adam Cohen9211d422014-10-07 18:14:53 -0700622 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700623 }
624
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700625 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700626 public void onLauncherProviderChange() {
627 if (mLauncherCallbacks != null) {
628 mLauncherCallbacks.onLauncherProviderChange();
629 }
630 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700631
Adam Cohen9211d422014-10-07 18:14:53 -0700632 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700633 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700634 if (mLauncherCallbacks != null) {
635 return mLauncherCallbacks.hasCustomContentToLeft();
636 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700637 return false;
638 }
639
Dave Hawkeya8881582013-09-17 15:55:33 -0600640 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500641 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600642 * {@link #addToCustomContentPage}. This will only be invoked if
643 * {@link #hasCustomContentToLeft()} is {@code true}.
644 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500645 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700646 if (mLauncherCallbacks != null) {
647 mLauncherCallbacks.populateCustomContentContainer();
648 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600649 }
650
651 /**
652 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
653 * ensure the custom content page is added or removed if necessary.
654 */
655 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600656 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600657 // Not bound yet, wait for bindScreens to be called.
658 return;
659 }
660
661 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
662 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500663 mWorkspace.createCustomContentContainer();
664 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600665 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
666 mWorkspace.removeCustomContentPage();
667 }
668 }
669
Adam Cohen091440a2015-03-18 14:16:05 -0700670 @Thunk void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700671 if (sLocaleConfiguration == null) {
672 new AsyncTask<Void, Void, LocaleConfiguration>() {
673 @Override
674 protected LocaleConfiguration doInBackground(Void... unused) {
675 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
676 readConfiguration(Launcher.this, localeConfiguration);
677 return localeConfiguration;
678 }
679
680 @Override
681 protected void onPostExecute(LocaleConfiguration result) {
682 sLocaleConfiguration = result;
683 checkForLocaleChange(); // recursive, but now with a locale configuration
684 }
685 }.execute();
686 return;
687 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700688
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800689 final Configuration configuration = getResources().getConfiguration();
690
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700691 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 final String locale = configuration.locale.toString();
693
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700694 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800695 final int mcc = configuration.mcc;
696
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700697 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800698 final int mnc = configuration.mnc;
699
Romain Guy84f296c2009-11-04 15:00:44 -0800700 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701
Romain Guy84f296c2009-11-04 15:00:44 -0800702 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700703 sLocaleConfiguration.locale = locale;
704 sLocaleConfiguration.mcc = mcc;
705 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700706
Joe Onorato0589f0f2010-02-08 13:44:00 -0800707 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700708
709 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100710 new AsyncTask<Void, Void, Void>() {
711 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700712 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100713 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700714 }
Michael Jurka43467462013-11-14 12:03:58 +0100715 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700716 }
717 }
718
Adam Cohen091440a2015-03-18 14:16:05 -0700719 @Thunk static class LocaleConfiguration {
Romain Guy98d01652009-06-30 16:21:04 -0700720 public String locale;
721 public int mcc = -1;
722 public int mnc = -1;
723 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700724
Adam Cohen091440a2015-03-18 14:16:05 -0700725 @Thunk static void readConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700726 DataInputStream in = null;
727 try {
Helena Josol28db2802014-10-09 17:04:09 +0100728 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700729 configuration.locale = in.readUTF();
730 configuration.mcc = in.readInt();
731 configuration.mnc = in.readInt();
732 } catch (FileNotFoundException e) {
733 // Ignore
734 } catch (IOException e) {
735 // Ignore
736 } finally {
737 if (in != null) {
738 try {
739 in.close();
740 } catch (IOException e) {
741 // Ignore
742 }
743 }
744 }
745 }
746
Adam Cohen091440a2015-03-18 14:16:05 -0700747 @Thunk static void writeConfiguration(Context context, LocaleConfiguration configuration) {
Romain Guy98d01652009-06-30 16:21:04 -0700748 DataOutputStream out = null;
749 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100750 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100751 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700752 out.writeUTF(configuration.locale);
753 out.writeInt(configuration.mcc);
754 out.writeInt(configuration.mnc);
755 out.flush();
756 } catch (FileNotFoundException e) {
757 // Ignore
758 } catch (IOException e) {
759 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100760 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700761 } finally {
762 if (out != null) {
763 try {
764 out.close();
765 } catch (IOException e) {
766 // Ignore
767 }
768 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800769 }
770 }
771
Anton Hanssona2f665f2013-09-26 12:18:32 +0100772 public Stats getStats() {
773 return mStats;
774 }
775
Bjorn Bringertc459e522013-06-07 19:36:01 +0100776 public LayoutInflater getInflater() {
777 return mInflater;
778 }
779
Hyunyoung Song3f471442015-04-08 19:01:34 -0700780 public boolean isDraggingEnabled() {
Winson Chung36a62fe2012-05-06 18:04:42 -0700781 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
782 // that is subsequently removed from the workspace in startBinding().
783 return !mModel.isLoadingWorkspace();
784 }
785
Sunny Goyal71b5c0b2015-01-08 16:59:04 -0800786 @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000787 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100788 if (Build.VERSION.SDK_INT >= 17) {
789 return View.generateViewId();
790 } else {
791 // View.generateViewId() is not available. The following fallback logic is a copy
792 // of its implementation.
793 for (;;) {
794 final int result = sNextGeneratedId.get();
795 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
796 int newValue = result + 1;
797 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
798 if (sNextGeneratedId.compareAndSet(result, newValue)) {
799 return result;
800 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000801 }
802 }
803 }
804
805 public int getViewIdForItem(ItemInfo info) {
806 // This cast is safe given the > 2B range for int.
807 int itemId = (int) info.id;
808 if (mItemIdToViewId.containsKey(itemId)) {
809 return mItemIdToViewId.get(itemId);
810 }
811 int viewId = generateViewId();
812 mItemIdToViewId.put(itemId, viewId);
813 return viewId;
814 }
815
816 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700817 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
818 * a configuration step, this allows the proper animations to run after other transitions.
819 */
Adam Cohendb364c32014-05-20 14:23:40 -0700820 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700821 long screenId = args.screenId;
822 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
823 // When the screen id represents an actual screen (as opposed to a rank) we make sure
824 // that the drop page actually exists.
825 screenId = ensurePendingDropLayoutExists(args.screenId);
826 }
Adam Cohendb364c32014-05-20 14:23:40 -0700827
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800828 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800829 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700830 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700831 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700832 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800833 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700834 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700835 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700836 case REQUEST_RECONFIGURE_APPWIDGET:
837 completeRestoreAppWidget(args.appWidgetId);
838 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800839 }
Michael Jurka27614d22012-04-02 06:40:22 -0700840 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
841 // if you turned the screen off and then back while in All Apps, Launcher would not
842 // return to the workspace. Clearing mAddInfo.container here fixes this issue
843 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700844 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800845 }
846
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800847 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700848 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700849 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700850 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700851 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800852 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700853
Adam Cohenad4e15c2013-10-17 16:21:35 -0700854 Runnable exitSpringLoaded = new Runnable() {
855 @Override
856 public void run() {
857 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
858 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
859 }
860 };
861
Michael Jurka8b805b12012-04-18 14:23:14 -0700862 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700863 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700864 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
865 if (resultCode == RESULT_CANCELED) {
866 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700867 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700868 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700869 } else if (resultCode == RESULT_OK) {
Sunny Goyale6b63a32015-01-16 16:14:29 -0800870 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700871 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700872 }
873 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200874 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
875 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -0700876 showWorkspace(false);
Michael Jurka336fd4f2013-09-12 00:05:02 +0200877 }
878 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700879 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200880
Adam Cohened66b2b2012-01-23 17:28:51 -0800881 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
Sunny Goyal5c97f512015-05-19 16:03:28 -0700882 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700883
Adam Cohendb364c32014-05-20 14:23:40 -0700884 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800885 // We have special handling for widgets
886 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800887 final int appWidgetId;
888 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
889 : -1;
890 if (widgetId < 0) {
891 appWidgetId = pendingAddWidgetId;
892 } else {
893 appWidgetId = widgetId;
894 }
895
Adam Cohenad4e15c2013-10-17 16:21:35 -0700896 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800897 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700898 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
899 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700900 result = RESULT_CANCELED;
901 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700902 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700903 @Override
904 public void run() {
905 exitSpringLoadedDragModeDelayed(false, 0, null);
906 }
907 };
Adam Cohendb364c32014-05-20 14:23:40 -0700908 if (workspaceLocked) {
909 // No need to remove the empty screen if we're mid-binding, as the
910 // the bind will not add the empty screen.
911 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
912 } else {
913 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
914 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
915 }
Winson Chung5aaab772012-04-27 15:24:02 -0700916 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700917 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700918 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
919 // When the screen id represents an actual screen (as opposed to a rank)
920 // we make sure that the drop page actually exists.
921 mPendingAddInfo.screenId =
922 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
923 }
Adam Cohendb364c32014-05-20 14:23:40 -0700924 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
925
926 dropLayout.setDropPending(true);
927 final Runnable onComplete = new Runnable() {
928 @Override
929 public void run() {
930 completeTwoStageWidgetDrop(resultCode, appWidgetId);
931 dropLayout.setDropPending(false);
932 }
933 };
934 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
935 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
936 } else {
937 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
938 mPendingAddInfo);
939 sPendingAddItem = args;
940 }
Winson Chung5aaab772012-04-27 15:24:02 -0700941 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800942 return;
943 }
944
Sunny Goyalff572272014-07-23 13:58:07 -0700945 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
946 if (resultCode == RESULT_OK) {
947 // Update the widget view.
948 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
949 pendingAddWidgetId, mPendingAddInfo);
950 if (workspaceLocked) {
951 sPendingAddItem = args;
952 } else {
953 completeAdd(args);
954 }
955 }
956 // Leave the widget in the pending state if the user canceled the configure.
957 return;
958 }
959
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800960 // The pattern used here is that a user PICKs a specific application,
961 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700962
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800963 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
964 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700965 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700966 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
967 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800968 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700969 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800970 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700971 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700972 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
973 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 }
Adam Cohen00074722013-10-11 17:46:09 -0700975 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700976 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700977 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800978 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800979 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800980
981 }
982
983 @Override
984 protected void onActivityResult(
985 final int requestCode, final int resultCode, final Intent data) {
986 handleActivityResult(requestCode, resultCode, data);
987 if (mLauncherCallbacks != null) {
988 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
989 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800990 }
991
Adam Cohendb364c32014-05-20 14:23:40 -0700992 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
993 appWidgetId, ItemInfo info) {
994 PendingAddArguments args = new PendingAddArguments();
995 args.requestCode = requestCode;
996 args.intent = data;
997 args.container = info.container;
998 args.screenId = info.screenId;
999 args.cellX = info.cellX;
1000 args.cellY = info.cellY;
1001 args.appWidgetId = appWidgetId;
1002 return args;
1003 }
1004
1005 /**
1006 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
1007 *
1008 * @param screenId the screen id to check
1009 * @return the new screen, or screenId if it exists
1010 */
1011 private long ensurePendingDropLayoutExists(long screenId) {
1012 CellLayout dropLayout =
1013 (CellLayout) mWorkspace.getScreenWithId(screenId);
1014 if (dropLayout == null) {
1015 // it's possible that the add screen was removed because it was
1016 // empty and a re-bind occurred
1017 mWorkspace.addExtraEmptyScreen();
1018 return mWorkspace.commitExtraEmptyScreen();
1019 } else {
1020 return screenId;
1021 }
1022 }
1023
Adam Cohen091440a2015-03-18 14:16:05 -07001024 @Thunk void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -07001025 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -07001026 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001027 Runnable onCompleteRunnable = null;
1028 int animationType = 0;
1029
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001030 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -08001031 if (resultCode == RESULT_OK) {
1032 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
1033 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001034 mPendingAddWidgetInfo);
1035 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -08001036 onCompleteRunnable = new Runnable() {
1037 @Override
1038 public void run() {
1039 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -07001040 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001041 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
1042 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -08001043 }
1044 };
1045 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -08001046 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -08001047 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -08001048 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001049 if (mDragLayer.getAnimatedView() != null) {
1050 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
1051 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
1052 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -07001053 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001054 // The animated view may be null in the case of a rotation during widget configuration
1055 onCompleteRunnable.run();
1056 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001057 }
1058
1059 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -07001060 protected void onStop() {
1061 super.onStop();
1062 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -07001063
1064 if (mLauncherCallbacks != null) {
1065 mLauncherCallbacks.onStop();
1066 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001067 }
1068
1069 @Override
1070 protected void onStart() {
1071 super.onStart();
1072 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -07001073
1074 if (mLauncherCallbacks != null) {
1075 mLauncherCallbacks.onStart();
1076 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001077 }
1078
1079 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001080 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001081 long startTime = 0;
1082 if (DEBUG_RESUME_TIME) {
1083 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001084 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001085 }
Adam Cohen9211d422014-10-07 18:14:53 -07001086
1087 if (mLauncherCallbacks != null) {
1088 mLauncherCallbacks.preOnResume();
1089 }
1090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001091 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001092
Winson Chung4a2afa32012-07-19 14:53:05 -07001093 // Restore the previous launcher state
Winson Chung75cc8252015-04-10 10:19:58 -07001094 if (mOnResumeState == State.WORKSPACE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001095 showWorkspace(false);
Winson Chungb745afb2015-03-02 11:51:23 -08001096 } else if (mOnResumeState == State.APPS) {
Winson Chung13eb5272015-05-11 16:30:13 -07001097 boolean launchedFromApp = (mWaitingForResume != null);
Winson Chung4ac30062015-05-08 17:34:17 -07001098 // Don't update the predicted apps if the user is returning to launcher in the apps
Winson Chung13eb5272015-05-11 16:30:13 -07001099 // view after launching an app, as they may be depending on the UI to be static to
1100 // switch to another app, otherwise, if it was
Winson Chung4ac30062015-05-08 17:34:17 -07001101 showAppsView(false /* animated */, false /* resetListToTop */,
Winson Chung13eb5272015-05-11 16:30:13 -07001102 !launchedFromApp /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08001103 } else if (mOnResumeState == State.WIDGETS) {
1104 showWidgetsView(false, false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001105 }
1106 mOnResumeState = State.NONE;
1107
Winson Chungcd99cd32015-04-29 11:03:24 -07001108 // Restore the apps state if we are in all apps
Winson Chung4ac30062015-05-08 17:34:17 -07001109 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
1110 // Otherwise, notify the callbacks if we are in all apps mode
1111 if (mState == State.APPS) {
1112 if (mLauncherCallbacks != null) {
1113 mLauncherCallbacks.onAllAppsShown();
1114 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001115 }
1116 }
1117
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07001118 // Background was set to gradient in onPause(), restore to transparent if in all apps.
Sunny Goyald70ad0d2015-05-26 17:20:07 -07001119 setWorkspaceBackground(mState == State.WORKSPACE ? WORKSPACE_BACKGROUND_GRADIENT
1120 : WORKSPACE_BACKGROUND_TRANSPARENT);
Craig Mautner360310b2012-10-26 15:13:08 -07001121
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001122 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001123 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001124 setWorkspaceLoading(true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001125 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001126 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001127 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001128 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001129 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001130 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1131 // execute them here
1132 long startTimeCallbacks = 0;
1133 if (DEBUG_RESUME_TIME) {
1134 startTimeCallbacks = System.currentTimeMillis();
1135 }
1136
Michael Jurka1e2f4652013-07-08 18:03:46 -07001137 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1138 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001139 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001140 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001141 if (DEBUG_RESUME_TIME) {
1142 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1143 (System.currentTimeMillis() - startTimeCallbacks));
1144 }
Michael Jurka447bf842013-05-15 14:52:15 +02001145 }
Michael Jurka54554252013-08-01 12:52:23 +02001146 if (mOnResumeCallbacks.size() > 0) {
1147 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1148 mOnResumeCallbacks.get(i).run();
1149 }
1150 mOnResumeCallbacks.clear();
1151 }
Winson Chunge4e50662012-01-23 14:45:13 -08001152
1153 // Reset the pressed state of icons that were locked in the press state while activities
1154 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001155 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001156 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001157 mWaitingForResume.setStayPressed(false);
1158 }
Winson Chung82963d52013-10-09 11:20:57 -07001159
Adam Cohen06dff352012-06-01 17:17:08 -07001160 // It is possible that widgets can receive updates while launcher is not in the foreground.
1161 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1162 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1163 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001164 getWorkspace().reinflateWidgetsIfNecessary();
Sunny Goyal22235bc2015-04-03 09:23:43 -07001165 reinflateQSBIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001166
Michael Jurka447bf842013-05-15 14:52:15 +02001167 if (DEBUG_RESUME_TIME) {
1168 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1169 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001170
1171 if (mWorkspace.getCustomContentCallbacks() != null) {
1172 // If we are resuming and the custom content is the current page, we call onShow().
1173 // It is also poassible that onShow will instead be called slightly after first layout
1174 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1175 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001176 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001177 }
1178 }
Winson Chungcd99cd32015-04-29 11:03:24 -07001179 updateInteraction(Workspace.State.NORMAL, mWorkspace.getState());
Adam Cohen53805212013-10-01 10:39:23 -07001180 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001181
Sunny Goyal756adbc2015-04-16 15:20:51 -07001182 if (!isWorkspaceLoading()) {
1183 // Process any items that were added while Launcher was away.
1184 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1185 }
Adam Cohen9211d422014-10-07 18:14:53 -07001186
1187 if (mLauncherCallbacks != null) {
1188 mLauncherCallbacks.onResume();
1189 }
Adam Cohen06dff352012-06-01 17:17:08 -07001190 }
1191
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001192 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001193 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001194 // Ensure that items added to Launcher are queued until Launcher returns
1195 InstallShortcutReceiver.enableInstallQueue();
1196
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001197 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001198 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001199 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001200 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001201
1202 // We call onHide() aggressively. The custom content callbacks should be able to
1203 // debounce excess onHide calls.
1204 if (mWorkspace.getCustomContentCallbacks() != null) {
1205 mWorkspace.getCustomContentCallbacks().onHide();
1206 }
Romain Guycbb89e42009-06-08 15:52:54 -07001207
Adam Cohen9211d422014-10-07 18:14:53 -07001208 if (mLauncherCallbacks != null) {
1209 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001210 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001211 }
1212
1213 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001214 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1215 // by a onResume or by scrolling otherwise.
1216 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001217
1218 // Custom content is completely hidden
1219 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001220
1221 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1222 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001223
1224 // Indicates whether the user is allowed to scroll away from the custom content.
1225 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001226 }
1227
Adam Cohenc2d6e892014-10-16 09:49:24 -07001228 public interface LauncherOverlay {
1229
1230 /**
1231 * Touch interaction leading to overscroll has begun
1232 */
1233 public void onScrollInteractionBegin();
1234
1235 /**
1236 * Touch interaction related to overscroll has ended
1237 */
1238 public void onScrollInteractionEnd();
1239
1240 /**
1241 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1242 * screen (or in the case of RTL, the rightmost screen).
1243 */
1244 public void onScrollChange(int progress, boolean rtl);
1245
1246 /**
1247 * Screen has stopped scrolling
1248 */
1249 public void onScrollSettled();
1250
1251 /**
1252 * This method can be called by the Launcher in order to force the LauncherOverlay
1253 * to exit fully immersive mode.
1254 */
1255 public void forceExitFullImmersion();
1256 }
1257
Winson Chung0f785722015-04-08 10:27:49 -07001258 public interface LauncherAppsCallbacks {
1259 /**
1260 * Updates launcher to the available space that AllApps can take so as not to overlap with
1261 * any other views.
1262 */
1263 public void onAllAppsBoundsChanged(Rect bounds);
1264
1265 /**
1266 * Called to dismiss all apps if it is showing.
1267 */
1268 public void dismissAllApps();
1269 }
1270
Jun Mukai8af0cd82015-05-12 12:32:12 -07001271 public interface LauncherSearchCallbacks {
1272 /**
1273 * Called when the search overlay is shown.
1274 */
1275 public void onSearchOverlayOpened();
1276
1277 /**
1278 * Called when the search overlay is dismissed.
1279 */
1280 public void onSearchOverlayClosed();
1281 }
1282
Adam Cohenc2d6e892014-10-16 09:49:24 -07001283 public interface LauncherOverlayCallbacks {
1284 /**
1285 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1286 * however it doesn't modify any state within the launcher.
1287 */
1288 public boolean canEnterFullImmersion();
1289
1290 /**
1291 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1292 * eg. by occupying the full screen and handling all touch events.
1293 *
1294 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1295 * case, Launcher will modify any necessary state and assumes the overlay is
1296 * handling all interaction. If false, the LauncherOverlay should cancel any
1297 *
1298 */
1299 public boolean enterFullImmersion();
1300
1301 /**
1302 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1303 * full control over UI and state.
1304 */
1305 public void exitFullImmersion();
1306 }
1307
1308 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1309
1310 @Override
1311 public boolean canEnterFullImmersion() {
1312 return mState == State.WORKSPACE;
1313 }
1314
1315 @Override
1316 public boolean enterFullImmersion() {
1317 if (mState == State.WORKSPACE) {
1318 // When fully immersed, disregard any touches which fall through.
1319 mDragLayer.setBlockTouch(true);
1320 return true;
1321 }
1322 return false;
1323 }
1324
1325 @Override
1326 public void exitFullImmersion() {
1327 mDragLayer.setBlockTouch(false);
1328 }
1329 }
1330
Jorim Jaggid017f882014-01-14 17:08:48 -08001331 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001332 if (mLauncherCallbacks != null) {
1333 return mLauncherCallbacks.hasSettings();
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07001334 } else {
1335 // On devices with a locked orientation, we will at least have the allow rotation
1336 // setting.
1337 return !Utilities.isRotationAllowedForDevice(this);
Adam Cohen9211d422014-10-07 18:14:53 -07001338 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001339 }
1340
Adam Cohen9211d422014-10-07 18:14:53 -07001341 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001342 CustomContentCallbacks callbacks, String description) {
1343 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001344 }
1345
Adam Cohenedb40762013-07-18 16:45:45 -07001346 // The custom content needs to offset its content to account for the QSB
1347 public int getTopOffsetForCustomContent() {
1348 return mWorkspace.getPaddingTop();
1349 }
1350
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001351 @Override
1352 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001353 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001354 if (mModel.isCurrentCallbacks(this)) {
1355 mModel.stopLoader();
1356 }
Hyunyoung Song3f471442015-04-08 19:01:34 -07001357 //TODO(hyunyoungs): stop the widgets loader when there is a rotation.
1358
Romain Guy13c2e7b2010-03-10 19:45:00 -08001359 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001360 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001361
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001362 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001363 @Override
1364 public void onWindowFocusChanged(boolean hasFocus) {
1365 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001366 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001367
1368 if (mLauncherCallbacks != null) {
1369 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1370 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001371 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001372
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001373 private boolean acceptFilter() {
1374 final InputMethodManager inputManager = (InputMethodManager)
1375 getSystemService(Context.INPUT_METHOD_SERVICE);
1376 return !inputManager.isFullscreenMode();
1377 }
1378
1379 @Override
1380 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001381 final int uniChar = event.getUnicodeChar();
1382 final boolean handled = super.onKeyDown(keyCode, event);
1383 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1384 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001385 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1386 keyCode, event);
1387 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001388 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001389 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001390 // showSearchDialog()
1391 // If there are multiple keystrokes before the search dialog takes focus,
1392 // onSearchRequested() will be called for every keystroke,
1393 // but it is idempotent, so it's fine.
1394 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395 }
1396 }
1397
Joe Onorato8a9625e2010-01-28 15:55:35 -08001398 // Eat the long press event so the keyboard doesn't come up.
1399 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1400 return true;
1401 }
1402
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001403 return handled;
1404 }
1405
Karl Rosaen138a0412009-04-23 19:00:21 -07001406 private String getTypedText() {
1407 return mDefaultKeySsb.toString();
1408 }
1409
1410 private void clearTypedText() {
1411 mDefaultKeySsb.clear();
1412 mDefaultKeySsb.clearSpans();
1413 Selection.setSelection(mDefaultKeySsb, 0);
1414 }
1415
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001416 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001417 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1418 * State
1419 */
1420 private static State intToState(int stateOrdinal) {
1421 State state = State.WORKSPACE;
1422 final State[] stateValues = State.values();
1423 for (int i = 0; i < stateValues.length; i++) {
1424 if (stateValues[i].ordinal() == stateOrdinal) {
1425 state = stateValues[i];
1426 break;
1427 }
1428 }
1429 return state;
1430 }
1431
1432 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001433 * Restores the previous state, if it exists.
1434 *
1435 * @param savedState The previous state.
1436 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001437 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 private void restoreState(Bundle savedState) {
1439 if (savedState == null) {
1440 return;
1441 }
1442
Michael Jurka883f55b2010-10-21 15:47:14 -07001443 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungb745afb2015-03-02 11:51:23 -08001444 if (state == State.APPS || state == State.WIDGETS) {
1445 mOnResumeState = state;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001446 }
1447
Adam Cohen21cd0022013-10-09 18:57:02 -07001448 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1449 PagedView.INVALID_RESTORE_PAGE);
1450 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001451 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 }
1453
Winson Chung3d503fb2011-07-13 17:25:49 -07001454 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001455 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001456
Winson Chung3d503fb2011-07-13 17:25:49 -07001457 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1458 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001459 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001460 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1461 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001462 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1463 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
Adam Cohenb823ae42015-03-27 18:07:52 -07001464 AppWidgetProviderInfo info = savedState.getParcelable(
1465 RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Sunny Goyalf044bb12015-05-12 13:30:07 -07001466 mPendingAddWidgetInfo = info == null ?
1467 null : LauncherAppWidgetProviderInfo.fromProviderInfo(this, info);
1468
Adam Cohen4637b5a2013-11-04 18:21:24 -08001469 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001470 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001471 mRestoring = true;
1472 }
1473
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001474 mItemIdToViewId = (HashMap<Integer, Integer>)
1475 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001476 }
1477
1478 /**
1479 * Finds all the views we need and configure them properly.
1480 */
1481 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001482 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001483
Craig Mautner360310b2012-10-26 15:13:08 -07001484 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001485 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001486 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1487 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001488 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001489 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001490
John Spurlock77e1f472013-09-11 10:09:51 -04001491 mLauncherView.setSystemUiVisibility(
1492 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001493 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001494
Winson Chung4c98d922011-05-31 16:50:48 -07001495 // Setup the drag layer
1496 mDragLayer.setup(this, dragController);
1497
Winson Chung3d503fb2011-07-13 17:25:49 -07001498 // Setup the hotseat
1499 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1500 if (mHotseat != null) {
Winson Chung11a1a532013-09-13 11:14:45 -07001501 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001502 }
1503
Jorim Jaggid017f882014-01-14 17:08:48 -08001504 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001505 View widgetButton = findViewById(R.id.widget_button);
1506 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001507 @Override
1508 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001509 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001510 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001511 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001512 }
1513 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001514 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1515
1516 View wallpaperButton = findViewById(R.id.wallpaper_button);
1517 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001518 @Override
1519 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001520 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001521 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001522 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001523 }
1524 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001525 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1526
1527 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001528 if (hasSettings()) {
1529 settingsButton.setOnClickListener(new OnClickListener() {
1530 @Override
1531 public void onClick(View arg0) {
1532 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001533 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001534 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001535 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001536 });
1537 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1538 } else {
1539 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001540 }
1541
Adam Cohendbdff6b2013-09-18 19:09:15 -07001542 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001543
Winson Chung4c98d922011-05-31 16:50:48 -07001544 // Setup the workspace
1545 mWorkspace.setHapticFeedbackEnabled(false);
1546 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001547 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001548 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001549
Winson Chungf0ea4d32011-06-06 14:27:16 -07001550 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001551 mSearchDropTargetBar = (SearchDropTargetBar)
1552 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001553
Winson Chungb745afb2015-03-02 11:51:23 -08001554 // Setup Apps
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001555 mAppsView = (AllAppsContainerView) findViewById(R.id.apps_view);
Winson Chung83f59ab2015-05-05 17:21:58 -07001556 if (isAllAppsSearchOverridden()) {
1557 mAppsView.hideHeaderBar();
Winson Chung0f785722015-04-08 10:27:49 -07001558 }
Winson Chungb745afb2015-03-02 11:51:23 -08001559
Winson Chungf0ea4d32011-06-06 14:27:16 -07001560 // Setup AppsCustomize
Hyunyoung Song3f471442015-04-08 19:01:34 -07001561 mWidgetsView = (WidgetsContainerView) findViewById(R.id.widgets_view);
Craig Mautner360310b2012-10-26 15:13:08 -07001562
Winson Chung3d503fb2011-07-13 17:25:49 -07001563 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001564 dragController.setDragScoller(mWorkspace);
1565 dragController.setScrollView(mDragLayer);
1566 dragController.setMoveTarget(mWorkspace);
1567 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001568 if (mSearchDropTargetBar != null) {
1569 mSearchDropTargetBar.setup(this, dragController);
Adam Cohenb0df1b02015-03-18 22:21:40 -07001570 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001571 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001572
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001573 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001574 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001575 mWeightWatcher = new WeightWatcher(this);
1576 mWeightWatcher.setAlpha(0.5f);
1577 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001578 new FrameLayout.LayoutParams(
1579 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001580 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001581 Gravity.BOTTOM)
1582 );
Adam Cohen39a06042013-07-19 14:30:12 -07001583
1584 boolean show = shouldShowWeightWatcher();
1585 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001586 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001587 }
1588
1589 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001590 * Sets the all apps button. This method is called from {@link Hotseat}.
1591 */
1592 public void setAllAppsButton(View allAppsButton) {
1593 mAllAppsButton = allAppsButton;
1594 }
1595
1596 public View getAllAppsButton() {
1597 return mAllAppsButton;
1598 }
1599
1600 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 * Creates a view representing a shortcut.
1602 *
1603 * @param info The data structure describing the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001604 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001605 View createShortcut(ShortcutInfo info) {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001606 return createShortcut((ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001607 }
1608
1609 /**
1610 * Creates a view representing a shortcut inflated from the specified resource.
1611 *
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 * @param parent The group the shortcut belongs to.
1613 * @param info The data structure describing the shortcut.
1614 *
1615 * @return A View inflated from layoutResId.
1616 */
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001617 public View createShortcut(ViewGroup parent, ShortcutInfo info) {
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001618 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(R.layout.app_icon,
Sunny Goyaldfaccf62015-05-11 16:30:44 -07001619 parent, false);
1620 favorite.applyFromShortcutInfo(info, mIconCache);
1621 favorite.setCompoundDrawablePadding(mDeviceProfile.iconDrawablePaddingPx);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001623 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 return favorite;
1625 }
1626
1627 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001628 * Add a shortcut to the workspace.
1629 *
1630 * @param data The intent describing the shortcut.
1631 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001633 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001634 int cellY) {
1635 int[] cellXY = mTmpAddItemCellCoordinates;
1636 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001637 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001638
Sunny Goyal5c97f512015-05-19 16:03:28 -07001639 ShortcutInfo info = InstallShortcutReceiver.fromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001640 if (info == null) {
1641 return;
1642 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001643 final View view = createShortcut(info);
1644
Sunny Goyal5c97f512015-05-19 16:03:28 -07001645 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001646 // First we check if we already know the exact location where we want to add this item.
1647 if (cellX >= 0 && cellY >= 0) {
1648 cellXY[0] = cellX;
1649 cellXY[1] = cellY;
1650 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001651
1652 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001653 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001654 true, null,null)) {
1655 return;
1656 }
1657 DragObject dragObject = new DragObject();
1658 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001659 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1660 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001661 return;
1662 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001663 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001664 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001665 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001666 foundCellSpan = (result != null);
1667 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001668 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001669 }
1670
1671 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001672 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001673 return;
1674 }
1675
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001676 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1]);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001677
1678 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001679 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001680 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681 }
1682 }
1683
Adam Cohen2e6da152015-05-06 11:42:25 -07001684 private int[] getSpanForWidget(ComponentName component, int minWidth, int minHeight) {
1685 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(this, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001686 // We want to account for the extra amount of padding that we are adding to the widget
1687 // to ensure that it gets the full amount of space that it has requested
1688 int requiredWidth = minWidth + padding.left + padding.right;
1689 int requiredHeight = minHeight + padding.top + padding.bottom;
Adam Cohen2e6da152015-05-06 11:42:25 -07001690 return CellLayout.rectToCell(this, requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001691 }
1692
Adam Cohen2e6da152015-05-06 11:42:25 -07001693 public int[] getSpanForWidget(AppWidgetProviderInfo info) {
1694 return getSpanForWidget(info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001695 }
1696
Adam Cohen2e6da152015-05-06 11:42:25 -07001697 public int[] getMinSpanForWidget(AppWidgetProviderInfo info) {
1698 return getSpanForWidget(info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001699 }
1700
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001701 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001702 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001703 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001704 * @param appWidgetId The app widget id
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001705 */
Adam Cohen091440a2015-03-18 14:16:05 -07001706 @Thunk void completeAddAppWidget(int appWidgetId, long container, long screenId,
Adam Cohen59400422014-03-05 18:07:04 -08001707 AppWidgetHostView hostView, LauncherAppWidgetProviderInfo appWidgetInfo) {
1708
1709 ItemInfo info = mPendingAddInfo;
Adam Cohened66b2b2012-01-23 17:28:51 -08001710 if (appWidgetInfo == null) {
Adam Cohen59400422014-03-05 18:07:04 -08001711 appWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(this,
1712 mAppWidgetManager.getAppWidgetInfo(appWidgetId));
Adam Cohened66b2b2012-01-23 17:28:51 -08001713 }
Romain Guycbb89e42009-06-08 15:52:54 -07001714
Adam Cohen59400422014-03-05 18:07:04 -08001715 if (appWidgetInfo.isCustomWidget) {
1716 appWidgetId = LauncherAppWidgetInfo.CUSTOM_WIDGET_ID;
Michael Jurkaa63c4522010-08-19 13:52:27 -07001717 }
1718
Adam Cohen59400422014-03-05 18:07:04 -08001719 LauncherAppWidgetInfo launcherInfo;
1720 launcherInfo = new LauncherAppWidgetInfo(appWidgetId, appWidgetInfo.provider);
1721 launcherInfo.spanX = info.spanX;
1722 launcherInfo.spanY = info.spanY;
1723 launcherInfo.minSpanX = info.minSpanX;
1724 launcherInfo.minSpanY = info.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001725 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001726
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001727 LauncherModel.addItemToDatabase(this, launcherInfo,
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07001728 container, screenId, info.cellX, info.cellY);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001729
1730 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001731 if (hostView == null) {
1732 // Perform actual inflation because we're live
Adam Cohen59400422014-03-05 18:07:04 -08001733 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId,
1734 appWidgetInfo);
Adam Cohened66b2b2012-01-23 17:28:51 -08001735 } else {
1736 // The AppWidgetHostView has already been inflated and instantiated
1737 launcherInfo.hostView = hostView;
1738 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001739 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001740 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001741 launcherInfo.notifyWidgetSizeChanged(this);
1742
Adam Cohen59400422014-03-05 18:07:04 -08001743 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, info.cellX,
1744 info.cellY, launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001745
1746 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001747 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001748 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001749 }
Romain Guycbb89e42009-06-08 15:52:54 -07001750
Adam Cohended9f8d2010-11-03 13:25:16 -07001751 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1752 @Override
1753 public void onReceive(Context context, Intent intent) {
1754 final String action = intent.getAction();
1755 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1756 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001757 mDragLayer.clearAllResizeFrames();
Winson Chungb745afb2015-03-02 11:51:23 -08001758 updateAutoAdvanceState();
Winson Chung337cd9d2011-03-30 10:39:30 -07001759
Winson Chungc100e8e2011-08-09 16:02:43 -07001760 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001761 // processing a multi-step drop
Hyunyoung Song3f471442015-04-08 19:01:34 -07001762 if (mAppsView != null && mWidgetsView != null &&
Winson Chungb745afb2015-03-02 11:51:23 -08001763 mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001764 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001765 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001766 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1767 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08001768 updateAutoAdvanceState();
Dan Sandlerd5024042014-01-09 15:01:33 -05001769 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1770 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001771 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001772 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1773 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1774 mModel.resetLoadedState(false, true);
Sunny Goyal2bba4c32015-05-18 15:42:48 -07001775 mModel.startLoader(PagedView.INVALID_RESTORE_PAGE,
Dan Sandlerd5024042014-01-09 15:01:33 -05001776 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1777 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Adam Cohended9f8d2010-11-03 13:25:16 -07001778 }
1779 }
1780 };
1781
1782 @Override
1783 public void onAttachedToWindow() {
1784 super.onAttachedToWindow();
1785
1786 // Listen for broadcasts related to user-presence
1787 final IntentFilter filter = new IntentFilter();
1788 filter.addAction(Intent.ACTION_SCREEN_OFF);
1789 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001790 // For handling managed profiles
Dan Sandlerd5024042014-01-09 15:01:33 -05001791 if (ENABLE_DEBUG_INTENTS) {
1792 filter.addAction(DebugIntents.DELETE_DATABASE);
1793 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1794 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001795 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001796 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001797 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001798 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001799 mVisible = true;
1800 }
1801
Adam Cohen0b395352014-06-09 22:54:36 +00001802 /**
1803 * Sets up transparent navigation and status bars in LMP.
1804 * This method is a no-op for other platform versions.
1805 */
Sunny Goyald0091012015-01-20 15:55:34 -08001806 @TargetApi(Build.VERSION_CODES.LOLLIPOP)
Adam Cohen0b395352014-06-09 22:54:36 +00001807 private void setupTransparentSystemBarsForLmp() {
Kenny Guyd794a3f2014-09-16 15:17:58 +01001808 if (Utilities.isLmpOrAbove()) {
Sunny Goyald0091012015-01-20 15:55:34 -08001809 Window window = getWindow();
1810 window.getAttributes().systemUiVisibility |=
1811 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1812 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1813 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1814 window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1815 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1816 window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
1817 window.setStatusBarColor(Color.TRANSPARENT);
1818 window.setNavigationBarColor(Color.TRANSPARENT);
Adam Cohen0b395352014-06-09 22:54:36 +00001819 }
1820 }
1821
Adam Cohended9f8d2010-11-03 13:25:16 -07001822 @Override
1823 public void onDetachedFromWindow() {
1824 super.onDetachedFromWindow();
1825 mVisible = false;
1826
Adam Cohend113e0c2010-11-11 10:48:05 -08001827 if (mAttached) {
1828 unregisterReceiver(mReceiver);
1829 mAttached = false;
1830 }
Winson Chungb745afb2015-03-02 11:51:23 -08001831 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001832 }
1833
1834 public void onWindowVisibilityChanged(int visibility) {
1835 mVisible = visibility == View.VISIBLE;
Winson Chungb745afb2015-03-02 11:51:23 -08001836 updateAutoAdvanceState();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001837 // The following code used to be in onResume, but it turns out onResume is called when
1838 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1839 // is a more appropriate event to handle
1840 if (mVisible) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001841 if (!mWorkspaceLoading) {
1842 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001843 // We want to let Launcher draw itself at least once before we force it to build
1844 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001845 // apps is nice and speedy.
1846 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001847 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001848 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001849 if (mStarted) return;
1850 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001851 // We delay the layer building a bit in order to give
1852 // other message processing a time to run. In particular
1853 // this avoids a delay in hiding the IME if it was
1854 // currently shown, because doing that may involve
1855 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001856 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001857 final ViewTreeObserver.OnDrawListener listener = this;
1858 mWorkspace.post(new Runnable() {
1859 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001860 if (mWorkspace != null &&
1861 mWorkspace.getViewTreeObserver() != null) {
1862 mWorkspace.getViewTreeObserver().
1863 removeOnDrawListener(listener);
1864 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001865 }
1866 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001867 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001868 }
1869 });
1870 }
1871 clearTypedText();
1872 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001873 }
1874
Adam Cohen091440a2015-03-18 14:16:05 -07001875 @Thunk void sendAdvanceMessage(long delay) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001876 mHandler.removeMessages(ADVANCE_MSG);
1877 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1878 mHandler.sendMessageDelayed(msg, delay);
1879 mAutoAdvanceSentTime = System.currentTimeMillis();
1880 }
1881
Adam Cohen091440a2015-03-18 14:16:05 -07001882 @Thunk void updateAutoAdvanceState() {
Adam Cohended9f8d2010-11-03 13:25:16 -07001883 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1884 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1885 mAutoAdvanceRunning = autoAdvanceRunning;
1886 if (autoAdvanceRunning) {
1887 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1888 sendAdvanceMessage(delay);
1889 } else {
1890 if (!mWidgetsToAdvance.isEmpty()) {
1891 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1892 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1893 }
1894 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001895 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001896 }
1897 }
1898 }
1899
1900 private final Handler mHandler = new Handler() {
1901 @Override
1902 public void handleMessage(Message msg) {
1903 if (msg.what == ADVANCE_MSG) {
1904 int i = 0;
1905 for (View key: mWidgetsToAdvance.keySet()) {
1906 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1907 final int delay = mAdvanceStagger * i;
1908 if (v instanceof Advanceable) {
1909 postDelayed(new Runnable() {
1910 public void run() {
1911 ((Advanceable) v).advance();
1912 }
1913 }, delay);
1914 }
1915 i++;
1916 }
1917 sendAdvanceMessage(mAdvanceInterval);
1918 }
1919 }
1920 };
1921
1922 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001923 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001924 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1925 if (v instanceof Advanceable) {
1926 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001927 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Winson Chungb745afb2015-03-02 11:51:23 -08001928 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001929 }
1930 }
1931
1932 void removeWidgetToAutoAdvance(View hostView) {
1933 if (mWidgetsToAdvance.containsKey(hostView)) {
1934 mWidgetsToAdvance.remove(hostView);
Winson Chungb745afb2015-03-02 11:51:23 -08001935 updateAutoAdvanceState();
Adam Cohended9f8d2010-11-03 13:25:16 -07001936 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001937 }
1938
Joe Onorato9c1289c2009-08-17 11:03:03 -04001939 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001940 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001941 launcherInfo.hostView = null;
1942 }
1943
Hyunyoung Song3f471442015-04-08 19:01:34 -07001944 public void showOutOfSpaceMessage(boolean isHotseatLayout) {
Winson Chung93eef082012-03-23 15:59:27 -07001945 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1946 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001947 }
1948
Winson Chunga6945242014-01-08 14:04:34 -08001949 public DragLayer getDragLayer() {
1950 return mDragLayer;
1951 }
1952
Winson Chung5f4e0fd2015-05-22 11:12:27 -07001953 public AllAppsContainerView getAppsView() {
Winson Chungb745afb2015-03-02 11:51:23 -08001954 return mAppsView;
1955 }
1956
Hyunyoung Song3f471442015-04-08 19:01:34 -07001957 public WidgetsContainerView getWidgetsView() {
1958 return mWidgetsView;
Winson Chungb745afb2015-03-02 11:51:23 -08001959 }
1960
Winson Chunga6945242014-01-08 14:04:34 -08001961 public Workspace getWorkspace() {
1962 return mWorkspace;
1963 }
1964
1965 public Hotseat getHotseat() {
1966 return mHotseat;
1967 }
1968
Jorim Jaggid017f882014-01-14 17:08:48 -08001969 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001970 return mOverviewPanel;
1971 }
1972
1973 public SearchDropTargetBar getSearchBar() {
1974 return mSearchDropTargetBar;
1975 }
1976
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001977 public LauncherAppWidgetHost getAppWidgetHost() {
1978 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001979 }
Romain Guycbb89e42009-06-08 15:52:54 -07001980
Winson Chunga9abd0e2010-10-27 17:18:37 -07001981 public LauncherModel getModel() {
1982 return mModel;
1983 }
1984
Winson Chunga6945242014-01-08 14:04:34 -08001985 protected SharedPreferences getSharedPrefs() {
1986 return mSharedPrefs;
1987 }
1988
Adam Cohen2e6da152015-05-06 11:42:25 -07001989 public DeviceProfile getDeviceProfile() {
1990 return mDeviceProfile;
1991 }
1992
Bjorn Bringertc459e522013-06-07 19:36:01 +01001993 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001994 getWindow().closeAllPanels();
1995
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001996 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001997 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001998 }
1999
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 @Override
2001 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02002002 long startTime = 0;
2003 if (DEBUG_RESUME_TIME) {
2004 startTime = System.currentTimeMillis();
2005 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002006 super.onNewIntent(intent);
2007
2008 // Close the menu
2009 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08002010 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002011 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07002012
Adam Cohened307df2013-10-02 09:37:31 -07002013 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
2014 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
2015 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07002016
Adam Cohen6fecd412013-10-02 17:41:50 -07002017 if (mWorkspace == null) {
2018 // Can be cases where mWorkspace is null, this prevents a NPE
2019 return;
2020 }
2021 Folder openFolder = mWorkspace.getOpenFolder();
2022 // In all these cases, only animate if we're already on home
2023 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07002024
2025 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
2026 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07002027 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07002028 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07002029 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07002030 }
Romain Guy1dd3a072009-07-16 13:21:01 -07002031
Adam Cohen6fecd412013-10-02 17:41:50 -07002032 closeFolder();
2033 exitSpringLoadedDragMode();
2034
2035 // If we are already on home, then just animate back to the workspace,
2036 // otherwise, just wait until onResume to set the state back to Workspace
2037 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07002038 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07002039 } else {
2040 mOnResumeState = State.WORKSPACE;
2041 }
2042
2043 final View v = getWindow().peekDecorView();
2044 if (v != null && v.getWindowToken() != null) {
2045 InputMethodManager imm = (InputMethodManager)getSystemService(
2046 INPUT_METHOD_SERVICE);
2047 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
2048 }
2049
Winson Chungb745afb2015-03-02 11:51:23 -08002050 // Reset the apps view
2051 if (!alreadyOnHome && mAppsView != null) {
2052 mAppsView.scrollToTop();
2053 }
2054
Hyunyoung Song3f471442015-04-08 19:01:34 -07002055 // Reset the widgets view
2056 if (!alreadyOnHome && mWidgetsView != null) {
2057 mWidgetsView.scrollToTop();
Adam Cohen6fecd412013-10-02 17:41:50 -07002058 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002059
Adam Cohen9211d422014-10-07 18:14:53 -07002060 if (mLauncherCallbacks != null) {
2061 mLauncherCallbacks.onHomeIntent();
2062 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063 }
Adam Cohened307df2013-10-02 09:37:31 -07002064
Michael Jurka447bf842013-05-15 14:52:15 +02002065 if (DEBUG_RESUME_TIME) {
2066 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
2067 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002068
Adam Cohen9211d422014-10-07 18:14:53 -07002069 if (mLauncherCallbacks != null) {
2070 mLauncherCallbacks.onNewIntent(intent);
2071 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002072 }
2073
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07002075 public void onRestoreInstanceState(Bundle state) {
2076 super.onRestoreInstanceState(state);
2077 for (int page: mSynchronouslyBoundPages) {
2078 mWorkspace.restoreInstanceStateForChild(page);
2079 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002080 }
2081
2082 @Override
2083 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002084 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002085 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2086 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002087 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002088 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089
Michael Jurka883f55b2010-10-21 15:47:14 -07002090 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002091 // We close any open folder since it will not be re-opened, and we need to make sure
2092 // this state is reflected.
2093 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002094
Adam Cohendcd297f2013-06-18 13:13:40 -07002095 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002096 mWaitingForResult) {
2097 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002098 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002099 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2100 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002101 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2102 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2103 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002104 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002105 }
2106
Hyunyoung Song3f471442015-04-08 19:01:34 -07002107 // Save the current widgets tray?
2108 // TODO(hyunyoungs)
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002109 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002110
2111 if (mLauncherCallbacks != null) {
2112 mLauncherCallbacks.onSaveInstanceState(outState);
2113 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 }
2115
2116 @Override
2117 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002119
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002120 if (mPreferenceObserverRegistered) {
2121 getSharedPreferences(LauncherFiles.ROTATION_PREF_FILE,
2122 Context.MODE_MULTI_PROCESS).unregisterOnSharedPreferenceChangeListener(
2123 mSettingsObserver);
2124 mPreferenceObserverRegistered = false;
2125 }
2126
Winson Chunge7a03942011-08-05 15:05:12 -07002127 // Remove all pending runnables
2128 mHandler.removeMessages(ADVANCE_MSG);
2129 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002130 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002131
Winson Chungcd2b0142011-06-08 16:02:26 -07002132 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002133 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002134
2135 // It's possible to receive onDestroy after a new Launcher activity has
2136 // been created. In this case, don't interfere with the new Launcher.
2137 if (mModel.isCurrentCallbacks(this)) {
2138 mModel.stopLoader();
2139 app.setLauncher(null);
2140 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002141
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002142 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002143 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002144 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002145 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002146 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002147 mAppWidgetHost = null;
2148
2149 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002150
2151 TextKeyListener.getInstance().release();
2152
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002153 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002154 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002155
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002156 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002157 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002158 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002159 mWorkspace = null;
2160 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002161
Michael Jurka2ecf9952012-06-18 12:52:28 -07002162 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002163
2164 if (mLauncherCallbacks != null) {
2165 mLauncherCallbacks.onDestroy();
2166 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002167 }
2168
Adam Cohena9cf38f2011-05-02 15:36:58 -07002169 public DragController getDragController() {
2170 return mDragController;
2171 }
2172
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173 @Override
2174 public void startActivityForResult(Intent intent, int requestCode) {
Adam Cohen173f7112015-03-27 15:14:00 -07002175 onStartForResult(requestCode);
2176 super.startActivityForResult(intent, requestCode);
2177 }
2178
2179 @Override
2180 public void startIntentSenderForResult (IntentSender intent, int requestCode,
2181 Intent fillInIntent, int flagsMask, int flagsValues, int extraFlags, Bundle options) {
2182 onStartForResult(requestCode);
2183 try {
2184 super.startIntentSenderForResult(intent, requestCode,
2185 fillInIntent, flagsMask, flagsValues, extraFlags, options);
2186 } catch (IntentSender.SendIntentException e) {
2187 throw new ActivityNotFoundException();
2188 }
2189 }
2190
2191 private void onStartForResult(int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002192 if (requestCode >= 0) {
2193 setWaitingForResult(true);
2194 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002195 }
2196
Winson Chung70d72102011-08-12 11:24:35 -07002197 /**
2198 * Indicates that we want global search for this activity by setting the globalSearch
2199 * argument for {@link #startSearch} to true.
2200 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002201 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002202 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002204
Karl Rosaen138a0412009-04-23 19:00:21 -07002205 if (initialQuery == null) {
2206 // Use any text typed in the launcher as the initial query
2207 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002208 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002209 if (appSearchData == null) {
2210 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002211 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002212 }
Winson Chungadf0c182012-08-23 14:59:07 -07002213 Rect sourceBounds = new Rect();
2214 if (mSearchDropTargetBar != null) {
2215 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2216 }
Romain Guycbb89e42009-06-08 15:52:54 -07002217
Ian Parkinson047036e2014-09-01 15:40:53 +01002218 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002219 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002220 if (clearTextImmediately) {
2221 clearTypedText();
2222 }
Winson Chungcd99cd32015-04-29 11:03:24 -07002223
2224 // We need to show the workspace after starting the search
2225 showWorkspace(true);
Bjorn Bringertc459e522013-06-07 19:36:01 +01002226 }
2227
Ian Parkinson047036e2014-09-01 15:40:53 +01002228 /**
2229 * Start a text search.
2230 *
2231 * @return {@code true} if the search will start immediately, so any further keypresses
2232 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2233 * to buffer keypresses.
2234 */
2235 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002236 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002237 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2238 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2239 sourceBounds);
2240 }
2241
Michael Jurkaa33411c2012-06-14 16:18:21 -07002242 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002243 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002244 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002245 }
2246
2247 /**
2248 * Starts the global search activity. This code is a copied from SearchManager
2249 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002250 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002251 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002252 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002253 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2254 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2255 if (globalSearchActivity == null) {
2256 Log.w(TAG, "No global search activity found.");
2257 return;
2258 }
2259 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2260 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2261 intent.setComponent(globalSearchActivity);
2262 // Make sure that we have a Bundle to put source in
2263 if (appSearchData == null) {
2264 appSearchData = new Bundle();
2265 } else {
2266 appSearchData = new Bundle(appSearchData);
2267 }
Adam Cohen9211d422014-10-07 18:14:53 -07002268 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002269 if (!appSearchData.containsKey("source")) {
2270 appSearchData.putString("source", getPackageName());
2271 }
2272 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2273 if (!TextUtils.isEmpty(initialQuery)) {
2274 intent.putExtra(SearchManager.QUERY, initialQuery);
2275 }
2276 if (selectInitialQuery) {
2277 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2278 }
2279 intent.setSourceBounds(sourceBounds);
2280 try {
2281 startActivity(intent);
2282 } catch (ActivityNotFoundException ex) {
2283 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2284 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002285 }
2286
Yura4f93ec62014-02-04 14:15:21 +00002287 public boolean isOnCustomContent() {
2288 return mWorkspace.isOnOrMovingToCustomContent();
2289 }
2290
Winson Chung70d72102011-08-12 11:24:35 -07002291 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002292 public boolean onPrepareOptionsMenu(Menu menu) {
2293 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002294 if (!isOnCustomContent()) {
2295 // Close any open folders
2296 closeFolder();
2297 // Stop resizing any widgets
2298 mWorkspace.exitWidgetResizeMode();
2299 if (!mWorkspace.isInOverviewMode()) {
2300 // Show the overview mode
2301 showOverviewMode(true);
2302 } else {
2303 showWorkspace(true);
2304 }
Winson Chunge0298742014-01-17 12:03:00 -08002305 }
Adam Cohen9211d422014-10-07 18:14:53 -07002306 if (mLauncherCallbacks != null) {
2307 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2308 }
2309
Michael Jurkab94f3f82013-09-11 18:08:54 +02002310 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002311 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002312
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002313 @Override
2314 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002315 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002316 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002317 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002318 }
2319
Joe Onorato9c1289c2009-08-17 11:03:03 -04002320 public boolean isWorkspaceLocked() {
2321 return mWorkspaceLoading || mWaitingForResult;
2322 }
2323
Adam Cohen517a7f52014-03-01 12:12:59 -08002324 public boolean isWorkspaceLoading() {
2325 return mWorkspaceLoading;
2326 }
2327
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002328 private void setWorkspaceLoading(boolean value) {
2329 boolean isLocked = isWorkspaceLocked();
2330 mWorkspaceLoading = value;
2331 if (isLocked != isWorkspaceLocked()) {
2332 onWorkspaceLockedChanged();
2333 }
2334 }
2335
2336 private void setWaitingForResult(boolean value) {
2337 boolean isLocked = isWorkspaceLocked();
2338 mWaitingForResult = value;
2339 if (isLocked != isWorkspaceLocked()) {
2340 onWorkspaceLockedChanged();
2341 }
2342 }
2343
Adam Cohen9211d422014-10-07 18:14:53 -07002344 protected void onWorkspaceLockedChanged() {
2345 if (mLauncherCallbacks != null) {
2346 mLauncherCallbacks.onWorkspaceLockedChanged();
2347 }
2348 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002349
Michael Jurka0280c3b2010-09-17 15:00:07 -07002350 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002351 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002352 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002353 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2354 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002355 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002356 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002357 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002358
Sunny Goyale6b63a32015-01-16 16:14:29 -08002359 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info, final
Adam Cohen59400422014-03-05 18:07:04 -08002360 AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo) {
Adam Cohenad4e15c2013-10-17 16:21:35 -07002361 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2362 }
2363
Sunny Goyale6b63a32015-01-16 16:14:29 -08002364 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
Adam Cohen59400422014-03-05 18:07:04 -08002365 final AppWidgetHostView boundWidget, final LauncherAppWidgetProviderInfo appWidgetInfo,
2366 int delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002367 if (appWidgetInfo.configure != null) {
2368 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002369 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002370
Bjorn Bringert7984c942009-12-09 15:38:25 +00002371 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002372 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2373 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2374
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002375 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002376 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002377 Runnable onComplete = new Runnable() {
2378 @Override
2379 public void run() {
2380 // Exit spring loaded mode if necessary after adding the widget
2381 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2382 null);
2383 }
2384 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002385 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002386 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002387 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002388 }
2389 }
Romain Guycbb89e42009-06-08 15:52:54 -07002390
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002391 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002392 // Close any folders that may be open.
2393 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002394 mWorkspace.moveToCustomContentScreen(animate);
2395 }
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002396
2397 public void addPendingItem(PendingAddItemInfo info, long container, long screenId,
2398 int[] cell, int spanX, int spanY) {
2399 switch (info.itemType) {
2400 case LauncherSettings.Favorites.ITEM_TYPE_CUSTOM_APPWIDGET:
2401 case LauncherSettings.Favorites.ITEM_TYPE_APPWIDGET:
2402 int span[] = new int[2];
2403 span[0] = spanX;
2404 span[1] = spanY;
2405 addAppWidgetFromDrop((PendingAddWidgetInfo) info,
2406 container, screenId, cell, span);
2407 break;
2408 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
2409 processShortcutFromDrop(info.componentName, container, screenId, cell);
2410 break;
2411 default:
2412 throw new IllegalStateException("Unknown item type: " + info.itemType);
2413 }
2414 }
2415
Adam Cohenfbba09b2011-07-18 21:43:05 -07002416 /**
2417 * Process a shortcut drop.
2418 *
2419 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002420 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002421 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002422 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002423 private void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
2424 int[] cell) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002425 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002426 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002427 mPendingAddInfo.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002428 mPendingAddInfo.dropPos = null;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002429
2430 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002431 mPendingAddInfo.cellX = cell[0];
2432 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002433 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002434
2435 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2436 createShortcutIntent.setComponent(componentName);
2437 processShortcut(createShortcutIntent);
2438 }
2439
Adam Cohenfbba09b2011-07-18 21:43:05 -07002440 /**
2441 * Process a widget drop.
2442 *
2443 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002444 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002445 * @param cell The cell it should be added to, optional
Adam Cohenfbba09b2011-07-18 21:43:05 -07002446 */
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002447 private void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
2448 int[] cell, int[] span) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002449 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002450 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002451 mPendingAddInfo.screenId = info.screenId = screenId;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -08002452 mPendingAddInfo.dropPos = null;
Adam Cohend41fbf52012-02-16 23:53:59 -08002453 mPendingAddInfo.minSpanX = info.minSpanX;
2454 mPendingAddInfo.minSpanY = info.minSpanY;
2455
Adam Cohenfbba09b2011-07-18 21:43:05 -07002456 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002457 mPendingAddInfo.cellX = cell[0];
2458 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002459 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002460 if (span != null) {
2461 mPendingAddInfo.spanX = span[0];
2462 mPendingAddInfo.spanY = span[1];
2463 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002464
Adam Cohened66b2b2012-01-23 17:28:51 -08002465 AppWidgetHostView hostView = info.boundWidget;
2466 int appWidgetId;
2467 if (hostView != null) {
2468 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002469 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002470 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002471 // In this case, we either need to start an activity to get permission to bind
2472 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002473 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002474 Bundle options = info.bindOptions;
2475
Sunny Goyalffe83f12014-08-14 17:39:34 -07002476 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2477 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002478 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002479 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002480 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002481 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002482 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2483 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2484 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002485 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2486 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002487 // TODO: we need to make sure that this accounts for the options bundle.
2488 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002489 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2490 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002491 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002492 }
2493
Joe Onoratodeb98af2010-02-19 14:59:39 -08002494 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002495 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002496 }
2497
Winson Chung24ab2f12010-09-16 14:10:47 -07002498 void processWallpaper(Intent intent) {
2499 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2500 }
2501
Adam Cohendcd297f2013-06-18 13:13:40 -07002502 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002503 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002504 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 folderInfo.title = getText(R.string.folder_name);
2506
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507 // Update the model
Sunny Goyal1d4a2df2015-03-30 11:11:46 -07002508 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId,
2509 cellX, cellY);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002510 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511
2512 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002513 FolderIcon newFolder =
2514 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002515 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002516 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002517 // Force measure the new folder icon
2518 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2519 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002520 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002521 }
Romain Guycbb89e42009-06-08 15:52:54 -07002522
Joe Onorato9c1289c2009-08-17 11:03:03 -04002523 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002524 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002525 }
2526
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002527 /**
2528 * Registers various content observers. The current implementation registers
2529 * only a favorites observer to keep track of the favorites applications.
2530 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002531 private void registerContentObservers() {
2532 ContentResolver resolver = getContentResolver();
2533 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2534 true, mWidgetObserver);
2535 }
2536
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002537 @Override
2538 public boolean dispatchKeyEvent(KeyEvent event) {
2539 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2540 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002541 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002542 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002543 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002544 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002545 dumpState();
2546 return true;
2547 }
2548 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002549 }
2550 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2551 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002552 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 return true;
2554 }
2555 }
2556
2557 return super.dispatchKeyEvent(event);
2558 }
2559
Joe Onorato88ec0992009-11-19 13:16:06 -08002560 @Override
2561 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002562 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2563 return;
2564 }
2565
Winson Chung3d9490a2015-03-24 17:38:42 -07002566 LauncherAccessibilityDelegate delegate =
2567 LauncherAppState.getInstance().getAccessibilityDelegate();
2568 if (delegate != null && delegate.onBackPressed()) {
Adam Cohenc9735cf2015-01-23 16:11:55 -08002569 return;
2570 }
2571
Winson Chungb745afb2015-03-02 11:51:23 -08002572 if (isAppsViewVisible()) {
2573 showWorkspace(true);
2574 } else if (isWidgetsViewVisible()) {
2575 showOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002576 } else if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002577 showWorkspace(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002578 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002579 Folder openFolder = mWorkspace.getOpenFolder();
2580 if (openFolder.isEditingName()) {
2581 openFolder.dismissEditingName();
2582 } else {
2583 closeFolder();
2584 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002585 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002586 mWorkspace.exitWidgetResizeMode();
2587
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002588 // Back button is a no-op here, but give at least some feedback for the button press
2589 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002590 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002591 }
2592
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002593 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002594 * Re-listen when widgets are reset.
2595 */
Adam Cohen091440a2015-03-18 14:16:05 -07002596 @Thunk void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002597 if (mAppWidgetHost != null) {
2598 mAppWidgetHost.startListening();
2599 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002600 }
2601
2602 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002603 * Launches the intent referred by the clicked shortcut.
2604 *
2605 * @param v The view representing the clicked shortcut.
2606 */
2607 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002608 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2609 // view has detached (it's possible for this to happen if the view is removed mid touch).
2610 if (v.getWindowToken() == null) {
2611 return;
2612 }
2613
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002614 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002615 return;
2616 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002617
Adam Cohen1697b792013-09-17 19:08:21 -07002618 if (v instanceof Workspace) {
2619 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002620 showWorkspace(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002621 }
2622 return;
2623 }
2624
2625 if (v instanceof CellLayout) {
2626 if (mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07002627 showWorkspace(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002628 }
2629 }
2630
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002631 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002632 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002633 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002634 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002635 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002636 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002637 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002638 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002639 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002640 } else if (tag instanceof AppInfo) {
2641 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002642 } else if (tag instanceof LauncherAppWidgetInfo) {
2643 if (v instanceof PendingAppWidgetHostView) {
2644 onClickPendingWidget((PendingAppWidgetHostView) v);
2645 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002646 }
2647 }
2648
Sunny Goyal508da152014-08-14 10:53:27 -07002649 public void onClickPagedViewIcon(View v) {
2650 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002651 if (mLauncherCallbacks != null) {
2652 mLauncherCallbacks.onClickPagedViewIcon(v);
2653 }
Sunny Goyal508da152014-08-14 10:53:27 -07002654 }
2655
Sunny Goyal70660032015-05-14 00:07:08 -07002656 @SuppressLint("ClickableViewAccessibility")
Michael Jurka0e260592010-06-30 17:07:39 -07002657 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002658 return false;
2659 }
2660
Michael Jurkaaf442092010-06-10 17:01:57 -07002661 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002662 * Event handler for the app widget view which has not fully restored.
2663 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002664 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002665 if (mIsSafeModeEnabled) {
2666 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2667 return;
2668 }
2669
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002670 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002671 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002672 int widgetId = info.appWidgetId;
2673 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2674 if (appWidgetInfo != null) {
Adam Cohen59400422014-03-05 18:07:04 -08002675 mPendingAddWidgetInfo = LauncherAppWidgetProviderInfo.fromProviderInfo(
2676 this, appWidgetInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07002677 mPendingAddInfo.copyFrom(info);
2678 mPendingAddWidgetId = widgetId;
2679
Sunny Goyalffe83f12014-08-14 17:39:34 -07002680 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2681 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002682 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002683 } else if (info.installProgress < 0) {
2684 // The install has not been queued
2685 final String packageName = info.providerName.getPackageName();
2686 showBrokenAppInstallDialog(packageName,
2687 new DialogInterface.OnClickListener() {
2688 public void onClick(DialogInterface dialog, int id) {
2689 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2690 }
2691 });
2692 } else {
2693 // Download has started.
2694 final String packageName = info.providerName.getPackageName();
2695 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002696 }
2697 }
2698
2699 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002700 * Event handler for the "grid" button that appears on the home screen, which
2701 * enters all apps mode.
2702 *
2703 * @param v The view that was clicked.
2704 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002705 protected void onClickAllAppsButton(View v) {
2706 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
Winson Chungb745afb2015-03-02 11:51:23 -08002707 if (isAppsViewVisible()) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002708 showWorkspace(true);
2709 } else {
Winson Chung4ac30062015-05-08 17:34:17 -07002710 // Try and refresh the set of predicted apps before we enter launcher
2711 showAppsView(true /* animated */, false /* resetListToTop */,
2712 true /* updatePredictedApps */);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002713 }
2714 }
2715
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002716 private void showBrokenAppInstallDialog(final String packageName,
2717 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002718 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002719 .setTitle(R.string.abandoned_promises_title)
2720 .setMessage(R.string.abandoned_promise_explanation)
2721 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2722 .setNeutralButton(R.string.abandoned_clean_this,
2723 new DialogInterface.OnClickListener() {
2724 public void onClick(DialogInterface dialog, int id) {
2725 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2726 mWorkspace.removeAbandonedPromise(packageName, user);
2727 }
2728 })
2729 .create().show();
2730 return;
2731 }
2732
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002733 /**
2734 * Event handler for an app shortcut click.
2735 *
2736 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2737 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002738 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002739 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2740 Object tag = v.getTag();
2741 if (!(tag instanceof ShortcutInfo)) {
2742 throw new IllegalArgumentException("Input must be a Shortcut");
2743 }
2744
2745 // Open shortcut
2746 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002747
2748 if (shortcut.isDisabled != 0) {
2749 int error = R.string.activity_not_available;
2750 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2751 error = R.string.safemode_shortcut_error;
2752 }
2753 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2754 return;
2755 }
2756
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002757 final Intent intent = shortcut.intent;
2758
2759 // Check for special shortcuts
2760 if (intent.getComponent() != null) {
2761 final String shortcutClass = intent.getComponent().getClassName();
2762
2763 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2764 MemoryDumpActivity.startDump(this);
2765 return;
2766 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2767 toggleShowWeightWatcher();
2768 return;
2769 }
2770 }
2771
Chris Wren40c5ed32014-06-24 18:24:23 -04002772 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002773 if ((v instanceof BubbleTextView)
2774 && shortcut.isPromise()
2775 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002776 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002777 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002778 new DialogInterface.OnClickListener() {
2779 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002780 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002781 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002782 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002783 return;
2784 }
2785
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002786 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002787 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002788
2789 if (mLauncherCallbacks != null) {
2790 mLauncherCallbacks.onClickAppShortcut(v);
2791 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002792 }
2793
Adam Cohen091440a2015-03-18 14:16:05 -07002794 @Thunk void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002795 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002796 final ShortcutInfo shortcut;
2797 final Intent intent;
2798 if (tag instanceof ShortcutInfo) {
2799 shortcut = (ShortcutInfo) tag;
2800 intent = shortcut.intent;
2801 int[] pos = new int[2];
2802 v.getLocationOnScreen(pos);
2803 intent.setSourceBounds(new Rect(pos[0], pos[1],
2804 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002805
Sunny Goyal508da152014-08-14 10:53:27 -07002806 } else if (tag instanceof AppInfo) {
2807 shortcut = null;
2808 intent = ((AppInfo) tag).intent;
2809 } else {
2810 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2811 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002812
2813 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002814 mStats.recordLaunch(intent, shortcut);
2815
2816 if (success && v instanceof BubbleTextView) {
2817 mWaitingForResume = (BubbleTextView) v;
2818 mWaitingForResume.setStayPressed(true);
2819 }
2820 }
2821
2822 /**
2823 * Event handler for a folder icon click.
2824 *
2825 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2826 */
2827 protected void onClickFolderIcon(View v) {
2828 if (LOGD) Log.d(TAG, "onClickFolder");
2829 if (!(v instanceof FolderIcon)){
2830 throw new IllegalArgumentException("Input must be a FolderIcon");
2831 }
2832
2833 FolderIcon folderIcon = (FolderIcon) v;
2834 final FolderInfo info = folderIcon.getFolderInfo();
2835 Folder openFolder = mWorkspace.getFolderForTag(info);
2836
2837 // If the folder info reports that the associated folder is open, then verify that
2838 // it is actually opened. There have been a few instances where this gets out of sync.
2839 if (info.opened && openFolder == null) {
2840 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2841 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2842 info.opened = false;
2843 }
2844
2845 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2846 // Close any open folder
2847 closeFolder();
2848 // Open the requested folder
2849 openFolder(folderIcon);
2850 } else {
2851 // Find the open folder...
2852 int folderScreen;
2853 if (openFolder != null) {
2854 folderScreen = mWorkspace.getPageForView(openFolder);
2855 // .. and close it
2856 closeFolder(openFolder);
2857 if (folderScreen != mWorkspace.getCurrentPage()) {
2858 // Close any folder open on the current screen
2859 closeFolder();
2860 // Pull the folder onto this screen
2861 openFolder(folderIcon);
2862 }
2863 }
2864 }
Adam Cohen9211d422014-10-07 18:14:53 -07002865
2866 if (mLauncherCallbacks != null) {
2867 mLauncherCallbacks.onClickFolderIcon(v);
2868 }
Michael Jurka5130e402011-10-13 04:55:35 -07002869 }
2870
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002871 /**
2872 * Event handler for the (Add) Widgets button that appears after a long press
2873 * on the home screen.
2874 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002875 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002876 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002877 if (mIsSafeModeEnabled) {
2878 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2879 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08002880 showWidgetsView(true /* animated */, true /* resetPageToZero */);
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002881 if (mLauncherCallbacks != null) {
2882 mLauncherCallbacks.onClickAddWidgetButton(view);
2883 }
Adam Cohen9211d422014-10-07 18:14:53 -07002884 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002885 }
2886
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002887 /**
2888 * Event handler for the wallpaper picker button that appears after a long press
2889 * on the home screen.
2890 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002891 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002892 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
Sunny Goyal6a1e95a2015-03-20 17:26:30 -07002893 startActivityForResult(new Intent(Intent.ACTION_SET_WALLPAPER).setPackage(getPackageName()),
2894 REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002895
2896 if (mLauncherCallbacks != null) {
2897 mLauncherCallbacks.onClickWallpaperPicker(v);
2898 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002899 }
2900
2901 /**
2902 * Event handler for a click on the settings button that appears after a long press
2903 * on the home screen.
2904 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002905 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002906 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002907 if (mLauncherCallbacks != null) {
2908 mLauncherCallbacks.onClickSettingsButton(v);
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07002909 } else {
2910 showSettingsActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002911 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002912 }
2913
Michael Jurka5130e402011-10-13 04:55:35 -07002914 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002915 // Provide the same haptic feedback that the system offers for virtual keys.
2916 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002917 }
2918
Adam Cohen61f560d2013-09-30 15:58:20 -07002919 public void performHapticFeedbackOnTouchDown(View v) {
2920 // Provide the same haptic feedback that the system offers for virtual keys.
2921 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2922 }
2923
2924 public View.OnTouchListener getHapticFeedbackTouchListener() {
2925 if (mHapticFeedbackTouchListener == null) {
2926 mHapticFeedbackTouchListener = new View.OnTouchListener() {
Sunny Goyal70660032015-05-14 00:07:08 -07002927 @SuppressLint("ClickableViewAccessibility")
Adam Cohen61f560d2013-09-30 15:58:20 -07002928 @Override
2929 public boolean onTouch(View v, MotionEvent event) {
2930 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2931 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2932 }
2933 return false;
2934 }
2935 };
2936 }
2937 return mHapticFeedbackTouchListener;
2938 }
2939
Adam Cohen9211d422014-10-07 18:14:53 -07002940 public void onDragStarted(View view) {
2941 if (isOnCustomContent()) {
2942 // Custom content screen doesn't participate in drag and drop. If on custom
2943 // content screen, move to default.
2944 moveWorkspaceToDefaultScreen();
2945 }
2946
2947 if (mLauncherCallbacks != null) {
2948 mLauncherCallbacks.onDragStarted(view);
2949 }
2950 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002951
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002952 /**
2953 * Called when the user stops interacting with the launcher.
2954 * This implies that the user is now on the homescreen and is not doing housekeeping.
2955 */
Adam Cohen9211d422014-10-07 18:14:53 -07002956 protected void onInteractionEnd() {
2957 if (mLauncherCallbacks != null) {
2958 mLauncherCallbacks.onInteractionEnd();
2959 }
2960 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002961
2962 /**
2963 * Called when the user starts interacting with the launcher.
2964 * The possible interactions are:
2965 * - open all apps
2966 * - reorder an app shortcut, or a widget
2967 * - open the overview mode.
2968 * This is a good time to stop doing things that only make sense
2969 * when the user is on the homescreen and not doing housekeeping.
2970 */
Adam Cohen9211d422014-10-07 18:14:53 -07002971 protected void onInteractionBegin() {
2972 if (mLauncherCallbacks != null) {
2973 mLauncherCallbacks.onInteractionBegin();
2974 }
2975 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002976
Winson Chungcd99cd32015-04-29 11:03:24 -07002977 /** Updates the interaction state. */
2978 public void updateInteraction(Workspace.State fromState, Workspace.State toState) {
Winson Chung83f59ab2015-05-05 17:21:58 -07002979 // Only update the interacting state if we are transitioning to/from a view with an
Winson Chungcd99cd32015-04-29 11:03:24 -07002980 // overlay
Winson Chung83f59ab2015-05-05 17:21:58 -07002981 boolean fromStateWithOverlay;
2982 boolean toStateWithOverlay;
2983 if (Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
2984 fromStateWithOverlay = fromState != Workspace.State.NORMAL;
2985 toStateWithOverlay = toState != Workspace.State.NORMAL;
2986 } else {
2987 fromStateWithOverlay = fromState != Workspace.State.NORMAL &&
2988 fromState != Workspace.State.NORMAL_HIDDEN;
2989 toStateWithOverlay = toState != Workspace.State.NORMAL &&
2990 toState != Workspace.State.NORMAL_HIDDEN;
2991 }
2992 if (toStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002993 onInteractionBegin();
Winson Chung83f59ab2015-05-05 17:21:58 -07002994 } else if (fromStateWithOverlay) {
Winson Chungcd99cd32015-04-29 11:03:24 -07002995 onInteractionEnd();
2996 }
2997 }
2998
Kenny Guyf07af7b2014-07-31 11:39:16 +01002999 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01003000 try {
3001 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01003002 launcherApps.showAppDetailsForProfile(componentName, user);
3003 } catch (SecurityException e) {
3004 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3005 Log.e(TAG, "Launcher does not have permission to launch settings");
3006 } catch (ActivityNotFoundException e) {
3007 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3008 Log.e(TAG, "Unable to launch settings");
3009 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07003010 }
3011
Michael Jurka1e2f4652013-07-08 18:03:46 -07003012 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01003013 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
3014 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02003015 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07003016 // System applications cannot be installed. For now, show a toast explaining that.
3017 // We may give them the option of disabling apps this way.
3018 int messageId = R.string.uninstall_system_app_text;
3019 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07003020 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07003021 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003022 String packageName = componentName.getPackageName();
3023 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07003024 Intent intent = new Intent(
3025 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07003026 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
3027 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01003028 if (user != null) {
3029 user.addToIntent(intent, Intent.EXTRA_USER);
3030 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07003031 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003032 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07003033 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07003034 }
3035
Michael Jurka86a720e2012-05-09 11:23:23 -07003036 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01003037 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003038 try {
Winson Chung2672ff92012-05-04 16:22:30 -07003039 // Only launch using the new animation if the shortcut has not opted out (this is a
3040 // private contract between launcher and may be ignored in the future).
3041 boolean useLaunchAnimation = (v != null) &&
3042 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01003043 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
3044 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01003045
Kenny Guy1317e2d2014-05-08 18:52:50 +01003046 UserHandleCompat user = null;
3047 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
3048 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
3049 user = userManager.getUserForSerialNumber(serialNumber);
3050 }
3051
3052 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07003053 if (useLaunchAnimation) {
Chet Haasea8f996d2015-02-17 12:56:04 -08003054 ActivityOptions opts = null;
3055 if (sClipRevealMethod != null) {
3056 // TODO: call method directly when Launcher3 can depend on M APIs
3057 int left = 0, top = 0;
3058 int width = v.getMeasuredWidth(), height = v.getMeasuredHeight();
3059 if (v instanceof TextView) {
3060 // Launch from center of icon, not entire view
Winson Chungb745afb2015-03-02 11:51:23 -08003061 Drawable icon = Workspace.getTextViewIcon((TextView) v);
3062 if (icon != null) {
3063 Rect bounds = icon.getBounds();
Chet Haasea8f996d2015-02-17 12:56:04 -08003064 left = (width - bounds.width()) / 2;
Winson Chungb745afb2015-03-02 11:51:23 -08003065 top = v.getPaddingTop();
Chet Haasea8f996d2015-02-17 12:56:04 -08003066 width = bounds.width();
3067 height = bounds.height();
3068 }
3069 }
3070 try {
3071 opts = (ActivityOptions) sClipRevealMethod.invoke(null, v,
3072 left, top, width, height);
3073 } catch (IllegalAccessException e) {
3074 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
3075 sClipRevealMethod = null;
3076 } catch (InvocationTargetException e) {
3077 Log.d(TAG, "Could not call makeClipRevealAnimation: " + e);
3078 sClipRevealMethod = null;
3079 }
3080 }
Adam Cohen2e52c902015-04-06 13:11:28 -07003081 if (opts == null && !Utilities.isLmpOrAbove()) {
3082 opts = ActivityOptions.makeScaleUpAnimation(v, 0, 0,
Chet Haasea8f996d2015-02-17 12:56:04 -08003083 v.getMeasuredWidth(), v.getMeasuredHeight());
3084 }
Adam Cohen2e52c902015-04-06 13:11:28 -07003085 optsBundle = opts != null ? opts.toBundle() : null;
Adam Cohen6ea3b112014-06-11 11:38:49 -07003086 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01003087
3088 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
3089 // Could be launching some bookkeeping activity
3090 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003091 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003092 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003093 launcherApps.startActivityForProfile(intent.getComponent(), user,
3094 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003095 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003096 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003097 } catch (SecurityException e) {
3098 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003099 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003100 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003101 "or use the exported attribute for this activity. "
3102 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003103 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003104 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003105 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003106
Michael Jurka86a720e2012-05-09 11:23:23 -07003107 boolean startActivitySafely(View v, Intent intent, Object tag) {
3108 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003109 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3110 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3111 return false;
3112 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003113 try {
3114 success = startActivity(v, intent, tag);
3115 } catch (ActivityNotFoundException e) {
3116 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3117 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3118 }
3119 return success;
3120 }
3121
Adam Cohen268c4752012-06-06 17:47:33 -07003122 /**
3123 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3124 * in the DragLayer in the exact absolute location of the original FolderIcon.
3125 */
3126 private void copyFolderIconToImage(FolderIcon fi) {
3127 final int width = fi.getMeasuredWidth();
3128 final int height = fi.getMeasuredHeight();
3129
3130 // Lazy load ImageView, Bitmap and Canvas
3131 if (mFolderIconImageView == null) {
3132 mFolderIconImageView = new ImageView(this);
3133 }
3134 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3135 mFolderIconBitmap.getHeight() != height) {
3136 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3137 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3138 }
3139
3140 DragLayer.LayoutParams lp;
3141 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3142 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3143 } else {
3144 lp = new DragLayer.LayoutParams(width, height);
3145 }
3146
Adam Cohen307fe232012-08-16 17:55:58 -07003147 // The layout from which the folder is being opened may be scaled, adjust the starting
3148 // view size by this scale factor.
3149 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003150 lp.customPosition = true;
3151 lp.x = mRectForFolderAnimation.left;
3152 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003153 lp.width = (int) (scale * width);
3154 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003155
3156 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3157 fi.draw(mFolderIconCanvas);
3158 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003159 if (fi.getFolder() != null) {
3160 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3161 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003162 }
Adam Cohen268c4752012-06-06 17:47:33 -07003163 // Just in case this image view is still in the drag layer from a previous animation,
3164 // we remove it and re-add it.
3165 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3166 mDragLayer.removeView(mFolderIconImageView);
3167 }
3168 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003169 if (fi.getFolder() != null) {
3170 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003171 }
Adam Cohen268c4752012-06-06 17:47:33 -07003172 }
3173
Adam Cohen2801caf2011-05-13 20:57:39 -07003174 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003175 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003176 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3177 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3178 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3179
Adam Cohenc51934b2011-07-26 21:07:43 -07003180 FolderInfo info = (FolderInfo) fi.getTag();
3181 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3182 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003183 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3184 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003185 }
3186
Adam Cohen268c4752012-06-06 17:47:33 -07003187 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3188 copyFolderIconToImage(fi);
3189 fi.setVisibility(View.INVISIBLE);
3190
Michael Jurka2ecf9952012-06-18 12:52:28 -07003191 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003192 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003193 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003194 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3195 }
3196 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003197 oa.start();
3198 }
3199
Adam Cohen268c4752012-06-06 17:47:33 -07003200 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003201 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003202 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3203 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3204 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3205
Adam Cohen268c4752012-06-06 17:47:33 -07003206 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003207
Adam Cohen268c4752012-06-06 17:47:33 -07003208 // We remove and re-draw the FolderIcon in-case it has changed
3209 mDragLayer.removeView(mFolderIconImageView);
3210 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003211 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003212 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003213 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003214 oa.addListener(new AnimatorListenerAdapter() {
3215 @Override
3216 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003217 if (cl != null) {
3218 cl.clearFolderLeaveBehind();
3219 // Remove the ImageView copy of the FolderIcon and make the original visible.
3220 mDragLayer.removeView(mFolderIconImageView);
3221 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003222 }
3223 }
3224 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003225 oa.start();
3226 }
3227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003228 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003229 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003230 * is animated relative to the specified View. If the View is null, no animation
3231 * is played.
3232 *
3233 * @param folderInfo The FolderInfo describing the folder to open.
3234 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003235 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003236 Folder folder = folderIcon.getFolder();
Sunny Goyalf4066152015-04-15 09:42:19 -07003237 Folder openFolder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
3238 if (openFolder != null && openFolder != folder) {
3239 // Close any open folder before opening a folder.
3240 closeFolder();
3241 }
3242
Adam Cohena9cf38f2011-05-02 15:36:58 -07003243 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003244
Adam Cohena9cf38f2011-05-02 15:36:58 -07003245 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003246
Sunny Goyalf4066152015-04-15 09:42:19 -07003247 // While the folder is open, the position of the icon cannot change.
3248 ((CellLayout.LayoutParams) folderIcon.getLayoutParams()).canReorder = false;
3249
Adam Cohen4554ee12011-08-03 16:13:21 -07003250 // Just verify that the folder hasn't already been added to the DragLayer.
3251 // There was a one-off crash where the folder had a parent already.
3252 if (folder.getParent() == null) {
3253 mDragLayer.addView(folder);
3254 mDragController.addDropTarget((DropTarget) folder);
3255 } else {
3256 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3257 folder.getParent() + ").");
3258 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003259 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003260 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003261
3262 // Notify the accessibility manager that this folder "window" has appeared and occluded
3263 // the workspace items
3264 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3265 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003266 }
3267
3268 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003269 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003270 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003271 if (folder.isEditingName()) {
3272 folder.dismissEditingName();
3273 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003274 closeFolder(folder);
3275 }
3276 }
3277
Sunny Goyal83a8f042015-05-19 12:52:12 -07003278 public void closeFolder(Folder folder) {
Adam Cohen4554ee12011-08-03 16:13:21 -07003279 folder.getInfo().opened = false;
3280
3281 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3282 if (parent != null) {
3283 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3284 shrinkAndFadeInFolderIcon(fi);
Sunny Goyalf4066152015-04-15 09:42:19 -07003285 if (fi != null) {
3286 ((CellLayout.LayoutParams) fi.getLayoutParams()).canReorder = true;
3287 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003288 }
3289 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003290
3291 // Notify the accessibility manager that this folder "window" has disappeard and no
3292 // longer occludeds the workspace items
3293 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003294 }
3295
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003296 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003297 if (!isDraggingEnabled()) return false;
3298 if (isWorkspaceLocked()) return false;
3299 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003300
Adam Cohen1697b792013-09-17 19:08:21 -07003301 if (v instanceof Workspace) {
3302 if (!mWorkspace.isInOverviewMode()) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003303 if (!mWorkspace.isTouchActive()) {
3304 showOverviewMode(true);
Adam Cohen93c97562013-09-26 13:48:01 -07003305 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3306 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3307 return true;
3308 } else {
3309 return false;
3310 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003311 } else {
3312 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003313 }
Adam Cohen1697b792013-09-17 19:08:21 -07003314 }
3315
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003316 CellLayout.CellInfo longClickCellInfo = null;
3317 View itemUnderLongClick = null;
3318 if (v.getTag() instanceof ItemInfo) {
3319 ItemInfo info = (ItemInfo) v.getTag();
Adam Cohenc9735cf2015-01-23 16:11:55 -08003320 longClickCellInfo = new CellLayout.CellInfo(v, info);
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003321 itemUnderLongClick = longClickCellInfo.cell;
3322 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003323 }
3324
Winson Chung3d503fb2011-07-13 17:25:49 -07003325 // The hotseat touch handling does not go through Workspace, and we always allow long press
3326 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003327 final boolean inHotseat = isHotseatLayout(v);
Sunny Goyal8e2133b2015-05-06 13:39:07 -07003328 if (!mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003329 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003330 // User long pressed on empty space
3331 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3332 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003333 if (mWorkspace.isInOverviewMode()) {
3334 mWorkspace.startReordering(v);
3335 } else {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003336 showOverviewMode(true);
Adam Cohendedbd962013-07-11 14:21:49 -07003337 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003338 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003339 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3340 mHotseat.getOrderInHotseat(
3341 longClickCellInfo.cellX,
3342 longClickCellInfo.cellY));
3343 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003344 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003345 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003346 }
3347 }
3348 }
3349 return true;
3350 }
3351
Winson Chung3d503fb2011-07-13 17:25:49 -07003352 boolean isHotseatLayout(View layout) {
3353 return mHotseat != null && layout != null &&
3354 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3355 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003356
Winson Chung3d503fb2011-07-13 17:25:49 -07003357 /**
3358 * Returns the CellLayout of the specified container at the specified screen.
3359 */
Sunny Goyal92820592015-03-02 11:31:35 -08003360 public CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003361 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3362 if (mHotseat != null) {
3363 return mHotseat.getLayout();
3364 } else {
3365 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003366 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003367 } else {
Winson Chungb745afb2015-03-02 11:51:23 -08003368 return mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003369 }
3370 }
3371
Winson Chungb745afb2015-03-02 11:51:23 -08003372 /**
3373 * For overridden classes.
3374 */
Daniel Sandler843e8602010-06-07 14:59:01 -04003375 public boolean isAllAppsVisible() {
Winson Chungb745afb2015-03-02 11:51:23 -08003376 return isAppsViewVisible();
3377 }
3378
3379 public boolean isAppsViewVisible() {
3380 return (mState == State.APPS) || (mOnResumeState == State.APPS);
3381 }
3382
3383 public boolean isWidgetsViewVisible() {
3384 return (mState == State.WIDGETS) || (mOnResumeState == State.WIDGETS);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003385 }
3386
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003387 private void setWorkspaceBackground(int background) {
3388 switch (background) {
3389 case WORKSPACE_BACKGROUND_TRANSPARENT:
3390 getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
3391 break;
3392 case WORKSPACE_BACKGROUND_BLACK:
3393 getWindow().setBackgroundDrawable(null);
3394 break;
3395 default:
3396 getWindow().setBackgroundDrawable(mWorkspaceBackgroundDrawable);
3397 }
Craig Mautner360310b2012-10-26 15:13:08 -07003398 }
3399
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003400 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003401 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3402 int curflags = getWindow().getAttributes().flags
3403 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3404 if (wpflags != curflags) {
3405 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3406 }
Sunny Goyal4a6c6f32015-05-19 12:36:46 -07003407 setWorkspaceBackground(visible ? WORKSPACE_BACKGROUND_GRADIENT : WORKSPACE_BACKGROUND_BLACK);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003408 }
3409
Michael Jurkae326f182011-11-21 14:05:46 -08003410 @Override
3411 public void onTrimMemory(int level) {
3412 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003413 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3414 // The widget preview db can result in holding onto over
3415 // 3MB of memory for caching which isn't necessary.
3416 SQLiteDatabase.releaseMemory();
3417
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003418 // This clears all widget bitmaps from the widget tray
Hyunyoung Song3f471442015-04-08 19:01:34 -07003419 // TODO(hyunyoungs)
Michael Jurkae326f182011-11-21 14:05:46 -08003420 }
Robert Kozikowski67c30862015-03-30 23:46:46 +01003421 if (mLauncherCallbacks != null) {
3422 mLauncherCallbacks.onTrimMemory(level);
3423 }
Michael Jurkae326f182011-11-21 14:05:46 -08003424 }
3425
Winson Chungb745afb2015-03-02 11:51:23 -08003426 @Override
3427 public void onStateTransitionHideSearchBar() {
3428 // Hide the search bar
3429 if (mSearchDropTargetBar != null) {
3430 mSearchDropTargetBar.hideSearchBar(false /* animated */);
3431 }
Adam Cohened66b2b2012-01-23 17:28:51 -08003432 }
3433
Winson Chung5f4e0fd2015-05-22 11:12:27 -07003434 public void showWorkspace(boolean animated) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003435 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated, null,
3436 true);
Adam Cohened307df2013-10-02 09:37:31 -07003437 }
3438
Sunny Goyal83a8f042015-05-19 12:52:12 -07003439 public void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Winson Chungdc61c4d2015-04-20 18:26:57 -07003440 showWorkspace(WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3441 onCompleteRunnable, true);
3442 }
3443
3444 protected void showWorkspace(int snapToPage, boolean animated) {
3445 showWorkspace(snapToPage, animated, null, true);
3446 }
3447
3448 void showWorkspace(int snapToPage, boolean animated, Runnable onCompleteRunnable,
3449 boolean notifyLauncherCallbacks) {
Winson Chung0f785722015-04-08 10:27:49 -07003450 boolean changed = mState != State.WORKSPACE ||
3451 mWorkspace.getState() != Workspace.State.NORMAL;
3452 if (changed) {
Winson Chung2d75f122013-09-23 16:53:31 -07003453 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003454 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003455 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.NORMAL,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003456 snapToPage, animated, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003457
Winson Chungc7d2b602012-05-16 17:02:20 -07003458 // Show the search bar (only animate if we were showing the drop target bar in spring
3459 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003460 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003461 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003462 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003463
Michael Jurkab3e22d92011-10-31 15:58:33 -07003464 // Set focus to the AppsCustomize button
3465 if (mAllAppsButton != null) {
3466 mAllAppsButton.requestFocus();
3467 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003468 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003469
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003470 // Change the state *after* we've called all the transition code
3471 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003472
Winson Chung5fb63472011-02-02 17:03:37 -08003473 // Resume the auto-advance of widgets
3474 mUserPresent = true;
Winson Chungb745afb2015-03-02 11:51:23 -08003475 updateAutoAdvanceState();
Winson Chung5fb63472011-02-02 17:03:37 -08003476
Winson Chung0f785722015-04-08 10:27:49 -07003477 if (changed) {
3478 // Send an accessibility event to announce the context change
3479 getWindow().getDecorView()
3480 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Winson Chungcd99cd32015-04-29 11:03:24 -07003481 if (notifyLauncherCallbacks) {
3482 // Dismiss all apps when the workspace is shown
Winson Chung83f59ab2015-05-05 17:21:58 -07003483 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003484 mLauncherCallbacks.onAllAppsHidden();
3485 }
3486 }
Winson Chung0f785722015-04-08 10:27:49 -07003487 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01003488 }
3489
Adam Cohened307df2013-10-02 09:37:31 -07003490 void showOverviewMode(boolean animated) {
3491 mWorkspace.setVisibility(View.VISIBLE);
Winson Chungb745afb2015-03-02 11:51:23 -08003492 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.OVERVIEW,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003493 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, animated,
3494 null /* onCompleteRunnable */);
Adam Cohened307df2013-10-02 09:37:31 -07003495 mState = State.WORKSPACE;
Joe Onorato00acb122009-08-04 16:04:30 -04003496 }
3497
Winson Chungb745afb2015-03-02 11:51:23 -08003498 /**
3499 * Shows the apps view.
3500 */
Winson Chung4ac30062015-05-08 17:34:17 -07003501 void showAppsView(boolean animated, boolean resetListToTop, boolean updatePredictedApps) {
Winson Chungb745afb2015-03-02 11:51:23 -08003502 if (resetListToTop) {
3503 mAppsView.scrollToTop();
3504 }
Winson Chung4ac30062015-05-08 17:34:17 -07003505 if (updatePredictedApps) {
3506 tryAndUpdatePredictedApps();
3507 }
Winson Chungb745afb2015-03-02 11:51:23 -08003508 showAppsOrWidgets(animated, State.APPS);
3509 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003510
Winson Chungb745afb2015-03-02 11:51:23 -08003511 /**
3512 * Shows the widgets view.
3513 */
3514 void showWidgetsView(boolean animated, boolean resetPageToZero) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003515 if (LOGD) Log.d(TAG, "showWidgetsView:" + animated + " resetPageToZero:" + resetPageToZero);
Winson Chung82963d52013-10-09 11:20:57 -07003516 if (resetPageToZero) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003517 mWidgetsView.scrollToTop();
Winson Chung82963d52013-10-09 11:20:57 -07003518 }
Winson Chungb745afb2015-03-02 11:51:23 -08003519 showAppsOrWidgets(animated, State.WIDGETS);
Hyunyoung Song3f471442015-04-08 19:01:34 -07003520
3521 mWidgetsView.post(new Runnable() {
Adam Cohendcc5e712014-06-23 15:38:55 -04003522 @Override
3523 public void run() {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003524 mWidgetsView.requestFocus();
Adam Cohendcc5e712014-06-23 15:38:55 -04003525 }
3526 });
Winson Chungb745afb2015-03-02 11:51:23 -08003527 }
3528
3529 /**
3530 * Sets up the transition to show the apps/widgets view.
Hyunyoung Songb7900832015-05-15 16:29:45 -07003531 *
3532 * @return whether the current from and to state allowed this operation
Winson Chungb745afb2015-03-02 11:51:23 -08003533 */
Hyunyoung Songb7900832015-05-15 16:29:45 -07003534 // TODO: calling method should use the return value so that when {@code false} is returned
3535 // the workspace transition doesn't fall into invalid state.
3536 private boolean showAppsOrWidgets(boolean animated, State toState) {
3537 if (mState != State.WORKSPACE && mState != State.APPS_SPRING_LOADED &&
3538 mState != State.WIDGETS_SPRING_LOADED) {
3539 return false;
3540 }
3541 if (toState != State.APPS && toState != State.WIDGETS) {
3542 return false;
3543 }
Winson Chungb745afb2015-03-02 11:51:23 -08003544
3545 if (toState == State.APPS) {
3546 mStateTransitionAnimation.startAnimationToAllApps(animated);
Winson Chung83f59ab2015-05-05 17:21:58 -07003547 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chung0f785722015-04-08 10:27:49 -07003548 mLauncherCallbacks.onAllAppsShown();
3549 }
Winson Chungb745afb2015-03-02 11:51:23 -08003550 } else {
3551 mStateTransitionAnimation.startAnimationToWidgets(animated);
3552 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003553
Michael Jurkab3e22d92011-10-31 15:58:33 -07003554 // Change the state *after* we've called all the transition code
Winson Chungb745afb2015-03-02 11:51:23 -08003555 mState = toState;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003556
3557 // Pause the auto-advance of widgets until we are out of AllApps
3558 mUserPresent = false;
Winson Chungb745afb2015-03-02 11:51:23 -08003559 updateAutoAdvanceState();
Michael Jurkab3e22d92011-10-31 15:58:33 -07003560 closeFolder();
3561
3562 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003563 getWindow().getDecorView()
3564 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Hyunyoung Songb7900832015-05-15 16:29:45 -07003565 return true;
Michael Jurkab3e22d92011-10-31 15:58:33 -07003566 }
3567
Winson Chungcd99cd32015-04-29 11:03:24 -07003568 /**
3569 * Updates the workspace and interaction state on state change, and return the animation to this
3570 * new state.
3571 */
3572 public Animator startWorkspaceStateChangeAnimation(Workspace.State toState, int toPage,
3573 boolean animated, HashMap<View, Integer> layerViews) {
3574 Workspace.State fromState = mWorkspace.getState();
3575 Animator anim = mWorkspace.setStateWithAnimation(toState, toPage, animated, layerViews);
3576 updateInteraction(fromState, toState);
3577 return anim;
3578 }
3579
Hyunyoung Song3f471442015-04-08 19:01:34 -07003580 public void enterSpringLoadedDragMode() {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003581 if (LOGD) Log.d(TAG, String.format("enterSpringLoadedDragMode [mState=%s", mState.name()));
Winson Chungb745afb2015-03-02 11:51:23 -08003582 if (mState == State.WORKSPACE || mState == State.APPS_SPRING_LOADED ||
3583 mState == State.WIDGETS_SPRING_LOADED) {
3584 return;
Winson Chungb26f3d62011-06-02 10:49:29 -07003585 }
Winson Chungb745afb2015-03-02 11:51:23 -08003586
3587 mStateTransitionAnimation.startAnimationToWorkspace(mState, Workspace.State.SPRING_LOADED,
Winson Chungdc61c4d2015-04-20 18:26:57 -07003588 WorkspaceStateTransitionAnimation.SCROLL_TO_CURRENT_PAGE, true /* animated */,
3589 null /* onCompleteRunnable */);
Winson Chungb745afb2015-03-02 11:51:23 -08003590 mState = isAppsViewVisible() ? State.APPS_SPRING_LOADED : State.WIDGETS_SPRING_LOADED;
Michael Jurkad3ef3062010-11-23 16:23:58 -08003591 }
Adam Cohen7777d962011-08-18 18:58:38 -07003592
Hyunyoung Song3f471442015-04-08 19:01:34 -07003593 public void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003594 final Runnable onCompleteRunnable) {
Winson Chungb745afb2015-03-02 11:51:23 -08003595 if (mState != State.APPS_SPRING_LOADED && mState != State.WIDGETS_SPRING_LOADED) return;
Winson Chung09bfc452011-09-09 11:30:20 -07003596
Winson Chungcd99cd32015-04-29 11:03:24 -07003597 if (successfulDrop) {
3598 // We need to trigger all apps hidden to notify search to update itself before the
3599 // delayed call to showWorkspace below
Winson Chung83f59ab2015-05-05 17:21:58 -07003600 if (!Launcher.DISABLE_ALL_APPS_SEARCH_INTEGRATION && mLauncherCallbacks != null) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003601 mLauncherCallbacks.onAllAppsHidden();
3602 }
3603 }
3604
Winson Chunge7a03942011-08-05 15:05:12 -07003605 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003606 @Override
3607 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003608 if (successfulDrop) {
Hyunyoung Song3f471442015-04-08 19:01:34 -07003609 // TODO(hyunyoungs): verify if this hack is still needed, if not, delete.
3610 //
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003611 // Before we show workspace, hide all apps again because
3612 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3613 // clean up our state transition functions
Hyunyoung Song3f471442015-04-08 19:01:34 -07003614 mWidgetsView.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003615 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003616 } else {
3617 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003618 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003619 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003620 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003621 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003622
Michael Jurkad3ef3062010-11-23 16:23:58 -08003623 void exitSpringLoadedDragMode() {
Winson Chungb745afb2015-03-02 11:51:23 -08003624 if (mState == State.APPS_SPRING_LOADED) {
Winson Chung4ac30062015-05-08 17:34:17 -07003625 showAppsView(true /* animated */, false /* resetListToTop */,
3626 false /* updatePredictedApps */);
Winson Chungb745afb2015-03-02 11:51:23 -08003627 } else if (mState == State.WIDGETS_SPRING_LOADED) {
Winson Chungcd99cd32015-04-29 11:03:24 -07003628 showWidgetsView(true, false);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003629 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003630 }
3631
Winson Chung4ac30062015-05-08 17:34:17 -07003632 /**
3633 * Updates the set of predicted apps if it hasn't been updated since the last time Launcher was
3634 * resumed.
3635 */
3636 private void tryAndUpdatePredictedApps() {
3637 if (mLauncherCallbacks != null) {
3638 List<ComponentName> apps = mLauncherCallbacks.getPredictedApps();
3639 if (!apps.isEmpty()) {
3640 mAppsView.setPredictedApps(apps);
3641 }
3642 }
3643 }
3644
Michael Jurkab3e22d92011-10-31 15:58:33 -07003645 void lockAllApps() {
3646 // TODO
3647 }
3648
3649 void unlockAllApps() {
3650 // TODO
3651 }
3652
Sunny Goyal594d76d2014-11-06 10:12:54 -08003653 protected void disableVoiceButtonProxy(boolean disable) {
3654 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003655 }
3656
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003657 public View getOrCreateQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003658 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3659 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003660 }
3661
Adam Cohen24ce0b32014-01-14 16:18:14 -08003662 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003663 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3664 if (searchProvider == null) {
3665 return null;
3666 }
3667
3668 Bundle opts = new Bundle();
3669 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
Adam Coheneb8e2822015-02-20 16:38:48 +00003670 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003671
3672 SharedPreferences sp = getSharedPreferences(
3673 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3674 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003675 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003676 if (!searchProvider.provider.flattenToString().equals(
3677 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003678 || (widgetInfo == null)
3679 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003680 // A valid widget is not already bound.
3681 if (widgetId > -1) {
3682 mAppWidgetHost.deleteAppWidgetId(widgetId);
3683 widgetId = -1;
3684 }
3685
3686 // Try to bind a new widget
3687 widgetId = mAppWidgetHost.allocateAppWidgetId();
3688
3689 if (!AppWidgetManagerCompat.getInstance(this)
3690 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3691 mAppWidgetHost.deleteAppWidgetId(widgetId);
3692 widgetId = -1;
3693 }
3694
3695 sp.edit()
3696 .putInt(QSB_WIDGET_ID, widgetId)
3697 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3698 .commit();
3699 }
3700
3701 if (widgetId != -1) {
3702 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3703 mQsb.updateAppWidgetOptions(opts);
3704 mQsb.setPadding(0, 0, 0, 0);
3705 mSearchDropTargetBar.addView(mQsb);
Sunny Goyal64b3fcc2015-03-04 13:54:52 -08003706 mSearchDropTargetBar.setQsbSearchBar(mQsb);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003707 }
Cristina Stancu476493b2013-08-07 17:20:14 +01003708 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08003709 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01003710 }
3711
Sunny Goyal22235bc2015-04-03 09:23:43 -07003712 private void reinflateQSBIfNecessary() {
3713 if (mQsb instanceof LauncherAppWidgetHostView &&
3714 ((LauncherAppWidgetHostView) mQsb).isReinflateRequired()) {
3715 mSearchDropTargetBar.removeView(mQsb);
3716 mQsb = null;
3717 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
3718 }
3719 }
3720
Michael Jurkad7c28052012-04-27 15:43:36 -07003721 @Override
3722 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07003723 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07003724 final List<CharSequence> text = event.getText();
3725 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07003726 // Populate event with a fake title based on the current state.
Winson Chungb745afb2015-03-02 11:51:23 -08003727 if (mState == State.APPS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003728 text.add(getString(R.string.all_apps_button_label));
Winson Chungb745afb2015-03-02 11:51:23 -08003729 } else if (mState == State.WIDGETS) {
Sunny Goyalc525d802015-04-29 11:05:34 -07003730 text.add(getString(R.string.widget_button_text));
alanv1d4fde62012-10-17 13:15:47 -07003731 } else {
3732 text.add(getString(R.string.all_apps_home_button_label));
3733 }
Michael Jurkad7c28052012-04-27 15:43:36 -07003734 return result;
3735 }
3736
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003737 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07003738 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003739 */
Adam Cohen091440a2015-03-18 14:16:05 -07003740 @Thunk class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003741 @Override
3742 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08003743 closeSystemDialogs();
3744 }
3745 }
3746
3747 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08003748 * Receives notifications whenever the appwidgets are reset.
3749 */
3750 private class AppWidgetResetObserver extends ContentObserver {
3751 public AppWidgetResetObserver() {
3752 super(new Handler());
3753 }
3754
3755 @Override
3756 public void onChange(boolean selfChange) {
3757 onAppWidgetReset();
3758 }
3759 }
3760
3761 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07003762 * If the activity is currently paused, signal that we need to run the passed Runnable
3763 * in onResume.
3764 *
3765 * This needs to be called from incoming places where resources might have been loaded
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003766 * while the activity is paused. That is because the Configuration (e.g., rotation) might be
3767 * wrong when we're not running, and if the activity comes back to what the configuration was
3768 * when we were paused, activity is not restarted.
Michael Jurka7607c2f2013-04-03 14:33:19 -07003769 *
3770 * Implementation of the method from LauncherModel.Callbacks.
3771 *
Hyunyoung Song8821ca92015-05-04 16:28:20 -07003772 * @return {@code true} if we are currently paused. The caller might be able to skip some work
Michael Jurka7607c2f2013-04-03 14:33:19 -07003773 */
Michael Jurkac402cd92013-05-20 15:49:32 +02003774 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003775 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003776 if (LOGD) Log.d(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02003777 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07003778 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02003779 }
3780 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07003781 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07003782 return true;
3783 } else {
3784 return false;
3785 }
3786 }
3787
Michael Jurkac402cd92013-05-20 15:49:32 +02003788 private boolean waitUntilResume(Runnable run) {
3789 return waitUntilResume(run, false);
3790 }
3791
Michael Jurka1e2f4652013-07-08 18:03:46 -07003792 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02003793 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003794 }
3795
Michael Jurka7607c2f2013-04-03 14:33:19 -07003796 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003797 * If the activity is currently paused, signal that we need to re-run the loader
3798 * in onResume.
3799 *
3800 * This needs to be called from incoming places where resources might have been loaded
3801 * while we are paused. That is becaues the Configuration might be wrong
3802 * when we're not running, and if it comes back to what it was when we
3803 * were paused, we are not restarted.
3804 *
3805 * Implementation of the method from LauncherModel.Callbacks.
3806 *
3807 * @return true if we are currently paused. The caller might be able to
3808 * skip some work in that case since we will come back again.
3809 */
3810 public boolean setLoadOnResume() {
3811 if (mPaused) {
Sunny Goyal2bba4c32015-05-18 15:42:48 -07003812 if (LOGD) Log.d(TAG, "setLoadOnResume");
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003813 mOnResumeNeedsLoad = true;
3814 return true;
3815 } else {
3816 return false;
3817 }
3818 }
3819
3820 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003821 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003822 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04003823 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07003824 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07003825 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07003826 } else {
3827 return SCREEN_COUNT / 2;
3828 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04003829 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07003830
Joe Onorato9c1289c2009-08-17 11:03:03 -04003831 /**
3832 * Refreshes the shortcuts shown on the workspace.
3833 *
3834 * Implementation of the method from LauncherModel.Callbacks.
3835 */
3836 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07003837 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08003838
Michael Jurka7607c2f2013-04-03 14:33:19 -07003839 // If we're starting binding all over again, clear any bind calls we'd postponed in
3840 // the past (see waitUntilResume) -- we don't need them since we're starting binding
3841 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07003842 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07003843
Winson Chungd64d1762013-08-20 14:37:16 -07003844 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07003845 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07003846 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07003847
Michael Jurka05bf6442011-11-30 20:28:53 -08003848 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07003849 if (mHotseat != null) {
3850 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003851 }
3852 }
3853
Adam Cohendcd297f2013-06-18 13:13:40 -07003854 @Override
3855 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07003856 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07003857
Adam Cohen5084cba2013-09-03 12:01:16 -07003858 // If there are no screens, we need to have an empty screen
3859 if (orderedScreenIds.size() == 0) {
3860 mWorkspace.addExtraEmptyScreen();
3861 }
Winson Chung0e6a7132013-08-23 12:55:10 -07003862
3863 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06003864 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05003865 if (hasCustomContentToLeft()) {
3866 mWorkspace.createCustomContentContainer();
3867 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07003868 }
Winson Chung64359a52013-07-08 17:17:08 -07003869 }
3870
3871 @Override
3872 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08003873 // Log to disk
3874 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
3875 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
3876 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07003877 int count = orderedScreenIds.size();
3878 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07003879 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07003880 }
Adam Cohendcd297f2013-06-18 13:13:40 -07003881 }
3882
Adam Cohen39a06042013-07-19 14:30:12 -07003883 private boolean shouldShowWeightWatcher() {
3884 String spKey = LauncherAppState.getSharedPreferencesKey();
3885 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07003886 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07003887
3888 return show;
3889 }
3890
3891 private void toggleShowWeightWatcher() {
3892 String spKey = LauncherAppState.getSharedPreferencesKey();
3893 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
3894 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
3895
3896 show = !show;
3897
3898 SharedPreferences.Editor editor = sp.edit();
3899 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
3900 editor.commit();
3901
3902 if (mWeightWatcher != null) {
3903 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
3904 }
3905 }
3906
Winson Chungd64d1762013-08-20 14:37:16 -07003907 public void bindAppsAdded(final ArrayList<Long> newScreens,
3908 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07003909 final ArrayList<ItemInfo> addAnimated,
3910 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07003911 Runnable r = new Runnable() {
3912 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07003913 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07003914 }
3915 };
3916 if (waitUntilResume(r)) {
3917 return;
3918 }
3919
Winson Chungd64d1762013-08-20 14:37:16 -07003920 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08003921 if (newScreens != null) {
3922 bindAddScreens(newScreens);
3923 }
Winson Chungd64d1762013-08-20 14:37:16 -07003924
3925 // We add the items without animation on non-visible pages, and with
3926 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08003927 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003928 bindItems(addNotAnimated, 0,
3929 addNotAnimated.size(), false);
3930 }
Adam Cohen76a47a12014-02-05 11:47:43 -08003931 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07003932 bindItems(addAnimated, 0,
3933 addAnimated.size(), true);
3934 }
Winson Chungc58497e2013-09-03 17:48:37 -07003935
Winson Chung87412982013-10-03 18:34:14 -07003936 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07003937 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07003938
Winson Chungb745afb2015-03-02 11:51:23 -08003939 if (addedApps != null && mAppsView != null) {
3940 mAppsView.addApps(addedApps);
Winson Chungc58497e2013-09-03 17:48:37 -07003941 }
Winson Chungd64d1762013-08-20 14:37:16 -07003942 }
3943
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003944 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04003945 * Bind the items start-end from the list.
3946 *
3947 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003948 */
Winson Chung64359a52013-07-08 17:17:08 -07003949 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
3950 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07003951 Runnable r = new Runnable() {
3952 public void run() {
3953 bindItems(shortcuts, start, end, forceAnimateIcons);
3954 }
3955 };
3956 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07003957 return;
3958 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07003959
Winson Chungf0c6ae02012-03-21 16:10:31 -07003960 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07003961 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
3962 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07003963 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07003964 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07003965 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003966 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04003967 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07003968
3969 // Short circuit if we are loading dock items for a configuration which has no dock
3970 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
3971 mHotseat == null) {
3972 continue;
3973 }
3974
Joe Onorato9c1289c2009-08-17 11:03:03 -04003975 switch (item.itemType) {
3976 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
3977 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07003978 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07003979 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07003980
Winson Chung64359a52013-07-08 17:17:08 -07003981 /*
3982 * TODO: FIX collision case
3983 */
Winson Chungce376632013-07-11 16:12:41 -07003984 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
3985 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
3986 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07003987 View v = cl.getChildAt(item.cellX, item.cellY);
3988 Object tag = v.getTag();
3989 String desc = "Collision while binding workspace item: " + item
3990 + ". Collides with " + tag;
3991 if (LauncherAppState.isDogfoodBuild()) {
3992 throw (new RuntimeException(desc));
3993 } else {
3994 Log.d(TAG, desc);
3995 }
Winson Chungce376632013-07-11 16:12:41 -07003996 }
Winson Chung64359a52013-07-08 17:17:08 -07003997 }
3998
Adam Cohendcd297f2013-06-18 13:13:40 -07003999 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4000 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004001 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004002 // Animate all the applications up now
4003 shortcut.setAlpha(0f);
4004 shortcut.setScaleX(0f);
4005 shortcut.setScaleY(0f);
4006 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004007 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004008 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004009 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004010 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004011 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004012 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004013 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004014 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4015 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004016 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004017 default:
4018 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004019 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004020 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004021
Winson Chung997a9232013-07-24 15:33:46 -07004022 if (animateIcons) {
4023 // Animate to the correct page
4024 if (newShortcutsScreenId > -1) {
4025 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004026 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004027 final Runnable startBounceAnimRunnable = new Runnable() {
4028 public void run() {
4029 anim.playTogether(bounceAnims);
4030 anim.start();
4031 }
4032 };
Winson Chung997a9232013-07-24 15:33:46 -07004033 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004034 // We post the animation slightly delayed to prevent slowdowns
4035 // when we are loading right after we return to launcher.
4036 mWorkspace.postDelayed(new Runnable() {
4037 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004038 if (mWorkspace != null) {
4039 mWorkspace.snapToPage(newScreenIndex);
4040 mWorkspace.postDelayed(startBounceAnimRunnable,
4041 NEW_APPS_ANIMATION_DELAY);
4042 }
Winson Chung94d67682013-09-25 16:29:40 -07004043 }
4044 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004045 } else {
4046 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004047 }
4048 }
Winson Chung64359a52013-07-08 17:17:08 -07004049 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004050 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004051 }
4052
Joe Onorato9c1289c2009-08-17 11:03:03 -04004053 /**
4054 * Implementation of the method from LauncherModel.Callbacks.
4055 */
Sunny Goyale2df0622015-04-24 11:27:00 -07004056 public void bindFolders(final LongArrayMap<FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004057 Runnable r = new Runnable() {
4058 public void run() {
4059 bindFolders(folders);
4060 }
4061 };
4062 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004063 return;
4064 }
Sunny Goyale2df0622015-04-24 11:27:00 -07004065 sFolders = folders.clone();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004066 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004067
4068 /**
4069 * Add the views for a widget to the workspace.
4070 *
4071 * Implementation of the method from LauncherModel.Callbacks.
4072 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004073 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004074 Runnable r = new Runnable() {
4075 public void run() {
4076 bindAppWidget(item);
4077 }
4078 };
4079 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004080 return;
4081 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004082
Daniel Sandler843e8602010-06-07 14:59:01 -04004083 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4084 if (DEBUG_WIDGETS) {
4085 Log.d(TAG, "bindAppWidget: " + item);
4086 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004087 final Workspace workspace = mWorkspace;
4088
Adam Cohen59400422014-03-05 18:07:04 -08004089 LauncherAppWidgetProviderInfo appWidgetInfo =
Robin Lee26ace122015-03-16 19:41:43 +00004090 LauncherModel.getProviderInfo(this, item.providerName, item.user);
Adam Cohen59400422014-03-05 18:07:04 -08004091
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004092 if (!mIsSafeModeEnabled
4093 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4094 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004095 if (appWidgetInfo == null) {
4096 if (DEBUG_WIDGETS) {
4097 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4098 + " belongs to component " + item.providerName
4099 + ", as the povider is null");
4100 }
4101 LauncherModel.deleteItemFromDatabase(this, item);
4102 return;
4103 }
4104 // Note: This assumes that the id remap broadcast is received before this step.
4105 // If that is not the case, the id remap will be ignored and user may see the
4106 // click to setup view.
Adam Cohen2e6da152015-05-06 11:42:25 -07004107 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(this, appWidgetInfo, null);
Sunny Goyalff572272014-07-23 13:58:07 -07004108 pendingInfo.spanX = item.spanX;
4109 pendingInfo.spanY = item.spanY;
4110 pendingInfo.minSpanX = item.minSpanX;
4111 pendingInfo.minSpanY = item.minSpanY;
Hyunyoung Song3f471442015-04-08 19:01:34 -07004112 Bundle options = null;
Hyunyoung Songb99ff3e2015-04-23 15:17:50 -07004113 WidgetHostViewLoader.getDefaultOptionsForWidget(this, pendingInfo);
Sunny Goyalff572272014-07-23 13:58:07 -07004114
Sunny Goyalff572272014-07-23 13:58:07 -07004115 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004116 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4117 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004118
4119 // TODO consider showing a permission dialog when the widget is clicked.
4120 if (!success) {
4121 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4122 if (DEBUG_WIDGETS) {
4123 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4124 + " belongs to component " + item.providerName
4125 + ", as the launcher is unable to bing a new widget id");
4126 }
4127 LauncherModel.deleteItemFromDatabase(this, item);
4128 return;
4129 }
4130
4131 item.appWidgetId = newWidgetId;
4132
4133 // If the widget has a configure activity, it is still needs to set it up, otherwise
4134 // the widget is ready to go.
4135 item.restoreStatus = (appWidgetInfo.configure == null)
4136 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4137 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4138
4139 LauncherModel.updateItemInDatabase(this, item);
4140 }
4141
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004142 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004143 final int appWidgetId = item.appWidgetId;
Sunny Goyal651077b2014-06-30 14:15:31 -07004144 if (DEBUG_WIDGETS) {
Adam Cohen59400422014-03-05 18:07:04 -08004145 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component "
4146 + appWidgetInfo.provider);
Sunny Goyal651077b2014-06-30 14:15:31 -07004147 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004148
Sunny Goyal651077b2014-06-30 14:15:31 -07004149 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4150 } else {
4151 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004152 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4153 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004154 view.updateIcon(mIconCache);
4155 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004156 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004157 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004158 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004159
Joe Onorato9c1289c2009-08-17 11:03:03 -04004160 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004161 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004162
Adam Cohendcd297f2013-06-18 13:13:40 -07004163 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004164 item.cellY, item.spanX, item.spanY, false);
Adam Cohen59400422014-03-05 18:07:04 -08004165 if (!item.isCustomWidget()) {
4166 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4167 }
Adam Cohended9f8d2010-11-03 13:25:16 -07004168
Joe Onorato9c1289c2009-08-17 11:03:03 -04004169 workspace.requestLayout();
4170
Daniel Sandler843e8602010-06-07 14:59:01 -04004171 if (DEBUG_WIDGETS) {
4172 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4173 + (SystemClock.uptimeMillis()-start) + "ms");
4174 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004175 }
4176
Sunny Goyalff572272014-07-23 13:58:07 -07004177 /**
4178 * Restores a pending widget.
4179 *
4180 * @param appWidgetId The app widget id
4181 * @param cellInfo The position on screen where to create the widget.
4182 */
4183 private void completeRestoreAppWidget(final int appWidgetId) {
4184 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4185 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4186 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4187 return;
4188 }
4189
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004190 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004191 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4192
4193 mWorkspace.reinflateWidgetsIfNecessary();
4194 LauncherModel.updateItemInDatabase(this, info);
4195 }
4196
Adam Cohen1462de32012-07-24 22:34:36 -07004197 public void onPageBoundSynchronously(int page) {
4198 mSynchronouslyBoundPages.add(page);
4199 }
4200
Joe Onorato9c1289c2009-08-17 11:03:03 -04004201 /**
4202 * Callback saying that there aren't any more items to bind.
4203 *
4204 * Implementation of the method from LauncherModel.Callbacks.
4205 */
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004206 public void finishBindingItems() {
Winson Chungd64d1762013-08-20 14:37:16 -07004207 Runnable r = new Runnable() {
4208 public void run() {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004209 finishBindingItems();
Winson Chungd64d1762013-08-20 14:37:16 -07004210 }
4211 };
4212 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004213 return;
4214 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004215 if (mSavedState != null) {
4216 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004217 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004218 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004219 mSavedState = null;
4220 }
4221
Adam Cohen1462de32012-07-24 22:34:36 -07004222 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004223
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004224 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004225 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004226
4227 // If we received the result of any pending adds while the loader was running (e.g. the
4228 // widget configuration forced an orientation change), process them now.
4229 if (sPendingAddItem != null) {
4230 final long screenId = completeAdd(sPendingAddItem);
4231
4232 // TODO: this moves the user to the page where the pending item was added. Ideally,
4233 // the screen would be guaranteed to exist after bind, and the page would be set through
4234 // the workspace restore process.
4235 mWorkspace.post(new Runnable() {
4236 @Override
4237 public void run() {
4238 mWorkspace.snapToScreenId(screenId);
4239 }
4240 });
4241 sPendingAddItem = null;
4242 }
4243
Sunny Goyal756adbc2015-04-16 15:20:51 -07004244 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
Adam Cohen9211d422014-10-07 18:14:53 -07004245
4246 if (mLauncherCallbacks != null) {
Sunny Goyal66cfdc22015-02-02 13:01:51 -08004247 mLauncherCallbacks.finishBindingItems(false);
Adam Cohen9211d422014-10-07 18:14:53 -07004248 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004249 }
4250
Adam Cohen3ed316a2014-07-23 18:21:20 -07004251 private void sendLoadingCompleteBroadcastIfNecessary() {
4252 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4253 String permission =
4254 getResources().getString(R.string.receive_first_load_broadcast_permission);
4255 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4256 sendBroadcast(intent, permission);
4257 SharedPreferences.Editor editor = mSharedPrefs.edit();
4258 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4259 editor.apply();
4260 }
4261 }
4262
Winson Chunga0b7e862013-09-05 16:03:15 -07004263 public boolean isAllAppsButtonRank(int rank) {
4264 if (mHotseat != null) {
4265 return mHotseat.isAllAppsButtonRank(rank);
4266 }
4267 return false;
4268 }
4269
Winson Chunga2413752012-04-03 14:22:34 -07004270 private boolean canRunNewAppsAnimation() {
4271 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4272 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4273 }
4274
Winson Chungc9168342013-06-26 14:54:55 -07004275 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4276 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4277 PropertyValuesHolder.ofFloat("alpha", 1f),
4278 PropertyValuesHolder.ofFloat("scaleX", 1f),
4279 PropertyValuesHolder.ofFloat("scaleY", 1f));
4280 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4281 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
Sunny Goyal5a1f53b2015-05-27 10:24:24 -07004282 bounceAnim.setInterpolator(new OvershootInterpolator(BOUNCE_ANIMATION_TENSION));
Winson Chungc9168342013-06-26 14:54:55 -07004283 return bounceAnim;
4284 }
4285
Adam Cohen27772732013-11-11 14:00:56 +00004286 public boolean useVerticalBarLayout() {
Sunny Goyaldfaccf62015-05-11 16:30:44 -07004287 return mDeviceProfile.isVerticalBarLayout();
Adam Cohen27772732013-11-11 14:00:56 +00004288 }
4289
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004290 protected Rect getSearchBarBounds() {
Sunny Goyalc6205602015-05-21 20:46:33 -07004291 return mDeviceProfile.getSearchBarBounds(Utilities.isRtl(getResources()));
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004292 }
4293
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004294 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004295 if (mSearchDropTargetBar == null) {
4296 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004297 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004298 if (mQsb != null) {
4299 mSearchDropTargetBar.removeView(mQsb);
4300 mQsb = null;
4301 }
Adam Cohenb0df1b02015-03-18 22:21:40 -07004302 mSearchDropTargetBar.setQsbSearchBar(getOrCreateQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004303 }
4304
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004305 /**
Winson Chungbb785c62015-05-05 10:05:08 -07004306 * A runnable that we can dequeue and re-enqueue when all applications are bound (to prevent
4307 * multiple calls to bind the same list.)
4308 */
4309 @Thunk ArrayList<AppInfo> mTmpAppsList;
4310 private Runnable mBindAllApplicationsRunnable = new Runnable() {
4311 public void run() {
4312 bindAllApplications(mTmpAppsList);
4313 mTmpAppsList = null;
4314 }
4315 };
4316
4317 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004318 * Add the icons for all apps.
4319 *
4320 * Implementation of the method from LauncherModel.Callbacks.
4321 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004322 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Winson Chungbb785c62015-05-05 10:05:08 -07004323 if (waitUntilResume(mBindAllApplicationsRunnable, true)) {
4324 mTmpAppsList = apps;
4325 return;
4326 }
4327
Winson Chungb745afb2015-03-02 11:51:23 -08004328 if (mAppsView != null) {
4329 mAppsView.setApps(apps);
4330 }
Adam Cohen9211d422014-10-07 18:14:53 -07004331 if (mLauncherCallbacks != null) {
4332 mLauncherCallbacks.bindAllApplications(apps);
4333 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004334 }
4335
4336 /**
4337 * A package was updated.
4338 *
4339 * Implementation of the method from LauncherModel.Callbacks.
4340 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004341 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004342 Runnable r = new Runnable() {
4343 public void run() {
4344 bindAppsUpdated(apps);
4345 }
4346 };
4347 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004348 return;
4349 }
4350
Winson Chungb745afb2015-03-02 11:51:23 -08004351 if (mAppsView != null) {
4352 mAppsView.updateApps(apps);
Winson Chungc58497e2013-09-03 17:48:37 -07004353 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004354 }
4355
Sunny Goyal4390ace2014-10-13 11:33:11 -07004356 @Override
4357 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4358 Runnable r = new Runnable() {
4359 public void run() {
4360 bindWidgetsRestored(widgets);
4361 }
4362 };
4363 if (waitUntilResume(r)) {
4364 return;
4365 }
4366 mWorkspace.widgetsRestored(widgets);
4367 }
4368
Joe Onorato9c1289c2009-08-17 11:03:03 -04004369 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004370 * Some shortcuts were updated in the background.
4371 *
4372 * Implementation of the method from LauncherModel.Callbacks.
4373 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004374 @Override
4375 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4376 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004377 Runnable r = new Runnable() {
4378 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004379 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004380 }
4381 };
4382 if (waitUntilResume(r)) {
4383 return;
4384 }
4385
Sunny Goyal4390ace2014-10-13 11:33:11 -07004386 if (!updated.isEmpty()) {
4387 mWorkspace.updateShortcuts(updated);
4388 }
4389
4390 if (!removed.isEmpty()) {
4391 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4392 for (ShortcutInfo si : removed) {
4393 removedComponents.add(si.getTargetComponent());
4394 }
4395 mWorkspace.removeItemsByComponentName(removedComponents, user);
4396 // Notify the drag controller
4397 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004398 }
4399 }
4400
4401 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004402 * Update the state of a package, typically related to install state.
4403 *
4404 * Implementation of the method from LauncherModel.Callbacks.
4405 */
Sunny Goyale755d462014-07-22 13:48:29 -07004406 @Override
Sunny Goyal756adbc2015-04-16 15:20:51 -07004407 public void bindRestoreItemsChange(final HashSet<ItemInfo> updates) {
4408 Runnable r = new Runnable() {
4409 public void run() {
4410 bindRestoreItemsChange(updates);
4411 }
4412 };
4413 if (waitUntilResume(r)) {
4414 return;
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004415 }
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004416
Sunny Goyal756adbc2015-04-16 15:20:51 -07004417 mWorkspace.updateRestoreItems(updates);
Sunny Goyala22666f2014-09-18 13:25:15 -07004418 }
4419
4420 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004421 * A package was uninstalled. We take both the super set of packageNames
4422 * in addition to specific applications to remove, the reason being that
4423 * this can be called when a package is updated as well. In that scenario,
4424 * we only remove specific components from the workspace, where as
4425 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004426 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004427 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004428 * Implementation of the method from LauncherModel.Callbacks.
4429 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004430 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004431 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004432 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004433 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004434 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004435 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004436 }
Winson Chungd64d1762013-08-20 14:37:16 -07004437 };
4438 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004439 return;
4440 }
4441
Sunny Goyal1a745e82014-10-02 15:58:31 -07004442 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004443 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4444 for (AppInfo info : appInfos) {
4445 removedComponents.add(info.componentName);
4446 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004447 if (!packageNames.isEmpty()) {
4448 mWorkspace.removeItemsByPackageName(packageNames, user);
4449 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004450 if (!removedComponents.isEmpty()) {
4451 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004452 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004453 // Notify the drag controller
4454 mDragController.onAppsRemoved(packageNames, removedComponents);
4455
Sunny Goyal1a745e82014-10-02 15:58:31 -07004456 } else {
4457 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004458 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004459
Winson Chungdf95eb12013-10-16 14:57:07 -07004460 // Update AllApps
Winson Chungb745afb2015-03-02 11:51:23 -08004461 if (mAppsView != null) {
4462 mAppsView.removeApps(appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004463 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004464 }
Joe Onoratobe386092009-11-17 17:32:16 -08004465
Michael Jurkac402cd92013-05-20 15:49:32 +02004466 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004467 public void run() {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004468 bindAllPackages(mWidgetsModel);
Winson Chung83892cc2013-05-01 16:53:33 -07004469 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004470 };
Hyunyoung Song3f471442015-04-08 19:01:34 -07004471
Hyunyoung Song8821ca92015-05-04 16:28:20 -07004472 @Override
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004473 public void bindAllPackages(final WidgetsModel model) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004474 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004475 mWidgetsModel = model;
Winson Chung83892cc2013-05-01 16:53:33 -07004476 return;
4477 }
4478
Hyunyoung Song2bd3d7d2015-05-21 13:04:53 -07004479 if (mWidgetsView != null && model != null) {
4480 mWidgetsView.addWidgets(model);
4481 mWidgetsModel = null;
Winson Chung785d2eb2011-04-14 16:08:02 -07004482 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004483 }
4484
Winson Chung400438b2011-01-16 17:53:48 -08004485 private int mapConfigurationOriActivityInfoOri(int configOri) {
4486 final Display d = getWindowManager().getDefaultDisplay();
4487 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4488 switch (d.getRotation()) {
4489 case Surface.ROTATION_0:
4490 case Surface.ROTATION_180:
4491 // We are currently in the same basic orientation as the natural orientation
4492 naturalOri = configOri;
4493 break;
4494 case Surface.ROTATION_90:
4495 case Surface.ROTATION_270:
4496 // We are currently in the other basic orientation to the natural orientation
4497 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4498 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4499 break;
4500 }
4501
4502 int[] oriMap = {
4503 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4504 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4505 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4506 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4507 };
4508 // Since the map starts at portrait, we need to offset if this device's natural orientation
4509 // is landscape.
4510 int indexOffset = 0;
4511 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4512 indexOffset = 1;
4513 }
4514 return oriMap[(d.getRotation() + indexOffset) % 4];
4515 }
Adam Cohen446e9402011-09-15 18:21:21 -07004516
Winson Chung4b919f82012-05-01 10:44:08 -07004517 public void lockScreenOrientation() {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004518 if (mRotationEnabled) {
Sunny Goyal3c1865a2015-02-10 14:28:44 -08004519 if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR2) {
4520 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4521 .getConfiguration().orientation));
4522 } else {
4523 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LOCKED);
4524 }
Winson Chung4b919f82012-05-01 10:44:08 -07004525 }
4526 }
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004527
Winson Chung4b919f82012-05-01 10:44:08 -07004528 public void unlockScreenOrientation(boolean immediate) {
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004529 if (mRotationEnabled) {
Winson Chung4b919f82012-05-01 10:44:08 -07004530 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004531 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004532 } else {
4533 mHandler.postDelayed(new Runnable() {
4534 public void run() {
4535 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4536 }
4537 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004538 }
Winson Chung4b919f82012-05-01 10:44:08 -07004539 }
Winson Chung400438b2011-01-16 17:53:48 -08004540 }
4541
Winson Chunge43a1e72014-01-15 10:33:02 -08004542 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004543 if (mLauncherCallbacks != null) {
4544 return mLauncherCallbacks.isLauncherPreinstalled();
4545 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004546 PackageManager pm = getPackageManager();
4547 try {
4548 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4549 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4550 return true;
4551 } else {
4552 return false;
4553 }
4554 } catch (NameNotFoundException e) {
4555 e.printStackTrace();
4556 return false;
4557 }
4558 }
4559
Adam Cohenea90f832014-05-21 15:03:34 -07004560 /**
4561 * This method indicates whether or not we should suggest default wallpaper dimensions
4562 * when our wallpaper cropper was not yet used to set a wallpaper.
4563 */
4564 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004565 if (mLauncherCallbacks != null) {
4566 return mLauncherCallbacks.overrideWallpaperDimensions();
4567 }
Adam Cohenea90f832014-05-21 15:03:34 -07004568 return true;
4569 }
4570
Adam Cohen432609a2014-03-13 17:03:22 -07004571 /**
4572 * To be overridden by subclasses to indicate that there is an activity to launch
4573 * before showing the standard launcher experience.
4574 */
4575 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004576 if (mLauncherCallbacks != null) {
4577 return mLauncherCallbacks.hasFirstRunActivity();
4578 }
Adam Cohen432609a2014-03-13 17:03:22 -07004579 return false;
4580 }
4581
4582 /**
4583 * To be overridden by subclasses to launch any first run activity
4584 */
4585 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004586 if (mLauncherCallbacks != null) {
4587 return mLauncherCallbacks.getFirstRunActivity();
4588 }
Adam Cohen432609a2014-03-13 17:03:22 -07004589 return null;
4590 }
4591
Winson Chung2826a402015-04-17 16:18:53 -07004592 /**
4593 * Returns whether the launcher callbacks overrides search in all apps.
Winson Chung2826a402015-04-17 16:18:53 -07004594 */
4595 @Thunk boolean isAllAppsSearchOverridden() {
Winson Chung83f59ab2015-05-05 17:21:58 -07004596 if (DISABLE_ALL_APPS_SEARCH_INTEGRATION) {
4597 return false;
4598 }
4599
Winson Chung2826a402015-04-17 16:18:53 -07004600 if (mLauncherCallbacks != null) {
4601 return mLauncherCallbacks.overrideAllAppsSearch();
4602 }
4603 return false;
4604 }
4605
Winson Chunga6945242014-01-08 14:04:34 -08004606 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004607 return !ActivityManager.isRunningInTestHarness() &&
4608 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004609 }
4610
Adam Cohen4a9423d2014-03-28 14:22:31 -07004611 protected boolean hasRunFirstRunActivity() {
4612 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4613 }
4614
Adam Cohen432609a2014-03-13 17:03:22 -07004615 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004616 if (shouldRunFirstRunActivity() &&
4617 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004618 Intent firstRunIntent = getFirstRunActivity();
4619 if (firstRunIntent != null) {
4620 startActivity(firstRunIntent);
4621 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004622 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004623 }
4624 }
Adam Cohen432609a2014-03-13 17:03:22 -07004625 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004626 }
4627
4628 private void markFirstRunActivityShown() {
4629 SharedPreferences.Editor editor = mSharedPrefs.edit();
4630 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4631 editor.apply();
4632 }
4633
Rahul Chaturvedi7fc77ca2015-05-19 18:02:16 -07004634 private void showSettingsActivity() {
4635 startActivity(new Intent(this, SettingsActivity.class));
4636 }
4637
Adam Cohen432609a2014-03-13 17:03:22 -07004638 /**
4639 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4640 * screen that must be displayed and dismissed.
4641 */
4642 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004643 if (mLauncherCallbacks != null) {
4644 return mLauncherCallbacks.hasDismissableIntroScreen();
4645 }
Adam Cohen432609a2014-03-13 17:03:22 -07004646 return false;
4647 }
4648
4649 /**
4650 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4651 */
4652 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004653 if (mLauncherCallbacks != null) {
4654 return mLauncherCallbacks.getIntroScreen();
4655 }
Adam Cohen432609a2014-03-13 17:03:22 -07004656 return null;
4657 }
4658
4659 /**
4660 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4661 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4662 */
4663 private boolean shouldShowIntroScreen() {
4664 return hasDismissableIntroScreen() &&
4665 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4666 }
4667
4668 protected void showIntroScreen() {
4669 View introScreen = getIntroScreen();
4670 changeWallpaperVisiblity(false);
4671 if (introScreen != null) {
4672 mDragLayer.showOverlayView(introScreen);
4673 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004674 if (mLauncherOverlayContainer != null) {
4675 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4676 }
Adam Cohen432609a2014-03-13 17:03:22 -07004677 }
4678
4679 public void dismissIntroScreen() {
4680 markIntroScreenDismissed();
4681 if (showFirstRunActivity()) {
4682 // We delay hiding the intro view until the first run activity is showing. This
4683 // avoids a blip.
4684 mWorkspace.postDelayed(new Runnable() {
4685 @Override
4686 public void run() {
4687 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004688 if (mLauncherOverlayContainer != null) {
4689 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4690 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004691 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004692 }
4693 }, ACTIVITY_START_DELAY);
4694 } else {
4695 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004696 if (mLauncherOverlayContainer != null) {
4697 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4698 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004699 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004700 }
4701 changeWallpaperVisiblity(true);
4702 }
4703
4704 private void markIntroScreenDismissed() {
4705 SharedPreferences.Editor editor = mSharedPrefs.edit();
4706 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4707 editor.apply();
4708 }
4709
Adam Cohen091440a2015-03-18 14:16:05 -07004710 @Thunk void showFirstRunClings() {
Sunny Goyal88d60f12014-09-08 03:47:29 -07004711 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4712 // on the device, then we always show the first run cling experience (or if there is no
4713 // launcher2). Otherwise, we prompt the user upon started for migration
4714 LauncherClings launcherClings = new LauncherClings(this);
4715 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4716 if (mModel.canMigrateFromOldLauncherDb(this)) {
4717 launcherClings.showMigrationCling();
4718 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004719 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004720 }
4721 }
4722 }
4723
Winson Chunga6945242014-01-08 14:04:34 -08004724 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004725 if (mWorkspace != null) mWorkspace.setAlpha(1f);
4726 if (mHotseat != null) mHotseat.setAlpha(1f);
4727 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
4728 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004729 }
4730
4731 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08004732 if (mWorkspace != null) mWorkspace.setAlpha(0f);
4733 if (mHotseat != null) mHotseat.setAlpha(0f);
4734 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
4735 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08004736 }
4737
Mathew Inwood72fbec12013-11-19 15:45:07 +00004738 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01004739 // Called from search suggestion, not supported in other profiles.
4740 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
4741 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
4742 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
4743 myUser);
4744 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00004745 return null;
4746 }
Sunny Goyal4fbc3822015-02-18 16:46:50 -08004747 return new AppInfo(this, activityInfo, myUser, mIconCache);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004748 }
4749
4750 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4751 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01004752 // Called from search suggestion, not supported in other profiles.
4753 return createShortcutDragInfo(shortcutIntent, caption, icon,
4754 UserHandleCompat.myUserHandle());
4755 }
4756
4757 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
4758 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01004759 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01004760 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01004761 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004762 }
4763
Mathew Inwoodb90860a2014-04-11 17:17:39 +01004764 protected void moveWorkspaceToDefaultScreen() {
4765 mWorkspace.moveToDefaultScreen(false);
4766 }
4767
Mathew Inwood72fbec12013-11-19 15:45:07 +00004768 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
4769 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07004770 mWorkspace.onExternalDragStartedWithItem(dragView);
4771 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00004772 }
4773
Anjali Koppalf5d29132014-02-28 13:33:27 -08004774 @Override
4775 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07004776 if (mLauncherCallbacks != null) {
4777 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
4778 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08004779 }
4780
Winson Chung80baf5a2010-08-09 16:03:15 -07004781 /**
Joe Onoratobe386092009-11-17 17:32:16 -08004782 * Prints out out state for debugging.
4783 */
4784 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04004785 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08004786 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08004787 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
4788 Log.d(TAG, "mRestoring=" + mRestoring);
4789 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
4790 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004791 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08004792 mModel.dumpState();
Hyunyoung Song3f471442015-04-08 19:01:34 -07004793 // TODO(hyunyoungs): add mWidgetsView.dumpState(); or mWidgetsModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07004794
Daniel Sandler325dc232013-06-05 22:57:57 -04004795 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08004796 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004797
4798 @Override
4799 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
4800 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07004801 synchronized (sDumpLogs) {
4802 writer.println(" ");
4803 writer.println("Debug logs: ");
4804 for (int i = 0; i < sDumpLogs.size(); i++) {
4805 writer.println(" " + sDumpLogs.get(i));
4806 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004807 }
Adam Cohen9211d422014-10-07 18:14:53 -07004808 if (mLauncherCallbacks != null) {
4809 mLauncherCallbacks.dump(prefix, fd, writer, args);
4810 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07004811 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004812
4813 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07004814 if (DEBUG_DUMP_LOG) {
4815 synchronized (sDumpLogs) {
4816 Log.d(TAG, "");
4817 Log.d(TAG, "*********************");
4818 Log.d(TAG, "Launcher debug logs: ");
4819 for (int i = 0; i < sDumpLogs.size(); i++) {
4820 Log.d(TAG, " " + sDumpLogs.get(i));
4821 }
4822 Log.d(TAG, "*********************");
4823 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07004824 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004825 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004826 }
4827
4828 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004829 addDumpLog(tag, log, null, debugLog);
4830 }
4831
4832 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004833 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05004834 if (e != null) {
4835 Log.d(tag, log, e);
4836 } else {
4837 Log.d(tag, log);
4838 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004839 }
Winson Chungede41292013-09-19 16:27:36 -07004840 if (DEBUG_DUMP_LOG) {
4841 sDateStamp.setTime(System.currentTimeMillis());
4842 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05004843 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
4844 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07004845 }
Winson Chungede41292013-09-19 16:27:36 -07004846 }
Adam Cohen4caf2982013-08-20 18:54:31 -07004847 }
4848
Adam Cohen59400422014-03-05 18:07:04 -08004849 public static CustomAppWidget getCustomAppWidget(String name) {
4850 return sCustomAppWidgets.get(name);
4851 }
4852
4853 public static HashMap<String, CustomAppWidget> getCustomAppWidgets() {
4854 return sCustomAppWidgets;
4855 }
4856
Winson Chungede41292013-09-19 16:27:36 -07004857 public void dumpLogsToLocalData() {
4858 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01004859 new AsyncTask<Void, Void, Void>() {
4860 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07004861 boolean success = false;
4862 sDateStamp.setTime(sRunStart);
4863 String FILENAME = sDateStamp.getMonth() + "-"
4864 + sDateStamp.getDay() + "_"
4865 + sDateStamp.getHours() + "-"
4866 + sDateStamp.getMinutes() + "_"
4867 + sDateStamp.getSeconds() + ".txt";
4868
4869 FileOutputStream fos = null;
4870 File outFile = null;
4871 try {
4872 outFile = new File(getFilesDir(), FILENAME);
4873 outFile.createNewFile();
4874 fos = new FileOutputStream(outFile);
4875 } catch (Exception e) {
4876 e.printStackTrace();
4877 }
4878 if (fos != null) {
4879 PrintWriter writer = new PrintWriter(fos);
4880
4881 writer.println(" ");
4882 writer.println("Debug logs: ");
4883 synchronized (sDumpLogs) {
4884 for (int i = 0; i < sDumpLogs.size(); i++) {
4885 writer.println(" " + sDumpLogs.get(i));
4886 }
4887 }
4888 writer.close();
4889 }
4890 try {
4891 if (fos != null) {
4892 fos.close();
4893 success = true;
4894 }
4895 } catch (IOException e) {
4896 e.printStackTrace();
4897 }
Michael Jurka43467462013-11-14 12:03:58 +01004898 return null;
Winson Chungede41292013-09-19 16:27:36 -07004899 }
Michael Jurka43467462013-11-14 12:03:58 +01004900 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07004901 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07004902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004903}
Michael Jurka2763be32011-02-24 11:19:57 -08004904
Dan Sandlerd5024042014-01-09 15:01:33 -05004905interface DebugIntents {
4906 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
4907 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00004908}