blob: ae67320eb1ead0a268ed73511c170b2f35eac803 [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 Josol4fbbb3e2014-10-06 16:06:46 +0100651 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFS));
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(
674 LauncherFiles.LAUNCHER_PREFS, 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 Josol4fbbb3e2014-10-06 16:06:46 +0100683 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFS).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700684 } finally {
685 if (out != null) {
686 try {
687 out.close();
688 } catch (IOException e) {
689 // Ignore
690 }
691 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800692 }
693 }
694
Anton Hanssona2f665f2013-09-26 12:18:32 +0100695 public Stats getStats() {
696 return mStats;
697 }
698
Bjorn Bringertc459e522013-06-07 19:36:01 +0100699 public LayoutInflater getInflater() {
700 return mInflater;
701 }
702
Winson Chung36a62fe2012-05-06 18:04:42 -0700703 boolean isDraggingEnabled() {
704 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
705 // that is subsequently removed from the workspace in startBinding().
706 return !mModel.isLoadingWorkspace();
707 }
708
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800709 static int getScreen() {
710 synchronized (sLock) {
711 return sScreen;
712 }
713 }
714
715 static void setScreen(int screen) {
716 synchronized (sLock) {
717 sScreen = screen;
718 }
719 }
720
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000721 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100722 if (Build.VERSION.SDK_INT >= 17) {
723 return View.generateViewId();
724 } else {
725 // View.generateViewId() is not available. The following fallback logic is a copy
726 // of its implementation.
727 for (;;) {
728 final int result = sNextGeneratedId.get();
729 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
730 int newValue = result + 1;
731 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
732 if (sNextGeneratedId.compareAndSet(result, newValue)) {
733 return result;
734 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000735 }
736 }
737 }
738
739 public int getViewIdForItem(ItemInfo info) {
740 // This cast is safe given the > 2B range for int.
741 int itemId = (int) info.id;
742 if (mItemIdToViewId.containsKey(itemId)) {
743 return mItemIdToViewId.get(itemId);
744 }
745 int viewId = generateViewId();
746 mItemIdToViewId.put(itemId, viewId);
747 return viewId;
748 }
749
750 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700751 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
752 * a configuration step, this allows the proper animations to run after other transitions.
753 */
Adam Cohendb364c32014-05-20 14:23:40 -0700754 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700755 long screenId = args.screenId;
756 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
757 // When the screen id represents an actual screen (as opposed to a rank) we make sure
758 // that the drop page actually exists.
759 screenId = ensurePendingDropLayoutExists(args.screenId);
760 }
Adam Cohendb364c32014-05-20 14:23:40 -0700761
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800762 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800763 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700764 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700765 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700766 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800767 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700768 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700769 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700770 case REQUEST_RECONFIGURE_APPWIDGET:
771 completeRestoreAppWidget(args.appWidgetId);
772 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800773 }
Michael Jurka27614d22012-04-02 06:40:22 -0700774 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
775 // if you turned the screen off and then back while in All Apps, Launcher would not
776 // return to the workspace. Clearing mAddInfo.container here fixes this issue
777 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700778 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800779 }
780
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800781 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700782 protected void onActivityResult(
783 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700784 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700785 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700786 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800787 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700788
Adam Cohenad4e15c2013-10-17 16:21:35 -0700789 Runnable exitSpringLoaded = new Runnable() {
790 @Override
791 public void run() {
792 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
793 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
794 }
795 };
796
Michael Jurka8b805b12012-04-18 14:23:14 -0700797 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700798 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700799 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
800 if (resultCode == RESULT_CANCELED) {
801 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700802 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700803 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700804 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700805 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
806 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700807 }
808 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200809 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
810 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
811 mWorkspace.exitOverviewMode(false);
812 }
813 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700814 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200815
Adam Cohened66b2b2012-01-23 17:28:51 -0800816 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
817 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700818
Adam Cohendb364c32014-05-20 14:23:40 -0700819 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800820 // We have special handling for widgets
821 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800822 final int appWidgetId;
823 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
824 : -1;
825 if (widgetId < 0) {
826 appWidgetId = pendingAddWidgetId;
827 } else {
828 appWidgetId = widgetId;
829 }
830
Adam Cohenad4e15c2013-10-17 16:21:35 -0700831 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800832 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700833 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
834 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700835 result = RESULT_CANCELED;
836 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700837 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700838 @Override
839 public void run() {
840 exitSpringLoadedDragModeDelayed(false, 0, null);
841 }
842 };
Adam Cohendb364c32014-05-20 14:23:40 -0700843 if (workspaceLocked) {
844 // No need to remove the empty screen if we're mid-binding, as the
845 // the bind will not add the empty screen.
846 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
847 } else {
848 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
849 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
850 }
Winson Chung5aaab772012-04-27 15:24:02 -0700851 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700852 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700853 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
854 // When the screen id represents an actual screen (as opposed to a rank)
855 // we make sure that the drop page actually exists.
856 mPendingAddInfo.screenId =
857 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
858 }
Adam Cohendb364c32014-05-20 14:23:40 -0700859 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
860
861 dropLayout.setDropPending(true);
862 final Runnable onComplete = new Runnable() {
863 @Override
864 public void run() {
865 completeTwoStageWidgetDrop(resultCode, appWidgetId);
866 dropLayout.setDropPending(false);
867 }
868 };
869 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
870 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
871 } else {
872 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
873 mPendingAddInfo);
874 sPendingAddItem = args;
875 }
Winson Chung5aaab772012-04-27 15:24:02 -0700876 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800877 return;
878 }
879
Sunny Goyalff572272014-07-23 13:58:07 -0700880 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
881 if (resultCode == RESULT_OK) {
882 // Update the widget view.
883 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
884 pendingAddWidgetId, mPendingAddInfo);
885 if (workspaceLocked) {
886 sPendingAddItem = args;
887 } else {
888 completeAdd(args);
889 }
890 }
891 // Leave the widget in the pending state if the user canceled the configure.
892 return;
893 }
894
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800895 // The pattern used here is that a user PICKs a specific application,
896 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700897
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800898 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
899 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700900 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700901 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
902 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800903 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700904 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800905 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700906 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700907 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
908 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800909 }
Adam Cohen00074722013-10-11 17:46:09 -0700910 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700911 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700912 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800913 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800915 }
916
Adam Cohendb364c32014-05-20 14:23:40 -0700917 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
918 appWidgetId, ItemInfo info) {
919 PendingAddArguments args = new PendingAddArguments();
920 args.requestCode = requestCode;
921 args.intent = data;
922 args.container = info.container;
923 args.screenId = info.screenId;
924 args.cellX = info.cellX;
925 args.cellY = info.cellY;
926 args.appWidgetId = appWidgetId;
927 return args;
928 }
929
930 /**
931 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
932 *
933 * @param screenId the screen id to check
934 * @return the new screen, or screenId if it exists
935 */
936 private long ensurePendingDropLayoutExists(long screenId) {
937 CellLayout dropLayout =
938 (CellLayout) mWorkspace.getScreenWithId(screenId);
939 if (dropLayout == null) {
940 // it's possible that the add screen was removed because it was
941 // empty and a re-bind occurred
942 mWorkspace.addExtraEmptyScreen();
943 return mWorkspace.commitExtraEmptyScreen();
944 } else {
945 return screenId;
946 }
947 }
948
Adam Cohened66b2b2012-01-23 17:28:51 -0800949 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700950 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700951 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800952 Runnable onCompleteRunnable = null;
953 int animationType = 0;
954
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700955 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800956 if (resultCode == RESULT_OK) {
957 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
958 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700959 mPendingAddWidgetInfo);
960 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800961 onCompleteRunnable = new Runnable() {
962 @Override
963 public void run() {
964 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700965 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700966 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
967 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800968 }
969 };
970 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800971 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800972 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800973 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700974 if (mDragLayer.getAnimatedView() != null) {
975 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
976 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
977 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700978 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700979 // The animated view may be null in the case of a rotation during widget configuration
980 onCompleteRunnable.run();
981 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800982 }
983
984 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700985 protected void onStop() {
986 super.onStop();
987 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700988
989 if (mLauncherCallbacks != null) {
990 mLauncherCallbacks.onStop();
991 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700992 }
993
994 @Override
995 protected void onStart() {
996 super.onStart();
997 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700998
999 if (mLauncherCallbacks != null) {
1000 mLauncherCallbacks.onStart();
1001 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001002 }
1003
1004 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001005 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001006 long startTime = 0;
1007 if (DEBUG_RESUME_TIME) {
1008 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001009 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001010 }
Adam Cohen9211d422014-10-07 18:14:53 -07001011
1012 if (mLauncherCallbacks != null) {
1013 mLauncherCallbacks.preOnResume();
1014 }
1015
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001016 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001017
Winson Chung4a2afa32012-07-19 14:53:05 -07001018 // Restore the previous launcher state
1019 if (mOnResumeState == State.WORKSPACE) {
1020 showWorkspace(false);
1021 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -08001022 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001023 }
1024 mOnResumeState = State.NONE;
1025
Craig Mautner360310b2012-10-26 15:13:08 -07001026 // Background was set to gradient in onPause(), restore to black if in all apps.
1027 setWorkspaceBackground(mState == State.WORKSPACE);
1028
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001029 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001030 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001031 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001032 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001033 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001034 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001035 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001036 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001037 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1038 // execute them here
1039 long startTimeCallbacks = 0;
1040 if (DEBUG_RESUME_TIME) {
1041 startTimeCallbacks = System.currentTimeMillis();
1042 }
1043
1044 if (mAppsCustomizeContent != null) {
1045 mAppsCustomizeContent.setBulkBind(true);
1046 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001047 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1048 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001049 }
1050 if (mAppsCustomizeContent != null) {
1051 mAppsCustomizeContent.setBulkBind(false);
1052 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001053 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001054 if (DEBUG_RESUME_TIME) {
1055 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1056 (System.currentTimeMillis() - startTimeCallbacks));
1057 }
Michael Jurka447bf842013-05-15 14:52:15 +02001058 }
Michael Jurka54554252013-08-01 12:52:23 +02001059 if (mOnResumeCallbacks.size() > 0) {
1060 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1061 mOnResumeCallbacks.get(i).run();
1062 }
1063 mOnResumeCallbacks.clear();
1064 }
Winson Chunge4e50662012-01-23 14:45:13 -08001065
1066 // Reset the pressed state of icons that were locked in the press state while activities
1067 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001068 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001069 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001070 mWaitingForResume.setStayPressed(false);
1071 }
Winson Chung82963d52013-10-09 11:20:57 -07001072
Adam Cohen06dff352012-06-01 17:17:08 -07001073 // It is possible that widgets can receive updates while launcher is not in the foreground.
1074 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1075 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1076 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001077 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001078
Winson Chung780fe592013-09-26 14:48:44 -07001079 // Process any items that were added while Launcher was away.
1080 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1081
Winson Chung5841efa2013-09-30 18:06:44 -07001082 // Update the voice search button proxy
1083 updateVoiceButtonProxyVisible(false);
1084
Adam Cohenf9426d52012-06-04 17:26:21 -07001085 // Again, as with the above scenario, it's possible that one or more of the global icons
1086 // were updated in the wrong orientation.
1087 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001088 if (DEBUG_RESUME_TIME) {
1089 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1090 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001091
1092 if (mWorkspace.getCustomContentCallbacks() != null) {
1093 // If we are resuming and the custom content is the current page, we call onShow().
1094 // It is also poassible that onShow will instead be called slightly after first layout
1095 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1096 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001097 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001098 }
1099 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001100 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001101 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001102
1103 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001104
1105 if (mLauncherCallbacks != null) {
1106 mLauncherCallbacks.onResume();
1107 }
Adam Cohen06dff352012-06-01 17:17:08 -07001108 }
1109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001110 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001111 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001112 // Ensure that items added to Launcher are queued until Launcher returns
1113 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001114 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001115
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001116 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001117 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001118 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001119 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001120
1121 // We call onHide() aggressively. The custom content callbacks should be able to
1122 // debounce excess onHide calls.
1123 if (mWorkspace.getCustomContentCallbacks() != null) {
1124 mWorkspace.getCustomContentCallbacks().onHide();
1125 }
Romain Guycbb89e42009-06-08 15:52:54 -07001126
Adam Cohen9211d422014-10-07 18:14:53 -07001127 if (mLauncherCallbacks != null) {
1128 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001129 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001130 }
1131
1132 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001133 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1134 // by a onResume or by scrolling otherwise.
1135 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001136
1137 // Custom content is completely hidden
1138 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001139
1140 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1141 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001142
1143 // Indicates whether the user is allowed to scroll away from the custom content.
1144 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001145 }
1146
Jorim Jaggid017f882014-01-14 17:08:48 -08001147 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001148 if (mLauncherCallbacks != null) {
1149 return mLauncherCallbacks.hasSettings();
1150 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001151 return false;
1152 }
1153
Adam Cohenbffe7452013-07-22 18:21:45 -07001154
Adam Cohen9211d422014-10-07 18:14:53 -07001155 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001156 CustomContentCallbacks callbacks, String description) {
1157 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001158 }
1159
Adam Cohenedb40762013-07-18 16:45:45 -07001160 // The custom content needs to offset its content to account for the QSB
1161 public int getTopOffsetForCustomContent() {
1162 return mWorkspace.getPaddingTop();
1163 }
1164
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001165 @Override
1166 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001167 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001168 if (mModel.isCurrentCallbacks(this)) {
1169 mModel.stopLoader();
1170 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001171 if (mAppsCustomizeContent != null) {
1172 mAppsCustomizeContent.surrender();
1173 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001174 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001175 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001176
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001177 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001178 @Override
1179 public void onWindowFocusChanged(boolean hasFocus) {
1180 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001181 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001182
1183 if (mLauncherCallbacks != null) {
1184 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1185 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001186 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001187
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001188 private boolean acceptFilter() {
1189 final InputMethodManager inputManager = (InputMethodManager)
1190 getSystemService(Context.INPUT_METHOD_SERVICE);
1191 return !inputManager.isFullscreenMode();
1192 }
1193
1194 @Override
1195 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001196 final int uniChar = event.getUnicodeChar();
1197 final boolean handled = super.onKeyDown(keyCode, event);
1198 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1199 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001200 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1201 keyCode, event);
1202 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001203 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001204 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001205 // showSearchDialog()
1206 // If there are multiple keystrokes before the search dialog takes focus,
1207 // onSearchRequested() will be called for every keystroke,
1208 // but it is idempotent, so it's fine.
1209 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001210 }
1211 }
1212
Joe Onorato8a9625e2010-01-28 15:55:35 -08001213 // Eat the long press event so the keyboard doesn't come up.
1214 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1215 return true;
1216 }
1217
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001218 return handled;
1219 }
1220
Karl Rosaen138a0412009-04-23 19:00:21 -07001221 private String getTypedText() {
1222 return mDefaultKeySsb.toString();
1223 }
1224
1225 private void clearTypedText() {
1226 mDefaultKeySsb.clear();
1227 mDefaultKeySsb.clearSpans();
1228 Selection.setSelection(mDefaultKeySsb, 0);
1229 }
1230
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001231 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001232 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1233 * State
1234 */
1235 private static State intToState(int stateOrdinal) {
1236 State state = State.WORKSPACE;
1237 final State[] stateValues = State.values();
1238 for (int i = 0; i < stateValues.length; i++) {
1239 if (stateValues[i].ordinal() == stateOrdinal) {
1240 state = stateValues[i];
1241 break;
1242 }
1243 }
1244 return state;
1245 }
1246
1247 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001248 * Restores the previous state, if it exists.
1249 *
1250 * @param savedState The previous state.
1251 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001252 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001253 private void restoreState(Bundle savedState) {
1254 if (savedState == null) {
1255 return;
1256 }
1257
Michael Jurka883f55b2010-10-21 15:47:14 -07001258 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001259 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001260 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001261 }
1262
Adam Cohen21cd0022013-10-09 18:57:02 -07001263 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1264 PagedView.INVALID_RESTORE_PAGE);
1265 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001266 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001267 }
1268
Winson Chung3d503fb2011-07-13 17:25:49 -07001269 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001270 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001271
Winson Chung3d503fb2011-07-13 17:25:49 -07001272 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1273 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001274 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001275 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1276 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001277 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1278 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1279 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001280 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001281 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001282 mRestoring = true;
1283 }
1284
1285 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1286 if (renameFolder) {
1287 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001288 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001289 mRestoring = true;
1290 }
Winson Chunga12a2502010-12-20 14:41:35 -08001291
Winson Chung785d2eb2011-04-14 16:08:02 -07001292 // Restore the AppsCustomize tab
1293 if (mAppsCustomizeTabHost != null) {
1294 String curTab = savedState.getString("apps_customize_currentTab");
1295 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001296 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001297 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001298 mAppsCustomizeContent.loadAssociatedPages(
1299 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001300 }
1301
Winson Chung5afbf7b2011-07-25 11:53:08 -07001302 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1303 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001304 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001305 mItemIdToViewId = (HashMap<Integer, Integer>)
1306 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001307 }
1308
1309 /**
1310 * Finds all the views we need and configure them properly.
1311 */
1312 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001313 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001314
Craig Mautner360310b2012-10-26 15:13:08 -07001315 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001316 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001317 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1318 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001319 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001320 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001321
John Spurlock77e1f472013-09-11 10:09:51 -04001322 mLauncherView.setSystemUiVisibility(
1323 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001324 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325
Winson Chung4c98d922011-05-31 16:50:48 -07001326 // Setup the drag layer
1327 mDragLayer.setup(this, dragController);
1328
Winson Chung3d503fb2011-07-13 17:25:49 -07001329 // Setup the hotseat
1330 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1331 if (mHotseat != null) {
1332 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001333 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001334 }
1335
Jorim Jaggid017f882014-01-14 17:08:48 -08001336 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001337 View widgetButton = findViewById(R.id.widget_button);
1338 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001339 @Override
1340 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001341 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001342 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001343 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001344 }
1345 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001346 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1347
1348 View wallpaperButton = findViewById(R.id.wallpaper_button);
1349 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001350 @Override
1351 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001352 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001353 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001354 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001355 }
1356 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001357 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1358
1359 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001360 if (hasSettings()) {
1361 settingsButton.setOnClickListener(new OnClickListener() {
1362 @Override
1363 public void onClick(View arg0) {
1364 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001365 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001366 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001367 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001368 });
1369 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1370 } else {
1371 settingsButton.setVisibility(View.GONE);
1372 FrameLayout.LayoutParams lp = (FrameLayout.LayoutParams) widgetButton.getLayoutParams();
1373 lp.gravity = Gravity.END | Gravity.TOP;
1374 widgetButton.requestLayout();
1375 }
1376
Adam Cohendbdff6b2013-09-18 19:09:15 -07001377 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001378
Winson Chung4c98d922011-05-31 16:50:48 -07001379 // Setup the workspace
1380 mWorkspace.setHapticFeedbackEnabled(false);
1381 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001382 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001383 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001384
Winson Chungf0ea4d32011-06-06 14:27:16 -07001385 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001386 mSearchDropTargetBar = (SearchDropTargetBar)
1387 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001388
Winson Chungf0ea4d32011-06-06 14:27:16 -07001389 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001390 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001391 mAppsCustomizeContent = (AppsCustomizePagedView)
1392 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1393 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001394
Winson Chung3d503fb2011-07-13 17:25:49 -07001395 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001396 dragController.setDragScoller(mWorkspace);
1397 dragController.setScrollView(mDragLayer);
1398 dragController.setMoveTarget(mWorkspace);
1399 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001400 if (mSearchDropTargetBar != null) {
1401 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001402 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001403
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001404 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001405 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001406 mWeightWatcher = new WeightWatcher(this);
1407 mWeightWatcher.setAlpha(0.5f);
1408 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001409 new FrameLayout.LayoutParams(
1410 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001411 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001412 Gravity.BOTTOM)
1413 );
Adam Cohen39a06042013-07-19 14:30:12 -07001414
1415 boolean show = shouldShowWeightWatcher();
1416 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001417 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001418 }
1419
1420 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001421 * Sets the all apps button. This method is called from {@link Hotseat}.
1422 */
1423 public void setAllAppsButton(View allAppsButton) {
1424 mAllAppsButton = allAppsButton;
1425 }
1426
1427 public View getAllAppsButton() {
1428 return mAllAppsButton;
1429 }
1430
1431 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001432 * Creates a view representing a shortcut.
1433 *
1434 * @param info The data structure describing the shortcut.
1435 *
1436 * @return A View inflated from R.layout.application.
1437 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001438 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001439 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001440 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001441 }
1442
1443 /**
1444 * Creates a view representing a shortcut inflated from the specified resource.
1445 *
1446 * @param layoutResId The id of the XML layout used to create the shortcut.
1447 * @param parent The group the shortcut belongs to.
1448 * @param info The data structure describing the shortcut.
1449 *
1450 * @return A View inflated from layoutResId.
1451 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001452 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001453 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001454 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001455 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001456 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001457 return favorite;
1458 }
1459
1460 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001461 * Add a shortcut to the workspace.
1462 *
1463 * @param data The intent describing the shortcut.
1464 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001465 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001466 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001467 int cellY) {
1468 int[] cellXY = mTmpAddItemCellCoordinates;
1469 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001470 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001471
Michael Jurkad3ef3062010-11-23 16:23:58 -08001472 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001473
Adam Cohen558baaf2011-08-15 15:22:57 -07001474 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data, null);
Adam Cohend9198822011-11-22 16:42:47 -08001475 if (info == null) {
1476 return;
1477 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001478 final View view = createShortcut(info);
1479
Adam Cohenfbba09b2011-07-18 21:43:05 -07001480 // First we check if we already know the exact location where we want to add this item.
1481 if (cellX >= 0 && cellY >= 0) {
1482 cellXY[0] = cellX;
1483 cellXY[1] = cellY;
1484 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001485
1486 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001487 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001488 true, null,null)) {
1489 return;
1490 }
1491 DragObject dragObject = new DragObject();
1492 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001493 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1494 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001495 return;
1496 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001497 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001498 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001499 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001500 foundCellSpan = (result != null);
1501 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001502 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001503 }
1504
1505 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001506 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001507 return;
1508 }
1509
Adam Cohendcd297f2013-06-18 13:13:40 -07001510 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001511
1512 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001513 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001514 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 }
1516 }
1517
Adam Cohen2f093b62012-04-30 18:59:53 -07001518 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1519 int minHeight) {
1520 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001521 // We want to account for the extra amount of padding that we are adding to the widget
1522 // to ensure that it gets the full amount of space that it has requested
1523 int requiredWidth = minWidth + padding.left + padding.right;
1524 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001525 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001526 }
1527
Adam Cohen2f093b62012-04-30 18:59:53 -07001528 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1529 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001530 }
1531
Adam Cohen2f093b62012-04-30 18:59:53 -07001532 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1533 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001534 }
1535
Adam Cohen2f093b62012-04-30 18:59:53 -07001536 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1537 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001538 }
1539
Adam Cohen2f093b62012-04-30 18:59:53 -07001540 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1541 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001542 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001543 }
1544
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001545 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001546 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001547 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001548 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001549 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001550 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001551 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001552 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1553 if (appWidgetInfo == null) {
1554 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1555 }
Romain Guycbb89e42009-06-08 15:52:54 -07001556
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001557 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001558 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001559
Adam Cohen2f093b62012-04-30 18:59:53 -07001560 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1561 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001562
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001563 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001564 // We have saved the position to which the widget was dragged-- this really only matters
1565 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001566 int[] cellXY = mTmpAddItemCellCoordinates;
1567 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001568 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001569 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001570 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1571 cellXY[0] = mPendingAddInfo.cellX;
1572 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001573 spanXY[0] = mPendingAddInfo.spanX;
1574 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001575 foundCellSpan = true;
1576 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001577 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001578 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001579 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1580 spanXY[1], cellXY, finalSpan);
1581 spanXY[0] = finalSpan[0];
1582 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001583 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001584 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001585 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001586 }
1587
Michael Jurka0280c3b2010-09-17 15:00:07 -07001588 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001589 if (appWidgetId != -1) {
1590 // Deleting an app widget ID is a void call but writes to disk before returning
1591 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001592 new AsyncTask<Void, Void, Void>() {
1593 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001594 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001595 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001596 }
Michael Jurka43467462013-11-14 12:03:58 +01001597 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001598 }
Winson Chung93eef082012-03-23 15:59:27 -07001599 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001600 return;
1601 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001602
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001603 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001604 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1605 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001606 launcherInfo.spanX = spanXY[0];
1607 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001608 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1609 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001610 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001611
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001613 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614
1615 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001616 if (hostView == null) {
1617 // Perform actual inflation because we're live
1618 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1619 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1620 } else {
1621 // The AppWidgetHostView has already been inflated and instantiated
1622 launcherInfo.hostView = hostView;
1623 }
Romain Guycbb89e42009-06-08 15:52:54 -07001624
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001625 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001626 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001627 launcherInfo.notifyWidgetSizeChanged(this);
1628
Adam Cohendcd297f2013-06-18 13:13:40 -07001629 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001630 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001631
1632 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001633 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001634 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001635 }
Romain Guycbb89e42009-06-08 15:52:54 -07001636
Adam Cohended9f8d2010-11-03 13:25:16 -07001637 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1638 @Override
1639 public void onReceive(Context context, Intent intent) {
1640 final String action = intent.getAction();
1641 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1642 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001643 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001644 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001645
Winson Chungc100e8e2011-08-09 16:02:43 -07001646 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001647 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001648 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001649 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001650 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001651 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1652 mUserPresent = true;
1653 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001654 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1655 mModel.resetLoadedState(false, true);
1656 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1657 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1658 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1659 mModel.resetLoadedState(false, true);
1660 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1661 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1662 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001663 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1664 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1665 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001666 }
1667 }
1668 };
1669
1670 @Override
1671 public void onAttachedToWindow() {
1672 super.onAttachedToWindow();
1673
1674 // Listen for broadcasts related to user-presence
1675 final IntentFilter filter = new IntentFilter();
1676 filter.addAction(Intent.ACTION_SCREEN_OFF);
1677 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001678 // For handling managed profiles
1679 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1680 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001681 if (ENABLE_DEBUG_INTENTS) {
1682 filter.addAction(DebugIntents.DELETE_DATABASE);
1683 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1684 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001685 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001686 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001687 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001688 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001689 mVisible = true;
1690 }
1691
Adam Cohen0b395352014-06-09 22:54:36 +00001692 /**
1693 * Sets up transparent navigation and status bars in LMP.
1694 * This method is a no-op for other platform versions.
1695 */
1696 @TargetApi(19)
1697 private void setupTransparentSystemBarsForLmp() {
1698 // TODO(sansid): use the APIs directly when compiling against L sdk.
1699 // Currently we use reflection to access the flags and the API to set the transparency
1700 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001701 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001702 try {
1703 getWindow().getAttributes().systemUiVisibility |=
1704 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1705 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1706 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1707 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1708 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1709 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1710 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1711 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1712
1713 Method setStatusBarColorMethod =
1714 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1715 Method setNavigationBarColorMethod =
1716 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1717 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1718 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1719 } catch (NoSuchFieldException e) {
1720 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1721 } catch (NoSuchMethodException ex) {
1722 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1723 } catch (IllegalAccessException e) {
1724 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1725 } catch (IllegalArgumentException e) {
1726 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1727 } catch (InvocationTargetException e) {
1728 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1729 } finally {}
1730 }
1731 }
1732
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 @Override
1734 public void onDetachedFromWindow() {
1735 super.onDetachedFromWindow();
1736 mVisible = false;
1737
Adam Cohend113e0c2010-11-11 10:48:05 -08001738 if (mAttached) {
1739 unregisterReceiver(mReceiver);
1740 mAttached = false;
1741 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001742 updateRunning();
1743 }
1744
1745 public void onWindowVisibilityChanged(int visibility) {
1746 mVisible = visibility == View.VISIBLE;
1747 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001748 // The following code used to be in onResume, but it turns out onResume is called when
1749 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1750 // is a more appropriate event to handle
1751 if (mVisible) {
1752 mAppsCustomizeTabHost.onWindowVisible();
1753 if (!mWorkspaceLoading) {
1754 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001755 // We want to let Launcher draw itself at least once before we force it to build
1756 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001757 // apps is nice and speedy.
1758 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001759 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001760 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001761 if (mStarted) return;
1762 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001763 // We delay the layer building a bit in order to give
1764 // other message processing a time to run. In particular
1765 // this avoids a delay in hiding the IME if it was
1766 // currently shown, because doing that may involve
1767 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001768 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001769 final ViewTreeObserver.OnDrawListener listener = this;
1770 mWorkspace.post(new Runnable() {
1771 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001772 if (mWorkspace != null &&
1773 mWorkspace.getViewTreeObserver() != null) {
1774 mWorkspace.getViewTreeObserver().
1775 removeOnDrawListener(listener);
1776 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001777 }
1778 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001779 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001780 }
1781 });
1782 }
1783 clearTypedText();
1784 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001785 }
1786
1787 private void sendAdvanceMessage(long delay) {
1788 mHandler.removeMessages(ADVANCE_MSG);
1789 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1790 mHandler.sendMessageDelayed(msg, delay);
1791 mAutoAdvanceSentTime = System.currentTimeMillis();
1792 }
1793
1794 private void updateRunning() {
1795 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1796 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1797 mAutoAdvanceRunning = autoAdvanceRunning;
1798 if (autoAdvanceRunning) {
1799 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1800 sendAdvanceMessage(delay);
1801 } else {
1802 if (!mWidgetsToAdvance.isEmpty()) {
1803 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1804 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1805 }
1806 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001807 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001808 }
1809 }
1810 }
1811
1812 private final Handler mHandler = new Handler() {
1813 @Override
1814 public void handleMessage(Message msg) {
1815 if (msg.what == ADVANCE_MSG) {
1816 int i = 0;
1817 for (View key: mWidgetsToAdvance.keySet()) {
1818 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1819 final int delay = mAdvanceStagger * i;
1820 if (v instanceof Advanceable) {
1821 postDelayed(new Runnable() {
1822 public void run() {
1823 ((Advanceable) v).advance();
1824 }
1825 }, delay);
1826 }
1827 i++;
1828 }
1829 sendAdvanceMessage(mAdvanceInterval);
1830 }
1831 }
1832 };
1833
1834 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001835 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001836 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1837 if (v instanceof Advanceable) {
1838 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001839 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001840 updateRunning();
1841 }
1842 }
1843
1844 void removeWidgetToAutoAdvance(View hostView) {
1845 if (mWidgetsToAdvance.containsKey(hostView)) {
1846 mWidgetsToAdvance.remove(hostView);
1847 updateRunning();
1848 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001849 }
1850
Joe Onorato9c1289c2009-08-17 11:03:03 -04001851 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001852 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001853 launcherInfo.hostView = null;
1854 }
1855
Winson Chung93eef082012-03-23 15:59:27 -07001856 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1857 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1858 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001859 }
1860
Winson Chunga6945242014-01-08 14:04:34 -08001861 public DragLayer getDragLayer() {
1862 return mDragLayer;
1863 }
1864
1865 public Workspace getWorkspace() {
1866 return mWorkspace;
1867 }
1868
1869 public Hotseat getHotseat() {
1870 return mHotseat;
1871 }
1872
Jorim Jaggid017f882014-01-14 17:08:48 -08001873 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001874 return mOverviewPanel;
1875 }
1876
1877 public SearchDropTargetBar getSearchBar() {
1878 return mSearchDropTargetBar;
1879 }
1880
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001881 public LauncherAppWidgetHost getAppWidgetHost() {
1882 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001883 }
Romain Guycbb89e42009-06-08 15:52:54 -07001884
Winson Chunga9abd0e2010-10-27 17:18:37 -07001885 public LauncherModel getModel() {
1886 return mModel;
1887 }
1888
Winson Chunga6945242014-01-08 14:04:34 -08001889 protected SharedPreferences getSharedPrefs() {
1890 return mSharedPrefs;
1891 }
1892
Bjorn Bringertc459e522013-06-07 19:36:01 +01001893 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001894 getWindow().closeAllPanels();
1895
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001896 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001897 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001898 }
1899
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001900 @Override
1901 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001902 long startTime = 0;
1903 if (DEBUG_RESUME_TIME) {
1904 startTime = System.currentTimeMillis();
1905 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001906 super.onNewIntent(intent);
1907
1908 // Close the menu
1909 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001910 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001911 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001912
Adam Cohened307df2013-10-02 09:37:31 -07001913 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1914 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1915 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001916
Adam Cohen6fecd412013-10-02 17:41:50 -07001917 if (mWorkspace == null) {
1918 // Can be cases where mWorkspace is null, this prevents a NPE
1919 return;
1920 }
1921 Folder openFolder = mWorkspace.getOpenFolder();
1922 // In all these cases, only animate if we're already on home
1923 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001924
1925 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1926 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001927 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001928 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001929 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001930 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001931
Adam Cohen6fecd412013-10-02 17:41:50 -07001932 closeFolder();
1933 exitSpringLoadedDragMode();
1934
1935 // If we are already on home, then just animate back to the workspace,
1936 // otherwise, just wait until onResume to set the state back to Workspace
1937 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07001938 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001939 } else {
1940 mOnResumeState = State.WORKSPACE;
1941 }
1942
1943 final View v = getWindow().peekDecorView();
1944 if (v != null && v.getWindowToken() != null) {
1945 InputMethodManager imm = (InputMethodManager)getSystemService(
1946 INPUT_METHOD_SERVICE);
1947 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
1948 }
1949
1950 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08001951 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001952 mAppsCustomizeTabHost.reset();
1953 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001954
Adam Cohen9211d422014-10-07 18:14:53 -07001955 if (mLauncherCallbacks != null) {
1956 mLauncherCallbacks.onHomeIntent();
1957 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001958 }
Adam Cohened307df2013-10-02 09:37:31 -07001959
Michael Jurka447bf842013-05-15 14:52:15 +02001960 if (DEBUG_RESUME_TIME) {
1961 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
1962 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001963
Adam Cohen9211d422014-10-07 18:14:53 -07001964 if (mLauncherCallbacks != null) {
1965 mLauncherCallbacks.onNewIntent(intent);
1966 }
Adam Coppa120b8e2013-11-12 16:26:04 +00001967 }
1968
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001969 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07001970 public void onRestoreInstanceState(Bundle state) {
1971 super.onRestoreInstanceState(state);
1972 for (int page: mSynchronouslyBoundPages) {
1973 mWorkspace.restoreInstanceStateForChild(page);
1974 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001975 }
1976
1977 @Override
1978 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07001979 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08001980 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
1981 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07001982 }
Adam Cohen95bb8002011-07-03 23:40:28 -07001983 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001984
Michael Jurka883f55b2010-10-21 15:47:14 -07001985 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07001986 // We close any open folder since it will not be re-opened, and we need to make sure
1987 // this state is reflected.
1988 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001989
Adam Cohendcd297f2013-06-18 13:13:40 -07001990 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07001991 mWaitingForResult) {
1992 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07001993 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07001994 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
1995 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001996 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
1997 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
1998 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001999 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002000 }
2001
2002 if (mFolderInfo != null && mWaitingForResult) {
2003 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2004 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2005 }
Michael Jurka946ad472010-07-09 18:05:18 -07002006
Winson Chung785d2eb2011-04-14 16:08:02 -07002007 // Save the current AppsCustomize tab
2008 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08002009 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2010 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002011 if (currentTabTag != null) {
2012 outState.putString("apps_customize_currentTab", currentTabTag);
2013 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002014 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2015 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002016 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002017 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002018
2019 if (mLauncherCallbacks != null) {
2020 mLauncherCallbacks.onSaveInstanceState(outState);
2021 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002022 }
2023
2024 @Override
2025 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002026 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002027
Winson Chunge7a03942011-08-05 15:05:12 -07002028 // Remove all pending runnables
2029 mHandler.removeMessages(ADVANCE_MSG);
2030 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002031 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002032
Winson Chungcd2b0142011-06-08 16:02:26 -07002033 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002034 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002035
2036 // It's possible to receive onDestroy after a new Launcher activity has
2037 // been created. In this case, don't interfere with the new Launcher.
2038 if (mModel.isCurrentCallbacks(this)) {
2039 mModel.stopLoader();
2040 app.setLauncher(null);
2041 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002044 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002045 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002046 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002047 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002048 mAppWidgetHost = null;
2049
2050 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051
2052 TextKeyListener.getInstance().release();
2053
Winson Chung81b52252012-08-27 15:34:29 -07002054 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2055 // to prevent leaking Launcher activities on orientation change.
2056 if (mModel != null) {
2057 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2058 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002059
2060 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002061 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002062
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002063 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002064 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002065 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002066 mWorkspace = null;
2067 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002068
Sunny Goyale755d462014-07-22 13:48:29 -07002069 PackageInstallerCompat.getInstance(this).onStop();
Michael Jurka2ecf9952012-06-18 12:52:28 -07002070 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002071
2072 if (mLauncherCallbacks != null) {
2073 mLauncherCallbacks.onDestroy();
2074 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002075 }
2076
Adam Cohena9cf38f2011-05-02 15:36:58 -07002077 public DragController getDragController() {
2078 return mDragController;
2079 }
2080
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002081 @Override
2082 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002083 if (requestCode >= 0) {
2084 setWaitingForResult(true);
2085 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002086 super.startActivityForResult(intent, requestCode);
2087 }
2088
Winson Chung70d72102011-08-12 11:24:35 -07002089 /**
2090 * Indicates that we want global search for this activity by setting the globalSearch
2091 * argument for {@link #startSearch} to true.
2092 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002094 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002095 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002096
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002097 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002098
Karl Rosaen138a0412009-04-23 19:00:21 -07002099 if (initialQuery == null) {
2100 // Use any text typed in the launcher as the initial query
2101 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002102 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002103 if (appSearchData == null) {
2104 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002105 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002106 }
Winson Chungadf0c182012-08-23 14:59:07 -07002107 Rect sourceBounds = new Rect();
2108 if (mSearchDropTargetBar != null) {
2109 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2110 }
Romain Guycbb89e42009-06-08 15:52:54 -07002111
Ian Parkinson047036e2014-09-01 15:40:53 +01002112 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002113 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002114 if (clearTextImmediately) {
2115 clearTypedText();
2116 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002117 }
2118
Ian Parkinson047036e2014-09-01 15:40:53 +01002119 /**
2120 * Start a text search.
2121 *
2122 * @return {@code true} if the search will start immediately, so any further keypresses
2123 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2124 * to buffer keypresses.
2125 */
2126 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002127 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002128 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2129 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2130 sourceBounds);
2131 }
2132
Michael Jurkaa33411c2012-06-14 16:18:21 -07002133 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002134 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002135 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002136 }
2137
2138 /**
2139 * Starts the global search activity. This code is a copied from SearchManager
2140 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002141 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002142 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002143 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002144 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2145 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2146 if (globalSearchActivity == null) {
2147 Log.w(TAG, "No global search activity found.");
2148 return;
2149 }
2150 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2151 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2152 intent.setComponent(globalSearchActivity);
2153 // Make sure that we have a Bundle to put source in
2154 if (appSearchData == null) {
2155 appSearchData = new Bundle();
2156 } else {
2157 appSearchData = new Bundle(appSearchData);
2158 }
Adam Cohen9211d422014-10-07 18:14:53 -07002159 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002160 if (!appSearchData.containsKey("source")) {
2161 appSearchData.putString("source", getPackageName());
2162 }
2163 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2164 if (!TextUtils.isEmpty(initialQuery)) {
2165 intent.putExtra(SearchManager.QUERY, initialQuery);
2166 }
2167 if (selectInitialQuery) {
2168 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2169 }
2170 intent.setSourceBounds(sourceBounds);
2171 try {
2172 startActivity(intent);
2173 } catch (ActivityNotFoundException ex) {
2174 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 }
2177
Yura4f93ec62014-02-04 14:15:21 +00002178 public boolean isOnCustomContent() {
2179 return mWorkspace.isOnOrMovingToCustomContent();
2180 }
2181
Winson Chung70d72102011-08-12 11:24:35 -07002182 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002183 public boolean onPrepareOptionsMenu(Menu menu) {
2184 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002185 if (!isOnCustomContent()) {
2186 // Close any open folders
2187 closeFolder();
2188 // Stop resizing any widgets
2189 mWorkspace.exitWidgetResizeMode();
2190 if (!mWorkspace.isInOverviewMode()) {
2191 // Show the overview mode
2192 showOverviewMode(true);
2193 } else {
2194 showWorkspace(true);
2195 }
Winson Chunge0298742014-01-17 12:03:00 -08002196 }
Adam Cohen9211d422014-10-07 18:14:53 -07002197 if (mLauncherCallbacks != null) {
2198 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2199 }
2200
Michael Jurkab94f3f82013-09-11 18:08:54 +02002201 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002202 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002203
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002204 @Override
2205 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002206 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002207 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002208 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002209 }
2210
Joe Onorato9c1289c2009-08-17 11:03:03 -04002211 public boolean isWorkspaceLocked() {
2212 return mWorkspaceLoading || mWaitingForResult;
2213 }
2214
Adam Cohen517a7f52014-03-01 12:12:59 -08002215 public boolean isWorkspaceLoading() {
2216 return mWorkspaceLoading;
2217 }
2218
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002219 private void setWorkspaceLoading(boolean value) {
2220 boolean isLocked = isWorkspaceLocked();
2221 mWorkspaceLoading = value;
2222 if (isLocked != isWorkspaceLocked()) {
2223 onWorkspaceLockedChanged();
2224 }
2225 }
2226
2227 private void setWaitingForResult(boolean value) {
2228 boolean isLocked = isWorkspaceLocked();
2229 mWaitingForResult = value;
2230 if (isLocked != isWorkspaceLocked()) {
2231 onWorkspaceLockedChanged();
2232 }
2233 }
2234
Adam Cohen9211d422014-10-07 18:14:53 -07002235 protected void onWorkspaceLockedChanged() {
2236 if (mLauncherCallbacks != null) {
2237 mLauncherCallbacks.onWorkspaceLockedChanged();
2238 }
2239 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002240
Michael Jurka0280c3b2010-09-17 15:00:07 -07002241 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002242 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002243 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002244 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2245 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002246 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002247 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002248 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002249
Adam Cohenad4e15c2013-10-17 16:21:35 -07002250 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2251 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2252 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2253 }
2254
2255 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2256 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2257 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002258 if (appWidgetInfo.configure != null) {
2259 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002260 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002261
Bjorn Bringert7984c942009-12-09 15:38:25 +00002262 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002263 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2264 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2265
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002266 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002267 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002268 Runnable onComplete = new Runnable() {
2269 @Override
2270 public void run() {
2271 // Exit spring loaded mode if necessary after adding the widget
2272 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2273 null);
2274 }
2275 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002276 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002277 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002278 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002279 }
2280 }
Romain Guycbb89e42009-06-08 15:52:54 -07002281
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002282 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002283 // Close any folders that may be open.
2284 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002285 mWorkspace.moveToCustomContentScreen(animate);
2286 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002287 /**
2288 * Process a shortcut drop.
2289 *
2290 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002291 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002292 * @param cell The cell it should be added to, optional
2293 * @param position The location on the screen where it was dropped, optional
2294 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002295 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002296 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002297 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002298 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002299 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002300 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002301
2302 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002303 mPendingAddInfo.cellX = cell[0];
2304 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002305 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002306
2307 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2308 createShortcutIntent.setComponent(componentName);
2309 processShortcut(createShortcutIntent);
2310 }
2311
Adam Cohenfbba09b2011-07-18 21:43:05 -07002312 /**
2313 * Process a widget drop.
2314 *
2315 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002316 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002317 * @param cell The cell it should be added to, optional
2318 * @param position The location on the screen where it was dropped, optional
2319 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002320 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002321 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002322 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002323 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002324 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002325 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002326 mPendingAddInfo.minSpanX = info.minSpanX;
2327 mPendingAddInfo.minSpanY = info.minSpanY;
2328
Adam Cohenfbba09b2011-07-18 21:43:05 -07002329 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002330 mPendingAddInfo.cellX = cell[0];
2331 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002332 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002333 if (span != null) {
2334 mPendingAddInfo.spanX = span[0];
2335 mPendingAddInfo.spanY = span[1];
2336 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002337
Adam Cohened66b2b2012-01-23 17:28:51 -08002338 AppWidgetHostView hostView = info.boundWidget;
2339 int appWidgetId;
2340 if (hostView != null) {
2341 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002342 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002343 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002344 // In this case, we either need to start an activity to get permission to bind
2345 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002346 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002347 Bundle options = info.bindOptions;
2348
Sunny Goyalffe83f12014-08-14 17:39:34 -07002349 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2350 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002351 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002352 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002353 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002354 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002355 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2356 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2357 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002358 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2359 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002360 // TODO: we need to make sure that this accounts for the options bundle.
2361 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002362 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2363 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002364 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002365 }
2366
Joe Onoratodeb98af2010-02-19 14:59:39 -08002367 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002368 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002369 }
2370
Winson Chung24ab2f12010-09-16 14:10:47 -07002371 void processWallpaper(Intent intent) {
2372 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2373 }
2374
Adam Cohendcd297f2013-06-18 13:13:40 -07002375 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002376 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002377 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002378 folderInfo.title = getText(R.string.folder_name);
2379
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002380 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002381 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002382 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002383 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002384
2385 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002386 FolderIcon newFolder =
2387 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002388 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002389 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002390 // Force measure the new folder icon
2391 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2392 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002393 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002394 }
Romain Guycbb89e42009-06-08 15:52:54 -07002395
Joe Onorato9c1289c2009-08-17 11:03:03 -04002396 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002397 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002398 }
2399
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002400 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002401 if (mLauncherCallbacks != null) {
2402 return mLauncherCallbacks.getWallpaperPickerComponent();
2403 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002404 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002405 }
2406
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002407 /**
2408 * Registers various content observers. The current implementation registers
2409 * only a favorites observer to keep track of the favorites applications.
2410 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002411 private void registerContentObservers() {
2412 ContentResolver resolver = getContentResolver();
2413 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2414 true, mWidgetObserver);
2415 }
2416
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002417 @Override
2418 public boolean dispatchKeyEvent(KeyEvent event) {
2419 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2420 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002421 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002422 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002423 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002424 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002425 dumpState();
2426 return true;
2427 }
2428 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002429 }
2430 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2431 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002432 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002433 return true;
2434 }
2435 }
2436
2437 return super.dispatchKeyEvent(event);
2438 }
2439
Joe Onorato88ec0992009-11-19 13:16:06 -08002440 @Override
2441 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002442 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2443 return;
2444 }
2445
Winson Chungc93e5ae2012-07-23 20:48:26 -07002446 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002447 if (mAppsCustomizeContent.getContentType() ==
2448 AppsCustomizePagedView.ContentType.Applications) {
2449 showWorkspace(true);
2450 } else {
2451 showOverviewMode(true);
2452 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002453 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002454 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002455 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002456 Folder openFolder = mWorkspace.getOpenFolder();
2457 if (openFolder.isEditingName()) {
2458 openFolder.dismissEditingName();
2459 } else {
2460 closeFolder();
2461 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002462 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002463 mWorkspace.exitWidgetResizeMode();
2464
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002465 // Back button is a no-op here, but give at least some feedback for the button press
2466 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002467 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002468 }
2469
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002470 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002471 * Re-listen when widgets are reset.
2472 */
2473 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002474 if (mAppWidgetHost != null) {
2475 mAppWidgetHost.startListening();
2476 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002477 }
2478
2479 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002480 * Launches the intent referred by the clicked shortcut.
2481 *
2482 * @param v The view representing the clicked shortcut.
2483 */
2484 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002485 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2486 // view has detached (it's possible for this to happen if the view is removed mid touch).
2487 if (v.getWindowToken() == null) {
2488 return;
2489 }
2490
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002491 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002492 return;
2493 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002494
Adam Cohen1697b792013-09-17 19:08:21 -07002495 if (v instanceof Workspace) {
2496 if (mWorkspace.isInOverviewMode()) {
2497 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002498 }
2499 return;
2500 }
2501
2502 if (v instanceof CellLayout) {
2503 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002504 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002505 }
2506 }
2507
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002508 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002509 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002510 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002511 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002512 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002513 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002514 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002515 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002516 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002517 } else if (tag instanceof AppInfo) {
2518 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002519 } else if (tag instanceof LauncherAppWidgetInfo) {
2520 if (v instanceof PendingAppWidgetHostView) {
2521 onClickPendingWidget((PendingAppWidgetHostView) v);
2522 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002523 }
2524 }
2525
Sunny Goyal508da152014-08-14 10:53:27 -07002526 public void onClickPagedViewIcon(View v) {
2527 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002528 if (mLauncherCallbacks != null) {
2529 mLauncherCallbacks.onClickPagedViewIcon(v);
2530 }
Sunny Goyal508da152014-08-14 10:53:27 -07002531 }
2532
Michael Jurka0e260592010-06-30 17:07:39 -07002533 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002534 return false;
2535 }
2536
Michael Jurkaaf442092010-06-10 17:01:57 -07002537 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002538 * Event handler for the app widget view which has not fully restored.
2539 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002540 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
2541 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002542 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002543 int widgetId = info.appWidgetId;
2544 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2545 if (appWidgetInfo != null) {
2546 mPendingAddWidgetInfo = appWidgetInfo;
2547 mPendingAddInfo.copyFrom(info);
2548 mPendingAddWidgetId = widgetId;
2549
Sunny Goyalffe83f12014-08-14 17:39:34 -07002550 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2551 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002552 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002553 } else if (info.installProgress < 0) {
2554 // The install has not been queued
2555 final String packageName = info.providerName.getPackageName();
2556 showBrokenAppInstallDialog(packageName,
2557 new DialogInterface.OnClickListener() {
2558 public void onClick(DialogInterface dialog, int id) {
2559 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2560 }
2561 });
2562 } else {
2563 // Download has started.
2564 final String packageName = info.providerName.getPackageName();
2565 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002566 }
2567 }
2568
2569 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002570 * Event handler for the search button
2571 *
2572 * @param v The view that was clicked.
2573 */
2574 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002575 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2576
Amith Yamasania135ba82011-08-09 17:42:01 -07002577 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002578 }
2579
2580 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002581 * Event handler for the voice button
2582 *
2583 * @param v The view that was clicked.
2584 */
2585 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002586 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002587
Bjorn Bringertc459e522013-06-07 19:36:01 +01002588 startVoice();
2589 }
2590
2591 public void startVoice() {
Adam Cohen9211d422014-10-07 18:14:53 -07002592 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2593 mLauncherCallbacks.startVoice();
2594 return;
2595 }
2596
Michael Jurkaae65ee32012-04-30 11:45:54 -07002597 try {
2598 final SearchManager searchManager =
2599 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2600 ComponentName activityName = searchManager.getGlobalSearchActivity();
2601 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002602 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002603 if (activityName != null) {
2604 intent.setPackage(activityName.getPackageName());
2605 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002606 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002607 } catch (ActivityNotFoundException e) {
2608 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002609 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002610 startActivitySafely(null, intent, "onClickVoiceButton");
2611 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002612 }
2613
2614 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002615 * Event handler for the "grid" button that appears on the home screen, which
2616 * enters all apps mode.
2617 *
2618 * @param v The view that was clicked.
2619 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002620 protected void onClickAllAppsButton(View v) {
2621 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2622 if (isAllAppsVisible()) {
2623 showWorkspace(true);
2624 } else {
2625 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2626 }
Adam Cohen9211d422014-10-07 18:14:53 -07002627 if (mLauncherCallbacks != null) {
2628 mLauncherCallbacks.onClickAllAppsButton(v);
2629 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002630 }
2631
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002632 private void showBrokenAppInstallDialog(final String packageName,
2633 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002634 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002635 .setTitle(R.string.abandoned_promises_title)
2636 .setMessage(R.string.abandoned_promise_explanation)
2637 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2638 .setNeutralButton(R.string.abandoned_clean_this,
2639 new DialogInterface.OnClickListener() {
2640 public void onClick(DialogInterface dialog, int id) {
2641 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2642 mWorkspace.removeAbandonedPromise(packageName, user);
2643 }
2644 })
2645 .create().show();
2646 return;
2647 }
2648
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002649 /**
2650 * Event handler for an app shortcut click.
2651 *
2652 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2653 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002654 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002655 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2656 Object tag = v.getTag();
2657 if (!(tag instanceof ShortcutInfo)) {
2658 throw new IllegalArgumentException("Input must be a Shortcut");
2659 }
2660
2661 // Open shortcut
2662 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002663
2664 if (shortcut.isDisabled != 0) {
2665 int error = R.string.activity_not_available;
2666 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2667 error = R.string.safemode_shortcut_error;
2668 }
2669 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2670 return;
2671 }
2672
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002673 final Intent intent = shortcut.intent;
2674
2675 // Check for special shortcuts
2676 if (intent.getComponent() != null) {
2677 final String shortcutClass = intent.getComponent().getClassName();
2678
2679 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2680 MemoryDumpActivity.startDump(this);
2681 return;
2682 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2683 toggleShowWeightWatcher();
2684 return;
2685 }
2686 }
2687
Chris Wren40c5ed32014-06-24 18:24:23 -04002688 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002689 if ((v instanceof BubbleTextView)
2690 && shortcut.isPromise()
2691 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002692 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002693 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002694 new DialogInterface.OnClickListener() {
2695 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002696 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002697 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002698 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002699 return;
2700 }
2701
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002702 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002703 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002704
2705 if (mLauncherCallbacks != null) {
2706 mLauncherCallbacks.onClickAppShortcut(v);
2707 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002708 }
2709
Sunny Goyal508da152014-08-14 10:53:27 -07002710 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002711 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002712 final ShortcutInfo shortcut;
2713 final Intent intent;
2714 if (tag instanceof ShortcutInfo) {
2715 shortcut = (ShortcutInfo) tag;
2716 intent = shortcut.intent;
2717 int[] pos = new int[2];
2718 v.getLocationOnScreen(pos);
2719 intent.setSourceBounds(new Rect(pos[0], pos[1],
2720 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002721
Sunny Goyal508da152014-08-14 10:53:27 -07002722 } else if (tag instanceof AppInfo) {
2723 shortcut = null;
2724 intent = ((AppInfo) tag).intent;
2725 } else {
2726 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2727 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002728
2729 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002730 mStats.recordLaunch(intent, shortcut);
2731
2732 if (success && v instanceof BubbleTextView) {
2733 mWaitingForResume = (BubbleTextView) v;
2734 mWaitingForResume.setStayPressed(true);
2735 }
2736 }
2737
2738 /**
2739 * Event handler for a folder icon click.
2740 *
2741 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2742 */
2743 protected void onClickFolderIcon(View v) {
2744 if (LOGD) Log.d(TAG, "onClickFolder");
2745 if (!(v instanceof FolderIcon)){
2746 throw new IllegalArgumentException("Input must be a FolderIcon");
2747 }
2748
2749 FolderIcon folderIcon = (FolderIcon) v;
2750 final FolderInfo info = folderIcon.getFolderInfo();
2751 Folder openFolder = mWorkspace.getFolderForTag(info);
2752
2753 // If the folder info reports that the associated folder is open, then verify that
2754 // it is actually opened. There have been a few instances where this gets out of sync.
2755 if (info.opened && openFolder == null) {
2756 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2757 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2758 info.opened = false;
2759 }
2760
2761 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2762 // Close any open folder
2763 closeFolder();
2764 // Open the requested folder
2765 openFolder(folderIcon);
2766 } else {
2767 // Find the open folder...
2768 int folderScreen;
2769 if (openFolder != null) {
2770 folderScreen = mWorkspace.getPageForView(openFolder);
2771 // .. and close it
2772 closeFolder(openFolder);
2773 if (folderScreen != mWorkspace.getCurrentPage()) {
2774 // Close any folder open on the current screen
2775 closeFolder();
2776 // Pull the folder onto this screen
2777 openFolder(folderIcon);
2778 }
2779 }
2780 }
Adam Cohen9211d422014-10-07 18:14:53 -07002781
2782 if (mLauncherCallbacks != null) {
2783 mLauncherCallbacks.onClickFolderIcon(v);
2784 }
Michael Jurka5130e402011-10-13 04:55:35 -07002785 }
2786
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002787 /**
2788 * Event handler for the (Add) Widgets button that appears after a long press
2789 * on the home screen.
2790 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002791 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002792 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002793 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
Adam Cohen9211d422014-10-07 18:14:53 -07002794
2795 if (mLauncherCallbacks != null) {
2796 mLauncherCallbacks.onClickAddWidgetButton(view);
2797 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002798 }
2799
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002800 /**
2801 * Event handler for the wallpaper picker button that appears after a long press
2802 * on the home screen.
2803 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002804 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002805 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2806 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2807 pickWallpaper.setComponent(getWallpaperPickerComponent());
2808 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002809
2810 if (mLauncherCallbacks != null) {
2811 mLauncherCallbacks.onClickWallpaperPicker(v);
2812 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002813 }
2814
2815 /**
2816 * Event handler for a click on the settings button that appears after a long press
2817 * on the home screen.
2818 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002819 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002820 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002821 if (mLauncherCallbacks != null) {
2822 mLauncherCallbacks.onClickSettingsButton(v);
2823 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002824 }
2825
Michael Jurka5130e402011-10-13 04:55:35 -07002826 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002827 // Provide the same haptic feedback that the system offers for virtual keys.
2828 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002829 }
2830
Adam Cohen61f560d2013-09-30 15:58:20 -07002831 public void performHapticFeedbackOnTouchDown(View v) {
2832 // Provide the same haptic feedback that the system offers for virtual keys.
2833 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2834 }
2835
2836 public View.OnTouchListener getHapticFeedbackTouchListener() {
2837 if (mHapticFeedbackTouchListener == null) {
2838 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2839 @Override
2840 public boolean onTouch(View v, MotionEvent event) {
2841 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2842 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2843 }
2844 return false;
2845 }
2846 };
2847 }
2848 return mHapticFeedbackTouchListener;
2849 }
2850
Adam Cohen9211d422014-10-07 18:14:53 -07002851 public void onDragStarted(View view) {
2852 if (isOnCustomContent()) {
2853 // Custom content screen doesn't participate in drag and drop. If on custom
2854 // content screen, move to default.
2855 moveWorkspaceToDefaultScreen();
2856 }
2857
2858 if (mLauncherCallbacks != null) {
2859 mLauncherCallbacks.onDragStarted(view);
2860 }
2861 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002862
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002863 /**
2864 * Called when the user stops interacting with the launcher.
2865 * This implies that the user is now on the homescreen and is not doing housekeeping.
2866 */
Adam Cohen9211d422014-10-07 18:14:53 -07002867 protected void onInteractionEnd() {
2868 if (mLauncherCallbacks != null) {
2869 mLauncherCallbacks.onInteractionEnd();
2870 }
2871 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002872
2873 /**
2874 * Called when the user starts interacting with the launcher.
2875 * The possible interactions are:
2876 * - open all apps
2877 * - reorder an app shortcut, or a widget
2878 * - open the overview mode.
2879 * This is a good time to stop doing things that only make sense
2880 * when the user is on the homescreen and not doing housekeeping.
2881 */
Adam Cohen9211d422014-10-07 18:14:53 -07002882 protected void onInteractionBegin() {
2883 if (mLauncherCallbacks != null) {
2884 mLauncherCallbacks.onInteractionBegin();
2885 }
2886 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002887
Kenny Guyf07af7b2014-07-31 11:39:16 +01002888 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002889 String packageName = componentName.getPackageName();
Kenny Guyf07af7b2014-07-31 11:39:16 +01002890 try {
2891 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2892 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2893 launcherApps.showAppDetailsForProfile(componentName, user);
2894 } catch (SecurityException e) {
2895 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2896 Log.e(TAG, "Launcher does not have permission to launch settings");
2897 } catch (ActivityNotFoundException e) {
2898 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2899 Log.e(TAG, "Unable to launch settings");
2900 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002901 }
2902
Michael Jurka1e2f4652013-07-08 18:03:46 -07002903 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002904 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2905 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002906 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002907 // System applications cannot be installed. For now, show a toast explaining that.
2908 // We may give them the option of disabling apps this way.
2909 int messageId = R.string.uninstall_system_app_text;
2910 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002911 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002912 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002913 String packageName = componentName.getPackageName();
2914 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002915 Intent intent = new Intent(
2916 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002917 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2918 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002919 if (user != null) {
2920 user.addToIntent(intent, Intent.EXTRA_USER);
2921 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002922 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002923 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002924 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002925 }
2926
Michael Jurka86a720e2012-05-09 11:23:23 -07002927 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01002928 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002929 try {
Winson Chung2672ff92012-05-04 16:22:30 -07002930 // Only launch using the new animation if the shortcut has not opted out (this is a
2931 // private contract between launcher and may be ignored in the future).
2932 boolean useLaunchAnimation = (v != null) &&
2933 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01002934 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2935 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01002936
Kenny Guy1317e2d2014-05-08 18:52:50 +01002937 UserHandleCompat user = null;
2938 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
2939 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
2940 user = userManager.getUserForSerialNumber(serialNumber);
2941 }
2942
2943 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07002944 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01002945 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07002946 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
2947 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07002948 optsBundle = opts.toBundle();
2949 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01002950
2951 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
2952 // Could be launching some bookkeeping activity
2953 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002954 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002955 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01002956 launcherApps.startActivityForProfile(intent.getComponent(), user,
2957 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07002958 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002959 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002960 } catch (SecurityException e) {
2961 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002962 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002963 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07002964 "or use the exported attribute for this activity. "
2965 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002966 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08002967 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002968 }
Winson Chungaafa03c2010-06-11 17:34:16 -07002969
Michael Jurka86a720e2012-05-09 11:23:23 -07002970 boolean startActivitySafely(View v, Intent intent, Object tag) {
2971 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07002972 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
2973 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
2974 return false;
2975 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002976 try {
2977 success = startActivity(v, intent, tag);
2978 } catch (ActivityNotFoundException e) {
2979 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2980 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
2981 }
2982 return success;
2983 }
2984
Adam Cohen268c4752012-06-06 17:47:33 -07002985 /**
2986 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
2987 * in the DragLayer in the exact absolute location of the original FolderIcon.
2988 */
2989 private void copyFolderIconToImage(FolderIcon fi) {
2990 final int width = fi.getMeasuredWidth();
2991 final int height = fi.getMeasuredHeight();
2992
2993 // Lazy load ImageView, Bitmap and Canvas
2994 if (mFolderIconImageView == null) {
2995 mFolderIconImageView = new ImageView(this);
2996 }
2997 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
2998 mFolderIconBitmap.getHeight() != height) {
2999 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3000 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3001 }
3002
3003 DragLayer.LayoutParams lp;
3004 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3005 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3006 } else {
3007 lp = new DragLayer.LayoutParams(width, height);
3008 }
3009
Adam Cohen307fe232012-08-16 17:55:58 -07003010 // The layout from which the folder is being opened may be scaled, adjust the starting
3011 // view size by this scale factor.
3012 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003013 lp.customPosition = true;
3014 lp.x = mRectForFolderAnimation.left;
3015 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003016 lp.width = (int) (scale * width);
3017 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003018
3019 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3020 fi.draw(mFolderIconCanvas);
3021 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003022 if (fi.getFolder() != null) {
3023 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3024 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003025 }
Adam Cohen268c4752012-06-06 17:47:33 -07003026 // Just in case this image view is still in the drag layer from a previous animation,
3027 // we remove it and re-add it.
3028 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3029 mDragLayer.removeView(mFolderIconImageView);
3030 }
3031 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003032 if (fi.getFolder() != null) {
3033 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003034 }
Adam Cohen268c4752012-06-06 17:47:33 -07003035 }
3036
Adam Cohen2801caf2011-05-13 20:57:39 -07003037 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003038 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003039 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3040 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3041 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3042
Adam Cohenc51934b2011-07-26 21:07:43 -07003043 FolderInfo info = (FolderInfo) fi.getTag();
3044 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3045 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003046 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3047 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003048 }
3049
Adam Cohen268c4752012-06-06 17:47:33 -07003050 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3051 copyFolderIconToImage(fi);
3052 fi.setVisibility(View.INVISIBLE);
3053
Michael Jurka2ecf9952012-06-18 12:52:28 -07003054 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003055 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003056 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003057 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3058 }
3059 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003060 oa.start();
3061 }
3062
Adam Cohen268c4752012-06-06 17:47:33 -07003063 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003064 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003065 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3066 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3067 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3068
Adam Cohen268c4752012-06-06 17:47:33 -07003069 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003070
Adam Cohen268c4752012-06-06 17:47:33 -07003071 // We remove and re-draw the FolderIcon in-case it has changed
3072 mDragLayer.removeView(mFolderIconImageView);
3073 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003074 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003075 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003076 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003077 oa.addListener(new AnimatorListenerAdapter() {
3078 @Override
3079 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003080 if (cl != null) {
3081 cl.clearFolderLeaveBehind();
3082 // Remove the ImageView copy of the FolderIcon and make the original visible.
3083 mDragLayer.removeView(mFolderIconImageView);
3084 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003085 }
3086 }
3087 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003088 oa.start();
3089 }
3090
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003091 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003092 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003093 * is animated relative to the specified View. If the View is null, no animation
3094 * is played.
3095 *
3096 * @param folderInfo The FolderInfo describing the folder to open.
3097 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003098 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003099 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003100 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003101
Adam Cohena9cf38f2011-05-02 15:36:58 -07003102 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003103
Adam Cohen4554ee12011-08-03 16:13:21 -07003104 // Just verify that the folder hasn't already been added to the DragLayer.
3105 // There was a one-off crash where the folder had a parent already.
3106 if (folder.getParent() == null) {
3107 mDragLayer.addView(folder);
3108 mDragController.addDropTarget((DropTarget) folder);
3109 } else {
3110 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3111 folder.getParent() + ").");
3112 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003113 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003114 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003115
3116 // Notify the accessibility manager that this folder "window" has appeared and occluded
3117 // the workspace items
3118 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3119 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003120 }
3121
3122 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003123 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003124 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003125 if (folder.isEditingName()) {
3126 folder.dismissEditingName();
3127 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003128 closeFolder(folder);
3129 }
3130 }
3131
3132 void closeFolder(Folder folder) {
3133 folder.getInfo().opened = false;
3134
3135 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3136 if (parent != null) {
3137 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3138 shrinkAndFadeInFolderIcon(fi);
3139 }
3140 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003141
3142 // Notify the accessibility manager that this folder "window" has disappeard and no
3143 // longer occludeds the workspace items
3144 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003145 }
3146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003147 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003148 if (!isDraggingEnabled()) return false;
3149 if (isWorkspaceLocked()) return false;
3150 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003151
Adam Cohen1697b792013-09-17 19:08:21 -07003152 if (v instanceof Workspace) {
3153 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003154 if (mWorkspace.enterOverviewMode()) {
3155 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3156 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3157 return true;
3158 } else {
3159 return false;
3160 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003161 } else {
3162 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003163 }
Adam Cohen1697b792013-09-17 19:08:21 -07003164 }
3165
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003166 CellLayout.CellInfo longClickCellInfo = null;
3167 View itemUnderLongClick = null;
3168 if (v.getTag() instanceof ItemInfo) {
3169 ItemInfo info = (ItemInfo) v.getTag();
3170 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3171 itemUnderLongClick = longClickCellInfo.cell;
3172 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003173 }
3174
Winson Chung3d503fb2011-07-13 17:25:49 -07003175 // The hotseat touch handling does not go through Workspace, and we always allow long press
3176 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003177 final boolean inHotseat = isHotseatLayout(v);
3178 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003179 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003180 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003181 // User long pressed on empty space
3182 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3183 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003184 if (mWorkspace.isInOverviewMode()) {
3185 mWorkspace.startReordering(v);
3186 } else {
3187 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003188 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003189 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003190 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3191 mHotseat.getOrderInHotseat(
3192 longClickCellInfo.cellX,
3193 longClickCellInfo.cellY));
3194 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003195 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003196 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003197 }
3198 }
3199 }
3200 return true;
3201 }
3202
Winson Chung3d503fb2011-07-13 17:25:49 -07003203 boolean isHotseatLayout(View layout) {
3204 return mHotseat != null && layout != null &&
3205 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3206 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003207
Winson Chung3d503fb2011-07-13 17:25:49 -07003208 /**
3209 * Returns the CellLayout of the specified container at the specified screen.
3210 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003211 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003212 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3213 if (mHotseat != null) {
3214 return mHotseat.getLayout();
3215 } else {
3216 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003217 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003218 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003219 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003220 }
3221 }
3222
Daniel Sandler843e8602010-06-07 14:59:01 -04003223 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003224 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003225 }
3226
Craig Mautner360310b2012-10-26 15:13:08 -07003227 private void setWorkspaceBackground(boolean workspace) {
3228 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003229 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003230 }
3231
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003232 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003233 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3234 int curflags = getWindow().getAttributes().flags
3235 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3236 if (wpflags != curflags) {
3237 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3238 }
Craig Mautner360310b2012-10-26 15:13:08 -07003239 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003240 }
3241
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003242 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3243 if (v instanceof LauncherTransitionable) {
3244 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3245 }
3246 }
3247
Michael Jurkabed61d22012-02-14 22:51:29 -08003248 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3249 if (v instanceof LauncherTransitionable) {
3250 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3251 }
Winson Chung70442722012-02-10 15:43:22 -08003252
3253 // Update the workspace transition step as well
3254 dispatchOnLauncherTransitionStep(v, 0f);
3255 }
3256
3257 private void dispatchOnLauncherTransitionStep(View v, float t) {
3258 if (v instanceof LauncherTransitionable) {
3259 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3260 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003261 }
3262
3263 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3264 if (v instanceof LauncherTransitionable) {
3265 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3266 }
Winson Chung70442722012-02-10 15:43:22 -08003267
3268 // Update the workspace transition step as well
3269 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003270 }
3271
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003272 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003273 * Things to test when changing the following seven functions.
3274 * - Home from workspace
3275 * - from center screen
3276 * - from other screens
3277 * - Home from all apps
3278 * - from center screen
3279 * - from other screens
3280 * - Back from all apps
3281 * - from center screen
3282 * - from other screens
3283 * - Launch app from workspace and quit
3284 * - with back
3285 * - with home
3286 * - Launch app from all apps and quit
3287 * - with back
3288 * - with home
3289 * - Go to a screen that's not the default, then all
3290 * apps, and launch and app, and go back
3291 * - with back
3292 * -with home
3293 * - On workspace, long press power and go back
3294 * - with back
3295 * - with home
3296 * - On all apps, long press power and go back
3297 * - with back
3298 * - with home
3299 * - On workspace, power off
3300 * - On all apps, power off
3301 * - Launch an app and turn off the screen while in that app
3302 * - Go back with home key
3303 * - Go back with back key TODO: make this not go to workspace
3304 * - From all apps
3305 * - From workspace
3306 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3307 * - From all apps
3308 * - From the center workspace
3309 * - From another workspace
3310 */
3311
3312 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003313 * Zoom the camera out from the workspace to reveal 'toView'.
3314 * Assumes that the view to show is anchored at either the very top or very bottom
3315 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003316 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003317 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003318 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3319 showAppsCustomizeHelper(animated, springLoaded, contentType);
3320 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003321
Winson Chungc58497e2013-09-03 17:48:37 -07003322 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3323 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003324 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003325 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003326 mStateAnimation.cancel();
3327 mStateAnimation = null;
3328 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003329
Kenny Guyd794a3f2014-09-16 15:17:58 +01003330 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003331
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003332 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003333
Winson Chungf0ea4d32011-06-06 14:27:16 -07003334 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3335 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003336 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003337 final int itemsAlphaStagger =
3338 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003339
Winson Chungf0ea4d32011-06-06 14:27:16 -07003340 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003341 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003342 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003343
Adam Cohen2854d252014-08-27 16:04:07 -07003344 final ArrayList<View> layerViews = new ArrayList<View>();
3345
Adam Cohen6c5891a2014-07-09 23:53:15 -07003346 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3347 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003348 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003349 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003350 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003351 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3352 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003353 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3354 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003355
Adam Cohena38dc902014-09-07 17:48:55 +02003356 // If for some reason our views aren't initialized, don't animate
3357 boolean initialized = getAllAppsButton() != null;
3358
3359 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003360 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003361 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3362 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003363
Adam Cohen9bfdb762014-07-21 17:44:06 -07003364 final View page = content.getPageAt(content.getCurrentPage());
3365 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003366
Adam Cohen63f1ec02014-08-12 09:23:13 -07003367 final float initialPanelAlpha = 1f;
3368
3369 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3370 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003371 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3372 } else {
3373 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3374 }
3375
Adam Cohen9bfdb762014-07-21 17:44:06 -07003376 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003377 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003378 revealView.setVisibility(View.VISIBLE);
3379 // We need to hide this view as the animation start will be posted.
3380 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003381
Adam Cohen9bfdb762014-07-21 17:44:06 -07003382 int width = revealView.getMeasuredWidth();
3383 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003384 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003385
Adam Cohen63f1ec02014-08-12 09:23:13 -07003386 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003387 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003388
Adam Cohen63f1ec02014-08-12 09:23:13 -07003389 // Get the y delta between the center of the page and the center of the all apps button
3390 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3391 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003392
Adam Cohen2854d252014-08-27 16:04:07 -07003393 float alpha = 0;
3394 float xDrift = 0;
3395 float yDrift = 0;
3396 if (material) {
3397 alpha = isWidgetTray ? 0.3f : 1f;
3398 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3399 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3400 } else {
3401 yDrift = 2 * height / 3;
3402 xDrift = 0;
3403 }
3404 final float initAlpha = alpha;
3405
Adam Cohen9bfdb762014-07-21 17:44:06 -07003406 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003407 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003408 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003409 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003410 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003411 PropertyValuesHolder panelDriftX =
3412 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003413
Adam Cohen2854d252014-08-27 16:04:07 -07003414 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3415 panelAlpha, panelDriftY, panelDriftX);
3416
Adam Cohen9bfdb762014-07-21 17:44:06 -07003417 panelAlphaAndDrift.setDuration(revealDuration);
3418 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003419
Adam Cohen9bfdb762014-07-21 17:44:06 -07003420 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003421
Adam Cohen4e243a22014-08-10 18:30:55 -07003422 if (page != null) {
3423 page.setVisibility(View.VISIBLE);
3424 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003425 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003426
Adam Cohen2854d252014-08-27 16:04:07 -07003427 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3428 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003429 pageDrift.setDuration(revealDuration);
3430 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003431 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003432 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003433
Adam Cohen63f1ec02014-08-12 09:23:13 -07003434 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003435 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003436 itemsAlpha.setDuration(revealDuration);
3437 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3438 itemsAlpha.setStartDelay(itemsAlphaStagger);
3439 mStateAnimation.play(itemsAlpha);
3440 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003441
Adam Cohen4e243a22014-08-10 18:30:55 -07003442 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3443 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003444 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003445 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003446 indicatorsAlpha.setDuration(revealDuration);
3447 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003448
Adam Cohen9bfdb762014-07-21 17:44:06 -07003449 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003450 final View allApps = getAllAppsButton();
3451 int allAppsButtonSize = LauncherAppState.getInstance().
3452 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3453 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003454 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003455 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003456 reveal.setDuration(revealDuration);
3457 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003458
3459 reveal.addListener(new AnimatorListenerAdapter() {
3460 public void onAnimationStart(Animator animation) {
3461 if (!isWidgetTray) {
3462 allApps.setVisibility(View.INVISIBLE);
3463 }
3464 }
3465 public void onAnimationEnd(Animator animation) {
3466 if (!isWidgetTray) {
3467 allApps.setVisibility(View.VISIBLE);
3468 }
3469 }
3470 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003471 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003472 }
Michael Jurka1899a362011-11-03 13:50:45 -07003473
Adam Cohen9bfdb762014-07-21 17:44:06 -07003474 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3475 @Override
3476 public void onAnimationEnd(Animator animation) {
3477 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3478 dispatchOnLauncherTransitionEnd(toView, animated, false);
3479
3480 revealView.setVisibility(View.INVISIBLE);
3481 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003482 if (page != null) {
3483 page.setLayerType(View.LAYER_TYPE_NONE, null);
3484 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003485 content.setPageBackgroundsVisible(true);
3486
3487 // Hide the search bar
3488 if (mSearchDropTargetBar != null) {
3489 mSearchDropTargetBar.hideSearchBar(false);
3490 }
3491 }
3492
Adam Cohen9bfdb762014-07-21 17:44:06 -07003493 });
3494
Adam Cohen6c5891a2014-07-09 23:53:15 -07003495 if (workspaceAnim != null) {
3496 mStateAnimation.play(workspaceAnim);
3497 }
Adam Cohen2854d252014-08-27 16:04:07 -07003498
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003499 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3500 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003501 final AnimatorSet stateAnimation = mStateAnimation;
3502 final Runnable startAnimRunnable = new Runnable() {
3503 public void run() {
3504 // Check that mStateAnimation hasn't changed while
3505 // we waited for a layout/draw pass
3506 if (mStateAnimation != stateAnimation)
3507 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003508 dispatchOnLauncherTransitionStart(fromView, animated, false);
3509 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003510
3511 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003512 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003513 for (int i = 0; i < layerViews.size(); i++) {
3514 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003515 if (v != null) {
3516 boolean attached = true;
3517 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3518 attached = v.isAttachedToWindow();
3519 }
3520 if (attached) v.buildLayer();
3521 }
Adam Cohen2854d252014-08-27 16:04:07 -07003522 }
3523 }
3524 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003525 }
3526 };
Adam Cohen2854d252014-08-27 16:04:07 -07003527 toView.bringToFront();
3528 toView.setVisibility(View.VISIBLE);
3529 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003530 } else {
3531 toView.setTranslationX(0.0f);
3532 toView.setTranslationY(0.0f);
3533 toView.setScaleX(1.0f);
3534 toView.setScaleY(1.0f);
3535 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003536 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003537
Daniel Sandlere4f98912013-06-25 15:13:26 -04003538 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003539 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003540 if (mSearchDropTargetBar != null) {
3541 mSearchDropTargetBar.hideSearchBar(false);
3542 }
Michael Jurkaabded662011-03-04 12:06:57 -08003543 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003544 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003545 dispatchOnLauncherTransitionStart(fromView, animated, false);
3546 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003547 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003548 dispatchOnLauncherTransitionStart(toView, animated, false);
3549 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003550 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003551 }
3552
3553 /**
3554 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003555 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003556 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003557 */
Adam Cohened307df2013-10-02 09:37:31 -07003558 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003559 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003560
Michael Jurkab3e22d92011-10-31 15:58:33 -07003561 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003562 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003563 mStateAnimation.cancel();
3564 mStateAnimation = null;
3565 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003566
Kenny Guyd794a3f2014-09-16 15:17:58 +01003567 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003568 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003569
Winson Chungf0ea4d32011-06-06 14:27:16 -07003570 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003571 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003572 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003573 final int itemsAlphaStagger =
3574 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003575
Winson Chungf0ea4d32011-06-06 14:27:16 -07003576 final float scaleFactor = (float)
3577 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3578 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003579 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003580 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003581 final ArrayList<View> layerViews = new ArrayList<View>();
3582
Adam Cohened307df2013-10-02 09:37:31 -07003583 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003584 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003585 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003586 } else if (toState == Workspace.State.SPRING_LOADED ||
3587 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003588 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003589 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003590 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003591
Adam Cohena38dc902014-09-07 17:48:55 +02003592 // If for some reason our views aren't initialized, don't animate
3593 boolean initialized = getAllAppsButton() != null;
3594
3595 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003596 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003597 if (workspaceAnim != null) {
3598 mStateAnimation.play(workspaceAnim);
3599 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003600
Adam Cohen9bfdb762014-07-21 17:44:06 -07003601 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3602 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003603
Adam Cohen9bfdb762014-07-21 17:44:06 -07003604 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003605
3606 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3607 int count = content.getChildCount();
3608 for (int i = 0; i < count; i++) {
3609 View child = content.getChildAt(i);
3610 if (child != page) {
3611 child.setVisibility(View.INVISIBLE);
3612 }
3613 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003614 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003615
Adam Cohen2854d252014-08-27 16:04:07 -07003616 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3617 // don't perform all these no-op animations. In particularly, this was causing
3618 // the all-apps button to pop in and out.
3619 if (fromView.getVisibility() == View.VISIBLE) {
3620 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3621 final boolean isWidgetTray =
3622 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003623
Adam Cohen2854d252014-08-27 16:04:07 -07003624 if (isWidgetTray) {
3625 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3626 } else {
3627 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003628 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003629
Adam Cohen2854d252014-08-27 16:04:07 -07003630 int width = revealView.getMeasuredWidth();
3631 int height = revealView.getMeasuredHeight();
3632 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3633
3634 // Hide the real page background, and swap in the fake one
3635 revealView.setVisibility(View.VISIBLE);
3636 content.setPageBackgroundsVisible(false);
3637
3638 final View allAppsButton = getAllAppsButton();
3639 revealView.setTranslationY(0);
3640 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3641 allAppsButton, null);
3642
3643 float xDrift = 0;
3644 float yDrift = 0;
3645 if (material) {
3646 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3647 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3648 } else {
3649 yDrift = 5 * height / 4;
3650 xDrift = 0;
3651 }
3652
3653 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3654 TimeInterpolator decelerateInterpolator = material ?
3655 new LogDecelerateInterpolator(100, 0) :
3656 new LogDecelerateInterpolator(30, 0);
3657
3658 // The vertical motion of the apps panel should be delayed by one frame
3659 // from the conceal animation in order to give the right feel. We correpsondingly
3660 // shorten the duration so that the slide and conceal end at the same time.
3661 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3662 0, yDrift);
3663 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3664 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3665 panelDriftY.setInterpolator(decelerateInterpolator);
3666 mStateAnimation.play(panelDriftY);
3667
3668 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3669 0, xDrift);
3670 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3671 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3672 panelDriftX.setInterpolator(decelerateInterpolator);
3673 mStateAnimation.play(panelDriftX);
3674
3675 if (isWidgetTray || !material) {
3676 float finalAlpha = material ? 0.4f : 0f;
3677 revealView.setAlpha(1f);
3678 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3679 1f, finalAlpha);
3680 panelAlpha.setDuration(revealDuration);
3681 panelAlpha.setInterpolator(material ? decelerateInterpolator :
3682 new AccelerateInterpolator(1.5f));
3683 mStateAnimation.play(panelAlpha);
3684 }
3685
3686 if (page != null) {
3687 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3688
3689 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3690 0, yDrift);
3691 page.setTranslationY(0);
3692 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3693 pageDrift.setInterpolator(decelerateInterpolator);
3694 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3695 mStateAnimation.play(pageDrift);
3696
3697 page.setAlpha(1f);
3698 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3699 itemsAlpha.setDuration(100);
3700 itemsAlpha.setInterpolator(decelerateInterpolator);
3701 mStateAnimation.play(itemsAlpha);
3702 }
3703
3704 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3705 pageIndicators.setAlpha(1f);
3706 ObjectAnimator indicatorsAlpha =
3707 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3708 indicatorsAlpha.setDuration(revealDuration);
3709 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3710 mStateAnimation.play(indicatorsAlpha);
3711
3712 width = revealView.getMeasuredWidth();
3713
3714 if (material) {
3715 if (!isWidgetTray) {
3716 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003717 }
Adam Cohen2854d252014-08-27 16:04:07 -07003718 int allAppsButtonSize = LauncherAppState.getInstance().
3719 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3720 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3721 Animator reveal =
3722 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3723 height / 2, revealRadius, finalRadius);
3724 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3725 reveal.setDuration(revealDuration);
3726 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003727
Adam Cohen2854d252014-08-27 16:04:07 -07003728 reveal.addListener(new AnimatorListenerAdapter() {
3729 public void onAnimationEnd(Animator animation) {
3730 revealView.setVisibility(View.INVISIBLE);
3731 if (!isWidgetTray) {
3732 allAppsButton.setVisibility(View.VISIBLE);
3733 }
3734 }
3735 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003736
Adam Cohen2854d252014-08-27 16:04:07 -07003737 mStateAnimation.play(reveal);
3738 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003739
Adam Cohen2854d252014-08-27 16:04:07 -07003740 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3741 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3742 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003743 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003744
3745 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003746 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003747 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003748 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003749 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3750 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003751 if (onCompleteRunnable != null) {
3752 onCompleteRunnable.run();
3753 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003754
3755 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003756 if (page != null) {
3757 page.setLayerType(View.LAYER_TYPE_NONE, null);
3758 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003759 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003760 // Unhide side pages
3761 int count = content.getChildCount();
3762 for (int i = 0; i < count; i++) {
3763 View child = content.getChildAt(i);
3764 child.setVisibility(View.VISIBLE);
3765 }
3766
3767 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003768 if (page != null) {
3769 page.setTranslationX(0);
3770 page.setTranslationY(0);
3771 page.setAlpha(1);
3772 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003773 content.setCurrentPage(content.getNextPage());
3774
Chet Haasebc2f0822012-10-26 17:59:53 -07003775 mAppsCustomizeContent.updateCurrentPageScroll();
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003776 }
3777 });
3778
Adam Cohen2854d252014-08-27 16:04:07 -07003779 final AnimatorSet stateAnimation = mStateAnimation;
3780 final Runnable startAnimRunnable = new Runnable() {
3781 public void run() {
3782 // Check that mStateAnimation hasn't changed while
3783 // we waited for a layout/draw pass
3784 if (mStateAnimation != stateAnimation)
3785 return;
3786 dispatchOnLauncherTransitionStart(fromView, animated, false);
3787 dispatchOnLauncherTransitionStart(toView, animated, false);
3788
Kenny Guyd794a3f2014-09-16 15:17:58 +01003789 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003790 for (int i = 0; i < layerViews.size(); i++) {
3791 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003792 if (v != null) {
3793 boolean attached = true;
3794 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3795 attached = v.isAttachedToWindow();
3796 }
3797 if (attached) v.buildLayer();
3798 }
Adam Cohen2854d252014-08-27 16:04:07 -07003799 }
3800 }
3801 mStateAnimation.start();
3802 }
3803 };
3804 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003805 } else {
3806 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003807 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003808 dispatchOnLauncherTransitionStart(fromView, animated, true);
3809 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003810 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003811 dispatchOnLauncherTransitionStart(toView, animated, true);
3812 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003813 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003814 }
3815
Michael Jurkae326f182011-11-21 14:05:46 -08003816 @Override
3817 public void onTrimMemory(int level) {
3818 super.onTrimMemory(level);
Winson Chungc7450e32012-04-17 17:34:08 -07003819 if (level >= ComponentCallbacks2.TRIM_MEMORY_MODERATE) {
Michael Jurkae326f182011-11-21 14:05:46 -08003820 mAppsCustomizeTabHost.onTrimMemory();
3821 }
3822 }
3823
Adam Cohened307df2013-10-02 09:37:31 -07003824 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003825 showWorkspace(animated, null);
3826 }
3827
Adam Cohened307df2013-10-02 09:37:31 -07003828 protected void showWorkspace() {
3829 showWorkspace(true);
3830 }
3831
Adam Cohened66b2b2012-01-23 17:28:51 -08003832 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003833 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003834 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003835 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003836 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003837
Winson Chungc7d2b602012-05-16 17:02:20 -07003838 // Show the search bar (only animate if we were showing the drop target bar in spring
3839 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003840 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003841 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003842 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003843
Michael Jurkab3e22d92011-10-31 15:58:33 -07003844 // Set focus to the AppsCustomize button
3845 if (mAllAppsButton != null) {
3846 mAllAppsButton.requestFocus();
3847 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003848 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003849
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003850 // Change the state *after* we've called all the transition code
3851 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003852
Winson Chung5fb63472011-02-02 17:03:37 -08003853 // Resume the auto-advance of widgets
3854 mUserPresent = true;
3855 updateRunning();
3856
alanv1d4fde62012-10-17 13:15:47 -07003857 // Send an accessibility event to announce the context change
3858 getWindow().getDecorView()
3859 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003860
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003861 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003862 }
3863
Adam Cohened307df2013-10-02 09:37:31 -07003864 void showOverviewMode(boolean animated) {
3865 mWorkspace.setVisibility(View.VISIBLE);
3866 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3867 mState = State.WORKSPACE;
3868 onWorkspaceShown(animated);
3869 }
3870
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003871 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003872 }
3873
Winson Chung82963d52013-10-09 11:20:57 -07003874 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3875 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003876 if (mState != State.WORKSPACE) return;
3877
Winson Chung82963d52013-10-09 11:20:57 -07003878 if (resetPageToZero) {
3879 mAppsCustomizeTabHost.reset();
3880 }
Winson Chungc58497e2013-09-03 17:48:37 -07003881 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003882 mAppsCustomizeTabHost.post(new Runnable() {
3883 @Override
3884 public void run() {
3885 // We post this in-case the all apps view isn't yet constructed.
3886 mAppsCustomizeTabHost.requestFocus();
3887 }
3888 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003889
Michael Jurkab3e22d92011-10-31 15:58:33 -07003890 // Change the state *after* we've called all the transition code
3891 mState = State.APPS_CUSTOMIZE;
3892
3893 // Pause the auto-advance of widgets until we are out of AllApps
3894 mUserPresent = false;
3895 updateRunning();
3896 closeFolder();
3897
3898 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003899 getWindow().getDecorView()
3900 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003901 }
3902
Adam Cohen7777d962011-08-18 18:58:38 -07003903 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003904 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003905 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003906 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003907 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003908 }
Adam Cohen7777d962011-08-18 18:58:38 -07003909
Adam Cohenad4e15c2013-10-17 16:21:35 -07003910 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003911 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003912 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3913
Winson Chunge7a03942011-08-05 15:05:12 -07003914 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003915 @Override
3916 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003917 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003918 // Before we show workspace, hide all apps again because
3919 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
3920 // clean up our state transition functions
3921 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08003922 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07003923 } else {
3924 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003925 }
Winson Chung557d6ed2011-07-08 15:34:52 -07003926 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07003927 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07003928 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07003929
Michael Jurkad3ef3062010-11-23 16:23:58 -08003930 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07003931 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003932 final boolean animated = true;
3933 final boolean springLoaded = true;
3934 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07003935 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07003936 }
3937 // Otherwise, we are not in spring loaded mode, so don't do anything.
3938 }
3939
Michael Jurkab3e22d92011-10-31 15:58:33 -07003940 void lockAllApps() {
3941 // TODO
3942 }
3943
3944 void unlockAllApps() {
3945 // TODO
3946 }
3947
Winson Chungf0ea4d32011-06-06 14:27:16 -07003948 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07003949 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07003950 */
Winson Chung3d503fb2011-07-13 17:25:49 -07003951 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04003952 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07003953 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08003954 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07003955 int duration = 0;
3956 if (mSearchDropTargetBar != null) {
3957 duration = mSearchDropTargetBar.getTransitionOutDuration();
3958 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08003959 mHotseat.animate().alpha(0f).setDuration(duration);
3960 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07003961 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07003962 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07003963 }
Winson Chungb26f3d62011-06-02 10:49:29 -07003964 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003965 }
3966
Patrick Dubroy5f445422011-02-18 14:35:21 -08003967 /**
3968 * Add an item from all apps or customize onto the given workspace screen.
3969 * If layout is null, add to the current screen.
3970 */
3971 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003972 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07003973 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07003974 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07003975 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003976
Winson Chungdff8ebb2011-09-08 17:25:31 -07003977 /** Maps the current orientation to an index for referencing orientation correct global icons */
3978 private int getCurrentOrientationIndexForGlobalIcons() {
3979 // default - 0, landscape - 1
3980 switch (getResources().getConfiguration().orientation) {
3981 case Configuration.ORIENTATION_LANDSCAPE:
3982 return 1;
3983 default:
3984 return 0;
3985 }
3986 }
3987
Michael Jurkaae65ee32012-04-30 11:45:54 -07003988 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07003989 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07003990 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07003991 // Look for the toolbar icon specified in the activity meta-data
3992 Bundle metaData = packageManager.getActivityInfo(
3993 activityName, PackageManager.GET_META_DATA).metaData;
3994 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07003995 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003996 if (iconResId != 0) {
3997 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08003998 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07003999 }
4000 }
4001 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07004002 // This can happen if the activity defines an invalid drawable
4003 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
4004 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01004005 } catch (Resources.NotFoundException nfe) {
4006 // This can happen if the activity defines an invalid drawable
4007 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
4008 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004009 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004010 return null;
4011 }
4012
4013 // if successful in getting icon, return it; otherwise, set button to use default drawable
4014 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004015 int buttonId, ComponentName activityName, int fallbackDrawableId,
4016 String toolbarResourceName) {
4017 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07004018 Resources r = getResources();
4019 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
4020 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004021
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004022 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004023 // If we were unable to find the icon via the meta-data, use a generic one
4024 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07004025 toolbarIcon = r.getDrawable(fallbackDrawableId);
4026 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004027 if (button != null) {
4028 button.setCompoundDrawables(toolbarIcon, null, null, null);
4029 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004030 return null;
4031 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07004032 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004033 if (button != null) {
4034 button.setCompoundDrawables(toolbarIcon, null, null, null);
4035 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004036 return toolbarIcon.getConstantState();
4037 }
4038 }
4039
4040 // if successful in getting icon, return it; otherwise, set button to use default drawable
4041 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004042 int buttonId, ComponentName activityName, int fallbackDrawableId,
4043 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004044 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07004045 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004046
Michael Jurka19e0fc52011-07-22 18:00:21 -07004047 if (button != null) {
4048 // If we were unable to find the icon via the meta-data, use a
4049 // generic one
4050 if (toolbarIcon == null) {
4051 button.setImageResource(fallbackDrawableId);
4052 } else {
4053 button.setImageDrawable(toolbarIcon);
4054 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004055 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07004056
4057 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
4058
Michael Jurka0423dcf2010-10-05 14:56:18 -07004059 }
4060
Winson Chung1b884092012-06-08 17:13:02 -07004061 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004062 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07004063 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004064 }
4065
Michael Jurkae7bf83b2010-12-14 18:02:21 -08004066 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08004067 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08004068 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08004069 }
4070
Winson Chungbb185bd2011-11-21 12:31:42 -08004071 private void invalidatePressedFocusedStates(View container, View button) {
4072 if (container instanceof HolographicLinearLayout) {
4073 HolographicLinearLayout layout = (HolographicLinearLayout) container;
4074 layout.invalidatePressedFocusedStates();
4075 } else if (button instanceof HolographicImageView) {
4076 HolographicImageView view = (HolographicImageView) button;
4077 view.invalidatePressedFocusedStates();
4078 }
4079 }
4080
Cristina Stancu476493b2013-08-07 17:20:14 +01004081 public View getQsbBar() {
Adam Cohen9211d422014-10-07 18:14:53 -07004082 if (mLauncherCallbacks != null) {
4083 View qsb = mLauncherCallbacks.getQsbBar();
4084 if (qsb != null) {
4085 return qsb;
4086 }
4087 }
4088
Adam Cohen24ce0b32014-01-14 16:18:14 -08004089 if (mQsb == null) {
4090 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
4091 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01004092 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08004093 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01004094 }
4095
4096 protected boolean updateGlobalSearchIcon() {
Adam Cohen9211d422014-10-07 18:14:53 -07004097 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4098 return true;
4099 }
4100
Winson Chungc51db6a2011-10-05 11:44:49 -07004101 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004102 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07004103 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004104 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004105
Winson Chung1cad91e2011-05-25 17:41:01 -07004106 final SearchManager searchManager =
4107 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4108 ComponentName activityName = searchManager.getGlobalSearchActivity();
4109 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004110 int coi = getCurrentOrientationIndexForGlobalIcons();
4111 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004112 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
4113 TOOLBAR_SEARCH_ICON_METADATA_NAME);
4114 if (sGlobalSearchIcon[coi] == null) {
4115 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4116 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
4117 TOOLBAR_ICON_METADATA_NAME);
4118 }
4119
Winson Chungc51db6a2011-10-05 11:44:49 -07004120 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
4121 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08004122 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004123 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004124 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004125 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07004126 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
4127 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004128 if (searchButton != null) searchButton.setVisibility(View.GONE);
4129 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004130 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004131 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004132 }
4133 }
4134
Cristina Stancu476493b2013-08-07 17:20:14 +01004135 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Adam Cohen9211d422014-10-07 18:14:53 -07004136 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) return;
Winson Chungbb185bd2011-11-21 12:31:42 -08004137 final View searchButtonContainer = findViewById(R.id.search_button_container);
4138 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004139 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004140 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004141 }
4142
Cristina Stancu476493b2013-08-07 17:20:14 +01004143 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Adam Cohen9211d422014-10-07 18:14:53 -07004144 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4145 return true;
4146 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004147 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004148 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004149
Winson Chungc51db6a2011-10-05 11:44:49 -07004150 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07004151 final SearchManager searchManager =
4152 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4153 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
4154
4155 ComponentName activityName = null;
4156 if (globalSearchActivity != null) {
4157 // Check if the global search activity handles voice search
4158 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4159 intent.setPackage(globalSearchActivity.getPackageName());
4160 activityName = intent.resolveActivity(getPackageManager());
4161 }
4162
4163 if (activityName == null) {
4164 // Fallback: check if an activity other than the global search activity
4165 // resolves this
4166 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4167 activityName = intent.resolveActivity(getPackageManager());
4168 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004169 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004170 int coi = getCurrentOrientationIndexForGlobalIcons();
4171 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004172 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4173 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
4174 if (sVoiceSearchIcon[coi] == null) {
4175 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4176 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4177 TOOLBAR_ICON_METADATA_NAME);
4178 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004179 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07004180 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07004181 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08004182 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004183 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004184 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07004185 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004186 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004187 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004188 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004189 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07004190 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004191
Cristina Stancu476493b2013-08-07 17:20:14 +01004192 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Adam Cohen9211d422014-10-07 18:14:53 -07004193 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4194 return;
4195 }
4196
Winson Chungbb185bd2011-11-21 12:31:42 -08004197 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
4198 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004199 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004200 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004201 }
4202
Winson Chung5841efa2013-09-30 18:06:44 -07004203 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Adam Cohen9211d422014-10-07 18:14:53 -07004204 if (mLauncherCallbacks != null) {
4205 forceDisableVoiceButtonProxy |= mLauncherCallbacks.forceDisableVoiceButtonProxy();
4206 }
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004207 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
4208 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07004209 boolean visible = !forceDisableVoiceButtonProxy &&
4210 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004211 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004212 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004213 }
4214 }
Winson Chung5841efa2013-09-30 18:06:44 -07004215
4216 /**
4217 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
4218 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
4219 */
4220 public void disableVoiceButtonProxy(boolean disabled) {
4221 updateVoiceButtonProxyVisible(disabled);
4222 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08004223
Michael Jurkad7c28052012-04-27 15:43:36 -07004224 @Override
4225 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004226 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004227 final List<CharSequence> text = event.getText();
4228 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004229 // Populate event with a fake title based on the current state.
4230 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004231 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004232 } else {
4233 text.add(getString(R.string.all_apps_home_button_label));
4234 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004235 return result;
4236 }
4237
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004238 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004239 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004240 */
4241 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4242 @Override
4243 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004244 closeSystemDialogs();
4245 }
4246 }
4247
4248 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004249 * Receives notifications whenever the appwidgets are reset.
4250 */
4251 private class AppWidgetResetObserver extends ContentObserver {
4252 public AppWidgetResetObserver() {
4253 super(new Handler());
4254 }
4255
4256 @Override
4257 public void onChange(boolean selfChange) {
4258 onAppWidgetReset();
4259 }
4260 }
4261
4262 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004263 * If the activity is currently paused, signal that we need to run the passed Runnable
4264 * in onResume.
4265 *
4266 * This needs to be called from incoming places where resources might have been loaded
4267 * while we are paused. That is becaues the Configuration might be wrong
4268 * when we're not running, and if it comes back to what it was when we
4269 * were paused, we are not restarted.
4270 *
4271 * Implementation of the method from LauncherModel.Callbacks.
4272 *
4273 * @return true if we are currently paused. The caller might be able to
4274 * skip some work in that case since we will come back again.
4275 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004276 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004277 if (mPaused) {
4278 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004279 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004280 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004281 }
4282 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004283 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004284 return true;
4285 } else {
4286 return false;
4287 }
4288 }
4289
Michael Jurkac402cd92013-05-20 15:49:32 +02004290 private boolean waitUntilResume(Runnable run) {
4291 return waitUntilResume(run, false);
4292 }
4293
Michael Jurka1e2f4652013-07-08 18:03:46 -07004294 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004295 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004296 }
4297
Michael Jurka7607c2f2013-04-03 14:33:19 -07004298 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004299 * If the activity is currently paused, signal that we need to re-run the loader
4300 * in onResume.
4301 *
4302 * This needs to be called from incoming places where resources might have been loaded
4303 * while we are paused. That is becaues the Configuration might be wrong
4304 * when we're not running, and if it comes back to what it was when we
4305 * were paused, we are not restarted.
4306 *
4307 * Implementation of the method from LauncherModel.Callbacks.
4308 *
4309 * @return true if we are currently paused. The caller might be able to
4310 * skip some work in that case since we will come back again.
4311 */
4312 public boolean setLoadOnResume() {
4313 if (mPaused) {
4314 Log.i(TAG, "setLoadOnResume");
4315 mOnResumeNeedsLoad = true;
4316 return true;
4317 } else {
4318 return false;
4319 }
4320 }
4321
4322 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004323 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004324 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004325 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004326 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004327 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004328 } else {
4329 return SCREEN_COUNT / 2;
4330 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004331 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004332
Joe Onorato9c1289c2009-08-17 11:03:03 -04004333 /**
4334 * Refreshes the shortcuts shown on the workspace.
4335 *
4336 * Implementation of the method from LauncherModel.Callbacks.
4337 */
4338 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004339 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004340
Michael Jurka7607c2f2013-04-03 14:33:19 -07004341 // If we're starting binding all over again, clear any bind calls we'd postponed in
4342 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4343 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004344 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004345
Winson Chungd64d1762013-08-20 14:37:16 -07004346 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004347 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004348 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004349
Michael Jurka05bf6442011-11-30 20:28:53 -08004350 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004351 if (mHotseat != null) {
4352 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004353 }
4354 }
4355
Adam Cohendcd297f2013-06-18 13:13:40 -07004356 @Override
4357 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004358 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004359
Adam Cohen5084cba2013-09-03 12:01:16 -07004360 // If there are no screens, we need to have an empty screen
4361 if (orderedScreenIds.size() == 0) {
4362 mWorkspace.addExtraEmptyScreen();
4363 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004364
4365 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004366 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004367 if (hasCustomContentToLeft()) {
4368 mWorkspace.createCustomContentContainer();
4369 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004370 }
Winson Chung64359a52013-07-08 17:17:08 -07004371 }
4372
4373 @Override
4374 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004375 // Log to disk
4376 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4377 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4378 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004379 int count = orderedScreenIds.size();
4380 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004381 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004382 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004383 }
4384
Adam Cohen39a06042013-07-19 14:30:12 -07004385 private boolean shouldShowWeightWatcher() {
4386 String spKey = LauncherAppState.getSharedPreferencesKey();
4387 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004388 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004389
4390 return show;
4391 }
4392
4393 private void toggleShowWeightWatcher() {
4394 String spKey = LauncherAppState.getSharedPreferencesKey();
4395 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4396 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4397
4398 show = !show;
4399
4400 SharedPreferences.Editor editor = sp.edit();
4401 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4402 editor.commit();
4403
4404 if (mWeightWatcher != null) {
4405 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4406 }
4407 }
4408
Winson Chungd64d1762013-08-20 14:37:16 -07004409 public void bindAppsAdded(final ArrayList<Long> newScreens,
4410 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004411 final ArrayList<ItemInfo> addAnimated,
4412 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004413 Runnable r = new Runnable() {
4414 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004415 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004416 }
4417 };
4418 if (waitUntilResume(r)) {
4419 return;
4420 }
4421
Winson Chungd64d1762013-08-20 14:37:16 -07004422 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004423 if (newScreens != null) {
4424 bindAddScreens(newScreens);
4425 }
Winson Chungd64d1762013-08-20 14:37:16 -07004426
4427 // We add the items without animation on non-visible pages, and with
4428 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004429 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004430 bindItems(addNotAnimated, 0,
4431 addNotAnimated.size(), false);
4432 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004433 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004434 bindItems(addAnimated, 0,
4435 addAnimated.size(), true);
4436 }
Winson Chungc58497e2013-09-03 17:48:37 -07004437
Winson Chung87412982013-10-03 18:34:14 -07004438 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004439 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004440
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004441 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004442 addedApps != null && mAppsCustomizeContent != null) {
4443 mAppsCustomizeContent.addApps(addedApps);
4444 }
Winson Chungd64d1762013-08-20 14:37:16 -07004445 }
4446
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004447 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004448 * Bind the items start-end from the list.
4449 *
4450 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004451 */
Winson Chung64359a52013-07-08 17:17:08 -07004452 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4453 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004454 Runnable r = new Runnable() {
4455 public void run() {
4456 bindItems(shortcuts, start, end, forceAnimateIcons);
4457 }
4458 };
4459 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004460 return;
4461 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004462
Winson Chungf0c6ae02012-03-21 16:10:31 -07004463 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004464 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4465 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004466 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004467 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004468 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004469 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004470 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004471
4472 // Short circuit if we are loading dock items for a configuration which has no dock
4473 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4474 mHotseat == null) {
4475 continue;
4476 }
4477
Joe Onorato9c1289c2009-08-17 11:03:03 -04004478 switch (item.itemType) {
4479 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4480 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004481 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004482 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004483
Winson Chung64359a52013-07-08 17:17:08 -07004484 /*
4485 * TODO: FIX collision case
4486 */
Winson Chungce376632013-07-11 16:12:41 -07004487 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4488 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4489 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004490 View v = cl.getChildAt(item.cellX, item.cellY);
4491 Object tag = v.getTag();
4492 String desc = "Collision while binding workspace item: " + item
4493 + ". Collides with " + tag;
4494 if (LauncherAppState.isDogfoodBuild()) {
4495 throw (new RuntimeException(desc));
4496 } else {
4497 Log.d(TAG, desc);
4498 }
Winson Chungce376632013-07-11 16:12:41 -07004499 }
Winson Chung64359a52013-07-08 17:17:08 -07004500 }
4501
Adam Cohendcd297f2013-06-18 13:13:40 -07004502 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4503 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004504 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004505 // Animate all the applications up now
4506 shortcut.setAlpha(0f);
4507 shortcut.setScaleX(0f);
4508 shortcut.setScaleY(0f);
4509 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004510 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004511 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004512 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004513 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004514 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004515 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004516 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004517 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4518 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004519 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004520 default:
4521 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004522 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004523 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004524
Winson Chung997a9232013-07-24 15:33:46 -07004525 if (animateIcons) {
4526 // Animate to the correct page
4527 if (newShortcutsScreenId > -1) {
4528 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004529 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004530 final Runnable startBounceAnimRunnable = new Runnable() {
4531 public void run() {
4532 anim.playTogether(bounceAnims);
4533 anim.start();
4534 }
4535 };
Winson Chung997a9232013-07-24 15:33:46 -07004536 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004537 // We post the animation slightly delayed to prevent slowdowns
4538 // when we are loading right after we return to launcher.
4539 mWorkspace.postDelayed(new Runnable() {
4540 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004541 if (mWorkspace != null) {
4542 mWorkspace.snapToPage(newScreenIndex);
4543 mWorkspace.postDelayed(startBounceAnimRunnable,
4544 NEW_APPS_ANIMATION_DELAY);
4545 }
Winson Chung94d67682013-09-25 16:29:40 -07004546 }
4547 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004548 } else {
4549 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004550 }
4551 }
Winson Chung64359a52013-07-08 17:17:08 -07004552 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004553 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004554 }
4555
Joe Onorato9c1289c2009-08-17 11:03:03 -04004556 /**
4557 * Implementation of the method from LauncherModel.Callbacks.
4558 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004559 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004560 Runnable r = new Runnable() {
4561 public void run() {
4562 bindFolders(folders);
4563 }
4564 };
4565 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004566 return;
4567 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004568 sFolders.clear();
4569 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004570 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004571
4572 /**
4573 * Add the views for a widget to the workspace.
4574 *
4575 * Implementation of the method from LauncherModel.Callbacks.
4576 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004577 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004578 Runnable r = new Runnable() {
4579 public void run() {
4580 bindAppWidget(item);
4581 }
4582 };
4583 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004584 return;
4585 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004586
Daniel Sandler843e8602010-06-07 14:59:01 -04004587 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4588 if (DEBUG_WIDGETS) {
4589 Log.d(TAG, "bindAppWidget: " + item);
4590 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004591 final Workspace workspace = mWorkspace;
4592
Sunny Goyalff572272014-07-23 13:58:07 -07004593 AppWidgetProviderInfo appWidgetInfo;
4594 if (((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0) &&
4595 ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
4596
4597 appWidgetInfo = mModel.findAppWidgetProviderInfoWithComponent(this, item.providerName);
4598 if (appWidgetInfo == null) {
4599 if (DEBUG_WIDGETS) {
4600 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4601 + " belongs to component " + item.providerName
4602 + ", as the povider is null");
4603 }
4604 LauncherModel.deleteItemFromDatabase(this, item);
4605 return;
4606 }
4607 // Note: This assumes that the id remap broadcast is received before this step.
4608 // If that is not the case, the id remap will be ignored and user may see the
4609 // click to setup view.
4610 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null, null);
4611 pendingInfo.spanX = item.spanX;
4612 pendingInfo.spanY = item.spanY;
4613 pendingInfo.minSpanX = item.minSpanX;
4614 pendingInfo.minSpanY = item.minSpanY;
4615 Bundle options =
4616 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4617
Sunny Goyalff572272014-07-23 13:58:07 -07004618 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004619 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4620 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004621
4622 // TODO consider showing a permission dialog when the widget is clicked.
4623 if (!success) {
4624 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4625 if (DEBUG_WIDGETS) {
4626 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4627 + " belongs to component " + item.providerName
4628 + ", as the launcher is unable to bing a new widget id");
4629 }
4630 LauncherModel.deleteItemFromDatabase(this, item);
4631 return;
4632 }
4633
4634 item.appWidgetId = newWidgetId;
4635
4636 // If the widget has a configure activity, it is still needs to set it up, otherwise
4637 // the widget is ready to go.
4638 item.restoreStatus = (appWidgetInfo.configure == null)
4639 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4640 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4641
4642 LauncherModel.updateItemInDatabase(this, item);
4643 }
4644
Sunny Goyal651077b2014-06-30 14:15:31 -07004645 if (item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
4646 final int appWidgetId = item.appWidgetId;
4647 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4648 if (DEBUG_WIDGETS) {
4649 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4650 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004651
Sunny Goyal651077b2014-06-30 14:15:31 -07004652 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4653 } else {
4654 appWidgetInfo = null;
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004655 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item);
4656 view.updateIcon(mIconCache);
4657 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004658 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004659 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004660 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004661
Joe Onorato9c1289c2009-08-17 11:03:03 -04004662 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004663 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004664
Adam Cohendcd297f2013-06-18 13:13:40 -07004665 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004666 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004667 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4668
Joe Onorato9c1289c2009-08-17 11:03:03 -04004669 workspace.requestLayout();
4670
Daniel Sandler843e8602010-06-07 14:59:01 -04004671 if (DEBUG_WIDGETS) {
4672 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4673 + (SystemClock.uptimeMillis()-start) + "ms");
4674 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004675 }
4676
Sunny Goyalff572272014-07-23 13:58:07 -07004677 /**
4678 * Restores a pending widget.
4679 *
4680 * @param appWidgetId The app widget id
4681 * @param cellInfo The position on screen where to create the widget.
4682 */
4683 private void completeRestoreAppWidget(final int appWidgetId) {
4684 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4685 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4686 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4687 return;
4688 }
4689
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004690 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004691 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4692
4693 mWorkspace.reinflateWidgetsIfNecessary();
4694 LauncherModel.updateItemInDatabase(this, info);
4695 }
4696
Adam Cohen1462de32012-07-24 22:34:36 -07004697 public void onPageBoundSynchronously(int page) {
4698 mSynchronouslyBoundPages.add(page);
4699 }
4700
Joe Onorato9c1289c2009-08-17 11:03:03 -04004701 /**
4702 * Callback saying that there aren't any more items to bind.
4703 *
4704 * Implementation of the method from LauncherModel.Callbacks.
4705 */
Adam Cohene25af792013-06-06 23:08:25 -07004706 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004707 Runnable r = new Runnable() {
4708 public void run() {
4709 finishBindingItems(upgradePath);
4710 }
4711 };
4712 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004713 return;
4714 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004715 if (mSavedState != null) {
4716 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004717 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004718 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004719 mSavedState = null;
4720 }
4721
Adam Cohen1462de32012-07-24 22:34:36 -07004722 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004723
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004724 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004725 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004726
4727 // If we received the result of any pending adds while the loader was running (e.g. the
4728 // widget configuration forced an orientation change), process them now.
4729 if (sPendingAddItem != null) {
4730 final long screenId = completeAdd(sPendingAddItem);
4731
4732 // TODO: this moves the user to the page where the pending item was added. Ideally,
4733 // the screen would be guaranteed to exist after bind, and the page would be set through
4734 // the workspace restore process.
4735 mWorkspace.post(new Runnable() {
4736 @Override
4737 public void run() {
4738 mWorkspace.snapToScreenId(screenId);
4739 }
4740 });
4741 sPendingAddItem = null;
4742 }
4743
Adam Cohene25af792013-06-06 23:08:25 -07004744 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004745 mWorkspace.getUniqueComponents(true, null);
4746 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004747 }
Sunny Goyale755d462014-07-22 13:48:29 -07004748 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004749
4750 if (mLauncherCallbacks != null) {
4751 mLauncherCallbacks.finishBindingItems(upgradePath);
4752 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004753 }
4754
Adam Cohen3ed316a2014-07-23 18:21:20 -07004755 private void sendLoadingCompleteBroadcastIfNecessary() {
4756 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4757 String permission =
4758 getResources().getString(R.string.receive_first_load_broadcast_permission);
4759 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4760 sendBroadcast(intent, permission);
4761 SharedPreferences.Editor editor = mSharedPrefs.edit();
4762 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4763 editor.apply();
4764 }
4765 }
4766
Winson Chunga0b7e862013-09-05 16:03:15 -07004767 public boolean isAllAppsButtonRank(int rank) {
4768 if (mHotseat != null) {
4769 return mHotseat.isAllAppsButtonRank(rank);
4770 }
4771 return false;
4772 }
4773
Winson Chunga2413752012-04-03 14:22:34 -07004774 private boolean canRunNewAppsAnimation() {
4775 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4776 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4777 }
4778
Winson Chungc9168342013-06-26 14:54:55 -07004779 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4780 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4781 PropertyValuesHolder.ofFloat("alpha", 1f),
4782 PropertyValuesHolder.ofFloat("scaleX", 1f),
4783 PropertyValuesHolder.ofFloat("scaleY", 1f));
4784 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4785 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4786 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4787 return bounceAnim;
4788 }
4789
Adam Cohen27772732013-11-11 14:00:56 +00004790 public boolean useVerticalBarLayout() {
4791 return LauncherAppState.getInstance().getDynamicGrid().
4792 getDeviceProfile().isVerticalBarLayout();
4793 }
4794
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004795 protected Rect getSearchBarBounds() {
4796 return LauncherAppState.getInstance().getDynamicGrid().
4797 getDeviceProfile().getSearchBarBounds();
4798 }
4799
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004800 @Override
4801 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004802 boolean searchVisible = updateGlobalSearchIcon();
4803 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004804 if (mSearchDropTargetBar != null) {
4805 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4806 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004807 }
4808
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004809 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004810 * Add the icons for all apps.
4811 *
4812 * Implementation of the method from LauncherModel.Callbacks.
4813 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004814 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004815 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004816 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4817 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4818 getHotseat().addAllAppsFolder(mIconCache, apps,
4819 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4820 }
4821 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004822 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004823 if (mAppsCustomizeContent != null) {
4824 mAppsCustomizeContent.onPackagesUpdated(
4825 LauncherModel.getSortedWidgetsAndShortcuts(this));
4826 }
Winson Chungc58497e2013-09-03 17:48:37 -07004827 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004828 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004829 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004830 mAppsCustomizeContent.onPackagesUpdated(
4831 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004832 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004833 }
Adam Cohen9211d422014-10-07 18:14:53 -07004834 if (mLauncherCallbacks != null) {
4835 mLauncherCallbacks.bindAllApplications(apps);
4836 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004837 }
4838
4839 /**
4840 * A package was updated.
4841 *
4842 * Implementation of the method from LauncherModel.Callbacks.
4843 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004844 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004845 Runnable r = new Runnable() {
4846 public void run() {
4847 bindAppsUpdated(apps);
4848 }
4849 };
4850 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004851 return;
4852 }
4853
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004854 if (mWorkspace != null) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004855 mWorkspace.updateShortcutsAndWidgets(apps);
Patrick Dubroyf5afda72011-02-28 12:04:18 -08004856 }
Winson Chungc58497e2013-09-03 17:48:37 -07004857
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004858 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004859 mAppsCustomizeContent != null) {
4860 mAppsCustomizeContent.updateApps(apps);
4861 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004862 }
4863
4864 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004865 * Some shortcuts were updated in the background.
4866 *
4867 * Implementation of the method from LauncherModel.Callbacks.
4868 */
4869 public void bindShortcutsUpdated(final ArrayList<ShortcutInfo> shortcuts) {
4870 Runnable r = new Runnable() {
4871 public void run() {
4872 bindShortcutsUpdated(shortcuts);
4873 }
4874 };
4875 if (waitUntilResume(r)) {
4876 return;
4877 }
4878
4879 if (mWorkspace != null) {
4880 mWorkspace.updateShortcuts(shortcuts);
4881 }
4882 }
4883
4884 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004885 * Update the state of a package, typically related to install state.
4886 *
4887 * Implementation of the method from LauncherModel.Callbacks.
4888 */
Sunny Goyale755d462014-07-22 13:48:29 -07004889 @Override
4890 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004891 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004892 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004893 }
4894 }
4895
4896 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004897 * Update the label and icon of all the icons in a package
4898 *
4899 * Implementation of the method from LauncherModel.Callbacks.
4900 */
4901 @Override
4902 public void updatePackageBadge(String packageName) {
4903 if (mWorkspace != null) {
4904 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
4905 }
4906 }
4907
4908 /**
Winson Chung83892cc2013-05-01 16:53:33 -07004909 * A package was uninstalled. We take both the super set of packageNames
4910 * in addition to specific applications to remove, the reason being that
4911 * this can be called when a package is updated as well. In that scenario,
4912 * we only remove specific components from the workspace, where as
4913 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004914 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07004915 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04004916 * Implementation of the method from LauncherModel.Callbacks.
4917 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07004918 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07004919 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07004920 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07004921 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004922 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07004923 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07004924 }
Winson Chungd64d1762013-08-20 14:37:16 -07004925 };
4926 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07004927 return;
4928 }
4929
Sunny Goyal1a745e82014-10-02 15:58:31 -07004930 if (reason == 0) {
4931 if (!packageNames.isEmpty()) {
4932 mWorkspace.removeItemsByPackageName(packageNames, user);
4933 }
4934 if (!appInfos.isEmpty()) {
4935 mWorkspace.removeItemsByApplicationInfo(appInfos, user);
4936 }
4937 } else {
4938 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04004939 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004940
Winson Chunga1820962011-10-03 16:31:06 -07004941 // Notify the drag controller
Winson Chungdf95eb12013-10-16 14:57:07 -07004942 mDragController.onAppsRemoved(packageNames, appInfos);
Winson Chungc58497e2013-09-03 17:48:37 -07004943
Winson Chungdf95eb12013-10-16 14:57:07 -07004944 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004945 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004946 mAppsCustomizeContent != null) {
4947 mAppsCustomizeContent.removeApps(appInfos);
4948 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004949 }
Joe Onoratobe386092009-11-17 17:32:16 -08004950
4951 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07004952 * A number of packages were updated.
4953 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004954 private ArrayList<Object> mWidgetsAndShortcuts;
4955 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07004956 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02004957 bindPackagesUpdated(mWidgetsAndShortcuts);
4958 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07004959 }
Michael Jurkac402cd92013-05-20 15:49:32 +02004960 };
Michael Jurkac402cd92013-05-20 15:49:32 +02004961 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
4962 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
4963 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07004964 return;
4965 }
4966
Winson Chung64359a52013-07-08 17:17:08 -07004967 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004968 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004969 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07004970 }
Winson Chung80baf5a2010-08-09 16:03:15 -07004971 }
4972
Winson Chung400438b2011-01-16 17:53:48 -08004973 private int mapConfigurationOriActivityInfoOri(int configOri) {
4974 final Display d = getWindowManager().getDefaultDisplay();
4975 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
4976 switch (d.getRotation()) {
4977 case Surface.ROTATION_0:
4978 case Surface.ROTATION_180:
4979 // We are currently in the same basic orientation as the natural orientation
4980 naturalOri = configOri;
4981 break;
4982 case Surface.ROTATION_90:
4983 case Surface.ROTATION_270:
4984 // We are currently in the other basic orientation to the natural orientation
4985 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
4986 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
4987 break;
4988 }
4989
4990 int[] oriMap = {
4991 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
4992 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
4993 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
4994 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
4995 };
4996 // Since the map starts at portrait, we need to offset if this device's natural orientation
4997 // is landscape.
4998 int indexOffset = 0;
4999 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
5000 indexOffset = 1;
5001 }
5002 return oriMap[(d.getRotation() + indexOffset) % 4];
5003 }
Adam Cohen446e9402011-09-15 18:21:21 -07005004
Winson Chung4b919f82012-05-01 10:44:08 -07005005 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08005006 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07005007 getResources().getBoolean(R.bool.allow_rotation);
5008 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08005009 }
Winson Chung4b919f82012-05-01 10:44:08 -07005010 public void lockScreenOrientation() {
5011 if (isRotationEnabled()) {
5012 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
5013 .getConfiguration().orientation));
5014 }
5015 }
5016 public void unlockScreenOrientation(boolean immediate) {
5017 if (isRotationEnabled()) {
5018 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07005019 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07005020 } else {
5021 mHandler.postDelayed(new Runnable() {
5022 public void run() {
5023 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
5024 }
5025 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07005026 }
Winson Chung4b919f82012-05-01 10:44:08 -07005027 }
Winson Chung400438b2011-01-16 17:53:48 -08005028 }
5029
Winson Chunge43a1e72014-01-15 10:33:02 -08005030 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07005031 if (mLauncherCallbacks != null) {
5032 return mLauncherCallbacks.isLauncherPreinstalled();
5033 }
Winson Chunge43a1e72014-01-15 10:33:02 -08005034 PackageManager pm = getPackageManager();
5035 try {
5036 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
5037 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
5038 return true;
5039 } else {
5040 return false;
5041 }
5042 } catch (NameNotFoundException e) {
5043 e.printStackTrace();
5044 return false;
5045 }
5046 }
5047
Adam Cohenea90f832014-05-21 15:03:34 -07005048 /**
5049 * This method indicates whether or not we should suggest default wallpaper dimensions
5050 * when our wallpaper cropper was not yet used to set a wallpaper.
5051 */
5052 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07005053 if (mLauncherCallbacks != null) {
5054 return mLauncherCallbacks.overrideWallpaperDimensions();
5055 }
Adam Cohenea90f832014-05-21 15:03:34 -07005056 return true;
5057 }
5058
Adam Cohen432609a2014-03-13 17:03:22 -07005059 /**
5060 * To be overridden by subclasses to indicate that there is an activity to launch
5061 * before showing the standard launcher experience.
5062 */
5063 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07005064 if (mLauncherCallbacks != null) {
5065 return mLauncherCallbacks.hasFirstRunActivity();
5066 }
Adam Cohen432609a2014-03-13 17:03:22 -07005067 return false;
5068 }
5069
5070 /**
5071 * To be overridden by subclasses to launch any first run activity
5072 */
5073 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07005074 if (mLauncherCallbacks != null) {
5075 return mLauncherCallbacks.getFirstRunActivity();
5076 }
Adam Cohen432609a2014-03-13 17:03:22 -07005077 return null;
5078 }
5079
Winson Chunga6945242014-01-08 14:04:34 -08005080 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08005081 return !ActivityManager.isRunningInTestHarness() &&
5082 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08005083 }
5084
Adam Cohen4a9423d2014-03-28 14:22:31 -07005085 protected boolean hasRunFirstRunActivity() {
5086 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
5087 }
5088
Adam Cohen432609a2014-03-13 17:03:22 -07005089 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08005090 if (shouldRunFirstRunActivity() &&
5091 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08005092 Intent firstRunIntent = getFirstRunActivity();
5093 if (firstRunIntent != null) {
5094 startActivity(firstRunIntent);
5095 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07005096 return true;
Winson Chunga6945242014-01-08 14:04:34 -08005097 }
5098 }
Adam Cohen432609a2014-03-13 17:03:22 -07005099 return false;
Winson Chunga6945242014-01-08 14:04:34 -08005100 }
5101
5102 private void markFirstRunActivityShown() {
5103 SharedPreferences.Editor editor = mSharedPrefs.edit();
5104 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
5105 editor.apply();
5106 }
5107
Adam Cohen432609a2014-03-13 17:03:22 -07005108 /**
5109 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
5110 * screen that must be displayed and dismissed.
5111 */
5112 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07005113 if (mLauncherCallbacks != null) {
5114 return mLauncherCallbacks.hasDismissableIntroScreen();
5115 }
Adam Cohen432609a2014-03-13 17:03:22 -07005116 return false;
5117 }
5118
5119 /**
5120 * Full screen intro screen to be shown and dismissed before the launcher can be used.
5121 */
5122 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07005123 if (mLauncherCallbacks != null) {
5124 return mLauncherCallbacks.getIntroScreen();
5125 }
Adam Cohen432609a2014-03-13 17:03:22 -07005126 return null;
5127 }
5128
5129 /**
5130 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
5131 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
5132 */
5133 private boolean shouldShowIntroScreen() {
5134 return hasDismissableIntroScreen() &&
5135 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
5136 }
5137
5138 protected void showIntroScreen() {
5139 View introScreen = getIntroScreen();
5140 changeWallpaperVisiblity(false);
5141 if (introScreen != null) {
5142 mDragLayer.showOverlayView(introScreen);
5143 }
5144 }
5145
5146 public void dismissIntroScreen() {
5147 markIntroScreenDismissed();
5148 if (showFirstRunActivity()) {
5149 // We delay hiding the intro view until the first run activity is showing. This
5150 // avoids a blip.
5151 mWorkspace.postDelayed(new Runnable() {
5152 @Override
5153 public void run() {
5154 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005155 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005156 }
5157 }, ACTIVITY_START_DELAY);
5158 } else {
5159 mDragLayer.dismissOverlayView();
Sunny Goyal88d60f12014-09-08 03:47:29 -07005160 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005161 }
5162 changeWallpaperVisiblity(true);
5163 }
5164
5165 private void markIntroScreenDismissed() {
5166 SharedPreferences.Editor editor = mSharedPrefs.edit();
5167 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
5168 editor.apply();
5169 }
5170
Sunny Goyal88d60f12014-09-08 03:47:29 -07005171 private void showFirstRunClings() {
5172 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
5173 // on the device, then we always show the first run cling experience (or if there is no
5174 // launcher2). Otherwise, we prompt the user upon started for migration
5175 LauncherClings launcherClings = new LauncherClings(this);
5176 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
5177 if (mModel.canMigrateFromOldLauncherDb(this)) {
5178 launcherClings.showMigrationCling();
5179 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005180 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005181 }
5182 }
5183 }
5184
Winson Chunga6945242014-01-08 14:04:34 -08005185 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005186 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5187 if (mHotseat != null) mHotseat.setAlpha(1f);
5188 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5189 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005190 }
5191
5192 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005193 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5194 if (mHotseat != null) mHotseat.setAlpha(0f);
5195 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5196 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005197 }
5198
Mathew Inwood72fbec12013-11-19 15:45:07 +00005199 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005200 // Called from search suggestion, not supported in other profiles.
5201 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5202 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5203 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5204 myUser);
5205 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005206 return null;
5207 }
Kenny Guyed131872014-04-30 03:02:21 +01005208 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005209 }
5210
5211 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5212 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005213 // Called from search suggestion, not supported in other profiles.
5214 return createShortcutDragInfo(shortcutIntent, caption, icon,
5215 UserHandleCompat.myUserHandle());
5216 }
5217
5218 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5219 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005220 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005221 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005222 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005223 }
5224
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005225 protected void moveWorkspaceToDefaultScreen() {
5226 mWorkspace.moveToDefaultScreen(false);
5227 }
5228
Mathew Inwood72fbec12013-11-19 15:45:07 +00005229 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5230 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005231 mWorkspace.onExternalDragStartedWithItem(dragView);
5232 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005233 }
5234
Anjali Koppalf5d29132014-02-28 13:33:27 -08005235 @Override
5236 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005237 if (mLauncherCallbacks != null) {
5238 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5239 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005240 }
5241
Winson Chung80baf5a2010-08-09 16:03:15 -07005242 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005243 * Prints out out state for debugging.
5244 */
5245 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005246 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005247 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005248 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5249 Log.d(TAG, "mRestoring=" + mRestoring);
5250 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5251 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005252 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005253 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005254
Winson Chung785d2eb2011-04-14 16:08:02 -07005255 if (mAppsCustomizeContent != null) {
5256 mAppsCustomizeContent.dumpState();
5257 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005258 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005259 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005260
5261 @Override
5262 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5263 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005264 synchronized (sDumpLogs) {
5265 writer.println(" ");
5266 writer.println("Debug logs: ");
5267 for (int i = 0; i < sDumpLogs.size(); i++) {
5268 writer.println(" " + sDumpLogs.get(i));
5269 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005270 }
Adam Cohen9211d422014-10-07 18:14:53 -07005271 if (mLauncherCallbacks != null) {
5272 mLauncherCallbacks.dump(prefix, fd, writer, args);
5273 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005274 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005275
5276 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005277 if (DEBUG_DUMP_LOG) {
5278 synchronized (sDumpLogs) {
5279 Log.d(TAG, "");
5280 Log.d(TAG, "*********************");
5281 Log.d(TAG, "Launcher debug logs: ");
5282 for (int i = 0; i < sDumpLogs.size(); i++) {
5283 Log.d(TAG, " " + sDumpLogs.get(i));
5284 }
5285 Log.d(TAG, "*********************");
5286 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005287 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005288 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005289 }
5290
5291 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005292 addDumpLog(tag, log, null, debugLog);
5293 }
5294
5295 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005296 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005297 if (e != null) {
5298 Log.d(tag, log, e);
5299 } else {
5300 Log.d(tag, log);
5301 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005302 }
Winson Chungede41292013-09-19 16:27:36 -07005303 if (DEBUG_DUMP_LOG) {
5304 sDateStamp.setTime(System.currentTimeMillis());
5305 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005306 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5307 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005308 }
Winson Chungede41292013-09-19 16:27:36 -07005309 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005310 }
5311
Winson Chungede41292013-09-19 16:27:36 -07005312 public void dumpLogsToLocalData() {
5313 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005314 new AsyncTask<Void, Void, Void>() {
5315 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005316 boolean success = false;
5317 sDateStamp.setTime(sRunStart);
5318 String FILENAME = sDateStamp.getMonth() + "-"
5319 + sDateStamp.getDay() + "_"
5320 + sDateStamp.getHours() + "-"
5321 + sDateStamp.getMinutes() + "_"
5322 + sDateStamp.getSeconds() + ".txt";
5323
5324 FileOutputStream fos = null;
5325 File outFile = null;
5326 try {
5327 outFile = new File(getFilesDir(), FILENAME);
5328 outFile.createNewFile();
5329 fos = new FileOutputStream(outFile);
5330 } catch (Exception e) {
5331 e.printStackTrace();
5332 }
5333 if (fos != null) {
5334 PrintWriter writer = new PrintWriter(fos);
5335
5336 writer.println(" ");
5337 writer.println("Debug logs: ");
5338 synchronized (sDumpLogs) {
5339 for (int i = 0; i < sDumpLogs.size(); i++) {
5340 writer.println(" " + sDumpLogs.get(i));
5341 }
5342 }
5343 writer.close();
5344 }
5345 try {
5346 if (fos != null) {
5347 fos.close();
5348 success = true;
5349 }
5350 } catch (IOException e) {
5351 e.printStackTrace();
5352 }
Michael Jurka43467462013-11-14 12:03:58 +01005353 return null;
Winson Chungede41292013-09-19 16:27:36 -07005354 }
Michael Jurka43467462013-11-14 12:03:58 +01005355 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005356 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005357 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005358}
Michael Jurka2763be32011-02-24 11:19:57 -08005359
Michael Jurkaabded662011-03-04 12:06:57 -08005360interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005361 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005362 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005363 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005364 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005365 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005366}
Dan Sandlerd5024042014-01-09 15:01:33 -05005367
5368interface DebugIntents {
5369 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5370 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005371}