blob: be845400df15728577686a042947385bd0461314 [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);
Jorim Jaggid017f882014-01-14 17:08:48 -08001372 }
1373
Adam Cohendbdff6b2013-09-18 19:09:15 -07001374 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001375
Winson Chung4c98d922011-05-31 16:50:48 -07001376 // Setup the workspace
1377 mWorkspace.setHapticFeedbackEnabled(false);
1378 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001379 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001380 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001381
Winson Chungf0ea4d32011-06-06 14:27:16 -07001382 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001383 mSearchDropTargetBar = (SearchDropTargetBar)
1384 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001385
Winson Chungf0ea4d32011-06-06 14:27:16 -07001386 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001387 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001388 mAppsCustomizeContent = (AppsCustomizePagedView)
1389 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1390 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001391
Winson Chung3d503fb2011-07-13 17:25:49 -07001392 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001393 dragController.setDragScoller(mWorkspace);
1394 dragController.setScrollView(mDragLayer);
1395 dragController.setMoveTarget(mWorkspace);
1396 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001397 if (mSearchDropTargetBar != null) {
1398 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001399 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001400
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001401 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001402 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001403 mWeightWatcher = new WeightWatcher(this);
1404 mWeightWatcher.setAlpha(0.5f);
1405 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001406 new FrameLayout.LayoutParams(
1407 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001408 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001409 Gravity.BOTTOM)
1410 );
Adam Cohen39a06042013-07-19 14:30:12 -07001411
1412 boolean show = shouldShowWeightWatcher();
1413 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001414 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001415 }
1416
1417 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001418 * Sets the all apps button. This method is called from {@link Hotseat}.
1419 */
1420 public void setAllAppsButton(View allAppsButton) {
1421 mAllAppsButton = allAppsButton;
1422 }
1423
1424 public View getAllAppsButton() {
1425 return mAllAppsButton;
1426 }
1427
1428 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001429 * Creates a view representing a shortcut.
1430 *
1431 * @param info The data structure describing the shortcut.
1432 *
1433 * @return A View inflated from R.layout.application.
1434 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001435 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001436 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001437 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001438 }
1439
1440 /**
1441 * Creates a view representing a shortcut inflated from the specified resource.
1442 *
1443 * @param layoutResId The id of the XML layout used to create the shortcut.
1444 * @param parent The group the shortcut belongs to.
1445 * @param info The data structure describing the shortcut.
1446 *
1447 * @return A View inflated from layoutResId.
1448 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001449 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001450 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001451 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001452 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001453 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001454 return favorite;
1455 }
1456
1457 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001458 * Add a shortcut to the workspace.
1459 *
1460 * @param data The intent describing the shortcut.
1461 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001462 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001463 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001464 int cellY) {
1465 int[] cellXY = mTmpAddItemCellCoordinates;
1466 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001467 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001468
Michael Jurkad3ef3062010-11-23 16:23:58 -08001469 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001470
Sunny Goyal2350bc92014-10-14 16:42:54 -07001471 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001472 if (info == null) {
1473 return;
1474 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001475 final View view = createShortcut(info);
1476
Adam Cohenfbba09b2011-07-18 21:43:05 -07001477 // First we check if we already know the exact location where we want to add this item.
1478 if (cellX >= 0 && cellY >= 0) {
1479 cellXY[0] = cellX;
1480 cellXY[1] = cellY;
1481 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001482
1483 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001484 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001485 true, null,null)) {
1486 return;
1487 }
1488 DragObject dragObject = new DragObject();
1489 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001490 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1491 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001492 return;
1493 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001494 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001495 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001496 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001497 foundCellSpan = (result != null);
1498 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001499 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001500 }
1501
1502 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001503 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001504 return;
1505 }
1506
Adam Cohendcd297f2013-06-18 13:13:40 -07001507 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508
1509 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001510 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001511 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001512 }
1513 }
1514
Adam Cohen2f093b62012-04-30 18:59:53 -07001515 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1516 int minHeight) {
1517 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001518 // We want to account for the extra amount of padding that we are adding to the widget
1519 // to ensure that it gets the full amount of space that it has requested
1520 int requiredWidth = minWidth + padding.left + padding.right;
1521 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001522 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001523 }
1524
Adam Cohen2f093b62012-04-30 18:59:53 -07001525 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1526 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001527 }
1528
Adam Cohen2f093b62012-04-30 18:59:53 -07001529 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1530 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001531 }
1532
Adam Cohen2f093b62012-04-30 18:59:53 -07001533 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1534 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001535 }
1536
Adam Cohen2f093b62012-04-30 18:59:53 -07001537 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1538 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001539 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001540 }
1541
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001542 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001543 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001544 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001545 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001546 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001548 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001549 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1550 if (appWidgetInfo == null) {
1551 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1552 }
Romain Guycbb89e42009-06-08 15:52:54 -07001553
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001554 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001555 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001556
Adam Cohen2f093b62012-04-30 18:59:53 -07001557 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1558 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001559
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001560 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001561 // We have saved the position to which the widget was dragged-- this really only matters
1562 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001563 int[] cellXY = mTmpAddItemCellCoordinates;
1564 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001565 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001566 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001567 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1568 cellXY[0] = mPendingAddInfo.cellX;
1569 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001570 spanXY[0] = mPendingAddInfo.spanX;
1571 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001572 foundCellSpan = true;
1573 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001574 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001575 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001576 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1577 spanXY[1], cellXY, finalSpan);
1578 spanXY[0] = finalSpan[0];
1579 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001580 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001581 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001582 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001583 }
1584
Michael Jurka0280c3b2010-09-17 15:00:07 -07001585 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001586 if (appWidgetId != -1) {
1587 // Deleting an app widget ID is a void call but writes to disk before returning
1588 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001589 new AsyncTask<Void, Void, Void>() {
1590 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001591 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001592 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001593 }
Michael Jurka43467462013-11-14 12:03:58 +01001594 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001595 }
Winson Chung93eef082012-03-23 15:59:27 -07001596 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001597 return;
1598 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001599
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001600 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001601 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1602 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001603 launcherInfo.spanX = spanXY[0];
1604 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001605 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1606 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001607 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001608
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001609 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001610 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001611
1612 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001613 if (hostView == null) {
1614 // Perform actual inflation because we're live
1615 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1616 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1617 } else {
1618 // The AppWidgetHostView has already been inflated and instantiated
1619 launcherInfo.hostView = hostView;
1620 }
Romain Guycbb89e42009-06-08 15:52:54 -07001621
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001622 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001623 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001624 launcherInfo.notifyWidgetSizeChanged(this);
1625
Adam Cohendcd297f2013-06-18 13:13:40 -07001626 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001627 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001628
1629 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001631 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001632 }
Romain Guycbb89e42009-06-08 15:52:54 -07001633
Adam Cohended9f8d2010-11-03 13:25:16 -07001634 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1635 @Override
1636 public void onReceive(Context context, Intent intent) {
1637 final String action = intent.getAction();
1638 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1639 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001640 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001641 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001642
Winson Chungc100e8e2011-08-09 16:02:43 -07001643 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001644 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001645 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001646 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001647 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001648 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1649 mUserPresent = true;
1650 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001651 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1652 mModel.resetLoadedState(false, true);
1653 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1654 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1655 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1656 mModel.resetLoadedState(false, true);
1657 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1658 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1659 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001660 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1661 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1662 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001663 }
1664 }
1665 };
1666
1667 @Override
1668 public void onAttachedToWindow() {
1669 super.onAttachedToWindow();
1670
1671 // Listen for broadcasts related to user-presence
1672 final IntentFilter filter = new IntentFilter();
1673 filter.addAction(Intent.ACTION_SCREEN_OFF);
1674 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001675 // For handling managed profiles
1676 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1677 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001678 if (ENABLE_DEBUG_INTENTS) {
1679 filter.addAction(DebugIntents.DELETE_DATABASE);
1680 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1681 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001682 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001683 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001684 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001685 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001686 mVisible = true;
1687 }
1688
Adam Cohen0b395352014-06-09 22:54:36 +00001689 /**
1690 * Sets up transparent navigation and status bars in LMP.
1691 * This method is a no-op for other platform versions.
1692 */
1693 @TargetApi(19)
1694 private void setupTransparentSystemBarsForLmp() {
1695 // TODO(sansid): use the APIs directly when compiling against L sdk.
1696 // Currently we use reflection to access the flags and the API to set the transparency
1697 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001698 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001699 try {
1700 getWindow().getAttributes().systemUiVisibility |=
1701 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1702 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1703 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1704 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1705 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1706 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1707 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1708 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1709
1710 Method setStatusBarColorMethod =
1711 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1712 Method setNavigationBarColorMethod =
1713 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1714 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1715 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1716 } catch (NoSuchFieldException e) {
1717 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1718 } catch (NoSuchMethodException ex) {
1719 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1720 } catch (IllegalAccessException e) {
1721 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1722 } catch (IllegalArgumentException e) {
1723 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1724 } catch (InvocationTargetException e) {
1725 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1726 } finally {}
1727 }
1728 }
1729
Adam Cohended9f8d2010-11-03 13:25:16 -07001730 @Override
1731 public void onDetachedFromWindow() {
1732 super.onDetachedFromWindow();
1733 mVisible = false;
1734
Adam Cohend113e0c2010-11-11 10:48:05 -08001735 if (mAttached) {
1736 unregisterReceiver(mReceiver);
1737 mAttached = false;
1738 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001739 updateRunning();
1740 }
1741
1742 public void onWindowVisibilityChanged(int visibility) {
1743 mVisible = visibility == View.VISIBLE;
1744 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001745 // The following code used to be in onResume, but it turns out onResume is called when
1746 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1747 // is a more appropriate event to handle
1748 if (mVisible) {
1749 mAppsCustomizeTabHost.onWindowVisible();
1750 if (!mWorkspaceLoading) {
1751 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001752 // We want to let Launcher draw itself at least once before we force it to build
1753 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001754 // apps is nice and speedy.
1755 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001756 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001757 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001758 if (mStarted) return;
1759 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001760 // We delay the layer building a bit in order to give
1761 // other message processing a time to run. In particular
1762 // this avoids a delay in hiding the IME if it was
1763 // currently shown, because doing that may involve
1764 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001765 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001766 final ViewTreeObserver.OnDrawListener listener = this;
1767 mWorkspace.post(new Runnable() {
1768 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001769 if (mWorkspace != null &&
1770 mWorkspace.getViewTreeObserver() != null) {
1771 mWorkspace.getViewTreeObserver().
1772 removeOnDrawListener(listener);
1773 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001774 }
1775 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001776 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001777 }
1778 });
1779 }
1780 clearTypedText();
1781 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001782 }
1783
1784 private void sendAdvanceMessage(long delay) {
1785 mHandler.removeMessages(ADVANCE_MSG);
1786 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1787 mHandler.sendMessageDelayed(msg, delay);
1788 mAutoAdvanceSentTime = System.currentTimeMillis();
1789 }
1790
1791 private void updateRunning() {
1792 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1793 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1794 mAutoAdvanceRunning = autoAdvanceRunning;
1795 if (autoAdvanceRunning) {
1796 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1797 sendAdvanceMessage(delay);
1798 } else {
1799 if (!mWidgetsToAdvance.isEmpty()) {
1800 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1801 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1802 }
1803 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001804 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001805 }
1806 }
1807 }
1808
1809 private final Handler mHandler = new Handler() {
1810 @Override
1811 public void handleMessage(Message msg) {
1812 if (msg.what == ADVANCE_MSG) {
1813 int i = 0;
1814 for (View key: mWidgetsToAdvance.keySet()) {
1815 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1816 final int delay = mAdvanceStagger * i;
1817 if (v instanceof Advanceable) {
1818 postDelayed(new Runnable() {
1819 public void run() {
1820 ((Advanceable) v).advance();
1821 }
1822 }, delay);
1823 }
1824 i++;
1825 }
1826 sendAdvanceMessage(mAdvanceInterval);
1827 }
1828 }
1829 };
1830
1831 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001832 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001833 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1834 if (v instanceof Advanceable) {
1835 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001836 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001837 updateRunning();
1838 }
1839 }
1840
1841 void removeWidgetToAutoAdvance(View hostView) {
1842 if (mWidgetsToAdvance.containsKey(hostView)) {
1843 mWidgetsToAdvance.remove(hostView);
1844 updateRunning();
1845 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001846 }
1847
Joe Onorato9c1289c2009-08-17 11:03:03 -04001848 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001849 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001850 launcherInfo.hostView = null;
1851 }
1852
Winson Chung93eef082012-03-23 15:59:27 -07001853 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1854 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1855 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001856 }
1857
Winson Chunga6945242014-01-08 14:04:34 -08001858 public DragLayer getDragLayer() {
1859 return mDragLayer;
1860 }
1861
1862 public Workspace getWorkspace() {
1863 return mWorkspace;
1864 }
1865
1866 public Hotseat getHotseat() {
1867 return mHotseat;
1868 }
1869
Jorim Jaggid017f882014-01-14 17:08:48 -08001870 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001871 return mOverviewPanel;
1872 }
1873
1874 public SearchDropTargetBar getSearchBar() {
1875 return mSearchDropTargetBar;
1876 }
1877
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001878 public LauncherAppWidgetHost getAppWidgetHost() {
1879 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001880 }
Romain Guycbb89e42009-06-08 15:52:54 -07001881
Winson Chunga9abd0e2010-10-27 17:18:37 -07001882 public LauncherModel getModel() {
1883 return mModel;
1884 }
1885
Winson Chunga6945242014-01-08 14:04:34 -08001886 protected SharedPreferences getSharedPrefs() {
1887 return mSharedPrefs;
1888 }
1889
Bjorn Bringertc459e522013-06-07 19:36:01 +01001890 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001891 getWindow().closeAllPanels();
1892
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001893 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001894 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001895 }
1896
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001897 @Override
1898 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001899 long startTime = 0;
1900 if (DEBUG_RESUME_TIME) {
1901 startTime = System.currentTimeMillis();
1902 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001903 super.onNewIntent(intent);
1904
1905 // Close the menu
1906 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001907 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001908 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001909
Adam Cohened307df2013-10-02 09:37:31 -07001910 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1911 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1912 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001913
Adam Cohen6fecd412013-10-02 17:41:50 -07001914 if (mWorkspace == null) {
1915 // Can be cases where mWorkspace is null, this prevents a NPE
1916 return;
1917 }
1918 Folder openFolder = mWorkspace.getOpenFolder();
1919 // In all these cases, only animate if we're already on home
1920 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001921
1922 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1923 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001924 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001925 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001926 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001927 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001928
Adam Cohen6fecd412013-10-02 17:41:50 -07001929 closeFolder();
1930 exitSpringLoadedDragMode();
1931
1932 // If we are already on home, then just animate back to the workspace,
1933 // otherwise, just wait until onResume to set the state back to Workspace
1934 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001935 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001936 } else {
1937 mOnResumeState = State.WORKSPACE;
1938 }
1939
1940 final View v = getWindow().peekDecorView();
1941 if (v != null && v.getWindowToken() != null) {
1942 InputMethodManager imm = (InputMethodManager)getSystemService(
1943 INPUT_METHOD_SERVICE);
1944 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1945 }
1946
1947 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001948 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001949 mAppsCustomizeTabHost.reset();
1950 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001951
Adam Cohen9211d422014-10-07 18:14:53 -07001952 if (mLauncherCallbacks != null) {
1953 mLauncherCallbacks.onHomeIntent();
1954 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001955 }
Adam Cohened307df2013-10-02 09:37:31 -07001956
Michael Jurka447bf842013-05-15 14:52:15 +02001957 if (DEBUG_RESUME_TIME) {
1958 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1959 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001960
Adam Cohen9211d422014-10-07 18:14:53 -07001961 if (mLauncherCallbacks != null) {
1962 mLauncherCallbacks.onNewIntent(intent);
1963 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001964 }
1965
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001966 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001967 public void onRestoreInstanceState(Bundle state) {
1968 super.onRestoreInstanceState(state);
1969 for (int page: mSynchronouslyBoundPages) {
1970 mWorkspace.restoreInstanceStateForChild(page);
1971 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001972 }
1973
1974 @Override
1975 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001976 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001977 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1978 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001979 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001980 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001981
Michael Jurka883f55b2010-10-21 15:47:14 -07001982 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001983 // We close any open folder since it will not be re-opened, and we need to make sure
1984 // this state is reflected.
1985 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001986
Adam Cohendcd297f2013-06-18 13:13:40 -07001987 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001988 mWaitingForResult) {
1989 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001990 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001991 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1992 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001993 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1994 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1995 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001996 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001997 }
1998
1999 if (mFolderInfo != null && mWaitingForResult) {
2000 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2001 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2002 }
Michael Jurka946ad472010-07-09 18:05:18 -07002003
Winson Chung785d2eb2011-04-14 16:08:02 -07002004 // Save the current AppsCustomize tab
2005 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08002006 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2007 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002008 if (currentTabTag != null) {
2009 outState.putString("apps_customize_currentTab", currentTabTag);
2010 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002011 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2012 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002013 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002014 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002015
2016 if (mLauncherCallbacks != null) {
2017 mLauncherCallbacks.onSaveInstanceState(outState);
2018 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002019 }
2020
2021 @Override
2022 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002023 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002024
Winson Chunge7a03942011-08-05 15:05:12 -07002025 // Remove all pending runnables
2026 mHandler.removeMessages(ADVANCE_MSG);
2027 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002028 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002029
Winson Chungcd2b0142011-06-08 16:02:26 -07002030 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002031 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002032
2033 // It's possible to receive onDestroy after a new Launcher activity has
2034 // been created. In this case, don't interfere with the new Launcher.
2035 if (mModel.isCurrentCallbacks(this)) {
2036 mModel.stopLoader();
2037 app.setLauncher(null);
2038 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002039
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002040 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002041 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002043 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002044 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002045 mAppWidgetHost = null;
2046
2047 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002048
2049 TextKeyListener.getInstance().release();
2050
Winson Chung81b52252012-08-27 15:34:29 -07002051 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2052 // to prevent leaking Launcher activities on orientation change.
2053 if (mModel != null) {
2054 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2055 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002056
2057 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002058 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002059
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002060 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002061 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002062 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002063 mWorkspace = null;
2064 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002065
Sunny Goyale755d462014-07-22 13:48:29 -07002066 PackageInstallerCompat.getInstance(this).onStop();
Michael Jurka2ecf9952012-06-18 12:52:28 -07002067 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002068
2069 if (mLauncherCallbacks != null) {
2070 mLauncherCallbacks.onDestroy();
2071 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002072 }
2073
Adam Cohena9cf38f2011-05-02 15:36:58 -07002074 public DragController getDragController() {
2075 return mDragController;
2076 }
2077
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002078 @Override
2079 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002080 if (requestCode >= 0) {
2081 setWaitingForResult(true);
2082 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002083 super.startActivityForResult(intent, requestCode);
2084 }
2085
Winson Chung70d72102011-08-12 11:24:35 -07002086 /**
2087 * Indicates that we want global search for this activity by setting the globalSearch
2088 * argument for {@link #startSearch} to true.
2089 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002090 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002091 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002092 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002093
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002094 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002095
Karl Rosaen138a0412009-04-23 19:00:21 -07002096 if (initialQuery == null) {
2097 // Use any text typed in the launcher as the initial query
2098 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002099 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 if (appSearchData == null) {
2101 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002102 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 }
Winson Chungadf0c182012-08-23 14:59:07 -07002104 Rect sourceBounds = new Rect();
2105 if (mSearchDropTargetBar != null) {
2106 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2107 }
Romain Guycbb89e42009-06-08 15:52:54 -07002108
Ian Parkinson047036e2014-09-01 15:40:53 +01002109 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002110 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002111 if (clearTextImmediately) {
2112 clearTypedText();
2113 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002114 }
2115
Ian Parkinson047036e2014-09-01 15:40:53 +01002116 /**
2117 * Start a text search.
2118 *
2119 * @return {@code true} if the search will start immediately, so any further keypresses
2120 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2121 * to buffer keypresses.
2122 */
2123 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002124 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002125 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2126 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2127 sourceBounds);
2128 }
2129
Michael Jurkaa33411c2012-06-14 16:18:21 -07002130 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002131 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002132 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002133 }
2134
2135 /**
2136 * Starts the global search activity. This code is a copied from SearchManager
2137 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002138 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002139 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002140 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002141 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2142 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2143 if (globalSearchActivity == null) {
2144 Log.w(TAG, "No global search activity found.");
2145 return;
2146 }
2147 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2148 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2149 intent.setComponent(globalSearchActivity);
2150 // Make sure that we have a Bundle to put source in
2151 if (appSearchData == null) {
2152 appSearchData = new Bundle();
2153 } else {
2154 appSearchData = new Bundle(appSearchData);
2155 }
Adam Cohen9211d422014-10-07 18:14:53 -07002156 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002157 if (!appSearchData.containsKey("source")) {
2158 appSearchData.putString("source", getPackageName());
2159 }
2160 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2161 if (!TextUtils.isEmpty(initialQuery)) {
2162 intent.putExtra(SearchManager.QUERY, initialQuery);
2163 }
2164 if (selectInitialQuery) {
2165 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2166 }
2167 intent.setSourceBounds(sourceBounds);
2168 try {
2169 startActivity(intent);
2170 } catch (ActivityNotFoundException ex) {
2171 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2172 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002173 }
2174
Yura4f93ec62014-02-04 14:15:21 +00002175 public boolean isOnCustomContent() {
2176 return mWorkspace.isOnOrMovingToCustomContent();
2177 }
2178
Winson Chung70d72102011-08-12 11:24:35 -07002179 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002180 public boolean onPrepareOptionsMenu(Menu menu) {
2181 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002182 if (!isOnCustomContent()) {
2183 // Close any open folders
2184 closeFolder();
2185 // Stop resizing any widgets
2186 mWorkspace.exitWidgetResizeMode();
2187 if (!mWorkspace.isInOverviewMode()) {
2188 // Show the overview mode
2189 showOverviewMode(true);
2190 } else {
2191 showWorkspace(true);
2192 }
Winson Chunge0298742014-01-17 12:03:00 -08002193 }
Adam Cohen9211d422014-10-07 18:14:53 -07002194 if (mLauncherCallbacks != null) {
2195 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2196 }
2197
Michael Jurkab94f3f82013-09-11 18:08:54 +02002198 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002199 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002200
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002201 @Override
2202 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002203 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002204 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002205 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002206 }
2207
Joe Onorato9c1289c2009-08-17 11:03:03 -04002208 public boolean isWorkspaceLocked() {
2209 return mWorkspaceLoading || mWaitingForResult;
2210 }
2211
Adam Cohen517a7f52014-03-01 12:12:59 -08002212 public boolean isWorkspaceLoading() {
2213 return mWorkspaceLoading;
2214 }
2215
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002216 private void setWorkspaceLoading(boolean value) {
2217 boolean isLocked = isWorkspaceLocked();
2218 mWorkspaceLoading = value;
2219 if (isLocked != isWorkspaceLocked()) {
2220 onWorkspaceLockedChanged();
2221 }
2222 }
2223
2224 private void setWaitingForResult(boolean value) {
2225 boolean isLocked = isWorkspaceLocked();
2226 mWaitingForResult = value;
2227 if (isLocked != isWorkspaceLocked()) {
2228 onWorkspaceLockedChanged();
2229 }
2230 }
2231
Adam Cohen9211d422014-10-07 18:14:53 -07002232 protected void onWorkspaceLockedChanged() {
2233 if (mLauncherCallbacks != null) {
2234 mLauncherCallbacks.onWorkspaceLockedChanged();
2235 }
2236 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002237
Michael Jurka0280c3b2010-09-17 15:00:07 -07002238 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002239 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002240 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002241 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2242 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002243 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002244 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002245 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002246
Adam Cohenad4e15c2013-10-17 16:21:35 -07002247 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2248 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2249 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2250 }
2251
2252 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2253 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2254 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002255 if (appWidgetInfo.configure != null) {
2256 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002257 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002258
Bjorn Bringert7984c942009-12-09 15:38:25 +00002259 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002260 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2261 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2262
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002263 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002264 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002265 Runnable onComplete = new Runnable() {
2266 @Override
2267 public void run() {
2268 // Exit spring loaded mode if necessary after adding the widget
2269 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2270 null);
2271 }
2272 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002273 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002274 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002275 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002276 }
2277 }
Romain Guycbb89e42009-06-08 15:52:54 -07002278
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002279 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002280 // Close any folders that may be open.
2281 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002282 mWorkspace.moveToCustomContentScreen(animate);
2283 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002284 /**
2285 * Process a shortcut drop.
2286 *
2287 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002288 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002289 * @param cell The cell it should be added to, optional
2290 * @param position The location on the screen where it was dropped, optional
2291 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002292 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002293 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002294 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002295 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002296 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002297 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002298
2299 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002300 mPendingAddInfo.cellX = cell[0];
2301 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002302 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002303
2304 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2305 createShortcutIntent.setComponent(componentName);
2306 processShortcut(createShortcutIntent);
2307 }
2308
Adam Cohenfbba09b2011-07-18 21:43:05 -07002309 /**
2310 * Process a widget drop.
2311 *
2312 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002313 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002314 * @param cell The cell it should be added to, optional
2315 * @param position The location on the screen where it was dropped, optional
2316 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002317 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002318 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002319 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002320 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002321 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002322 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002323 mPendingAddInfo.minSpanX = info.minSpanX;
2324 mPendingAddInfo.minSpanY = info.minSpanY;
2325
Adam Cohenfbba09b2011-07-18 21:43:05 -07002326 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002327 mPendingAddInfo.cellX = cell[0];
2328 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002330 if (span != null) {
2331 mPendingAddInfo.spanX = span[0];
2332 mPendingAddInfo.spanY = span[1];
2333 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002334
Adam Cohened66b2b2012-01-23 17:28:51 -08002335 AppWidgetHostView hostView = info.boundWidget;
2336 int appWidgetId;
2337 if (hostView != null) {
2338 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002339 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002340 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002341 // In this case, we either need to start an activity to get permission to bind
2342 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002343 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002344 Bundle options = info.bindOptions;
2345
Sunny Goyalffe83f12014-08-14 17:39:34 -07002346 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2347 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002348 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002349 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002350 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002351 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002352 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2353 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2354 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002355 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2356 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002357 // TODO: we need to make sure that this accounts for the options bundle.
2358 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002359 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2360 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002361 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002362 }
2363
Joe Onoratodeb98af2010-02-19 14:59:39 -08002364 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002365 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002366 }
2367
Winson Chung24ab2f12010-09-16 14:10:47 -07002368 void processWallpaper(Intent intent) {
2369 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2370 }
2371
Adam Cohendcd297f2013-06-18 13:13:40 -07002372 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002373 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002374 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002375 folderInfo.title = getText(R.string.folder_name);
2376
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002377 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002378 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002379 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002380 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002381
2382 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002383 FolderIcon newFolder =
2384 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002385 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002386 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002387 // Force measure the new folder icon
2388 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2389 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002390 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002391 }
Romain Guycbb89e42009-06-08 15:52:54 -07002392
Joe Onorato9c1289c2009-08-17 11:03:03 -04002393 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002394 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002395 }
2396
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002397 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002398 if (mLauncherCallbacks != null) {
2399 return mLauncherCallbacks.getWallpaperPickerComponent();
2400 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002401 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002402 }
2403
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002404 /**
2405 * Registers various content observers. The current implementation registers
2406 * only a favorites observer to keep track of the favorites applications.
2407 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002408 private void registerContentObservers() {
2409 ContentResolver resolver = getContentResolver();
2410 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2411 true, mWidgetObserver);
2412 }
2413
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002414 @Override
2415 public boolean dispatchKeyEvent(KeyEvent event) {
2416 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2417 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002418 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002419 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002420 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002421 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002422 dumpState();
2423 return true;
2424 }
2425 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002426 }
2427 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2428 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002429 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002430 return true;
2431 }
2432 }
2433
2434 return super.dispatchKeyEvent(event);
2435 }
2436
Joe Onorato88ec0992009-11-19 13:16:06 -08002437 @Override
2438 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002439 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2440 return;
2441 }
2442
Winson Chungc93e5ae2012-07-23 20:48:26 -07002443 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002444 if (mAppsCustomizeContent.getContentType() ==
2445 AppsCustomizePagedView.ContentType.Applications) {
2446 showWorkspace(true);
2447 } else {
2448 showOverviewMode(true);
2449 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002450 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002451 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002452 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002453 Folder openFolder = mWorkspace.getOpenFolder();
2454 if (openFolder.isEditingName()) {
2455 openFolder.dismissEditingName();
2456 } else {
2457 closeFolder();
2458 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002459 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002460 mWorkspace.exitWidgetResizeMode();
2461
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002462 // Back button is a no-op here, but give at least some feedback for the button press
2463 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002464 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002465 }
2466
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002467 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002468 * Re-listen when widgets are reset.
2469 */
2470 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002471 if (mAppWidgetHost != null) {
2472 mAppWidgetHost.startListening();
2473 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002474 }
2475
2476 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002477 * Launches the intent referred by the clicked shortcut.
2478 *
2479 * @param v The view representing the clicked shortcut.
2480 */
2481 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002482 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2483 // view has detached (it's possible for this to happen if the view is removed mid touch).
2484 if (v.getWindowToken() == null) {
2485 return;
2486 }
2487
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002488 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002489 return;
2490 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002491
Adam Cohen1697b792013-09-17 19:08:21 -07002492 if (v instanceof Workspace) {
2493 if (mWorkspace.isInOverviewMode()) {
2494 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002495 }
2496 return;
2497 }
2498
2499 if (v instanceof CellLayout) {
2500 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002501 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002502 }
2503 }
2504
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002505 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002506 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002507 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002509 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002510 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002511 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002512 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002513 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002514 } else if (tag instanceof AppInfo) {
2515 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002516 } else if (tag instanceof LauncherAppWidgetInfo) {
2517 if (v instanceof PendingAppWidgetHostView) {
2518 onClickPendingWidget((PendingAppWidgetHostView) v);
2519 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002520 }
2521 }
2522
Sunny Goyal508da152014-08-14 10:53:27 -07002523 public void onClickPagedViewIcon(View v) {
2524 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002525 if (mLauncherCallbacks != null) {
2526 mLauncherCallbacks.onClickPagedViewIcon(v);
2527 }
Sunny Goyal508da152014-08-14 10:53:27 -07002528 }
2529
Michael Jurka0e260592010-06-30 17:07:39 -07002530 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002531 return false;
2532 }
2533
Michael Jurkaaf442092010-06-10 17:01:57 -07002534 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002535 * Event handler for the app widget view which has not fully restored.
2536 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002537 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002538 if (mIsSafeModeEnabled) {
2539 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2540 return;
2541 }
2542
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002543 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002544 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002545 int widgetId = info.appWidgetId;
2546 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2547 if (appWidgetInfo != null) {
2548 mPendingAddWidgetInfo = appWidgetInfo;
2549 mPendingAddInfo.copyFrom(info);
2550 mPendingAddWidgetId = widgetId;
2551
Sunny Goyalffe83f12014-08-14 17:39:34 -07002552 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2553 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002554 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002555 } else if (info.installProgress < 0) {
2556 // The install has not been queued
2557 final String packageName = info.providerName.getPackageName();
2558 showBrokenAppInstallDialog(packageName,
2559 new DialogInterface.OnClickListener() {
2560 public void onClick(DialogInterface dialog, int id) {
2561 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2562 }
2563 });
2564 } else {
2565 // Download has started.
2566 final String packageName = info.providerName.getPackageName();
2567 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002568 }
2569 }
2570
2571 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002572 * Event handler for the search button
2573 *
2574 * @param v The view that was clicked.
2575 */
2576 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002577 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2578
Amith Yamasania135ba82011-08-09 17:42:01 -07002579 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002580 }
2581
2582 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002583 * Event handler for the voice button
2584 *
2585 * @param v The view that was clicked.
2586 */
2587 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002588 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002589
Bjorn Bringertc459e522013-06-07 19:36:01 +01002590 startVoice();
2591 }
2592
2593 public void startVoice() {
Adam Cohen9211d422014-10-07 18:14:53 -07002594 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2595 mLauncherCallbacks.startVoice();
2596 return;
2597 }
2598
Michael Jurkaae65ee32012-04-30 11:45:54 -07002599 try {
2600 final SearchManager searchManager =
2601 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2602 ComponentName activityName = searchManager.getGlobalSearchActivity();
2603 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002604 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002605 if (activityName != null) {
2606 intent.setPackage(activityName.getPackageName());
2607 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002608 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002609 } catch (ActivityNotFoundException e) {
2610 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002611 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002612 startActivitySafely(null, intent, "onClickVoiceButton");
2613 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002614 }
2615
2616 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002617 * Event handler for the "grid" button that appears on the home screen, which
2618 * enters all apps mode.
2619 *
2620 * @param v The view that was clicked.
2621 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002622 protected void onClickAllAppsButton(View v) {
2623 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2624 if (isAllAppsVisible()) {
2625 showWorkspace(true);
2626 } else {
2627 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2628 }
Adam Cohen9211d422014-10-07 18:14:53 -07002629 if (mLauncherCallbacks != null) {
2630 mLauncherCallbacks.onClickAllAppsButton(v);
2631 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002632 }
2633
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002634 private void showBrokenAppInstallDialog(final String packageName,
2635 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002636 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002637 .setTitle(R.string.abandoned_promises_title)
2638 .setMessage(R.string.abandoned_promise_explanation)
2639 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2640 .setNeutralButton(R.string.abandoned_clean_this,
2641 new DialogInterface.OnClickListener() {
2642 public void onClick(DialogInterface dialog, int id) {
2643 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2644 mWorkspace.removeAbandonedPromise(packageName, user);
2645 }
2646 })
2647 .create().show();
2648 return;
2649 }
2650
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002651 /**
2652 * Event handler for an app shortcut click.
2653 *
2654 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2655 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002656 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002657 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2658 Object tag = v.getTag();
2659 if (!(tag instanceof ShortcutInfo)) {
2660 throw new IllegalArgumentException("Input must be a Shortcut");
2661 }
2662
2663 // Open shortcut
2664 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002665
2666 if (shortcut.isDisabled != 0) {
2667 int error = R.string.activity_not_available;
2668 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2669 error = R.string.safemode_shortcut_error;
2670 }
2671 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2672 return;
2673 }
2674
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002675 final Intent intent = shortcut.intent;
2676
2677 // Check for special shortcuts
2678 if (intent.getComponent() != null) {
2679 final String shortcutClass = intent.getComponent().getClassName();
2680
2681 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2682 MemoryDumpActivity.startDump(this);
2683 return;
2684 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2685 toggleShowWeightWatcher();
2686 return;
2687 }
2688 }
2689
Chris Wren40c5ed32014-06-24 18:24:23 -04002690 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002691 if ((v instanceof BubbleTextView)
2692 && shortcut.isPromise()
2693 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002694 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002695 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002696 new DialogInterface.OnClickListener() {
2697 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002698 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002699 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002700 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002701 return;
2702 }
2703
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002704 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002705 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002706
2707 if (mLauncherCallbacks != null) {
2708 mLauncherCallbacks.onClickAppShortcut(v);
2709 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002710 }
2711
Sunny Goyal508da152014-08-14 10:53:27 -07002712 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002713 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002714 final ShortcutInfo shortcut;
2715 final Intent intent;
2716 if (tag instanceof ShortcutInfo) {
2717 shortcut = (ShortcutInfo) tag;
2718 intent = shortcut.intent;
2719 int[] pos = new int[2];
2720 v.getLocationOnScreen(pos);
2721 intent.setSourceBounds(new Rect(pos[0], pos[1],
2722 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002723
Sunny Goyal508da152014-08-14 10:53:27 -07002724 } else if (tag instanceof AppInfo) {
2725 shortcut = null;
2726 intent = ((AppInfo) tag).intent;
2727 } else {
2728 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2729 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002730
2731 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002732 mStats.recordLaunch(intent, shortcut);
2733
2734 if (success && v instanceof BubbleTextView) {
2735 mWaitingForResume = (BubbleTextView) v;
2736 mWaitingForResume.setStayPressed(true);
2737 }
2738 }
2739
2740 /**
2741 * Event handler for a folder icon click.
2742 *
2743 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2744 */
2745 protected void onClickFolderIcon(View v) {
2746 if (LOGD) Log.d(TAG, "onClickFolder");
2747 if (!(v instanceof FolderIcon)){
2748 throw new IllegalArgumentException("Input must be a FolderIcon");
2749 }
2750
2751 FolderIcon folderIcon = (FolderIcon) v;
2752 final FolderInfo info = folderIcon.getFolderInfo();
2753 Folder openFolder = mWorkspace.getFolderForTag(info);
2754
2755 // If the folder info reports that the associated folder is open, then verify that
2756 // it is actually opened. There have been a few instances where this gets out of sync.
2757 if (info.opened && openFolder == null) {
2758 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2759 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2760 info.opened = false;
2761 }
2762
2763 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2764 // Close any open folder
2765 closeFolder();
2766 // Open the requested folder
2767 openFolder(folderIcon);
2768 } else {
2769 // Find the open folder...
2770 int folderScreen;
2771 if (openFolder != null) {
2772 folderScreen = mWorkspace.getPageForView(openFolder);
2773 // .. and close it
2774 closeFolder(openFolder);
2775 if (folderScreen != mWorkspace.getCurrentPage()) {
2776 // Close any folder open on the current screen
2777 closeFolder();
2778 // Pull the folder onto this screen
2779 openFolder(folderIcon);
2780 }
2781 }
2782 }
Adam Cohen9211d422014-10-07 18:14:53 -07002783
2784 if (mLauncherCallbacks != null) {
2785 mLauncherCallbacks.onClickFolderIcon(v);
2786 }
Michael Jurka5130e402011-10-13 04:55:35 -07002787 }
2788
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002789 /**
2790 * Event handler for the (Add) Widgets button that appears after a long press
2791 * on the home screen.
2792 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002793 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002794 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002795 if (mIsSafeModeEnabled) {
2796 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2797 } else {
2798 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2799 if (mLauncherCallbacks != null) {
2800 mLauncherCallbacks.onClickAddWidgetButton(view);
2801 }
Adam Cohen9211d422014-10-07 18:14:53 -07002802 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002803 }
2804
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002805 /**
2806 * Event handler for the wallpaper picker button that appears after a long press
2807 * on the home screen.
2808 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002809 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002810 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2811 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2812 pickWallpaper.setComponent(getWallpaperPickerComponent());
2813 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002814
2815 if (mLauncherCallbacks != null) {
2816 mLauncherCallbacks.onClickWallpaperPicker(v);
2817 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002818 }
2819
2820 /**
2821 * Event handler for a click on the settings button that appears after a long press
2822 * on the home screen.
2823 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002824 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002825 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002826 if (mLauncherCallbacks != null) {
2827 mLauncherCallbacks.onClickSettingsButton(v);
2828 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002829 }
2830
Michael Jurka5130e402011-10-13 04:55:35 -07002831 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002832 // Provide the same haptic feedback that the system offers for virtual keys.
2833 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002834 }
2835
Adam Cohen61f560d2013-09-30 15:58:20 -07002836 public void performHapticFeedbackOnTouchDown(View v) {
2837 // Provide the same haptic feedback that the system offers for virtual keys.
2838 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2839 }
2840
2841 public View.OnTouchListener getHapticFeedbackTouchListener() {
2842 if (mHapticFeedbackTouchListener == null) {
2843 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2844 @Override
2845 public boolean onTouch(View v, MotionEvent event) {
2846 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2847 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2848 }
2849 return false;
2850 }
2851 };
2852 }
2853 return mHapticFeedbackTouchListener;
2854 }
2855
Adam Cohen9211d422014-10-07 18:14:53 -07002856 public void onDragStarted(View view) {
2857 if (isOnCustomContent()) {
2858 // Custom content screen doesn't participate in drag and drop. If on custom
2859 // content screen, move to default.
2860 moveWorkspaceToDefaultScreen();
2861 }
2862
2863 if (mLauncherCallbacks != null) {
2864 mLauncherCallbacks.onDragStarted(view);
2865 }
2866 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002867
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002868 /**
2869 * Called when the user stops interacting with the launcher.
2870 * This implies that the user is now on the homescreen and is not doing housekeeping.
2871 */
Adam Cohen9211d422014-10-07 18:14:53 -07002872 protected void onInteractionEnd() {
2873 if (mLauncherCallbacks != null) {
2874 mLauncherCallbacks.onInteractionEnd();
2875 }
2876 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002877
2878 /**
2879 * Called when the user starts interacting with the launcher.
2880 * The possible interactions are:
2881 * - open all apps
2882 * - reorder an app shortcut, or a widget
2883 * - open the overview mode.
2884 * This is a good time to stop doing things that only make sense
2885 * when the user is on the homescreen and not doing housekeeping.
2886 */
Adam Cohen9211d422014-10-07 18:14:53 -07002887 protected void onInteractionBegin() {
2888 if (mLauncherCallbacks != null) {
2889 mLauncherCallbacks.onInteractionBegin();
2890 }
2891 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002892
Kenny Guyf07af7b2014-07-31 11:39:16 +01002893 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002894 String packageName = componentName.getPackageName();
Kenny Guyf07af7b2014-07-31 11:39:16 +01002895 try {
2896 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2897 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2898 launcherApps.showAppDetailsForProfile(componentName, user);
2899 } catch (SecurityException e) {
2900 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2901 Log.e(TAG, "Launcher does not have permission to launch settings");
2902 } catch (ActivityNotFoundException e) {
2903 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2904 Log.e(TAG, "Unable to launch settings");
2905 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002906 }
2907
Michael Jurka1e2f4652013-07-08 18:03:46 -07002908 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002909 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2910 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002911 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002912 // System applications cannot be installed. For now, show a toast explaining that.
2913 // We may give them the option of disabling apps this way.
2914 int messageId = R.string.uninstall_system_app_text;
2915 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002916 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002917 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002918 String packageName = componentName.getPackageName();
2919 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002920 Intent intent = new Intent(
2921 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002922 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2923 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002924 if (user != null) {
2925 user.addToIntent(intent, Intent.EXTRA_USER);
2926 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002927 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002928 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002929 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002930 }
2931
Michael Jurka86a720e2012-05-09 11:23:23 -07002932 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002933 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002934 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002935 // Only launch using the new animation if the shortcut has not opted out (this is a
2936 // private contract between launcher and may be ignored in the future).
2937 boolean useLaunchAnimation = (v != null) &&
2938 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002939 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2940 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002941
Kenny Guy1317e2d2014-05-08 18:52:50 +01002942 UserHandleCompat user = null;
2943 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2944 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2945 user = userManager.getUserForSerialNumber(serialNumber);
2946 }
2947
2948 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002949 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002950 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002951 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2952 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002953 optsBundle = opts.toBundle();
2954 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002955
2956 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2957 // Could be launching some bookkeeping activity
2958 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002959 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002960 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002961 launcherApps.startActivityForProfile(intent.getComponent(), user,
2962 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002963 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002964 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002965 } catch (SecurityException e) {
2966 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002967 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002969 "or use the exported attribute for this activity. "
2970 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002971 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002972 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002973 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002974
Michael Jurka86a720e2012-05-09 11:23:23 -07002975 boolean startActivitySafely(View v, Intent intent, Object tag) {
2976 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002977 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2978 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2979 return false;
2980 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002981 try {
2982 success = startActivity(v, intent, tag);
2983 } catch (ActivityNotFoundException e) {
2984 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2985 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2986 }
2987 return success;
2988 }
2989
Adam Cohen268c4752012-06-06 17:47:33 -07002990 /**
2991 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2992 * in the DragLayer in the exact absolute location of the original FolderIcon.
2993 */
2994 private void copyFolderIconToImage(FolderIcon fi) {
2995 final int width = fi.getMeasuredWidth();
2996 final int height = fi.getMeasuredHeight();
2997
2998 // Lazy load ImageView, Bitmap and Canvas
2999 if (mFolderIconImageView == null) {
3000 mFolderIconImageView = new ImageView(this);
3001 }
3002 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3003 mFolderIconBitmap.getHeight() != height) {
3004 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3005 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3006 }
3007
3008 DragLayer.LayoutParams lp;
3009 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3010 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3011 } else {
3012 lp = new DragLayer.LayoutParams(width, height);
3013 }
3014
Adam Cohen307fe232012-08-16 17:55:58 -07003015 // The layout from which the folder is being opened may be scaled, adjust the starting
3016 // view size by this scale factor.
3017 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003018 lp.customPosition = true;
3019 lp.x = mRectForFolderAnimation.left;
3020 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003021 lp.width = (int) (scale * width);
3022 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003023
3024 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3025 fi.draw(mFolderIconCanvas);
3026 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003027 if (fi.getFolder() != null) {
3028 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3029 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003030 }
Adam Cohen268c4752012-06-06 17:47:33 -07003031 // Just in case this image view is still in the drag layer from a previous animation,
3032 // we remove it and re-add it.
3033 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3034 mDragLayer.removeView(mFolderIconImageView);
3035 }
3036 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003037 if (fi.getFolder() != null) {
3038 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003039 }
Adam Cohen268c4752012-06-06 17:47:33 -07003040 }
3041
Adam Cohen2801caf2011-05-13 20:57:39 -07003042 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003043 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003044 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3045 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3046 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3047
Adam Cohenc51934b2011-07-26 21:07:43 -07003048 FolderInfo info = (FolderInfo) fi.getTag();
3049 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3050 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003051 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3052 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003053 }
3054
Adam Cohen268c4752012-06-06 17:47:33 -07003055 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3056 copyFolderIconToImage(fi);
3057 fi.setVisibility(View.INVISIBLE);
3058
Michael Jurka2ecf9952012-06-18 12:52:28 -07003059 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003060 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003061 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003062 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3063 }
3064 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003065 oa.start();
3066 }
3067
Adam Cohen268c4752012-06-06 17:47:33 -07003068 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003069 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003070 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3071 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3072 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3073
Adam Cohen268c4752012-06-06 17:47:33 -07003074 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003075
Adam Cohen268c4752012-06-06 17:47:33 -07003076 // We remove and re-draw the FolderIcon in-case it has changed
3077 mDragLayer.removeView(mFolderIconImageView);
3078 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003079 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003080 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003081 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003082 oa.addListener(new AnimatorListenerAdapter() {
3083 @Override
3084 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003085 if (cl != null) {
3086 cl.clearFolderLeaveBehind();
3087 // Remove the ImageView copy of the FolderIcon and make the original visible.
3088 mDragLayer.removeView(mFolderIconImageView);
3089 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003090 }
3091 }
3092 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003093 oa.start();
3094 }
3095
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003096 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003097 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003098 * is animated relative to the specified View. If the View is null, no animation
3099 * is played.
3100 *
3101 * @param folderInfo The FolderInfo describing the folder to open.
3102 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003103 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003104 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003105 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003106
Adam Cohena9cf38f2011-05-02 15:36:58 -07003107 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003108
Adam Cohen4554ee12011-08-03 16:13:21 -07003109 // Just verify that the folder hasn't already been added to the DragLayer.
3110 // There was a one-off crash where the folder had a parent already.
3111 if (folder.getParent() == null) {
3112 mDragLayer.addView(folder);
3113 mDragController.addDropTarget((DropTarget) folder);
3114 } else {
3115 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3116 folder.getParent() + ").");
3117 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003118 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003119 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003120
3121 // Notify the accessibility manager that this folder "window" has appeared and occluded
3122 // the workspace items
3123 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3124 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003125 }
3126
3127 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003128 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003129 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003130 if (folder.isEditingName()) {
3131 folder.dismissEditingName();
3132 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003133 closeFolder(folder);
3134 }
3135 }
3136
3137 void closeFolder(Folder folder) {
3138 folder.getInfo().opened = false;
3139
3140 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3141 if (parent != null) {
3142 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3143 shrinkAndFadeInFolderIcon(fi);
3144 }
3145 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003146
3147 // Notify the accessibility manager that this folder "window" has disappeard and no
3148 // longer occludeds the workspace items
3149 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003150 }
3151
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003152 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003153 if (!isDraggingEnabled()) return false;
3154 if (isWorkspaceLocked()) return false;
3155 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003156
Adam Cohen1697b792013-09-17 19:08:21 -07003157 if (v instanceof Workspace) {
3158 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003159 if (mWorkspace.enterOverviewMode()) {
3160 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3161 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3162 return true;
3163 } else {
3164 return false;
3165 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003166 } else {
3167 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003168 }
Adam Cohen1697b792013-09-17 19:08:21 -07003169 }
3170
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003171 CellLayout.CellInfo longClickCellInfo = null;
3172 View itemUnderLongClick = null;
3173 if (v.getTag() instanceof ItemInfo) {
3174 ItemInfo info = (ItemInfo) v.getTag();
3175 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3176 itemUnderLongClick = longClickCellInfo.cell;
3177 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003178 }
3179
Winson Chung3d503fb2011-07-13 17:25:49 -07003180 // The hotseat touch handling does not go through Workspace, and we always allow long press
3181 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003182 final boolean inHotseat = isHotseatLayout(v);
3183 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003184 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003185 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003186 // User long pressed on empty space
3187 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3188 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003189 if (mWorkspace.isInOverviewMode()) {
3190 mWorkspace.startReordering(v);
3191 } else {
3192 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003193 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003194 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003195 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3196 mHotseat.getOrderInHotseat(
3197 longClickCellInfo.cellX,
3198 longClickCellInfo.cellY));
3199 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003200 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003201 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003202 }
3203 }
3204 }
3205 return true;
3206 }
3207
Winson Chung3d503fb2011-07-13 17:25:49 -07003208 boolean isHotseatLayout(View layout) {
3209 return mHotseat != null && layout != null &&
3210 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3211 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003212
Winson Chung3d503fb2011-07-13 17:25:49 -07003213 /**
3214 * Returns the CellLayout of the specified container at the specified screen.
3215 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003216 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003217 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3218 if (mHotseat != null) {
3219 return mHotseat.getLayout();
3220 } else {
3221 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003222 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003223 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003224 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003225 }
3226 }
3227
Daniel Sandler843e8602010-06-07 14:59:01 -04003228 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003229 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003230 }
3231
Craig Mautner360310b2012-10-26 15:13:08 -07003232 private void setWorkspaceBackground(boolean workspace) {
3233 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003234 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003235 }
3236
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003237 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003238 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3239 int curflags = getWindow().getAttributes().flags
3240 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3241 if (wpflags != curflags) {
3242 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3243 }
Craig Mautner360310b2012-10-26 15:13:08 -07003244 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003245 }
3246
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003247 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3248 if (v instanceof LauncherTransitionable) {
3249 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3250 }
3251 }
3252
Michael Jurkabed61d22012-02-14 22:51:29 -08003253 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3254 if (v instanceof LauncherTransitionable) {
3255 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3256 }
Winson Chung70442722012-02-10 15:43:22 -08003257
3258 // Update the workspace transition step as well
3259 dispatchOnLauncherTransitionStep(v, 0f);
3260 }
3261
3262 private void dispatchOnLauncherTransitionStep(View v, float t) {
3263 if (v instanceof LauncherTransitionable) {
3264 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3265 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003266 }
3267
3268 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3269 if (v instanceof LauncherTransitionable) {
3270 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3271 }
Winson Chung70442722012-02-10 15:43:22 -08003272
3273 // Update the workspace transition step as well
3274 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003275 }
3276
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003277 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003278 * Things to test when changing the following seven functions.
3279 * - Home from workspace
3280 * - from center screen
3281 * - from other screens
3282 * - Home from all apps
3283 * - from center screen
3284 * - from other screens
3285 * - Back from all apps
3286 * - from center screen
3287 * - from other screens
3288 * - Launch app from workspace and quit
3289 * - with back
3290 * - with home
3291 * - Launch app from all apps and quit
3292 * - with back
3293 * - with home
3294 * - Go to a screen that's not the default, then all
3295 * apps, and launch and app, and go back
3296 * - with back
3297 * -with home
3298 * - On workspace, long press power and go back
3299 * - with back
3300 * - with home
3301 * - On all apps, long press power and go back
3302 * - with back
3303 * - with home
3304 * - On workspace, power off
3305 * - On all apps, power off
3306 * - Launch an app and turn off the screen while in that app
3307 * - Go back with home key
3308 * - Go back with back key TODO: make this not go to workspace
3309 * - From all apps
3310 * - From workspace
3311 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3312 * - From all apps
3313 * - From the center workspace
3314 * - From another workspace
3315 */
3316
3317 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003318 * Zoom the camera out from the workspace to reveal 'toView'.
3319 * Assumes that the view to show is anchored at either the very top or very bottom
3320 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003321 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003322 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003323 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3324 showAppsCustomizeHelper(animated, springLoaded, contentType);
3325 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003326
Winson Chungc58497e2013-09-03 17:48:37 -07003327 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3328 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003329 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003330 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003331 mStateAnimation.cancel();
3332 mStateAnimation = null;
3333 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003334
Kenny Guyd794a3f2014-09-16 15:17:58 +01003335 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003336
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003337 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003338
Winson Chungf0ea4d32011-06-06 14:27:16 -07003339 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3340 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003341 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003342 final int itemsAlphaStagger =
3343 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003344
Winson Chungf0ea4d32011-06-06 14:27:16 -07003345 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003346 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003347 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003348
Adam Cohen2854d252014-08-27 16:04:07 -07003349 final ArrayList<View> layerViews = new ArrayList<View>();
3350
Adam Cohen6c5891a2014-07-09 23:53:15 -07003351 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3352 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003353 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003354 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003355 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003356 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3357 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003358 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3359 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003360
Adam Cohena38dc902014-09-07 17:48:55 +02003361 // If for some reason our views aren't initialized, don't animate
3362 boolean initialized = getAllAppsButton() != null;
3363
3364 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003365 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003366 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3367 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003368
Adam Cohen9bfdb762014-07-21 17:44:06 -07003369 final View page = content.getPageAt(content.getCurrentPage());
3370 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003371
Adam Cohen63f1ec02014-08-12 09:23:13 -07003372 final float initialPanelAlpha = 1f;
3373
3374 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3375 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003376 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3377 } else {
3378 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3379 }
3380
Adam Cohen9bfdb762014-07-21 17:44:06 -07003381 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003382 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003383 revealView.setVisibility(View.VISIBLE);
3384 // We need to hide this view as the animation start will be posted.
3385 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003386
Adam Cohen9bfdb762014-07-21 17:44:06 -07003387 int width = revealView.getMeasuredWidth();
3388 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003389 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003390
Adam Cohen63f1ec02014-08-12 09:23:13 -07003391 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003392 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003393
Adam Cohen63f1ec02014-08-12 09:23:13 -07003394 // Get the y delta between the center of the page and the center of the all apps button
3395 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3396 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003397
Adam Cohen2854d252014-08-27 16:04:07 -07003398 float alpha = 0;
3399 float xDrift = 0;
3400 float yDrift = 0;
3401 if (material) {
3402 alpha = isWidgetTray ? 0.3f : 1f;
3403 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3404 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3405 } else {
3406 yDrift = 2 * height / 3;
3407 xDrift = 0;
3408 }
3409 final float initAlpha = alpha;
3410
Adam Cohen9bfdb762014-07-21 17:44:06 -07003411 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003412 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003413 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003414 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003415 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003416 PropertyValuesHolder panelDriftX =
3417 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003418
Adam Cohen2854d252014-08-27 16:04:07 -07003419 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3420 panelAlpha, panelDriftY, panelDriftX);
3421
Adam Cohen9bfdb762014-07-21 17:44:06 -07003422 panelAlphaAndDrift.setDuration(revealDuration);
3423 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003424
Adam Cohen9bfdb762014-07-21 17:44:06 -07003425 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003426
Adam Cohen4e243a22014-08-10 18:30:55 -07003427 if (page != null) {
3428 page.setVisibility(View.VISIBLE);
3429 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003430 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003431
Adam Cohen2854d252014-08-27 16:04:07 -07003432 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3433 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003434 pageDrift.setDuration(revealDuration);
3435 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003436 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003437 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003438
Adam Cohen63f1ec02014-08-12 09:23:13 -07003439 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003440 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003441 itemsAlpha.setDuration(revealDuration);
3442 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3443 itemsAlpha.setStartDelay(itemsAlphaStagger);
3444 mStateAnimation.play(itemsAlpha);
3445 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003446
Adam Cohen4e243a22014-08-10 18:30:55 -07003447 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3448 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003449 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003450 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003451 indicatorsAlpha.setDuration(revealDuration);
3452 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003453
Adam Cohen9bfdb762014-07-21 17:44:06 -07003454 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003455 final View allApps = getAllAppsButton();
3456 int allAppsButtonSize = LauncherAppState.getInstance().
3457 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3458 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003459 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003460 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003461 reveal.setDuration(revealDuration);
3462 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003463
3464 reveal.addListener(new AnimatorListenerAdapter() {
3465 public void onAnimationStart(Animator animation) {
3466 if (!isWidgetTray) {
3467 allApps.setVisibility(View.INVISIBLE);
3468 }
3469 }
3470 public void onAnimationEnd(Animator animation) {
3471 if (!isWidgetTray) {
3472 allApps.setVisibility(View.VISIBLE);
3473 }
3474 }
3475 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003476 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003477 }
Michael Jurka1899a362011-11-03 13:50:45 -07003478
Adam Cohen9bfdb762014-07-21 17:44:06 -07003479 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3480 @Override
3481 public void onAnimationEnd(Animator animation) {
3482 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3483 dispatchOnLauncherTransitionEnd(toView, animated, false);
3484
3485 revealView.setVisibility(View.INVISIBLE);
3486 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003487 if (page != null) {
3488 page.setLayerType(View.LAYER_TYPE_NONE, null);
3489 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003490 content.setPageBackgroundsVisible(true);
3491
3492 // Hide the search bar
3493 if (mSearchDropTargetBar != null) {
3494 mSearchDropTargetBar.hideSearchBar(false);
3495 }
3496 }
3497
Adam Cohen9bfdb762014-07-21 17:44:06 -07003498 });
3499
Adam Cohen6c5891a2014-07-09 23:53:15 -07003500 if (workspaceAnim != null) {
3501 mStateAnimation.play(workspaceAnim);
3502 }
Adam Cohen2854d252014-08-27 16:04:07 -07003503
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003504 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3505 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003506 final AnimatorSet stateAnimation = mStateAnimation;
3507 final Runnable startAnimRunnable = new Runnable() {
3508 public void run() {
3509 // Check that mStateAnimation hasn't changed while
3510 // we waited for a layout/draw pass
3511 if (mStateAnimation != stateAnimation)
3512 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003513 dispatchOnLauncherTransitionStart(fromView, animated, false);
3514 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003515
3516 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003517 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003518 for (int i = 0; i < layerViews.size(); i++) {
3519 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003520 if (v != null) {
3521 boolean attached = true;
3522 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3523 attached = v.isAttachedToWindow();
3524 }
3525 if (attached) v.buildLayer();
3526 }
Adam Cohen2854d252014-08-27 16:04:07 -07003527 }
3528 }
3529 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003530 }
3531 };
Adam Cohen2854d252014-08-27 16:04:07 -07003532 toView.bringToFront();
3533 toView.setVisibility(View.VISIBLE);
3534 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003535 } else {
3536 toView.setTranslationX(0.0f);
3537 toView.setTranslationY(0.0f);
3538 toView.setScaleX(1.0f);
3539 toView.setScaleY(1.0f);
3540 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003541 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003542
Daniel Sandlere4f98912013-06-25 15:13:26 -04003543 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003544 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003545 if (mSearchDropTargetBar != null) {
3546 mSearchDropTargetBar.hideSearchBar(false);
3547 }
Michael Jurkaabded662011-03-04 12:06:57 -08003548 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003549 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003550 dispatchOnLauncherTransitionStart(fromView, animated, false);
3551 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003552 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003553 dispatchOnLauncherTransitionStart(toView, animated, false);
3554 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003555 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003556 }
3557
3558 /**
3559 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003560 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003561 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003562 */
Adam Cohened307df2013-10-02 09:37:31 -07003563 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003564 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003565
Michael Jurkab3e22d92011-10-31 15:58:33 -07003566 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003567 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003568 mStateAnimation.cancel();
3569 mStateAnimation = null;
3570 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003571
Kenny Guyd794a3f2014-09-16 15:17:58 +01003572 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003573 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003574
Winson Chungf0ea4d32011-06-06 14:27:16 -07003575 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003576 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003577 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003578 final int itemsAlphaStagger =
3579 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003580
Winson Chungf0ea4d32011-06-06 14:27:16 -07003581 final float scaleFactor = (float)
3582 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3583 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003584 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003585 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003586 final ArrayList<View> layerViews = new ArrayList<View>();
3587
Adam Cohened307df2013-10-02 09:37:31 -07003588 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003589 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003590 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003591 } else if (toState == Workspace.State.SPRING_LOADED ||
3592 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003593 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003594 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003595 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003596
Adam Cohena38dc902014-09-07 17:48:55 +02003597 // If for some reason our views aren't initialized, don't animate
3598 boolean initialized = getAllAppsButton() != null;
3599
3600 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003601 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003602 if (workspaceAnim != null) {
3603 mStateAnimation.play(workspaceAnim);
3604 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003605
Adam Cohen9bfdb762014-07-21 17:44:06 -07003606 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3607 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003608
Adam Cohen9bfdb762014-07-21 17:44:06 -07003609 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003610
3611 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3612 int count = content.getChildCount();
3613 for (int i = 0; i < count; i++) {
3614 View child = content.getChildAt(i);
3615 if (child != page) {
3616 child.setVisibility(View.INVISIBLE);
3617 }
3618 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003619 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003620
Adam Cohen2854d252014-08-27 16:04:07 -07003621 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3622 // don't perform all these no-op animations. In particularly, this was causing
3623 // the all-apps button to pop in and out.
3624 if (fromView.getVisibility() == View.VISIBLE) {
3625 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3626 final boolean isWidgetTray =
3627 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003628
Adam Cohen2854d252014-08-27 16:04:07 -07003629 if (isWidgetTray) {
3630 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3631 } else {
3632 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003633 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003634
Adam Cohen2854d252014-08-27 16:04:07 -07003635 int width = revealView.getMeasuredWidth();
3636 int height = revealView.getMeasuredHeight();
3637 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3638
3639 // Hide the real page background, and swap in the fake one
3640 revealView.setVisibility(View.VISIBLE);
3641 content.setPageBackgroundsVisible(false);
3642
3643 final View allAppsButton = getAllAppsButton();
3644 revealView.setTranslationY(0);
3645 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3646 allAppsButton, null);
3647
3648 float xDrift = 0;
3649 float yDrift = 0;
3650 if (material) {
3651 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3652 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3653 } else {
3654 yDrift = 5 * height / 4;
3655 xDrift = 0;
3656 }
3657
3658 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3659 TimeInterpolator decelerateInterpolator = material ?
3660 new LogDecelerateInterpolator(100, 0) :
3661 new LogDecelerateInterpolator(30, 0);
3662
3663 // The vertical motion of the apps panel should be delayed by one frame
3664 // from the conceal animation in order to give the right feel. We correpsondingly
3665 // shorten the duration so that the slide and conceal end at the same time.
3666 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3667 0, yDrift);
3668 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3669 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3670 panelDriftY.setInterpolator(decelerateInterpolator);
3671 mStateAnimation.play(panelDriftY);
3672
3673 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3674 0, xDrift);
3675 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3676 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3677 panelDriftX.setInterpolator(decelerateInterpolator);
3678 mStateAnimation.play(panelDriftX);
3679
3680 if (isWidgetTray || !material) {
3681 float finalAlpha = material ? 0.4f : 0f;
3682 revealView.setAlpha(1f);
3683 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3684 1f, finalAlpha);
3685 panelAlpha.setDuration(revealDuration);
3686 panelAlpha.setInterpolator(material ? decelerateInterpolator :
3687 new AccelerateInterpolator(1.5f));
3688 mStateAnimation.play(panelAlpha);
3689 }
3690
3691 if (page != null) {
3692 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3693
3694 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3695 0, yDrift);
3696 page.setTranslationY(0);
3697 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3698 pageDrift.setInterpolator(decelerateInterpolator);
3699 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3700 mStateAnimation.play(pageDrift);
3701
3702 page.setAlpha(1f);
3703 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3704 itemsAlpha.setDuration(100);
3705 itemsAlpha.setInterpolator(decelerateInterpolator);
3706 mStateAnimation.play(itemsAlpha);
3707 }
3708
3709 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3710 pageIndicators.setAlpha(1f);
3711 ObjectAnimator indicatorsAlpha =
3712 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3713 indicatorsAlpha.setDuration(revealDuration);
3714 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3715 mStateAnimation.play(indicatorsAlpha);
3716
3717 width = revealView.getMeasuredWidth();
3718
3719 if (material) {
3720 if (!isWidgetTray) {
3721 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003722 }
Adam Cohen2854d252014-08-27 16:04:07 -07003723 int allAppsButtonSize = LauncherAppState.getInstance().
3724 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3725 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3726 Animator reveal =
3727 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3728 height / 2, revealRadius, finalRadius);
3729 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3730 reveal.setDuration(revealDuration);
3731 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003732
Adam Cohen2854d252014-08-27 16:04:07 -07003733 reveal.addListener(new AnimatorListenerAdapter() {
3734 public void onAnimationEnd(Animator animation) {
3735 revealView.setVisibility(View.INVISIBLE);
3736 if (!isWidgetTray) {
3737 allAppsButton.setVisibility(View.VISIBLE);
3738 }
3739 }
3740 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003741
Adam Cohen2854d252014-08-27 16:04:07 -07003742 mStateAnimation.play(reveal);
3743 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003744
Adam Cohen2854d252014-08-27 16:04:07 -07003745 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3746 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3747 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003748 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003749
3750 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003751 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003752 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003753 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003754 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3755 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003756 if (onCompleteRunnable != null) {
3757 onCompleteRunnable.run();
3758 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003759
3760 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003761 if (page != null) {
3762 page.setLayerType(View.LAYER_TYPE_NONE, null);
3763 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003764 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003765 // Unhide side pages
3766 int count = content.getChildCount();
3767 for (int i = 0; i < count; i++) {
3768 View child = content.getChildAt(i);
3769 child.setVisibility(View.VISIBLE);
3770 }
3771
3772 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003773 if (page != null) {
3774 page.setTranslationX(0);
3775 page.setTranslationY(0);
3776 page.setAlpha(1);
3777 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003778 content.setCurrentPage(content.getNextPage());
3779
Chet Haasebc2f0822012-10-26 17:59:53 -07003780 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003781 }
3782 });
3783
Adam Cohen2854d252014-08-27 16:04:07 -07003784 final AnimatorSet stateAnimation = mStateAnimation;
3785 final Runnable startAnimRunnable = new Runnable() {
3786 public void run() {
3787 // Check that mStateAnimation hasn't changed while
3788 // we waited for a layout/draw pass
3789 if (mStateAnimation != stateAnimation)
3790 return;
3791 dispatchOnLauncherTransitionStart(fromView, animated, false);
3792 dispatchOnLauncherTransitionStart(toView, animated, false);
3793
Kenny Guyd794a3f2014-09-16 15:17:58 +01003794 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003795 for (int i = 0; i < layerViews.size(); i++) {
3796 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003797 if (v != null) {
3798 boolean attached = true;
3799 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3800 attached = v.isAttachedToWindow();
3801 }
3802 if (attached) v.buildLayer();
3803 }
Adam Cohen2854d252014-08-27 16:04:07 -07003804 }
3805 }
3806 mStateAnimation.start();
3807 }
3808 };
3809 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003810 } else {
3811 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003812 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003813 dispatchOnLauncherTransitionStart(fromView, animated, true);
3814 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003815 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003816 dispatchOnLauncherTransitionStart(toView, animated, true);
3817 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003818 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003819 }
3820
Michael Jurkae326f182011-11-21 14:05:46 -08003821 @Override
3822 public void onTrimMemory(int level) {
3823 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003824 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003825 mAppsCustomizeTabHost.onTrimMemory();
3826 }
3827 }
3828
Adam Cohened307df2013-10-02 09:37:31 -07003829 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003830 showWorkspace(animated, null);
3831 }
3832
Adam Cohened307df2013-10-02 09:37:31 -07003833 protected void showWorkspace() {
3834 showWorkspace(true);
3835 }
3836
Adam Cohened66b2b2012-01-23 17:28:51 -08003837 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003838 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003839 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003840 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003841 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003842
Winson Chungc7d2b602012-05-16 17:02:20 -07003843 // Show the search bar (only animate if we were showing the drop target bar in spring
3844 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003845 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003846 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003847 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003848
Michael Jurkab3e22d92011-10-31 15:58:33 -07003849 // Set focus to the AppsCustomize button
3850 if (mAllAppsButton != null) {
3851 mAllAppsButton.requestFocus();
3852 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003853 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003854
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003855 // Change the state *after* we've called all the transition code
3856 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003857
Winson Chung5fb63472011-02-02 17:03:37 -08003858 // Resume the auto-advance of widgets
3859 mUserPresent = true;
3860 updateRunning();
3861
alanv1d4fde62012-10-17 13:15:47 -07003862 // Send an accessibility event to announce the context change
3863 getWindow().getDecorView()
3864 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003865
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003866 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003867 }
3868
Adam Cohened307df2013-10-02 09:37:31 -07003869 void showOverviewMode(boolean animated) {
3870 mWorkspace.setVisibility(View.VISIBLE);
3871 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3872 mState = State.WORKSPACE;
3873 onWorkspaceShown(animated);
3874 }
3875
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003876 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003877 }
3878
Winson Chung82963d52013-10-09 11:20:57 -07003879 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3880 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003881 if (mState != State.WORKSPACE) return;
3882
Winson Chung82963d52013-10-09 11:20:57 -07003883 if (resetPageToZero) {
3884 mAppsCustomizeTabHost.reset();
3885 }
Winson Chungc58497e2013-09-03 17:48:37 -07003886 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003887 mAppsCustomizeTabHost.post(new Runnable() {
3888 @Override
3889 public void run() {
3890 // We post this in-case the all apps view isn't yet constructed.
3891 mAppsCustomizeTabHost.requestFocus();
3892 }
3893 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003894
Michael Jurkab3e22d92011-10-31 15:58:33 -07003895 // Change the state *after* we've called all the transition code
3896 mState = State.APPS_CUSTOMIZE;
3897
3898 // Pause the auto-advance of widgets until we are out of AllApps
3899 mUserPresent = false;
3900 updateRunning();
3901 closeFolder();
3902
3903 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003904 getWindow().getDecorView()
3905 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003906 }
3907
Adam Cohen7777d962011-08-18 18:58:38 -07003908 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003909 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003910 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003911 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003912 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003913 }
Adam Cohen7777d962011-08-18 18:58:38 -07003914
Adam Cohenad4e15c2013-10-17 16:21:35 -07003915 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003916 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003917 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3918
Winson Chunge7a03942011-08-05 15:05:12 -07003919 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003920 @Override
3921 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003922 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003923 // Before we show workspace, hide all apps again because
3924 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3925 // clean up our state transition functions
3926 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003927 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003928 } else {
3929 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003930 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003931 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003932 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003933 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003934
Michael Jurkad3ef3062010-11-23 16:23:58 -08003935 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003936 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003937 final boolean animated = true;
3938 final boolean springLoaded = true;
3939 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003940 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003941 }
3942 // Otherwise, we are not in spring loaded mode, so don't do anything.
3943 }
3944
Michael Jurkab3e22d92011-10-31 15:58:33 -07003945 void lockAllApps() {
3946 // TODO
3947 }
3948
3949 void unlockAllApps() {
3950 // TODO
3951 }
3952
Winson Chungf0ea4d32011-06-06 14:27:16 -07003953 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003954 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003955 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003956 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003957 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003958 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003959 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003960 int duration = 0;
3961 if (mSearchDropTargetBar != null) {
3962 duration = mSearchDropTargetBar.getTransitionOutDuration();
3963 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003964 mHotseat.animate().alpha(0f).setDuration(duration);
3965 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003966 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003967 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003968 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003969 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003970 }
3971
Patrick Dubroy5f445422011-02-18 14:35:21 -08003972 /**
3973 * Add an item from all apps or customize onto the given workspace screen.
3974 * If layout is null, add to the current screen.
3975 */
3976 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003977 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003978 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003979 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003980 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003981
Winson Chungdff8ebb2011-09-08 17:25:31 -07003982 /** Maps the current orientation to an index for referencing orientation correct global icons */
3983 private int getCurrentOrientationIndexForGlobalIcons() {
3984 // default - 0, landscape - 1
3985 switch (getResources().getConfiguration().orientation) {
3986 case Configuration.ORIENTATION_LANDSCAPE:
3987 return 1;
3988 default:
3989 return 0;
3990 }
3991 }
3992
Michael Jurkaae65ee32012-04-30 11:45:54 -07003993 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003994 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003995 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003996 // Look for the toolbar icon specified in the activity meta-data
3997 Bundle metaData = packageManager.getActivityInfo(
3998 activityName, PackageManager.GET_META_DATA).metaData;
3999 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07004000 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004001 if (iconResId != 0) {
4002 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004003 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004004 }
4005 }
4006 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07004007 // This can happen if the activity defines an invalid drawable
4008 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
4009 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01004010 } catch (Resources.NotFoundException nfe) {
4011 // This can happen if the activity defines an invalid drawable
4012 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
4013 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004014 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004015 return null;
4016 }
4017
4018 // if successful in getting icon, return it; otherwise, set button to use default drawable
4019 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004020 int buttonId, ComponentName activityName, int fallbackDrawableId,
4021 String toolbarResourceName) {
4022 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07004023 Resources r = getResources();
4024 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
4025 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004026
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004027 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004028 // If we were unable to find the icon via the meta-data, use a generic one
4029 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07004030 toolbarIcon = r.getDrawable(fallbackDrawableId);
4031 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004032 if (button != null) {
4033 button.setCompoundDrawables(toolbarIcon, null, null, null);
4034 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004035 return null;
4036 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07004037 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004038 if (button != null) {
4039 button.setCompoundDrawables(toolbarIcon, null, null, null);
4040 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004041 return toolbarIcon.getConstantState();
4042 }
4043 }
4044
4045 // if successful in getting icon, return it; otherwise, set button to use default drawable
4046 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004047 int buttonId, ComponentName activityName, int fallbackDrawableId,
4048 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004049 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07004050 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004051
Michael Jurka19e0fc52011-07-22 18:00:21 -07004052 if (button != null) {
4053 // If we were unable to find the icon via the meta-data, use a
4054 // generic one
4055 if (toolbarIcon == null) {
4056 button.setImageResource(fallbackDrawableId);
4057 } else {
4058 button.setImageDrawable(toolbarIcon);
4059 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004060 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07004061
4062 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
4063
Michael Jurka0423dcf2010-10-05 14:56:18 -07004064 }
4065
Winson Chung1b884092012-06-08 17:13:02 -07004066 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004067 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07004068 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004069 }
4070
Michael Jurkae7bf83b2010-12-14 18:02:21 -08004071 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08004072 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08004073 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08004074 }
4075
Winson Chungbb185bd2011-11-21 12:31:42 -08004076 private void invalidatePressedFocusedStates(View container, View button) {
4077 if (container instanceof HolographicLinearLayout) {
4078 HolographicLinearLayout layout = (HolographicLinearLayout) container;
4079 layout.invalidatePressedFocusedStates();
4080 } else if (button instanceof HolographicImageView) {
4081 HolographicImageView view = (HolographicImageView) button;
4082 view.invalidatePressedFocusedStates();
4083 }
4084 }
4085
Cristina Stancu476493b2013-08-07 17:20:14 +01004086 public View getQsbBar() {
Adam Cohen9211d422014-10-07 18:14:53 -07004087 if (mLauncherCallbacks != null) {
4088 View qsb = mLauncherCallbacks.getQsbBar();
4089 if (qsb != null) {
4090 return qsb;
4091 }
4092 }
4093
Adam Cohen24ce0b32014-01-14 16:18:14 -08004094 if (mQsb == null) {
4095 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
4096 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01004097 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08004098 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01004099 }
4100
4101 protected boolean updateGlobalSearchIcon() {
Adam Cohen9211d422014-10-07 18:14:53 -07004102 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4103 return true;
4104 }
4105
Winson Chungc51db6a2011-10-05 11:44:49 -07004106 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004107 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07004108 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004109 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004110
Winson Chung1cad91e2011-05-25 17:41:01 -07004111 final SearchManager searchManager =
4112 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4113 ComponentName activityName = searchManager.getGlobalSearchActivity();
4114 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004115 int coi = getCurrentOrientationIndexForGlobalIcons();
4116 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004117 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
4118 TOOLBAR_SEARCH_ICON_METADATA_NAME);
4119 if (sGlobalSearchIcon[coi] == null) {
4120 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4121 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
4122 TOOLBAR_ICON_METADATA_NAME);
4123 }
4124
Winson Chungc51db6a2011-10-05 11:44:49 -07004125 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
4126 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08004127 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004128 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004129 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004130 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07004131 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
4132 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004133 if (searchButton != null) searchButton.setVisibility(View.GONE);
4134 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004135 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004136 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004137 }
4138 }
4139
Cristina Stancu476493b2013-08-07 17:20:14 +01004140 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Adam Cohen9211d422014-10-07 18:14:53 -07004141 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) return;
Winson Chungbb185bd2011-11-21 12:31:42 -08004142 final View searchButtonContainer = findViewById(R.id.search_button_container);
4143 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004144 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004145 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004146 }
4147
Cristina Stancu476493b2013-08-07 17:20:14 +01004148 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Adam Cohen9211d422014-10-07 18:14:53 -07004149 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4150 return true;
4151 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004152 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004153 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004154
Winson Chungc51db6a2011-10-05 11:44:49 -07004155 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07004156 final SearchManager searchManager =
4157 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4158 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
4159
4160 ComponentName activityName = null;
4161 if (globalSearchActivity != null) {
4162 // Check if the global search activity handles voice search
4163 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4164 intent.setPackage(globalSearchActivity.getPackageName());
4165 activityName = intent.resolveActivity(getPackageManager());
4166 }
4167
4168 if (activityName == null) {
4169 // Fallback: check if an activity other than the global search activity
4170 // resolves this
4171 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4172 activityName = intent.resolveActivity(getPackageManager());
4173 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004174 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004175 int coi = getCurrentOrientationIndexForGlobalIcons();
4176 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004177 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4178 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
4179 if (sVoiceSearchIcon[coi] == null) {
4180 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4181 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4182 TOOLBAR_ICON_METADATA_NAME);
4183 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004184 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07004185 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07004186 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08004187 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004188 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004189 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07004190 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004191 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004192 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004193 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004194 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07004195 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004196
Cristina Stancu476493b2013-08-07 17:20:14 +01004197 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Adam Cohen9211d422014-10-07 18:14:53 -07004198 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4199 return;
4200 }
4201
Winson Chungbb185bd2011-11-21 12:31:42 -08004202 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
4203 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004204 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004205 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004206 }
4207
Winson Chung5841efa2013-09-30 18:06:44 -07004208 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Adam Cohen9211d422014-10-07 18:14:53 -07004209 if (mLauncherCallbacks != null) {
4210 forceDisableVoiceButtonProxy |= mLauncherCallbacks.forceDisableVoiceButtonProxy();
4211 }
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004212 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
4213 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07004214 boolean visible = !forceDisableVoiceButtonProxy &&
4215 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004216 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004217 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004218 }
4219 }
Winson Chung5841efa2013-09-30 18:06:44 -07004220
4221 /**
4222 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
4223 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
4224 */
4225 public void disableVoiceButtonProxy(boolean disabled) {
4226 updateVoiceButtonProxyVisible(disabled);
4227 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08004228
Michael Jurkad7c28052012-04-27 15:43:36 -07004229 @Override
4230 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004231 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004232 final List<CharSequence> text = event.getText();
4233 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004234 // Populate event with a fake title based on the current state.
4235 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004236 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004237 } else {
4238 text.add(getString(R.string.all_apps_home_button_label));
4239 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004240 return result;
4241 }
4242
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004243 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004244 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004245 */
4246 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4247 @Override
4248 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004249 closeSystemDialogs();
4250 }
4251 }
4252
4253 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004254 * Receives notifications whenever the appwidgets are reset.
4255 */
4256 private class AppWidgetResetObserver extends ContentObserver {
4257 public AppWidgetResetObserver() {
4258 super(new Handler());
4259 }
4260
4261 @Override
4262 public void onChange(boolean selfChange) {
4263 onAppWidgetReset();
4264 }
4265 }
4266
4267 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004268 * If the activity is currently paused, signal that we need to run the passed Runnable
4269 * in onResume.
4270 *
4271 * This needs to be called from incoming places where resources might have been loaded
4272 * while we are paused. That is becaues the Configuration might be wrong
4273 * when we're not running, and if it comes back to what it was when we
4274 * were paused, we are not restarted.
4275 *
4276 * Implementation of the method from LauncherModel.Callbacks.
4277 *
4278 * @return true if we are currently paused. The caller might be able to
4279 * skip some work in that case since we will come back again.
4280 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004281 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004282 if (mPaused) {
4283 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004284 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004285 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004286 }
4287 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004288 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004289 return true;
4290 } else {
4291 return false;
4292 }
4293 }
4294
Michael Jurkac402cd92013-05-20 15:49:32 +02004295 private boolean waitUntilResume(Runnable run) {
4296 return waitUntilResume(run, false);
4297 }
4298
Michael Jurka1e2f4652013-07-08 18:03:46 -07004299 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004300 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004301 }
4302
Michael Jurka7607c2f2013-04-03 14:33:19 -07004303 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004304 * If the activity is currently paused, signal that we need to re-run the loader
4305 * in onResume.
4306 *
4307 * This needs to be called from incoming places where resources might have been loaded
4308 * while we are paused. That is becaues the Configuration might be wrong
4309 * when we're not running, and if it comes back to what it was when we
4310 * were paused, we are not restarted.
4311 *
4312 * Implementation of the method from LauncherModel.Callbacks.
4313 *
4314 * @return true if we are currently paused. The caller might be able to
4315 * skip some work in that case since we will come back again.
4316 */
4317 public boolean setLoadOnResume() {
4318 if (mPaused) {
4319 Log.i(TAG, "setLoadOnResume");
4320 mOnResumeNeedsLoad = true;
4321 return true;
4322 } else {
4323 return false;
4324 }
4325 }
4326
4327 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004328 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004329 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004330 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004331 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004332 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004333 } else {
4334 return SCREEN_COUNT / 2;
4335 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004336 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004337
Joe Onorato9c1289c2009-08-17 11:03:03 -04004338 /**
4339 * Refreshes the shortcuts shown on the workspace.
4340 *
4341 * Implementation of the method from LauncherModel.Callbacks.
4342 */
4343 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004344 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004345
Michael Jurka7607c2f2013-04-03 14:33:19 -07004346 // If we're starting binding all over again, clear any bind calls we'd postponed in
4347 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4348 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004349 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004350
Winson Chungd64d1762013-08-20 14:37:16 -07004351 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004352 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004353 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004354
Michael Jurka05bf6442011-11-30 20:28:53 -08004355 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004356 if (mHotseat != null) {
4357 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004358 }
4359 }
4360
Adam Cohendcd297f2013-06-18 13:13:40 -07004361 @Override
4362 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004363 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004364
Adam Cohen5084cba2013-09-03 12:01:16 -07004365 // If there are no screens, we need to have an empty screen
4366 if (orderedScreenIds.size() == 0) {
4367 mWorkspace.addExtraEmptyScreen();
4368 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004369
4370 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004371 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004372 if (hasCustomContentToLeft()) {
4373 mWorkspace.createCustomContentContainer();
4374 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004375 }
Winson Chung64359a52013-07-08 17:17:08 -07004376 }
4377
4378 @Override
4379 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004380 // Log to disk
4381 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4382 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4383 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004384 int count = orderedScreenIds.size();
4385 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004386 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004387 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004388 }
4389
Adam Cohen39a06042013-07-19 14:30:12 -07004390 private boolean shouldShowWeightWatcher() {
4391 String spKey = LauncherAppState.getSharedPreferencesKey();
4392 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004393 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004394
4395 return show;
4396 }
4397
4398 private void toggleShowWeightWatcher() {
4399 String spKey = LauncherAppState.getSharedPreferencesKey();
4400 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4401 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4402
4403 show = !show;
4404
4405 SharedPreferences.Editor editor = sp.edit();
4406 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4407 editor.commit();
4408
4409 if (mWeightWatcher != null) {
4410 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4411 }
4412 }
4413
Winson Chungd64d1762013-08-20 14:37:16 -07004414 public void bindAppsAdded(final ArrayList<Long> newScreens,
4415 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004416 final ArrayList<ItemInfo> addAnimated,
4417 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004418 Runnable r = new Runnable() {
4419 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004420 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004421 }
4422 };
4423 if (waitUntilResume(r)) {
4424 return;
4425 }
4426
Winson Chungd64d1762013-08-20 14:37:16 -07004427 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004428 if (newScreens != null) {
4429 bindAddScreens(newScreens);
4430 }
Winson Chungd64d1762013-08-20 14:37:16 -07004431
4432 // We add the items without animation on non-visible pages, and with
4433 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004434 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004435 bindItems(addNotAnimated, 0,
4436 addNotAnimated.size(), false);
4437 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004438 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004439 bindItems(addAnimated, 0,
4440 addAnimated.size(), true);
4441 }
Winson Chungc58497e2013-09-03 17:48:37 -07004442
Winson Chung87412982013-10-03 18:34:14 -07004443 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004444 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004445
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004446 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004447 addedApps != null && mAppsCustomizeContent != null) {
4448 mAppsCustomizeContent.addApps(addedApps);
4449 }
Winson Chungd64d1762013-08-20 14:37:16 -07004450 }
4451
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004452 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004453 * Bind the items start-end from the list.
4454 *
4455 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004456 */
Winson Chung64359a52013-07-08 17:17:08 -07004457 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4458 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004459 Runnable r = new Runnable() {
4460 public void run() {
4461 bindItems(shortcuts, start, end, forceAnimateIcons);
4462 }
4463 };
4464 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004465 return;
4466 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004467
Winson Chungf0c6ae02012-03-21 16:10:31 -07004468 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004469 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4470 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004471 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004472 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004473 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004474 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004475 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004476
4477 // Short circuit if we are loading dock items for a configuration which has no dock
4478 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4479 mHotseat == null) {
4480 continue;
4481 }
4482
Joe Onorato9c1289c2009-08-17 11:03:03 -04004483 switch (item.itemType) {
4484 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4485 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004486 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004487 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004488
Winson Chung64359a52013-07-08 17:17:08 -07004489 /*
4490 * TODO: FIX collision case
4491 */
Winson Chungce376632013-07-11 16:12:41 -07004492 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4493 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4494 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004495 View v = cl.getChildAt(item.cellX, item.cellY);
4496 Object tag = v.getTag();
4497 String desc = "Collision while binding workspace item: " + item
4498 + ". Collides with " + tag;
4499 if (LauncherAppState.isDogfoodBuild()) {
4500 throw (new RuntimeException(desc));
4501 } else {
4502 Log.d(TAG, desc);
4503 }
Winson Chungce376632013-07-11 16:12:41 -07004504 }
Winson Chung64359a52013-07-08 17:17:08 -07004505 }
4506
Adam Cohendcd297f2013-06-18 13:13:40 -07004507 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4508 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004509 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004510 // Animate all the applications up now
4511 shortcut.setAlpha(0f);
4512 shortcut.setScaleX(0f);
4513 shortcut.setScaleY(0f);
4514 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004515 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004516 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004517 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004518 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004519 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004520 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004521 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004522 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4523 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004524 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004525 default:
4526 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004527 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004528 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004529
Winson Chung997a9232013-07-24 15:33:46 -07004530 if (animateIcons) {
4531 // Animate to the correct page
4532 if (newShortcutsScreenId > -1) {
4533 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004534 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004535 final Runnable startBounceAnimRunnable = new Runnable() {
4536 public void run() {
4537 anim.playTogether(bounceAnims);
4538 anim.start();
4539 }
4540 };
Winson Chung997a9232013-07-24 15:33:46 -07004541 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004542 // We post the animation slightly delayed to prevent slowdowns
4543 // when we are loading right after we return to launcher.
4544 mWorkspace.postDelayed(new Runnable() {
4545 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004546 if (mWorkspace != null) {
4547 mWorkspace.snapToPage(newScreenIndex);
4548 mWorkspace.postDelayed(startBounceAnimRunnable,
4549 NEW_APPS_ANIMATION_DELAY);
4550 }
Winson Chung94d67682013-09-25 16:29:40 -07004551 }
4552 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004553 } else {
4554 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004555 }
4556 }
Winson Chung64359a52013-07-08 17:17:08 -07004557 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004558 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004559 }
4560
Joe Onorato9c1289c2009-08-17 11:03:03 -04004561 /**
4562 * Implementation of the method from LauncherModel.Callbacks.
4563 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004564 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004565 Runnable r = new Runnable() {
4566 public void run() {
4567 bindFolders(folders);
4568 }
4569 };
4570 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004571 return;
4572 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004573 sFolders.clear();
4574 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004575 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004576
4577 /**
4578 * Add the views for a widget to the workspace.
4579 *
4580 * Implementation of the method from LauncherModel.Callbacks.
4581 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004582 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004583 Runnable r = new Runnable() {
4584 public void run() {
4585 bindAppWidget(item);
4586 }
4587 };
4588 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004589 return;
4590 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004591
Daniel Sandler843e8602010-06-07 14:59:01 -04004592 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4593 if (DEBUG_WIDGETS) {
4594 Log.d(TAG, "bindAppWidget: " + item);
4595 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004596 final Workspace workspace = mWorkspace;
4597
Sunny Goyalff572272014-07-23 13:58:07 -07004598 AppWidgetProviderInfo appWidgetInfo;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004599 if (!mIsSafeModeEnabled
4600 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4601 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004602
4603 appWidgetInfo = mModel.findAppWidgetProviderInfoWithComponent(this, item.providerName);
4604 if (appWidgetInfo == null) {
4605 if (DEBUG_WIDGETS) {
4606 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4607 + " belongs to component " + item.providerName
4608 + ", as the povider is null");
4609 }
4610 LauncherModel.deleteItemFromDatabase(this, item);
4611 return;
4612 }
4613 // Note: This assumes that the id remap broadcast is received before this step.
4614 // If that is not the case, the id remap will be ignored and user may see the
4615 // click to setup view.
4616 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null, null);
4617 pendingInfo.spanX = item.spanX;
4618 pendingInfo.spanY = item.spanY;
4619 pendingInfo.minSpanX = item.minSpanX;
4620 pendingInfo.minSpanY = item.minSpanY;
4621 Bundle options =
4622 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4623
Sunny Goyalff572272014-07-23 13:58:07 -07004624 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004625 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4626 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004627
4628 // TODO consider showing a permission dialog when the widget is clicked.
4629 if (!success) {
4630 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4631 if (DEBUG_WIDGETS) {
4632 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4633 + " belongs to component " + item.providerName
4634 + ", as the launcher is unable to bing a new widget id");
4635 }
4636 LauncherModel.deleteItemFromDatabase(this, item);
4637 return;
4638 }
4639
4640 item.appWidgetId = newWidgetId;
4641
4642 // If the widget has a configure activity, it is still needs to set it up, otherwise
4643 // the widget is ready to go.
4644 item.restoreStatus = (appWidgetInfo.configure == null)
4645 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4646 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4647
4648 LauncherModel.updateItemInDatabase(this, item);
4649 }
4650
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004651 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004652 final int appWidgetId = item.appWidgetId;
4653 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4654 if (DEBUG_WIDGETS) {
4655 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4656 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004657
Sunny Goyal651077b2014-06-30 14:15:31 -07004658 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4659 } else {
4660 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004661 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4662 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004663 view.updateIcon(mIconCache);
4664 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004665 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004666 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004667 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004668
Joe Onorato9c1289c2009-08-17 11:03:03 -04004669 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004670 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004671
Adam Cohendcd297f2013-06-18 13:13:40 -07004672 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004673 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004674 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4675
Joe Onorato9c1289c2009-08-17 11:03:03 -04004676 workspace.requestLayout();
4677
Daniel Sandler843e8602010-06-07 14:59:01 -04004678 if (DEBUG_WIDGETS) {
4679 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4680 + (SystemClock.uptimeMillis()-start) + "ms");
4681 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004682 }
4683
Sunny Goyalff572272014-07-23 13:58:07 -07004684 /**
4685 * Restores a pending widget.
4686 *
4687 * @param appWidgetId The app widget id
4688 * @param cellInfo The position on screen where to create the widget.
4689 */
4690 private void completeRestoreAppWidget(final int appWidgetId) {
4691 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4692 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4693 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4694 return;
4695 }
4696
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004697 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004698 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4699
4700 mWorkspace.reinflateWidgetsIfNecessary();
4701 LauncherModel.updateItemInDatabase(this, info);
4702 }
4703
Adam Cohen1462de32012-07-24 22:34:36 -07004704 public void onPageBoundSynchronously(int page) {
4705 mSynchronouslyBoundPages.add(page);
4706 }
4707
Joe Onorato9c1289c2009-08-17 11:03:03 -04004708 /**
4709 * Callback saying that there aren't any more items to bind.
4710 *
4711 * Implementation of the method from LauncherModel.Callbacks.
4712 */
Adam Cohene25af792013-06-06 23:08:25 -07004713 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004714 Runnable r = new Runnable() {
4715 public void run() {
4716 finishBindingItems(upgradePath);
4717 }
4718 };
4719 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004720 return;
4721 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004722 if (mSavedState != null) {
4723 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004724 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004725 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004726 mSavedState = null;
4727 }
4728
Adam Cohen1462de32012-07-24 22:34:36 -07004729 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004730
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004731 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004732 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004733
4734 // If we received the result of any pending adds while the loader was running (e.g. the
4735 // widget configuration forced an orientation change), process them now.
4736 if (sPendingAddItem != null) {
4737 final long screenId = completeAdd(sPendingAddItem);
4738
4739 // TODO: this moves the user to the page where the pending item was added. Ideally,
4740 // the screen would be guaranteed to exist after bind, and the page would be set through
4741 // the workspace restore process.
4742 mWorkspace.post(new Runnable() {
4743 @Override
4744 public void run() {
4745 mWorkspace.snapToScreenId(screenId);
4746 }
4747 });
4748 sPendingAddItem = null;
4749 }
4750
Adam Cohene25af792013-06-06 23:08:25 -07004751 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004752 mWorkspace.getUniqueComponents(true, null);
4753 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004754 }
Sunny Goyale755d462014-07-22 13:48:29 -07004755 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004756
4757 if (mLauncherCallbacks != null) {
4758 mLauncherCallbacks.finishBindingItems(upgradePath);
4759 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004760 }
4761
Adam Cohen3ed316a2014-07-23 18:21:20 -07004762 private void sendLoadingCompleteBroadcastIfNecessary() {
4763 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4764 String permission =
4765 getResources().getString(R.string.receive_first_load_broadcast_permission);
4766 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4767 sendBroadcast(intent, permission);
4768 SharedPreferences.Editor editor = mSharedPrefs.edit();
4769 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4770 editor.apply();
4771 }
4772 }
4773
Winson Chunga0b7e862013-09-05 16:03:15 -07004774 public boolean isAllAppsButtonRank(int rank) {
4775 if (mHotseat != null) {
4776 return mHotseat.isAllAppsButtonRank(rank);
4777 }
4778 return false;
4779 }
4780
Winson Chunga2413752012-04-03 14:22:34 -07004781 private boolean canRunNewAppsAnimation() {
4782 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4783 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4784 }
4785
Winson Chungc9168342013-06-26 14:54:55 -07004786 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4787 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4788 PropertyValuesHolder.ofFloat("alpha", 1f),
4789 PropertyValuesHolder.ofFloat("scaleX", 1f),
4790 PropertyValuesHolder.ofFloat("scaleY", 1f));
4791 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4792 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4793 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4794 return bounceAnim;
4795 }
4796
Adam Cohen27772732013-11-11 14:00:56 +00004797 public boolean useVerticalBarLayout() {
4798 return LauncherAppState.getInstance().getDynamicGrid().
4799 getDeviceProfile().isVerticalBarLayout();
4800 }
4801
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004802 protected Rect getSearchBarBounds() {
4803 return LauncherAppState.getInstance().getDynamicGrid().
4804 getDeviceProfile().getSearchBarBounds();
4805 }
4806
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004807 @Override
4808 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004809 boolean searchVisible = updateGlobalSearchIcon();
4810 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004811 if (mSearchDropTargetBar != null) {
4812 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4813 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004814 }
4815
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004816 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004817 * Add the icons for all apps.
4818 *
4819 * Implementation of the method from LauncherModel.Callbacks.
4820 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004821 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004822 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004823 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4824 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4825 getHotseat().addAllAppsFolder(mIconCache, apps,
4826 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4827 }
4828 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004829 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004830 if (mAppsCustomizeContent != null) {
4831 mAppsCustomizeContent.onPackagesUpdated(
4832 LauncherModel.getSortedWidgetsAndShortcuts(this));
4833 }
Winson Chungc58497e2013-09-03 17:48:37 -07004834 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004835 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004836 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004837 mAppsCustomizeContent.onPackagesUpdated(
4838 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004839 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004840 }
Adam Cohen9211d422014-10-07 18:14:53 -07004841 if (mLauncherCallbacks != null) {
4842 mLauncherCallbacks.bindAllApplications(apps);
4843 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004844 }
4845
4846 /**
4847 * A package was updated.
4848 *
4849 * Implementation of the method from LauncherModel.Callbacks.
4850 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004851 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004852 Runnable r = new Runnable() {
4853 public void run() {
4854 bindAppsUpdated(apps);
4855 }
4856 };
4857 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004858 return;
4859 }
4860
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004861 if (mWorkspace != null) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004862 mWorkspace.updateShortcutsAndWidgets(apps);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004863 }
Winson Chungc58497e2013-09-03 17:48:37 -07004864
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004865 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004866 mAppsCustomizeContent != null) {
4867 mAppsCustomizeContent.updateApps(apps);
4868 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004869 }
4870
4871 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004872 * Some shortcuts were updated in the background.
4873 *
4874 * Implementation of the method from LauncherModel.Callbacks.
4875 */
4876 public void bindShortcutsUpdated(final ArrayList<ShortcutInfo> shortcuts) {
4877 Runnable r = new Runnable() {
4878 public void run() {
4879 bindShortcutsUpdated(shortcuts);
4880 }
4881 };
4882 if (waitUntilResume(r)) {
4883 return;
4884 }
4885
4886 if (mWorkspace != null) {
4887 mWorkspace.updateShortcuts(shortcuts);
4888 }
4889 }
4890
4891 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004892 * Update the state of a package, typically related to install state.
4893 *
4894 * Implementation of the method from LauncherModel.Callbacks.
4895 */
Sunny Goyale755d462014-07-22 13:48:29 -07004896 @Override
4897 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004898 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004899 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004900 }
4901 }
4902
4903 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004904 * Update the label and icon of all the icons in a package
4905 *
4906 * Implementation of the method from LauncherModel.Callbacks.
4907 */
4908 @Override
4909 public void updatePackageBadge(String packageName) {
4910 if (mWorkspace != null) {
4911 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4912 }
4913 }
4914
4915 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004916 * A package was uninstalled. We take both the super set of packageNames
4917 * in addition to specific applications to remove, the reason being that
4918 * this can be called when a package is updated as well. In that scenario,
4919 * we only remove specific components from the workspace, where as
4920 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004921 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004922 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004923 * Implementation of the method from LauncherModel.Callbacks.
4924 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004925 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004926 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004927 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004928 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004929 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004930 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004931 }
Winson Chungd64d1762013-08-20 14:37:16 -07004932 };
4933 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004934 return;
4935 }
4936
Sunny Goyal1a745e82014-10-02 15:58:31 -07004937 if (reason == 0) {
4938 if (!packageNames.isEmpty()) {
4939 mWorkspace.removeItemsByPackageName(packageNames, user);
4940 }
4941 if (!appInfos.isEmpty()) {
4942 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
4943 }
4944 } else {
4945 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004946 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004947
Winson Chunga1820962011-10-03 16:31:06 -07004948 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004949 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004950
Winson Chungdf95eb12013-10-16 14:57:07 -07004951 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004952 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004953 mAppsCustomizeContent != null) {
4954 mAppsCustomizeContent.removeApps(appInfos);
4955 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004956 }
Joe Onoratobe386092009-11-17 17:32:16 -08004957
4958 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004959 * A number of packages were updated.
4960 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004961 private ArrayList<Object> mWidgetsAndShortcuts;
4962 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004963 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004964 bindPackagesUpdated(mWidgetsAndShortcuts);
4965 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004966 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004967 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004968 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4969 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4970 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004971 return;
4972 }
4973
Winson Chung64359a52013-07-08 17:17:08 -07004974 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004975 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004976 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004977 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004978 }
4979
Winson Chung400438b2011-01-16 17:53:48 -08004980 private int mapConfigurationOriActivityInfoOri(int configOri) {
4981 final Display d = getWindowManager().getDefaultDisplay();
4982 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4983 switch (d.getRotation()) {
4984 case Surface.ROTATION_0:
4985 case Surface.ROTATION_180:
4986 // We are currently in the same basic orientation as the natural orientation
4987 naturalOri = configOri;
4988 break;
4989 case Surface.ROTATION_90:
4990 case Surface.ROTATION_270:
4991 // We are currently in the other basic orientation to the natural orientation
4992 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4993 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4994 break;
4995 }
4996
4997 int[] oriMap = {
4998 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4999 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
5000 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
5001 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
5002 };
5003 // Since the map starts at portrait, we need to offset if this device's natural orientation
5004 // is landscape.
5005 int indexOffset = 0;
5006 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
5007 indexOffset = 1;
5008 }
5009 return oriMap[(d.getRotation() + indexOffset) % 4];
5010 }
Adam Cohen446e9402011-09-15 18:21:21 -07005011
Winson Chung4b919f82012-05-01 10:44:08 -07005012 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08005013 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07005014 getResources().getBoolean(R.bool.allow_rotation);
5015 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08005016 }
Winson Chung4b919f82012-05-01 10:44:08 -07005017 public void lockScreenOrientation() {
5018 if (isRotationEnabled()) {
5019 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
5020 .getConfiguration().orientation));
5021 }
5022 }
5023 public void unlockScreenOrientation(boolean immediate) {
5024 if (isRotationEnabled()) {
5025 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07005026 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07005027 } else {
5028 mHandler.postDelayed(new Runnable() {
5029 public void run() {
5030 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
5031 }
5032 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07005033 }
Winson Chung4b919f82012-05-01 10:44:08 -07005034 }
Winson Chung400438b2011-01-16 17:53:48 -08005035 }
5036
Winson Chunge43a1e72014-01-15 10:33:02 -08005037 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07005038 if (mLauncherCallbacks != null) {
5039 return mLauncherCallbacks.isLauncherPreinstalled();
5040 }
Winson Chunge43a1e72014-01-15 10:33:02 -08005041 PackageManager pm = getPackageManager();
5042 try {
5043 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
5044 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
5045 return true;
5046 } else {
5047 return false;
5048 }
5049 } catch (NameNotFoundException e) {
5050 e.printStackTrace();
5051 return false;
5052 }
5053 }
5054
Adam Cohenea90f832014-05-21 15:03:34 -07005055 /**
5056 * This method indicates whether or not we should suggest default wallpaper dimensions
5057 * when our wallpaper cropper was not yet used to set a wallpaper.
5058 */
5059 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07005060 if (mLauncherCallbacks != null) {
5061 return mLauncherCallbacks.overrideWallpaperDimensions();
5062 }
Adam Cohenea90f832014-05-21 15:03:34 -07005063 return true;
5064 }
5065
Adam Cohen432609a2014-03-13 17:03:22 -07005066 /**
5067 * To be overridden by subclasses to indicate that there is an activity to launch
5068 * before showing the standard launcher experience.
5069 */
5070 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07005071 if (mLauncherCallbacks != null) {
5072 return mLauncherCallbacks.hasFirstRunActivity();
5073 }
Adam Cohen432609a2014-03-13 17:03:22 -07005074 return false;
5075 }
5076
5077 /**
5078 * To be overridden by subclasses to launch any first run activity
5079 */
5080 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07005081 if (mLauncherCallbacks != null) {
5082 return mLauncherCallbacks.getFirstRunActivity();
5083 }
Adam Cohen432609a2014-03-13 17:03:22 -07005084 return null;
5085 }
5086
Winson Chunga6945242014-01-08 14:04:34 -08005087 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08005088 return !ActivityManager.isRunningInTestHarness() &&
5089 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08005090 }
5091
Adam Cohen4a9423d2014-03-28 14:22:31 -07005092 protected boolean hasRunFirstRunActivity() {
5093 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
5094 }
5095
Adam Cohen432609a2014-03-13 17:03:22 -07005096 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08005097 if (shouldRunFirstRunActivity() &&
5098 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08005099 Intent firstRunIntent = getFirstRunActivity();
5100 if (firstRunIntent != null) {
5101 startActivity(firstRunIntent);
5102 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07005103 return true;
Winson Chunga6945242014-01-08 14:04:34 -08005104 }
5105 }
Adam Cohen432609a2014-03-13 17:03:22 -07005106 return false;
Winson Chunga6945242014-01-08 14:04:34 -08005107 }
5108
5109 private void markFirstRunActivityShown() {
5110 SharedPreferences.Editor editor = mSharedPrefs.edit();
5111 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
5112 editor.apply();
5113 }
5114
Adam Cohen432609a2014-03-13 17:03:22 -07005115 /**
5116 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
5117 * screen that must be displayed and dismissed.
5118 */
5119 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07005120 if (mLauncherCallbacks != null) {
5121 return mLauncherCallbacks.hasDismissableIntroScreen();
5122 }
Adam Cohen432609a2014-03-13 17:03:22 -07005123 return false;
5124 }
5125
5126 /**
5127 * Full screen intro screen to be shown and dismissed before the launcher can be used.
5128 */
5129 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07005130 if (mLauncherCallbacks != null) {
5131 return mLauncherCallbacks.getIntroScreen();
5132 }
Adam Cohen432609a2014-03-13 17:03:22 -07005133 return null;
5134 }
5135
5136 /**
5137 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
5138 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
5139 */
5140 private boolean shouldShowIntroScreen() {
5141 return hasDismissableIntroScreen() &&
5142 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
5143 }
5144
5145 protected void showIntroScreen() {
5146 View introScreen = getIntroScreen();
5147 changeWallpaperVisiblity(false);
5148 if (introScreen != null) {
5149 mDragLayer.showOverlayView(introScreen);
5150 }
5151 }
5152
5153 public void dismissIntroScreen() {
5154 markIntroScreenDismissed();
5155 if (showFirstRunActivity()) {
5156 // We delay hiding the intro view until the first run activity is showing. This
5157 // avoids a blip.
5158 mWorkspace.postDelayed(new Runnable() {
5159 @Override
5160 public void run() {
5161 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005162 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005163 }
5164 }, ACTIVITY_START_DELAY);
5165 } else {
5166 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005167 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005168 }
5169 changeWallpaperVisiblity(true);
5170 }
5171
5172 private void markIntroScreenDismissed() {
5173 SharedPreferences.Editor editor = mSharedPrefs.edit();
5174 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
5175 editor.apply();
5176 }
5177
Sunny Goyal88d60f12014-09-08 03:47:29 -07005178 private void showFirstRunClings() {
5179 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
5180 // on the device, then we always show the first run cling experience (or if there is no
5181 // launcher2). Otherwise, we prompt the user upon started for migration
5182 LauncherClings launcherClings = new LauncherClings(this);
5183 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
5184 if (mModel.canMigrateFromOldLauncherDb(this)) {
5185 launcherClings.showMigrationCling();
5186 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005187 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005188 }
5189 }
5190 }
5191
Winson Chunga6945242014-01-08 14:04:34 -08005192 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005193 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5194 if (mHotseat != null) mHotseat.setAlpha(1f);
5195 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5196 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005197 }
5198
5199 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005200 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5201 if (mHotseat != null) mHotseat.setAlpha(0f);
5202 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5203 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005204 }
5205
Mathew Inwood72fbec12013-11-19 15:45:07 +00005206 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005207 // Called from search suggestion, not supported in other profiles.
5208 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5209 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5210 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5211 myUser);
5212 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005213 return null;
5214 }
Kenny Guyed131872014-04-30 03:02:21 +01005215 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005216 }
5217
5218 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5219 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005220 // Called from search suggestion, not supported in other profiles.
5221 return createShortcutDragInfo(shortcutIntent, caption, icon,
5222 UserHandleCompat.myUserHandle());
5223 }
5224
5225 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5226 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005227 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005228 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005229 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005230 }
5231
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005232 protected void moveWorkspaceToDefaultScreen() {
5233 mWorkspace.moveToDefaultScreen(false);
5234 }
5235
Mathew Inwood72fbec12013-11-19 15:45:07 +00005236 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5237 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005238 mWorkspace.onExternalDragStartedWithItem(dragView);
5239 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005240 }
5241
Anjali Koppalf5d29132014-02-28 13:33:27 -08005242 @Override
5243 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005244 if (mLauncherCallbacks != null) {
5245 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5246 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005247 }
5248
Winson Chung80baf5a2010-08-09 16:03:15 -07005249 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005250 * Prints out out state for debugging.
5251 */
5252 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005253 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005254 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005255 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5256 Log.d(TAG, "mRestoring=" + mRestoring);
5257 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5258 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005259 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005260 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005261
Winson Chung785d2eb2011-04-14 16:08:02 -07005262 if (mAppsCustomizeContent != null) {
5263 mAppsCustomizeContent.dumpState();
5264 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005265 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005266 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005267
5268 @Override
5269 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5270 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005271 synchronized (sDumpLogs) {
5272 writer.println(" ");
5273 writer.println("Debug logs: ");
5274 for (int i = 0; i < sDumpLogs.size(); i++) {
5275 writer.println(" " + sDumpLogs.get(i));
5276 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005277 }
Adam Cohen9211d422014-10-07 18:14:53 -07005278 if (mLauncherCallbacks != null) {
5279 mLauncherCallbacks.dump(prefix, fd, writer, args);
5280 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005281 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005282
5283 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005284 if (DEBUG_DUMP_LOG) {
5285 synchronized (sDumpLogs) {
5286 Log.d(TAG, "");
5287 Log.d(TAG, "*********************");
5288 Log.d(TAG, "Launcher debug logs: ");
5289 for (int i = 0; i < sDumpLogs.size(); i++) {
5290 Log.d(TAG, " " + sDumpLogs.get(i));
5291 }
5292 Log.d(TAG, "*********************");
5293 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005294 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005295 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005296 }
5297
5298 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005299 addDumpLog(tag, log, null, debugLog);
5300 }
5301
5302 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005303 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005304 if (e != null) {
5305 Log.d(tag, log, e);
5306 } else {
5307 Log.d(tag, log);
5308 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005309 }
Winson Chungede41292013-09-19 16:27:36 -07005310 if (DEBUG_DUMP_LOG) {
5311 sDateStamp.setTime(System.currentTimeMillis());
5312 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005313 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5314 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005315 }
Winson Chungede41292013-09-19 16:27:36 -07005316 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005317 }
5318
Winson Chungede41292013-09-19 16:27:36 -07005319 public void dumpLogsToLocalData() {
5320 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005321 new AsyncTask<Void, Void, Void>() {
5322 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005323 boolean success = false;
5324 sDateStamp.setTime(sRunStart);
5325 String FILENAME = sDateStamp.getMonth() + "-"
5326 + sDateStamp.getDay() + "_"
5327 + sDateStamp.getHours() + "-"
5328 + sDateStamp.getMinutes() + "_"
5329 + sDateStamp.getSeconds() + ".txt";
5330
5331 FileOutputStream fos = null;
5332 File outFile = null;
5333 try {
5334 outFile = new File(getFilesDir(), FILENAME);
5335 outFile.createNewFile();
5336 fos = new FileOutputStream(outFile);
5337 } catch (Exception e) {
5338 e.printStackTrace();
5339 }
5340 if (fos != null) {
5341 PrintWriter writer = new PrintWriter(fos);
5342
5343 writer.println(" ");
5344 writer.println("Debug logs: ");
5345 synchronized (sDumpLogs) {
5346 for (int i = 0; i < sDumpLogs.size(); i++) {
5347 writer.println(" " + sDumpLogs.get(i));
5348 }
5349 }
5350 writer.close();
5351 }
5352 try {
5353 if (fos != null) {
5354 fos.close();
5355 success = true;
5356 }
5357 } catch (IOException e) {
5358 e.printStackTrace();
5359 }
Michael Jurka43467462013-11-14 12:03:58 +01005360 return null;
Winson Chungede41292013-09-19 16:27:36 -07005361 }
Michael Jurka43467462013-11-14 12:03:58 +01005362 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005363 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005364 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005365}
Michael Jurka2763be32011-02-24 11:19:57 -08005366
Michael Jurkaabded662011-03-04 12:06:57 -08005367interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005368 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005369 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005370 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005371 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005372 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005373}
Dan Sandlerd5024042014-01-09 15:01:33 -05005374
5375interface DebugIntents {
5376 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5377 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005378}