blob: ac46fd33d7158ff0d60fa0a492c1f30cef5ef074 [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Adam Cohen2854d252014-08-27 16:04:07 -070025import android.animation.TimeInterpolator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000027import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070029import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070030import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
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;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070053import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.Bitmap;
55import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070056import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
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 Cohen2854d252014-08-27 16:04:07 -070085import android.view.ViewAnimationUtils;
Adam Cohen96d30a12013-07-16 18:13:21 -070086import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070087import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070089import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070090import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080091import android.view.accessibility.AccessibilityEvent;
Adam Cohen9bfdb762014-07-21 17:44:06 -070092import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080093import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080094import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070095import android.widget.Advanceable;
96import android.widget.FrameLayout;
97import android.widget.ImageView;
Adam Cohen96d30a12013-07-16 18:13:21 -070098import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -070099
Sunny Goyal651077b2014-06-30 14:15:31 -0700100import com.android.launcher3.DropTarget.DragObject;
101import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700102import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100103import com.android.launcher3.compat.LauncherActivityInfoCompat;
104import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700105import com.android.launcher3.compat.PackageInstallerCompat;
106import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100107import com.android.launcher3.compat.UserHandleCompat;
108import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700109
Adam Cohenc0dcf592011-06-01 15:30:43 -0700110import java.io.DataInputStream;
111import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700112import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700113import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700114import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700115import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700117import java.io.PrintWriter;
Adam Cohen0b395352014-06-09 22:54:36 +0000118import java.lang.reflect.Field;
119import java.lang.reflect.InvocationTargetException;
120import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700121import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700122import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700123import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700124import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700125import java.util.HashMap;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700126import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700127import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000128import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700129
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800130/**
131 * Default launcher application.
132 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100133public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700134 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700135 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800136 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700137 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800138
Daniel Sandlerf061f822013-06-27 13:59:36 -0400139 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400140 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700141 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400142 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700143 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700144
Dan Sandlerd5024042014-01-09 15:01:33 -0500145 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
146
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800147 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700148 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700150 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700151 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800152
Michael Jurka8b805b12012-04-18 14:23:14 -0700153 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700154 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700155
Mathew Inwood876a8462013-06-14 14:12:41 +0100156 /**
157 * IntentStarter uses request codes starting with this. This must be greater than all activity
158 * request codes used internally.
159 */
160 protected static final int REQUEST_LAST = 100;
161
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800162 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
163
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800164 static final int SCREEN_COUNT = 5;
165 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800166
Michael Jurka0a457bf2012-11-19 14:05:05 -0800167 // To turn on these properties, type
168 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800170 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800171
Winson Chung2672ff92012-05-04 16:22:30 -0700172 // The Intent extra that defines whether to ignore the launch animation
173 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400174 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700175
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800176 // Type: int
177 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700178 // Type: int
179 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800180 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700181 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
182 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
184 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700185 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: boolean
189 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
190 // Type: long
191 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700192 // Type: int
193 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
194 // Type: int
195 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
196 // Type: parcelable
197 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000198 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800199 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000200 // Type: int[]
201 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800202
Adam Cohen432609a2014-03-13 17:03:22 -0700203 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800204 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
205
Adam Cohen3ed316a2014-07-23 18:21:20 -0700206 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
207 static final String ACTION_FIRST_LOAD_COMPLETE =
208 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
209
Adam Cohen39a06042013-07-19 14:30:12 -0700210 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700211 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700212
Sunny Goyal594d76d2014-11-06 10:12:54 -0800213 private static final String QSB_WIDGET_ID = "qsb_widget_id";
214 private static final String QSB_WIDGET_PROVIDER = "qsb_widget_provider";
215
Winson Chunga6945242014-01-08 14:04:34 -0800216 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
217
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700218 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700219 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700220 private State mState = State.WORKSPACE;
221 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700222
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700223 private boolean mIsSafeModeEnabled;
224
Adam Cohenc2d6e892014-10-16 09:49:24 -0700225 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
226 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700227 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700228
Joe Onorato9c1289c2009-08-17 11:03:03 -0400229 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700230 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
231 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700232 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800233
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800235 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800236
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000237 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
238 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
239
Winson Chunga2413752012-04-03 14:22:34 -0700240 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700241 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
242 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700243 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700244 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700245
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800246 private final BroadcastReceiver mCloseSystemDialogsReceiver
247 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800248 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
249
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800250 private LayoutInflater mInflater;
251
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800252 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700253 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800254 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800255 private DragLayer mDragLayer;
256 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700257 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700258
Sunny Goyalffe83f12014-08-14 17:39:34 -0700259 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700260 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700261
Michael Jurkac9d95c52011-08-29 14:03:34 -0700262 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700263 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800264 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700265
Michael Jurka0280c3b2010-09-17 15:00:07 -0700266 private int[] mTmpAddItemCellCoordinates = new int[2];
267
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800268 private FolderInfo mFolderInfo;
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;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700276 private AppsCustomizeTabHost mAppsCustomizeTabHost;
277 private AppsCustomizePagedView mAppsCustomizeContent;
278 private boolean mAutoAdvanceRunning = false;
Sunny Goyal594d76d2014-11-06 10:12:54 -0800279 private AppWidgetHostView mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800280
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700282 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
283 // scroll issues (because the workspace may not have been measured yet) and extra work.
284 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
285 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286
287 private SpannableStringBuilder mDefaultKeySsb = null;
288
Joe Onorato9c1289c2009-08-17 11:03:03 -0400289 private boolean mWorkspaceLoading = true;
290
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800291 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292 private boolean mRestoring;
293 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700294 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800295
Michael Jurka1e2f4652013-07-08 18:03:46 -0700296 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700297 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
298
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800299 private Bundle mSavedInstanceState;
300
Joe Onorato9c1289c2009-08-17 11:03:03 -0400301 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800302 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800303 private boolean mUserPresent = true;
304 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700305 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800306 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400307
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700308 private static LocaleConfiguration sLocaleConfiguration = null;
309
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700310 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700311
Adam Cohen61f560d2013-09-30 15:58:20 -0700312 private View.OnTouchListener mHapticFeedbackTouchListener;
313
Adam Cohended9f8d2010-11-03 13:25:16 -0700314 // Related to the auto-advancing of widgets
315 private final int ADVANCE_MSG = 1;
316 private final int mAdvanceInterval = 20000;
317 private final int mAdvanceStagger = 250;
318 private long mAutoAdvanceSentTime;
319 private long mAutoAdvanceTimeLeft = -1;
320 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
321 new HashMap<View, AppWidgetProviderInfo>();
322
Winson Chung400438b2011-01-16 17:53:48 -0800323 // Determines how long to wait after a rotation before restoring the screen orientation to
324 // match the sensor state.
325 private final int mRestoreScreenOrientationDelay = 500;
326
Craig Mautner360310b2012-10-26 15:13:08 -0700327 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700328
Adam Cohen1462de32012-07-24 22:34:36 -0700329 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700330 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700331
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700332 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700333 static Date sDateStamp = new Date();
334 static DateFormat sDateFormat =
335 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
336 static long sRunStart = System.currentTimeMillis();
337 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700338
Winson Chung46353de2012-02-16 14:05:10 -0800339 // We only want to get the SharedPreferences once since it does an FS stat each time we get
340 // it from the context.
341 private SharedPreferences mSharedPrefs;
342
Adam Cohene25af792013-06-06 23:08:25 -0700343 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
344
Winson Chungf0c6ae02012-03-21 16:10:31 -0700345 // Holds the page that we need to animate to, and the icon views that we need to animate up
346 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700347 private ImageView mFolderIconImageView;
348 private Bitmap mFolderIconBitmap;
349 private Canvas mFolderIconCanvas;
350 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700351
Michael Jurkaddd62e92011-02-16 17:49:14 -0800352 private BubbleTextView mWaitingForResume;
353
Michael Jurkac1f5d262011-09-30 19:32:27 -0700354 private Runnable mBuildLayersRunnable = new Runnable() {
355 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700356 if (mWorkspace != null) {
357 mWorkspace.buildPageHardwareLayers();
358 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700359 }
360 };
361
Adam Cohendb364c32014-05-20 14:23:40 -0700362 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800363
364 private static class PendingAddArguments {
365 int requestCode;
366 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700367 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700368 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800369 int cellX;
370 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700371 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800372 }
373
Daniel Sandlerff02d492013-08-05 02:12:05 -0400374 private Stats mStats;
375
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700376 FocusIndicatorView mFocusHandler;
377
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800378 @Override
379 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700380 if (DEBUG_STRICT_MODE) {
381 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
382 .detectDiskReads()
383 .detectDiskWrites()
384 .detectNetwork() // or .detectAll() for all detectable problems
385 .penaltyLog()
386 .build());
387 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
388 .detectLeakedSqlLiteObjects()
389 .detectLeakedClosableObjects()
390 .penaltyLog()
391 .penaltyDeath()
392 .build());
393 }
394
Adam Cohen9211d422014-10-07 18:14:53 -0700395 if (mLauncherCallbacks != null) {
396 mLauncherCallbacks.preOnCreate();
397 }
398
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800399 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400400
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400401 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400402 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700403 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700404
Winson Chung5f8afe62013-08-12 16:19:28 -0700405 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700406 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700407
408 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400409 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700410 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700411 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800412 mModel = app.setLauncher(this);
413 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700414 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400415 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800416 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700417
Daniel Sandlerff02d492013-08-05 02:12:05 -0400418 mStats = new Stats(this);
419
Sunny Goyalffe83f12014-08-14 17:39:34 -0700420 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700421
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700422 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
423 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700424
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700425 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
426 // this also ensures that any synchronous binding below doesn't re-trigger another
427 // LauncherModel load.
428 mPaused = false;
429
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800430 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200431 android.os.Debug.startMethodTracing(
432 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800433 }
434
435 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800436 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700437
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800438 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100439 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800441 registerContentObservers();
442
Joe Onorato7c312c12009-08-13 21:36:53 -0700443 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700444
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 mSavedState = savedInstanceState;
446 restoreState(mSavedState);
447
448 if (PROFILE_STARTUP) {
449 android.os.Debug.stopMethodTracing();
450 }
451
452 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700453 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700454 // If the user leaves launcher, then we should just load items asynchronously when
455 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500456 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700457 } else {
458 // We only load the page synchronously if the user rotates (or triggers a
459 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800460 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700461 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 }
463
464 // For handling default keys
465 mDefaultKeySsb = new SpannableStringBuilder();
466 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800467
468 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
469 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800470
Adam Cohenf9426d52012-06-04 17:26:21 -0700471 // On large interfaces, we want the screen to auto-rotate based on the current orientation
472 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700473
Adam Cohen9211d422014-10-07 18:14:53 -0700474 if (mLauncherCallbacks != null) {
475 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700476 if (mLauncherCallbacks.hasLauncherOverlay()) {
477 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700478 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
479 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
480 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700481 mWorkspace.setLauncherOverlay(mLauncherOverlay);
482 }
Adam Cohen9211d422014-10-07 18:14:53 -0700483 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700484
485 if (shouldShowIntroScreen()) {
486 showIntroScreen();
487 } else {
488 showFirstRunActivity();
489 showFirstRunClings();
490 }
Adam Cohen9211d422014-10-07 18:14:53 -0700491 }
492
493 private LauncherCallbacks mLauncherCallbacks;
494
495 public void onPostCreate(Bundle savedInstanceState) {
496 super.onPostCreate(savedInstanceState);
497 if (mLauncherCallbacks != null) {
498 mLauncherCallbacks.onPostCreate(savedInstanceState);
499 }
500 }
501
502 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
503 mLauncherCallbacks = callbacks;
504 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700505 }
506
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700507 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700508 public void onLauncherProviderChange() {
509 if (mLauncherCallbacks != null) {
510 mLauncherCallbacks.onLauncherProviderChange();
511 }
512 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700513
Adam Cohen9211d422014-10-07 18:14:53 -0700514 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700515 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700516 if (mLauncherCallbacks != null) {
517 return mLauncherCallbacks.hasCustomContentToLeft();
518 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700519 return false;
520 }
521
Dave Hawkeya8881582013-09-17 15:55:33 -0600522 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500523 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600524 * {@link #addToCustomContentPage}. This will only be invoked if
525 * {@link #hasCustomContentToLeft()} is {@code true}.
526 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500527 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700528 if (mLauncherCallbacks != null) {
529 mLauncherCallbacks.populateCustomContentContainer();
530 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600531 }
532
533 /**
534 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
535 * ensure the custom content page is added or removed if necessary.
536 */
537 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600538 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600539 // Not bound yet, wait for bindScreens to be called.
540 return;
541 }
542
543 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
544 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500545 mWorkspace.createCustomContentContainer();
546 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600547 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
548 mWorkspace.removeCustomContentPage();
549 }
550 }
551
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800552 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700553 if (sLocaleConfiguration == null) {
554 new AsyncTask<Void, Void, LocaleConfiguration>() {
555 @Override
556 protected LocaleConfiguration doInBackground(Void... unused) {
557 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
558 readConfiguration(Launcher.this, localeConfiguration);
559 return localeConfiguration;
560 }
561
562 @Override
563 protected void onPostExecute(LocaleConfiguration result) {
564 sLocaleConfiguration = result;
565 checkForLocaleChange(); // recursive, but now with a locale configuration
566 }
567 }.execute();
568 return;
569 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700570
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800571 final Configuration configuration = getResources().getConfiguration();
572
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700573 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800574 final String locale = configuration.locale.toString();
575
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700576 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800577 final int mcc = configuration.mcc;
578
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700579 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800580 final int mnc = configuration.mnc;
581
Romain Guy84f296c2009-11-04 15:00:44 -0800582 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583
Romain Guy84f296c2009-11-04 15:00:44 -0800584 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700585 sLocaleConfiguration.locale = locale;
586 sLocaleConfiguration.mcc = mcc;
587 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700588
Joe Onorato0589f0f2010-02-08 13:44:00 -0800589 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700590
591 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100592 new AsyncTask<Void, Void, Void>() {
593 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700594 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100595 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700596 }
Michael Jurka43467462013-11-14 12:03:58 +0100597 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700598 }
599 }
600
601 private static class LocaleConfiguration {
602 public String locale;
603 public int mcc = -1;
604 public int mnc = -1;
605 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700606
Romain Guy98d01652009-06-30 16:21:04 -0700607 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
608 DataInputStream in = null;
609 try {
Helena Josol28db2802014-10-09 17:04:09 +0100610 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700611 configuration.locale = in.readUTF();
612 configuration.mcc = in.readInt();
613 configuration.mnc = in.readInt();
614 } catch (FileNotFoundException e) {
615 // Ignore
616 } catch (IOException e) {
617 // Ignore
618 } finally {
619 if (in != null) {
620 try {
621 in.close();
622 } catch (IOException e) {
623 // Ignore
624 }
625 }
626 }
627 }
628
629 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
630 DataOutputStream out = null;
631 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100632 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100633 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700634 out.writeUTF(configuration.locale);
635 out.writeInt(configuration.mcc);
636 out.writeInt(configuration.mnc);
637 out.flush();
638 } catch (FileNotFoundException e) {
639 // Ignore
640 } catch (IOException e) {
641 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100642 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700643 } finally {
644 if (out != null) {
645 try {
646 out.close();
647 } catch (IOException e) {
648 // Ignore
649 }
650 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800651 }
652 }
653
Anton Hanssona2f665f2013-09-26 12:18:32 +0100654 public Stats getStats() {
655 return mStats;
656 }
657
Bjorn Bringertc459e522013-06-07 19:36:01 +0100658 public LayoutInflater getInflater() {
659 return mInflater;
660 }
661
Winson Chung36a62fe2012-05-06 18:04:42 -0700662 boolean isDraggingEnabled() {
663 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
664 // that is subsequently removed from the workspace in startBinding().
665 return !mModel.isLoadingWorkspace();
666 }
667
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800668 static int getScreen() {
669 synchronized (sLock) {
670 return sScreen;
671 }
672 }
673
674 static void setScreen(int screen) {
675 synchronized (sLock) {
676 sScreen = screen;
677 }
678 }
679
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000680 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100681 if (Build.VERSION.SDK_INT >= 17) {
682 return View.generateViewId();
683 } else {
684 // View.generateViewId() is not available. The following fallback logic is a copy
685 // of its implementation.
686 for (;;) {
687 final int result = sNextGeneratedId.get();
688 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
689 int newValue = result + 1;
690 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
691 if (sNextGeneratedId.compareAndSet(result, newValue)) {
692 return result;
693 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000694 }
695 }
696 }
697
698 public int getViewIdForItem(ItemInfo info) {
699 // This cast is safe given the > 2B range for int.
700 int itemId = (int) info.id;
701 if (mItemIdToViewId.containsKey(itemId)) {
702 return mItemIdToViewId.get(itemId);
703 }
704 int viewId = generateViewId();
705 mItemIdToViewId.put(itemId, viewId);
706 return viewId;
707 }
708
709 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700710 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
711 * a configuration step, this allows the proper animations to run after other transitions.
712 */
Adam Cohendb364c32014-05-20 14:23:40 -0700713 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700714 long screenId = args.screenId;
715 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
716 // When the screen id represents an actual screen (as opposed to a rank) we make sure
717 // that the drop page actually exists.
718 screenId = ensurePendingDropLayoutExists(args.screenId);
719 }
Adam Cohendb364c32014-05-20 14:23:40 -0700720
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800721 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800722 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700723 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700724 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700725 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800726 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700727 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700728 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700729 case REQUEST_RECONFIGURE_APPWIDGET:
730 completeRestoreAppWidget(args.appWidgetId);
731 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800732 }
Michael Jurka27614d22012-04-02 06:40:22 -0700733 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
734 // if you turned the screen off and then back while in All Apps, Launcher would not
735 // return to the workspace. Clearing mAddInfo.container here fixes this issue
736 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700737 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800738 }
739
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800740 private void handleActivityResult(
Michael Jurka8b805b12012-04-18 14:23:14 -0700741 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700742 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700743 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700744 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800745 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700746
Adam Cohenad4e15c2013-10-17 16:21:35 -0700747 Runnable exitSpringLoaded = new Runnable() {
748 @Override
749 public void run() {
750 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
751 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
752 }
753 };
754
Michael Jurka8b805b12012-04-18 14:23:14 -0700755 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700756 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700757 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
758 if (resultCode == RESULT_CANCELED) {
759 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700760 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700761 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700762 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700763 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
764 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700765 }
766 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200767 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
768 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
769 mWorkspace.exitOverviewMode(false);
770 }
771 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700772 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200773
Adam Cohened66b2b2012-01-23 17:28:51 -0800774 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
775 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700776
Adam Cohendb364c32014-05-20 14:23:40 -0700777 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800778 // We have special handling for widgets
779 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800780 final int appWidgetId;
781 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
782 : -1;
783 if (widgetId < 0) {
784 appWidgetId = pendingAddWidgetId;
785 } else {
786 appWidgetId = widgetId;
787 }
788
Adam Cohenad4e15c2013-10-17 16:21:35 -0700789 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800790 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700791 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
792 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700793 result = RESULT_CANCELED;
794 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700795 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700796 @Override
797 public void run() {
798 exitSpringLoadedDragModeDelayed(false, 0, null);
799 }
800 };
Adam Cohendb364c32014-05-20 14:23:40 -0700801 if (workspaceLocked) {
802 // No need to remove the empty screen if we're mid-binding, as the
803 // the bind will not add the empty screen.
804 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
805 } else {
806 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
807 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
808 }
Winson Chung5aaab772012-04-27 15:24:02 -0700809 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700810 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700811 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
812 // When the screen id represents an actual screen (as opposed to a rank)
813 // we make sure that the drop page actually exists.
814 mPendingAddInfo.screenId =
815 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
816 }
Adam Cohendb364c32014-05-20 14:23:40 -0700817 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
818
819 dropLayout.setDropPending(true);
820 final Runnable onComplete = new Runnable() {
821 @Override
822 public void run() {
823 completeTwoStageWidgetDrop(resultCode, appWidgetId);
824 dropLayout.setDropPending(false);
825 }
826 };
827 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
828 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
829 } else {
830 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
831 mPendingAddInfo);
832 sPendingAddItem = args;
833 }
Winson Chung5aaab772012-04-27 15:24:02 -0700834 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800835 return;
836 }
837
Sunny Goyalff572272014-07-23 13:58:07 -0700838 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
839 if (resultCode == RESULT_OK) {
840 // Update the widget view.
841 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
842 pendingAddWidgetId, mPendingAddInfo);
843 if (workspaceLocked) {
844 sPendingAddItem = args;
845 } else {
846 completeAdd(args);
847 }
848 }
849 // Leave the widget in the pending state if the user canceled the configure.
850 return;
851 }
852
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800853 // The pattern used here is that a user PICKs a specific application,
854 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700855
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800856 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
857 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700858 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700859 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
860 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800861 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700862 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800863 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700864 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700865 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
866 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800867 }
Adam Cohen00074722013-10-11 17:46:09 -0700868 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700869 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700870 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800871 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800872 mDragLayer.clearAnimatedView();
Adam Cohenc7cd2cb2014-11-17 17:45:34 -0800873
874 }
875
876 @Override
877 protected void onActivityResult(
878 final int requestCode, final int resultCode, final Intent data) {
879 handleActivityResult(requestCode, resultCode, data);
880 if (mLauncherCallbacks != null) {
881 mLauncherCallbacks.onActivityResult(requestCode, resultCode, data);
882 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800883 }
884
Adam Cohendb364c32014-05-20 14:23:40 -0700885 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
886 appWidgetId, ItemInfo info) {
887 PendingAddArguments args = new PendingAddArguments();
888 args.requestCode = requestCode;
889 args.intent = data;
890 args.container = info.container;
891 args.screenId = info.screenId;
892 args.cellX = info.cellX;
893 args.cellY = info.cellY;
894 args.appWidgetId = appWidgetId;
895 return args;
896 }
897
898 /**
899 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
900 *
901 * @param screenId the screen id to check
902 * @return the new screen, or screenId if it exists
903 */
904 private long ensurePendingDropLayoutExists(long screenId) {
905 CellLayout dropLayout =
906 (CellLayout) mWorkspace.getScreenWithId(screenId);
907 if (dropLayout == null) {
908 // it's possible that the add screen was removed because it was
909 // empty and a re-bind occurred
910 mWorkspace.addExtraEmptyScreen();
911 return mWorkspace.commitExtraEmptyScreen();
912 } else {
913 return screenId;
914 }
915 }
916
Adam Cohened66b2b2012-01-23 17:28:51 -0800917 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700918 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700919 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800920 Runnable onCompleteRunnable = null;
921 int animationType = 0;
922
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700923 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800924 if (resultCode == RESULT_OK) {
925 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
926 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700927 mPendingAddWidgetInfo);
928 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800929 onCompleteRunnable = new Runnable() {
930 @Override
931 public void run() {
932 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700933 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700934 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
935 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800936 }
937 };
938 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800939 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800940 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700942 if (mDragLayer.getAnimatedView() != null) {
943 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
944 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
945 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700946 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700947 // The animated view may be null in the case of a rotation during widget configuration
948 onCompleteRunnable.run();
949 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800950 }
951
952 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700953 protected void onStop() {
954 super.onStop();
955 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700956
957 if (mLauncherCallbacks != null) {
958 mLauncherCallbacks.onStop();
959 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700960 }
961
962 @Override
963 protected void onStart() {
964 super.onStart();
965 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700966
967 if (mLauncherCallbacks != null) {
968 mLauncherCallbacks.onStart();
969 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700970 }
971
972 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800973 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200974 long startTime = 0;
975 if (DEBUG_RESUME_TIME) {
976 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -0400977 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +0200978 }
Adam Cohen9211d422014-10-07 18:14:53 -0700979
980 if (mLauncherCallbacks != null) {
981 mLauncherCallbacks.preOnResume();
982 }
983
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800984 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -0700985
Winson Chung4a2afa32012-07-19 14:53:05 -0700986 // Restore the previous launcher state
987 if (mOnResumeState == State.WORKSPACE) {
988 showWorkspace(false);
989 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -0800990 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -0700991 }
992 mOnResumeState = State.NONE;
993
Craig Mautner360310b2012-10-26 15:13:08 -0700994 // Background was set to gradient in onPause(), restore to black if in all apps.
995 setWorkspaceBackground(mState == State.WORKSPACE);
996
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800997 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700998 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700999 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001000 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001001 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001002 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001003 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001004 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001005 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1006 // execute them here
1007 long startTimeCallbacks = 0;
1008 if (DEBUG_RESUME_TIME) {
1009 startTimeCallbacks = System.currentTimeMillis();
1010 }
1011
1012 if (mAppsCustomizeContent != null) {
1013 mAppsCustomizeContent.setBulkBind(true);
1014 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001015 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1016 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001017 }
1018 if (mAppsCustomizeContent != null) {
1019 mAppsCustomizeContent.setBulkBind(false);
1020 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001021 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001022 if (DEBUG_RESUME_TIME) {
1023 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1024 (System.currentTimeMillis() - startTimeCallbacks));
1025 }
Michael Jurka447bf842013-05-15 14:52:15 +02001026 }
Michael Jurka54554252013-08-01 12:52:23 +02001027 if (mOnResumeCallbacks.size() > 0) {
1028 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1029 mOnResumeCallbacks.get(i).run();
1030 }
1031 mOnResumeCallbacks.clear();
1032 }
Winson Chunge4e50662012-01-23 14:45:13 -08001033
1034 // Reset the pressed state of icons that were locked in the press state while activities
1035 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001036 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001037 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001038 mWaitingForResume.setStayPressed(false);
1039 }
Winson Chung82963d52013-10-09 11:20:57 -07001040
Adam Cohen06dff352012-06-01 17:17:08 -07001041 // It is possible that widgets can receive updates while launcher is not in the foreground.
1042 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1043 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1044 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001045 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001046
Winson Chung780fe592013-09-26 14:48:44 -07001047 // Process any items that were added while Launcher was away.
1048 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1049
Michael Jurka447bf842013-05-15 14:52:15 +02001050 if (DEBUG_RESUME_TIME) {
1051 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1052 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001053
1054 if (mWorkspace.getCustomContentCallbacks() != null) {
1055 // If we are resuming and the custom content is the current page, we call onShow().
1056 // It is also poassible that onShow will instead be called slightly after first layout
1057 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1058 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001059 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001060 }
1061 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001062 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001063 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001064
1065 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001066
1067 if (mLauncherCallbacks != null) {
1068 mLauncherCallbacks.onResume();
1069 }
Adam Cohen06dff352012-06-01 17:17:08 -07001070 }
1071
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001072 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001073 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001074 // Ensure that items added to Launcher are queued until Launcher returns
1075 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001076 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001077
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001078 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001079 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001080 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001081 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001082
1083 // We call onHide() aggressively. The custom content callbacks should be able to
1084 // debounce excess onHide calls.
1085 if (mWorkspace.getCustomContentCallbacks() != null) {
1086 mWorkspace.getCustomContentCallbacks().onHide();
1087 }
Romain Guycbb89e42009-06-08 15:52:54 -07001088
Adam Cohen9211d422014-10-07 18:14:53 -07001089 if (mLauncherCallbacks != null) {
1090 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001091 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001092 }
1093
1094 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001095 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1096 // by a onResume or by scrolling otherwise.
1097 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001098
1099 // Custom content is completely hidden
1100 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001101
1102 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1103 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001104
1105 // Indicates whether the user is allowed to scroll away from the custom content.
1106 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001107 }
1108
Adam Cohenc2d6e892014-10-16 09:49:24 -07001109 public interface LauncherOverlay {
1110
1111 /**
1112 * Touch interaction leading to overscroll has begun
1113 */
1114 public void onScrollInteractionBegin();
1115
1116 /**
1117 * Touch interaction related to overscroll has ended
1118 */
1119 public void onScrollInteractionEnd();
1120
1121 /**
1122 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1123 * screen (or in the case of RTL, the rightmost screen).
1124 */
1125 public void onScrollChange(int progress, boolean rtl);
1126
1127 /**
1128 * Screen has stopped scrolling
1129 */
1130 public void onScrollSettled();
1131
1132 /**
1133 * This method can be called by the Launcher in order to force the LauncherOverlay
1134 * to exit fully immersive mode.
1135 */
1136 public void forceExitFullImmersion();
1137 }
1138
1139 public interface LauncherOverlayCallbacks {
1140 /**
1141 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1142 * however it doesn't modify any state within the launcher.
1143 */
1144 public boolean canEnterFullImmersion();
1145
1146 /**
1147 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1148 * eg. by occupying the full screen and handling all touch events.
1149 *
1150 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1151 * case, Launcher will modify any necessary state and assumes the overlay is
1152 * handling all interaction. If false, the LauncherOverlay should cancel any
1153 *
1154 */
1155 public boolean enterFullImmersion();
1156
1157 /**
1158 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1159 * full control over UI and state.
1160 */
1161 public void exitFullImmersion();
1162 }
1163
1164 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1165
1166 @Override
1167 public boolean canEnterFullImmersion() {
1168 return mState == State.WORKSPACE;
1169 }
1170
1171 @Override
1172 public boolean enterFullImmersion() {
1173 if (mState == State.WORKSPACE) {
1174 // When fully immersed, disregard any touches which fall through.
1175 mDragLayer.setBlockTouch(true);
1176 return true;
1177 }
1178 return false;
1179 }
1180
1181 @Override
1182 public void exitFullImmersion() {
1183 mDragLayer.setBlockTouch(false);
1184 }
1185 }
1186
Jorim Jaggid017f882014-01-14 17:08:48 -08001187 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001188 if (mLauncherCallbacks != null) {
1189 return mLauncherCallbacks.hasSettings();
1190 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001191 return false;
1192 }
1193
Adam Cohenbffe7452013-07-22 18:21:45 -07001194
Adam Cohen9211d422014-10-07 18:14:53 -07001195 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001196 CustomContentCallbacks callbacks, String description) {
1197 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001198 }
1199
Adam Cohenedb40762013-07-18 16:45:45 -07001200 // The custom content needs to offset its content to account for the QSB
1201 public int getTopOffsetForCustomContent() {
1202 return mWorkspace.getPaddingTop();
1203 }
1204
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001205 @Override
1206 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001207 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001208 if (mModel.isCurrentCallbacks(this)) {
1209 mModel.stopLoader();
1210 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001211 if (mAppsCustomizeContent != null) {
1212 mAppsCustomizeContent.surrender();
1213 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001214 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001215 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001216
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001217 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001218 @Override
1219 public void onWindowFocusChanged(boolean hasFocus) {
1220 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001221 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001222
1223 if (mLauncherCallbacks != null) {
1224 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1225 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001226 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001228 private boolean acceptFilter() {
1229 final InputMethodManager inputManager = (InputMethodManager)
1230 getSystemService(Context.INPUT_METHOD_SERVICE);
1231 return !inputManager.isFullscreenMode();
1232 }
1233
1234 @Override
1235 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001236 final int uniChar = event.getUnicodeChar();
1237 final boolean handled = super.onKeyDown(keyCode, event);
1238 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1239 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001240 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1241 keyCode, event);
1242 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001243 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001244 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001245 // showSearchDialog()
1246 // If there are multiple keystrokes before the search dialog takes focus,
1247 // onSearchRequested() will be called for every keystroke,
1248 // but it is idempotent, so it's fine.
1249 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001250 }
1251 }
1252
Joe Onorato8a9625e2010-01-28 15:55:35 -08001253 // Eat the long press event so the keyboard doesn't come up.
1254 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1255 return true;
1256 }
1257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 return handled;
1259 }
1260
Karl Rosaen138a0412009-04-23 19:00:21 -07001261 private String getTypedText() {
1262 return mDefaultKeySsb.toString();
1263 }
1264
1265 private void clearTypedText() {
1266 mDefaultKeySsb.clear();
1267 mDefaultKeySsb.clearSpans();
1268 Selection.setSelection(mDefaultKeySsb, 0);
1269 }
1270
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001271 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001272 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1273 * State
1274 */
1275 private static State intToState(int stateOrdinal) {
1276 State state = State.WORKSPACE;
1277 final State[] stateValues = State.values();
1278 for (int i = 0; i < stateValues.length; i++) {
1279 if (stateValues[i].ordinal() == stateOrdinal) {
1280 state = stateValues[i];
1281 break;
1282 }
1283 }
1284 return state;
1285 }
1286
1287 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 * Restores the previous state, if it exists.
1289 *
1290 * @param savedState The previous state.
1291 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001292 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001293 private void restoreState(Bundle savedState) {
1294 if (savedState == null) {
1295 return;
1296 }
1297
Michael Jurka883f55b2010-10-21 15:47:14 -07001298 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001299 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001300 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001301 }
1302
Adam Cohen21cd0022013-10-09 18:57:02 -07001303 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1304 PagedView.INVALID_RESTORE_PAGE);
1305 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001306 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
1308
Winson Chung3d503fb2011-07-13 17:25:49 -07001309 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001310 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001311
Winson Chung3d503fb2011-07-13 17:25:49 -07001312 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1313 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001314 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001315 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1316 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001317 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1318 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1319 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001320 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001321 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001322 mRestoring = true;
1323 }
1324
1325 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1326 if (renameFolder) {
1327 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001328 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001329 mRestoring = true;
1330 }
Winson Chunga12a2502010-12-20 14:41:35 -08001331
Winson Chung785d2eb2011-04-14 16:08:02 -07001332 // Restore the AppsCustomize tab
1333 if (mAppsCustomizeTabHost != null) {
1334 String curTab = savedState.getString("apps_customize_currentTab");
1335 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001336 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001337 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001338 mAppsCustomizeContent.loadAssociatedPages(
1339 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001340 }
1341
Winson Chung5afbf7b2011-07-25 11:53:08 -07001342 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1343 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001344 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001345 mItemIdToViewId = (HashMap<Integer, Integer>)
1346 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001347 }
1348
1349 /**
1350 * Finds all the views we need and configure them properly.
1351 */
1352 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001353 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001354
Craig Mautner360310b2012-10-26 15:13:08 -07001355 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001356 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001357 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1358 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001359 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001360 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001361
John Spurlock77e1f472013-09-11 10:09:51 -04001362 mLauncherView.setSystemUiVisibility(
1363 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001364 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001365
Winson Chung4c98d922011-05-31 16:50:48 -07001366 // Setup the drag layer
1367 mDragLayer.setup(this, dragController);
1368
Winson Chung3d503fb2011-07-13 17:25:49 -07001369 // Setup the hotseat
1370 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1371 if (mHotseat != null) {
1372 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001373 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001374 }
1375
Jorim Jaggid017f882014-01-14 17:08:48 -08001376 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001377 View widgetButton = findViewById(R.id.widget_button);
1378 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001379 @Override
1380 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001381 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001382 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001383 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001384 }
1385 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001386 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1387
1388 View wallpaperButton = findViewById(R.id.wallpaper_button);
1389 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001390 @Override
1391 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001392 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001393 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001394 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001395 }
1396 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001397 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1398
1399 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001400 if (hasSettings()) {
1401 settingsButton.setOnClickListener(new OnClickListener() {
1402 @Override
1403 public void onClick(View arg0) {
1404 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001405 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001406 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001407 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001408 });
1409 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1410 } else {
1411 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001412 }
1413
Adam Cohendbdff6b2013-09-18 19:09:15 -07001414 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001415
Winson Chung4c98d922011-05-31 16:50:48 -07001416 // Setup the workspace
1417 mWorkspace.setHapticFeedbackEnabled(false);
1418 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001419 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001420 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001421
Winson Chungf0ea4d32011-06-06 14:27:16 -07001422 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001423 mSearchDropTargetBar = (SearchDropTargetBar)
1424 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001425
Winson Chungf0ea4d32011-06-06 14:27:16 -07001426 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001427 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001428 mAppsCustomizeContent = (AppsCustomizePagedView)
1429 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1430 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001431
Winson Chung3d503fb2011-07-13 17:25:49 -07001432 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001433 dragController.setDragScoller(mWorkspace);
1434 dragController.setScrollView(mDragLayer);
1435 dragController.setMoveTarget(mWorkspace);
1436 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001437 if (mSearchDropTargetBar != null) {
1438 mSearchDropTargetBar.setup(this, dragController);
Sunny Goyal594d76d2014-11-06 10:12:54 -08001439 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Michael Jurkae0f5a612011-02-07 16:45:41 -08001440 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001441
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001442 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001443 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001444 mWeightWatcher = new WeightWatcher(this);
1445 mWeightWatcher.setAlpha(0.5f);
1446 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001447 new FrameLayout.LayoutParams(
1448 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001449 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001450 Gravity.BOTTOM)
1451 );
Adam Cohen39a06042013-07-19 14:30:12 -07001452
1453 boolean show = shouldShowWeightWatcher();
1454 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001455 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001456 }
1457
1458 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001459 * Sets the all apps button. This method is called from {@link Hotseat}.
1460 */
1461 public void setAllAppsButton(View allAppsButton) {
1462 mAllAppsButton = allAppsButton;
1463 }
1464
1465 public View getAllAppsButton() {
1466 return mAllAppsButton;
1467 }
1468
1469 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001470 * Creates a view representing a shortcut.
1471 *
1472 * @param info The data structure describing the shortcut.
1473 *
1474 * @return A View inflated from R.layout.application.
1475 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001476 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001477 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001478 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001479 }
1480
1481 /**
1482 * Creates a view representing a shortcut inflated from the specified resource.
1483 *
1484 * @param layoutResId The id of the XML layout used to create the shortcut.
1485 * @param parent The group the shortcut belongs to.
1486 * @param info The data structure describing the shortcut.
1487 *
1488 * @return A View inflated from layoutResId.
1489 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001490 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001491 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001492 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001493 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001494 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001495 return favorite;
1496 }
1497
1498 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 * Add a shortcut to the workspace.
1500 *
1501 * @param data The intent describing the shortcut.
1502 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001503 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001504 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001505 int cellY) {
1506 int[] cellXY = mTmpAddItemCellCoordinates;
1507 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001508 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001509
Michael Jurkad3ef3062010-11-23 16:23:58 -08001510 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001511
Sunny Goyal2350bc92014-10-14 16:42:54 -07001512 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001513 if (info == null) {
1514 return;
1515 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001516 final View view = createShortcut(info);
1517
Adam Cohenfbba09b2011-07-18 21:43:05 -07001518 // First we check if we already know the exact location where we want to add this item.
1519 if (cellX >= 0 && cellY >= 0) {
1520 cellXY[0] = cellX;
1521 cellXY[1] = cellY;
1522 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001523
1524 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001525 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001526 true, null,null)) {
1527 return;
1528 }
1529 DragObject dragObject = new DragObject();
1530 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001531 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1532 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001533 return;
1534 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001535 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001536 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001537 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001538 foundCellSpan = (result != null);
1539 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001540 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001541 }
1542
1543 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001544 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001545 return;
1546 }
1547
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001549
1550 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001551 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001552 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001553 }
1554 }
1555
Adam Cohen2f093b62012-04-30 18:59:53 -07001556 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1557 int minHeight) {
1558 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001559 // We want to account for the extra amount of padding that we are adding to the widget
1560 // to ensure that it gets the full amount of space that it has requested
1561 int requiredWidth = minWidth + padding.left + padding.right;
1562 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001563 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001564 }
1565
Adam Cohen2f093b62012-04-30 18:59:53 -07001566 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1567 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001568 }
1569
Adam Cohen2f093b62012-04-30 18:59:53 -07001570 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1571 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001572 }
1573
Adam Cohen2f093b62012-04-30 18:59:53 -07001574 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1575 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001576 }
1577
Adam Cohen2f093b62012-04-30 18:59:53 -07001578 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1579 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001580 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001581 }
1582
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001583 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001584 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001586 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001587 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001588 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001589 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001590 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1591 if (appWidgetInfo == null) {
1592 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1593 }
Romain Guycbb89e42009-06-08 15:52:54 -07001594
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001595 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001596 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001597
Adam Cohen2f093b62012-04-30 18:59:53 -07001598 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1599 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001600
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001601 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001602 // We have saved the position to which the widget was dragged-- this really only matters
1603 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001604 int[] cellXY = mTmpAddItemCellCoordinates;
1605 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001606 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001607 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001608 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1609 cellXY[0] = mPendingAddInfo.cellX;
1610 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001611 spanXY[0] = mPendingAddInfo.spanX;
1612 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001613 foundCellSpan = true;
1614 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001615 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001616 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001617 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1618 spanXY[1], cellXY, finalSpan);
1619 spanXY[0] = finalSpan[0];
1620 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001621 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001622 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001623 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001624 }
1625
Michael Jurka0280c3b2010-09-17 15:00:07 -07001626 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001627 if (appWidgetId != -1) {
1628 // Deleting an app widget ID is a void call but writes to disk before returning
1629 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001630 new AsyncTask<Void, Void, Void>() {
1631 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001632 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001633 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001634 }
Michael Jurka43467462013-11-14 12:03:58 +01001635 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001636 }
Winson Chung93eef082012-03-23 15:59:27 -07001637 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001638 return;
1639 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001640
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001641 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001642 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1643 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001644 launcherInfo.spanX = spanXY[0];
1645 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001646 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1647 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001648 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001649
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001650 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001651 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001652
1653 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001654 if (hostView == null) {
1655 // Perform actual inflation because we're live
1656 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1657 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1658 } else {
1659 // The AppWidgetHostView has already been inflated and instantiated
1660 launcherInfo.hostView = hostView;
1661 }
Romain Guycbb89e42009-06-08 15:52:54 -07001662
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001663 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001664 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001665 launcherInfo.notifyWidgetSizeChanged(this);
1666
Adam Cohendcd297f2013-06-18 13:13:40 -07001667 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001668 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001669
1670 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001671 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001672 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001673 }
Romain Guycbb89e42009-06-08 15:52:54 -07001674
Adam Cohended9f8d2010-11-03 13:25:16 -07001675 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1676 @Override
1677 public void onReceive(Context context, Intent intent) {
1678 final String action = intent.getAction();
1679 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1680 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001681 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001682 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001683
Winson Chungc100e8e2011-08-09 16:02:43 -07001684 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001685 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001686 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001687 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001688 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001689 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1690 mUserPresent = true;
1691 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001692 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1693 mModel.resetLoadedState(false, true);
1694 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1695 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1696 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1697 mModel.resetLoadedState(false, true);
1698 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1699 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1700 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001701 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1702 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1703 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001704 }
1705 }
1706 };
1707
1708 @Override
1709 public void onAttachedToWindow() {
1710 super.onAttachedToWindow();
1711
1712 // Listen for broadcasts related to user-presence
1713 final IntentFilter filter = new IntentFilter();
1714 filter.addAction(Intent.ACTION_SCREEN_OFF);
1715 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001716 // For handling managed profiles
1717 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1718 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001719 if (ENABLE_DEBUG_INTENTS) {
1720 filter.addAction(DebugIntents.DELETE_DATABASE);
1721 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1722 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001723 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001724 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001725 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001726 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001727 mVisible = true;
1728 }
1729
Adam Cohen0b395352014-06-09 22:54:36 +00001730 /**
1731 * Sets up transparent navigation and status bars in LMP.
1732 * This method is a no-op for other platform versions.
1733 */
1734 @TargetApi(19)
1735 private void setupTransparentSystemBarsForLmp() {
1736 // TODO(sansid): use the APIs directly when compiling against L sdk.
1737 // Currently we use reflection to access the flags and the API to set the transparency
1738 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001739 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001740 try {
1741 getWindow().getAttributes().systemUiVisibility |=
1742 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1743 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1744 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1745 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1746 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1747 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1748 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1749 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1750
1751 Method setStatusBarColorMethod =
1752 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1753 Method setNavigationBarColorMethod =
1754 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1755 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1756 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1757 } catch (NoSuchFieldException e) {
1758 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1759 } catch (NoSuchMethodException ex) {
1760 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1761 } catch (IllegalAccessException e) {
1762 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1763 } catch (IllegalArgumentException e) {
1764 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1765 } catch (InvocationTargetException e) {
1766 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1767 } finally {}
1768 }
1769 }
1770
Adam Cohended9f8d2010-11-03 13:25:16 -07001771 @Override
1772 public void onDetachedFromWindow() {
1773 super.onDetachedFromWindow();
1774 mVisible = false;
1775
Adam Cohend113e0c2010-11-11 10:48:05 -08001776 if (mAttached) {
1777 unregisterReceiver(mReceiver);
1778 mAttached = false;
1779 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001780 updateRunning();
1781 }
1782
1783 public void onWindowVisibilityChanged(int visibility) {
1784 mVisible = visibility == View.VISIBLE;
1785 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001786 // The following code used to be in onResume, but it turns out onResume is called when
1787 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1788 // is a more appropriate event to handle
1789 if (mVisible) {
1790 mAppsCustomizeTabHost.onWindowVisible();
1791 if (!mWorkspaceLoading) {
1792 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001793 // We want to let Launcher draw itself at least once before we force it to build
1794 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001795 // apps is nice and speedy.
1796 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001797 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001798 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001799 if (mStarted) return;
1800 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001801 // We delay the layer building a bit in order to give
1802 // other message processing a time to run. In particular
1803 // this avoids a delay in hiding the IME if it was
1804 // currently shown, because doing that may involve
1805 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001806 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001807 final ViewTreeObserver.OnDrawListener listener = this;
1808 mWorkspace.post(new Runnable() {
1809 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001810 if (mWorkspace != null &&
1811 mWorkspace.getViewTreeObserver() != null) {
1812 mWorkspace.getViewTreeObserver().
1813 removeOnDrawListener(listener);
1814 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001815 }
1816 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001817 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001818 }
1819 });
1820 }
1821 clearTypedText();
1822 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001823 }
1824
1825 private void sendAdvanceMessage(long delay) {
1826 mHandler.removeMessages(ADVANCE_MSG);
1827 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1828 mHandler.sendMessageDelayed(msg, delay);
1829 mAutoAdvanceSentTime = System.currentTimeMillis();
1830 }
1831
1832 private void updateRunning() {
1833 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1834 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1835 mAutoAdvanceRunning = autoAdvanceRunning;
1836 if (autoAdvanceRunning) {
1837 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1838 sendAdvanceMessage(delay);
1839 } else {
1840 if (!mWidgetsToAdvance.isEmpty()) {
1841 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1842 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1843 }
1844 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001845 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001846 }
1847 }
1848 }
1849
1850 private final Handler mHandler = new Handler() {
1851 @Override
1852 public void handleMessage(Message msg) {
1853 if (msg.what == ADVANCE_MSG) {
1854 int i = 0;
1855 for (View key: mWidgetsToAdvance.keySet()) {
1856 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1857 final int delay = mAdvanceStagger * i;
1858 if (v instanceof Advanceable) {
1859 postDelayed(new Runnable() {
1860 public void run() {
1861 ((Advanceable) v).advance();
1862 }
1863 }, delay);
1864 }
1865 i++;
1866 }
1867 sendAdvanceMessage(mAdvanceInterval);
1868 }
1869 }
1870 };
1871
1872 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001873 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001874 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1875 if (v instanceof Advanceable) {
1876 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001877 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001878 updateRunning();
1879 }
1880 }
1881
1882 void removeWidgetToAutoAdvance(View hostView) {
1883 if (mWidgetsToAdvance.containsKey(hostView)) {
1884 mWidgetsToAdvance.remove(hostView);
1885 updateRunning();
1886 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001887 }
1888
Joe Onorato9c1289c2009-08-17 11:03:03 -04001889 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001890 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001891 launcherInfo.hostView = null;
1892 }
1893
Winson Chung93eef082012-03-23 15:59:27 -07001894 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1895 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1896 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001897 }
1898
Winson Chunga6945242014-01-08 14:04:34 -08001899 public DragLayer getDragLayer() {
1900 return mDragLayer;
1901 }
1902
1903 public Workspace getWorkspace() {
1904 return mWorkspace;
1905 }
1906
1907 public Hotseat getHotseat() {
1908 return mHotseat;
1909 }
1910
Jorim Jaggid017f882014-01-14 17:08:48 -08001911 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001912 return mOverviewPanel;
1913 }
1914
1915 public SearchDropTargetBar getSearchBar() {
1916 return mSearchDropTargetBar;
1917 }
1918
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001919 public LauncherAppWidgetHost getAppWidgetHost() {
1920 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001921 }
Romain Guycbb89e42009-06-08 15:52:54 -07001922
Winson Chunga9abd0e2010-10-27 17:18:37 -07001923 public LauncherModel getModel() {
1924 return mModel;
1925 }
1926
Winson Chunga6945242014-01-08 14:04:34 -08001927 protected SharedPreferences getSharedPrefs() {
1928 return mSharedPrefs;
1929 }
1930
Bjorn Bringertc459e522013-06-07 19:36:01 +01001931 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001932 getWindow().closeAllPanels();
1933
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001934 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001935 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001936 }
1937
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001938 @Override
1939 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001940 long startTime = 0;
1941 if (DEBUG_RESUME_TIME) {
1942 startTime = System.currentTimeMillis();
1943 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001944 super.onNewIntent(intent);
1945
1946 // Close the menu
1947 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001948 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001949 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001950
Adam Cohened307df2013-10-02 09:37:31 -07001951 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1952 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1953 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001954
Adam Cohen6fecd412013-10-02 17:41:50 -07001955 if (mWorkspace == null) {
1956 // Can be cases where mWorkspace is null, this prevents a NPE
1957 return;
1958 }
1959 Folder openFolder = mWorkspace.getOpenFolder();
1960 // In all these cases, only animate if we're already on home
1961 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001962
1963 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1964 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001965 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001966 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001967 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001968 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001969
Adam Cohen6fecd412013-10-02 17:41:50 -07001970 closeFolder();
1971 exitSpringLoadedDragMode();
1972
1973 // If we are already on home, then just animate back to the workspace,
1974 // otherwise, just wait until onResume to set the state back to Workspace
1975 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001976 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001977 } else {
1978 mOnResumeState = State.WORKSPACE;
1979 }
1980
1981 final View v = getWindow().peekDecorView();
1982 if (v != null && v.getWindowToken() != null) {
1983 InputMethodManager imm = (InputMethodManager)getSystemService(
1984 INPUT_METHOD_SERVICE);
1985 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1986 }
1987
1988 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001989 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001990 mAppsCustomizeTabHost.reset();
1991 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001992
Adam Cohen9211d422014-10-07 18:14:53 -07001993 if (mLauncherCallbacks != null) {
1994 mLauncherCallbacks.onHomeIntent();
1995 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001996 }
Adam Cohened307df2013-10-02 09:37:31 -07001997
Michael Jurka447bf842013-05-15 14:52:15 +02001998 if (DEBUG_RESUME_TIME) {
1999 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
2000 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002001
Adam Cohen9211d422014-10-07 18:14:53 -07002002 if (mLauncherCallbacks != null) {
2003 mLauncherCallbacks.onNewIntent(intent);
2004 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002005 }
2006
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002007 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07002008 public void onRestoreInstanceState(Bundle state) {
2009 super.onRestoreInstanceState(state);
2010 for (int page: mSynchronouslyBoundPages) {
2011 mWorkspace.restoreInstanceStateForChild(page);
2012 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002013 }
2014
2015 @Override
2016 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002017 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002018 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2019 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002020 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002021 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022
Michael Jurka883f55b2010-10-21 15:47:14 -07002023 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002024 // We close any open folder since it will not be re-opened, and we need to make sure
2025 // this state is reflected.
2026 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002027
Adam Cohendcd297f2013-06-18 13:13:40 -07002028 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002029 mWaitingForResult) {
2030 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002031 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002032 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2033 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002034 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2035 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2036 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002037 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002038 }
2039
2040 if (mFolderInfo != null && mWaitingForResult) {
2041 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2042 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2043 }
Michael Jurka946ad472010-07-09 18:05:18 -07002044
Winson Chung785d2eb2011-04-14 16:08:02 -07002045 // Save the current AppsCustomize tab
2046 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08002047 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2048 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002049 if (currentTabTag != null) {
2050 outState.putString("apps_customize_currentTab", currentTabTag);
2051 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002052 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2053 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002054 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002055 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002056
2057 if (mLauncherCallbacks != null) {
2058 mLauncherCallbacks.onSaveInstanceState(outState);
2059 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002060 }
2061
2062 @Override
2063 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002064 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002065
Winson Chunge7a03942011-08-05 15:05:12 -07002066 // Remove all pending runnables
2067 mHandler.removeMessages(ADVANCE_MSG);
2068 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002069 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002070
Winson Chungcd2b0142011-06-08 16:02:26 -07002071 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002072 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002073
2074 // It's possible to receive onDestroy after a new Launcher activity has
2075 // been created. In this case, don't interfere with the new Launcher.
2076 if (mModel.isCurrentCallbacks(this)) {
2077 mModel.stopLoader();
2078 app.setLauncher(null);
2079 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002082 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002084 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002085 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002086 mAppWidgetHost = null;
2087
2088 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089
2090 TextKeyListener.getInstance().release();
2091
Winson Chung81b52252012-08-27 15:34:29 -07002092 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2093 // to prevent leaking Launcher activities on orientation change.
2094 if (mModel != null) {
2095 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2096 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002097
2098 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002099 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002100
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002101 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002102 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002103 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002104 mWorkspace = null;
2105 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002106
Michael Jurka2ecf9952012-06-18 12:52:28 -07002107 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002108
2109 if (mLauncherCallbacks != null) {
2110 mLauncherCallbacks.onDestroy();
2111 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 }
2113
Adam Cohena9cf38f2011-05-02 15:36:58 -07002114 public DragController getDragController() {
2115 return mDragController;
2116 }
2117
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118 @Override
2119 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002120 if (requestCode >= 0) {
2121 setWaitingForResult(true);
2122 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002123 super.startActivityForResult(intent, requestCode);
2124 }
2125
Winson Chung70d72102011-08-12 11:24:35 -07002126 /**
2127 * Indicates that we want global search for this activity by setting the globalSearch
2128 * argument for {@link #startSearch} to true.
2129 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002130 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002131 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002132 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002133
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002134 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002135
Karl Rosaen138a0412009-04-23 19:00:21 -07002136 if (initialQuery == null) {
2137 // Use any text typed in the launcher as the initial query
2138 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002139 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002140 if (appSearchData == null) {
2141 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002142 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002143 }
Winson Chungadf0c182012-08-23 14:59:07 -07002144 Rect sourceBounds = new Rect();
2145 if (mSearchDropTargetBar != null) {
2146 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2147 }
Romain Guycbb89e42009-06-08 15:52:54 -07002148
Ian Parkinson047036e2014-09-01 15:40:53 +01002149 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002150 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002151 if (clearTextImmediately) {
2152 clearTypedText();
2153 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002154 }
2155
Ian Parkinson047036e2014-09-01 15:40:53 +01002156 /**
2157 * Start a text search.
2158 *
2159 * @return {@code true} if the search will start immediately, so any further keypresses
2160 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2161 * to buffer keypresses.
2162 */
2163 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002164 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002165 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2166 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2167 sourceBounds);
2168 }
2169
Michael Jurkaa33411c2012-06-14 16:18:21 -07002170 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002171 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002172 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002173 }
2174
2175 /**
2176 * Starts the global search activity. This code is a copied from SearchManager
2177 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002178 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002179 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002180 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002181 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2182 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2183 if (globalSearchActivity == null) {
2184 Log.w(TAG, "No global search activity found.");
2185 return;
2186 }
2187 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2188 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2189 intent.setComponent(globalSearchActivity);
2190 // Make sure that we have a Bundle to put source in
2191 if (appSearchData == null) {
2192 appSearchData = new Bundle();
2193 } else {
2194 appSearchData = new Bundle(appSearchData);
2195 }
Adam Cohen9211d422014-10-07 18:14:53 -07002196 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002197 if (!appSearchData.containsKey("source")) {
2198 appSearchData.putString("source", getPackageName());
2199 }
2200 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2201 if (!TextUtils.isEmpty(initialQuery)) {
2202 intent.putExtra(SearchManager.QUERY, initialQuery);
2203 }
2204 if (selectInitialQuery) {
2205 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2206 }
2207 intent.setSourceBounds(sourceBounds);
2208 try {
2209 startActivity(intent);
2210 } catch (ActivityNotFoundException ex) {
2211 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2212 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002213 }
2214
Yura4f93ec62014-02-04 14:15:21 +00002215 public boolean isOnCustomContent() {
2216 return mWorkspace.isOnOrMovingToCustomContent();
2217 }
2218
Winson Chung70d72102011-08-12 11:24:35 -07002219 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002220 public boolean onPrepareOptionsMenu(Menu menu) {
2221 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002222 if (!isOnCustomContent()) {
2223 // Close any open folders
2224 closeFolder();
2225 // Stop resizing any widgets
2226 mWorkspace.exitWidgetResizeMode();
2227 if (!mWorkspace.isInOverviewMode()) {
2228 // Show the overview mode
2229 showOverviewMode(true);
2230 } else {
2231 showWorkspace(true);
2232 }
Winson Chunge0298742014-01-17 12:03:00 -08002233 }
Adam Cohen9211d422014-10-07 18:14:53 -07002234 if (mLauncherCallbacks != null) {
2235 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2236 }
2237
Michael Jurkab94f3f82013-09-11 18:08:54 +02002238 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002239 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002240
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002241 @Override
2242 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002243 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002244 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002245 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002246 }
2247
Joe Onorato9c1289c2009-08-17 11:03:03 -04002248 public boolean isWorkspaceLocked() {
2249 return mWorkspaceLoading || mWaitingForResult;
2250 }
2251
Adam Cohen517a7f52014-03-01 12:12:59 -08002252 public boolean isWorkspaceLoading() {
2253 return mWorkspaceLoading;
2254 }
2255
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002256 private void setWorkspaceLoading(boolean value) {
2257 boolean isLocked = isWorkspaceLocked();
2258 mWorkspaceLoading = value;
2259 if (isLocked != isWorkspaceLocked()) {
2260 onWorkspaceLockedChanged();
2261 }
2262 }
2263
2264 private void setWaitingForResult(boolean value) {
2265 boolean isLocked = isWorkspaceLocked();
2266 mWaitingForResult = value;
2267 if (isLocked != isWorkspaceLocked()) {
2268 onWorkspaceLockedChanged();
2269 }
2270 }
2271
Adam Cohen9211d422014-10-07 18:14:53 -07002272 protected void onWorkspaceLockedChanged() {
2273 if (mLauncherCallbacks != null) {
2274 mLauncherCallbacks.onWorkspaceLockedChanged();
2275 }
2276 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002277
Michael Jurka0280c3b2010-09-17 15:00:07 -07002278 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002279 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002280 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002281 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2282 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002283 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002284 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002285 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002286
Adam Cohenad4e15c2013-10-17 16:21:35 -07002287 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2288 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2289 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2290 }
2291
2292 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2293 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2294 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002295 if (appWidgetInfo.configure != null) {
2296 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002297 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002298
Bjorn Bringert7984c942009-12-09 15:38:25 +00002299 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002300 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2301 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2302
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002303 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002304 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002305 Runnable onComplete = new Runnable() {
2306 @Override
2307 public void run() {
2308 // Exit spring loaded mode if necessary after adding the widget
2309 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2310 null);
2311 }
2312 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002313 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002314 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002315 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002316 }
2317 }
Romain Guycbb89e42009-06-08 15:52:54 -07002318
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002319 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002320 // Close any folders that may be open.
2321 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002322 mWorkspace.moveToCustomContentScreen(animate);
2323 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002324 /**
2325 * Process a shortcut drop.
2326 *
2327 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002328 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329 * @param cell The cell it should be added to, optional
2330 * @param position The location on the screen where it was dropped, optional
2331 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002332 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002333 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002334 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002335 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002336 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002337 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002338
2339 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002340 mPendingAddInfo.cellX = cell[0];
2341 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002342 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002343
2344 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2345 createShortcutIntent.setComponent(componentName);
2346 processShortcut(createShortcutIntent);
2347 }
2348
Adam Cohenfbba09b2011-07-18 21:43:05 -07002349 /**
2350 * Process a widget drop.
2351 *
2352 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002353 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002354 * @param cell The cell it should be added to, optional
2355 * @param position The location on the screen where it was dropped, optional
2356 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002357 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002358 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002359 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002360 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002361 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002362 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002363 mPendingAddInfo.minSpanX = info.minSpanX;
2364 mPendingAddInfo.minSpanY = info.minSpanY;
2365
Adam Cohenfbba09b2011-07-18 21:43:05 -07002366 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002367 mPendingAddInfo.cellX = cell[0];
2368 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002369 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002370 if (span != null) {
2371 mPendingAddInfo.spanX = span[0];
2372 mPendingAddInfo.spanY = span[1];
2373 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002374
Adam Cohened66b2b2012-01-23 17:28:51 -08002375 AppWidgetHostView hostView = info.boundWidget;
2376 int appWidgetId;
2377 if (hostView != null) {
2378 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002379 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002380 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002381 // In this case, we either need to start an activity to get permission to bind
2382 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002383 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002384 Bundle options = info.bindOptions;
2385
Sunny Goyalffe83f12014-08-14 17:39:34 -07002386 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2387 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002388 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002389 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002390 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002391 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002392 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2393 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2394 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002395 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2396 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002397 // TODO: we need to make sure that this accounts for the options bundle.
2398 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002399 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2400 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002401 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002402 }
2403
Joe Onoratodeb98af2010-02-19 14:59:39 -08002404 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002405 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002406 }
2407
Winson Chung24ab2f12010-09-16 14:10:47 -07002408 void processWallpaper(Intent intent) {
2409 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2410 }
2411
Adam Cohendcd297f2013-06-18 13:13:40 -07002412 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002413 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002414 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002415 folderInfo.title = getText(R.string.folder_name);
2416
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002418 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002419 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002420 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421
2422 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002423 FolderIcon newFolder =
2424 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002425 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002426 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002427 // Force measure the new folder icon
2428 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2429 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002430 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002431 }
Romain Guycbb89e42009-06-08 15:52:54 -07002432
Joe Onorato9c1289c2009-08-17 11:03:03 -04002433 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002434 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002435 }
2436
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002437 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002438 if (mLauncherCallbacks != null) {
2439 return mLauncherCallbacks.getWallpaperPickerComponent();
2440 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002441 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002442 }
2443
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002444 /**
2445 * Registers various content observers. The current implementation registers
2446 * only a favorites observer to keep track of the favorites applications.
2447 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002448 private void registerContentObservers() {
2449 ContentResolver resolver = getContentResolver();
2450 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2451 true, mWidgetObserver);
2452 }
2453
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002454 @Override
2455 public boolean dispatchKeyEvent(KeyEvent event) {
2456 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2457 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002458 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002459 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002460 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002461 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002462 dumpState();
2463 return true;
2464 }
2465 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002466 }
2467 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2468 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002469 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 return true;
2471 }
2472 }
2473
2474 return super.dispatchKeyEvent(event);
2475 }
2476
Joe Onorato88ec0992009-11-19 13:16:06 -08002477 @Override
2478 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002479 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2480 return;
2481 }
2482
Winson Chungc93e5ae2012-07-23 20:48:26 -07002483 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002484 if (mAppsCustomizeContent.getContentType() ==
2485 AppsCustomizePagedView.ContentType.Applications) {
2486 showWorkspace(true);
2487 } else {
2488 showOverviewMode(true);
2489 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002490 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002491 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002492 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002493 Folder openFolder = mWorkspace.getOpenFolder();
2494 if (openFolder.isEditingName()) {
2495 openFolder.dismissEditingName();
2496 } else {
2497 closeFolder();
2498 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002499 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002500 mWorkspace.exitWidgetResizeMode();
2501
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002502 // Back button is a no-op here, but give at least some feedback for the button press
2503 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002504 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002505 }
2506
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002507 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002508 * Re-listen when widgets are reset.
2509 */
2510 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002511 if (mAppWidgetHost != null) {
2512 mAppWidgetHost.startListening();
2513 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002514 }
2515
2516 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002517 * Launches the intent referred by the clicked shortcut.
2518 *
2519 * @param v The view representing the clicked shortcut.
2520 */
2521 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002522 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2523 // view has detached (it's possible for this to happen if the view is removed mid touch).
2524 if (v.getWindowToken() == null) {
2525 return;
2526 }
2527
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002528 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002529 return;
2530 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002531
Adam Cohen1697b792013-09-17 19:08:21 -07002532 if (v instanceof Workspace) {
2533 if (mWorkspace.isInOverviewMode()) {
2534 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002535 }
2536 return;
2537 }
2538
2539 if (v instanceof CellLayout) {
2540 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002541 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002542 }
2543 }
2544
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002545 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002546 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002547 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002548 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002549 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002550 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002551 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002552 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002553 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002554 } else if (tag instanceof AppInfo) {
2555 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002556 } else if (tag instanceof LauncherAppWidgetInfo) {
2557 if (v instanceof PendingAppWidgetHostView) {
2558 onClickPendingWidget((PendingAppWidgetHostView) v);
2559 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002560 }
2561 }
2562
Sunny Goyal508da152014-08-14 10:53:27 -07002563 public void onClickPagedViewIcon(View v) {
2564 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002565 if (mLauncherCallbacks != null) {
2566 mLauncherCallbacks.onClickPagedViewIcon(v);
2567 }
Sunny Goyal508da152014-08-14 10:53:27 -07002568 }
2569
Michael Jurka0e260592010-06-30 17:07:39 -07002570 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002571 return false;
2572 }
2573
Michael Jurkaaf442092010-06-10 17:01:57 -07002574 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002575 * Event handler for the app widget view which has not fully restored.
2576 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002577 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002578 if (mIsSafeModeEnabled) {
2579 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2580 return;
2581 }
2582
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002583 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002584 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002585 int widgetId = info.appWidgetId;
2586 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2587 if (appWidgetInfo != null) {
2588 mPendingAddWidgetInfo = appWidgetInfo;
2589 mPendingAddInfo.copyFrom(info);
2590 mPendingAddWidgetId = widgetId;
2591
Sunny Goyalffe83f12014-08-14 17:39:34 -07002592 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2593 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002594 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002595 } else if (info.installProgress < 0) {
2596 // The install has not been queued
2597 final String packageName = info.providerName.getPackageName();
2598 showBrokenAppInstallDialog(packageName,
2599 new DialogInterface.OnClickListener() {
2600 public void onClick(DialogInterface dialog, int id) {
2601 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2602 }
2603 });
2604 } else {
2605 // Download has started.
2606 final String packageName = info.providerName.getPackageName();
2607 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002608 }
2609 }
2610
2611 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002612 * Event handler for the "grid" button that appears on the home screen, which
2613 * enters all apps mode.
2614 *
2615 * @param v The view that was clicked.
2616 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002617 protected void onClickAllAppsButton(View v) {
2618 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2619 if (isAllAppsVisible()) {
2620 showWorkspace(true);
2621 } else {
2622 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2623 }
Adam Cohen9211d422014-10-07 18:14:53 -07002624 if (mLauncherCallbacks != null) {
2625 mLauncherCallbacks.onClickAllAppsButton(v);
2626 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002627 }
2628
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002629 private void showBrokenAppInstallDialog(final String packageName,
2630 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002631 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002632 .setTitle(R.string.abandoned_promises_title)
2633 .setMessage(R.string.abandoned_promise_explanation)
2634 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2635 .setNeutralButton(R.string.abandoned_clean_this,
2636 new DialogInterface.OnClickListener() {
2637 public void onClick(DialogInterface dialog, int id) {
2638 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2639 mWorkspace.removeAbandonedPromise(packageName, user);
2640 }
2641 })
2642 .create().show();
2643 return;
2644 }
2645
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002646 /**
2647 * Event handler for an app shortcut click.
2648 *
2649 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2650 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002651 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002652 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2653 Object tag = v.getTag();
2654 if (!(tag instanceof ShortcutInfo)) {
2655 throw new IllegalArgumentException("Input must be a Shortcut");
2656 }
2657
2658 // Open shortcut
2659 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002660
2661 if (shortcut.isDisabled != 0) {
2662 int error = R.string.activity_not_available;
2663 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2664 error = R.string.safemode_shortcut_error;
2665 }
2666 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2667 return;
2668 }
2669
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002670 final Intent intent = shortcut.intent;
2671
2672 // Check for special shortcuts
2673 if (intent.getComponent() != null) {
2674 final String shortcutClass = intent.getComponent().getClassName();
2675
2676 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2677 MemoryDumpActivity.startDump(this);
2678 return;
2679 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2680 toggleShowWeightWatcher();
2681 return;
2682 }
2683 }
2684
Chris Wren40c5ed32014-06-24 18:24:23 -04002685 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002686 if ((v instanceof BubbleTextView)
2687 && shortcut.isPromise()
2688 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002689 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002690 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002691 new DialogInterface.OnClickListener() {
2692 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002693 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002694 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002695 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002696 return;
2697 }
2698
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002699 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002700 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002701
2702 if (mLauncherCallbacks != null) {
2703 mLauncherCallbacks.onClickAppShortcut(v);
2704 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002705 }
2706
Sunny Goyal508da152014-08-14 10:53:27 -07002707 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002708 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002709 final ShortcutInfo shortcut;
2710 final Intent intent;
2711 if (tag instanceof ShortcutInfo) {
2712 shortcut = (ShortcutInfo) tag;
2713 intent = shortcut.intent;
2714 int[] pos = new int[2];
2715 v.getLocationOnScreen(pos);
2716 intent.setSourceBounds(new Rect(pos[0], pos[1],
2717 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002718
Sunny Goyal508da152014-08-14 10:53:27 -07002719 } else if (tag instanceof AppInfo) {
2720 shortcut = null;
2721 intent = ((AppInfo) tag).intent;
2722 } else {
2723 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2724 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002725
2726 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002727 mStats.recordLaunch(intent, shortcut);
2728
2729 if (success && v instanceof BubbleTextView) {
2730 mWaitingForResume = (BubbleTextView) v;
2731 mWaitingForResume.setStayPressed(true);
2732 }
2733 }
2734
2735 /**
2736 * Event handler for a folder icon click.
2737 *
2738 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2739 */
2740 protected void onClickFolderIcon(View v) {
2741 if (LOGD) Log.d(TAG, "onClickFolder");
2742 if (!(v instanceof FolderIcon)){
2743 throw new IllegalArgumentException("Input must be a FolderIcon");
2744 }
2745
2746 FolderIcon folderIcon = (FolderIcon) v;
2747 final FolderInfo info = folderIcon.getFolderInfo();
2748 Folder openFolder = mWorkspace.getFolderForTag(info);
2749
2750 // If the folder info reports that the associated folder is open, then verify that
2751 // it is actually opened. There have been a few instances where this gets out of sync.
2752 if (info.opened && openFolder == null) {
2753 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2754 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2755 info.opened = false;
2756 }
2757
2758 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2759 // Close any open folder
2760 closeFolder();
2761 // Open the requested folder
2762 openFolder(folderIcon);
2763 } else {
2764 // Find the open folder...
2765 int folderScreen;
2766 if (openFolder != null) {
2767 folderScreen = mWorkspace.getPageForView(openFolder);
2768 // .. and close it
2769 closeFolder(openFolder);
2770 if (folderScreen != mWorkspace.getCurrentPage()) {
2771 // Close any folder open on the current screen
2772 closeFolder();
2773 // Pull the folder onto this screen
2774 openFolder(folderIcon);
2775 }
2776 }
2777 }
Adam Cohen9211d422014-10-07 18:14:53 -07002778
2779 if (mLauncherCallbacks != null) {
2780 mLauncherCallbacks.onClickFolderIcon(v);
2781 }
Michael Jurka5130e402011-10-13 04:55:35 -07002782 }
2783
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002784 /**
2785 * Event handler for the (Add) Widgets button that appears after a long press
2786 * on the home screen.
2787 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002788 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002789 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002790 if (mIsSafeModeEnabled) {
2791 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2792 } else {
2793 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2794 if (mLauncherCallbacks != null) {
2795 mLauncherCallbacks.onClickAddWidgetButton(view);
2796 }
Adam Cohen9211d422014-10-07 18:14:53 -07002797 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002798 }
2799
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002800 /**
2801 * Event handler for the wallpaper picker button that appears after a long press
2802 * on the home screen.
2803 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002804 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002805 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2806 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2807 pickWallpaper.setComponent(getWallpaperPickerComponent());
2808 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002809
2810 if (mLauncherCallbacks != null) {
2811 mLauncherCallbacks.onClickWallpaperPicker(v);
2812 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002813 }
2814
2815 /**
2816 * Event handler for a click on the settings button that appears after a long press
2817 * on the home screen.
2818 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002819 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002820 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002821 if (mLauncherCallbacks != null) {
2822 mLauncherCallbacks.onClickSettingsButton(v);
2823 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002824 }
2825
Michael Jurka5130e402011-10-13 04:55:35 -07002826 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002827 // Provide the same haptic feedback that the system offers for virtual keys.
2828 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002829 }
2830
Adam Cohen61f560d2013-09-30 15:58:20 -07002831 public void performHapticFeedbackOnTouchDown(View v) {
2832 // Provide the same haptic feedback that the system offers for virtual keys.
2833 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2834 }
2835
2836 public View.OnTouchListener getHapticFeedbackTouchListener() {
2837 if (mHapticFeedbackTouchListener == null) {
2838 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2839 @Override
2840 public boolean onTouch(View v, MotionEvent event) {
2841 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2842 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2843 }
2844 return false;
2845 }
2846 };
2847 }
2848 return mHapticFeedbackTouchListener;
2849 }
2850
Adam Cohen9211d422014-10-07 18:14:53 -07002851 public void onDragStarted(View view) {
2852 if (isOnCustomContent()) {
2853 // Custom content screen doesn't participate in drag and drop. If on custom
2854 // content screen, move to default.
2855 moveWorkspaceToDefaultScreen();
2856 }
2857
2858 if (mLauncherCallbacks != null) {
2859 mLauncherCallbacks.onDragStarted(view);
2860 }
2861 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002862
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002863 /**
2864 * Called when the user stops interacting with the launcher.
2865 * This implies that the user is now on the homescreen and is not doing housekeeping.
2866 */
Adam Cohen9211d422014-10-07 18:14:53 -07002867 protected void onInteractionEnd() {
2868 if (mLauncherCallbacks != null) {
2869 mLauncherCallbacks.onInteractionEnd();
2870 }
2871 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002872
2873 /**
2874 * Called when the user starts interacting with the launcher.
2875 * The possible interactions are:
2876 * - open all apps
2877 * - reorder an app shortcut, or a widget
2878 * - open the overview mode.
2879 * This is a good time to stop doing things that only make sense
2880 * when the user is on the homescreen and not doing housekeeping.
2881 */
Adam Cohen9211d422014-10-07 18:14:53 -07002882 protected void onInteractionBegin() {
2883 if (mLauncherCallbacks != null) {
2884 mLauncherCallbacks.onInteractionBegin();
2885 }
2886 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002887
Kenny Guyf07af7b2014-07-31 11:39:16 +01002888 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Kenny Guyf07af7b2014-07-31 11:39:16 +01002889 try {
2890 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
Kenny Guyf07af7b2014-07-31 11:39:16 +01002891 launcherApps.showAppDetailsForProfile(componentName, user);
2892 } catch (SecurityException e) {
2893 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2894 Log.e(TAG, "Launcher does not have permission to launch settings");
2895 } catch (ActivityNotFoundException e) {
2896 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2897 Log.e(TAG, "Unable to launch settings");
2898 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002899 }
2900
Michael Jurka1e2f4652013-07-08 18:03:46 -07002901 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002902 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2903 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002904 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002905 // System applications cannot be installed. For now, show a toast explaining that.
2906 // We may give them the option of disabling apps this way.
2907 int messageId = R.string.uninstall_system_app_text;
2908 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002909 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002910 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002911 String packageName = componentName.getPackageName();
2912 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002913 Intent intent = new Intent(
2914 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002915 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2916 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002917 if (user != null) {
2918 user.addToIntent(intent, Intent.EXTRA_USER);
2919 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002920 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002921 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002922 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002923 }
2924
Michael Jurka86a720e2012-05-09 11:23:23 -07002925 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002926 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002927 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002928 // Only launch using the new animation if the shortcut has not opted out (this is a
2929 // private contract between launcher and may be ignored in the future).
2930 boolean useLaunchAnimation = (v != null) &&
2931 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002932 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2933 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002934
Kenny Guy1317e2d2014-05-08 18:52:50 +01002935 UserHandleCompat user = null;
2936 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2937 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2938 user = userManager.getUserForSerialNumber(serialNumber);
2939 }
2940
2941 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002942 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002943 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002944 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2945 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002946 optsBundle = opts.toBundle();
2947 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002948
2949 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2950 // Could be launching some bookkeeping activity
2951 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002952 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002953 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002954 launcherApps.startActivityForProfile(intent.getComponent(), user,
2955 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002956 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002957 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002958 } catch (SecurityException e) {
2959 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002960 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002961 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002962 "or use the exported attribute for this activity. "
2963 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002964 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002965 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002967
Michael Jurka86a720e2012-05-09 11:23:23 -07002968 boolean startActivitySafely(View v, Intent intent, Object tag) {
2969 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002970 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2971 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2972 return false;
2973 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002974 try {
2975 success = startActivity(v, intent, tag);
2976 } catch (ActivityNotFoundException e) {
2977 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2978 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2979 }
2980 return success;
2981 }
2982
Adam Cohen268c4752012-06-06 17:47:33 -07002983 /**
2984 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2985 * in the DragLayer in the exact absolute location of the original FolderIcon.
2986 */
2987 private void copyFolderIconToImage(FolderIcon fi) {
2988 final int width = fi.getMeasuredWidth();
2989 final int height = fi.getMeasuredHeight();
2990
2991 // Lazy load ImageView, Bitmap and Canvas
2992 if (mFolderIconImageView == null) {
2993 mFolderIconImageView = new ImageView(this);
2994 }
2995 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2996 mFolderIconBitmap.getHeight() != height) {
2997 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
2998 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
2999 }
3000
3001 DragLayer.LayoutParams lp;
3002 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3003 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3004 } else {
3005 lp = new DragLayer.LayoutParams(width, height);
3006 }
3007
Adam Cohen307fe232012-08-16 17:55:58 -07003008 // The layout from which the folder is being opened may be scaled, adjust the starting
3009 // view size by this scale factor.
3010 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003011 lp.customPosition = true;
3012 lp.x = mRectForFolderAnimation.left;
3013 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003014 lp.width = (int) (scale * width);
3015 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003016
3017 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3018 fi.draw(mFolderIconCanvas);
3019 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003020 if (fi.getFolder() != null) {
3021 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3022 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003023 }
Adam Cohen268c4752012-06-06 17:47:33 -07003024 // Just in case this image view is still in the drag layer from a previous animation,
3025 // we remove it and re-add it.
3026 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3027 mDragLayer.removeView(mFolderIconImageView);
3028 }
3029 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003030 if (fi.getFolder() != null) {
3031 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003032 }
Adam Cohen268c4752012-06-06 17:47:33 -07003033 }
3034
Adam Cohen2801caf2011-05-13 20:57:39 -07003035 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003036 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003037 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3038 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3039 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3040
Adam Cohenc51934b2011-07-26 21:07:43 -07003041 FolderInfo info = (FolderInfo) fi.getTag();
3042 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3043 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003044 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3045 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003046 }
3047
Adam Cohen268c4752012-06-06 17:47:33 -07003048 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3049 copyFolderIconToImage(fi);
3050 fi.setVisibility(View.INVISIBLE);
3051
Michael Jurka2ecf9952012-06-18 12:52:28 -07003052 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003053 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003054 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003055 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3056 }
3057 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003058 oa.start();
3059 }
3060
Adam Cohen268c4752012-06-06 17:47:33 -07003061 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003062 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003063 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3064 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3065 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3066
Adam Cohen268c4752012-06-06 17:47:33 -07003067 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003068
Adam Cohen268c4752012-06-06 17:47:33 -07003069 // We remove and re-draw the FolderIcon in-case it has changed
3070 mDragLayer.removeView(mFolderIconImageView);
3071 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003072 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003073 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003074 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003075 oa.addListener(new AnimatorListenerAdapter() {
3076 @Override
3077 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003078 if (cl != null) {
3079 cl.clearFolderLeaveBehind();
3080 // Remove the ImageView copy of the FolderIcon and make the original visible.
3081 mDragLayer.removeView(mFolderIconImageView);
3082 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003083 }
3084 }
3085 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003086 oa.start();
3087 }
3088
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003089 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003090 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003091 * is animated relative to the specified View. If the View is null, no animation
3092 * is played.
3093 *
3094 * @param folderInfo The FolderInfo describing the folder to open.
3095 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003096 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003097 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003098 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003099
Adam Cohena9cf38f2011-05-02 15:36:58 -07003100 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003101
Adam Cohen4554ee12011-08-03 16:13:21 -07003102 // Just verify that the folder hasn't already been added to the DragLayer.
3103 // There was a one-off crash where the folder had a parent already.
3104 if (folder.getParent() == null) {
3105 mDragLayer.addView(folder);
3106 mDragController.addDropTarget((DropTarget) folder);
3107 } else {
3108 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3109 folder.getParent() + ").");
3110 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003111 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003112 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003113
3114 // Notify the accessibility manager that this folder "window" has appeared and occluded
3115 // the workspace items
3116 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3117 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003118 }
3119
3120 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003121 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003122 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003123 if (folder.isEditingName()) {
3124 folder.dismissEditingName();
3125 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003126 closeFolder(folder);
3127 }
3128 }
3129
3130 void closeFolder(Folder folder) {
3131 folder.getInfo().opened = false;
3132
3133 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3134 if (parent != null) {
3135 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3136 shrinkAndFadeInFolderIcon(fi);
3137 }
3138 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003139
3140 // Notify the accessibility manager that this folder "window" has disappeard and no
3141 // longer occludeds the workspace items
3142 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003143 }
3144
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003146 if (!isDraggingEnabled()) return false;
3147 if (isWorkspaceLocked()) return false;
3148 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003149
Adam Cohen1697b792013-09-17 19:08:21 -07003150 if (v instanceof Workspace) {
3151 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003152 if (mWorkspace.enterOverviewMode()) {
3153 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3154 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3155 return true;
3156 } else {
3157 return false;
3158 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003159 } else {
3160 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003161 }
Adam Cohen1697b792013-09-17 19:08:21 -07003162 }
3163
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003164 CellLayout.CellInfo longClickCellInfo = null;
3165 View itemUnderLongClick = null;
3166 if (v.getTag() instanceof ItemInfo) {
3167 ItemInfo info = (ItemInfo) v.getTag();
3168 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3169 itemUnderLongClick = longClickCellInfo.cell;
3170 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003171 }
3172
Winson Chung3d503fb2011-07-13 17:25:49 -07003173 // The hotseat touch handling does not go through Workspace, and we always allow long press
3174 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003175 final boolean inHotseat = isHotseatLayout(v);
3176 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003177 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003178 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003179 // User long pressed on empty space
3180 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3181 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003182 if (mWorkspace.isInOverviewMode()) {
3183 mWorkspace.startReordering(v);
3184 } else {
3185 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003186 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003187 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003188 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3189 mHotseat.getOrderInHotseat(
3190 longClickCellInfo.cellX,
3191 longClickCellInfo.cellY));
3192 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003193 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003194 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003195 }
3196 }
3197 }
3198 return true;
3199 }
3200
Winson Chung3d503fb2011-07-13 17:25:49 -07003201 boolean isHotseatLayout(View layout) {
3202 return mHotseat != null && layout != null &&
3203 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3204 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003205
Winson Chung3d503fb2011-07-13 17:25:49 -07003206 /**
3207 * Returns the CellLayout of the specified container at the specified screen.
3208 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003209 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003210 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3211 if (mHotseat != null) {
3212 return mHotseat.getLayout();
3213 } else {
3214 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003215 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003216 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003217 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003218 }
3219 }
3220
Daniel Sandler843e8602010-06-07 14:59:01 -04003221 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003222 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003223 }
3224
Craig Mautner360310b2012-10-26 15:13:08 -07003225 private void setWorkspaceBackground(boolean workspace) {
3226 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003227 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003228 }
3229
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003230 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003231 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3232 int curflags = getWindow().getAttributes().flags
3233 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3234 if (wpflags != curflags) {
3235 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3236 }
Craig Mautner360310b2012-10-26 15:13:08 -07003237 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003238 }
3239
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003240 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3241 if (v instanceof LauncherTransitionable) {
3242 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3243 }
3244 }
3245
Michael Jurkabed61d22012-02-14 22:51:29 -08003246 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3247 if (v instanceof LauncherTransitionable) {
3248 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3249 }
Winson Chung70442722012-02-10 15:43:22 -08003250
3251 // Update the workspace transition step as well
3252 dispatchOnLauncherTransitionStep(v, 0f);
3253 }
3254
3255 private void dispatchOnLauncherTransitionStep(View v, float t) {
3256 if (v instanceof LauncherTransitionable) {
3257 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3258 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003259 }
3260
3261 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3262 if (v instanceof LauncherTransitionable) {
3263 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3264 }
Winson Chung70442722012-02-10 15:43:22 -08003265
3266 // Update the workspace transition step as well
3267 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003268 }
3269
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003270 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003271 * Things to test when changing the following seven functions.
3272 * - Home from workspace
3273 * - from center screen
3274 * - from other screens
3275 * - Home from all apps
3276 * - from center screen
3277 * - from other screens
3278 * - Back from all apps
3279 * - from center screen
3280 * - from other screens
3281 * - Launch app from workspace and quit
3282 * - with back
3283 * - with home
3284 * - Launch app from all apps and quit
3285 * - with back
3286 * - with home
3287 * - Go to a screen that's not the default, then all
3288 * apps, and launch and app, and go back
3289 * - with back
3290 * -with home
3291 * - On workspace, long press power and go back
3292 * - with back
3293 * - with home
3294 * - On all apps, long press power and go back
3295 * - with back
3296 * - with home
3297 * - On workspace, power off
3298 * - On all apps, power off
3299 * - Launch an app and turn off the screen while in that app
3300 * - Go back with home key
3301 * - Go back with back key TODO: make this not go to workspace
3302 * - From all apps
3303 * - From workspace
3304 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3305 * - From all apps
3306 * - From the center workspace
3307 * - From another workspace
3308 */
3309
3310 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003311 * Zoom the camera out from the workspace to reveal 'toView'.
3312 * Assumes that the view to show is anchored at either the very top or very bottom
3313 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003314 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003315 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003316 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3317 showAppsCustomizeHelper(animated, springLoaded, contentType);
3318 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003319
Winson Chungc58497e2013-09-03 17:48:37 -07003320 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3321 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003322 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003323 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003324 mStateAnimation.cancel();
3325 mStateAnimation = null;
3326 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003327
Kenny Guyd794a3f2014-09-16 15:17:58 +01003328 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003329
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003330 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003331
Winson Chungf0ea4d32011-06-06 14:27:16 -07003332 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3333 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003334 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003335 final int itemsAlphaStagger =
3336 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003337
Winson Chungf0ea4d32011-06-06 14:27:16 -07003338 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003339 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003340 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003341
Adam Cohen2854d252014-08-27 16:04:07 -07003342 final ArrayList<View> layerViews = new ArrayList<View>();
3343
Adam Cohen6c5891a2014-07-09 23:53:15 -07003344 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3345 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003346 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003347 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003348 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003349 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3350 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003351 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3352 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003353
Adam Cohena38dc902014-09-07 17:48:55 +02003354 // If for some reason our views aren't initialized, don't animate
3355 boolean initialized = getAllAppsButton() != null;
3356
3357 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003358 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003359 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3360 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003361
Adam Cohen9bfdb762014-07-21 17:44:06 -07003362 final View page = content.getPageAt(content.getCurrentPage());
3363 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003364
Adam Cohen63f1ec02014-08-12 09:23:13 -07003365 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3366 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003367 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3368 } else {
3369 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3370 }
3371
Adam Cohen9bfdb762014-07-21 17:44:06 -07003372 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003373 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003374 revealView.setVisibility(View.VISIBLE);
3375 // We need to hide this view as the animation start will be posted.
3376 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003377
Adam Cohen9bfdb762014-07-21 17:44:06 -07003378 int width = revealView.getMeasuredWidth();
3379 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003380 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003381
Adam Cohen63f1ec02014-08-12 09:23:13 -07003382 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003383 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003384
Adam Cohen63f1ec02014-08-12 09:23:13 -07003385 // Get the y delta between the center of the page and the center of the all apps button
3386 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3387 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003388
Adam Cohen2854d252014-08-27 16:04:07 -07003389 float alpha = 0;
3390 float xDrift = 0;
3391 float yDrift = 0;
3392 if (material) {
3393 alpha = isWidgetTray ? 0.3f : 1f;
3394 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3395 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3396 } else {
3397 yDrift = 2 * height / 3;
3398 xDrift = 0;
3399 }
3400 final float initAlpha = alpha;
3401
Adam Cohen9bfdb762014-07-21 17:44:06 -07003402 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003403 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003404 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003405 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003406 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003407 PropertyValuesHolder panelDriftX =
3408 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003409
Adam Cohen2854d252014-08-27 16:04:07 -07003410 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3411 panelAlpha, panelDriftY, panelDriftX);
3412
Adam Cohen9bfdb762014-07-21 17:44:06 -07003413 panelAlphaAndDrift.setDuration(revealDuration);
3414 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003415
Adam Cohen9bfdb762014-07-21 17:44:06 -07003416 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003417
Adam Cohen4e243a22014-08-10 18:30:55 -07003418 if (page != null) {
3419 page.setVisibility(View.VISIBLE);
3420 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003421 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003422
Adam Cohen2854d252014-08-27 16:04:07 -07003423 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3424 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003425 pageDrift.setDuration(revealDuration);
3426 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003427 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003428 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003429
Adam Cohen63f1ec02014-08-12 09:23:13 -07003430 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003431 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003432 itemsAlpha.setDuration(revealDuration);
3433 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3434 itemsAlpha.setStartDelay(itemsAlphaStagger);
3435 mStateAnimation.play(itemsAlpha);
3436 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003437
Adam Cohen4e243a22014-08-10 18:30:55 -07003438 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3439 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003440 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003441 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003442 indicatorsAlpha.setDuration(revealDuration);
3443 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003444
Adam Cohen9bfdb762014-07-21 17:44:06 -07003445 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003446 final View allApps = getAllAppsButton();
3447 int allAppsButtonSize = LauncherAppState.getInstance().
3448 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3449 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003450 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003451 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003452 reveal.setDuration(revealDuration);
3453 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003454
3455 reveal.addListener(new AnimatorListenerAdapter() {
3456 public void onAnimationStart(Animator animation) {
3457 if (!isWidgetTray) {
3458 allApps.setVisibility(View.INVISIBLE);
3459 }
3460 }
3461 public void onAnimationEnd(Animator animation) {
3462 if (!isWidgetTray) {
3463 allApps.setVisibility(View.VISIBLE);
3464 }
3465 }
3466 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003467 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003468 }
Michael Jurka1899a362011-11-03 13:50:45 -07003469
Adam Cohen9bfdb762014-07-21 17:44:06 -07003470 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3471 @Override
3472 public void onAnimationEnd(Animator animation) {
3473 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3474 dispatchOnLauncherTransitionEnd(toView, animated, false);
3475
3476 revealView.setVisibility(View.INVISIBLE);
3477 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003478 if (page != null) {
3479 page.setLayerType(View.LAYER_TYPE_NONE, null);
3480 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003481 content.setPageBackgroundsVisible(true);
3482
3483 // Hide the search bar
3484 if (mSearchDropTargetBar != null) {
3485 mSearchDropTargetBar.hideSearchBar(false);
3486 }
Adam Cohen3f9c9712014-10-31 11:48:25 -07003487
3488 // This can hold unnecessary references to views.
3489 mStateAnimation = null;
Adam Cohen9bfdb762014-07-21 17:44:06 -07003490 }
3491
Adam Cohen9bfdb762014-07-21 17:44:06 -07003492 });
3493
Adam Cohen6c5891a2014-07-09 23:53:15 -07003494 if (workspaceAnim != null) {
3495 mStateAnimation.play(workspaceAnim);
3496 }
Adam Cohen2854d252014-08-27 16:04:07 -07003497
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003498 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3499 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003500 final AnimatorSet stateAnimation = mStateAnimation;
3501 final Runnable startAnimRunnable = new Runnable() {
3502 public void run() {
3503 // Check that mStateAnimation hasn't changed while
3504 // we waited for a layout/draw pass
3505 if (mStateAnimation != stateAnimation)
3506 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003507 dispatchOnLauncherTransitionStart(fromView, animated, false);
3508 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003509
3510 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003511 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003512 for (int i = 0; i < layerViews.size(); i++) {
3513 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003514 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003515 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003516 }
Adam Cohen2854d252014-08-27 16:04:07 -07003517 }
3518 }
3519 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003520 }
3521 };
Adam Cohen2854d252014-08-27 16:04:07 -07003522 toView.bringToFront();
3523 toView.setVisibility(View.VISIBLE);
3524 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003525 } else {
3526 toView.setTranslationX(0.0f);
3527 toView.setTranslationY(0.0f);
3528 toView.setScaleX(1.0f);
3529 toView.setScaleY(1.0f);
3530 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003531 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003532
Daniel Sandlere4f98912013-06-25 15:13:26 -04003533 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003534 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003535 if (mSearchDropTargetBar != null) {
3536 mSearchDropTargetBar.hideSearchBar(false);
3537 }
Michael Jurkaabded662011-03-04 12:06:57 -08003538 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003539 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003540 dispatchOnLauncherTransitionStart(fromView, animated, false);
3541 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003542 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003543 dispatchOnLauncherTransitionStart(toView, animated, false);
3544 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003545 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003546 }
3547
3548 /**
3549 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003550 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003551 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003552 */
Adam Cohened307df2013-10-02 09:37:31 -07003553 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003554 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003555
Michael Jurkab3e22d92011-10-31 15:58:33 -07003556 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003557 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003558 mStateAnimation.cancel();
3559 mStateAnimation = null;
3560 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003561
Kenny Guyd794a3f2014-09-16 15:17:58 +01003562 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003563 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003564
Winson Chungf0ea4d32011-06-06 14:27:16 -07003565 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003566 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003567 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003568 final int itemsAlphaStagger =
3569 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003570
Winson Chungf0ea4d32011-06-06 14:27:16 -07003571 final float scaleFactor = (float)
3572 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3573 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003574 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003575 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003576 final ArrayList<View> layerViews = new ArrayList<View>();
3577
Adam Cohened307df2013-10-02 09:37:31 -07003578 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003579 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003580 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003581 } else if (toState == Workspace.State.SPRING_LOADED ||
3582 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003583 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003584 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003585 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003586
Adam Cohena38dc902014-09-07 17:48:55 +02003587 // If for some reason our views aren't initialized, don't animate
3588 boolean initialized = getAllAppsButton() != null;
3589
3590 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003591 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003592 if (workspaceAnim != null) {
3593 mStateAnimation.play(workspaceAnim);
3594 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003595
Adam Cohen9bfdb762014-07-21 17:44:06 -07003596 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3597 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003598
Adam Cohen9bfdb762014-07-21 17:44:06 -07003599 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003600
3601 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3602 int count = content.getChildCount();
3603 for (int i = 0; i < count; i++) {
3604 View child = content.getChildAt(i);
3605 if (child != page) {
3606 child.setVisibility(View.INVISIBLE);
3607 }
3608 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003609 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003610
Adam Cohen2854d252014-08-27 16:04:07 -07003611 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3612 // don't perform all these no-op animations. In particularly, this was causing
3613 // the all-apps button to pop in and out.
3614 if (fromView.getVisibility() == View.VISIBLE) {
3615 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3616 final boolean isWidgetTray =
3617 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003618
Adam Cohen2854d252014-08-27 16:04:07 -07003619 if (isWidgetTray) {
3620 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3621 } else {
3622 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003623 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003624
Adam Cohen2854d252014-08-27 16:04:07 -07003625 int width = revealView.getMeasuredWidth();
3626 int height = revealView.getMeasuredHeight();
3627 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3628
3629 // Hide the real page background, and swap in the fake one
3630 revealView.setVisibility(View.VISIBLE);
3631 content.setPageBackgroundsVisible(false);
3632
3633 final View allAppsButton = getAllAppsButton();
3634 revealView.setTranslationY(0);
3635 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3636 allAppsButton, null);
3637
3638 float xDrift = 0;
3639 float yDrift = 0;
3640 if (material) {
3641 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3642 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3643 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003644 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003645 xDrift = 0;
3646 }
3647
3648 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3649 TimeInterpolator decelerateInterpolator = material ?
3650 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003651 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003652
3653 // The vertical motion of the apps panel should be delayed by one frame
3654 // from the conceal animation in order to give the right feel. We correpsondingly
3655 // shorten the duration so that the slide and conceal end at the same time.
3656 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3657 0, yDrift);
3658 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3659 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3660 panelDriftY.setInterpolator(decelerateInterpolator);
3661 mStateAnimation.play(panelDriftY);
3662
3663 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3664 0, xDrift);
3665 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3666 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3667 panelDriftX.setInterpolator(decelerateInterpolator);
3668 mStateAnimation.play(panelDriftX);
3669
3670 if (isWidgetTray || !material) {
3671 float finalAlpha = material ? 0.4f : 0f;
3672 revealView.setAlpha(1f);
3673 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3674 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003675 panelAlpha.setDuration(material ? revealDuration : 150);
3676 panelAlpha.setInterpolator(decelerateInterpolator);
3677 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003678 mStateAnimation.play(panelAlpha);
3679 }
3680
3681 if (page != null) {
3682 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3683
3684 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3685 0, yDrift);
3686 page.setTranslationY(0);
3687 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3688 pageDrift.setInterpolator(decelerateInterpolator);
3689 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3690 mStateAnimation.play(pageDrift);
3691
3692 page.setAlpha(1f);
3693 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3694 itemsAlpha.setDuration(100);
3695 itemsAlpha.setInterpolator(decelerateInterpolator);
3696 mStateAnimation.play(itemsAlpha);
3697 }
3698
3699 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3700 pageIndicators.setAlpha(1f);
3701 ObjectAnimator indicatorsAlpha =
3702 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3703 indicatorsAlpha.setDuration(revealDuration);
3704 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3705 mStateAnimation.play(indicatorsAlpha);
3706
3707 width = revealView.getMeasuredWidth();
3708
3709 if (material) {
3710 if (!isWidgetTray) {
3711 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003712 }
Adam Cohen2854d252014-08-27 16:04:07 -07003713 int allAppsButtonSize = LauncherAppState.getInstance().
3714 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3715 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3716 Animator reveal =
3717 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3718 height / 2, revealRadius, finalRadius);
3719 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3720 reveal.setDuration(revealDuration);
3721 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003722
Adam Cohen2854d252014-08-27 16:04:07 -07003723 reveal.addListener(new AnimatorListenerAdapter() {
3724 public void onAnimationEnd(Animator animation) {
3725 revealView.setVisibility(View.INVISIBLE);
3726 if (!isWidgetTray) {
3727 allAppsButton.setVisibility(View.VISIBLE);
3728 }
3729 }
3730 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003731
Adam Cohen2854d252014-08-27 16:04:07 -07003732 mStateAnimation.play(reveal);
3733 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003734
Adam Cohen2854d252014-08-27 16:04:07 -07003735 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3736 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3737 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003738 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003739
3740 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003741 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003742 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003743 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003744 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3745 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003746 if (onCompleteRunnable != null) {
3747 onCompleteRunnable.run();
3748 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003749
3750 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003751 if (page != null) {
3752 page.setLayerType(View.LAYER_TYPE_NONE, null);
3753 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003754 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003755 // Unhide side pages
3756 int count = content.getChildCount();
3757 for (int i = 0; i < count; i++) {
3758 View child = content.getChildAt(i);
3759 child.setVisibility(View.VISIBLE);
3760 }
3761
3762 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003763 if (page != null) {
3764 page.setTranslationX(0);
3765 page.setTranslationY(0);
3766 page.setAlpha(1);
3767 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003768 content.setCurrentPage(content.getNextPage());
3769
Chet Haasebc2f0822012-10-26 17:59:53 -07003770 mAppsCustomizeContent.updateCurrentPageScroll();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003771
3772 // This can hold unnecessary references to views.
3773 mStateAnimation = null;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003774 }
3775 });
3776
Adam Cohen2854d252014-08-27 16:04:07 -07003777 final AnimatorSet stateAnimation = mStateAnimation;
3778 final Runnable startAnimRunnable = new Runnable() {
3779 public void run() {
3780 // Check that mStateAnimation hasn't changed while
3781 // we waited for a layout/draw pass
3782 if (mStateAnimation != stateAnimation)
3783 return;
3784 dispatchOnLauncherTransitionStart(fromView, animated, false);
3785 dispatchOnLauncherTransitionStart(toView, animated, false);
3786
Kenny Guyd794a3f2014-09-16 15:17:58 +01003787 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003788 for (int i = 0; i < layerViews.size(); i++) {
3789 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003790 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003791 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003792 }
Adam Cohen2854d252014-08-27 16:04:07 -07003793 }
3794 }
3795 mStateAnimation.start();
3796 }
3797 };
3798 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003799 } else {
3800 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003801 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003802 dispatchOnLauncherTransitionStart(fromView, animated, true);
3803 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003804 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003805 dispatchOnLauncherTransitionStart(toView, animated, true);
3806 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003807 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003808 }
3809
Michael Jurkae326f182011-11-21 14:05:46 -08003810 @Override
3811 public void onTrimMemory(int level) {
3812 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003813 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3814 // The widget preview db can result in holding onto over
3815 // 3MB of memory for caching which isn't necessary.
3816 SQLiteDatabase.releaseMemory();
3817
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003818 // This clears all widget bitmaps from the widget tray
Adam Cohen3f9c9712014-10-31 11:48:25 -07003819 if (mAppsCustomizeTabHost != null) {
Adam Cohenc8f4e1b2014-11-19 16:03:20 -08003820 mAppsCustomizeTabHost.trimMemory();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003821 }
Michael Jurkae326f182011-11-21 14:05:46 -08003822 }
3823 }
3824
Adam Cohened307df2013-10-02 09:37:31 -07003825 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003826 showWorkspace(animated, null);
3827 }
3828
Adam Cohened307df2013-10-02 09:37:31 -07003829 protected void showWorkspace() {
3830 showWorkspace(true);
3831 }
3832
Adam Cohened66b2b2012-01-23 17:28:51 -08003833 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003834 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003835 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003836 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003837 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003838
Winson Chungc7d2b602012-05-16 17:02:20 -07003839 // Show the search bar (only animate if we were showing the drop target bar in spring
3840 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003841 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003842 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003843 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003844
Michael Jurkab3e22d92011-10-31 15:58:33 -07003845 // Set focus to the AppsCustomize button
3846 if (mAllAppsButton != null) {
3847 mAllAppsButton.requestFocus();
3848 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003849 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003850
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003851 // Change the state *after* we've called all the transition code
3852 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003853
Winson Chung5fb63472011-02-02 17:03:37 -08003854 // Resume the auto-advance of widgets
3855 mUserPresent = true;
3856 updateRunning();
3857
alanv1d4fde62012-10-17 13:15:47 -07003858 // Send an accessibility event to announce the context change
3859 getWindow().getDecorView()
3860 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003861
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003862 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003863 }
3864
Adam Cohened307df2013-10-02 09:37:31 -07003865 void showOverviewMode(boolean animated) {
3866 mWorkspace.setVisibility(View.VISIBLE);
3867 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3868 mState = State.WORKSPACE;
3869 onWorkspaceShown(animated);
3870 }
3871
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003872 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003873 }
3874
Winson Chung82963d52013-10-09 11:20:57 -07003875 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3876 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003877 if (mState != State.WORKSPACE) return;
3878
Winson Chung82963d52013-10-09 11:20:57 -07003879 if (resetPageToZero) {
3880 mAppsCustomizeTabHost.reset();
3881 }
Winson Chungc58497e2013-09-03 17:48:37 -07003882 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003883 mAppsCustomizeTabHost.post(new Runnable() {
3884 @Override
3885 public void run() {
3886 // We post this in-case the all apps view isn't yet constructed.
3887 mAppsCustomizeTabHost.requestFocus();
3888 }
3889 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003890
Michael Jurkab3e22d92011-10-31 15:58:33 -07003891 // Change the state *after* we've called all the transition code
3892 mState = State.APPS_CUSTOMIZE;
3893
3894 // Pause the auto-advance of widgets until we are out of AllApps
3895 mUserPresent = false;
3896 updateRunning();
3897 closeFolder();
3898
3899 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003900 getWindow().getDecorView()
3901 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003902 }
3903
Adam Cohen7777d962011-08-18 18:58:38 -07003904 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003905 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003906 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003907 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003908 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003909 }
Adam Cohen7777d962011-08-18 18:58:38 -07003910
Adam Cohenad4e15c2013-10-17 16:21:35 -07003911 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003912 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003913 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3914
Winson Chunge7a03942011-08-05 15:05:12 -07003915 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003916 @Override
3917 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003918 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003919 // Before we show workspace, hide all apps again because
3920 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3921 // clean up our state transition functions
3922 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003923 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003924 } else {
3925 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003926 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003927 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003928 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003929 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003930
Michael Jurkad3ef3062010-11-23 16:23:58 -08003931 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003932 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003933 final boolean animated = true;
3934 final boolean springLoaded = true;
3935 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003936 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003937 }
3938 // Otherwise, we are not in spring loaded mode, so don't do anything.
3939 }
3940
Michael Jurkab3e22d92011-10-31 15:58:33 -07003941 void lockAllApps() {
3942 // TODO
3943 }
3944
3945 void unlockAllApps() {
3946 // TODO
3947 }
3948
Sunny Goyal594d76d2014-11-06 10:12:54 -08003949 protected void disableVoiceButtonProxy(boolean disable) {
3950 // NO-OP
Winson Chungbb185bd2011-11-21 12:31:42 -08003951 }
3952
Cristina Stancu476493b2013-08-07 17:20:14 +01003953 public View getQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07003954 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
3955 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07003956 }
3957
Adam Cohen24ce0b32014-01-14 16:18:14 -08003958 if (mQsb == null) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003959 AppWidgetProviderInfo searchProvider = Utilities.getSearchWidgetProvider(this);
3960 if (searchProvider == null) {
3961 return null;
3962 }
3963
3964 Bundle opts = new Bundle();
3965 opts.putInt(AppWidgetManager.OPTION_APPWIDGET_HOST_CATEGORY,
3966 AppWidgetProviderInfo.WIDGET_CATEGORY_SEARCHBOX);
3967
3968 SharedPreferences sp = getSharedPreferences(
3969 LauncherAppState.getSharedPreferencesKey(), MODE_PRIVATE);
3970 int widgetId = sp.getInt(QSB_WIDGET_ID, -1);
Sunny Goyal416541c2014-11-14 11:59:57 -08003971 AppWidgetProviderInfo widgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
Sunny Goyal594d76d2014-11-06 10:12:54 -08003972 if (!searchProvider.provider.flattenToString().equals(
3973 sp.getString(QSB_WIDGET_PROVIDER, null))
Sunny Goyal416541c2014-11-14 11:59:57 -08003974 || (widgetInfo == null)
3975 || !widgetInfo.provider.equals(searchProvider.provider)) {
Sunny Goyal594d76d2014-11-06 10:12:54 -08003976 // A valid widget is not already bound.
3977 if (widgetId > -1) {
3978 mAppWidgetHost.deleteAppWidgetId(widgetId);
3979 widgetId = -1;
3980 }
3981
3982 // Try to bind a new widget
3983 widgetId = mAppWidgetHost.allocateAppWidgetId();
3984
3985 if (!AppWidgetManagerCompat.getInstance(this)
3986 .bindAppWidgetIdIfAllowed(widgetId, searchProvider, opts)) {
3987 mAppWidgetHost.deleteAppWidgetId(widgetId);
3988 widgetId = -1;
3989 }
3990
3991 sp.edit()
3992 .putInt(QSB_WIDGET_ID, widgetId)
3993 .putString(QSB_WIDGET_PROVIDER, searchProvider.provider.flattenToString())
3994 .commit();
3995 }
3996
3997 if (widgetId != -1) {
3998 mQsb = mAppWidgetHost.createView(this, widgetId, searchProvider);
3999 mQsb.updateAppWidgetOptions(opts);
4000 mQsb.setPadding(0, 0, 0, 0);
4001 mSearchDropTargetBar.addView(mQsb);
4002 }
Cristina Stancu476493b2013-08-07 17:20:14 +01004003 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08004004 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01004005 }
4006
Michael Jurkad7c28052012-04-27 15:43:36 -07004007 @Override
4008 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004009 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004010 final List<CharSequence> text = event.getText();
4011 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004012 // Populate event with a fake title based on the current state.
4013 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004014 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004015 } else {
4016 text.add(getString(R.string.all_apps_home_button_label));
4017 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004018 return result;
4019 }
4020
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004021 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004022 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004023 */
4024 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4025 @Override
4026 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004027 closeSystemDialogs();
4028 }
4029 }
4030
4031 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004032 * Receives notifications whenever the appwidgets are reset.
4033 */
4034 private class AppWidgetResetObserver extends ContentObserver {
4035 public AppWidgetResetObserver() {
4036 super(new Handler());
4037 }
4038
4039 @Override
4040 public void onChange(boolean selfChange) {
4041 onAppWidgetReset();
4042 }
4043 }
4044
4045 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004046 * If the activity is currently paused, signal that we need to run the passed Runnable
4047 * in onResume.
4048 *
4049 * This needs to be called from incoming places where resources might have been loaded
4050 * while we are paused. That is becaues the Configuration might be wrong
4051 * when we're not running, and if it comes back to what it was when we
4052 * were paused, we are not restarted.
4053 *
4054 * Implementation of the method from LauncherModel.Callbacks.
4055 *
4056 * @return true if we are currently paused. The caller might be able to
4057 * skip some work in that case since we will come back again.
4058 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004059 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004060 if (mPaused) {
4061 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004062 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004063 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004064 }
4065 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004066 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004067 return true;
4068 } else {
4069 return false;
4070 }
4071 }
4072
Michael Jurkac402cd92013-05-20 15:49:32 +02004073 private boolean waitUntilResume(Runnable run) {
4074 return waitUntilResume(run, false);
4075 }
4076
Michael Jurka1e2f4652013-07-08 18:03:46 -07004077 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004078 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004079 }
4080
Michael Jurka7607c2f2013-04-03 14:33:19 -07004081 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004082 * If the activity is currently paused, signal that we need to re-run the loader
4083 * in onResume.
4084 *
4085 * This needs to be called from incoming places where resources might have been loaded
4086 * while we are paused. That is becaues the Configuration might be wrong
4087 * when we're not running, and if it comes back to what it was when we
4088 * were paused, we are not restarted.
4089 *
4090 * Implementation of the method from LauncherModel.Callbacks.
4091 *
4092 * @return true if we are currently paused. The caller might be able to
4093 * skip some work in that case since we will come back again.
4094 */
4095 public boolean setLoadOnResume() {
4096 if (mPaused) {
4097 Log.i(TAG, "setLoadOnResume");
4098 mOnResumeNeedsLoad = true;
4099 return true;
4100 } else {
4101 return false;
4102 }
4103 }
4104
4105 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004106 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004107 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004108 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004109 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004110 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004111 } else {
4112 return SCREEN_COUNT / 2;
4113 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004114 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004115
Joe Onorato9c1289c2009-08-17 11:03:03 -04004116 /**
4117 * Refreshes the shortcuts shown on the workspace.
4118 *
4119 * Implementation of the method from LauncherModel.Callbacks.
4120 */
4121 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004122 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004123
Michael Jurka7607c2f2013-04-03 14:33:19 -07004124 // If we're starting binding all over again, clear any bind calls we'd postponed in
4125 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4126 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004127 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004128
Winson Chungd64d1762013-08-20 14:37:16 -07004129 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004130 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004131 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004132
Michael Jurka05bf6442011-11-30 20:28:53 -08004133 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004134 if (mHotseat != null) {
4135 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004136 }
4137 }
4138
Adam Cohendcd297f2013-06-18 13:13:40 -07004139 @Override
4140 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004141 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004142
Adam Cohen5084cba2013-09-03 12:01:16 -07004143 // If there are no screens, we need to have an empty screen
4144 if (orderedScreenIds.size() == 0) {
4145 mWorkspace.addExtraEmptyScreen();
4146 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004147
4148 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004149 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004150 if (hasCustomContentToLeft()) {
4151 mWorkspace.createCustomContentContainer();
4152 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004153 }
Winson Chung64359a52013-07-08 17:17:08 -07004154 }
4155
4156 @Override
4157 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004158 // Log to disk
4159 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4160 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4161 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004162 int count = orderedScreenIds.size();
4163 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004164 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004165 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004166 }
4167
Adam Cohen39a06042013-07-19 14:30:12 -07004168 private boolean shouldShowWeightWatcher() {
4169 String spKey = LauncherAppState.getSharedPreferencesKey();
4170 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004171 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004172
4173 return show;
4174 }
4175
4176 private void toggleShowWeightWatcher() {
4177 String spKey = LauncherAppState.getSharedPreferencesKey();
4178 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4179 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4180
4181 show = !show;
4182
4183 SharedPreferences.Editor editor = sp.edit();
4184 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4185 editor.commit();
4186
4187 if (mWeightWatcher != null) {
4188 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4189 }
4190 }
4191
Winson Chungd64d1762013-08-20 14:37:16 -07004192 public void bindAppsAdded(final ArrayList<Long> newScreens,
4193 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004194 final ArrayList<ItemInfo> addAnimated,
4195 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004196 Runnable r = new Runnable() {
4197 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004198 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004199 }
4200 };
4201 if (waitUntilResume(r)) {
4202 return;
4203 }
4204
Winson Chungd64d1762013-08-20 14:37:16 -07004205 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004206 if (newScreens != null) {
4207 bindAddScreens(newScreens);
4208 }
Winson Chungd64d1762013-08-20 14:37:16 -07004209
4210 // We add the items without animation on non-visible pages, and with
4211 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004212 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004213 bindItems(addNotAnimated, 0,
4214 addNotAnimated.size(), false);
4215 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004216 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004217 bindItems(addAnimated, 0,
4218 addAnimated.size(), true);
4219 }
Winson Chungc58497e2013-09-03 17:48:37 -07004220
Winson Chung87412982013-10-03 18:34:14 -07004221 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004222 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004223
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004224 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004225 addedApps != null && mAppsCustomizeContent != null) {
4226 mAppsCustomizeContent.addApps(addedApps);
4227 }
Winson Chungd64d1762013-08-20 14:37:16 -07004228 }
4229
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004230 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004231 * Bind the items start-end from the list.
4232 *
4233 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004234 */
Winson Chung64359a52013-07-08 17:17:08 -07004235 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4236 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004237 Runnable r = new Runnable() {
4238 public void run() {
4239 bindItems(shortcuts, start, end, forceAnimateIcons);
4240 }
4241 };
4242 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004243 return;
4244 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004245
Winson Chungf0c6ae02012-03-21 16:10:31 -07004246 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004247 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4248 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004249 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004250 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004251 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004252 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004253 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004254
4255 // Short circuit if we are loading dock items for a configuration which has no dock
4256 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4257 mHotseat == null) {
4258 continue;
4259 }
4260
Joe Onorato9c1289c2009-08-17 11:03:03 -04004261 switch (item.itemType) {
4262 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4263 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004264 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004265 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004266
Winson Chung64359a52013-07-08 17:17:08 -07004267 /*
4268 * TODO: FIX collision case
4269 */
Winson Chungce376632013-07-11 16:12:41 -07004270 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4271 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4272 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004273 View v = cl.getChildAt(item.cellX, item.cellY);
4274 Object tag = v.getTag();
4275 String desc = "Collision while binding workspace item: " + item
4276 + ". Collides with " + tag;
4277 if (LauncherAppState.isDogfoodBuild()) {
4278 throw (new RuntimeException(desc));
4279 } else {
4280 Log.d(TAG, desc);
4281 }
Winson Chungce376632013-07-11 16:12:41 -07004282 }
Winson Chung64359a52013-07-08 17:17:08 -07004283 }
4284
Adam Cohendcd297f2013-06-18 13:13:40 -07004285 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4286 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004287 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004288 // Animate all the applications up now
4289 shortcut.setAlpha(0f);
4290 shortcut.setScaleX(0f);
4291 shortcut.setScaleY(0f);
4292 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004293 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004294 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004295 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004296 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004297 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004298 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004299 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004300 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4301 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004302 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004303 default:
4304 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004305 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004306 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004307
Winson Chung997a9232013-07-24 15:33:46 -07004308 if (animateIcons) {
4309 // Animate to the correct page
4310 if (newShortcutsScreenId > -1) {
4311 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004312 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004313 final Runnable startBounceAnimRunnable = new Runnable() {
4314 public void run() {
4315 anim.playTogether(bounceAnims);
4316 anim.start();
4317 }
4318 };
Winson Chung997a9232013-07-24 15:33:46 -07004319 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004320 // We post the animation slightly delayed to prevent slowdowns
4321 // when we are loading right after we return to launcher.
4322 mWorkspace.postDelayed(new Runnable() {
4323 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004324 if (mWorkspace != null) {
4325 mWorkspace.snapToPage(newScreenIndex);
4326 mWorkspace.postDelayed(startBounceAnimRunnable,
4327 NEW_APPS_ANIMATION_DELAY);
4328 }
Winson Chung94d67682013-09-25 16:29:40 -07004329 }
4330 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004331 } else {
4332 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004333 }
4334 }
Winson Chung64359a52013-07-08 17:17:08 -07004335 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004336 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004337 }
4338
Joe Onorato9c1289c2009-08-17 11:03:03 -04004339 /**
4340 * Implementation of the method from LauncherModel.Callbacks.
4341 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004342 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004343 Runnable r = new Runnable() {
4344 public void run() {
4345 bindFolders(folders);
4346 }
4347 };
4348 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004349 return;
4350 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004351 sFolders.clear();
4352 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004353 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004354
4355 /**
4356 * Add the views for a widget to the workspace.
4357 *
4358 * Implementation of the method from LauncherModel.Callbacks.
4359 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004360 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004361 Runnable r = new Runnable() {
4362 public void run() {
4363 bindAppWidget(item);
4364 }
4365 };
4366 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004367 return;
4368 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004369
Daniel Sandler843e8602010-06-07 14:59:01 -04004370 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4371 if (DEBUG_WIDGETS) {
4372 Log.d(TAG, "bindAppWidget: " + item);
4373 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004374 final Workspace workspace = mWorkspace;
4375
Sunny Goyalff572272014-07-23 13:58:07 -07004376 AppWidgetProviderInfo appWidgetInfo;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004377 if (!mIsSafeModeEnabled
4378 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4379 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004380
4381 appWidgetInfo = mModel.findAppWidgetProviderInfoWithComponent(this, item.providerName);
4382 if (appWidgetInfo == null) {
4383 if (DEBUG_WIDGETS) {
4384 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4385 + " belongs to component " + item.providerName
4386 + ", as the povider is null");
4387 }
4388 LauncherModel.deleteItemFromDatabase(this, item);
4389 return;
4390 }
4391 // Note: This assumes that the id remap broadcast is received before this step.
4392 // If that is not the case, the id remap will be ignored and user may see the
4393 // click to setup view.
4394 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null, null);
4395 pendingInfo.spanX = item.spanX;
4396 pendingInfo.spanY = item.spanY;
4397 pendingInfo.minSpanX = item.minSpanX;
4398 pendingInfo.minSpanY = item.minSpanY;
4399 Bundle options =
4400 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4401
Sunny Goyalff572272014-07-23 13:58:07 -07004402 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004403 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4404 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004405
4406 // TODO consider showing a permission dialog when the widget is clicked.
4407 if (!success) {
4408 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4409 if (DEBUG_WIDGETS) {
4410 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4411 + " belongs to component " + item.providerName
4412 + ", as the launcher is unable to bing a new widget id");
4413 }
4414 LauncherModel.deleteItemFromDatabase(this, item);
4415 return;
4416 }
4417
4418 item.appWidgetId = newWidgetId;
4419
4420 // If the widget has a configure activity, it is still needs to set it up, otherwise
4421 // the widget is ready to go.
4422 item.restoreStatus = (appWidgetInfo.configure == null)
4423 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4424 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4425
4426 LauncherModel.updateItemInDatabase(this, item);
4427 }
4428
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004429 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004430 final int appWidgetId = item.appWidgetId;
4431 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4432 if (DEBUG_WIDGETS) {
4433 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4434 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004435
Sunny Goyal651077b2014-06-30 14:15:31 -07004436 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4437 } else {
4438 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004439 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4440 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004441 view.updateIcon(mIconCache);
4442 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004443 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004444 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004445 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004446
Joe Onorato9c1289c2009-08-17 11:03:03 -04004447 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004448 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004449
Adam Cohendcd297f2013-06-18 13:13:40 -07004450 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004451 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004452 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4453
Joe Onorato9c1289c2009-08-17 11:03:03 -04004454 workspace.requestLayout();
4455
Daniel Sandler843e8602010-06-07 14:59:01 -04004456 if (DEBUG_WIDGETS) {
4457 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4458 + (SystemClock.uptimeMillis()-start) + "ms");
4459 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004460 }
4461
Sunny Goyalff572272014-07-23 13:58:07 -07004462 /**
4463 * Restores a pending widget.
4464 *
4465 * @param appWidgetId The app widget id
4466 * @param cellInfo The position on screen where to create the widget.
4467 */
4468 private void completeRestoreAppWidget(final int appWidgetId) {
4469 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4470 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4471 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4472 return;
4473 }
4474
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004475 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004476 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4477
4478 mWorkspace.reinflateWidgetsIfNecessary();
4479 LauncherModel.updateItemInDatabase(this, info);
4480 }
4481
Adam Cohen1462de32012-07-24 22:34:36 -07004482 public void onPageBoundSynchronously(int page) {
4483 mSynchronouslyBoundPages.add(page);
4484 }
4485
Joe Onorato9c1289c2009-08-17 11:03:03 -04004486 /**
4487 * Callback saying that there aren't any more items to bind.
4488 *
4489 * Implementation of the method from LauncherModel.Callbacks.
4490 */
Adam Cohene25af792013-06-06 23:08:25 -07004491 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004492 Runnable r = new Runnable() {
4493 public void run() {
4494 finishBindingItems(upgradePath);
4495 }
4496 };
4497 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004498 return;
4499 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004500 if (mSavedState != null) {
4501 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004502 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004503 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004504 mSavedState = null;
4505 }
4506
Adam Cohen1462de32012-07-24 22:34:36 -07004507 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004508
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004509 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004510 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004511
4512 // If we received the result of any pending adds while the loader was running (e.g. the
4513 // widget configuration forced an orientation change), process them now.
4514 if (sPendingAddItem != null) {
4515 final long screenId = completeAdd(sPendingAddItem);
4516
4517 // TODO: this moves the user to the page where the pending item was added. Ideally,
4518 // the screen would be guaranteed to exist after bind, and the page would be set through
4519 // the workspace restore process.
4520 mWorkspace.post(new Runnable() {
4521 @Override
4522 public void run() {
4523 mWorkspace.snapToScreenId(screenId);
4524 }
4525 });
4526 sPendingAddItem = null;
4527 }
4528
Adam Cohene25af792013-06-06 23:08:25 -07004529 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004530 mWorkspace.getUniqueComponents(true, null);
4531 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004532 }
Sunny Goyale755d462014-07-22 13:48:29 -07004533 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004534
4535 if (mLauncherCallbacks != null) {
4536 mLauncherCallbacks.finishBindingItems(upgradePath);
4537 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004538 }
4539
Adam Cohen3ed316a2014-07-23 18:21:20 -07004540 private void sendLoadingCompleteBroadcastIfNecessary() {
4541 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4542 String permission =
4543 getResources().getString(R.string.receive_first_load_broadcast_permission);
4544 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4545 sendBroadcast(intent, permission);
4546 SharedPreferences.Editor editor = mSharedPrefs.edit();
4547 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4548 editor.apply();
4549 }
4550 }
4551
Winson Chunga0b7e862013-09-05 16:03:15 -07004552 public boolean isAllAppsButtonRank(int rank) {
4553 if (mHotseat != null) {
4554 return mHotseat.isAllAppsButtonRank(rank);
4555 }
4556 return false;
4557 }
4558
Winson Chunga2413752012-04-03 14:22:34 -07004559 private boolean canRunNewAppsAnimation() {
4560 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4561 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4562 }
4563
Winson Chungc9168342013-06-26 14:54:55 -07004564 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4565 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4566 PropertyValuesHolder.ofFloat("alpha", 1f),
4567 PropertyValuesHolder.ofFloat("scaleX", 1f),
4568 PropertyValuesHolder.ofFloat("scaleY", 1f));
4569 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4570 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4571 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4572 return bounceAnim;
4573 }
4574
Adam Cohen27772732013-11-11 14:00:56 +00004575 public boolean useVerticalBarLayout() {
4576 return LauncherAppState.getInstance().getDynamicGrid().
4577 getDeviceProfile().isVerticalBarLayout();
4578 }
4579
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004580 protected Rect getSearchBarBounds() {
4581 return LauncherAppState.getInstance().getDynamicGrid().
4582 getDeviceProfile().getSearchBarBounds();
4583 }
4584
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004585 public void bindSearchablesChanged() {
Sunny Goyal594d76d2014-11-06 10:12:54 -08004586 if (mSearchDropTargetBar == null) {
4587 return;
Winson Chungadf0c182012-08-23 14:59:07 -07004588 }
Sunny Goyal594d76d2014-11-06 10:12:54 -08004589 if (mQsb != null) {
4590 mSearchDropTargetBar.removeView(mQsb);
4591 mQsb = null;
4592 }
4593 mSearchDropTargetBar.setQsbSearchBar(getQsbBar());
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004594 }
4595
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004596 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004597 * Add the icons for all apps.
4598 *
4599 * Implementation of the method from LauncherModel.Callbacks.
4600 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004601 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004602 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004603 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4604 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4605 getHotseat().addAllAppsFolder(mIconCache, apps,
4606 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4607 }
4608 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004609 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004610 if (mAppsCustomizeContent != null) {
4611 mAppsCustomizeContent.onPackagesUpdated(
4612 LauncherModel.getSortedWidgetsAndShortcuts(this));
4613 }
Winson Chungc58497e2013-09-03 17:48:37 -07004614 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004615 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004616 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004617 mAppsCustomizeContent.onPackagesUpdated(
4618 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004619 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004620 }
Adam Cohen9211d422014-10-07 18:14:53 -07004621 if (mLauncherCallbacks != null) {
4622 mLauncherCallbacks.bindAllApplications(apps);
4623 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004624 }
4625
4626 /**
4627 * A package was updated.
4628 *
4629 * Implementation of the method from LauncherModel.Callbacks.
4630 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004631 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004632 Runnable r = new Runnable() {
4633 public void run() {
4634 bindAppsUpdated(apps);
4635 }
4636 };
4637 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004638 return;
4639 }
4640
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004641 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004642 mAppsCustomizeContent != null) {
4643 mAppsCustomizeContent.updateApps(apps);
4644 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004645 }
4646
Sunny Goyal4390ace2014-10-13 11:33:11 -07004647 @Override
4648 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4649 Runnable r = new Runnable() {
4650 public void run() {
4651 bindWidgetsRestored(widgets);
4652 }
4653 };
4654 if (waitUntilResume(r)) {
4655 return;
4656 }
4657 mWorkspace.widgetsRestored(widgets);
4658 }
4659
Joe Onorato9c1289c2009-08-17 11:03:03 -04004660 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004661 * Some shortcuts were updated in the background.
4662 *
4663 * Implementation of the method from LauncherModel.Callbacks.
4664 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004665 @Override
4666 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4667 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004668 Runnable r = new Runnable() {
4669 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004670 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004671 }
4672 };
4673 if (waitUntilResume(r)) {
4674 return;
4675 }
4676
Sunny Goyal4390ace2014-10-13 11:33:11 -07004677 if (!updated.isEmpty()) {
4678 mWorkspace.updateShortcuts(updated);
4679 }
4680
4681 if (!removed.isEmpty()) {
4682 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4683 for (ShortcutInfo si : removed) {
4684 removedComponents.add(si.getTargetComponent());
4685 }
4686 mWorkspace.removeItemsByComponentName(removedComponents, user);
4687 // Notify the drag controller
4688 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004689 }
4690 }
4691
4692 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004693 * Update the state of a package, typically related to install state.
4694 *
4695 * Implementation of the method from LauncherModel.Callbacks.
4696 */
Sunny Goyale755d462014-07-22 13:48:29 -07004697 @Override
4698 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004699 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004700 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004701 }
4702 }
4703
4704 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004705 * Update the label and icon of all the icons in a package
4706 *
4707 * Implementation of the method from LauncherModel.Callbacks.
4708 */
4709 @Override
4710 public void updatePackageBadge(String packageName) {
4711 if (mWorkspace != null) {
4712 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4713 }
4714 }
4715
4716 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004717 * A package was uninstalled. We take both the super set of packageNames
4718 * in addition to specific applications to remove, the reason being that
4719 * this can be called when a package is updated as well. In that scenario,
4720 * we only remove specific components from the workspace, where as
4721 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004722 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004723 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004724 * Implementation of the method from LauncherModel.Callbacks.
4725 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004726 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004727 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004728 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004729 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004730 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004731 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004732 }
Winson Chungd64d1762013-08-20 14:37:16 -07004733 };
4734 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004735 return;
4736 }
4737
Sunny Goyal1a745e82014-10-02 15:58:31 -07004738 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004739 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4740 for (AppInfo info : appInfos) {
4741 removedComponents.add(info.componentName);
4742 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07004743 if (!packageNames.isEmpty()) {
4744 mWorkspace.removeItemsByPackageName(packageNames, user);
4745 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004746 if (!removedComponents.isEmpty()) {
4747 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07004748 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07004749 // Notify the drag controller
4750 mDragController.onAppsRemoved(packageNames, removedComponents);
4751
Sunny Goyal1a745e82014-10-02 15:58:31 -07004752 } else {
4753 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004754 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004755
Winson Chungdf95eb12013-10-16 14:57:07 -07004756 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004757 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004758 mAppsCustomizeContent != null) {
4759 mAppsCustomizeContent.removeApps(appInfos);
4760 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004761 }
Joe Onoratobe386092009-11-17 17:32:16 -08004762
4763 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004764 * A number of packages were updated.
4765 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004766 private ArrayList<Object> mWidgetsAndShortcuts;
4767 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004768 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004769 bindPackagesUpdated(mWidgetsAndShortcuts);
4770 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004771 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004772 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004773 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4774 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4775 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004776 return;
4777 }
4778
Winson Chung64359a52013-07-08 17:17:08 -07004779 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004780 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004781 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004782 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004783 }
4784
Winson Chung400438b2011-01-16 17:53:48 -08004785 private int mapConfigurationOriActivityInfoOri(int configOri) {
4786 final Display d = getWindowManager().getDefaultDisplay();
4787 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4788 switch (d.getRotation()) {
4789 case Surface.ROTATION_0:
4790 case Surface.ROTATION_180:
4791 // We are currently in the same basic orientation as the natural orientation
4792 naturalOri = configOri;
4793 break;
4794 case Surface.ROTATION_90:
4795 case Surface.ROTATION_270:
4796 // We are currently in the other basic orientation to the natural orientation
4797 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4798 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4799 break;
4800 }
4801
4802 int[] oriMap = {
4803 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4804 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4805 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4806 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4807 };
4808 // Since the map starts at portrait, we need to offset if this device's natural orientation
4809 // is landscape.
4810 int indexOffset = 0;
4811 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
4812 indexOffset = 1;
4813 }
4814 return oriMap[(d.getRotation() + indexOffset) % 4];
4815 }
Adam Cohen446e9402011-09-15 18:21:21 -07004816
Winson Chung4b919f82012-05-01 10:44:08 -07004817 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004818 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004819 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
4820 .getConfiguration().orientation));
4821 }
4822 }
4823 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08004824 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07004825 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07004826 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07004827 } else {
4828 mHandler.postDelayed(new Runnable() {
4829 public void run() {
4830 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
4831 }
4832 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07004833 }
Winson Chung4b919f82012-05-01 10:44:08 -07004834 }
Winson Chung400438b2011-01-16 17:53:48 -08004835 }
4836
Winson Chunge43a1e72014-01-15 10:33:02 -08004837 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07004838 if (mLauncherCallbacks != null) {
4839 return mLauncherCallbacks.isLauncherPreinstalled();
4840 }
Winson Chunge43a1e72014-01-15 10:33:02 -08004841 PackageManager pm = getPackageManager();
4842 try {
4843 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
4844 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
4845 return true;
4846 } else {
4847 return false;
4848 }
4849 } catch (NameNotFoundException e) {
4850 e.printStackTrace();
4851 return false;
4852 }
4853 }
4854
Adam Cohenea90f832014-05-21 15:03:34 -07004855 /**
4856 * This method indicates whether or not we should suggest default wallpaper dimensions
4857 * when our wallpaper cropper was not yet used to set a wallpaper.
4858 */
4859 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07004860 if (mLauncherCallbacks != null) {
4861 return mLauncherCallbacks.overrideWallpaperDimensions();
4862 }
Adam Cohenea90f832014-05-21 15:03:34 -07004863 return true;
4864 }
4865
Adam Cohen432609a2014-03-13 17:03:22 -07004866 /**
4867 * To be overridden by subclasses to indicate that there is an activity to launch
4868 * before showing the standard launcher experience.
4869 */
4870 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004871 if (mLauncherCallbacks != null) {
4872 return mLauncherCallbacks.hasFirstRunActivity();
4873 }
Adam Cohen432609a2014-03-13 17:03:22 -07004874 return false;
4875 }
4876
4877 /**
4878 * To be overridden by subclasses to launch any first run activity
4879 */
4880 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07004881 if (mLauncherCallbacks != null) {
4882 return mLauncherCallbacks.getFirstRunActivity();
4883 }
Adam Cohen432609a2014-03-13 17:03:22 -07004884 return null;
4885 }
4886
Winson Chunga6945242014-01-08 14:04:34 -08004887 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004888 return !ActivityManager.isRunningInTestHarness() &&
4889 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08004890 }
4891
Adam Cohen4a9423d2014-03-28 14:22:31 -07004892 protected boolean hasRunFirstRunActivity() {
4893 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
4894 }
4895
Adam Cohen432609a2014-03-13 17:03:22 -07004896 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08004897 if (shouldRunFirstRunActivity() &&
4898 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08004899 Intent firstRunIntent = getFirstRunActivity();
4900 if (firstRunIntent != null) {
4901 startActivity(firstRunIntent);
4902 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07004903 return true;
Winson Chunga6945242014-01-08 14:04:34 -08004904 }
4905 }
Adam Cohen432609a2014-03-13 17:03:22 -07004906 return false;
Winson Chunga6945242014-01-08 14:04:34 -08004907 }
4908
4909 private void markFirstRunActivityShown() {
4910 SharedPreferences.Editor editor = mSharedPrefs.edit();
4911 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
4912 editor.apply();
4913 }
4914
Adam Cohen432609a2014-03-13 17:03:22 -07004915 /**
4916 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
4917 * screen that must be displayed and dismissed.
4918 */
4919 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004920 if (mLauncherCallbacks != null) {
4921 return mLauncherCallbacks.hasDismissableIntroScreen();
4922 }
Adam Cohen432609a2014-03-13 17:03:22 -07004923 return false;
4924 }
4925
4926 /**
4927 * Full screen intro screen to be shown and dismissed before the launcher can be used.
4928 */
4929 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07004930 if (mLauncherCallbacks != null) {
4931 return mLauncherCallbacks.getIntroScreen();
4932 }
Adam Cohen432609a2014-03-13 17:03:22 -07004933 return null;
4934 }
4935
4936 /**
4937 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
4938 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
4939 */
4940 private boolean shouldShowIntroScreen() {
4941 return hasDismissableIntroScreen() &&
4942 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
4943 }
4944
4945 protected void showIntroScreen() {
4946 View introScreen = getIntroScreen();
4947 changeWallpaperVisiblity(false);
4948 if (introScreen != null) {
4949 mDragLayer.showOverlayView(introScreen);
4950 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07004951 if (mLauncherOverlayContainer != null) {
4952 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
4953 }
Adam Cohen432609a2014-03-13 17:03:22 -07004954 }
4955
4956 public void dismissIntroScreen() {
4957 markIntroScreenDismissed();
4958 if (showFirstRunActivity()) {
4959 // We delay hiding the intro view until the first run activity is showing. This
4960 // avoids a blip.
4961 mWorkspace.postDelayed(new Runnable() {
4962 @Override
4963 public void run() {
4964 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004965 if (mLauncherOverlayContainer != null) {
4966 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4967 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004968 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004969 }
4970 }, ACTIVITY_START_DELAY);
4971 } else {
4972 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07004973 if (mLauncherOverlayContainer != null) {
4974 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
4975 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07004976 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07004977 }
4978 changeWallpaperVisiblity(true);
4979 }
4980
4981 private void markIntroScreenDismissed() {
4982 SharedPreferences.Editor editor = mSharedPrefs.edit();
4983 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
4984 editor.apply();
4985 }
4986
Sunny Goyal88d60f12014-09-08 03:47:29 -07004987 private void showFirstRunClings() {
4988 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
4989 // on the device, then we always show the first run cling experience (or if there is no
4990 // launcher2). Otherwise, we prompt the user upon started for migration
4991 LauncherClings launcherClings = new LauncherClings(this);
4992 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
4993 if (mModel.canMigrateFromOldLauncherDb(this)) {
4994 launcherClings.showMigrationCling();
4995 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07004996 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07004997 }
4998 }
4999 }
5000
Winson Chunga6945242014-01-08 14:04:34 -08005001 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005002 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5003 if (mHotseat != null) mHotseat.setAlpha(1f);
5004 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5005 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005006 }
5007
5008 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005009 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5010 if (mHotseat != null) mHotseat.setAlpha(0f);
5011 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5012 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005013 }
5014
Mathew Inwood72fbec12013-11-19 15:45:07 +00005015 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005016 // Called from search suggestion, not supported in other profiles.
5017 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5018 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5019 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5020 myUser);
5021 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005022 return null;
5023 }
Kenny Guyed131872014-04-30 03:02:21 +01005024 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005025 }
5026
5027 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5028 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005029 // Called from search suggestion, not supported in other profiles.
5030 return createShortcutDragInfo(shortcutIntent, caption, icon,
5031 UserHandleCompat.myUserHandle());
5032 }
5033
5034 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5035 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005036 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005037 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005038 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005039 }
5040
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005041 protected void moveWorkspaceToDefaultScreen() {
5042 mWorkspace.moveToDefaultScreen(false);
5043 }
5044
Mathew Inwood72fbec12013-11-19 15:45:07 +00005045 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5046 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005047 mWorkspace.onExternalDragStartedWithItem(dragView);
5048 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005049 }
5050
Anjali Koppalf5d29132014-02-28 13:33:27 -08005051 @Override
5052 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005053 if (mLauncherCallbacks != null) {
5054 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5055 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005056 }
5057
Winson Chung80baf5a2010-08-09 16:03:15 -07005058 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005059 * Prints out out state for debugging.
5060 */
5061 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005062 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005063 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005064 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5065 Log.d(TAG, "mRestoring=" + mRestoring);
5066 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5067 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005068 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005069 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005070
Winson Chung785d2eb2011-04-14 16:08:02 -07005071 if (mAppsCustomizeContent != null) {
5072 mAppsCustomizeContent.dumpState();
5073 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005074 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005075 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005076
5077 @Override
5078 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5079 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005080 synchronized (sDumpLogs) {
5081 writer.println(" ");
5082 writer.println("Debug logs: ");
5083 for (int i = 0; i < sDumpLogs.size(); i++) {
5084 writer.println(" " + sDumpLogs.get(i));
5085 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005086 }
Adam Cohen9211d422014-10-07 18:14:53 -07005087 if (mLauncherCallbacks != null) {
5088 mLauncherCallbacks.dump(prefix, fd, writer, args);
5089 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005090 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005091
5092 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005093 if (DEBUG_DUMP_LOG) {
5094 synchronized (sDumpLogs) {
5095 Log.d(TAG, "");
5096 Log.d(TAG, "*********************");
5097 Log.d(TAG, "Launcher debug logs: ");
5098 for (int i = 0; i < sDumpLogs.size(); i++) {
5099 Log.d(TAG, " " + sDumpLogs.get(i));
5100 }
5101 Log.d(TAG, "*********************");
5102 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005103 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005104 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005105 }
5106
5107 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005108 addDumpLog(tag, log, null, debugLog);
5109 }
5110
5111 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005112 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005113 if (e != null) {
5114 Log.d(tag, log, e);
5115 } else {
5116 Log.d(tag, log);
5117 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005118 }
Winson Chungede41292013-09-19 16:27:36 -07005119 if (DEBUG_DUMP_LOG) {
5120 sDateStamp.setTime(System.currentTimeMillis());
5121 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005122 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5123 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005124 }
Winson Chungede41292013-09-19 16:27:36 -07005125 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005126 }
5127
Winson Chungede41292013-09-19 16:27:36 -07005128 public void dumpLogsToLocalData() {
5129 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005130 new AsyncTask<Void, Void, Void>() {
5131 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005132 boolean success = false;
5133 sDateStamp.setTime(sRunStart);
5134 String FILENAME = sDateStamp.getMonth() + "-"
5135 + sDateStamp.getDay() + "_"
5136 + sDateStamp.getHours() + "-"
5137 + sDateStamp.getMinutes() + "_"
5138 + sDateStamp.getSeconds() + ".txt";
5139
5140 FileOutputStream fos = null;
5141 File outFile = null;
5142 try {
5143 outFile = new File(getFilesDir(), FILENAME);
5144 outFile.createNewFile();
5145 fos = new FileOutputStream(outFile);
5146 } catch (Exception e) {
5147 e.printStackTrace();
5148 }
5149 if (fos != null) {
5150 PrintWriter writer = new PrintWriter(fos);
5151
5152 writer.println(" ");
5153 writer.println("Debug logs: ");
5154 synchronized (sDumpLogs) {
5155 for (int i = 0; i < sDumpLogs.size(); i++) {
5156 writer.println(" " + sDumpLogs.get(i));
5157 }
5158 }
5159 writer.close();
5160 }
5161 try {
5162 if (fos != null) {
5163 fos.close();
5164 success = true;
5165 }
5166 } catch (IOException e) {
5167 e.printStackTrace();
5168 }
Michael Jurka43467462013-11-14 12:03:58 +01005169 return null;
Winson Chungede41292013-09-19 16:27:36 -07005170 }
Michael Jurka43467462013-11-14 12:03:58 +01005171 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005172 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005173 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005174}
Michael Jurka2763be32011-02-24 11:19:57 -08005175
Michael Jurkaabded662011-03-04 12:06:57 -08005176interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005177 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005178 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005179 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005180 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005181 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005182}
Dan Sandlerd5024042014-01-09 15:01:33 -05005183
5184interface DebugIntents {
5185 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5186 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005187}