blob: 444a4bc1b0d40e5e6932a63f154b54cc89f79a3e [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 Cohen268c4752012-06-06 17:47:33 -070053import android.graphics.Bitmap;
54import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070055import android.graphics.Color;
Winson Chung5f8afe62013-08-12 16:19:28 -070056import android.graphics.Point;
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;
Amith Yamasani6d7fe502010-11-16 09:05:07 -080069import android.speech.RecognizerIntent;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080070import android.text.Selection;
71import android.text.SpannableStringBuilder;
Michael Jurkaa33411c2012-06-14 16:18:21 -070072import android.text.TextUtils;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080073import android.text.method.TextKeyListener;
Winson Chung892c74d2013-08-22 16:15:50 -070074import android.util.DisplayMetrics;
Joe Onorato7c312c12009-08-13 21:36:53 -070075import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070076import android.view.Display;
77import android.view.Gravity;
78import android.view.HapticFeedbackConstants;
79import android.view.KeyEvent;
80import android.view.LayoutInflater;
81import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070082import android.view.MotionEvent;
83import android.view.Surface;
84import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070085import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080086import android.view.View.OnLongClickListener;
Adam Cohen2854d252014-08-27 16:04:07 -070087import android.view.ViewAnimationUtils;
Adam Cohen96d30a12013-07-16 18:13:21 -070088import android.view.ViewGroup;
89import android.view.ViewTreeObserver;
Sunny Goyal651077b2014-06-30 14:15:31 -070090import android.view.Window;
Adam Cohen96d30a12013-07-16 18:13:21 -070091import android.view.WindowManager;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -080092import android.view.accessibility.AccessibilityEvent;
Adam Cohen9bfdb762014-07-21 17:44:06 -070093import android.view.animation.AccelerateInterpolator;
Adam Cohenf16e5712011-01-13 13:31:45 -080094import android.view.animation.DecelerateInterpolator;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080095import android.view.inputmethod.InputMethodManager;
Adam Cohen96d30a12013-07-16 18:13:21 -070096import android.widget.Advanceable;
97import android.widget.FrameLayout;
98import android.widget.ImageView;
99import android.widget.TextView;
100import android.widget.Toast;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700101
Sunny Goyal651077b2014-06-30 14:15:31 -0700102import com.android.launcher3.DropTarget.DragObject;
103import com.android.launcher3.PagedView.PageSwitchListener;
Sunny Goyalffe83f12014-08-14 17:39:34 -0700104import com.android.launcher3.compat.AppWidgetManagerCompat;
Kenny Guyed131872014-04-30 03:02:21 +0100105import com.android.launcher3.compat.LauncherActivityInfoCompat;
106import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyale755d462014-07-22 13:48:29 -0700107import com.android.launcher3.compat.PackageInstallerCompat;
108import com.android.launcher3.compat.PackageInstallerCompat.PackageInstallInfo;
Kenny Guyed131872014-04-30 03:02:21 +0100109import com.android.launcher3.compat.UserHandleCompat;
110import com.android.launcher3.compat.UserManagerCompat;
Adam Cohen6c5891a2014-07-09 23:53:15 -0700111
Adam Cohenc0dcf592011-06-01 15:30:43 -0700112import java.io.DataInputStream;
113import java.io.DataOutputStream;
Adam Cohen4caf2982013-08-20 18:54:31 -0700114import java.io.File;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700115import java.io.FileDescriptor;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700116import java.io.FileNotFoundException;
Adam Cohen4caf2982013-08-20 18:54:31 -0700117import java.io.FileOutputStream;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700118import java.io.IOException;
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700119import java.io.PrintWriter;
Adam Cohen0b395352014-06-09 22:54:36 +0000120import java.lang.reflect.Field;
121import java.lang.reflect.InvocationTargetException;
122import java.lang.reflect.Method;
Adam Cohen4caf2982013-08-20 18:54:31 -0700123import java.text.DateFormat;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700124import java.util.ArrayList;
Winson Chungf0c6ae02012-03-21 16:10:31 -0700125import java.util.Collection;
Adam Cohen4caf2982013-08-20 18:54:31 -0700126import java.util.Date;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700127import java.util.HashMap;
Michael Jurkad7c28052012-04-27 15:43:36 -0700128import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000129import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700130
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800131/**
132 * Default launcher application.
133 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100134public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700135 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700136 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800137 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700138 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800139
Daniel Sandlerf061f822013-06-27 13:59:36 -0400140 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400141 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700142 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400143 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700144 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700145
Dan Sandlerd5024042014-01-09 15:01:33 -0500146 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
147
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800148 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700149 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800150 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700151 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700152 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800153
Michael Jurka8b805b12012-04-18 14:23:14 -0700154 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700155 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700156
Mathew Inwood876a8462013-06-14 14:12:41 +0100157 /**
158 * IntentStarter uses request codes starting with this. This must be greater than all activity
159 * request codes used internally.
160 */
161 protected static final int REQUEST_LAST = 100;
162
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800163 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
164
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800165 static final int SCREEN_COUNT = 5;
166 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800167
Michael Jurka0a457bf2012-11-19 14:05:05 -0800168 // To turn on these properties, type
169 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
170 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
171 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800172 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800173
Winson Chung2672ff92012-05-04 16:22:30 -0700174 // The Intent extra that defines whether to ignore the launch animation
175 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400176 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700177
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800178 // Type: int
179 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700180 // Type: int
181 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800182 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700183 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
184 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800185 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
186 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700187 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800188 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700189 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800190 // Type: boolean
191 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
192 // Type: long
193 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700194 // Type: int
195 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
196 // Type: int
197 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
198 // Type: parcelable
199 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000200 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800201 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000202 // Type: int[]
203 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800204
Adam Cohen432609a2014-03-13 17:03:22 -0700205 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800206 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
207
Adam Cohen3ed316a2014-07-23 18:21:20 -0700208 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
209 static final String ACTION_FIRST_LOAD_COMPLETE =
210 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
211
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100212 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700213 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100214 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700215 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100216 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700217
Adam Cohen39a06042013-07-19 14:30:12 -0700218 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700219 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700220
Winson Chunga6945242014-01-08 14:04:34 -0800221 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
222
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700223 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700224 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700225 private State mState = State.WORKSPACE;
226 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700227
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700228 private boolean mIsSafeModeEnabled;
229
Joe Onorato9c1289c2009-08-17 11:03:03 -0400230 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700231 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
232 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700233 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800234
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800235 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800236 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800237
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000238 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
239 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
240
Winson Chunga2413752012-04-03 14:22:34 -0700241 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700242 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
243 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700244 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700245 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700246
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800247 private final BroadcastReceiver mCloseSystemDialogsReceiver
248 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800249 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
250
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800251 private LayoutInflater mInflater;
252
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800253 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700254 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800255 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800256 private DragLayer mDragLayer;
257 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700258 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700259
Sunny Goyalffe83f12014-08-14 17:39:34 -0700260 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700261 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700262
Michael Jurkac9d95c52011-08-29 14:03:34 -0700263 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700264 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800265 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700266
Michael Jurka0280c3b2010-09-17 15:00:07 -0700267 private int[] mTmpAddItemCellCoordinates = new int[2];
268
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800269 private FolderInfo mFolderInfo;
270
Winson Chung3d503fb2011-07-13 17:25:49 -0700271 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800272 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700273
Michael Jurka838a4ca2011-02-07 13:33:06 -0800274 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700275
Winson Chungc51db6a2011-10-05 11:44:49 -0700276 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700277 private AppsCustomizeTabHost mAppsCustomizeTabHost;
278 private AppsCustomizePagedView mAppsCustomizeContent;
279 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800280 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800281
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800282 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700283 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
284 // scroll issues (because the workspace may not have been measured yet) and extra work.
285 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
286 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287
288 private SpannableStringBuilder mDefaultKeySsb = null;
289
Joe Onorato9c1289c2009-08-17 11:03:03 -0400290 private boolean mWorkspaceLoading = true;
291
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800292 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800293 private boolean mRestoring;
294 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700295 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800296
Michael Jurka1e2f4652013-07-08 18:03:46 -0700297 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700298 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
299
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300 private Bundle mSavedInstanceState;
301
Joe Onorato9c1289c2009-08-17 11:03:03 -0400302 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800303 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800304 private boolean mUserPresent = true;
305 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700306 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800307 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400308
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700309 private static LocaleConfiguration sLocaleConfiguration = null;
310
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700311 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700312
Adam Cohen61f560d2013-09-30 15:58:20 -0700313 private View.OnTouchListener mHapticFeedbackTouchListener;
314
Adam Cohended9f8d2010-11-03 13:25:16 -0700315 // Related to the auto-advancing of widgets
316 private final int ADVANCE_MSG = 1;
317 private final int mAdvanceInterval = 20000;
318 private final int mAdvanceStagger = 250;
319 private long mAutoAdvanceSentTime;
320 private long mAutoAdvanceTimeLeft = -1;
321 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
322 new HashMap<View, AppWidgetProviderInfo>();
323
Winson Chung400438b2011-01-16 17:53:48 -0800324 // Determines how long to wait after a rotation before restoring the screen orientation to
325 // match the sensor state.
326 private final int mRestoreScreenOrientationDelay = 500;
327
Michael Jurka4ef207b2010-11-29 17:05:45 -0800328 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700329 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
330 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
Winson Chungd64a6662013-09-30 11:06:59 -0700331
Craig Mautner360310b2012-10-26 15:13:08 -0700332 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700333
Adam Cohen1462de32012-07-24 22:34:36 -0700334 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700335 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700336
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700337 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700338 static Date sDateStamp = new Date();
339 static DateFormat sDateFormat =
340 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
341 static long sRunStart = System.currentTimeMillis();
342 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700343
Winson Chung46353de2012-02-16 14:05:10 -0800344 // We only want to get the SharedPreferences once since it does an FS stat each time we get
345 // it from the context.
346 private SharedPreferences mSharedPrefs;
347
Adam Cohene25af792013-06-06 23:08:25 -0700348 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
349
Winson Chungf0c6ae02012-03-21 16:10:31 -0700350 // Holds the page that we need to animate to, and the icon views that we need to animate up
351 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700352 private ImageView mFolderIconImageView;
353 private Bitmap mFolderIconBitmap;
354 private Canvas mFolderIconCanvas;
355 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700356
Michael Jurkaddd62e92011-02-16 17:49:14 -0800357 private BubbleTextView mWaitingForResume;
358
Michael Jurkac1f5d262011-09-30 19:32:27 -0700359 private Runnable mBuildLayersRunnable = new Runnable() {
360 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700361 if (mWorkspace != null) {
362 mWorkspace.buildPageHardwareLayers();
363 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700364 }
365 };
366
Adam Cohendb364c32014-05-20 14:23:40 -0700367 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800368
Michael Jurka7267fa52013-09-26 15:29:57 -0700369 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800370
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800371 private static class PendingAddArguments {
372 int requestCode;
373 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700374 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700375 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800376 int cellX;
377 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700378 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800379 }
380
Daniel Sandlerff02d492013-08-05 02:12:05 -0400381 private Stats mStats;
382
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700383 FocusIndicatorView mFocusHandler;
384
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800385 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800386 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700387 }
388
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800389 @Override
390 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700391 if (DEBUG_STRICT_MODE) {
392 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
393 .detectDiskReads()
394 .detectDiskWrites()
395 .detectNetwork() // or .detectAll() for all detectable problems
396 .penaltyLog()
397 .build());
398 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
399 .detectLeakedSqlLiteObjects()
400 .detectLeakedClosableObjects()
401 .penaltyLog()
402 .penaltyDeath()
403 .build());
404 }
405
Adam Cohen9211d422014-10-07 18:14:53 -0700406 if (mLauncherCallbacks != null) {
407 mLauncherCallbacks.preOnCreate();
408 }
409
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800410 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400411
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400412 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400413 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700414 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700415 // Determine the dynamic grid properties
416 Point smallestSize = new Point();
417 Point largestSize = new Point();
418 Point realSize = new Point();
419 Display display = getWindowManager().getDefaultDisplay();
420 display.getCurrentSizeRange(smallestSize, largestSize);
421 display.getRealSize(realSize);
Winson Chung892c74d2013-08-22 16:15:50 -0700422 DisplayMetrics dm = new DisplayMetrics();
423 display.getMetrics(dm);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700424
Winson Chung5f8afe62013-08-12 16:19:28 -0700425 // Lazy-initialize the dynamic grid
426 DeviceProfile grid = app.initDynamicGrid(this,
427 Math.min(smallestSize.x, smallestSize.y),
428 Math.min(largestSize.x, largestSize.y),
Winson Chung892c74d2013-08-22 16:15:50 -0700429 realSize.x, realSize.y,
430 dm.widthPixels, dm.heightPixels);
Winson Chung5f8afe62013-08-12 16:19:28 -0700431
432 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400433 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700434 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700435 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800436 mModel = app.setLauncher(this);
437 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700438 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400439 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800440 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700441
Daniel Sandlerff02d492013-08-05 02:12:05 -0400442 mStats = new Stats(this);
443
Sunny Goyalffe83f12014-08-14 17:39:34 -0700444 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700445
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700446 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
447 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700448
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700449 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
450 // this also ensures that any synchronous binding below doesn't re-trigger another
451 // LauncherModel load.
452 mPaused = false;
453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200455 android.os.Debug.startMethodTracing(
456 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800457 }
458
459 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800460 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700461
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800462 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100463 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800464
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800465 registerContentObservers();
466
Joe Onorato7c312c12009-08-13 21:36:53 -0700467 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700468
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800469 mSavedState = savedInstanceState;
470 restoreState(mSavedState);
471
472 if (PROFILE_STARTUP) {
473 android.os.Debug.stopMethodTracing();
474 }
475
476 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700477 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700478 // If the user leaves launcher, then we should just load items asynchronously when
479 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500480 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700481 } else {
482 // We only load the page synchronously if the user rotates (or triggers a
483 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800484 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700485 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800486 }
487
488 // For handling default keys
489 mDefaultKeySsb = new SpannableStringBuilder();
490 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800491
492 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
493 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800494
Adam Cohenf9426d52012-06-04 17:26:21 -0700495 updateGlobalIcons();
496
497 // On large interfaces, we want the screen to auto-rotate based on the current orientation
498 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700499
Adam Cohen432609a2014-03-13 17:03:22 -0700500 if (shouldShowIntroScreen()) {
501 showIntroScreen();
502 } else {
503 showFirstRunActivity();
Sunny Goyal88d60f12014-09-08 03:47:29 -0700504 showFirstRunClings();
Winson Chunga6945242014-01-08 14:04:34 -0800505 }
Adam Cohen9211d422014-10-07 18:14:53 -0700506
507 if (mLauncherCallbacks != null) {
508 mLauncherCallbacks.onCreate(savedInstanceState);
509 }
510 }
511
512 private LauncherCallbacks mLauncherCallbacks;
513
514 public void onPostCreate(Bundle savedInstanceState) {
515 super.onPostCreate(savedInstanceState);
516 if (mLauncherCallbacks != null) {
517 mLauncherCallbacks.onPostCreate(savedInstanceState);
518 }
519 }
520
521 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
522 mLauncherCallbacks = callbacks;
523 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700524 }
525
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700526 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700527 public void onLauncherProviderChange() {
528 if (mLauncherCallbacks != null) {
529 mLauncherCallbacks.onLauncherProviderChange();
530 }
531 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700532
Adam Cohen9211d422014-10-07 18:14:53 -0700533 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700534 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700535 if (mLauncherCallbacks != null) {
536 return mLauncherCallbacks.hasCustomContentToLeft();
537 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700538 return false;
539 }
540
Dave Hawkeya8881582013-09-17 15:55:33 -0600541 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500542 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600543 * {@link #addToCustomContentPage}. This will only be invoked if
544 * {@link #hasCustomContentToLeft()} is {@code true}.
545 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500546 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700547 if (mLauncherCallbacks != null) {
548 mLauncherCallbacks.populateCustomContentContainer();
549 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600550 }
551
552 /**
553 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
554 * ensure the custom content page is added or removed if necessary.
555 */
556 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600557 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600558 // Not bound yet, wait for bindScreens to be called.
559 return;
560 }
561
562 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
563 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500564 mWorkspace.createCustomContentContainer();
565 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600566 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
567 mWorkspace.removeCustomContentPage();
568 }
569 }
570
Adam Cohenf9426d52012-06-04 17:26:21 -0700571 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700572 boolean searchVisible = false;
573 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800574 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700575 int coi = getCurrentOrientationIndexForGlobalIcons();
Adam Cohena00673c2014-08-14 12:57:28 -0700576 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700577 searchVisible = updateGlobalSearchIcon();
578 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700579 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700580 if (sGlobalSearchIcon[coi] != null) {
581 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700582 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700583 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700584 if (sVoiceSearchIcon[coi] != null) {
585 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700586 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700587 }
Winson Chungadf0c182012-08-23 14:59:07 -0700588 if (mSearchDropTargetBar != null) {
589 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
590 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800591 }
Romain Guycbb89e42009-06-08 15:52:54 -0700592
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800593 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700594 if (sLocaleConfiguration == null) {
595 new AsyncTask<Void, Void, LocaleConfiguration>() {
596 @Override
597 protected LocaleConfiguration doInBackground(Void... unused) {
598 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
599 readConfiguration(Launcher.this, localeConfiguration);
600 return localeConfiguration;
601 }
602
603 @Override
604 protected void onPostExecute(LocaleConfiguration result) {
605 sLocaleConfiguration = result;
606 checkForLocaleChange(); // recursive, but now with a locale configuration
607 }
608 }.execute();
609 return;
610 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700611
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800612 final Configuration configuration = getResources().getConfiguration();
613
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700614 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800615 final String locale = configuration.locale.toString();
616
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700617 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800618 final int mcc = configuration.mcc;
619
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700620 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800621 final int mnc = configuration.mnc;
622
Romain Guy84f296c2009-11-04 15:00:44 -0800623 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800624
Romain Guy84f296c2009-11-04 15:00:44 -0800625 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700626 sLocaleConfiguration.locale = locale;
627 sLocaleConfiguration.mcc = mcc;
628 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700629
Joe Onorato0589f0f2010-02-08 13:44:00 -0800630 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700631
632 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100633 new AsyncTask<Void, Void, Void>() {
634 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700635 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100636 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700637 }
Michael Jurka43467462013-11-14 12:03:58 +0100638 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700639 }
640 }
641
642 private static class LocaleConfiguration {
643 public String locale;
644 public int mcc = -1;
645 public int mnc = -1;
646 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700647
Romain Guy98d01652009-06-30 16:21:04 -0700648 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
649 DataInputStream in = null;
650 try {
Helena Josol28db2802014-10-09 17:04:09 +0100651 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700652 configuration.locale = in.readUTF();
653 configuration.mcc = in.readInt();
654 configuration.mnc = in.readInt();
655 } catch (FileNotFoundException e) {
656 // Ignore
657 } catch (IOException e) {
658 // Ignore
659 } finally {
660 if (in != null) {
661 try {
662 in.close();
663 } catch (IOException e) {
664 // Ignore
665 }
666 }
667 }
668 }
669
670 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
671 DataOutputStream out = null;
672 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100673 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100674 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700675 out.writeUTF(configuration.locale);
676 out.writeInt(configuration.mcc);
677 out.writeInt(configuration.mnc);
678 out.flush();
679 } catch (FileNotFoundException e) {
680 // Ignore
681 } catch (IOException e) {
682 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100683 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700684 } finally {
685 if (out != null) {
686 try {
687 out.close();
688 } catch (IOException e) {
689 // Ignore
690 }
691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 }
693 }
694
Anton Hanssona2f665f2013-09-26 12:18:32 +0100695 public Stats getStats() {
696 return mStats;
697 }
698
Bjorn Bringertc459e522013-06-07 19:36:01 +0100699 public LayoutInflater getInflater() {
700 return mInflater;
701 }
702
Winson Chung36a62fe2012-05-06 18:04:42 -0700703 boolean isDraggingEnabled() {
704 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
705 // that is subsequently removed from the workspace in startBinding().
706 return !mModel.isLoadingWorkspace();
707 }
708
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 static int getScreen() {
710 synchronized (sLock) {
711 return sScreen;
712 }
713 }
714
715 static void setScreen(int screen) {
716 synchronized (sLock) {
717 sScreen = screen;
718 }
719 }
720
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000721 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100722 if (Build.VERSION.SDK_INT >= 17) {
723 return View.generateViewId();
724 } else {
725 // View.generateViewId() is not available. The following fallback logic is a copy
726 // of its implementation.
727 for (;;) {
728 final int result = sNextGeneratedId.get();
729 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
730 int newValue = result + 1;
731 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
732 if (sNextGeneratedId.compareAndSet(result, newValue)) {
733 return result;
734 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000735 }
736 }
737 }
738
739 public int getViewIdForItem(ItemInfo info) {
740 // This cast is safe given the > 2B range for int.
741 int itemId = (int) info.id;
742 if (mItemIdToViewId.containsKey(itemId)) {
743 return mItemIdToViewId.get(itemId);
744 }
745 int viewId = generateViewId();
746 mItemIdToViewId.put(itemId, viewId);
747 return viewId;
748 }
749
750 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700751 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
752 * a configuration step, this allows the proper animations to run after other transitions.
753 */
Adam Cohendb364c32014-05-20 14:23:40 -0700754 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700755 long screenId = args.screenId;
756 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
757 // When the screen id represents an actual screen (as opposed to a rank) we make sure
758 // that the drop page actually exists.
759 screenId = ensurePendingDropLayoutExists(args.screenId);
760 }
Adam Cohendb364c32014-05-20 14:23:40 -0700761
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800762 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800763 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700764 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700765 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700766 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800767 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700768 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700769 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700770 case REQUEST_RECONFIGURE_APPWIDGET:
771 completeRestoreAppWidget(args.appWidgetId);
772 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800773 }
Michael Jurka27614d22012-04-02 06:40:22 -0700774 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
775 // if you turned the screen off and then back while in All Apps, Launcher would not
776 // return to the workspace. Clearing mAddInfo.container here fixes this issue
777 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700778 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800779 }
780
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700782 protected void onActivityResult(
783 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700784 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700785 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700786 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800787 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700788
Adam Cohenad4e15c2013-10-17 16:21:35 -0700789 Runnable exitSpringLoaded = new Runnable() {
790 @Override
791 public void run() {
792 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
793 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
794 }
795 };
796
Michael Jurka8b805b12012-04-18 14:23:14 -0700797 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700798 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700799 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
800 if (resultCode == RESULT_CANCELED) {
801 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700802 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700803 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700804 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700805 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
806 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700807 }
808 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200809 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
810 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
811 mWorkspace.exitOverviewMode(false);
812 }
813 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700814 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200815
Adam Cohened66b2b2012-01-23 17:28:51 -0800816 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
817 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700818
Adam Cohendb364c32014-05-20 14:23:40 -0700819 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800820 // We have special handling for widgets
821 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800822 final int appWidgetId;
823 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
824 : -1;
825 if (widgetId < 0) {
826 appWidgetId = pendingAddWidgetId;
827 } else {
828 appWidgetId = widgetId;
829 }
830
Adam Cohenad4e15c2013-10-17 16:21:35 -0700831 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800832 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700833 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
834 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700835 result = RESULT_CANCELED;
836 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700837 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700838 @Override
839 public void run() {
840 exitSpringLoadedDragModeDelayed(false, 0, null);
841 }
842 };
Adam Cohendb364c32014-05-20 14:23:40 -0700843 if (workspaceLocked) {
844 // No need to remove the empty screen if we're mid-binding, as the
845 // the bind will not add the empty screen.
846 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
847 } else {
848 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
849 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
850 }
Winson Chung5aaab772012-04-27 15:24:02 -0700851 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700852 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700853 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
854 // When the screen id represents an actual screen (as opposed to a rank)
855 // we make sure that the drop page actually exists.
856 mPendingAddInfo.screenId =
857 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
858 }
Adam Cohendb364c32014-05-20 14:23:40 -0700859 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
860
861 dropLayout.setDropPending(true);
862 final Runnable onComplete = new Runnable() {
863 @Override
864 public void run() {
865 completeTwoStageWidgetDrop(resultCode, appWidgetId);
866 dropLayout.setDropPending(false);
867 }
868 };
869 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
870 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
871 } else {
872 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
873 mPendingAddInfo);
874 sPendingAddItem = args;
875 }
Winson Chung5aaab772012-04-27 15:24:02 -0700876 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800877 return;
878 }
879
Sunny Goyalff572272014-07-23 13:58:07 -0700880 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
881 if (resultCode == RESULT_OK) {
882 // Update the widget view.
883 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
884 pendingAddWidgetId, mPendingAddInfo);
885 if (workspaceLocked) {
886 sPendingAddItem = args;
887 } else {
888 completeAdd(args);
889 }
890 }
891 // Leave the widget in the pending state if the user canceled the configure.
892 return;
893 }
894
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 // The pattern used here is that a user PICKs a specific application,
896 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700897
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
899 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700900 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700901 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
902 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800903 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700904 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800905 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700906 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700907 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
908 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 }
Adam Cohen00074722013-10-11 17:46:09 -0700910 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700911 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700912 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800915 }
916
Adam Cohendb364c32014-05-20 14:23:40 -0700917 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
918 appWidgetId, ItemInfo info) {
919 PendingAddArguments args = new PendingAddArguments();
920 args.requestCode = requestCode;
921 args.intent = data;
922 args.container = info.container;
923 args.screenId = info.screenId;
924 args.cellX = info.cellX;
925 args.cellY = info.cellY;
926 args.appWidgetId = appWidgetId;
927 return args;
928 }
929
930 /**
931 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
932 *
933 * @param screenId the screen id to check
934 * @return the new screen, or screenId if it exists
935 */
936 private long ensurePendingDropLayoutExists(long screenId) {
937 CellLayout dropLayout =
938 (CellLayout) mWorkspace.getScreenWithId(screenId);
939 if (dropLayout == null) {
940 // it's possible that the add screen was removed because it was
941 // empty and a re-bind occurred
942 mWorkspace.addExtraEmptyScreen();
943 return mWorkspace.commitExtraEmptyScreen();
944 } else {
945 return screenId;
946 }
947 }
948
Adam Cohened66b2b2012-01-23 17:28:51 -0800949 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700950 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700951 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800952 Runnable onCompleteRunnable = null;
953 int animationType = 0;
954
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700955 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800956 if (resultCode == RESULT_OK) {
957 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
958 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700959 mPendingAddWidgetInfo);
960 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800961 onCompleteRunnable = new Runnable() {
962 @Override
963 public void run() {
964 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700965 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700966 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
967 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800968 }
969 };
970 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800971 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800972 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800973 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700974 if (mDragLayer.getAnimatedView() != null) {
975 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
976 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
977 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700978 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700979 // The animated view may be null in the case of a rotation during widget configuration
980 onCompleteRunnable.run();
981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 }
983
984 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700985 protected void onStop() {
986 super.onStop();
987 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700988
989 if (mLauncherCallbacks != null) {
990 mLauncherCallbacks.onStop();
991 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700992 }
993
994 @Override
995 protected void onStart() {
996 super.onStart();
997 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700998
999 if (mLauncherCallbacks != null) {
1000 mLauncherCallbacks.onStart();
1001 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001002 }
1003
1004 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001006 long startTime = 0;
1007 if (DEBUG_RESUME_TIME) {
1008 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001009 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001010 }
Adam Cohen9211d422014-10-07 18:14:53 -07001011
1012 if (mLauncherCallbacks != null) {
1013 mLauncherCallbacks.preOnResume();
1014 }
1015
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001017
Winson Chung4a2afa32012-07-19 14:53:05 -07001018 // Restore the previous launcher state
1019 if (mOnResumeState == State.WORKSPACE) {
1020 showWorkspace(false);
1021 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -08001022 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001023 }
1024 mOnResumeState = State.NONE;
1025
Craig Mautner360310b2012-10-26 15:13:08 -07001026 // Background was set to gradient in onPause(), restore to black if in all apps.
1027 setWorkspaceBackground(mState == State.WORKSPACE);
1028
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001029 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001030 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001031 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001032 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001033 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001034 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001036 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001037 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1038 // execute them here
1039 long startTimeCallbacks = 0;
1040 if (DEBUG_RESUME_TIME) {
1041 startTimeCallbacks = System.currentTimeMillis();
1042 }
1043
1044 if (mAppsCustomizeContent != null) {
1045 mAppsCustomizeContent.setBulkBind(true);
1046 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001047 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1048 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001049 }
1050 if (mAppsCustomizeContent != null) {
1051 mAppsCustomizeContent.setBulkBind(false);
1052 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001053 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001054 if (DEBUG_RESUME_TIME) {
1055 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1056 (System.currentTimeMillis() - startTimeCallbacks));
1057 }
Michael Jurka447bf842013-05-15 14:52:15 +02001058 }
Michael Jurka54554252013-08-01 12:52:23 +02001059 if (mOnResumeCallbacks.size() > 0) {
1060 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1061 mOnResumeCallbacks.get(i).run();
1062 }
1063 mOnResumeCallbacks.clear();
1064 }
Winson Chunge4e50662012-01-23 14:45:13 -08001065
1066 // Reset the pressed state of icons that were locked in the press state while activities
1067 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001068 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001069 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001070 mWaitingForResume.setStayPressed(false);
1071 }
Winson Chung82963d52013-10-09 11:20:57 -07001072
Adam Cohen06dff352012-06-01 17:17:08 -07001073 // It is possible that widgets can receive updates while launcher is not in the foreground.
1074 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1075 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1076 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001077 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001078
Winson Chung780fe592013-09-26 14:48:44 -07001079 // Process any items that were added while Launcher was away.
1080 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1081
Winson Chung5841efa2013-09-30 18:06:44 -07001082 // Update the voice search button proxy
1083 updateVoiceButtonProxyVisible(false);
1084
Adam Cohenf9426d52012-06-04 17:26:21 -07001085 // Again, as with the above scenario, it's possible that one or more of the global icons
1086 // were updated in the wrong orientation.
1087 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001088 if (DEBUG_RESUME_TIME) {
1089 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1090 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001091
1092 if (mWorkspace.getCustomContentCallbacks() != null) {
1093 // If we are resuming and the custom content is the current page, we call onShow().
1094 // It is also poassible that onShow will instead be called slightly after first layout
1095 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1096 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001097 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001098 }
1099 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001100 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001101 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001102
1103 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001104
1105 if (mLauncherCallbacks != null) {
1106 mLauncherCallbacks.onResume();
1107 }
Adam Cohen06dff352012-06-01 17:17:08 -07001108 }
1109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001111 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001112 // Ensure that items added to Launcher are queued until Launcher returns
1113 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001114 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001115
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001116 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001117 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001118 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001119 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001120
1121 // We call onHide() aggressively. The custom content callbacks should be able to
1122 // debounce excess onHide calls.
1123 if (mWorkspace.getCustomContentCallbacks() != null) {
1124 mWorkspace.getCustomContentCallbacks().onHide();
1125 }
Romain Guycbb89e42009-06-08 15:52:54 -07001126
Adam Cohen9211d422014-10-07 18:14:53 -07001127 if (mLauncherCallbacks != null) {
1128 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001129 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001130 }
1131
1132 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001133 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1134 // by a onResume or by scrolling otherwise.
1135 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001136
1137 // Custom content is completely hidden
1138 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001139
1140 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1141 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001142
1143 // Indicates whether the user is allowed to scroll away from the custom content.
1144 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001145 }
1146
Jorim Jaggid017f882014-01-14 17:08:48 -08001147 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001148 if (mLauncherCallbacks != null) {
1149 return mLauncherCallbacks.hasSettings();
1150 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001151 return false;
1152 }
1153
Adam Cohenbffe7452013-07-22 18:21:45 -07001154
Adam Cohen9211d422014-10-07 18:14:53 -07001155 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001156 CustomContentCallbacks callbacks, String description) {
1157 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001158 }
1159
Adam Cohenedb40762013-07-18 16:45:45 -07001160 // The custom content needs to offset its content to account for the QSB
1161 public int getTopOffsetForCustomContent() {
1162 return mWorkspace.getPaddingTop();
1163 }
1164
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001165 @Override
1166 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001167 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001168 if (mModel.isCurrentCallbacks(this)) {
1169 mModel.stopLoader();
1170 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001171 if (mAppsCustomizeContent != null) {
1172 mAppsCustomizeContent.surrender();
1173 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001174 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001175 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001176
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001177 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001178 @Override
1179 public void onWindowFocusChanged(boolean hasFocus) {
1180 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001181 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001182
1183 if (mLauncherCallbacks != null) {
1184 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1185 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001186 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001187
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 private boolean acceptFilter() {
1189 final InputMethodManager inputManager = (InputMethodManager)
1190 getSystemService(Context.INPUT_METHOD_SERVICE);
1191 return !inputManager.isFullscreenMode();
1192 }
1193
1194 @Override
1195 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001196 final int uniChar = event.getUnicodeChar();
1197 final boolean handled = super.onKeyDown(keyCode, event);
1198 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1199 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1201 keyCode, event);
1202 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001203 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001204 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001205 // showSearchDialog()
1206 // If there are multiple keystrokes before the search dialog takes focus,
1207 // onSearchRequested() will be called for every keystroke,
1208 // but it is idempotent, so it's fine.
1209 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 }
1211 }
1212
Joe Onorato8a9625e2010-01-28 15:55:35 -08001213 // Eat the long press event so the keyboard doesn't come up.
1214 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1215 return true;
1216 }
1217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 return handled;
1219 }
1220
Karl Rosaen138a0412009-04-23 19:00:21 -07001221 private String getTypedText() {
1222 return mDefaultKeySsb.toString();
1223 }
1224
1225 private void clearTypedText() {
1226 mDefaultKeySsb.clear();
1227 mDefaultKeySsb.clearSpans();
1228 Selection.setSelection(mDefaultKeySsb, 0);
1229 }
1230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001232 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1233 * State
1234 */
1235 private static State intToState(int stateOrdinal) {
1236 State state = State.WORKSPACE;
1237 final State[] stateValues = State.values();
1238 for (int i = 0; i < stateValues.length; i++) {
1239 if (stateValues[i].ordinal() == stateOrdinal) {
1240 state = stateValues[i];
1241 break;
1242 }
1243 }
1244 return state;
1245 }
1246
1247 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 * Restores the previous state, if it exists.
1249 *
1250 * @param savedState The previous state.
1251 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001252 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 private void restoreState(Bundle savedState) {
1254 if (savedState == null) {
1255 return;
1256 }
1257
Michael Jurka883f55b2010-10-21 15:47:14 -07001258 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001259 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001260 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001261 }
1262
Adam Cohen21cd0022013-10-09 18:57:02 -07001263 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1264 PagedView.INVALID_RESTORE_PAGE);
1265 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001266 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 }
1268
Winson Chung3d503fb2011-07-13 17:25:49 -07001269 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001270 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001271
Winson Chung3d503fb2011-07-13 17:25:49 -07001272 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1273 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001274 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001275 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1276 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001277 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1278 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1279 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001280 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001281 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 mRestoring = true;
1283 }
1284
1285 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1286 if (renameFolder) {
1287 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001288 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 mRestoring = true;
1290 }
Winson Chunga12a2502010-12-20 14:41:35 -08001291
Winson Chung785d2eb2011-04-14 16:08:02 -07001292 // Restore the AppsCustomize tab
1293 if (mAppsCustomizeTabHost != null) {
1294 String curTab = savedState.getString("apps_customize_currentTab");
1295 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001296 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001297 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001298 mAppsCustomizeContent.loadAssociatedPages(
1299 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001300 }
1301
Winson Chung5afbf7b2011-07-25 11:53:08 -07001302 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1303 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001304 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001305 mItemIdToViewId = (HashMap<Integer, Integer>)
1306 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
1308
1309 /**
1310 * Finds all the views we need and configure them properly.
1311 */
1312 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001313 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001314
Craig Mautner360310b2012-10-26 15:13:08 -07001315 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001316 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001317 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1318 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001319 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001320 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001321
John Spurlock77e1f472013-09-11 10:09:51 -04001322 mLauncherView.setSystemUiVisibility(
1323 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001324 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
Winson Chung4c98d922011-05-31 16:50:48 -07001326 // Setup the drag layer
1327 mDragLayer.setup(this, dragController);
1328
Winson Chung3d503fb2011-07-13 17:25:49 -07001329 // Setup the hotseat
1330 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1331 if (mHotseat != null) {
1332 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001333 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001334 }
1335
Jorim Jaggid017f882014-01-14 17:08:48 -08001336 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001337 View widgetButton = findViewById(R.id.widget_button);
1338 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001339 @Override
1340 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001341 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001342 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001343 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001344 }
1345 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001346 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1347
1348 View wallpaperButton = findViewById(R.id.wallpaper_button);
1349 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001350 @Override
1351 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001352 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001353 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001354 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001355 }
1356 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001357 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1358
1359 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001360 if (hasSettings()) {
1361 settingsButton.setOnClickListener(new OnClickListener() {
1362 @Override
1363 public void onClick(View arg0) {
1364 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001365 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001366 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001367 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001368 });
1369 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1370 } else {
1371 settingsButton.setVisibility(View.GONE);
1372 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1373 lp.gravity = Gravity.END | Gravity.TOP;
1374 widgetButton.requestLayout();
1375 }
1376
Adam Cohendbdff6b2013-09-18 19:09:15 -07001377 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001378
Winson Chung4c98d922011-05-31 16:50:48 -07001379 // Setup the workspace
1380 mWorkspace.setHapticFeedbackEnabled(false);
1381 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001382 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001383 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001384
Winson Chungf0ea4d32011-06-06 14:27:16 -07001385 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001386 mSearchDropTargetBar = (SearchDropTargetBar)
1387 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001388
Winson Chungf0ea4d32011-06-06 14:27:16 -07001389 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001390 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001391 mAppsCustomizeContent = (AppsCustomizePagedView)
1392 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1393 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001394
Winson Chung3d503fb2011-07-13 17:25:49 -07001395 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001396 dragController.setDragScoller(mWorkspace);
1397 dragController.setScrollView(mDragLayer);
1398 dragController.setMoveTarget(mWorkspace);
1399 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001400 if (mSearchDropTargetBar != null) {
1401 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001402 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001403
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001404 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001405 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001406 mWeightWatcher = new WeightWatcher(this);
1407 mWeightWatcher.setAlpha(0.5f);
1408 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001409 new FrameLayout.LayoutParams(
1410 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001411 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001412 Gravity.BOTTOM)
1413 );
Adam Cohen39a06042013-07-19 14:30:12 -07001414
1415 boolean show = shouldShowWeightWatcher();
1416 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001417 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
1419
1420 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001421 * Sets the all apps button. This method is called from {@link Hotseat}.
1422 */
1423 public void setAllAppsButton(View allAppsButton) {
1424 mAllAppsButton = allAppsButton;
1425 }
1426
1427 public View getAllAppsButton() {
1428 return mAllAppsButton;
1429 }
1430
1431 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 * Creates a view representing a shortcut.
1433 *
1434 * @param info The data structure describing the shortcut.
1435 *
1436 * @return A View inflated from R.layout.application.
1437 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001438 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001440 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 }
1442
1443 /**
1444 * Creates a view representing a shortcut inflated from the specified resource.
1445 *
1446 * @param layoutResId The id of the XML layout used to create the shortcut.
1447 * @param parent The group the shortcut belongs to.
1448 * @param info The data structure describing the shortcut.
1449 *
1450 * @return A View inflated from layoutResId.
1451 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001452 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001453 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001454 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001456 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 return favorite;
1458 }
1459
1460 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 * Add a shortcut to the workspace.
1462 *
1463 * @param data The intent describing the shortcut.
1464 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001466 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001467 int cellY) {
1468 int[] cellXY = mTmpAddItemCellCoordinates;
1469 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001470 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001471
Michael Jurkad3ef3062010-11-23 16:23:58 -08001472 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001473
Sunny Goyal2350bc92014-10-14 16:42:54 -07001474 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001475 if (info == null) {
1476 return;
1477 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001478 final View view = createShortcut(info);
1479
Adam Cohenfbba09b2011-07-18 21:43:05 -07001480 // First we check if we already know the exact location where we want to add this item.
1481 if (cellX >= 0 && cellY >= 0) {
1482 cellXY[0] = cellX;
1483 cellXY[1] = cellY;
1484 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001485
1486 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001487 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001488 true, null,null)) {
1489 return;
1490 }
1491 DragObject dragObject = new DragObject();
1492 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001493 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1494 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001495 return;
1496 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001497 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001498 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001499 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001500 foundCellSpan = (result != null);
1501 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001502 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001503 }
1504
1505 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001506 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001507 return;
1508 }
1509
Adam Cohendcd297f2013-06-18 13:13:40 -07001510 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511
1512 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001513 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001514 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 }
1516 }
1517
Adam Cohen2f093b62012-04-30 18:59:53 -07001518 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1519 int minHeight) {
1520 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001521 // We want to account for the extra amount of padding that we are adding to the widget
1522 // to ensure that it gets the full amount of space that it has requested
1523 int requiredWidth = minWidth + padding.left + padding.right;
1524 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001525 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001526 }
1527
Adam Cohen2f093b62012-04-30 18:59:53 -07001528 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1529 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001530 }
1531
Adam Cohen2f093b62012-04-30 18:59:53 -07001532 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1533 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001534 }
1535
Adam Cohen2f093b62012-04-30 18:59:53 -07001536 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1537 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001538 }
1539
Adam Cohen2f093b62012-04-30 18:59:53 -07001540 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1541 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001542 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001543 }
1544
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001546 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001548 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001549 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001551 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001552 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1553 if (appWidgetInfo == null) {
1554 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1555 }
Romain Guycbb89e42009-06-08 15:52:54 -07001556
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001557 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001559
Adam Cohen2f093b62012-04-30 18:59:53 -07001560 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1561 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001562
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001564 // We have saved the position to which the widget was dragged-- this really only matters
1565 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001566 int[] cellXY = mTmpAddItemCellCoordinates;
1567 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001568 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001569 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001570 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1571 cellXY[0] = mPendingAddInfo.cellX;
1572 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001573 spanXY[0] = mPendingAddInfo.spanX;
1574 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001575 foundCellSpan = true;
1576 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001577 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001578 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001579 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1580 spanXY[1], cellXY, finalSpan);
1581 spanXY[0] = finalSpan[0];
1582 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001583 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001584 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001585 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001586 }
1587
Michael Jurka0280c3b2010-09-17 15:00:07 -07001588 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001589 if (appWidgetId != -1) {
1590 // Deleting an app widget ID is a void call but writes to disk before returning
1591 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001592 new AsyncTask<Void, Void, Void>() {
1593 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001594 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001595 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001596 }
Michael Jurka43467462013-11-14 12:03:58 +01001597 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001598 }
Winson Chung93eef082012-03-23 15:59:27 -07001599 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001600 return;
1601 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001603 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001604 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1605 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001606 launcherInfo.spanX = spanXY[0];
1607 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001608 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1609 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001610 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001611
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001613 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614
1615 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001616 if (hostView == null) {
1617 // Perform actual inflation because we're live
1618 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1619 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1620 } else {
1621 // The AppWidgetHostView has already been inflated and instantiated
1622 launcherInfo.hostView = hostView;
1623 }
Romain Guycbb89e42009-06-08 15:52:54 -07001624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001626 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001627 launcherInfo.notifyWidgetSizeChanged(this);
1628
Adam Cohendcd297f2013-06-18 13:13:40 -07001629 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001630 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001631
1632 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001634 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 }
Romain Guycbb89e42009-06-08 15:52:54 -07001636
Adam Cohended9f8d2010-11-03 13:25:16 -07001637 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1638 @Override
1639 public void onReceive(Context context, Intent intent) {
1640 final String action = intent.getAction();
1641 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1642 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001643 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001644 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001645
Winson Chungc100e8e2011-08-09 16:02:43 -07001646 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001647 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001648 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001649 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001650 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1652 mUserPresent = true;
1653 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001654 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1655 mModel.resetLoadedState(false, true);
1656 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1657 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1658 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1659 mModel.resetLoadedState(false, true);
1660 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1661 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1662 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001663 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1664 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1665 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001666 }
1667 }
1668 };
1669
1670 @Override
1671 public void onAttachedToWindow() {
1672 super.onAttachedToWindow();
1673
1674 // Listen for broadcasts related to user-presence
1675 final IntentFilter filter = new IntentFilter();
1676 filter.addAction(Intent.ACTION_SCREEN_OFF);
1677 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001678 // For handling managed profiles
1679 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1680 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001681 if (ENABLE_DEBUG_INTENTS) {
1682 filter.addAction(DebugIntents.DELETE_DATABASE);
1683 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1684 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001685 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001686 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001687 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001688 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001689 mVisible = true;
1690 }
1691
Adam Cohen0b395352014-06-09 22:54:36 +00001692 /**
1693 * Sets up transparent navigation and status bars in LMP.
1694 * This method is a no-op for other platform versions.
1695 */
1696 @TargetApi(19)
1697 private void setupTransparentSystemBarsForLmp() {
1698 // TODO(sansid): use the APIs directly when compiling against L sdk.
1699 // Currently we use reflection to access the flags and the API to set the transparency
1700 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001701 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001702 try {
1703 getWindow().getAttributes().systemUiVisibility |=
1704 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1705 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1706 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1707 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1708 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1709 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1710 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1711 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1712
1713 Method setStatusBarColorMethod =
1714 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1715 Method setNavigationBarColorMethod =
1716 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1717 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1718 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1719 } catch (NoSuchFieldException e) {
1720 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1721 } catch (NoSuchMethodException ex) {
1722 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1723 } catch (IllegalAccessException e) {
1724 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1725 } catch (IllegalArgumentException e) {
1726 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1727 } catch (InvocationTargetException e) {
1728 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1729 } finally {}
1730 }
1731 }
1732
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 @Override
1734 public void onDetachedFromWindow() {
1735 super.onDetachedFromWindow();
1736 mVisible = false;
1737
Adam Cohend113e0c2010-11-11 10:48:05 -08001738 if (mAttached) {
1739 unregisterReceiver(mReceiver);
1740 mAttached = false;
1741 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 updateRunning();
1743 }
1744
1745 public void onWindowVisibilityChanged(int visibility) {
1746 mVisible = visibility == View.VISIBLE;
1747 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001748 // The following code used to be in onResume, but it turns out onResume is called when
1749 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1750 // is a more appropriate event to handle
1751 if (mVisible) {
1752 mAppsCustomizeTabHost.onWindowVisible();
1753 if (!mWorkspaceLoading) {
1754 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001755 // We want to let Launcher draw itself at least once before we force it to build
1756 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001757 // apps is nice and speedy.
1758 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001759 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001760 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001761 if (mStarted) return;
1762 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001763 // We delay the layer building a bit in order to give
1764 // other message processing a time to run. In particular
1765 // this avoids a delay in hiding the IME if it was
1766 // currently shown, because doing that may involve
1767 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001768 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001769 final ViewTreeObserver.OnDrawListener listener = this;
1770 mWorkspace.post(new Runnable() {
1771 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001772 if (mWorkspace != null &&
1773 mWorkspace.getViewTreeObserver() != null) {
1774 mWorkspace.getViewTreeObserver().
1775 removeOnDrawListener(listener);
1776 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001777 }
1778 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001779 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001780 }
1781 });
1782 }
1783 clearTypedText();
1784 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001785 }
1786
1787 private void sendAdvanceMessage(long delay) {
1788 mHandler.removeMessages(ADVANCE_MSG);
1789 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1790 mHandler.sendMessageDelayed(msg, delay);
1791 mAutoAdvanceSentTime = System.currentTimeMillis();
1792 }
1793
1794 private void updateRunning() {
1795 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1796 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1797 mAutoAdvanceRunning = autoAdvanceRunning;
1798 if (autoAdvanceRunning) {
1799 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1800 sendAdvanceMessage(delay);
1801 } else {
1802 if (!mWidgetsToAdvance.isEmpty()) {
1803 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1804 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1805 }
1806 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001807 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001808 }
1809 }
1810 }
1811
1812 private final Handler mHandler = new Handler() {
1813 @Override
1814 public void handleMessage(Message msg) {
1815 if (msg.what == ADVANCE_MSG) {
1816 int i = 0;
1817 for (View key: mWidgetsToAdvance.keySet()) {
1818 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1819 final int delay = mAdvanceStagger * i;
1820 if (v instanceof Advanceable) {
1821 postDelayed(new Runnable() {
1822 public void run() {
1823 ((Advanceable) v).advance();
1824 }
1825 }, delay);
1826 }
1827 i++;
1828 }
1829 sendAdvanceMessage(mAdvanceInterval);
1830 }
1831 }
1832 };
1833
1834 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001835 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001836 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1837 if (v instanceof Advanceable) {
1838 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001839 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001840 updateRunning();
1841 }
1842 }
1843
1844 void removeWidgetToAutoAdvance(View hostView) {
1845 if (mWidgetsToAdvance.containsKey(hostView)) {
1846 mWidgetsToAdvance.remove(hostView);
1847 updateRunning();
1848 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001849 }
1850
Joe Onorato9c1289c2009-08-17 11:03:03 -04001851 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001852 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001853 launcherInfo.hostView = null;
1854 }
1855
Winson Chung93eef082012-03-23 15:59:27 -07001856 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1857 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1858 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001859 }
1860
Winson Chunga6945242014-01-08 14:04:34 -08001861 public DragLayer getDragLayer() {
1862 return mDragLayer;
1863 }
1864
1865 public Workspace getWorkspace() {
1866 return mWorkspace;
1867 }
1868
1869 public Hotseat getHotseat() {
1870 return mHotseat;
1871 }
1872
Jorim Jaggid017f882014-01-14 17:08:48 -08001873 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001874 return mOverviewPanel;
1875 }
1876
1877 public SearchDropTargetBar getSearchBar() {
1878 return mSearchDropTargetBar;
1879 }
1880
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001881 public LauncherAppWidgetHost getAppWidgetHost() {
1882 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 }
Romain Guycbb89e42009-06-08 15:52:54 -07001884
Winson Chunga9abd0e2010-10-27 17:18:37 -07001885 public LauncherModel getModel() {
1886 return mModel;
1887 }
1888
Winson Chunga6945242014-01-08 14:04:34 -08001889 protected SharedPreferences getSharedPrefs() {
1890 return mSharedPrefs;
1891 }
1892
Bjorn Bringertc459e522013-06-07 19:36:01 +01001893 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001894 getWindow().closeAllPanels();
1895
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001896 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001897 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001898 }
1899
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 @Override
1901 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001902 long startTime = 0;
1903 if (DEBUG_RESUME_TIME) {
1904 startTime = System.currentTimeMillis();
1905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 super.onNewIntent(intent);
1907
1908 // Close the menu
1909 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001910 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001911 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001912
Adam Cohened307df2013-10-02 09:37:31 -07001913 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1914 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1915 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001916
Adam Cohen6fecd412013-10-02 17:41:50 -07001917 if (mWorkspace == null) {
1918 // Can be cases where mWorkspace is null, this prevents a NPE
1919 return;
1920 }
1921 Folder openFolder = mWorkspace.getOpenFolder();
1922 // In all these cases, only animate if we're already on home
1923 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001924
1925 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1926 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001927 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001928 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001929 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001930 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001931
Adam Cohen6fecd412013-10-02 17:41:50 -07001932 closeFolder();
1933 exitSpringLoadedDragMode();
1934
1935 // If we are already on home, then just animate back to the workspace,
1936 // otherwise, just wait until onResume to set the state back to Workspace
1937 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001938 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001939 } else {
1940 mOnResumeState = State.WORKSPACE;
1941 }
1942
1943 final View v = getWindow().peekDecorView();
1944 if (v != null && v.getWindowToken() != null) {
1945 InputMethodManager imm = (InputMethodManager)getSystemService(
1946 INPUT_METHOD_SERVICE);
1947 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1948 }
1949
1950 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001951 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001952 mAppsCustomizeTabHost.reset();
1953 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001954
Adam Cohen9211d422014-10-07 18:14:53 -07001955 if (mLauncherCallbacks != null) {
1956 mLauncherCallbacks.onHomeIntent();
1957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
Adam Cohened307df2013-10-02 09:37:31 -07001959
Michael Jurka447bf842013-05-15 14:52:15 +02001960 if (DEBUG_RESUME_TIME) {
1961 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963
Adam Cohen9211d422014-10-07 18:14:53 -07001964 if (mLauncherCallbacks != null) {
1965 mLauncherCallbacks.onNewIntent(intent);
1966 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001967 }
1968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001970 public void onRestoreInstanceState(Bundle state) {
1971 super.onRestoreInstanceState(state);
1972 for (int page: mSynchronouslyBoundPages) {
1973 mWorkspace.restoreInstanceStateForChild(page);
1974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 }
1976
1977 @Override
1978 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001979 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001980 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1981 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001982 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001983 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984
Michael Jurka883f55b2010-10-21 15:47:14 -07001985 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001986 // We close any open folder since it will not be re-opened, and we need to make sure
1987 // this state is reflected.
1988 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989
Adam Cohendcd297f2013-06-18 13:13:40 -07001990 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001991 mWaitingForResult) {
1992 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001993 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001994 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1995 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001996 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1997 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1998 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001999 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 }
2001
2002 if (mFolderInfo != null && mWaitingForResult) {
2003 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2004 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2005 }
Michael Jurka946ad472010-07-09 18:05:18 -07002006
Winson Chung785d2eb2011-04-14 16:08:02 -07002007 // Save the current AppsCustomize tab
2008 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08002009 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2010 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002011 if (currentTabTag != null) {
2012 outState.putString("apps_customize_currentTab", currentTabTag);
2013 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002014 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2015 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002016 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002017 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002018
2019 if (mLauncherCallbacks != null) {
2020 mLauncherCallbacks.onSaveInstanceState(outState);
2021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
2023
2024 @Override
2025 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002027
Winson Chunge7a03942011-08-05 15:05:12 -07002028 // Remove all pending runnables
2029 mHandler.removeMessages(ADVANCE_MSG);
2030 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002031 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002032
Winson Chungcd2b0142011-06-08 16:02:26 -07002033 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002034 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002035
2036 // It's possible to receive onDestroy after a new Launcher activity has
2037 // been created. In this case, don't interfere with the new Launcher.
2038 if (mModel.isCurrentCallbacks(this)) {
2039 mModel.stopLoader();
2040 app.setLauncher(null);
2041 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002044 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002046 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002048 mAppWidgetHost = null;
2049
2050 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051
2052 TextKeyListener.getInstance().release();
2053
Winson Chung81b52252012-08-27 15:34:29 -07002054 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2055 // to prevent leaking Launcher activities on orientation change.
2056 if (mModel != null) {
2057 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2058 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002059
2060 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002061 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002062
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002063 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002064 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002065 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002066 mWorkspace = null;
2067 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002068
Sunny Goyale755d462014-07-22 13:48:29 -07002069 PackageInstallerCompat.getInstance(this).onStop();
Michael Jurka2ecf9952012-06-18 12:52:28 -07002070 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002071
2072 if (mLauncherCallbacks != null) {
2073 mLauncherCallbacks.onDestroy();
2074 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 }
2076
Adam Cohena9cf38f2011-05-02 15:36:58 -07002077 public DragController getDragController() {
2078 return mDragController;
2079 }
2080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 @Override
2082 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002083 if (requestCode >= 0) {
2084 setWaitingForResult(true);
2085 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 super.startActivityForResult(intent, requestCode);
2087 }
2088
Winson Chung70d72102011-08-12 11:24:35 -07002089 /**
2090 * Indicates that we want global search for this activity by setting the globalSearch
2091 * argument for {@link #startSearch} to true.
2092 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002094 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002096
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002097 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002098
Karl Rosaen138a0412009-04-23 19:00:21 -07002099 if (initialQuery == null) {
2100 // Use any text typed in the launcher as the initial query
2101 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002102 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 if (appSearchData == null) {
2104 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002105 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 }
Winson Chungadf0c182012-08-23 14:59:07 -07002107 Rect sourceBounds = new Rect();
2108 if (mSearchDropTargetBar != null) {
2109 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2110 }
Romain Guycbb89e42009-06-08 15:52:54 -07002111
Ian Parkinson047036e2014-09-01 15:40:53 +01002112 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002113 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002114 if (clearTextImmediately) {
2115 clearTypedText();
2116 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002117 }
2118
Ian Parkinson047036e2014-09-01 15:40:53 +01002119 /**
2120 * Start a text search.
2121 *
2122 * @return {@code true} if the search will start immediately, so any further keypresses
2123 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2124 * to buffer keypresses.
2125 */
2126 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002127 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002128 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2129 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2130 sourceBounds);
2131 }
2132
Michael Jurkaa33411c2012-06-14 16:18:21 -07002133 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002134 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002135 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002136 }
2137
2138 /**
2139 * Starts the global search activity. This code is a copied from SearchManager
2140 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002141 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002142 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002143 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002144 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2145 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2146 if (globalSearchActivity == null) {
2147 Log.w(TAG, "No global search activity found.");
2148 return;
2149 }
2150 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2151 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2152 intent.setComponent(globalSearchActivity);
2153 // Make sure that we have a Bundle to put source in
2154 if (appSearchData == null) {
2155 appSearchData = new Bundle();
2156 } else {
2157 appSearchData = new Bundle(appSearchData);
2158 }
Adam Cohen9211d422014-10-07 18:14:53 -07002159 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002160 if (!appSearchData.containsKey("source")) {
2161 appSearchData.putString("source", getPackageName());
2162 }
2163 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2164 if (!TextUtils.isEmpty(initialQuery)) {
2165 intent.putExtra(SearchManager.QUERY, initialQuery);
2166 }
2167 if (selectInitialQuery) {
2168 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2169 }
2170 intent.setSourceBounds(sourceBounds);
2171 try {
2172 startActivity(intent);
2173 } catch (ActivityNotFoundException ex) {
2174 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 }
2177
Yura4f93ec62014-02-04 14:15:21 +00002178 public boolean isOnCustomContent() {
2179 return mWorkspace.isOnOrMovingToCustomContent();
2180 }
2181
Winson Chung70d72102011-08-12 11:24:35 -07002182 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002183 public boolean onPrepareOptionsMenu(Menu menu) {
2184 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002185 if (!isOnCustomContent()) {
2186 // Close any open folders
2187 closeFolder();
2188 // Stop resizing any widgets
2189 mWorkspace.exitWidgetResizeMode();
2190 if (!mWorkspace.isInOverviewMode()) {
2191 // Show the overview mode
2192 showOverviewMode(true);
2193 } else {
2194 showWorkspace(true);
2195 }
Winson Chunge0298742014-01-17 12:03:00 -08002196 }
Adam Cohen9211d422014-10-07 18:14:53 -07002197 if (mLauncherCallbacks != null) {
2198 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2199 }
2200
Michael Jurkab94f3f82013-09-11 18:08:54 +02002201 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002204 @Override
2205 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002206 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002207 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002208 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002209 }
2210
Joe Onorato9c1289c2009-08-17 11:03:03 -04002211 public boolean isWorkspaceLocked() {
2212 return mWorkspaceLoading || mWaitingForResult;
2213 }
2214
Adam Cohen517a7f52014-03-01 12:12:59 -08002215 public boolean isWorkspaceLoading() {
2216 return mWorkspaceLoading;
2217 }
2218
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002219 private void setWorkspaceLoading(boolean value) {
2220 boolean isLocked = isWorkspaceLocked();
2221 mWorkspaceLoading = value;
2222 if (isLocked != isWorkspaceLocked()) {
2223 onWorkspaceLockedChanged();
2224 }
2225 }
2226
2227 private void setWaitingForResult(boolean value) {
2228 boolean isLocked = isWorkspaceLocked();
2229 mWaitingForResult = value;
2230 if (isLocked != isWorkspaceLocked()) {
2231 onWorkspaceLockedChanged();
2232 }
2233 }
2234
Adam Cohen9211d422014-10-07 18:14:53 -07002235 protected void onWorkspaceLockedChanged() {
2236 if (mLauncherCallbacks != null) {
2237 mLauncherCallbacks.onWorkspaceLockedChanged();
2238 }
2239 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002240
Michael Jurka0280c3b2010-09-17 15:00:07 -07002241 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002242 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002243 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002244 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2245 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002246 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002247 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002248 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002249
Adam Cohenad4e15c2013-10-17 16:21:35 -07002250 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2251 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2252 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2253 }
2254
2255 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2256 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2257 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002258 if (appWidgetInfo.configure != null) {
2259 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002260 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002261
Bjorn Bringert7984c942009-12-09 15:38:25 +00002262 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002263 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2264 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002267 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002268 Runnable onComplete = new Runnable() {
2269 @Override
2270 public void run() {
2271 // Exit spring loaded mode if necessary after adding the widget
2272 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2273 null);
2274 }
2275 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002276 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002277 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002278 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279 }
2280 }
Romain Guycbb89e42009-06-08 15:52:54 -07002281
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002282 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002283 // Close any folders that may be open.
2284 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002285 mWorkspace.moveToCustomContentScreen(animate);
2286 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002287 /**
2288 * Process a shortcut drop.
2289 *
2290 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002291 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002292 * @param cell The cell it should be added to, optional
2293 * @param position The location on the screen where it was dropped, optional
2294 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002295 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002296 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002297 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002298 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002299 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002300 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002301
2302 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002303 mPendingAddInfo.cellX = cell[0];
2304 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002305 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002306
2307 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2308 createShortcutIntent.setComponent(componentName);
2309 processShortcut(createShortcutIntent);
2310 }
2311
Adam Cohenfbba09b2011-07-18 21:43:05 -07002312 /**
2313 * Process a widget drop.
2314 *
2315 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002316 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002317 * @param cell The cell it should be added to, optional
2318 * @param position The location on the screen where it was dropped, optional
2319 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002320 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002321 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002322 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002323 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002324 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002325 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002326 mPendingAddInfo.minSpanX = info.minSpanX;
2327 mPendingAddInfo.minSpanY = info.minSpanY;
2328
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002330 mPendingAddInfo.cellX = cell[0];
2331 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002332 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002333 if (span != null) {
2334 mPendingAddInfo.spanX = span[0];
2335 mPendingAddInfo.spanY = span[1];
2336 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002337
Adam Cohened66b2b2012-01-23 17:28:51 -08002338 AppWidgetHostView hostView = info.boundWidget;
2339 int appWidgetId;
2340 if (hostView != null) {
2341 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002342 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002343 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002344 // In this case, we either need to start an activity to get permission to bind
2345 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002346 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002347 Bundle options = info.bindOptions;
2348
Sunny Goyalffe83f12014-08-14 17:39:34 -07002349 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2350 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002351 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002352 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002353 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002354 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002355 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2356 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2357 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002358 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2359 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002360 // TODO: we need to make sure that this accounts for the options bundle.
2361 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002362 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2363 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002364 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002365 }
2366
Joe Onoratodeb98af2010-02-19 14:59:39 -08002367 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002368 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002369 }
2370
Winson Chung24ab2f12010-09-16 14:10:47 -07002371 void processWallpaper(Intent intent) {
2372 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2373 }
2374
Adam Cohendcd297f2013-06-18 13:13:40 -07002375 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002376 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002377 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378 folderInfo.title = getText(R.string.folder_name);
2379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002380 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002381 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002382 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002383 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384
2385 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002386 FolderIcon newFolder =
2387 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002388 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002389 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002390 // Force measure the new folder icon
2391 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2392 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002393 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 }
Romain Guycbb89e42009-06-08 15:52:54 -07002395
Joe Onorato9c1289c2009-08-17 11:03:03 -04002396 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002397 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002398 }
2399
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002400 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002401 if (mLauncherCallbacks != null) {
2402 return mLauncherCallbacks.getWallpaperPickerComponent();
2403 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002404 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002405 }
2406
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002407 /**
2408 * Registers various content observers. The current implementation registers
2409 * only a favorites observer to keep track of the favorites applications.
2410 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002411 private void registerContentObservers() {
2412 ContentResolver resolver = getContentResolver();
2413 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2414 true, mWidgetObserver);
2415 }
2416
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 @Override
2418 public boolean dispatchKeyEvent(KeyEvent event) {
2419 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2420 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002422 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002423 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002424 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002425 dumpState();
2426 return true;
2427 }
2428 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002429 }
2430 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2431 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002432 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002433 return true;
2434 }
2435 }
2436
2437 return super.dispatchKeyEvent(event);
2438 }
2439
Joe Onorato88ec0992009-11-19 13:16:06 -08002440 @Override
2441 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002442 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2443 return;
2444 }
2445
Winson Chungc93e5ae2012-07-23 20:48:26 -07002446 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002447 if (mAppsCustomizeContent.getContentType() ==
2448 AppsCustomizePagedView.ContentType.Applications) {
2449 showWorkspace(true);
2450 } else {
2451 showOverviewMode(true);
2452 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002453 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002454 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002455 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002456 Folder openFolder = mWorkspace.getOpenFolder();
2457 if (openFolder.isEditingName()) {
2458 openFolder.dismissEditingName();
2459 } else {
2460 closeFolder();
2461 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002462 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002463 mWorkspace.exitWidgetResizeMode();
2464
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002465 // Back button is a no-op here, but give at least some feedback for the button press
2466 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002467 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002468 }
2469
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002471 * Re-listen when widgets are reset.
2472 */
2473 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002474 if (mAppWidgetHost != null) {
2475 mAppWidgetHost.startListening();
2476 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002477 }
2478
2479 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 * Launches the intent referred by the clicked shortcut.
2481 *
2482 * @param v The view representing the clicked shortcut.
2483 */
2484 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002485 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2486 // view has detached (it's possible for this to happen if the view is removed mid touch).
2487 if (v.getWindowToken() == null) {
2488 return;
2489 }
2490
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002491 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002492 return;
2493 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002494
Adam Cohen1697b792013-09-17 19:08:21 -07002495 if (v instanceof Workspace) {
2496 if (mWorkspace.isInOverviewMode()) {
2497 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002498 }
2499 return;
2500 }
2501
2502 if (v instanceof CellLayout) {
2503 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002504 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002505 }
2506 }
2507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002509 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002510 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002512 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002513 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002514 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002515 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002516 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002517 } else if (tag instanceof AppInfo) {
2518 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002519 } else if (tag instanceof LauncherAppWidgetInfo) {
2520 if (v instanceof PendingAppWidgetHostView) {
2521 onClickPendingWidget((PendingAppWidgetHostView) v);
2522 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 }
2524 }
2525
Sunny Goyal508da152014-08-14 10:53:27 -07002526 public void onClickPagedViewIcon(View v) {
2527 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002528 if (mLauncherCallbacks != null) {
2529 mLauncherCallbacks.onClickPagedViewIcon(v);
2530 }
Sunny Goyal508da152014-08-14 10:53:27 -07002531 }
2532
Michael Jurka0e260592010-06-30 17:07:39 -07002533 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002534 return false;
2535 }
2536
Michael Jurkaaf442092010-06-10 17:01:57 -07002537 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002538 * Event handler for the app widget view which has not fully restored.
2539 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002540 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002541 if (mIsSafeModeEnabled) {
2542 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2543 return;
2544 }
2545
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002546 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002547 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002548 int widgetId = info.appWidgetId;
2549 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2550 if (appWidgetInfo != null) {
2551 mPendingAddWidgetInfo = appWidgetInfo;
2552 mPendingAddInfo.copyFrom(info);
2553 mPendingAddWidgetId = widgetId;
2554
Sunny Goyalffe83f12014-08-14 17:39:34 -07002555 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2556 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002557 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002558 } else if (info.installProgress < 0) {
2559 // The install has not been queued
2560 final String packageName = info.providerName.getPackageName();
2561 showBrokenAppInstallDialog(packageName,
2562 new DialogInterface.OnClickListener() {
2563 public void onClick(DialogInterface dialog, int id) {
2564 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2565 }
2566 });
2567 } else {
2568 // Download has started.
2569 final String packageName = info.providerName.getPackageName();
2570 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002571 }
2572 }
2573
2574 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002575 * Event handler for the search button
2576 *
2577 * @param v The view that was clicked.
2578 */
2579 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002580 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2581
Amith Yamasania135ba82011-08-09 17:42:01 -07002582 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002583 }
2584
2585 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002586 * Event handler for the voice button
2587 *
2588 * @param v The view that was clicked.
2589 */
2590 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002591 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002592
Bjorn Bringertc459e522013-06-07 19:36:01 +01002593 startVoice();
2594 }
2595
2596 public void startVoice() {
Adam Cohen9211d422014-10-07 18:14:53 -07002597 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2598 mLauncherCallbacks.startVoice();
2599 return;
2600 }
2601
Michael Jurkaae65ee32012-04-30 11:45:54 -07002602 try {
2603 final SearchManager searchManager =
2604 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2605 ComponentName activityName = searchManager.getGlobalSearchActivity();
2606 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002607 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002608 if (activityName != null) {
2609 intent.setPackage(activityName.getPackageName());
2610 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002611 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002612 } catch (ActivityNotFoundException e) {
2613 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002614 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002615 startActivitySafely(null, intent, "onClickVoiceButton");
2616 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002617 }
2618
2619 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002620 * Event handler for the "grid" button that appears on the home screen, which
2621 * enters all apps mode.
2622 *
2623 * @param v The view that was clicked.
2624 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002625 protected void onClickAllAppsButton(View v) {
2626 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2627 if (isAllAppsVisible()) {
2628 showWorkspace(true);
2629 } else {
2630 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2631 }
Adam Cohen9211d422014-10-07 18:14:53 -07002632 if (mLauncherCallbacks != null) {
2633 mLauncherCallbacks.onClickAllAppsButton(v);
2634 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002635 }
2636
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002637 private void showBrokenAppInstallDialog(final String packageName,
2638 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002639 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002640 .setTitle(R.string.abandoned_promises_title)
2641 .setMessage(R.string.abandoned_promise_explanation)
2642 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2643 .setNeutralButton(R.string.abandoned_clean_this,
2644 new DialogInterface.OnClickListener() {
2645 public void onClick(DialogInterface dialog, int id) {
2646 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2647 mWorkspace.removeAbandonedPromise(packageName, user);
2648 }
2649 })
2650 .create().show();
2651 return;
2652 }
2653
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002654 /**
2655 * Event handler for an app shortcut click.
2656 *
2657 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2658 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002659 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002660 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2661 Object tag = v.getTag();
2662 if (!(tag instanceof ShortcutInfo)) {
2663 throw new IllegalArgumentException("Input must be a Shortcut");
2664 }
2665
2666 // Open shortcut
2667 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002668
2669 if (shortcut.isDisabled != 0) {
2670 int error = R.string.activity_not_available;
2671 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2672 error = R.string.safemode_shortcut_error;
2673 }
2674 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2675 return;
2676 }
2677
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002678 final Intent intent = shortcut.intent;
2679
2680 // Check for special shortcuts
2681 if (intent.getComponent() != null) {
2682 final String shortcutClass = intent.getComponent().getClassName();
2683
2684 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2685 MemoryDumpActivity.startDump(this);
2686 return;
2687 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2688 toggleShowWeightWatcher();
2689 return;
2690 }
2691 }
2692
Chris Wren40c5ed32014-06-24 18:24:23 -04002693 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002694 if ((v instanceof BubbleTextView)
2695 && shortcut.isPromise()
2696 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002697 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002698 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002699 new DialogInterface.OnClickListener() {
2700 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002701 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002702 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002703 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002704 return;
2705 }
2706
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002707 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002708 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002709
2710 if (mLauncherCallbacks != null) {
2711 mLauncherCallbacks.onClickAppShortcut(v);
2712 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002713 }
2714
Sunny Goyal508da152014-08-14 10:53:27 -07002715 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002716 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002717 final ShortcutInfo shortcut;
2718 final Intent intent;
2719 if (tag instanceof ShortcutInfo) {
2720 shortcut = (ShortcutInfo) tag;
2721 intent = shortcut.intent;
2722 int[] pos = new int[2];
2723 v.getLocationOnScreen(pos);
2724 intent.setSourceBounds(new Rect(pos[0], pos[1],
2725 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002726
Sunny Goyal508da152014-08-14 10:53:27 -07002727 } else if (tag instanceof AppInfo) {
2728 shortcut = null;
2729 intent = ((AppInfo) tag).intent;
2730 } else {
2731 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2732 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002733
2734 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002735 mStats.recordLaunch(intent, shortcut);
2736
2737 if (success && v instanceof BubbleTextView) {
2738 mWaitingForResume = (BubbleTextView) v;
2739 mWaitingForResume.setStayPressed(true);
2740 }
2741 }
2742
2743 /**
2744 * Event handler for a folder icon click.
2745 *
2746 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2747 */
2748 protected void onClickFolderIcon(View v) {
2749 if (LOGD) Log.d(TAG, "onClickFolder");
2750 if (!(v instanceof FolderIcon)){
2751 throw new IllegalArgumentException("Input must be a FolderIcon");
2752 }
2753
2754 FolderIcon folderIcon = (FolderIcon) v;
2755 final FolderInfo info = folderIcon.getFolderInfo();
2756 Folder openFolder = mWorkspace.getFolderForTag(info);
2757
2758 // If the folder info reports that the associated folder is open, then verify that
2759 // it is actually opened. There have been a few instances where this gets out of sync.
2760 if (info.opened && openFolder == null) {
2761 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2762 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2763 info.opened = false;
2764 }
2765
2766 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2767 // Close any open folder
2768 closeFolder();
2769 // Open the requested folder
2770 openFolder(folderIcon);
2771 } else {
2772 // Find the open folder...
2773 int folderScreen;
2774 if (openFolder != null) {
2775 folderScreen = mWorkspace.getPageForView(openFolder);
2776 // .. and close it
2777 closeFolder(openFolder);
2778 if (folderScreen != mWorkspace.getCurrentPage()) {
2779 // Close any folder open on the current screen
2780 closeFolder();
2781 // Pull the folder onto this screen
2782 openFolder(folderIcon);
2783 }
2784 }
2785 }
Adam Cohen9211d422014-10-07 18:14:53 -07002786
2787 if (mLauncherCallbacks != null) {
2788 mLauncherCallbacks.onClickFolderIcon(v);
2789 }
Michael Jurka5130e402011-10-13 04:55:35 -07002790 }
2791
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002792 /**
2793 * Event handler for the (Add) Widgets button that appears after a long press
2794 * on the home screen.
2795 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002796 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002797 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002798 if (mIsSafeModeEnabled) {
2799 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2800 } else {
2801 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2802 if (mLauncherCallbacks != null) {
2803 mLauncherCallbacks.onClickAddWidgetButton(view);
2804 }
Adam Cohen9211d422014-10-07 18:14:53 -07002805 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002806 }
2807
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002808 /**
2809 * Event handler for the wallpaper picker button that appears after a long press
2810 * on the home screen.
2811 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002812 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002813 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2814 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2815 pickWallpaper.setComponent(getWallpaperPickerComponent());
2816 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002817
2818 if (mLauncherCallbacks != null) {
2819 mLauncherCallbacks.onClickWallpaperPicker(v);
2820 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002821 }
2822
2823 /**
2824 * Event handler for a click on the settings button that appears after a long press
2825 * on the home screen.
2826 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002827 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002828 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002829 if (mLauncherCallbacks != null) {
2830 mLauncherCallbacks.onClickSettingsButton(v);
2831 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002832 }
2833
Michael Jurka5130e402011-10-13 04:55:35 -07002834 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002835 // Provide the same haptic feedback that the system offers for virtual keys.
2836 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002837 }
2838
Adam Cohen61f560d2013-09-30 15:58:20 -07002839 public void performHapticFeedbackOnTouchDown(View v) {
2840 // Provide the same haptic feedback that the system offers for virtual keys.
2841 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2842 }
2843
2844 public View.OnTouchListener getHapticFeedbackTouchListener() {
2845 if (mHapticFeedbackTouchListener == null) {
2846 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2847 @Override
2848 public boolean onTouch(View v, MotionEvent event) {
2849 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2850 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2851 }
2852 return false;
2853 }
2854 };
2855 }
2856 return mHapticFeedbackTouchListener;
2857 }
2858
Adam Cohen9211d422014-10-07 18:14:53 -07002859 public void onDragStarted(View view) {
2860 if (isOnCustomContent()) {
2861 // Custom content screen doesn't participate in drag and drop. If on custom
2862 // content screen, move to default.
2863 moveWorkspaceToDefaultScreen();
2864 }
2865
2866 if (mLauncherCallbacks != null) {
2867 mLauncherCallbacks.onDragStarted(view);
2868 }
2869 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002870
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002871 /**
2872 * Called when the user stops interacting with the launcher.
2873 * This implies that the user is now on the homescreen and is not doing housekeeping.
2874 */
Adam Cohen9211d422014-10-07 18:14:53 -07002875 protected void onInteractionEnd() {
2876 if (mLauncherCallbacks != null) {
2877 mLauncherCallbacks.onInteractionEnd();
2878 }
2879 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002880
2881 /**
2882 * Called when the user starts interacting with the launcher.
2883 * The possible interactions are:
2884 * - open all apps
2885 * - reorder an app shortcut, or a widget
2886 * - open the overview mode.
2887 * This is a good time to stop doing things that only make sense
2888 * when the user is on the homescreen and not doing housekeeping.
2889 */
Adam Cohen9211d422014-10-07 18:14:53 -07002890 protected void onInteractionBegin() {
2891 if (mLauncherCallbacks != null) {
2892 mLauncherCallbacks.onInteractionBegin();
2893 }
2894 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002895
Kenny Guyf07af7b2014-07-31 11:39:16 +01002896 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002897 String packageName = componentName.getPackageName();
Kenny Guyf07af7b2014-07-31 11:39:16 +01002898 try {
2899 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2900 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2901 launcherApps.showAppDetailsForProfile(componentName, user);
2902 } catch (SecurityException e) {
2903 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2904 Log.e(TAG, "Launcher does not have permission to launch settings");
2905 } catch (ActivityNotFoundException e) {
2906 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2907 Log.e(TAG, "Unable to launch settings");
2908 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002909 }
2910
Michael Jurka1e2f4652013-07-08 18:03:46 -07002911 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002912 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2913 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002914 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002915 // System applications cannot be installed. For now, show a toast explaining that.
2916 // We may give them the option of disabling apps this way.
2917 int messageId = R.string.uninstall_system_app_text;
2918 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002919 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002920 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002921 String packageName = componentName.getPackageName();
2922 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002923 Intent intent = new Intent(
2924 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002925 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2926 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002927 if (user != null) {
2928 user.addToIntent(intent, Intent.EXTRA_USER);
2929 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002930 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002931 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002932 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002933 }
2934
Michael Jurka86a720e2012-05-09 11:23:23 -07002935 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002936 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002937 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002938 // Only launch using the new animation if the shortcut has not opted out (this is a
2939 // private contract between launcher and may be ignored in the future).
2940 boolean useLaunchAnimation = (v != null) &&
2941 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002942 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2943 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002944
Kenny Guy1317e2d2014-05-08 18:52:50 +01002945 UserHandleCompat user = null;
2946 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2947 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2948 user = userManager.getUserForSerialNumber(serialNumber);
2949 }
2950
2951 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002952 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002953 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002954 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2955 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002956 optsBundle = opts.toBundle();
2957 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002958
2959 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2960 // Could be launching some bookkeeping activity
2961 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002962 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002963 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002964 launcherApps.startActivityForProfile(intent.getComponent(), user,
2965 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002966 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002967 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 } catch (SecurityException e) {
2969 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002970 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002972 "or use the exported attribute for this activity. "
2973 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002974 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002975 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002976 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002977
Michael Jurka86a720e2012-05-09 11:23:23 -07002978 boolean startActivitySafely(View v, Intent intent, Object tag) {
2979 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002980 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2981 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2982 return false;
2983 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002984 try {
2985 success = startActivity(v, intent, tag);
2986 } catch (ActivityNotFoundException e) {
2987 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2988 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2989 }
2990 return success;
2991 }
2992
Adam Cohen268c4752012-06-06 17:47:33 -07002993 /**
2994 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2995 * in the DragLayer in the exact absolute location of the original FolderIcon.
2996 */
2997 private void copyFolderIconToImage(FolderIcon fi) {
2998 final int width = fi.getMeasuredWidth();
2999 final int height = fi.getMeasuredHeight();
3000
3001 // Lazy load ImageView, Bitmap and Canvas
3002 if (mFolderIconImageView == null) {
3003 mFolderIconImageView = new ImageView(this);
3004 }
3005 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3006 mFolderIconBitmap.getHeight() != height) {
3007 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3008 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3009 }
3010
3011 DragLayer.LayoutParams lp;
3012 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3013 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3014 } else {
3015 lp = new DragLayer.LayoutParams(width, height);
3016 }
3017
Adam Cohen307fe232012-08-16 17:55:58 -07003018 // The layout from which the folder is being opened may be scaled, adjust the starting
3019 // view size by this scale factor.
3020 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003021 lp.customPosition = true;
3022 lp.x = mRectForFolderAnimation.left;
3023 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003024 lp.width = (int) (scale * width);
3025 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003026
3027 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3028 fi.draw(mFolderIconCanvas);
3029 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003030 if (fi.getFolder() != null) {
3031 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3032 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003033 }
Adam Cohen268c4752012-06-06 17:47:33 -07003034 // Just in case this image view is still in the drag layer from a previous animation,
3035 // we remove it and re-add it.
3036 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3037 mDragLayer.removeView(mFolderIconImageView);
3038 }
3039 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003040 if (fi.getFolder() != null) {
3041 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003042 }
Adam Cohen268c4752012-06-06 17:47:33 -07003043 }
3044
Adam Cohen2801caf2011-05-13 20:57:39 -07003045 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003046 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003047 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3048 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3049 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3050
Adam Cohenc51934b2011-07-26 21:07:43 -07003051 FolderInfo info = (FolderInfo) fi.getTag();
3052 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3053 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003054 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3055 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003056 }
3057
Adam Cohen268c4752012-06-06 17:47:33 -07003058 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3059 copyFolderIconToImage(fi);
3060 fi.setVisibility(View.INVISIBLE);
3061
Michael Jurka2ecf9952012-06-18 12:52:28 -07003062 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003063 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003064 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003065 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3066 }
3067 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003068 oa.start();
3069 }
3070
Adam Cohen268c4752012-06-06 17:47:33 -07003071 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003072 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003073 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3074 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3075 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3076
Adam Cohen268c4752012-06-06 17:47:33 -07003077 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003078
Adam Cohen268c4752012-06-06 17:47:33 -07003079 // We remove and re-draw the FolderIcon in-case it has changed
3080 mDragLayer.removeView(mFolderIconImageView);
3081 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003082 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003083 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003084 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003085 oa.addListener(new AnimatorListenerAdapter() {
3086 @Override
3087 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003088 if (cl != null) {
3089 cl.clearFolderLeaveBehind();
3090 // Remove the ImageView copy of the FolderIcon and make the original visible.
3091 mDragLayer.removeView(mFolderIconImageView);
3092 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003093 }
3094 }
3095 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003096 oa.start();
3097 }
3098
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003099 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003100 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003101 * is animated relative to the specified View. If the View is null, no animation
3102 * is played.
3103 *
3104 * @param folderInfo The FolderInfo describing the folder to open.
3105 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003106 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003107 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003108 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003109
Adam Cohena9cf38f2011-05-02 15:36:58 -07003110 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003111
Adam Cohen4554ee12011-08-03 16:13:21 -07003112 // Just verify that the folder hasn't already been added to the DragLayer.
3113 // There was a one-off crash where the folder had a parent already.
3114 if (folder.getParent() == null) {
3115 mDragLayer.addView(folder);
3116 mDragController.addDropTarget((DropTarget) folder);
3117 } else {
3118 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3119 folder.getParent() + ").");
3120 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003121 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003122 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003123
3124 // Notify the accessibility manager that this folder "window" has appeared and occluded
3125 // the workspace items
3126 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3127 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003128 }
3129
3130 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003131 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003132 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003133 if (folder.isEditingName()) {
3134 folder.dismissEditingName();
3135 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003136 closeFolder(folder);
3137 }
3138 }
3139
3140 void closeFolder(Folder folder) {
3141 folder.getInfo().opened = false;
3142
3143 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3144 if (parent != null) {
3145 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3146 shrinkAndFadeInFolderIcon(fi);
3147 }
3148 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003149
3150 // Notify the accessibility manager that this folder "window" has disappeard and no
3151 // longer occludeds the workspace items
3152 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003153 }
3154
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003155 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003156 if (!isDraggingEnabled()) return false;
3157 if (isWorkspaceLocked()) return false;
3158 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003159
Adam Cohen1697b792013-09-17 19:08:21 -07003160 if (v instanceof Workspace) {
3161 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003162 if (mWorkspace.enterOverviewMode()) {
3163 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3164 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3165 return true;
3166 } else {
3167 return false;
3168 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003169 } else {
3170 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003171 }
Adam Cohen1697b792013-09-17 19:08:21 -07003172 }
3173
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003174 CellLayout.CellInfo longClickCellInfo = null;
3175 View itemUnderLongClick = null;
3176 if (v.getTag() instanceof ItemInfo) {
3177 ItemInfo info = (ItemInfo) v.getTag();
3178 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3179 itemUnderLongClick = longClickCellInfo.cell;
3180 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003181 }
3182
Winson Chung3d503fb2011-07-13 17:25:49 -07003183 // The hotseat touch handling does not go through Workspace, and we always allow long press
3184 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003185 final boolean inHotseat = isHotseatLayout(v);
3186 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003187 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003188 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003189 // User long pressed on empty space
3190 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3191 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003192 if (mWorkspace.isInOverviewMode()) {
3193 mWorkspace.startReordering(v);
3194 } else {
3195 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003196 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003197 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003198 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3199 mHotseat.getOrderInHotseat(
3200 longClickCellInfo.cellX,
3201 longClickCellInfo.cellY));
3202 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003203 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003204 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003205 }
3206 }
3207 }
3208 return true;
3209 }
3210
Winson Chung3d503fb2011-07-13 17:25:49 -07003211 boolean isHotseatLayout(View layout) {
3212 return mHotseat != null && layout != null &&
3213 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3214 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003215
Winson Chung3d503fb2011-07-13 17:25:49 -07003216 /**
3217 * Returns the CellLayout of the specified container at the specified screen.
3218 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003219 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003220 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3221 if (mHotseat != null) {
3222 return mHotseat.getLayout();
3223 } else {
3224 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003225 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003226 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003227 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003228 }
3229 }
3230
Daniel Sandler843e8602010-06-07 14:59:01 -04003231 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003232 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003233 }
3234
Craig Mautner360310b2012-10-26 15:13:08 -07003235 private void setWorkspaceBackground(boolean workspace) {
3236 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003237 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003238 }
3239
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003240 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003241 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3242 int curflags = getWindow().getAttributes().flags
3243 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3244 if (wpflags != curflags) {
3245 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3246 }
Craig Mautner360310b2012-10-26 15:13:08 -07003247 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003248 }
3249
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003250 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3251 if (v instanceof LauncherTransitionable) {
3252 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3253 }
3254 }
3255
Michael Jurkabed61d22012-02-14 22:51:29 -08003256 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3257 if (v instanceof LauncherTransitionable) {
3258 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3259 }
Winson Chung70442722012-02-10 15:43:22 -08003260
3261 // Update the workspace transition step as well
3262 dispatchOnLauncherTransitionStep(v, 0f);
3263 }
3264
3265 private void dispatchOnLauncherTransitionStep(View v, float t) {
3266 if (v instanceof LauncherTransitionable) {
3267 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3268 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003269 }
3270
3271 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3272 if (v instanceof LauncherTransitionable) {
3273 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3274 }
Winson Chung70442722012-02-10 15:43:22 -08003275
3276 // Update the workspace transition step as well
3277 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003278 }
3279
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003280 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003281 * Things to test when changing the following seven functions.
3282 * - Home from workspace
3283 * - from center screen
3284 * - from other screens
3285 * - Home from all apps
3286 * - from center screen
3287 * - from other screens
3288 * - Back from all apps
3289 * - from center screen
3290 * - from other screens
3291 * - Launch app from workspace and quit
3292 * - with back
3293 * - with home
3294 * - Launch app from all apps and quit
3295 * - with back
3296 * - with home
3297 * - Go to a screen that's not the default, then all
3298 * apps, and launch and app, and go back
3299 * - with back
3300 * -with home
3301 * - On workspace, long press power and go back
3302 * - with back
3303 * - with home
3304 * - On all apps, long press power and go back
3305 * - with back
3306 * - with home
3307 * - On workspace, power off
3308 * - On all apps, power off
3309 * - Launch an app and turn off the screen while in that app
3310 * - Go back with home key
3311 * - Go back with back key TODO: make this not go to workspace
3312 * - From all apps
3313 * - From workspace
3314 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3315 * - From all apps
3316 * - From the center workspace
3317 * - From another workspace
3318 */
3319
3320 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003321 * Zoom the camera out from the workspace to reveal 'toView'.
3322 * Assumes that the view to show is anchored at either the very top or very bottom
3323 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003324 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003325 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003326 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3327 showAppsCustomizeHelper(animated, springLoaded, contentType);
3328 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003329
Winson Chungc58497e2013-09-03 17:48:37 -07003330 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3331 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003332 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003333 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003334 mStateAnimation.cancel();
3335 mStateAnimation = null;
3336 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003337
Kenny Guyd794a3f2014-09-16 15:17:58 +01003338 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003339
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003340 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003341
Winson Chungf0ea4d32011-06-06 14:27:16 -07003342 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3343 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003344 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003345 final int itemsAlphaStagger =
3346 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003347
Winson Chungf0ea4d32011-06-06 14:27:16 -07003348 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003349 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003350 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003351
Adam Cohen2854d252014-08-27 16:04:07 -07003352 final ArrayList<View> layerViews = new ArrayList<View>();
3353
Adam Cohen6c5891a2014-07-09 23:53:15 -07003354 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3355 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003356 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003357 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003358 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003359 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3360 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003361 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3362 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003363
Adam Cohena38dc902014-09-07 17:48:55 +02003364 // If for some reason our views aren't initialized, don't animate
3365 boolean initialized = getAllAppsButton() != null;
3366
3367 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003368 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003369 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3370 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003371
Adam Cohen9bfdb762014-07-21 17:44:06 -07003372 final View page = content.getPageAt(content.getCurrentPage());
3373 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003374
Adam Cohen63f1ec02014-08-12 09:23:13 -07003375 final float initialPanelAlpha = 1f;
3376
3377 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3378 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003379 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3380 } else {
3381 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3382 }
3383
Adam Cohen9bfdb762014-07-21 17:44:06 -07003384 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003385 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003386 revealView.setVisibility(View.VISIBLE);
3387 // We need to hide this view as the animation start will be posted.
3388 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003389
Adam Cohen9bfdb762014-07-21 17:44:06 -07003390 int width = revealView.getMeasuredWidth();
3391 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003392 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003393
Adam Cohen63f1ec02014-08-12 09:23:13 -07003394 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003395 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003396
Adam Cohen63f1ec02014-08-12 09:23:13 -07003397 // Get the y delta between the center of the page and the center of the all apps button
3398 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3399 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003400
Adam Cohen2854d252014-08-27 16:04:07 -07003401 float alpha = 0;
3402 float xDrift = 0;
3403 float yDrift = 0;
3404 if (material) {
3405 alpha = isWidgetTray ? 0.3f : 1f;
3406 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3407 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3408 } else {
3409 yDrift = 2 * height / 3;
3410 xDrift = 0;
3411 }
3412 final float initAlpha = alpha;
3413
Adam Cohen9bfdb762014-07-21 17:44:06 -07003414 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003415 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003416 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003417 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003418 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003419 PropertyValuesHolder panelDriftX =
3420 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003421
Adam Cohen2854d252014-08-27 16:04:07 -07003422 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3423 panelAlpha, panelDriftY, panelDriftX);
3424
Adam Cohen9bfdb762014-07-21 17:44:06 -07003425 panelAlphaAndDrift.setDuration(revealDuration);
3426 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003427
Adam Cohen9bfdb762014-07-21 17:44:06 -07003428 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003429
Adam Cohen4e243a22014-08-10 18:30:55 -07003430 if (page != null) {
3431 page.setVisibility(View.VISIBLE);
3432 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003433 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003434
Adam Cohen2854d252014-08-27 16:04:07 -07003435 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3436 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003437 pageDrift.setDuration(revealDuration);
3438 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003439 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003440 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003441
Adam Cohen63f1ec02014-08-12 09:23:13 -07003442 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003443 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003444 itemsAlpha.setDuration(revealDuration);
3445 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3446 itemsAlpha.setStartDelay(itemsAlphaStagger);
3447 mStateAnimation.play(itemsAlpha);
3448 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003449
Adam Cohen4e243a22014-08-10 18:30:55 -07003450 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3451 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003452 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003453 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003454 indicatorsAlpha.setDuration(revealDuration);
3455 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003456
Adam Cohen9bfdb762014-07-21 17:44:06 -07003457 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003458 final View allApps = getAllAppsButton();
3459 int allAppsButtonSize = LauncherAppState.getInstance().
3460 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3461 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003462 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003463 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003464 reveal.setDuration(revealDuration);
3465 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003466
3467 reveal.addListener(new AnimatorListenerAdapter() {
3468 public void onAnimationStart(Animator animation) {
3469 if (!isWidgetTray) {
3470 allApps.setVisibility(View.INVISIBLE);
3471 }
3472 }
3473 public void onAnimationEnd(Animator animation) {
3474 if (!isWidgetTray) {
3475 allApps.setVisibility(View.VISIBLE);
3476 }
3477 }
3478 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003479 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003480 }
Michael Jurka1899a362011-11-03 13:50:45 -07003481
Adam Cohen9bfdb762014-07-21 17:44:06 -07003482 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3483 @Override
3484 public void onAnimationEnd(Animator animation) {
3485 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3486 dispatchOnLauncherTransitionEnd(toView, animated, false);
3487
3488 revealView.setVisibility(View.INVISIBLE);
3489 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003490 if (page != null) {
3491 page.setLayerType(View.LAYER_TYPE_NONE, null);
3492 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003493 content.setPageBackgroundsVisible(true);
3494
3495 // Hide the search bar
3496 if (mSearchDropTargetBar != null) {
3497 mSearchDropTargetBar.hideSearchBar(false);
3498 }
3499 }
3500
Adam Cohen9bfdb762014-07-21 17:44:06 -07003501 });
3502
Adam Cohen6c5891a2014-07-09 23:53:15 -07003503 if (workspaceAnim != null) {
3504 mStateAnimation.play(workspaceAnim);
3505 }
Adam Cohen2854d252014-08-27 16:04:07 -07003506
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003507 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3508 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003509 final AnimatorSet stateAnimation = mStateAnimation;
3510 final Runnable startAnimRunnable = new Runnable() {
3511 public void run() {
3512 // Check that mStateAnimation hasn't changed while
3513 // we waited for a layout/draw pass
3514 if (mStateAnimation != stateAnimation)
3515 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003516 dispatchOnLauncherTransitionStart(fromView, animated, false);
3517 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003518
3519 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003520 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003521 for (int i = 0; i < layerViews.size(); i++) {
3522 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003523 if (v != null) {
3524 boolean attached = true;
3525 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3526 attached = v.isAttachedToWindow();
3527 }
3528 if (attached) v.buildLayer();
3529 }
Adam Cohen2854d252014-08-27 16:04:07 -07003530 }
3531 }
3532 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003533 }
3534 };
Adam Cohen2854d252014-08-27 16:04:07 -07003535 toView.bringToFront();
3536 toView.setVisibility(View.VISIBLE);
3537 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003538 } else {
3539 toView.setTranslationX(0.0f);
3540 toView.setTranslationY(0.0f);
3541 toView.setScaleX(1.0f);
3542 toView.setScaleY(1.0f);
3543 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003544 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003545
Daniel Sandlere4f98912013-06-25 15:13:26 -04003546 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003547 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003548 if (mSearchDropTargetBar != null) {
3549 mSearchDropTargetBar.hideSearchBar(false);
3550 }
Michael Jurkaabded662011-03-04 12:06:57 -08003551 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003552 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003553 dispatchOnLauncherTransitionStart(fromView, animated, false);
3554 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003555 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003556 dispatchOnLauncherTransitionStart(toView, animated, false);
3557 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003558 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003559 }
3560
3561 /**
3562 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003563 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003564 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003565 */
Adam Cohened307df2013-10-02 09:37:31 -07003566 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003567 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003568
Michael Jurkab3e22d92011-10-31 15:58:33 -07003569 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003570 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003571 mStateAnimation.cancel();
3572 mStateAnimation = null;
3573 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003574
Kenny Guyd794a3f2014-09-16 15:17:58 +01003575 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003576 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003577
Winson Chungf0ea4d32011-06-06 14:27:16 -07003578 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003579 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003580 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003581 final int itemsAlphaStagger =
3582 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003583
Winson Chungf0ea4d32011-06-06 14:27:16 -07003584 final float scaleFactor = (float)
3585 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3586 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003587 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003588 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003589 final ArrayList<View> layerViews = new ArrayList<View>();
3590
Adam Cohened307df2013-10-02 09:37:31 -07003591 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003592 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003593 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003594 } else if (toState == Workspace.State.SPRING_LOADED ||
3595 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003596 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003597 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003598 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003599
Adam Cohena38dc902014-09-07 17:48:55 +02003600 // If for some reason our views aren't initialized, don't animate
3601 boolean initialized = getAllAppsButton() != null;
3602
3603 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003604 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003605 if (workspaceAnim != null) {
3606 mStateAnimation.play(workspaceAnim);
3607 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003608
Adam Cohen9bfdb762014-07-21 17:44:06 -07003609 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3610 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003611
Adam Cohen9bfdb762014-07-21 17:44:06 -07003612 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003613
3614 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3615 int count = content.getChildCount();
3616 for (int i = 0; i < count; i++) {
3617 View child = content.getChildAt(i);
3618 if (child != page) {
3619 child.setVisibility(View.INVISIBLE);
3620 }
3621 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003622 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003623
Adam Cohen2854d252014-08-27 16:04:07 -07003624 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3625 // don't perform all these no-op animations. In particularly, this was causing
3626 // the all-apps button to pop in and out.
3627 if (fromView.getVisibility() == View.VISIBLE) {
3628 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3629 final boolean isWidgetTray =
3630 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003631
Adam Cohen2854d252014-08-27 16:04:07 -07003632 if (isWidgetTray) {
3633 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3634 } else {
3635 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003636 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003637
Adam Cohen2854d252014-08-27 16:04:07 -07003638 int width = revealView.getMeasuredWidth();
3639 int height = revealView.getMeasuredHeight();
3640 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3641
3642 // Hide the real page background, and swap in the fake one
3643 revealView.setVisibility(View.VISIBLE);
3644 content.setPageBackgroundsVisible(false);
3645
3646 final View allAppsButton = getAllAppsButton();
3647 revealView.setTranslationY(0);
3648 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3649 allAppsButton, null);
3650
3651 float xDrift = 0;
3652 float yDrift = 0;
3653 if (material) {
3654 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3655 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3656 } else {
3657 yDrift = 5 * height / 4;
3658 xDrift = 0;
3659 }
3660
3661 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3662 TimeInterpolator decelerateInterpolator = material ?
3663 new LogDecelerateInterpolator(100, 0) :
3664 new LogDecelerateInterpolator(30, 0);
3665
3666 // The vertical motion of the apps panel should be delayed by one frame
3667 // from the conceal animation in order to give the right feel. We correpsondingly
3668 // shorten the duration so that the slide and conceal end at the same time.
3669 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3670 0, yDrift);
3671 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3672 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3673 panelDriftY.setInterpolator(decelerateInterpolator);
3674 mStateAnimation.play(panelDriftY);
3675
3676 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3677 0, xDrift);
3678 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3679 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3680 panelDriftX.setInterpolator(decelerateInterpolator);
3681 mStateAnimation.play(panelDriftX);
3682
3683 if (isWidgetTray || !material) {
3684 float finalAlpha = material ? 0.4f : 0f;
3685 revealView.setAlpha(1f);
3686 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3687 1f, finalAlpha);
3688 panelAlpha.setDuration(revealDuration);
3689 panelAlpha.setInterpolator(material ? decelerateInterpolator :
3690 new AccelerateInterpolator(1.5f));
3691 mStateAnimation.play(panelAlpha);
3692 }
3693
3694 if (page != null) {
3695 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3696
3697 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3698 0, yDrift);
3699 page.setTranslationY(0);
3700 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3701 pageDrift.setInterpolator(decelerateInterpolator);
3702 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3703 mStateAnimation.play(pageDrift);
3704
3705 page.setAlpha(1f);
3706 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3707 itemsAlpha.setDuration(100);
3708 itemsAlpha.setInterpolator(decelerateInterpolator);
3709 mStateAnimation.play(itemsAlpha);
3710 }
3711
3712 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3713 pageIndicators.setAlpha(1f);
3714 ObjectAnimator indicatorsAlpha =
3715 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3716 indicatorsAlpha.setDuration(revealDuration);
3717 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3718 mStateAnimation.play(indicatorsAlpha);
3719
3720 width = revealView.getMeasuredWidth();
3721
3722 if (material) {
3723 if (!isWidgetTray) {
3724 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003725 }
Adam Cohen2854d252014-08-27 16:04:07 -07003726 int allAppsButtonSize = LauncherAppState.getInstance().
3727 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3728 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3729 Animator reveal =
3730 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3731 height / 2, revealRadius, finalRadius);
3732 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3733 reveal.setDuration(revealDuration);
3734 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003735
Adam Cohen2854d252014-08-27 16:04:07 -07003736 reveal.addListener(new AnimatorListenerAdapter() {
3737 public void onAnimationEnd(Animator animation) {
3738 revealView.setVisibility(View.INVISIBLE);
3739 if (!isWidgetTray) {
3740 allAppsButton.setVisibility(View.VISIBLE);
3741 }
3742 }
3743 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003744
Adam Cohen2854d252014-08-27 16:04:07 -07003745 mStateAnimation.play(reveal);
3746 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003747
Adam Cohen2854d252014-08-27 16:04:07 -07003748 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3749 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3750 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003751 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003752
3753 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003754 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003755 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003756 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003757 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3758 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003759 if (onCompleteRunnable != null) {
3760 onCompleteRunnable.run();
3761 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003762
3763 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003764 if (page != null) {
3765 page.setLayerType(View.LAYER_TYPE_NONE, null);
3766 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003767 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003768 // Unhide side pages
3769 int count = content.getChildCount();
3770 for (int i = 0; i < count; i++) {
3771 View child = content.getChildAt(i);
3772 child.setVisibility(View.VISIBLE);
3773 }
3774
3775 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003776 if (page != null) {
3777 page.setTranslationX(0);
3778 page.setTranslationY(0);
3779 page.setAlpha(1);
3780 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003781 content.setCurrentPage(content.getNextPage());
3782
Chet Haasebc2f0822012-10-26 17:59:53 -07003783 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003784 }
3785 });
3786
Adam Cohen2854d252014-08-27 16:04:07 -07003787 final AnimatorSet stateAnimation = mStateAnimation;
3788 final Runnable startAnimRunnable = new Runnable() {
3789 public void run() {
3790 // Check that mStateAnimation hasn't changed while
3791 // we waited for a layout/draw pass
3792 if (mStateAnimation != stateAnimation)
3793 return;
3794 dispatchOnLauncherTransitionStart(fromView, animated, false);
3795 dispatchOnLauncherTransitionStart(toView, animated, false);
3796
Kenny Guyd794a3f2014-09-16 15:17:58 +01003797 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003798 for (int i = 0; i < layerViews.size(); i++) {
3799 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003800 if (v != null) {
3801 boolean attached = true;
3802 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3803 attached = v.isAttachedToWindow();
3804 }
3805 if (attached) v.buildLayer();
3806 }
Adam Cohen2854d252014-08-27 16:04:07 -07003807 }
3808 }
3809 mStateAnimation.start();
3810 }
3811 };
3812 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003813 } else {
3814 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003815 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003816 dispatchOnLauncherTransitionStart(fromView, animated, true);
3817 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003818 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003819 dispatchOnLauncherTransitionStart(toView, animated, true);
3820 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003821 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003822 }
3823
Michael Jurkae326f182011-11-21 14:05:46 -08003824 @Override
3825 public void onTrimMemory(int level) {
3826 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003827 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003828 mAppsCustomizeTabHost.onTrimMemory();
3829 }
3830 }
3831
Adam Cohened307df2013-10-02 09:37:31 -07003832 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003833 showWorkspace(animated, null);
3834 }
3835
Adam Cohened307df2013-10-02 09:37:31 -07003836 protected void showWorkspace() {
3837 showWorkspace(true);
3838 }
3839
Adam Cohened66b2b2012-01-23 17:28:51 -08003840 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003841 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003842 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003843 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003844 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003845
Winson Chungc7d2b602012-05-16 17:02:20 -07003846 // Show the search bar (only animate if we were showing the drop target bar in spring
3847 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003848 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003849 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003850 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003851
Michael Jurkab3e22d92011-10-31 15:58:33 -07003852 // Set focus to the AppsCustomize button
3853 if (mAllAppsButton != null) {
3854 mAllAppsButton.requestFocus();
3855 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003856 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003857
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003858 // Change the state *after* we've called all the transition code
3859 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003860
Winson Chung5fb63472011-02-02 17:03:37 -08003861 // Resume the auto-advance of widgets
3862 mUserPresent = true;
3863 updateRunning();
3864
alanv1d4fde62012-10-17 13:15:47 -07003865 // Send an accessibility event to announce the context change
3866 getWindow().getDecorView()
3867 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003868
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003869 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003870 }
3871
Adam Cohened307df2013-10-02 09:37:31 -07003872 void showOverviewMode(boolean animated) {
3873 mWorkspace.setVisibility(View.VISIBLE);
3874 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3875 mState = State.WORKSPACE;
3876 onWorkspaceShown(animated);
3877 }
3878
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003879 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003880 }
3881
Winson Chung82963d52013-10-09 11:20:57 -07003882 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3883 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003884 if (mState != State.WORKSPACE) return;
3885
Winson Chung82963d52013-10-09 11:20:57 -07003886 if (resetPageToZero) {
3887 mAppsCustomizeTabHost.reset();
3888 }
Winson Chungc58497e2013-09-03 17:48:37 -07003889 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003890 mAppsCustomizeTabHost.post(new Runnable() {
3891 @Override
3892 public void run() {
3893 // We post this in-case the all apps view isn't yet constructed.
3894 mAppsCustomizeTabHost.requestFocus();
3895 }
3896 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003897
Michael Jurkab3e22d92011-10-31 15:58:33 -07003898 // Change the state *after* we've called all the transition code
3899 mState = State.APPS_CUSTOMIZE;
3900
3901 // Pause the auto-advance of widgets until we are out of AllApps
3902 mUserPresent = false;
3903 updateRunning();
3904 closeFolder();
3905
3906 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003907 getWindow().getDecorView()
3908 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003909 }
3910
Adam Cohen7777d962011-08-18 18:58:38 -07003911 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003912 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003913 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003914 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003915 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003916 }
Adam Cohen7777d962011-08-18 18:58:38 -07003917
Adam Cohenad4e15c2013-10-17 16:21:35 -07003918 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003919 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003920 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3921
Winson Chunge7a03942011-08-05 15:05:12 -07003922 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003923 @Override
3924 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003925 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003926 // Before we show workspace, hide all apps again because
3927 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3928 // clean up our state transition functions
3929 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003930 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003931 } else {
3932 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003933 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003934 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003935 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003936 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003937
Michael Jurkad3ef3062010-11-23 16:23:58 -08003938 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003939 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003940 final boolean animated = true;
3941 final boolean springLoaded = true;
3942 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003943 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003944 }
3945 // Otherwise, we are not in spring loaded mode, so don't do anything.
3946 }
3947
Michael Jurkab3e22d92011-10-31 15:58:33 -07003948 void lockAllApps() {
3949 // TODO
3950 }
3951
3952 void unlockAllApps() {
3953 // TODO
3954 }
3955
Winson Chungf0ea4d32011-06-06 14:27:16 -07003956 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003957 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003958 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003959 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003960 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003961 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003962 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003963 int duration = 0;
3964 if (mSearchDropTargetBar != null) {
3965 duration = mSearchDropTargetBar.getTransitionOutDuration();
3966 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003967 mHotseat.animate().alpha(0f).setDuration(duration);
3968 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003969 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003970 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003971 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003972 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003973 }
3974
Patrick Dubroy5f445422011-02-18 14:35:21 -08003975 /**
3976 * Add an item from all apps or customize onto the given workspace screen.
3977 * If layout is null, add to the current screen.
3978 */
3979 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003980 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003981 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003982 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003983 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003984
Winson Chungdff8ebb2011-09-08 17:25:31 -07003985 /** Maps the current orientation to an index for referencing orientation correct global icons */
3986 private int getCurrentOrientationIndexForGlobalIcons() {
3987 // default - 0, landscape - 1
3988 switch (getResources().getConfiguration().orientation) {
3989 case Configuration.ORIENTATION_LANDSCAPE:
3990 return 1;
3991 default:
3992 return 0;
3993 }
3994 }
3995
Michael Jurkaae65ee32012-04-30 11:45:54 -07003996 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003997 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003998 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003999 // Look for the toolbar icon specified in the activity meta-data
4000 Bundle metaData = packageManager.getActivityInfo(
4001 activityName, PackageManager.GET_META_DATA).metaData;
4002 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07004003 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004004 if (iconResId != 0) {
4005 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004006 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004007 }
4008 }
4009 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07004010 // This can happen if the activity defines an invalid drawable
4011 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
4012 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01004013 } catch (Resources.NotFoundException nfe) {
4014 // This can happen if the activity defines an invalid drawable
4015 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
4016 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004017 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004018 return null;
4019 }
4020
4021 // if successful in getting icon, return it; otherwise, set button to use default drawable
4022 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004023 int buttonId, ComponentName activityName, int fallbackDrawableId,
4024 String toolbarResourceName) {
4025 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07004026 Resources r = getResources();
4027 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
4028 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004029
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004030 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004031 // If we were unable to find the icon via the meta-data, use a generic one
4032 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07004033 toolbarIcon = r.getDrawable(fallbackDrawableId);
4034 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004035 if (button != null) {
4036 button.setCompoundDrawables(toolbarIcon, null, null, null);
4037 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004038 return null;
4039 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07004040 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004041 if (button != null) {
4042 button.setCompoundDrawables(toolbarIcon, null, null, null);
4043 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004044 return toolbarIcon.getConstantState();
4045 }
4046 }
4047
4048 // if successful in getting icon, return it; otherwise, set button to use default drawable
4049 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004050 int buttonId, ComponentName activityName, int fallbackDrawableId,
4051 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004052 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07004053 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004054
Michael Jurka19e0fc52011-07-22 18:00:21 -07004055 if (button != null) {
4056 // If we were unable to find the icon via the meta-data, use a
4057 // generic one
4058 if (toolbarIcon == null) {
4059 button.setImageResource(fallbackDrawableId);
4060 } else {
4061 button.setImageDrawable(toolbarIcon);
4062 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004063 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07004064
4065 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
4066
Michael Jurka0423dcf2010-10-05 14:56:18 -07004067 }
4068
Winson Chung1b884092012-06-08 17:13:02 -07004069 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004070 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07004071 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004072 }
4073
Michael Jurkae7bf83b2010-12-14 18:02:21 -08004074 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08004075 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08004076 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08004077 }
4078
Winson Chungbb185bd2011-11-21 12:31:42 -08004079 private void invalidatePressedFocusedStates(View container, View button) {
4080 if (container instanceof HolographicLinearLayout) {
4081 HolographicLinearLayout layout = (HolographicLinearLayout) container;
4082 layout.invalidatePressedFocusedStates();
4083 } else if (button instanceof HolographicImageView) {
4084 HolographicImageView view = (HolographicImageView) button;
4085 view.invalidatePressedFocusedStates();
4086 }
4087 }
4088
Cristina Stancu476493b2013-08-07 17:20:14 +01004089 public View getQsbBar() {
Adam Cohen9211d422014-10-07 18:14:53 -07004090 if (mLauncherCallbacks != null) {
4091 View qsb = mLauncherCallbacks.getQsbBar();
4092 if (qsb != null) {
4093 return qsb;
4094 }
4095 }
4096
Adam Cohen24ce0b32014-01-14 16:18:14 -08004097 if (mQsb == null) {
4098 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
4099 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01004100 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08004101 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01004102 }
4103
4104 protected boolean updateGlobalSearchIcon() {
Adam Cohen9211d422014-10-07 18:14:53 -07004105 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4106 return true;
4107 }
4108
Winson Chungc51db6a2011-10-05 11:44:49 -07004109 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004110 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07004111 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004112 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004113
Winson Chung1cad91e2011-05-25 17:41:01 -07004114 final SearchManager searchManager =
4115 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4116 ComponentName activityName = searchManager.getGlobalSearchActivity();
4117 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004118 int coi = getCurrentOrientationIndexForGlobalIcons();
4119 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004120 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
4121 TOOLBAR_SEARCH_ICON_METADATA_NAME);
4122 if (sGlobalSearchIcon[coi] == null) {
4123 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4124 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
4125 TOOLBAR_ICON_METADATA_NAME);
4126 }
4127
Winson Chungc51db6a2011-10-05 11:44:49 -07004128 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
4129 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08004130 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004131 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004132 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004133 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07004134 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
4135 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004136 if (searchButton != null) searchButton.setVisibility(View.GONE);
4137 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004138 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004139 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004140 }
4141 }
4142
Cristina Stancu476493b2013-08-07 17:20:14 +01004143 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Adam Cohen9211d422014-10-07 18:14:53 -07004144 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) return;
Winson Chungbb185bd2011-11-21 12:31:42 -08004145 final View searchButtonContainer = findViewById(R.id.search_button_container);
4146 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004147 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004148 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004149 }
4150
Cristina Stancu476493b2013-08-07 17:20:14 +01004151 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Adam Cohen9211d422014-10-07 18:14:53 -07004152 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4153 return true;
4154 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004155 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004156 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004157
Winson Chungc51db6a2011-10-05 11:44:49 -07004158 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07004159 final SearchManager searchManager =
4160 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4161 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
4162
4163 ComponentName activityName = null;
4164 if (globalSearchActivity != null) {
4165 // Check if the global search activity handles voice search
4166 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4167 intent.setPackage(globalSearchActivity.getPackageName());
4168 activityName = intent.resolveActivity(getPackageManager());
4169 }
4170
4171 if (activityName == null) {
4172 // Fallback: check if an activity other than the global search activity
4173 // resolves this
4174 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4175 activityName = intent.resolveActivity(getPackageManager());
4176 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004177 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004178 int coi = getCurrentOrientationIndexForGlobalIcons();
4179 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004180 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4181 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
4182 if (sVoiceSearchIcon[coi] == null) {
4183 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4184 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4185 TOOLBAR_ICON_METADATA_NAME);
4186 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004187 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07004188 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07004189 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08004190 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004191 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004192 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07004193 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004194 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004195 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004196 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004197 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07004198 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004199
Cristina Stancu476493b2013-08-07 17:20:14 +01004200 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Adam Cohen9211d422014-10-07 18:14:53 -07004201 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4202 return;
4203 }
4204
Winson Chungbb185bd2011-11-21 12:31:42 -08004205 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
4206 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004207 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004208 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004209 }
4210
Winson Chung5841efa2013-09-30 18:06:44 -07004211 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Adam Cohen9211d422014-10-07 18:14:53 -07004212 if (mLauncherCallbacks != null) {
4213 forceDisableVoiceButtonProxy |= mLauncherCallbacks.forceDisableVoiceButtonProxy();
4214 }
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004215 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
4216 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07004217 boolean visible = !forceDisableVoiceButtonProxy &&
4218 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004219 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004220 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004221 }
4222 }
Winson Chung5841efa2013-09-30 18:06:44 -07004223
4224 /**
4225 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
4226 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
4227 */
4228 public void disableVoiceButtonProxy(boolean disabled) {
4229 updateVoiceButtonProxyVisible(disabled);
4230 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08004231
Michael Jurkad7c28052012-04-27 15:43:36 -07004232 @Override
4233 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004234 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004235 final List<CharSequence> text = event.getText();
4236 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004237 // Populate event with a fake title based on the current state.
4238 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004239 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004240 } else {
4241 text.add(getString(R.string.all_apps_home_button_label));
4242 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004243 return result;
4244 }
4245
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004246 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004247 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004248 */
4249 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4250 @Override
4251 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004252 closeSystemDialogs();
4253 }
4254 }
4255
4256 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004257 * Receives notifications whenever the appwidgets are reset.
4258 */
4259 private class AppWidgetResetObserver extends ContentObserver {
4260 public AppWidgetResetObserver() {
4261 super(new Handler());
4262 }
4263
4264 @Override
4265 public void onChange(boolean selfChange) {
4266 onAppWidgetReset();
4267 }
4268 }
4269
4270 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004271 * If the activity is currently paused, signal that we need to run the passed Runnable
4272 * in onResume.
4273 *
4274 * This needs to be called from incoming places where resources might have been loaded
4275 * while we are paused. That is becaues the Configuration might be wrong
4276 * when we're not running, and if it comes back to what it was when we
4277 * were paused, we are not restarted.
4278 *
4279 * Implementation of the method from LauncherModel.Callbacks.
4280 *
4281 * @return true if we are currently paused. The caller might be able to
4282 * skip some work in that case since we will come back again.
4283 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004284 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004285 if (mPaused) {
4286 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004287 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004288 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004289 }
4290 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004291 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004292 return true;
4293 } else {
4294 return false;
4295 }
4296 }
4297
Michael Jurkac402cd92013-05-20 15:49:32 +02004298 private boolean waitUntilResume(Runnable run) {
4299 return waitUntilResume(run, false);
4300 }
4301
Michael Jurka1e2f4652013-07-08 18:03:46 -07004302 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004303 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004304 }
4305
Michael Jurka7607c2f2013-04-03 14:33:19 -07004306 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004307 * If the activity is currently paused, signal that we need to re-run the loader
4308 * in onResume.
4309 *
4310 * This needs to be called from incoming places where resources might have been loaded
4311 * while we are paused. That is becaues the Configuration might be wrong
4312 * when we're not running, and if it comes back to what it was when we
4313 * were paused, we are not restarted.
4314 *
4315 * Implementation of the method from LauncherModel.Callbacks.
4316 *
4317 * @return true if we are currently paused. The caller might be able to
4318 * skip some work in that case since we will come back again.
4319 */
4320 public boolean setLoadOnResume() {
4321 if (mPaused) {
4322 Log.i(TAG, "setLoadOnResume");
4323 mOnResumeNeedsLoad = true;
4324 return true;
4325 } else {
4326 return false;
4327 }
4328 }
4329
4330 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004331 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004332 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004333 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004334 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004335 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004336 } else {
4337 return SCREEN_COUNT / 2;
4338 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004339 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004340
Joe Onorato9c1289c2009-08-17 11:03:03 -04004341 /**
4342 * Refreshes the shortcuts shown on the workspace.
4343 *
4344 * Implementation of the method from LauncherModel.Callbacks.
4345 */
4346 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004347 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004348
Michael Jurka7607c2f2013-04-03 14:33:19 -07004349 // If we're starting binding all over again, clear any bind calls we'd postponed in
4350 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4351 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004352 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004353
Winson Chungd64d1762013-08-20 14:37:16 -07004354 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004355 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004356 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004357
Michael Jurka05bf6442011-11-30 20:28:53 -08004358 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004359 if (mHotseat != null) {
4360 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004361 }
4362 }
4363
Adam Cohendcd297f2013-06-18 13:13:40 -07004364 @Override
4365 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004366 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004367
Adam Cohen5084cba2013-09-03 12:01:16 -07004368 // If there are no screens, we need to have an empty screen
4369 if (orderedScreenIds.size() == 0) {
4370 mWorkspace.addExtraEmptyScreen();
4371 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004372
4373 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004374 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004375 if (hasCustomContentToLeft()) {
4376 mWorkspace.createCustomContentContainer();
4377 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004378 }
Winson Chung64359a52013-07-08 17:17:08 -07004379 }
4380
4381 @Override
4382 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004383 // Log to disk
4384 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4385 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4386 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004387 int count = orderedScreenIds.size();
4388 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004389 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004390 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004391 }
4392
Adam Cohen39a06042013-07-19 14:30:12 -07004393 private boolean shouldShowWeightWatcher() {
4394 String spKey = LauncherAppState.getSharedPreferencesKey();
4395 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004396 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004397
4398 return show;
4399 }
4400
4401 private void toggleShowWeightWatcher() {
4402 String spKey = LauncherAppState.getSharedPreferencesKey();
4403 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4404 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4405
4406 show = !show;
4407
4408 SharedPreferences.Editor editor = sp.edit();
4409 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4410 editor.commit();
4411
4412 if (mWeightWatcher != null) {
4413 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4414 }
4415 }
4416
Winson Chungd64d1762013-08-20 14:37:16 -07004417 public void bindAppsAdded(final ArrayList<Long> newScreens,
4418 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004419 final ArrayList<ItemInfo> addAnimated,
4420 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004421 Runnable r = new Runnable() {
4422 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004423 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004424 }
4425 };
4426 if (waitUntilResume(r)) {
4427 return;
4428 }
4429
Winson Chungd64d1762013-08-20 14:37:16 -07004430 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004431 if (newScreens != null) {
4432 bindAddScreens(newScreens);
4433 }
Winson Chungd64d1762013-08-20 14:37:16 -07004434
4435 // We add the items without animation on non-visible pages, and with
4436 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004437 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004438 bindItems(addNotAnimated, 0,
4439 addNotAnimated.size(), false);
4440 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004441 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004442 bindItems(addAnimated, 0,
4443 addAnimated.size(), true);
4444 }
Winson Chungc58497e2013-09-03 17:48:37 -07004445
Winson Chung87412982013-10-03 18:34:14 -07004446 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004447 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004448
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004449 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004450 addedApps != null && mAppsCustomizeContent != null) {
4451 mAppsCustomizeContent.addApps(addedApps);
4452 }
Winson Chungd64d1762013-08-20 14:37:16 -07004453 }
4454
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004455 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004456 * Bind the items start-end from the list.
4457 *
4458 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004459 */
Winson Chung64359a52013-07-08 17:17:08 -07004460 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4461 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004462 Runnable r = new Runnable() {
4463 public void run() {
4464 bindItems(shortcuts, start, end, forceAnimateIcons);
4465 }
4466 };
4467 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004468 return;
4469 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004470
Winson Chungf0c6ae02012-03-21 16:10:31 -07004471 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004472 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4473 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004474 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004475 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004476 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004477 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004478 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004479
4480 // Short circuit if we are loading dock items for a configuration which has no dock
4481 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4482 mHotseat == null) {
4483 continue;
4484 }
4485
Joe Onorato9c1289c2009-08-17 11:03:03 -04004486 switch (item.itemType) {
4487 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4488 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004489 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004490 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004491
Winson Chung64359a52013-07-08 17:17:08 -07004492 /*
4493 * TODO: FIX collision case
4494 */
Winson Chungce376632013-07-11 16:12:41 -07004495 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4496 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4497 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004498 View v = cl.getChildAt(item.cellX, item.cellY);
4499 Object tag = v.getTag();
4500 String desc = "Collision while binding workspace item: " + item
4501 + ". Collides with " + tag;
4502 if (LauncherAppState.isDogfoodBuild()) {
4503 throw (new RuntimeException(desc));
4504 } else {
4505 Log.d(TAG, desc);
4506 }
Winson Chungce376632013-07-11 16:12:41 -07004507 }
Winson Chung64359a52013-07-08 17:17:08 -07004508 }
4509
Adam Cohendcd297f2013-06-18 13:13:40 -07004510 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4511 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004512 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004513 // Animate all the applications up now
4514 shortcut.setAlpha(0f);
4515 shortcut.setScaleX(0f);
4516 shortcut.setScaleY(0f);
4517 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004518 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004519 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004520 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004521 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004522 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004523 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004524 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004525 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4526 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004527 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004528 default:
4529 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004530 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004531 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004532
Winson Chung997a9232013-07-24 15:33:46 -07004533 if (animateIcons) {
4534 // Animate to the correct page
4535 if (newShortcutsScreenId > -1) {
4536 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004537 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004538 final Runnable startBounceAnimRunnable = new Runnable() {
4539 public void run() {
4540 anim.playTogether(bounceAnims);
4541 anim.start();
4542 }
4543 };
Winson Chung997a9232013-07-24 15:33:46 -07004544 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004545 // We post the animation slightly delayed to prevent slowdowns
4546 // when we are loading right after we return to launcher.
4547 mWorkspace.postDelayed(new Runnable() {
4548 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004549 if (mWorkspace != null) {
4550 mWorkspace.snapToPage(newScreenIndex);
4551 mWorkspace.postDelayed(startBounceAnimRunnable,
4552 NEW_APPS_ANIMATION_DELAY);
4553 }
Winson Chung94d67682013-09-25 16:29:40 -07004554 }
4555 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004556 } else {
4557 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004558 }
4559 }
Winson Chung64359a52013-07-08 17:17:08 -07004560 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004561 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004562 }
4563
Joe Onorato9c1289c2009-08-17 11:03:03 -04004564 /**
4565 * Implementation of the method from LauncherModel.Callbacks.
4566 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004567 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004568 Runnable r = new Runnable() {
4569 public void run() {
4570 bindFolders(folders);
4571 }
4572 };
4573 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004574 return;
4575 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004576 sFolders.clear();
4577 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004578 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004579
4580 /**
4581 * Add the views for a widget to the workspace.
4582 *
4583 * Implementation of the method from LauncherModel.Callbacks.
4584 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004585 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004586 Runnable r = new Runnable() {
4587 public void run() {
4588 bindAppWidget(item);
4589 }
4590 };
4591 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004592 return;
4593 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004594
Daniel Sandler843e8602010-06-07 14:59:01 -04004595 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4596 if (DEBUG_WIDGETS) {
4597 Log.d(TAG, "bindAppWidget: " + item);
4598 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004599 final Workspace workspace = mWorkspace;
4600
Sunny Goyalff572272014-07-23 13:58:07 -07004601 AppWidgetProviderInfo appWidgetInfo;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004602 if (!mIsSafeModeEnabled
4603 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4604 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004605
4606 appWidgetInfo = mModel.findAppWidgetProviderInfoWithComponent(this, item.providerName);
4607 if (appWidgetInfo == null) {
4608 if (DEBUG_WIDGETS) {
4609 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4610 + " belongs to component " + item.providerName
4611 + ", as the povider is null");
4612 }
4613 LauncherModel.deleteItemFromDatabase(this, item);
4614 return;
4615 }
4616 // Note: This assumes that the id remap broadcast is received before this step.
4617 // If that is not the case, the id remap will be ignored and user may see the
4618 // click to setup view.
4619 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null, null);
4620 pendingInfo.spanX = item.spanX;
4621 pendingInfo.spanY = item.spanY;
4622 pendingInfo.minSpanX = item.minSpanX;
4623 pendingInfo.minSpanY = item.minSpanY;
4624 Bundle options =
4625 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4626
Sunny Goyalff572272014-07-23 13:58:07 -07004627 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004628 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4629 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004630
4631 // TODO consider showing a permission dialog when the widget is clicked.
4632 if (!success) {
4633 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4634 if (DEBUG_WIDGETS) {
4635 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4636 + " belongs to component " + item.providerName
4637 + ", as the launcher is unable to bing a new widget id");
4638 }
4639 LauncherModel.deleteItemFromDatabase(this, item);
4640 return;
4641 }
4642
4643 item.appWidgetId = newWidgetId;
4644
4645 // If the widget has a configure activity, it is still needs to set it up, otherwise
4646 // the widget is ready to go.
4647 item.restoreStatus = (appWidgetInfo.configure == null)
4648 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4649 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4650
4651 LauncherModel.updateItemInDatabase(this, item);
4652 }
4653
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004654 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004655 final int appWidgetId = item.appWidgetId;
4656 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4657 if (DEBUG_WIDGETS) {
4658 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4659 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004660
Sunny Goyal651077b2014-06-30 14:15:31 -07004661 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4662 } else {
4663 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004664 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4665 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004666 view.updateIcon(mIconCache);
4667 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004668 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004669 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004670 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004671
Joe Onorato9c1289c2009-08-17 11:03:03 -04004672 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004673 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004674
Adam Cohendcd297f2013-06-18 13:13:40 -07004675 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004676 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004677 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4678
Joe Onorato9c1289c2009-08-17 11:03:03 -04004679 workspace.requestLayout();
4680
Daniel Sandler843e8602010-06-07 14:59:01 -04004681 if (DEBUG_WIDGETS) {
4682 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4683 + (SystemClock.uptimeMillis()-start) + "ms");
4684 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004685 }
4686
Sunny Goyalff572272014-07-23 13:58:07 -07004687 /**
4688 * Restores a pending widget.
4689 *
4690 * @param appWidgetId The app widget id
4691 * @param cellInfo The position on screen where to create the widget.
4692 */
4693 private void completeRestoreAppWidget(final int appWidgetId) {
4694 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4695 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4696 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4697 return;
4698 }
4699
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004700 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004701 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4702
4703 mWorkspace.reinflateWidgetsIfNecessary();
4704 LauncherModel.updateItemInDatabase(this, info);
4705 }
4706
Adam Cohen1462de32012-07-24 22:34:36 -07004707 public void onPageBoundSynchronously(int page) {
4708 mSynchronouslyBoundPages.add(page);
4709 }
4710
Joe Onorato9c1289c2009-08-17 11:03:03 -04004711 /**
4712 * Callback saying that there aren't any more items to bind.
4713 *
4714 * Implementation of the method from LauncherModel.Callbacks.
4715 */
Adam Cohene25af792013-06-06 23:08:25 -07004716 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004717 Runnable r = new Runnable() {
4718 public void run() {
4719 finishBindingItems(upgradePath);
4720 }
4721 };
4722 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004723 return;
4724 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004725 if (mSavedState != null) {
4726 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004727 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004728 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004729 mSavedState = null;
4730 }
4731
Adam Cohen1462de32012-07-24 22:34:36 -07004732 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004733
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004734 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004735 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004736
4737 // If we received the result of any pending adds while the loader was running (e.g. the
4738 // widget configuration forced an orientation change), process them now.
4739 if (sPendingAddItem != null) {
4740 final long screenId = completeAdd(sPendingAddItem);
4741
4742 // TODO: this moves the user to the page where the pending item was added. Ideally,
4743 // the screen would be guaranteed to exist after bind, and the page would be set through
4744 // the workspace restore process.
4745 mWorkspace.post(new Runnable() {
4746 @Override
4747 public void run() {
4748 mWorkspace.snapToScreenId(screenId);
4749 }
4750 });
4751 sPendingAddItem = null;
4752 }
4753
Adam Cohene25af792013-06-06 23:08:25 -07004754 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004755 mWorkspace.getUniqueComponents(true, null);
4756 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004757 }
Sunny Goyale755d462014-07-22 13:48:29 -07004758 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004759
4760 if (mLauncherCallbacks != null) {
4761 mLauncherCallbacks.finishBindingItems(upgradePath);
4762 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004763 }
4764
Adam Cohen3ed316a2014-07-23 18:21:20 -07004765 private void sendLoadingCompleteBroadcastIfNecessary() {
4766 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4767 String permission =
4768 getResources().getString(R.string.receive_first_load_broadcast_permission);
4769 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4770 sendBroadcast(intent, permission);
4771 SharedPreferences.Editor editor = mSharedPrefs.edit();
4772 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4773 editor.apply();
4774 }
4775 }
4776
Winson Chunga0b7e862013-09-05 16:03:15 -07004777 public boolean isAllAppsButtonRank(int rank) {
4778 if (mHotseat != null) {
4779 return mHotseat.isAllAppsButtonRank(rank);
4780 }
4781 return false;
4782 }
4783
Winson Chunga2413752012-04-03 14:22:34 -07004784 private boolean canRunNewAppsAnimation() {
4785 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4786 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4787 }
4788
Winson Chungc9168342013-06-26 14:54:55 -07004789 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4790 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4791 PropertyValuesHolder.ofFloat("alpha", 1f),
4792 PropertyValuesHolder.ofFloat("scaleX", 1f),
4793 PropertyValuesHolder.ofFloat("scaleY", 1f));
4794 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4795 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4796 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4797 return bounceAnim;
4798 }
4799
Adam Cohen27772732013-11-11 14:00:56 +00004800 public boolean useVerticalBarLayout() {
4801 return LauncherAppState.getInstance().getDynamicGrid().
4802 getDeviceProfile().isVerticalBarLayout();
4803 }
4804
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004805 protected Rect getSearchBarBounds() {
4806 return LauncherAppState.getInstance().getDynamicGrid().
4807 getDeviceProfile().getSearchBarBounds();
4808 }
4809
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004810 @Override
4811 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004812 boolean searchVisible = updateGlobalSearchIcon();
4813 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004814 if (mSearchDropTargetBar != null) {
4815 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4816 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004817 }
4818
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004819 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004820 * Add the icons for all apps.
4821 *
4822 * Implementation of the method from LauncherModel.Callbacks.
4823 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004824 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004825 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004826 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4827 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4828 getHotseat().addAllAppsFolder(mIconCache, apps,
4829 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4830 }
4831 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004832 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004833 if (mAppsCustomizeContent != null) {
4834 mAppsCustomizeContent.onPackagesUpdated(
4835 LauncherModel.getSortedWidgetsAndShortcuts(this));
4836 }
Winson Chungc58497e2013-09-03 17:48:37 -07004837 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004838 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004839 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004840 mAppsCustomizeContent.onPackagesUpdated(
4841 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004842 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004843 }
Adam Cohen9211d422014-10-07 18:14:53 -07004844 if (mLauncherCallbacks != null) {
4845 mLauncherCallbacks.bindAllApplications(apps);
4846 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004847 }
4848
4849 /**
4850 * A package was updated.
4851 *
4852 * Implementation of the method from LauncherModel.Callbacks.
4853 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004854 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004855 Runnable r = new Runnable() {
4856 public void run() {
4857 bindAppsUpdated(apps);
4858 }
4859 };
4860 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004861 return;
4862 }
4863
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004864 if (mWorkspace != null) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004865 mWorkspace.updateShortcutsAndWidgets(apps);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004866 }
Winson Chungc58497e2013-09-03 17:48:37 -07004867
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004868 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004869 mAppsCustomizeContent != null) {
4870 mAppsCustomizeContent.updateApps(apps);
4871 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004872 }
4873
4874 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004875 * Some shortcuts were updated in the background.
4876 *
4877 * Implementation of the method from LauncherModel.Callbacks.
4878 */
4879 public void bindShortcutsUpdated(final ArrayList<ShortcutInfo> shortcuts) {
4880 Runnable r = new Runnable() {
4881 public void run() {
4882 bindShortcutsUpdated(shortcuts);
4883 }
4884 };
4885 if (waitUntilResume(r)) {
4886 return;
4887 }
4888
4889 if (mWorkspace != null) {
4890 mWorkspace.updateShortcuts(shortcuts);
4891 }
4892 }
4893
4894 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004895 * Update the state of a package, typically related to install state.
4896 *
4897 * Implementation of the method from LauncherModel.Callbacks.
4898 */
Sunny Goyale755d462014-07-22 13:48:29 -07004899 @Override
4900 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004901 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004902 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004903 }
4904 }
4905
4906 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004907 * Update the label and icon of all the icons in a package
4908 *
4909 * Implementation of the method from LauncherModel.Callbacks.
4910 */
4911 @Override
4912 public void updatePackageBadge(String packageName) {
4913 if (mWorkspace != null) {
4914 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4915 }
4916 }
4917
4918 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004919 * A package was uninstalled. We take both the super set of packageNames
4920 * in addition to specific applications to remove, the reason being that
4921 * this can be called when a package is updated as well. In that scenario,
4922 * we only remove specific components from the workspace, where as
4923 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004924 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004925 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004926 * Implementation of the method from LauncherModel.Callbacks.
4927 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004928 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004929 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004930 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004931 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004932 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004933 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004934 }
Winson Chungd64d1762013-08-20 14:37:16 -07004935 };
4936 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004937 return;
4938 }
4939
Sunny Goyal1a745e82014-10-02 15:58:31 -07004940 if (reason == 0) {
4941 if (!packageNames.isEmpty()) {
4942 mWorkspace.removeItemsByPackageName(packageNames, user);
4943 }
4944 if (!appInfos.isEmpty()) {
4945 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
4946 }
4947 } else {
4948 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004949 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004950
Winson Chunga1820962011-10-03 16:31:06 -07004951 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004952 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004953
Winson Chungdf95eb12013-10-16 14:57:07 -07004954 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004955 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004956 mAppsCustomizeContent != null) {
4957 mAppsCustomizeContent.removeApps(appInfos);
4958 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004959 }
Joe Onoratobe386092009-11-17 17:32:16 -08004960
4961 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004962 * A number of packages were updated.
4963 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004964 private ArrayList<Object> mWidgetsAndShortcuts;
4965 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004966 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004967 bindPackagesUpdated(mWidgetsAndShortcuts);
4968 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004969 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004970 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004971 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4972 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4973 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004974 return;
4975 }
4976
Winson Chung64359a52013-07-08 17:17:08 -07004977 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004978 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004979 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004980 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004981 }
4982
Winson Chung400438b2011-01-16 17:53:48 -08004983 private int mapConfigurationOriActivityInfoOri(int configOri) {
4984 final Display d = getWindowManager().getDefaultDisplay();
4985 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4986 switch (d.getRotation()) {
4987 case Surface.ROTATION_0:
4988 case Surface.ROTATION_180:
4989 // We are currently in the same basic orientation as the natural orientation
4990 naturalOri = configOri;
4991 break;
4992 case Surface.ROTATION_90:
4993 case Surface.ROTATION_270:
4994 // We are currently in the other basic orientation to the natural orientation
4995 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4996 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4997 break;
4998 }
4999
5000 int[] oriMap = {
5001 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
5002 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
5003 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
5004 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
5005 };
5006 // Since the map starts at portrait, we need to offset if this device's natural orientation
5007 // is landscape.
5008 int indexOffset = 0;
5009 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
5010 indexOffset = 1;
5011 }
5012 return oriMap[(d.getRotation() + indexOffset) % 4];
5013 }
Adam Cohen446e9402011-09-15 18:21:21 -07005014
Winson Chung4b919f82012-05-01 10:44:08 -07005015 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08005016 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07005017 getResources().getBoolean(R.bool.allow_rotation);
5018 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08005019 }
Winson Chung4b919f82012-05-01 10:44:08 -07005020 public void lockScreenOrientation() {
5021 if (isRotationEnabled()) {
5022 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
5023 .getConfiguration().orientation));
5024 }
5025 }
5026 public void unlockScreenOrientation(boolean immediate) {
5027 if (isRotationEnabled()) {
5028 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07005029 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07005030 } else {
5031 mHandler.postDelayed(new Runnable() {
5032 public void run() {
5033 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
5034 }
5035 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07005036 }
Winson Chung4b919f82012-05-01 10:44:08 -07005037 }
Winson Chung400438b2011-01-16 17:53:48 -08005038 }
5039
Winson Chunge43a1e72014-01-15 10:33:02 -08005040 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07005041 if (mLauncherCallbacks != null) {
5042 return mLauncherCallbacks.isLauncherPreinstalled();
5043 }
Winson Chunge43a1e72014-01-15 10:33:02 -08005044 PackageManager pm = getPackageManager();
5045 try {
5046 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
5047 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
5048 return true;
5049 } else {
5050 return false;
5051 }
5052 } catch (NameNotFoundException e) {
5053 e.printStackTrace();
5054 return false;
5055 }
5056 }
5057
Adam Cohenea90f832014-05-21 15:03:34 -07005058 /**
5059 * This method indicates whether or not we should suggest default wallpaper dimensions
5060 * when our wallpaper cropper was not yet used to set a wallpaper.
5061 */
5062 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07005063 if (mLauncherCallbacks != null) {
5064 return mLauncherCallbacks.overrideWallpaperDimensions();
5065 }
Adam Cohenea90f832014-05-21 15:03:34 -07005066 return true;
5067 }
5068
Adam Cohen432609a2014-03-13 17:03:22 -07005069 /**
5070 * To be overridden by subclasses to indicate that there is an activity to launch
5071 * before showing the standard launcher experience.
5072 */
5073 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07005074 if (mLauncherCallbacks != null) {
5075 return mLauncherCallbacks.hasFirstRunActivity();
5076 }
Adam Cohen432609a2014-03-13 17:03:22 -07005077 return false;
5078 }
5079
5080 /**
5081 * To be overridden by subclasses to launch any first run activity
5082 */
5083 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07005084 if (mLauncherCallbacks != null) {
5085 return mLauncherCallbacks.getFirstRunActivity();
5086 }
Adam Cohen432609a2014-03-13 17:03:22 -07005087 return null;
5088 }
5089
Winson Chunga6945242014-01-08 14:04:34 -08005090 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08005091 return !ActivityManager.isRunningInTestHarness() &&
5092 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08005093 }
5094
Adam Cohen4a9423d2014-03-28 14:22:31 -07005095 protected boolean hasRunFirstRunActivity() {
5096 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
5097 }
5098
Adam Cohen432609a2014-03-13 17:03:22 -07005099 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08005100 if (shouldRunFirstRunActivity() &&
5101 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08005102 Intent firstRunIntent = getFirstRunActivity();
5103 if (firstRunIntent != null) {
5104 startActivity(firstRunIntent);
5105 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07005106 return true;
Winson Chunga6945242014-01-08 14:04:34 -08005107 }
5108 }
Adam Cohen432609a2014-03-13 17:03:22 -07005109 return false;
Winson Chunga6945242014-01-08 14:04:34 -08005110 }
5111
5112 private void markFirstRunActivityShown() {
5113 SharedPreferences.Editor editor = mSharedPrefs.edit();
5114 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
5115 editor.apply();
5116 }
5117
Adam Cohen432609a2014-03-13 17:03:22 -07005118 /**
5119 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
5120 * screen that must be displayed and dismissed.
5121 */
5122 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07005123 if (mLauncherCallbacks != null) {
5124 return mLauncherCallbacks.hasDismissableIntroScreen();
5125 }
Adam Cohen432609a2014-03-13 17:03:22 -07005126 return false;
5127 }
5128
5129 /**
5130 * Full screen intro screen to be shown and dismissed before the launcher can be used.
5131 */
5132 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07005133 if (mLauncherCallbacks != null) {
5134 return mLauncherCallbacks.getIntroScreen();
5135 }
Adam Cohen432609a2014-03-13 17:03:22 -07005136 return null;
5137 }
5138
5139 /**
5140 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
5141 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
5142 */
5143 private boolean shouldShowIntroScreen() {
5144 return hasDismissableIntroScreen() &&
5145 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
5146 }
5147
5148 protected void showIntroScreen() {
5149 View introScreen = getIntroScreen();
5150 changeWallpaperVisiblity(false);
5151 if (introScreen != null) {
5152 mDragLayer.showOverlayView(introScreen);
5153 }
5154 }
5155
5156 public void dismissIntroScreen() {
5157 markIntroScreenDismissed();
5158 if (showFirstRunActivity()) {
5159 // We delay hiding the intro view until the first run activity is showing. This
5160 // avoids a blip.
5161 mWorkspace.postDelayed(new Runnable() {
5162 @Override
5163 public void run() {
5164 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005165 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005166 }
5167 }, ACTIVITY_START_DELAY);
5168 } else {
5169 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005170 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005171 }
5172 changeWallpaperVisiblity(true);
5173 }
5174
5175 private void markIntroScreenDismissed() {
5176 SharedPreferences.Editor editor = mSharedPrefs.edit();
5177 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
5178 editor.apply();
5179 }
5180
Sunny Goyal88d60f12014-09-08 03:47:29 -07005181 private void showFirstRunClings() {
5182 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
5183 // on the device, then we always show the first run cling experience (or if there is no
5184 // launcher2). Otherwise, we prompt the user upon started for migration
5185 LauncherClings launcherClings = new LauncherClings(this);
5186 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
5187 if (mModel.canMigrateFromOldLauncherDb(this)) {
5188 launcherClings.showMigrationCling();
5189 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005190 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005191 }
5192 }
5193 }
5194
Winson Chunga6945242014-01-08 14:04:34 -08005195 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005196 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5197 if (mHotseat != null) mHotseat.setAlpha(1f);
5198 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5199 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005200 }
5201
5202 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005203 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5204 if (mHotseat != null) mHotseat.setAlpha(0f);
5205 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5206 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005207 }
5208
Mathew Inwood72fbec12013-11-19 15:45:07 +00005209 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005210 // Called from search suggestion, not supported in other profiles.
5211 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5212 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5213 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5214 myUser);
5215 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005216 return null;
5217 }
Kenny Guyed131872014-04-30 03:02:21 +01005218 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005219 }
5220
5221 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5222 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005223 // Called from search suggestion, not supported in other profiles.
5224 return createShortcutDragInfo(shortcutIntent, caption, icon,
5225 UserHandleCompat.myUserHandle());
5226 }
5227
5228 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5229 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005230 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005231 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005232 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005233 }
5234
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005235 protected void moveWorkspaceToDefaultScreen() {
5236 mWorkspace.moveToDefaultScreen(false);
5237 }
5238
Mathew Inwood72fbec12013-11-19 15:45:07 +00005239 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5240 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005241 mWorkspace.onExternalDragStartedWithItem(dragView);
5242 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005243 }
5244
Anjali Koppalf5d29132014-02-28 13:33:27 -08005245 @Override
5246 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005247 if (mLauncherCallbacks != null) {
5248 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5249 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005250 }
5251
Winson Chung80baf5a2010-08-09 16:03:15 -07005252 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005253 * Prints out out state for debugging.
5254 */
5255 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005256 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005257 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005258 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5259 Log.d(TAG, "mRestoring=" + mRestoring);
5260 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5261 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005262 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005263 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005264
Winson Chung785d2eb2011-04-14 16:08:02 -07005265 if (mAppsCustomizeContent != null) {
5266 mAppsCustomizeContent.dumpState();
5267 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005268 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005269 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005270
5271 @Override
5272 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5273 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005274 synchronized (sDumpLogs) {
5275 writer.println(" ");
5276 writer.println("Debug logs: ");
5277 for (int i = 0; i < sDumpLogs.size(); i++) {
5278 writer.println(" " + sDumpLogs.get(i));
5279 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005280 }
Adam Cohen9211d422014-10-07 18:14:53 -07005281 if (mLauncherCallbacks != null) {
5282 mLauncherCallbacks.dump(prefix, fd, writer, args);
5283 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005284 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005285
5286 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005287 if (DEBUG_DUMP_LOG) {
5288 synchronized (sDumpLogs) {
5289 Log.d(TAG, "");
5290 Log.d(TAG, "*********************");
5291 Log.d(TAG, "Launcher debug logs: ");
5292 for (int i = 0; i < sDumpLogs.size(); i++) {
5293 Log.d(TAG, " " + sDumpLogs.get(i));
5294 }
5295 Log.d(TAG, "*********************");
5296 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005297 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005298 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005299 }
5300
5301 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005302 addDumpLog(tag, log, null, debugLog);
5303 }
5304
5305 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005306 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005307 if (e != null) {
5308 Log.d(tag, log, e);
5309 } else {
5310 Log.d(tag, log);
5311 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005312 }
Winson Chungede41292013-09-19 16:27:36 -07005313 if (DEBUG_DUMP_LOG) {
5314 sDateStamp.setTime(System.currentTimeMillis());
5315 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005316 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5317 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005318 }
Winson Chungede41292013-09-19 16:27:36 -07005319 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005320 }
5321
Winson Chungede41292013-09-19 16:27:36 -07005322 public void dumpLogsToLocalData() {
5323 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005324 new AsyncTask<Void, Void, Void>() {
5325 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005326 boolean success = false;
5327 sDateStamp.setTime(sRunStart);
5328 String FILENAME = sDateStamp.getMonth() + "-"
5329 + sDateStamp.getDay() + "_"
5330 + sDateStamp.getHours() + "-"
5331 + sDateStamp.getMinutes() + "_"
5332 + sDateStamp.getSeconds() + ".txt";
5333
5334 FileOutputStream fos = null;
5335 File outFile = null;
5336 try {
5337 outFile = new File(getFilesDir(), FILENAME);
5338 outFile.createNewFile();
5339 fos = new FileOutputStream(outFile);
5340 } catch (Exception e) {
5341 e.printStackTrace();
5342 }
5343 if (fos != null) {
5344 PrintWriter writer = new PrintWriter(fos);
5345
5346 writer.println(" ");
5347 writer.println("Debug logs: ");
5348 synchronized (sDumpLogs) {
5349 for (int i = 0; i < sDumpLogs.size(); i++) {
5350 writer.println(" " + sDumpLogs.get(i));
5351 }
5352 }
5353 writer.close();
5354 }
5355 try {
5356 if (fos != null) {
5357 fos.close();
5358 success = true;
5359 }
5360 } catch (IOException e) {
5361 e.printStackTrace();
5362 }
Michael Jurka43467462013-11-14 12:03:58 +01005363 return null;
Winson Chungede41292013-09-19 16:27:36 -07005364 }
Michael Jurka43467462013-11-14 12:03:58 +01005365 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005366 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005367 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005368}
Michael Jurka2763be32011-02-24 11:19:57 -08005369
Michael Jurkaabded662011-03-04 12:06:57 -08005370interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005371 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005372 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005373 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005374 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005375 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005376}
Dan Sandlerd5024042014-01-09 15:01:33 -05005377
5378interface DebugIntents {
5379 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5380 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005381}