blob: c632f1c2e70e4af4c79e58648ef2660faa0fb8fa [file] [log] [blame]
Michael Jurka01f0ed42010-08-20 00:41:17 -07001
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002/*
3 * Copyright (C) 2008 The Android Open Source Project
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
Daniel Sandler325dc232013-06-05 22:57:57 -040018package com.android.launcher3;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080019
Gilles Debunnedd6c9922010-10-25 11:23:41 -070020import android.animation.Animator;
Michael Jurka8edd75c2010-12-17 20:15:06 -080021import android.animation.AnimatorListenerAdapter;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070022import android.animation.AnimatorSet;
Adam Cohen2801caf2011-05-13 20:57:39 -070023import android.animation.ObjectAnimator;
24import android.animation.PropertyValuesHolder;
Adam Cohen2854d252014-08-27 16:04:07 -070025import android.animation.TimeInterpolator;
Gilles Debunnedd6c9922010-10-25 11:23:41 -070026import android.animation.ValueAnimator;
Adam Cohen0b395352014-06-09 22:54:36 +000027import android.annotation.TargetApi;
Michael Jurka946ad472010-07-09 18:05:18 -070028import android.app.Activity;
Brett Chabot2a9e2282011-08-23 15:03:13 -070029import android.app.ActivityManager;
Winson Chungc7450e32012-04-17 17:34:08 -070030import android.app.ActivityOptions;
Chris Wren40c5ed32014-06-24 18:24:23 -040031import android.app.AlertDialog;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080032import android.app.SearchManager;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080033import android.appwidget.AppWidgetHostView;
Michael Jurkaaf442092010-06-10 17:01:57 -070034import android.appwidget.AppWidgetManager;
35import android.appwidget.AppWidgetProviderInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080036import android.content.ActivityNotFoundException;
Joe Onorato2ca0ae72009-11-10 13:14:13 -080037import android.content.BroadcastReceiver;
Michael Jurkae326f182011-11-21 14:05:46 -080038import android.content.ComponentCallbacks2;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040039import android.content.ComponentName;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080040import android.content.ContentResolver;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080041import android.content.Context;
Chris Wren40c5ed32014-06-24 18:24:23 -040042import android.content.DialogInterface;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080043import android.content.Intent;
Winson Chungf0ea4d32011-06-06 14:27:16 -070044import android.content.IntentFilter;
Winson Chung82f55532011-08-09 14:14:23 -070045import android.content.SharedPreferences;
Winson Chungaafa03c2010-06-11 17:34:16 -070046import android.content.pm.ActivityInfo;
Winson Chunge43a1e72014-01-15 10:33:02 -080047import android.content.pm.ApplicationInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080048import android.content.pm.PackageManager;
Adam Cohen716b51e2011-06-30 12:09:54 -070049import android.content.pm.PackageManager.NameNotFoundException;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080050import android.content.res.Configuration;
Karl Rosaen138a0412009-04-23 19:00:21 -070051import android.content.res.Resources;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080052import android.database.ContentObserver;
Adam Cohen3f9c9712014-10-31 11:48:25 -070053import android.database.sqlite.SQLiteDatabase;
Adam Cohen268c4752012-06-06 17:47:33 -070054import android.graphics.Bitmap;
55import android.graphics.Canvas;
Sandeep Siddhartha2efc7d92014-05-16 17:21:15 -070056import android.graphics.Color;
Adam Cohen268c4752012-06-06 17:47:33 -070057import android.graphics.PorterDuff;
Michael Jurkaaf442092010-06-10 17:01:57 -070058import android.graphics.Rect;
Michael Jurkaaf442092010-06-10 17:01:57 -070059import android.graphics.drawable.Drawable;
Daniel Sandlerc9b18772010-04-22 14:37:59 -040060import android.net.Uri;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -070061import android.os.AsyncTask;
Adam Cohen0f668f32014-09-08 19:54:17 +020062import android.os.Build;
The Android Open Source Project31dd5032009-03-03 19:32:27 -080063import android.os.Bundle;
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +020064import android.os.Environment;
Jeff Sharkey1e2efc82009-11-06 15:17:59 -080065import android.os.Handler;
Adam Cohended9f8d2010-11-03 13:25:16 -070066import android.os.Message;
Winson Chunga2413752012-04-03 14:22:34 -070067import android.os.StrictMode;
Daniel Sandler843e8602010-06-07 14:59:01 -040068import android.os.SystemClock;
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;
Joe Onorato7c312c12009-08-13 21:36:53 -070074import android.util.Log;
Adam Cohen96d30a12013-07-16 18:13:21 -070075import android.view.Display;
76import android.view.Gravity;
77import android.view.HapticFeedbackConstants;
78import android.view.KeyEvent;
79import android.view.LayoutInflater;
80import android.view.Menu;
Adam Cohen96d30a12013-07-16 18:13:21 -070081import android.view.MotionEvent;
82import android.view.Surface;
83import android.view.View;
Adam Cohenf358a4b2013-07-23 16:47:31 -070084import android.view.View.OnClickListener;
Adam Cohen0cf2a7c2011-11-08 15:07:01 -080085import android.view.View.OnLongClickListener;
Adam Cohen2854d252014-08-27 16:04:07 -070086import android.view.ViewAnimationUtils;
Adam Cohen96d30a12013-07-16 18:13:21 -070087import android.view.ViewGroup;
Adam Cohenc2d6e892014-10-16 09:49:24 -070088import android.view.ViewStub;
Adam Cohen96d30a12013-07-16 18:13:21 -070089import 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;
Sunny Goyal4390ace2014-10-13 11:33:11 -0700128import java.util.HashSet;
Michael Jurkad7c28052012-04-27 15:43:36 -0700129import java.util.List;
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000130import java.util.concurrent.atomic.AtomicInteger;
Adam Cohenc0dcf592011-06-01 15:30:43 -0700131
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800132/**
133 * Default launcher application.
134 */
Bjorn Bringertc459e522013-06-07 19:36:01 +0100135public class Launcher extends Activity
Michael Jurka0e260592010-06-30 17:07:39 -0700136 implements View.OnClickListener, OnLongClickListener, LauncherModel.Callbacks,
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700137 View.OnTouchListener, PageSwitchListener, LauncherProviderChangeListener {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -0800138 static final String TAG = "Launcher";
Joe Onoratocc67f472010-06-08 10:54:30 -0700139 static final boolean LOGD = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800140
Daniel Sandlerf061f822013-06-27 13:59:36 -0400141 static final boolean PROFILE_STARTUP = false;
Daniel Sandler843e8602010-06-07 14:59:01 -0400142 static final boolean DEBUG_WIDGETS = false;
Winson Chunga2413752012-04-03 14:22:34 -0700143 static final boolean DEBUG_STRICT_MODE = false;
Daniel Sandlerf061f822013-06-27 13:59:36 -0400144 static final boolean DEBUG_RESUME_TIME = false;
Winson Chungede41292013-09-19 16:27:36 -0700145 static final boolean DEBUG_DUMP_LOG = false;
Romain Guy6fefcf12009-06-11 13:07:43 -0700146
Dan Sandlerd5024042014-01-09 15:01:33 -0500147 static final boolean ENABLE_DEBUG_INTENTS = false; // allow DebugIntents to run
148
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800149 private static final int REQUEST_CREATE_SHORTCUT = 1;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700150 private static final int REQUEST_CREATE_APPWIDGET = 5;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800151 private static final int REQUEST_PICK_SHORTCUT = 7;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700152 private static final int REQUEST_PICK_APPWIDGET = 9;
Mike Clerona0618e42009-10-22 13:55:21 -0700153 private static final int REQUEST_PICK_WALLPAPER = 10;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800154
Michael Jurka8b805b12012-04-18 14:23:14 -0700155 private static final int REQUEST_BIND_APPWIDGET = 11;
Sunny Goyalff572272014-07-23 13:58:07 -0700156 private static final int REQUEST_RECONFIGURE_APPWIDGET = 12;
Michael Jurka8b805b12012-04-18 14:23:14 -0700157
Mathew Inwood876a8462013-06-14 14:12:41 +0100158 /**
159 * IntentStarter uses request codes starting with this. This must be greater than all activity
160 * request codes used internally.
161 */
162 protected static final int REQUEST_LAST = 100;
163
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800164 static final String EXTRA_SHORTCUT_DUPLICATE = "duplicate";
165
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800166 static final int SCREEN_COUNT = 5;
167 static final int DEFAULT_SCREEN = 2;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800168
Michael Jurka0a457bf2012-11-19 14:05:05 -0800169 // To turn on these properties, type
170 // adb shell setprop log.tag.PROPERTY_NAME [VERBOSE | SUPPRESS]
Michael Jurka0a457bf2012-11-19 14:05:05 -0800171 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 Sandler6053b8002013-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
Adam Cohenc2d6e892014-10-16 09:49:24 -0700230 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
231 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700232 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700233
Joe Onorato9c1289c2009-08-17 11:03:03 -0400234 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700235 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
236 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700237 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800238
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800240 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800241
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000242 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
243 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
244
Winson Chunga2413752012-04-03 14:22:34 -0700245 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700246 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
247 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700248 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700249 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700250
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800251 private final BroadcastReceiver mCloseSystemDialogsReceiver
252 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800253 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
254
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800255 private LayoutInflater mInflater;
256
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800257 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700258 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800259 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800260 private DragLayer mDragLayer;
261 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700262 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700263
Sunny Goyalffe83f12014-08-14 17:39:34 -0700264 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700265 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700266
Michael Jurkac9d95c52011-08-29 14:03:34 -0700267 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700268 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800269 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700270
Michael Jurka0280c3b2010-09-17 15:00:07 -0700271 private int[] mTmpAddItemCellCoordinates = new int[2];
272
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800273 private FolderInfo mFolderInfo;
274
Winson Chung3d503fb2011-07-13 17:25:49 -0700275 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800276 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700277
Michael Jurka838a4ca2011-02-07 13:33:06 -0800278 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700279
Winson Chungc51db6a2011-10-05 11:44:49 -0700280 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700281 private AppsCustomizeTabHost mAppsCustomizeTabHost;
282 private AppsCustomizePagedView mAppsCustomizeContent;
283 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800284 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800285
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700287 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
288 // scroll issues (because the workspace may not have been measured yet) and extra work.
289 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
290 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800291
292 private SpannableStringBuilder mDefaultKeySsb = null;
293
Joe Onorato9c1289c2009-08-17 11:03:03 -0400294 private boolean mWorkspaceLoading = true;
295
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800296 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800297 private boolean mRestoring;
298 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700299 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800300
Michael Jurka1e2f4652013-07-08 18:03:46 -0700301 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700302 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
303
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800304 private Bundle mSavedInstanceState;
305
Joe Onorato9c1289c2009-08-17 11:03:03 -0400306 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800307 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800308 private boolean mUserPresent = true;
309 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700310 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800311 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400312
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700313 private static LocaleConfiguration sLocaleConfiguration = null;
314
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700315 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700316
Adam Cohen61f560d2013-09-30 15:58:20 -0700317 private View.OnTouchListener mHapticFeedbackTouchListener;
318
Adam Cohended9f8d2010-11-03 13:25:16 -0700319 // Related to the auto-advancing of widgets
320 private final int ADVANCE_MSG = 1;
321 private final int mAdvanceInterval = 20000;
322 private final int mAdvanceStagger = 250;
323 private long mAutoAdvanceSentTime;
324 private long mAutoAdvanceTimeLeft = -1;
325 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
326 new HashMap<View, AppWidgetProviderInfo>();
327
Winson Chung400438b2011-01-16 17:53:48 -0800328 // Determines how long to wait after a rotation before restoring the screen orientation to
329 // match the sensor state.
330 private final int mRestoreScreenOrientationDelay = 500;
331
Michael Jurka4ef207b2010-11-29 17:05:45 -0800332 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700333 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
334 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
Winson Chungd64a6662013-09-30 11:06:59 -0700335
Craig Mautner360310b2012-10-26 15:13:08 -0700336 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700337
Adam Cohen1462de32012-07-24 22:34:36 -0700338 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700339 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700340
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700341 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700342 static Date sDateStamp = new Date();
343 static DateFormat sDateFormat =
344 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
345 static long sRunStart = System.currentTimeMillis();
346 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700347
Winson Chung46353de2012-02-16 14:05:10 -0800348 // We only want to get the SharedPreferences once since it does an FS stat each time we get
349 // it from the context.
350 private SharedPreferences mSharedPrefs;
351
Adam Cohene25af792013-06-06 23:08:25 -0700352 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
353
Winson Chungf0c6ae02012-03-21 16:10:31 -0700354 // Holds the page that we need to animate to, and the icon views that we need to animate up
355 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700356 private ImageView mFolderIconImageView;
357 private Bitmap mFolderIconBitmap;
358 private Canvas mFolderIconCanvas;
359 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700360
Michael Jurkaddd62e92011-02-16 17:49:14 -0800361 private BubbleTextView mWaitingForResume;
362
Michael Jurkac1f5d262011-09-30 19:32:27 -0700363 private Runnable mBuildLayersRunnable = new Runnable() {
364 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700365 if (mWorkspace != null) {
366 mWorkspace.buildPageHardwareLayers();
367 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700368 }
369 };
370
Adam Cohendb364c32014-05-20 14:23:40 -0700371 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800372
373 private static class PendingAddArguments {
374 int requestCode;
375 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700376 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700377 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800378 int cellX;
379 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700380 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800381 }
382
Daniel Sandlerff02d492013-08-05 02:12:05 -0400383 private Stats mStats;
384
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700385 FocusIndicatorView mFocusHandler;
386
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800387 @Override
388 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700389 if (DEBUG_STRICT_MODE) {
390 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
391 .detectDiskReads()
392 .detectDiskWrites()
393 .detectNetwork() // or .detectAll() for all detectable problems
394 .penaltyLog()
395 .build());
396 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
397 .detectLeakedSqlLiteObjects()
398 .detectLeakedClosableObjects()
399 .penaltyLog()
400 .penaltyDeath()
401 .build());
402 }
403
Adam Cohen9211d422014-10-07 18:14:53 -0700404 if (mLauncherCallbacks != null) {
405 mLauncherCallbacks.preOnCreate();
406 }
407
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800408 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400409
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400410 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400411 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700412 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700413
Winson Chung5f8afe62013-08-12 16:19:28 -0700414 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700415 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700416
417 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400418 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700419 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700420 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800421 mModel = app.setLauncher(this);
422 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700423 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400424 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800425 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700426
Daniel Sandlerff02d492013-08-05 02:12:05 -0400427 mStats = new Stats(this);
428
Sunny Goyalffe83f12014-08-14 17:39:34 -0700429 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700430
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700431 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
432 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700433
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700434 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
435 // this also ensures that any synchronous binding below doesn't re-trigger another
436 // LauncherModel load.
437 mPaused = false;
438
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800439 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200440 android.os.Debug.startMethodTracing(
441 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800442 }
443
444 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800445 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700446
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800447 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100448 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800450 registerContentObservers();
451
Joe Onorato7c312c12009-08-13 21:36:53 -0700452 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 mSavedState = savedInstanceState;
455 restoreState(mSavedState);
456
457 if (PROFILE_STARTUP) {
458 android.os.Debug.stopMethodTracing();
459 }
460
461 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700462 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700463 // If the user leaves launcher, then we should just load items asynchronously when
464 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500465 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700466 } else {
467 // We only load the page synchronously if the user rotates (or triggers a
468 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800469 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700470 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800471 }
472
473 // For handling default keys
474 mDefaultKeySsb = new SpannableStringBuilder();
475 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800476
477 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
478 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800479
Adam Cohenf9426d52012-06-04 17:26:21 -0700480 updateGlobalIcons();
481
482 // On large interfaces, we want the screen to auto-rotate based on the current orientation
483 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700484
Adam Cohen9211d422014-10-07 18:14:53 -0700485 if (mLauncherCallbacks != null) {
486 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700487 if (mLauncherCallbacks.hasLauncherOverlay()) {
488 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700489 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
490 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
491 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700492 mWorkspace.setLauncherOverlay(mLauncherOverlay);
493 }
Adam Cohen9211d422014-10-07 18:14:53 -0700494 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700495
496 if (shouldShowIntroScreen()) {
497 showIntroScreen();
498 } else {
499 showFirstRunActivity();
500 showFirstRunClings();
501 }
Adam Cohen9211d422014-10-07 18:14:53 -0700502 }
503
504 private LauncherCallbacks mLauncherCallbacks;
505
506 public void onPostCreate(Bundle savedInstanceState) {
507 super.onPostCreate(savedInstanceState);
508 if (mLauncherCallbacks != null) {
509 mLauncherCallbacks.onPostCreate(savedInstanceState);
510 }
511 }
512
513 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
514 mLauncherCallbacks = callbacks;
515 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700516 }
517
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700518 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700519 public void onLauncherProviderChange() {
520 if (mLauncherCallbacks != null) {
521 mLauncherCallbacks.onLauncherProviderChange();
522 }
523 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700524
Adam Cohen9211d422014-10-07 18:14:53 -0700525 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700526 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700527 if (mLauncherCallbacks != null) {
528 return mLauncherCallbacks.hasCustomContentToLeft();
529 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700530 return false;
531 }
532
Dave Hawkeya8881582013-09-17 15:55:33 -0600533 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500534 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600535 * {@link #addToCustomContentPage}. This will only be invoked if
536 * {@link #hasCustomContentToLeft()} is {@code true}.
537 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500538 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700539 if (mLauncherCallbacks != null) {
540 mLauncherCallbacks.populateCustomContentContainer();
541 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600542 }
543
544 /**
545 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
546 * ensure the custom content page is added or removed if necessary.
547 */
548 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600549 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600550 // Not bound yet, wait for bindScreens to be called.
551 return;
552 }
553
554 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
555 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500556 mWorkspace.createCustomContentContainer();
557 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600558 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
559 mWorkspace.removeCustomContentPage();
560 }
561 }
562
Adam Cohenf9426d52012-06-04 17:26:21 -0700563 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700564 boolean searchVisible = false;
565 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800566 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700567 int coi = getCurrentOrientationIndexForGlobalIcons();
Adam Cohena00673c2014-08-14 12:57:28 -0700568 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700569 searchVisible = updateGlobalSearchIcon();
570 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700571 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700572 if (sGlobalSearchIcon[coi] != null) {
573 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700574 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700575 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700576 if (sVoiceSearchIcon[coi] != null) {
577 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700578 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700579 }
Winson Chungadf0c182012-08-23 14:59:07 -0700580 if (mSearchDropTargetBar != null) {
581 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
582 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800583 }
Romain Guycbb89e42009-06-08 15:52:54 -0700584
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800585 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700586 if (sLocaleConfiguration == null) {
587 new AsyncTask<Void, Void, LocaleConfiguration>() {
588 @Override
589 protected LocaleConfiguration doInBackground(Void... unused) {
590 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
591 readConfiguration(Launcher.this, localeConfiguration);
592 return localeConfiguration;
593 }
594
595 @Override
596 protected void onPostExecute(LocaleConfiguration result) {
597 sLocaleConfiguration = result;
598 checkForLocaleChange(); // recursive, but now with a locale configuration
599 }
600 }.execute();
601 return;
602 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700603
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800604 final Configuration configuration = getResources().getConfiguration();
605
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700606 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800607 final String locale = configuration.locale.toString();
608
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700609 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800610 final int mcc = configuration.mcc;
611
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700612 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800613 final int mnc = configuration.mnc;
614
Romain Guy84f296c2009-11-04 15:00:44 -0800615 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800616
Romain Guy84f296c2009-11-04 15:00:44 -0800617 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700618 sLocaleConfiguration.locale = locale;
619 sLocaleConfiguration.mcc = mcc;
620 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700621
Joe Onorato0589f0f2010-02-08 13:44:00 -0800622 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700623
624 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100625 new AsyncTask<Void, Void, Void>() {
626 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700627 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100628 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700629 }
Michael Jurka43467462013-11-14 12:03:58 +0100630 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700631 }
632 }
633
634 private static class LocaleConfiguration {
635 public String locale;
636 public int mcc = -1;
637 public int mnc = -1;
638 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700639
Romain Guy98d01652009-06-30 16:21:04 -0700640 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
641 DataInputStream in = null;
642 try {
Helena Josol28db2802014-10-09 17:04:09 +0100643 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700644 configuration.locale = in.readUTF();
645 configuration.mcc = in.readInt();
646 configuration.mnc = in.readInt();
647 } catch (FileNotFoundException e) {
648 // Ignore
649 } catch (IOException e) {
650 // Ignore
651 } finally {
652 if (in != null) {
653 try {
654 in.close();
655 } catch (IOException e) {
656 // Ignore
657 }
658 }
659 }
660 }
661
662 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
663 DataOutputStream out = null;
664 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100665 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100666 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700667 out.writeUTF(configuration.locale);
668 out.writeInt(configuration.mcc);
669 out.writeInt(configuration.mnc);
670 out.flush();
671 } catch (FileNotFoundException e) {
672 // Ignore
673 } catch (IOException e) {
674 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100675 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700676 } finally {
677 if (out != null) {
678 try {
679 out.close();
680 } catch (IOException e) {
681 // Ignore
682 }
683 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800684 }
685 }
686
Anton Hanssona2f665f2013-09-26 12:18:32 +0100687 public Stats getStats() {
688 return mStats;
689 }
690
Bjorn Bringertc459e522013-06-07 19:36:01 +0100691 public LayoutInflater getInflater() {
692 return mInflater;
693 }
694
Winson Chung36a62fe2012-05-06 18:04:42 -0700695 boolean isDraggingEnabled() {
696 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
697 // that is subsequently removed from the workspace in startBinding().
698 return !mModel.isLoadingWorkspace();
699 }
700
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800701 static int getScreen() {
702 synchronized (sLock) {
703 return sScreen;
704 }
705 }
706
707 static void setScreen(int screen) {
708 synchronized (sLock) {
709 sScreen = screen;
710 }
711 }
712
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000713 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100714 if (Build.VERSION.SDK_INT >= 17) {
715 return View.generateViewId();
716 } else {
717 // View.generateViewId() is not available. The following fallback logic is a copy
718 // of its implementation.
719 for (;;) {
720 final int result = sNextGeneratedId.get();
721 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
722 int newValue = result + 1;
723 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
724 if (sNextGeneratedId.compareAndSet(result, newValue)) {
725 return result;
726 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000727 }
728 }
729 }
730
731 public int getViewIdForItem(ItemInfo info) {
732 // This cast is safe given the > 2B range for int.
733 int itemId = (int) info.id;
734 if (mItemIdToViewId.containsKey(itemId)) {
735 return mItemIdToViewId.get(itemId);
736 }
737 int viewId = generateViewId();
738 mItemIdToViewId.put(itemId, viewId);
739 return viewId;
740 }
741
742 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700743 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
744 * a configuration step, this allows the proper animations to run after other transitions.
745 */
Adam Cohendb364c32014-05-20 14:23:40 -0700746 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700747 long screenId = args.screenId;
748 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
749 // When the screen id represents an actual screen (as opposed to a rank) we make sure
750 // that the drop page actually exists.
751 screenId = ensurePendingDropLayoutExists(args.screenId);
752 }
Adam Cohendb364c32014-05-20 14:23:40 -0700753
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800754 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800755 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700756 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700757 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700758 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800759 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700760 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700761 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700762 case REQUEST_RECONFIGURE_APPWIDGET:
763 completeRestoreAppWidget(args.appWidgetId);
764 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800765 }
Michael Jurka27614d22012-04-02 06:40:22 -0700766 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
767 // if you turned the screen off and then back while in All Apps, Launcher would not
768 // return to the workspace. Clearing mAddInfo.container here fixes this issue
769 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700770 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800771 }
772
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800773 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700774 protected void onActivityResult(
775 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700776 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700777 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700778 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800779 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700780
Adam Cohenad4e15c2013-10-17 16:21:35 -0700781 Runnable exitSpringLoaded = new Runnable() {
782 @Override
783 public void run() {
784 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
785 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
786 }
787 };
788
Michael Jurka8b805b12012-04-18 14:23:14 -0700789 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700790 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700791 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
792 if (resultCode == RESULT_CANCELED) {
793 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700794 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700795 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700796 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700797 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
798 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700799 }
800 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200801 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
802 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
803 mWorkspace.exitOverviewMode(false);
804 }
805 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700806 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200807
Adam Cohened66b2b2012-01-23 17:28:51 -0800808 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
809 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700810
Adam Cohendb364c32014-05-20 14:23:40 -0700811 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800812 // We have special handling for widgets
813 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800814 final int appWidgetId;
815 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
816 : -1;
817 if (widgetId < 0) {
818 appWidgetId = pendingAddWidgetId;
819 } else {
820 appWidgetId = widgetId;
821 }
822
Adam Cohenad4e15c2013-10-17 16:21:35 -0700823 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800824 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700825 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
826 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700827 result = RESULT_CANCELED;
828 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700829 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700830 @Override
831 public void run() {
832 exitSpringLoadedDragModeDelayed(false, 0, null);
833 }
834 };
Adam Cohendb364c32014-05-20 14:23:40 -0700835 if (workspaceLocked) {
836 // No need to remove the empty screen if we're mid-binding, as the
837 // the bind will not add the empty screen.
838 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
839 } else {
840 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
841 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
842 }
Winson Chung5aaab772012-04-27 15:24:02 -0700843 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700844 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700845 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
846 // When the screen id represents an actual screen (as opposed to a rank)
847 // we make sure that the drop page actually exists.
848 mPendingAddInfo.screenId =
849 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
850 }
Adam Cohendb364c32014-05-20 14:23:40 -0700851 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
852
853 dropLayout.setDropPending(true);
854 final Runnable onComplete = new Runnable() {
855 @Override
856 public void run() {
857 completeTwoStageWidgetDrop(resultCode, appWidgetId);
858 dropLayout.setDropPending(false);
859 }
860 };
861 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
862 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
863 } else {
864 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
865 mPendingAddInfo);
866 sPendingAddItem = args;
867 }
Winson Chung5aaab772012-04-27 15:24:02 -0700868 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800869 return;
870 }
871
Sunny Goyalff572272014-07-23 13:58:07 -0700872 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
873 if (resultCode == RESULT_OK) {
874 // Update the widget view.
875 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
876 pendingAddWidgetId, mPendingAddInfo);
877 if (workspaceLocked) {
878 sPendingAddItem = args;
879 } else {
880 completeAdd(args);
881 }
882 }
883 // Leave the widget in the pending state if the user canceled the configure.
884 return;
885 }
886
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800887 // The pattern used here is that a user PICKs a specific application,
888 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700889
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800890 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
891 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700892 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700893 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
894 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800895 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700896 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800897 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700898 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700899 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
900 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800901 }
Adam Cohen00074722013-10-11 17:46:09 -0700902 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700903 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700904 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800905 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800906 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800907 }
908
Adam Cohendb364c32014-05-20 14:23:40 -0700909 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
910 appWidgetId, ItemInfo info) {
911 PendingAddArguments args = new PendingAddArguments();
912 args.requestCode = requestCode;
913 args.intent = data;
914 args.container = info.container;
915 args.screenId = info.screenId;
916 args.cellX = info.cellX;
917 args.cellY = info.cellY;
918 args.appWidgetId = appWidgetId;
919 return args;
920 }
921
922 /**
923 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
924 *
925 * @param screenId the screen id to check
926 * @return the new screen, or screenId if it exists
927 */
928 private long ensurePendingDropLayoutExists(long screenId) {
929 CellLayout dropLayout =
930 (CellLayout) mWorkspace.getScreenWithId(screenId);
931 if (dropLayout == null) {
932 // it's possible that the add screen was removed because it was
933 // empty and a re-bind occurred
934 mWorkspace.addExtraEmptyScreen();
935 return mWorkspace.commitExtraEmptyScreen();
936 } else {
937 return screenId;
938 }
939 }
940
Adam Cohened66b2b2012-01-23 17:28:51 -0800941 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700942 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700943 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800944 Runnable onCompleteRunnable = null;
945 int animationType = 0;
946
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700947 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800948 if (resultCode == RESULT_OK) {
949 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
950 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700951 mPendingAddWidgetInfo);
952 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800953 onCompleteRunnable = new Runnable() {
954 @Override
955 public void run() {
956 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700957 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700958 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
959 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800960 }
961 };
962 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800963 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800964 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800965 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700966 if (mDragLayer.getAnimatedView() != null) {
967 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
968 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
969 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700970 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700971 // The animated view may be null in the case of a rotation during widget configuration
972 onCompleteRunnable.run();
973 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800974 }
975
976 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700977 protected void onStop() {
978 super.onStop();
979 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700980
981 if (mLauncherCallbacks != null) {
982 mLauncherCallbacks.onStop();
983 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700984 }
985
986 @Override
987 protected void onStart() {
988 super.onStart();
989 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700990
991 if (mLauncherCallbacks != null) {
992 mLauncherCallbacks.onStart();
993 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700994 }
995
996 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800997 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +0200998 long startTime = 0;
999 if (DEBUG_RESUME_TIME) {
1000 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001001 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001002 }
Adam Cohen9211d422014-10-07 18:14:53 -07001003
1004 if (mLauncherCallbacks != null) {
1005 mLauncherCallbacks.preOnResume();
1006 }
1007
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001008 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001009
Winson Chung4a2afa32012-07-19 14:53:05 -07001010 // Restore the previous launcher state
1011 if (mOnResumeState == State.WORKSPACE) {
1012 showWorkspace(false);
1013 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c62013-11-06 15:49:51 -08001014 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001015 }
1016 mOnResumeState = State.NONE;
1017
Craig Mautner360310b2012-10-26 15:13:08 -07001018 // Background was set to gradient in onPause(), restore to black if in all apps.
1019 setWorkspaceBackground(mState == State.WORKSPACE);
1020
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001021 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001022 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001023 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001024 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001025 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001026 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001027 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001028 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001029 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1030 // execute them here
1031 long startTimeCallbacks = 0;
1032 if (DEBUG_RESUME_TIME) {
1033 startTimeCallbacks = System.currentTimeMillis();
1034 }
1035
1036 if (mAppsCustomizeContent != null) {
1037 mAppsCustomizeContent.setBulkBind(true);
1038 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001039 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1040 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001041 }
1042 if (mAppsCustomizeContent != null) {
1043 mAppsCustomizeContent.setBulkBind(false);
1044 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001045 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001046 if (DEBUG_RESUME_TIME) {
1047 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1048 (System.currentTimeMillis() - startTimeCallbacks));
1049 }
Michael Jurka447bf842013-05-15 14:52:15 +02001050 }
Michael Jurka54554252013-08-01 12:52:23 +02001051 if (mOnResumeCallbacks.size() > 0) {
1052 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1053 mOnResumeCallbacks.get(i).run();
1054 }
1055 mOnResumeCallbacks.clear();
1056 }
Winson Chunge4e50662012-01-23 14:45:13 -08001057
1058 // Reset the pressed state of icons that were locked in the press state while activities
1059 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001060 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001061 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001062 mWaitingForResume.setStayPressed(false);
1063 }
Winson Chung82963d52013-10-09 11:20:57 -07001064
Adam Cohen06dff352012-06-01 17:17:08 -07001065 // It is possible that widgets can receive updates while launcher is not in the foreground.
1066 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1067 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1068 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001069 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001070
Winson Chung780fe592013-09-26 14:48:44 -07001071 // Process any items that were added while Launcher was away.
1072 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1073
Winson Chung5841efa2013-09-30 18:06:44 -07001074 // Update the voice search button proxy
1075 updateVoiceButtonProxyVisible(false);
1076
Adam Cohenf9426d52012-06-04 17:26:21 -07001077 // Again, as with the above scenario, it's possible that one or more of the global icons
1078 // were updated in the wrong orientation.
1079 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001080 if (DEBUG_RESUME_TIME) {
1081 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1082 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001083
1084 if (mWorkspace.getCustomContentCallbacks() != null) {
1085 // If we are resuming and the custom content is the current page, we call onShow().
1086 // It is also poassible that onShow will instead be called slightly after first layout
1087 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1088 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001089 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001090 }
1091 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001092 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001093 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001094
1095 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001096
1097 if (mLauncherCallbacks != null) {
1098 mLauncherCallbacks.onResume();
1099 }
Adam Cohen06dff352012-06-01 17:17:08 -07001100 }
1101
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001102 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001103 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001104 // Ensure that items added to Launcher are queued until Launcher returns
1105 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001106 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001107
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001108 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001109 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001110 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001111 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001112
1113 // We call onHide() aggressively. The custom content callbacks should be able to
1114 // debounce excess onHide calls.
1115 if (mWorkspace.getCustomContentCallbacks() != null) {
1116 mWorkspace.getCustomContentCallbacks().onHide();
1117 }
Romain Guycbb89e42009-06-08 15:52:54 -07001118
Adam Cohen9211d422014-10-07 18:14:53 -07001119 if (mLauncherCallbacks != null) {
1120 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001121 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001122 }
1123
1124 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001125 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1126 // by a onResume or by scrolling otherwise.
1127 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001128
1129 // Custom content is completely hidden
1130 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001131
1132 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1133 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001134
1135 // Indicates whether the user is allowed to scroll away from the custom content.
1136 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001137 }
1138
Adam Cohenc2d6e892014-10-16 09:49:24 -07001139 public interface LauncherOverlay {
1140
1141 /**
1142 * Touch interaction leading to overscroll has begun
1143 */
1144 public void onScrollInteractionBegin();
1145
1146 /**
1147 * Touch interaction related to overscroll has ended
1148 */
1149 public void onScrollInteractionEnd();
1150
1151 /**
1152 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1153 * screen (or in the case of RTL, the rightmost screen).
1154 */
1155 public void onScrollChange(int progress, boolean rtl);
1156
1157 /**
1158 * Screen has stopped scrolling
1159 */
1160 public void onScrollSettled();
1161
1162 /**
1163 * This method can be called by the Launcher in order to force the LauncherOverlay
1164 * to exit fully immersive mode.
1165 */
1166 public void forceExitFullImmersion();
1167 }
1168
1169 public interface LauncherOverlayCallbacks {
1170 /**
1171 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1172 * however it doesn't modify any state within the launcher.
1173 */
1174 public boolean canEnterFullImmersion();
1175
1176 /**
1177 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1178 * eg. by occupying the full screen and handling all touch events.
1179 *
1180 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1181 * case, Launcher will modify any necessary state and assumes the overlay is
1182 * handling all interaction. If false, the LauncherOverlay should cancel any
1183 *
1184 */
1185 public boolean enterFullImmersion();
1186
1187 /**
1188 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1189 * full control over UI and state.
1190 */
1191 public void exitFullImmersion();
1192 }
1193
1194 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1195
1196 @Override
1197 public boolean canEnterFullImmersion() {
1198 return mState == State.WORKSPACE;
1199 }
1200
1201 @Override
1202 public boolean enterFullImmersion() {
1203 if (mState == State.WORKSPACE) {
1204 // When fully immersed, disregard any touches which fall through.
1205 mDragLayer.setBlockTouch(true);
1206 return true;
1207 }
1208 return false;
1209 }
1210
1211 @Override
1212 public void exitFullImmersion() {
1213 mDragLayer.setBlockTouch(false);
1214 }
1215 }
1216
Jorim Jaggid017f882014-01-14 17:08:48 -08001217 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001218 if (mLauncherCallbacks != null) {
1219 return mLauncherCallbacks.hasSettings();
1220 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001221 return false;
1222 }
1223
Adam Cohenbffe7452013-07-22 18:21:45 -07001224
Adam Cohen9211d422014-10-07 18:14:53 -07001225 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001226 CustomContentCallbacks callbacks, String description) {
1227 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001228 }
1229
Adam Cohenedb40762013-07-18 16:45:45 -07001230 // The custom content needs to offset its content to account for the QSB
1231 public int getTopOffsetForCustomContent() {
1232 return mWorkspace.getPaddingTop();
1233 }
1234
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001235 @Override
1236 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001237 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001238 if (mModel.isCurrentCallbacks(this)) {
1239 mModel.stopLoader();
1240 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001241 if (mAppsCustomizeContent != null) {
1242 mAppsCustomizeContent.surrender();
1243 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001244 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001245 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001246
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001247 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001248 @Override
1249 public void onWindowFocusChanged(boolean hasFocus) {
1250 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001251 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001252
1253 if (mLauncherCallbacks != null) {
1254 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1255 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001256 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001257
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001258 private boolean acceptFilter() {
1259 final InputMethodManager inputManager = (InputMethodManager)
1260 getSystemService(Context.INPUT_METHOD_SERVICE);
1261 return !inputManager.isFullscreenMode();
1262 }
1263
1264 @Override
1265 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001266 final int uniChar = event.getUnicodeChar();
1267 final boolean handled = super.onKeyDown(keyCode, event);
1268 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1269 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001270 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1271 keyCode, event);
1272 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001273 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001274 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001275 // showSearchDialog()
1276 // If there are multiple keystrokes before the search dialog takes focus,
1277 // onSearchRequested() will be called for every keystroke,
1278 // but it is idempotent, so it's fine.
1279 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001280 }
1281 }
1282
Joe Onorato8a9625e2010-01-28 15:55:35 -08001283 // Eat the long press event so the keyboard doesn't come up.
1284 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1285 return true;
1286 }
1287
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001288 return handled;
1289 }
1290
Karl Rosaen138a0412009-04-23 19:00:21 -07001291 private String getTypedText() {
1292 return mDefaultKeySsb.toString();
1293 }
1294
1295 private void clearTypedText() {
1296 mDefaultKeySsb.clear();
1297 mDefaultKeySsb.clearSpans();
1298 Selection.setSelection(mDefaultKeySsb, 0);
1299 }
1300
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001301 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001302 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1303 * State
1304 */
1305 private static State intToState(int stateOrdinal) {
1306 State state = State.WORKSPACE;
1307 final State[] stateValues = State.values();
1308 for (int i = 0; i < stateValues.length; i++) {
1309 if (stateValues[i].ordinal() == stateOrdinal) {
1310 state = stateValues[i];
1311 break;
1312 }
1313 }
1314 return state;
1315 }
1316
1317 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001318 * Restores the previous state, if it exists.
1319 *
1320 * @param savedState The previous state.
1321 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001322 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001323 private void restoreState(Bundle savedState) {
1324 if (savedState == null) {
1325 return;
1326 }
1327
Michael Jurka883f55b2010-10-21 15:47:14 -07001328 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001329 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001330 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001331 }
1332
Adam Cohen21cd0022013-10-09 18:57:02 -07001333 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1334 PagedView.INVALID_RESTORE_PAGE);
1335 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001336 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001337 }
1338
Winson Chung3d503fb2011-07-13 17:25:49 -07001339 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001340 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001341
Winson Chung3d503fb2011-07-13 17:25:49 -07001342 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1343 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001344 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001345 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1346 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001347 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1348 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1349 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001350 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001351 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001352 mRestoring = true;
1353 }
1354
1355 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1356 if (renameFolder) {
1357 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001358 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 mRestoring = true;
1360 }
Winson Chunga12a2502010-12-20 14:41:35 -08001361
Winson Chung785d2eb2011-04-14 16:08:02 -07001362 // Restore the AppsCustomize tab
1363 if (mAppsCustomizeTabHost != null) {
1364 String curTab = savedState.getString("apps_customize_currentTab");
1365 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001366 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001367 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001368 mAppsCustomizeContent.loadAssociatedPages(
1369 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001370 }
1371
Winson Chung5afbf7b2011-07-25 11:53:08 -07001372 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1373 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001374 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001375 mItemIdToViewId = (HashMap<Integer, Integer>)
1376 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001377 }
1378
1379 /**
1380 * Finds all the views we need and configure them properly.
1381 */
1382 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001383 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001384
Craig Mautner360310b2012-10-26 15:13:08 -07001385 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001386 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001387 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1388 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001389 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001390 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001391
John Spurlock77e1f472013-09-11 10:09:51 -04001392 mLauncherView.setSystemUiVisibility(
1393 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001394 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001395
Winson Chung4c98d922011-05-31 16:50:48 -07001396 // Setup the drag layer
1397 mDragLayer.setup(this, dragController);
1398
Winson Chung3d503fb2011-07-13 17:25:49 -07001399 // Setup the hotseat
1400 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1401 if (mHotseat != null) {
1402 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001403 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001404 }
1405
Jorim Jaggid017f882014-01-14 17:08:48 -08001406 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001407 View widgetButton = findViewById(R.id.widget_button);
1408 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001409 @Override
1410 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001411 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001412 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001413 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001414 }
1415 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001416 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1417
1418 View wallpaperButton = findViewById(R.id.wallpaper_button);
1419 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001420 @Override
1421 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001422 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001423 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001424 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001425 }
1426 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001427 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1428
1429 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001430 if (hasSettings()) {
1431 settingsButton.setOnClickListener(new OnClickListener() {
1432 @Override
1433 public void onClick(View arg0) {
1434 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001435 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001436 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001437 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001438 });
1439 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1440 } else {
1441 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001442 }
1443
Adam Cohendbdff6b2013-09-18 19:09:15 -07001444 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001445
Winson Chung4c98d922011-05-31 16:50:48 -07001446 // Setup the workspace
1447 mWorkspace.setHapticFeedbackEnabled(false);
1448 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001449 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001450 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001451
Winson Chungf0ea4d32011-06-06 14:27:16 -07001452 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001453 mSearchDropTargetBar = (SearchDropTargetBar)
1454 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001455
Winson Chungf0ea4d32011-06-06 14:27:16 -07001456 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001457 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001458 mAppsCustomizeContent = (AppsCustomizePagedView)
1459 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1460 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001461
Winson Chung3d503fb2011-07-13 17:25:49 -07001462 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001463 dragController.setDragScoller(mWorkspace);
1464 dragController.setScrollView(mDragLayer);
1465 dragController.setMoveTarget(mWorkspace);
1466 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001467 if (mSearchDropTargetBar != null) {
1468 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001469 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001470
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001471 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001472 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001473 mWeightWatcher = new WeightWatcher(this);
1474 mWeightWatcher.setAlpha(0.5f);
1475 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001476 new FrameLayout.LayoutParams(
1477 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001478 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001479 Gravity.BOTTOM)
1480 );
Adam Cohen39a06042013-07-19 14:30:12 -07001481
1482 boolean show = shouldShowWeightWatcher();
1483 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001484 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001485 }
1486
1487 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001488 * Sets the all apps button. This method is called from {@link Hotseat}.
1489 */
1490 public void setAllAppsButton(View allAppsButton) {
1491 mAllAppsButton = allAppsButton;
1492 }
1493
1494 public View getAllAppsButton() {
1495 return mAllAppsButton;
1496 }
1497
1498 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001499 * Creates a view representing a shortcut.
1500 *
1501 * @param info The data structure describing the shortcut.
1502 *
1503 * @return A View inflated from R.layout.application.
1504 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001505 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001507 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001508 }
1509
1510 /**
1511 * Creates a view representing a shortcut inflated from the specified resource.
1512 *
1513 * @param layoutResId The id of the XML layout used to create the shortcut.
1514 * @param parent The group the shortcut belongs to.
1515 * @param info The data structure describing the shortcut.
1516 *
1517 * @return A View inflated from layoutResId.
1518 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001519 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001520 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001521 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001522 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001523 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001524 return favorite;
1525 }
1526
1527 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001528 * Add a shortcut to the workspace.
1529 *
1530 * @param data The intent describing the shortcut.
1531 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001532 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001533 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001534 int cellY) {
1535 int[] cellXY = mTmpAddItemCellCoordinates;
1536 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001537 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001538
Michael Jurkad3ef3062010-11-23 16:23:58 -08001539 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001540
Sunny Goyal2350bc92014-10-14 16:42:54 -07001541 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001542 if (info == null) {
1543 return;
1544 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001545 final View view = createShortcut(info);
1546
Adam Cohenfbba09b2011-07-18 21:43:05 -07001547 // First we check if we already know the exact location where we want to add this item.
1548 if (cellX >= 0 && cellY >= 0) {
1549 cellXY[0] = cellX;
1550 cellXY[1] = cellY;
1551 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001552
1553 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001554 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001555 true, null,null)) {
1556 return;
1557 }
1558 DragObject dragObject = new DragObject();
1559 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001560 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1561 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001562 return;
1563 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001564 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001565 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001566 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001567 foundCellSpan = (result != null);
1568 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001569 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001570 }
1571
1572 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001573 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001574 return;
1575 }
1576
Adam Cohendcd297f2013-06-18 13:13:40 -07001577 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001578
1579 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001580 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001581 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001582 }
1583 }
1584
Adam Cohen2f093b62012-04-30 18:59:53 -07001585 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1586 int minHeight) {
1587 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001588 // We want to account for the extra amount of padding that we are adding to the widget
1589 // to ensure that it gets the full amount of space that it has requested
1590 int requiredWidth = minWidth + padding.left + padding.right;
1591 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001592 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001593 }
1594
Adam Cohen2f093b62012-04-30 18:59:53 -07001595 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1596 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001597 }
1598
Adam Cohen2f093b62012-04-30 18:59:53 -07001599 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1600 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001601 }
1602
Adam Cohen2f093b62012-04-30 18:59:53 -07001603 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1604 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001605 }
1606
Adam Cohen2f093b62012-04-30 18:59:53 -07001607 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1608 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001609 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001610 }
1611
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001612 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001613 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001614 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001615 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001616 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001617 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001618 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001619 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1620 if (appWidgetInfo == null) {
1621 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1622 }
Romain Guycbb89e42009-06-08 15:52:54 -07001623
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001624 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001625 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001626
Adam Cohen2f093b62012-04-30 18:59:53 -07001627 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1628 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001629
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001630 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001631 // We have saved the position to which the widget was dragged-- this really only matters
1632 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001633 int[] cellXY = mTmpAddItemCellCoordinates;
1634 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001635 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001636 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001637 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1638 cellXY[0] = mPendingAddInfo.cellX;
1639 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001640 spanXY[0] = mPendingAddInfo.spanX;
1641 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001642 foundCellSpan = true;
1643 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001644 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001645 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001646 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1647 spanXY[1], cellXY, finalSpan);
1648 spanXY[0] = finalSpan[0];
1649 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001650 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001651 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001652 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001653 }
1654
Michael Jurka0280c3b2010-09-17 15:00:07 -07001655 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001656 if (appWidgetId != -1) {
1657 // Deleting an app widget ID is a void call but writes to disk before returning
1658 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001659 new AsyncTask<Void, Void, Void>() {
1660 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001661 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001662 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001663 }
Michael Jurka43467462013-11-14 12:03:58 +01001664 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001665 }
Winson Chung93eef082012-03-23 15:59:27 -07001666 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001667 return;
1668 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001669
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001670 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001671 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1672 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001673 launcherInfo.spanX = spanXY[0];
1674 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001675 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1676 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001677 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001678
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001679 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001680 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001681
1682 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001683 if (hostView == null) {
1684 // Perform actual inflation because we're live
1685 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1686 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1687 } else {
1688 // The AppWidgetHostView has already been inflated and instantiated
1689 launcherInfo.hostView = hostView;
1690 }
Romain Guycbb89e42009-06-08 15:52:54 -07001691
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001692 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001693 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001694 launcherInfo.notifyWidgetSizeChanged(this);
1695
Adam Cohendcd297f2013-06-18 13:13:40 -07001696 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001697 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001698
1699 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001700 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001701 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001702 }
Romain Guycbb89e42009-06-08 15:52:54 -07001703
Adam Cohended9f8d2010-11-03 13:25:16 -07001704 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1705 @Override
1706 public void onReceive(Context context, Intent intent) {
1707 final String action = intent.getAction();
1708 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1709 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001710 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001712
Winson Chungc100e8e2011-08-09 16:02:43 -07001713 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001714 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001715 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001716 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001717 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001718 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1719 mUserPresent = true;
1720 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001721 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1722 mModel.resetLoadedState(false, true);
1723 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1724 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1725 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1726 mModel.resetLoadedState(false, true);
1727 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1728 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1729 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001730 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1731 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1732 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001733 }
1734 }
1735 };
1736
1737 @Override
1738 public void onAttachedToWindow() {
1739 super.onAttachedToWindow();
1740
1741 // Listen for broadcasts related to user-presence
1742 final IntentFilter filter = new IntentFilter();
1743 filter.addAction(Intent.ACTION_SCREEN_OFF);
1744 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001745 // For handling managed profiles
1746 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1747 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001748 if (ENABLE_DEBUG_INTENTS) {
1749 filter.addAction(DebugIntents.DELETE_DATABASE);
1750 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1751 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001752 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001753 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001754 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001755 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001756 mVisible = true;
1757 }
1758
Adam Cohen0b395352014-06-09 22:54:36 +00001759 /**
1760 * Sets up transparent navigation and status bars in LMP.
1761 * This method is a no-op for other platform versions.
1762 */
1763 @TargetApi(19)
1764 private void setupTransparentSystemBarsForLmp() {
1765 // TODO(sansid): use the APIs directly when compiling against L sdk.
1766 // Currently we use reflection to access the flags and the API to set the transparency
1767 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001768 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001769 try {
1770 getWindow().getAttributes().systemUiVisibility |=
1771 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1772 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1773 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1774 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1775 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1776 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1777 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1778 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1779
1780 Method setStatusBarColorMethod =
1781 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1782 Method setNavigationBarColorMethod =
1783 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1784 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1785 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1786 } catch (NoSuchFieldException e) {
1787 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1788 } catch (NoSuchMethodException ex) {
1789 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1790 } catch (IllegalAccessException e) {
1791 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1792 } catch (IllegalArgumentException e) {
1793 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1794 } catch (InvocationTargetException e) {
1795 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1796 } finally {}
1797 }
1798 }
1799
Adam Cohended9f8d2010-11-03 13:25:16 -07001800 @Override
1801 public void onDetachedFromWindow() {
1802 super.onDetachedFromWindow();
1803 mVisible = false;
1804
Adam Cohend113e0c2010-11-11 10:48:05 -08001805 if (mAttached) {
1806 unregisterReceiver(mReceiver);
1807 mAttached = false;
1808 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001809 updateRunning();
1810 }
1811
1812 public void onWindowVisibilityChanged(int visibility) {
1813 mVisible = visibility == View.VISIBLE;
1814 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001815 // The following code used to be in onResume, but it turns out onResume is called when
1816 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1817 // is a more appropriate event to handle
1818 if (mVisible) {
1819 mAppsCustomizeTabHost.onWindowVisible();
1820 if (!mWorkspaceLoading) {
1821 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001822 // We want to let Launcher draw itself at least once before we force it to build
1823 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001824 // apps is nice and speedy.
1825 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001826 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001827 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001828 if (mStarted) return;
1829 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001830 // We delay the layer building a bit in order to give
1831 // other message processing a time to run. In particular
1832 // this avoids a delay in hiding the IME if it was
1833 // currently shown, because doing that may involve
1834 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001835 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001836 final ViewTreeObserver.OnDrawListener listener = this;
1837 mWorkspace.post(new Runnable() {
1838 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001839 if (mWorkspace != null &&
1840 mWorkspace.getViewTreeObserver() != null) {
1841 mWorkspace.getViewTreeObserver().
1842 removeOnDrawListener(listener);
1843 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001844 }
1845 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001846 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001847 }
1848 });
1849 }
1850 clearTypedText();
1851 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001852 }
1853
1854 private void sendAdvanceMessage(long delay) {
1855 mHandler.removeMessages(ADVANCE_MSG);
1856 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1857 mHandler.sendMessageDelayed(msg, delay);
1858 mAutoAdvanceSentTime = System.currentTimeMillis();
1859 }
1860
1861 private void updateRunning() {
1862 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1863 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1864 mAutoAdvanceRunning = autoAdvanceRunning;
1865 if (autoAdvanceRunning) {
1866 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1867 sendAdvanceMessage(delay);
1868 } else {
1869 if (!mWidgetsToAdvance.isEmpty()) {
1870 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1871 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1872 }
1873 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001874 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001875 }
1876 }
1877 }
1878
1879 private final Handler mHandler = new Handler() {
1880 @Override
1881 public void handleMessage(Message msg) {
1882 if (msg.what == ADVANCE_MSG) {
1883 int i = 0;
1884 for (View key: mWidgetsToAdvance.keySet()) {
1885 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1886 final int delay = mAdvanceStagger * i;
1887 if (v instanceof Advanceable) {
1888 postDelayed(new Runnable() {
1889 public void run() {
1890 ((Advanceable) v).advance();
1891 }
1892 }, delay);
1893 }
1894 i++;
1895 }
1896 sendAdvanceMessage(mAdvanceInterval);
1897 }
1898 }
1899 };
1900
1901 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001902 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001903 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1904 if (v instanceof Advanceable) {
1905 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001906 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001907 updateRunning();
1908 }
1909 }
1910
1911 void removeWidgetToAutoAdvance(View hostView) {
1912 if (mWidgetsToAdvance.containsKey(hostView)) {
1913 mWidgetsToAdvance.remove(hostView);
1914 updateRunning();
1915 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001916 }
1917
Joe Onorato9c1289c2009-08-17 11:03:03 -04001918 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001919 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001920 launcherInfo.hostView = null;
1921 }
1922
Winson Chung93eef082012-03-23 15:59:27 -07001923 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1924 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1925 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001926 }
1927
Winson Chunga6945242014-01-08 14:04:34 -08001928 public DragLayer getDragLayer() {
1929 return mDragLayer;
1930 }
1931
1932 public Workspace getWorkspace() {
1933 return mWorkspace;
1934 }
1935
1936 public Hotseat getHotseat() {
1937 return mHotseat;
1938 }
1939
Jorim Jaggid017f882014-01-14 17:08:48 -08001940 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001941 return mOverviewPanel;
1942 }
1943
1944 public SearchDropTargetBar getSearchBar() {
1945 return mSearchDropTargetBar;
1946 }
1947
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001948 public LauncherAppWidgetHost getAppWidgetHost() {
1949 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001950 }
Romain Guycbb89e42009-06-08 15:52:54 -07001951
Winson Chunga9abd0e2010-10-27 17:18:37 -07001952 public LauncherModel getModel() {
1953 return mModel;
1954 }
1955
Winson Chunga6945242014-01-08 14:04:34 -08001956 protected SharedPreferences getSharedPrefs() {
1957 return mSharedPrefs;
1958 }
1959
Bjorn Bringertc459e522013-06-07 19:36:01 +01001960 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001961 getWindow().closeAllPanels();
1962
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001963 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001964 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001965 }
1966
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001967 @Override
1968 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001969 long startTime = 0;
1970 if (DEBUG_RESUME_TIME) {
1971 startTime = System.currentTimeMillis();
1972 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001973 super.onNewIntent(intent);
1974
1975 // Close the menu
1976 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001977 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001978 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001979
Adam Cohened307df2013-10-02 09:37:31 -07001980 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1981 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1982 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001983
Adam Cohen6fecd412013-10-02 17:41:50 -07001984 if (mWorkspace == null) {
1985 // Can be cases where mWorkspace is null, this prevents a NPE
1986 return;
1987 }
1988 Folder openFolder = mWorkspace.getOpenFolder();
1989 // In all these cases, only animate if we're already on home
1990 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001991
1992 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
1993 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07001994 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07001995 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07001996 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07001997 }
Romain Guy1dd3a072009-07-16 13:21:01 -07001998
Adam Cohen6fecd412013-10-02 17:41:50 -07001999 closeFolder();
2000 exitSpringLoadedDragMode();
2001
2002 // If we are already on home, then just animate back to the workspace,
2003 // otherwise, just wait until onResume to set the state back to Workspace
2004 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07002005 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07002006 } else {
2007 mOnResumeState = State.WORKSPACE;
2008 }
2009
2010 final View v = getWindow().peekDecorView();
2011 if (v != null && v.getWindowToken() != null) {
2012 InputMethodManager imm = (InputMethodManager)getSystemService(
2013 INPUT_METHOD_SERVICE);
2014 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
2015 }
2016
2017 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08002018 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07002019 mAppsCustomizeTabHost.reset();
2020 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002021
Adam Cohen9211d422014-10-07 18:14:53 -07002022 if (mLauncherCallbacks != null) {
2023 mLauncherCallbacks.onHomeIntent();
2024 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002025 }
Adam Cohened307df2013-10-02 09:37:31 -07002026
Michael Jurka447bf842013-05-15 14:52:15 +02002027 if (DEBUG_RESUME_TIME) {
2028 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
2029 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002030
Adam Cohen9211d422014-10-07 18:14:53 -07002031 if (mLauncherCallbacks != null) {
2032 mLauncherCallbacks.onNewIntent(intent);
2033 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002034 }
2035
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002036 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07002037 public void onRestoreInstanceState(Bundle state) {
2038 super.onRestoreInstanceState(state);
2039 for (int page: mSynchronouslyBoundPages) {
2040 mWorkspace.restoreInstanceStateForChild(page);
2041 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002042 }
2043
2044 @Override
2045 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002046 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002047 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2048 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002049 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002050 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002051
Michael Jurka883f55b2010-10-21 15:47:14 -07002052 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002053 // We close any open folder since it will not be re-opened, and we need to make sure
2054 // this state is reflected.
2055 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002056
Adam Cohendcd297f2013-06-18 13:13:40 -07002057 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002058 mWaitingForResult) {
2059 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002060 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002061 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2062 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002063 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2064 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2065 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002066 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002067 }
2068
2069 if (mFolderInfo != null && mWaitingForResult) {
2070 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2071 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2072 }
Michael Jurka946ad472010-07-09 18:05:18 -07002073
Winson Chung785d2eb2011-04-14 16:08:02 -07002074 // Save the current AppsCustomize tab
2075 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c62013-11-06 15:49:51 -08002076 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2077 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002078 if (currentTabTag != null) {
2079 outState.putString("apps_customize_currentTab", currentTabTag);
2080 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002081 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2082 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002083 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002084 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002085
2086 if (mLauncherCallbacks != null) {
2087 mLauncherCallbacks.onSaveInstanceState(outState);
2088 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002089 }
2090
2091 @Override
2092 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002093 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002094
Winson Chunge7a03942011-08-05 15:05:12 -07002095 // Remove all pending runnables
2096 mHandler.removeMessages(ADVANCE_MSG);
2097 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002098 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002099
Winson Chungcd2b0142011-06-08 16:02:26 -07002100 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002101 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002102
2103 // It's possible to receive onDestroy after a new Launcher activity has
2104 // been created. In this case, don't interfere with the new Launcher.
2105 if (mModel.isCurrentCallbacks(this)) {
2106 mModel.stopLoader();
2107 app.setLauncher(null);
2108 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002109
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002110 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002111 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002112 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002113 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002114 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002115 mAppWidgetHost = null;
2116
2117 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002118
2119 TextKeyListener.getInstance().release();
2120
Winson Chung81b52252012-08-27 15:34:29 -07002121 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2122 // to prevent leaking Launcher activities on orientation change.
2123 if (mModel != null) {
2124 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2125 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002126
2127 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002128 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002129
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002130 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002131 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002132 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002133 mWorkspace = null;
2134 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002135
Michael Jurka2ecf9952012-06-18 12:52:28 -07002136 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002137
2138 if (mLauncherCallbacks != null) {
2139 mLauncherCallbacks.onDestroy();
2140 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002141 }
2142
Adam Cohena9cf38f2011-05-02 15:36:58 -07002143 public DragController getDragController() {
2144 return mDragController;
2145 }
2146
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002147 @Override
2148 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002149 if (requestCode >= 0) {
2150 setWaitingForResult(true);
2151 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002152 super.startActivityForResult(intent, requestCode);
2153 }
2154
Winson Chung70d72102011-08-12 11:24:35 -07002155 /**
2156 * Indicates that we want global search for this activity by setting the globalSearch
2157 * argument for {@link #startSearch} to true.
2158 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002160 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002161 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002162
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002163 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002164
Karl Rosaen138a0412009-04-23 19:00:21 -07002165 if (initialQuery == null) {
2166 // Use any text typed in the launcher as the initial query
2167 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002168 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002169 if (appSearchData == null) {
2170 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002171 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002172 }
Winson Chungadf0c182012-08-23 14:59:07 -07002173 Rect sourceBounds = new Rect();
2174 if (mSearchDropTargetBar != null) {
2175 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2176 }
Romain Guycbb89e42009-06-08 15:52:54 -07002177
Ian Parkinson047036e2014-09-01 15:40:53 +01002178 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002179 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002180 if (clearTextImmediately) {
2181 clearTypedText();
2182 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002183 }
2184
Ian Parkinson047036e2014-09-01 15:40:53 +01002185 /**
2186 * Start a text search.
2187 *
2188 * @return {@code true} if the search will start immediately, so any further keypresses
2189 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2190 * to buffer keypresses.
2191 */
2192 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002193 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002194 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2195 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2196 sourceBounds);
2197 }
2198
Michael Jurkaa33411c2012-06-14 16:18:21 -07002199 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002200 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002201 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002202 }
2203
2204 /**
2205 * Starts the global search activity. This code is a copied from SearchManager
2206 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002207 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002208 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002209 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002210 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2211 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2212 if (globalSearchActivity == null) {
2213 Log.w(TAG, "No global search activity found.");
2214 return;
2215 }
2216 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2217 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2218 intent.setComponent(globalSearchActivity);
2219 // Make sure that we have a Bundle to put source in
2220 if (appSearchData == null) {
2221 appSearchData = new Bundle();
2222 } else {
2223 appSearchData = new Bundle(appSearchData);
2224 }
Adam Cohen9211d422014-10-07 18:14:53 -07002225 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002226 if (!appSearchData.containsKey("source")) {
2227 appSearchData.putString("source", getPackageName());
2228 }
2229 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2230 if (!TextUtils.isEmpty(initialQuery)) {
2231 intent.putExtra(SearchManager.QUERY, initialQuery);
2232 }
2233 if (selectInitialQuery) {
2234 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2235 }
2236 intent.setSourceBounds(sourceBounds);
2237 try {
2238 startActivity(intent);
2239 } catch (ActivityNotFoundException ex) {
2240 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2241 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002242 }
2243
Yura4f93ec62014-02-04 14:15:21 +00002244 public boolean isOnCustomContent() {
2245 return mWorkspace.isOnOrMovingToCustomContent();
2246 }
2247
Winson Chung70d72102011-08-12 11:24:35 -07002248 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002249 public boolean onPrepareOptionsMenu(Menu menu) {
2250 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002251 if (!isOnCustomContent()) {
2252 // Close any open folders
2253 closeFolder();
2254 // Stop resizing any widgets
2255 mWorkspace.exitWidgetResizeMode();
2256 if (!mWorkspace.isInOverviewMode()) {
2257 // Show the overview mode
2258 showOverviewMode(true);
2259 } else {
2260 showWorkspace(true);
2261 }
Winson Chunge0298742014-01-17 12:03:00 -08002262 }
Adam Cohen9211d422014-10-07 18:14:53 -07002263 if (mLauncherCallbacks != null) {
2264 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2265 }
2266
Michael Jurkab94f3f82013-09-11 18:08:54 +02002267 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002268 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002269
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002270 @Override
2271 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002272 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002273 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002274 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002275 }
2276
Joe Onorato9c1289c2009-08-17 11:03:03 -04002277 public boolean isWorkspaceLocked() {
2278 return mWorkspaceLoading || mWaitingForResult;
2279 }
2280
Adam Cohen517a7f52014-03-01 12:12:59 -08002281 public boolean isWorkspaceLoading() {
2282 return mWorkspaceLoading;
2283 }
2284
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002285 private void setWorkspaceLoading(boolean value) {
2286 boolean isLocked = isWorkspaceLocked();
2287 mWorkspaceLoading = value;
2288 if (isLocked != isWorkspaceLocked()) {
2289 onWorkspaceLockedChanged();
2290 }
2291 }
2292
2293 private void setWaitingForResult(boolean value) {
2294 boolean isLocked = isWorkspaceLocked();
2295 mWaitingForResult = value;
2296 if (isLocked != isWorkspaceLocked()) {
2297 onWorkspaceLockedChanged();
2298 }
2299 }
2300
Adam Cohen9211d422014-10-07 18:14:53 -07002301 protected void onWorkspaceLockedChanged() {
2302 if (mLauncherCallbacks != null) {
2303 mLauncherCallbacks.onWorkspaceLockedChanged();
2304 }
2305 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002306
Michael Jurka0280c3b2010-09-17 15:00:07 -07002307 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002308 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002309 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002310 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2311 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002312 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002313 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002314 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002315
Adam Cohenad4e15c2013-10-17 16:21:35 -07002316 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2317 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2318 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2319 }
2320
2321 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2322 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2323 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002324 if (appWidgetInfo.configure != null) {
2325 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002326 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002327
Bjorn Bringert7984c942009-12-09 15:38:25 +00002328 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002329 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2330 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2331
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002332 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002333 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002334 Runnable onComplete = new Runnable() {
2335 @Override
2336 public void run() {
2337 // Exit spring loaded mode if necessary after adding the widget
2338 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2339 null);
2340 }
2341 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002342 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002343 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002344 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002345 }
2346 }
Romain Guycbb89e42009-06-08 15:52:54 -07002347
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002348 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002349 // Close any folders that may be open.
2350 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002351 mWorkspace.moveToCustomContentScreen(animate);
2352 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002353 /**
2354 * Process a shortcut drop.
2355 *
2356 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002357 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002358 * @param cell The cell it should be added to, optional
2359 * @param position The location on the screen where it was dropped, optional
2360 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002361 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002362 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002363 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002364 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002365 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002366 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002367
2368 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002369 mPendingAddInfo.cellX = cell[0];
2370 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002371 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002372
2373 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2374 createShortcutIntent.setComponent(componentName);
2375 processShortcut(createShortcutIntent);
2376 }
2377
Adam Cohenfbba09b2011-07-18 21:43:05 -07002378 /**
2379 * Process a widget drop.
2380 *
2381 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002382 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002383 * @param cell The cell it should be added to, optional
2384 * @param position The location on the screen where it was dropped, optional
2385 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002386 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002387 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002388 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002389 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002390 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002391 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002392 mPendingAddInfo.minSpanX = info.minSpanX;
2393 mPendingAddInfo.minSpanY = info.minSpanY;
2394
Adam Cohenfbba09b2011-07-18 21:43:05 -07002395 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002396 mPendingAddInfo.cellX = cell[0];
2397 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002398 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002399 if (span != null) {
2400 mPendingAddInfo.spanX = span[0];
2401 mPendingAddInfo.spanY = span[1];
2402 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002403
Adam Cohened66b2b2012-01-23 17:28:51 -08002404 AppWidgetHostView hostView = info.boundWidget;
2405 int appWidgetId;
2406 if (hostView != null) {
2407 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002408 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002409 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002410 // In this case, we either need to start an activity to get permission to bind
2411 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002412 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002413 Bundle options = info.bindOptions;
2414
Sunny Goyalffe83f12014-08-14 17:39:34 -07002415 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2416 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002417 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002418 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002419 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002420 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002421 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2422 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2423 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002424 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2425 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002426 // TODO: we need to make sure that this accounts for the options bundle.
2427 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002428 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2429 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002430 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002431 }
2432
Joe Onoratodeb98af2010-02-19 14:59:39 -08002433 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002434 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002435 }
2436
Winson Chung24ab2f12010-09-16 14:10:47 -07002437 void processWallpaper(Intent intent) {
2438 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2439 }
2440
Adam Cohendcd297f2013-06-18 13:13:40 -07002441 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002442 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002443 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002444 folderInfo.title = getText(R.string.folder_name);
2445
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002446 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002447 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002448 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002449 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002450
2451 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002452 FolderIcon newFolder =
2453 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002454 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002455 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002456 // Force measure the new folder icon
2457 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2458 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002459 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002460 }
Romain Guycbb89e42009-06-08 15:52:54 -07002461
Joe Onorato9c1289c2009-08-17 11:03:03 -04002462 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002463 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002464 }
2465
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002466 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002467 if (mLauncherCallbacks != null) {
2468 return mLauncherCallbacks.getWallpaperPickerComponent();
2469 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002470 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002471 }
2472
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002473 /**
2474 * Registers various content observers. The current implementation registers
2475 * only a favorites observer to keep track of the favorites applications.
2476 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002477 private void registerContentObservers() {
2478 ContentResolver resolver = getContentResolver();
2479 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2480 true, mWidgetObserver);
2481 }
2482
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002483 @Override
2484 public boolean dispatchKeyEvent(KeyEvent event) {
2485 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2486 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002487 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002488 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002489 case KeyEvent.KEYCODE_VOLUME_DOWN:
Sunny Goyal4bbf4192014-11-11 12:23:59 -08002490 if (Utilities.isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002491 dumpState();
2492 return true;
2493 }
2494 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002495 }
2496 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2497 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002498 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002499 return true;
2500 }
2501 }
2502
2503 return super.dispatchKeyEvent(event);
2504 }
2505
Joe Onorato88ec0992009-11-19 13:16:06 -08002506 @Override
2507 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002508 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2509 return;
2510 }
2511
Winson Chungc93e5ae2012-07-23 20:48:26 -07002512 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002513 if (mAppsCustomizeContent.getContentType() ==
2514 AppsCustomizePagedView.ContentType.Applications) {
2515 showWorkspace(true);
2516 } else {
2517 showOverviewMode(true);
2518 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002519 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002520 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002521 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002522 Folder openFolder = mWorkspace.getOpenFolder();
2523 if (openFolder.isEditingName()) {
2524 openFolder.dismissEditingName();
2525 } else {
2526 closeFolder();
2527 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002528 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002529 mWorkspace.exitWidgetResizeMode();
2530
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002531 // Back button is a no-op here, but give at least some feedback for the button press
2532 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002533 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002534 }
2535
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002536 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002537 * Re-listen when widgets are reset.
2538 */
2539 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002540 if (mAppWidgetHost != null) {
2541 mAppWidgetHost.startListening();
2542 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002543 }
2544
2545 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002546 * Launches the intent referred by the clicked shortcut.
2547 *
2548 * @param v The view representing the clicked shortcut.
2549 */
2550 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002551 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2552 // view has detached (it's possible for this to happen if the view is removed mid touch).
2553 if (v.getWindowToken() == null) {
2554 return;
2555 }
2556
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002557 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002558 return;
2559 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002560
Adam Cohen1697b792013-09-17 19:08:21 -07002561 if (v instanceof Workspace) {
2562 if (mWorkspace.isInOverviewMode()) {
2563 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002564 }
2565 return;
2566 }
2567
2568 if (v instanceof CellLayout) {
2569 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002570 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002571 }
2572 }
2573
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002574 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002575 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002576 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002577 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002578 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002579 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002580 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002581 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002582 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002583 } else if (tag instanceof AppInfo) {
2584 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002585 } else if (tag instanceof LauncherAppWidgetInfo) {
2586 if (v instanceof PendingAppWidgetHostView) {
2587 onClickPendingWidget((PendingAppWidgetHostView) v);
2588 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002589 }
2590 }
2591
Sunny Goyal508da152014-08-14 10:53:27 -07002592 public void onClickPagedViewIcon(View v) {
2593 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002594 if (mLauncherCallbacks != null) {
2595 mLauncherCallbacks.onClickPagedViewIcon(v);
2596 }
Sunny Goyal508da152014-08-14 10:53:27 -07002597 }
2598
Michael Jurka0e260592010-06-30 17:07:39 -07002599 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002600 return false;
2601 }
2602
Michael Jurkaaf442092010-06-10 17:01:57 -07002603 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002604 * Event handler for the app widget view which has not fully restored.
2605 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002606 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002607 if (mIsSafeModeEnabled) {
2608 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2609 return;
2610 }
2611
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002612 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002613 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002614 int widgetId = info.appWidgetId;
2615 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2616 if (appWidgetInfo != null) {
2617 mPendingAddWidgetInfo = appWidgetInfo;
2618 mPendingAddInfo.copyFrom(info);
2619 mPendingAddWidgetId = widgetId;
2620
Sunny Goyalffe83f12014-08-14 17:39:34 -07002621 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2622 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002623 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002624 } else if (info.installProgress < 0) {
2625 // The install has not been queued
2626 final String packageName = info.providerName.getPackageName();
2627 showBrokenAppInstallDialog(packageName,
2628 new DialogInterface.OnClickListener() {
2629 public void onClick(DialogInterface dialog, int id) {
2630 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2631 }
2632 });
2633 } else {
2634 // Download has started.
2635 final String packageName = info.providerName.getPackageName();
2636 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002637 }
2638 }
2639
2640 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002641 * Event handler for the search button
2642 *
2643 * @param v The view that was clicked.
2644 */
2645 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002646 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2647
Amith Yamasania135ba82011-08-09 17:42:01 -07002648 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002649 }
2650
2651 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002652 * Event handler for the voice button
2653 *
2654 * @param v The view that was clicked.
2655 */
2656 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002657 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002658
Bjorn Bringertc459e522013-06-07 19:36:01 +01002659 startVoice();
2660 }
2661
2662 public void startVoice() {
Adam Cohen9211d422014-10-07 18:14:53 -07002663 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2664 mLauncherCallbacks.startVoice();
2665 return;
2666 }
2667
Michael Jurkaae65ee32012-04-30 11:45:54 -07002668 try {
2669 final SearchManager searchManager =
2670 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2671 ComponentName activityName = searchManager.getGlobalSearchActivity();
2672 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002673 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002674 if (activityName != null) {
2675 intent.setPackage(activityName.getPackageName());
2676 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002677 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002678 } catch (ActivityNotFoundException e) {
2679 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002680 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002681 startActivitySafely(null, intent, "onClickVoiceButton");
2682 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002683 }
2684
2685 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002686 * Event handler for the "grid" button that appears on the home screen, which
2687 * enters all apps mode.
2688 *
2689 * @param v The view that was clicked.
2690 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002691 protected void onClickAllAppsButton(View v) {
2692 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2693 if (isAllAppsVisible()) {
2694 showWorkspace(true);
2695 } else {
2696 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2697 }
Adam Cohen9211d422014-10-07 18:14:53 -07002698 if (mLauncherCallbacks != null) {
2699 mLauncherCallbacks.onClickAllAppsButton(v);
2700 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002701 }
2702
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002703 private void showBrokenAppInstallDialog(final String packageName,
2704 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002705 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002706 .setTitle(R.string.abandoned_promises_title)
2707 .setMessage(R.string.abandoned_promise_explanation)
2708 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2709 .setNeutralButton(R.string.abandoned_clean_this,
2710 new DialogInterface.OnClickListener() {
2711 public void onClick(DialogInterface dialog, int id) {
2712 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2713 mWorkspace.removeAbandonedPromise(packageName, user);
2714 }
2715 })
2716 .create().show();
2717 return;
2718 }
2719
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002720 /**
2721 * Event handler for an app shortcut click.
2722 *
2723 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2724 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002725 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002726 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2727 Object tag = v.getTag();
2728 if (!(tag instanceof ShortcutInfo)) {
2729 throw new IllegalArgumentException("Input must be a Shortcut");
2730 }
2731
2732 // Open shortcut
2733 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002734
2735 if (shortcut.isDisabled != 0) {
2736 int error = R.string.activity_not_available;
2737 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2738 error = R.string.safemode_shortcut_error;
2739 }
2740 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2741 return;
2742 }
2743
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002744 final Intent intent = shortcut.intent;
2745
2746 // Check for special shortcuts
2747 if (intent.getComponent() != null) {
2748 final String shortcutClass = intent.getComponent().getClassName();
2749
2750 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2751 MemoryDumpActivity.startDump(this);
2752 return;
2753 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2754 toggleShowWeightWatcher();
2755 return;
2756 }
2757 }
2758
Chris Wren40c5ed32014-06-24 18:24:23 -04002759 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002760 if ((v instanceof BubbleTextView)
2761 && shortcut.isPromise()
2762 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002763 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002764 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002765 new DialogInterface.OnClickListener() {
2766 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002767 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002768 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002769 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002770 return;
2771 }
2772
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002773 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002774 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002775
2776 if (mLauncherCallbacks != null) {
2777 mLauncherCallbacks.onClickAppShortcut(v);
2778 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002779 }
2780
Sunny Goyal508da152014-08-14 10:53:27 -07002781 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002782 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002783 final ShortcutInfo shortcut;
2784 final Intent intent;
2785 if (tag instanceof ShortcutInfo) {
2786 shortcut = (ShortcutInfo) tag;
2787 intent = shortcut.intent;
2788 int[] pos = new int[2];
2789 v.getLocationOnScreen(pos);
2790 intent.setSourceBounds(new Rect(pos[0], pos[1],
2791 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002792
Sunny Goyal508da152014-08-14 10:53:27 -07002793 } else if (tag instanceof AppInfo) {
2794 shortcut = null;
2795 intent = ((AppInfo) tag).intent;
2796 } else {
2797 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2798 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002799
2800 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002801 mStats.recordLaunch(intent, shortcut);
2802
2803 if (success && v instanceof BubbleTextView) {
2804 mWaitingForResume = (BubbleTextView) v;
2805 mWaitingForResume.setStayPressed(true);
2806 }
2807 }
2808
2809 /**
2810 * Event handler for a folder icon click.
2811 *
2812 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2813 */
2814 protected void onClickFolderIcon(View v) {
2815 if (LOGD) Log.d(TAG, "onClickFolder");
2816 if (!(v instanceof FolderIcon)){
2817 throw new IllegalArgumentException("Input must be a FolderIcon");
2818 }
2819
2820 FolderIcon folderIcon = (FolderIcon) v;
2821 final FolderInfo info = folderIcon.getFolderInfo();
2822 Folder openFolder = mWorkspace.getFolderForTag(info);
2823
2824 // If the folder info reports that the associated folder is open, then verify that
2825 // it is actually opened. There have been a few instances where this gets out of sync.
2826 if (info.opened && openFolder == null) {
2827 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2828 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2829 info.opened = false;
2830 }
2831
2832 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2833 // Close any open folder
2834 closeFolder();
2835 // Open the requested folder
2836 openFolder(folderIcon);
2837 } else {
2838 // Find the open folder...
2839 int folderScreen;
2840 if (openFolder != null) {
2841 folderScreen = mWorkspace.getPageForView(openFolder);
2842 // .. and close it
2843 closeFolder(openFolder);
2844 if (folderScreen != mWorkspace.getCurrentPage()) {
2845 // Close any folder open on the current screen
2846 closeFolder();
2847 // Pull the folder onto this screen
2848 openFolder(folderIcon);
2849 }
2850 }
2851 }
Adam Cohen9211d422014-10-07 18:14:53 -07002852
2853 if (mLauncherCallbacks != null) {
2854 mLauncherCallbacks.onClickFolderIcon(v);
2855 }
Michael Jurka5130e402011-10-13 04:55:35 -07002856 }
2857
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002858 /**
2859 * Event handler for the (Add) Widgets button that appears after a long press
2860 * on the home screen.
2861 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002862 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002863 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002864 if (mIsSafeModeEnabled) {
2865 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2866 } else {
2867 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2868 if (mLauncherCallbacks != null) {
2869 mLauncherCallbacks.onClickAddWidgetButton(view);
2870 }
Adam Cohen9211d422014-10-07 18:14:53 -07002871 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002872 }
2873
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002874 /**
2875 * Event handler for the wallpaper picker button that appears after a long press
2876 * on the home screen.
2877 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002878 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002879 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2880 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2881 pickWallpaper.setComponent(getWallpaperPickerComponent());
2882 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002883
2884 if (mLauncherCallbacks != null) {
2885 mLauncherCallbacks.onClickWallpaperPicker(v);
2886 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002887 }
2888
2889 /**
2890 * Event handler for a click on the settings button that appears after a long press
2891 * on the home screen.
2892 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002893 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002894 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002895 if (mLauncherCallbacks != null) {
2896 mLauncherCallbacks.onClickSettingsButton(v);
2897 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002898 }
2899
Michael Jurka5130e402011-10-13 04:55:35 -07002900 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002901 // Provide the same haptic feedback that the system offers for virtual keys.
2902 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002903 }
2904
Adam Cohen61f560d2013-09-30 15:58:20 -07002905 public void performHapticFeedbackOnTouchDown(View v) {
2906 // Provide the same haptic feedback that the system offers for virtual keys.
2907 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2908 }
2909
2910 public View.OnTouchListener getHapticFeedbackTouchListener() {
2911 if (mHapticFeedbackTouchListener == null) {
2912 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2913 @Override
2914 public boolean onTouch(View v, MotionEvent event) {
2915 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2916 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2917 }
2918 return false;
2919 }
2920 };
2921 }
2922 return mHapticFeedbackTouchListener;
2923 }
2924
Adam Cohen9211d422014-10-07 18:14:53 -07002925 public void onDragStarted(View view) {
2926 if (isOnCustomContent()) {
2927 // Custom content screen doesn't participate in drag and drop. If on custom
2928 // content screen, move to default.
2929 moveWorkspaceToDefaultScreen();
2930 }
2931
2932 if (mLauncherCallbacks != null) {
2933 mLauncherCallbacks.onDragStarted(view);
2934 }
2935 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002936
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002937 /**
2938 * Called when the user stops interacting with the launcher.
2939 * This implies that the user is now on the homescreen and is not doing housekeeping.
2940 */
Adam Cohen9211d422014-10-07 18:14:53 -07002941 protected void onInteractionEnd() {
2942 if (mLauncherCallbacks != null) {
2943 mLauncherCallbacks.onInteractionEnd();
2944 }
2945 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002946
2947 /**
2948 * Called when the user starts interacting with the launcher.
2949 * The possible interactions are:
2950 * - open all apps
2951 * - reorder an app shortcut, or a widget
2952 * - open the overview mode.
2953 * This is a good time to stop doing things that only make sense
2954 * when the user is on the homescreen and not doing housekeeping.
2955 */
Adam Cohen9211d422014-10-07 18:14:53 -07002956 protected void onInteractionBegin() {
2957 if (mLauncherCallbacks != null) {
2958 mLauncherCallbacks.onInteractionBegin();
2959 }
2960 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002961
Kenny Guyf07af7b2014-07-31 11:39:16 +01002962 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002963 String packageName = componentName.getPackageName();
Kenny Guyf07af7b2014-07-31 11:39:16 +01002964 try {
2965 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2966 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2967 launcherApps.showAppDetailsForProfile(componentName, user);
2968 } catch (SecurityException e) {
2969 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2970 Log.e(TAG, "Launcher does not have permission to launch settings");
2971 } catch (ActivityNotFoundException e) {
2972 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2973 Log.e(TAG, "Unable to launch settings");
2974 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002975 }
2976
Michael Jurka1e2f4652013-07-08 18:03:46 -07002977 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002978 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2979 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002980 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002981 // System applications cannot be installed. For now, show a toast explaining that.
2982 // We may give them the option of disabling apps this way.
2983 int messageId = R.string.uninstall_system_app_text;
2984 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002985 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002986 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002987 String packageName = componentName.getPackageName();
2988 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002989 Intent intent = new Intent(
2990 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002991 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2992 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01002993 if (user != null) {
2994 user.addToIntent(intent, Intent.EXTRA_USER);
2995 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07002996 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07002997 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002998 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002999 }
3000
Michael Jurka86a720e2012-05-09 11:23:23 -07003001 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01003002 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003003 try {
Winson Chung2672ff92012-05-04 16:22:30 -07003004 // Only launch using the new animation if the shortcut has not opted out (this is a
3005 // private contract between launcher and may be ignored in the future).
3006 boolean useLaunchAnimation = (v != null) &&
3007 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01003008 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
3009 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01003010
Kenny Guy1317e2d2014-05-08 18:52:50 +01003011 UserHandleCompat user = null;
3012 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
3013 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
3014 user = userManager.getUserForSerialNumber(serialNumber);
3015 }
3016
3017 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07003018 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01003019 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07003020 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
3021 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07003022 optsBundle = opts.toBundle();
3023 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01003024
3025 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
3026 // Could be launching some bookkeeping activity
3027 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003028 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003029 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003030 launcherApps.startActivityForProfile(intent.getComponent(), user,
3031 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003032 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003033 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003034 } catch (SecurityException e) {
3035 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003036 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003037 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003038 "or use the exported attribute for this activity. "
3039 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003040 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003041 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003042 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003043
Michael Jurka86a720e2012-05-09 11:23:23 -07003044 boolean startActivitySafely(View v, Intent intent, Object tag) {
3045 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003046 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3047 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3048 return false;
3049 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003050 try {
3051 success = startActivity(v, intent, tag);
3052 } catch (ActivityNotFoundException e) {
3053 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3054 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3055 }
3056 return success;
3057 }
3058
Adam Cohen268c4752012-06-06 17:47:33 -07003059 /**
3060 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3061 * in the DragLayer in the exact absolute location of the original FolderIcon.
3062 */
3063 private void copyFolderIconToImage(FolderIcon fi) {
3064 final int width = fi.getMeasuredWidth();
3065 final int height = fi.getMeasuredHeight();
3066
3067 // Lazy load ImageView, Bitmap and Canvas
3068 if (mFolderIconImageView == null) {
3069 mFolderIconImageView = new ImageView(this);
3070 }
3071 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3072 mFolderIconBitmap.getHeight() != height) {
3073 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3074 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3075 }
3076
3077 DragLayer.LayoutParams lp;
3078 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3079 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3080 } else {
3081 lp = new DragLayer.LayoutParams(width, height);
3082 }
3083
Adam Cohen307fe232012-08-16 17:55:58 -07003084 // The layout from which the folder is being opened may be scaled, adjust the starting
3085 // view size by this scale factor.
3086 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003087 lp.customPosition = true;
3088 lp.x = mRectForFolderAnimation.left;
3089 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003090 lp.width = (int) (scale * width);
3091 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003092
3093 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3094 fi.draw(mFolderIconCanvas);
3095 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003096 if (fi.getFolder() != null) {
3097 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3098 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003099 }
Adam Cohen268c4752012-06-06 17:47:33 -07003100 // Just in case this image view is still in the drag layer from a previous animation,
3101 // we remove it and re-add it.
3102 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3103 mDragLayer.removeView(mFolderIconImageView);
3104 }
3105 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003106 if (fi.getFolder() != null) {
3107 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003108 }
Adam Cohen268c4752012-06-06 17:47:33 -07003109 }
3110
Adam Cohen2801caf2011-05-13 20:57:39 -07003111 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003112 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003113 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3114 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3115 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3116
Adam Cohenc51934b2011-07-26 21:07:43 -07003117 FolderInfo info = (FolderInfo) fi.getTag();
3118 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3119 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003120 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3121 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003122 }
3123
Adam Cohen268c4752012-06-06 17:47:33 -07003124 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3125 copyFolderIconToImage(fi);
3126 fi.setVisibility(View.INVISIBLE);
3127
Michael Jurka2ecf9952012-06-18 12:52:28 -07003128 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003129 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003130 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003131 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3132 }
3133 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003134 oa.start();
3135 }
3136
Adam Cohen268c4752012-06-06 17:47:33 -07003137 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003138 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003139 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3140 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3141 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3142
Adam Cohen268c4752012-06-06 17:47:33 -07003143 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003144
Adam Cohen268c4752012-06-06 17:47:33 -07003145 // We remove and re-draw the FolderIcon in-case it has changed
3146 mDragLayer.removeView(mFolderIconImageView);
3147 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003148 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003149 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003150 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003151 oa.addListener(new AnimatorListenerAdapter() {
3152 @Override
3153 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003154 if (cl != null) {
3155 cl.clearFolderLeaveBehind();
3156 // Remove the ImageView copy of the FolderIcon and make the original visible.
3157 mDragLayer.removeView(mFolderIconImageView);
3158 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003159 }
3160 }
3161 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003162 oa.start();
3163 }
3164
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003165 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003166 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003167 * is animated relative to the specified View. If the View is null, no animation
3168 * is played.
3169 *
3170 * @param folderInfo The FolderInfo describing the folder to open.
3171 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003172 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003173 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003174 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003175
Adam Cohena9cf38f2011-05-02 15:36:58 -07003176 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003177
Adam Cohen4554ee12011-08-03 16:13:21 -07003178 // Just verify that the folder hasn't already been added to the DragLayer.
3179 // There was a one-off crash where the folder had a parent already.
3180 if (folder.getParent() == null) {
3181 mDragLayer.addView(folder);
3182 mDragController.addDropTarget((DropTarget) folder);
3183 } else {
3184 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3185 folder.getParent() + ").");
3186 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003187 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003188 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003189
3190 // Notify the accessibility manager that this folder "window" has appeared and occluded
3191 // the workspace items
3192 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3193 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003194 }
3195
3196 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003197 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003198 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003199 if (folder.isEditingName()) {
3200 folder.dismissEditingName();
3201 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003202 closeFolder(folder);
3203 }
3204 }
3205
3206 void closeFolder(Folder folder) {
3207 folder.getInfo().opened = false;
3208
3209 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3210 if (parent != null) {
3211 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3212 shrinkAndFadeInFolderIcon(fi);
3213 }
3214 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003215
3216 // Notify the accessibility manager that this folder "window" has disappeard and no
3217 // longer occludeds the workspace items
3218 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003219 }
3220
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003221 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003222 if (!isDraggingEnabled()) return false;
3223 if (isWorkspaceLocked()) return false;
3224 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003225
Adam Cohen1697b792013-09-17 19:08:21 -07003226 if (v instanceof Workspace) {
3227 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003228 if (mWorkspace.enterOverviewMode()) {
3229 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3230 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3231 return true;
3232 } else {
3233 return false;
3234 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003235 } else {
3236 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003237 }
Adam Cohen1697b792013-09-17 19:08:21 -07003238 }
3239
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003240 CellLayout.CellInfo longClickCellInfo = null;
3241 View itemUnderLongClick = null;
3242 if (v.getTag() instanceof ItemInfo) {
3243 ItemInfo info = (ItemInfo) v.getTag();
3244 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3245 itemUnderLongClick = longClickCellInfo.cell;
3246 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003247 }
3248
Winson Chung3d503fb2011-07-13 17:25:49 -07003249 // The hotseat touch handling does not go through Workspace, and we always allow long press
3250 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003251 final boolean inHotseat = isHotseatLayout(v);
3252 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003253 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003254 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003255 // User long pressed on empty space
3256 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3257 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003258 if (mWorkspace.isInOverviewMode()) {
3259 mWorkspace.startReordering(v);
3260 } else {
3261 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003262 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003263 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003264 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3265 mHotseat.getOrderInHotseat(
3266 longClickCellInfo.cellX,
3267 longClickCellInfo.cellY));
3268 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003269 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003270 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003271 }
3272 }
3273 }
3274 return true;
3275 }
3276
Winson Chung3d503fb2011-07-13 17:25:49 -07003277 boolean isHotseatLayout(View layout) {
3278 return mHotseat != null && layout != null &&
3279 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3280 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003281
Winson Chung3d503fb2011-07-13 17:25:49 -07003282 /**
3283 * Returns the CellLayout of the specified container at the specified screen.
3284 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003285 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003286 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3287 if (mHotseat != null) {
3288 return mHotseat.getLayout();
3289 } else {
3290 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003291 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003292 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003293 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003294 }
3295 }
3296
Daniel Sandler843e8602010-06-07 14:59:01 -04003297 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003298 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003299 }
3300
Craig Mautner360310b2012-10-26 15:13:08 -07003301 private void setWorkspaceBackground(boolean workspace) {
3302 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003303 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003304 }
3305
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003306 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003307 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3308 int curflags = getWindow().getAttributes().flags
3309 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3310 if (wpflags != curflags) {
3311 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3312 }
Craig Mautner360310b2012-10-26 15:13:08 -07003313 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003314 }
3315
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003316 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3317 if (v instanceof LauncherTransitionable) {
3318 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3319 }
3320 }
3321
Michael Jurkabed61d22012-02-14 22:51:29 -08003322 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3323 if (v instanceof LauncherTransitionable) {
3324 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3325 }
Winson Chung70442722012-02-10 15:43:22 -08003326
3327 // Update the workspace transition step as well
3328 dispatchOnLauncherTransitionStep(v, 0f);
3329 }
3330
3331 private void dispatchOnLauncherTransitionStep(View v, float t) {
3332 if (v instanceof LauncherTransitionable) {
3333 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3334 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003335 }
3336
3337 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3338 if (v instanceof LauncherTransitionable) {
3339 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3340 }
Winson Chung70442722012-02-10 15:43:22 -08003341
3342 // Update the workspace transition step as well
3343 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003344 }
3345
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003346 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003347 * Things to test when changing the following seven functions.
3348 * - Home from workspace
3349 * - from center screen
3350 * - from other screens
3351 * - Home from all apps
3352 * - from center screen
3353 * - from other screens
3354 * - Back from all apps
3355 * - from center screen
3356 * - from other screens
3357 * - Launch app from workspace and quit
3358 * - with back
3359 * - with home
3360 * - Launch app from all apps and quit
3361 * - with back
3362 * - with home
3363 * - Go to a screen that's not the default, then all
3364 * apps, and launch and app, and go back
3365 * - with back
3366 * -with home
3367 * - On workspace, long press power and go back
3368 * - with back
3369 * - with home
3370 * - On all apps, long press power and go back
3371 * - with back
3372 * - with home
3373 * - On workspace, power off
3374 * - On all apps, power off
3375 * - Launch an app and turn off the screen while in that app
3376 * - Go back with home key
3377 * - Go back with back key TODO: make this not go to workspace
3378 * - From all apps
3379 * - From workspace
3380 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3381 * - From all apps
3382 * - From the center workspace
3383 * - From another workspace
3384 */
3385
3386 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003387 * Zoom the camera out from the workspace to reveal 'toView'.
3388 * Assumes that the view to show is anchored at either the very top or very bottom
3389 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003390 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003391 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003392 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3393 showAppsCustomizeHelper(animated, springLoaded, contentType);
3394 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003395
Winson Chungc58497e2013-09-03 17:48:37 -07003396 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3397 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003398 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003399 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003400 mStateAnimation.cancel();
3401 mStateAnimation = null;
3402 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003403
Kenny Guyd794a3f2014-09-16 15:17:58 +01003404 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003405
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003406 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003407
Winson Chungf0ea4d32011-06-06 14:27:16 -07003408 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3409 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003410 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003411 final int itemsAlphaStagger =
3412 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003413
Winson Chungf0ea4d32011-06-06 14:27:16 -07003414 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003415 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003416 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003417
Adam Cohen2854d252014-08-27 16:04:07 -07003418 final ArrayList<View> layerViews = new ArrayList<View>();
3419
Adam Cohen6c5891a2014-07-09 23:53:15 -07003420 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3421 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003422 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003423 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003424 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003425 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3426 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003427 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3428 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003429
Adam Cohena38dc902014-09-07 17:48:55 +02003430 // If for some reason our views aren't initialized, don't animate
3431 boolean initialized = getAllAppsButton() != null;
3432
3433 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003434 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003435 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3436 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003437
Adam Cohen9bfdb762014-07-21 17:44:06 -07003438 final View page = content.getPageAt(content.getCurrentPage());
3439 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003440
Adam Cohen63f1ec02014-08-12 09:23:13 -07003441 final float initialPanelAlpha = 1f;
3442
3443 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3444 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003445 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3446 } else {
3447 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3448 }
3449
Adam Cohen9bfdb762014-07-21 17:44:06 -07003450 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003451 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003452 revealView.setVisibility(View.VISIBLE);
3453 // We need to hide this view as the animation start will be posted.
3454 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003455
Adam Cohen9bfdb762014-07-21 17:44:06 -07003456 int width = revealView.getMeasuredWidth();
3457 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003458 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003459
Adam Cohen63f1ec02014-08-12 09:23:13 -07003460 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003461 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003462
Adam Cohen63f1ec02014-08-12 09:23:13 -07003463 // Get the y delta between the center of the page and the center of the all apps button
3464 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3465 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003466
Adam Cohen2854d252014-08-27 16:04:07 -07003467 float alpha = 0;
3468 float xDrift = 0;
3469 float yDrift = 0;
3470 if (material) {
3471 alpha = isWidgetTray ? 0.3f : 1f;
3472 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3473 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3474 } else {
3475 yDrift = 2 * height / 3;
3476 xDrift = 0;
3477 }
3478 final float initAlpha = alpha;
3479
Adam Cohen9bfdb762014-07-21 17:44:06 -07003480 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003481 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003482 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003483 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003484 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003485 PropertyValuesHolder panelDriftX =
3486 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003487
Adam Cohen2854d252014-08-27 16:04:07 -07003488 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3489 panelAlpha, panelDriftY, panelDriftX);
3490
Adam Cohen9bfdb762014-07-21 17:44:06 -07003491 panelAlphaAndDrift.setDuration(revealDuration);
3492 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003493
Adam Cohen9bfdb762014-07-21 17:44:06 -07003494 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003495
Adam Cohen4e243a22014-08-10 18:30:55 -07003496 if (page != null) {
3497 page.setVisibility(View.VISIBLE);
3498 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003499 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003500
Adam Cohen2854d252014-08-27 16:04:07 -07003501 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3502 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003503 pageDrift.setDuration(revealDuration);
3504 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003505 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003506 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003507
Adam Cohen63f1ec02014-08-12 09:23:13 -07003508 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003509 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003510 itemsAlpha.setDuration(revealDuration);
3511 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3512 itemsAlpha.setStartDelay(itemsAlphaStagger);
3513 mStateAnimation.play(itemsAlpha);
3514 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003515
Adam Cohen4e243a22014-08-10 18:30:55 -07003516 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3517 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003518 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003519 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003520 indicatorsAlpha.setDuration(revealDuration);
3521 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003522
Adam Cohen9bfdb762014-07-21 17:44:06 -07003523 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003524 final View allApps = getAllAppsButton();
3525 int allAppsButtonSize = LauncherAppState.getInstance().
3526 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3527 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003528 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003529 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003530 reveal.setDuration(revealDuration);
3531 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003532
3533 reveal.addListener(new AnimatorListenerAdapter() {
3534 public void onAnimationStart(Animator animation) {
3535 if (!isWidgetTray) {
3536 allApps.setVisibility(View.INVISIBLE);
3537 }
3538 }
3539 public void onAnimationEnd(Animator animation) {
3540 if (!isWidgetTray) {
3541 allApps.setVisibility(View.VISIBLE);
3542 }
3543 }
3544 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003545 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003546 }
Michael Jurka1899a362011-11-03 13:50:45 -07003547
Adam Cohen9bfdb762014-07-21 17:44:06 -07003548 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3549 @Override
3550 public void onAnimationEnd(Animator animation) {
3551 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3552 dispatchOnLauncherTransitionEnd(toView, animated, false);
3553
3554 revealView.setVisibility(View.INVISIBLE);
3555 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003556 if (page != null) {
3557 page.setLayerType(View.LAYER_TYPE_NONE, null);
3558 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003559 content.setPageBackgroundsVisible(true);
3560
3561 // Hide the search bar
3562 if (mSearchDropTargetBar != null) {
3563 mSearchDropTargetBar.hideSearchBar(false);
3564 }
Adam Cohen3f9c9712014-10-31 11:48:25 -07003565
3566 // This can hold unnecessary references to views.
3567 mStateAnimation = null;
Adam Cohen9bfdb762014-07-21 17:44:06 -07003568 }
3569
Adam Cohen9bfdb762014-07-21 17:44:06 -07003570 });
3571
Adam Cohen6c5891a2014-07-09 23:53:15 -07003572 if (workspaceAnim != null) {
3573 mStateAnimation.play(workspaceAnim);
3574 }
Adam Cohen2854d252014-08-27 16:04:07 -07003575
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003576 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3577 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003578 final AnimatorSet stateAnimation = mStateAnimation;
3579 final Runnable startAnimRunnable = new Runnable() {
3580 public void run() {
3581 // Check that mStateAnimation hasn't changed while
3582 // we waited for a layout/draw pass
3583 if (mStateAnimation != stateAnimation)
3584 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003585 dispatchOnLauncherTransitionStart(fromView, animated, false);
3586 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003587
3588 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003589 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003590 for (int i = 0; i < layerViews.size(); i++) {
3591 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003592 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003593 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003594 }
Adam Cohen2854d252014-08-27 16:04:07 -07003595 }
3596 }
3597 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003598 }
3599 };
Adam Cohen2854d252014-08-27 16:04:07 -07003600 toView.bringToFront();
3601 toView.setVisibility(View.VISIBLE);
3602 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003603 } else {
3604 toView.setTranslationX(0.0f);
3605 toView.setTranslationY(0.0f);
3606 toView.setScaleX(1.0f);
3607 toView.setScaleY(1.0f);
3608 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003609 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003610
Daniel Sandlere4f98912013-06-25 15:13:26 -04003611 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003612 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003613 if (mSearchDropTargetBar != null) {
3614 mSearchDropTargetBar.hideSearchBar(false);
3615 }
Michael Jurkaabded662011-03-04 12:06:57 -08003616 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003617 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003618 dispatchOnLauncherTransitionStart(fromView, animated, false);
3619 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003620 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003621 dispatchOnLauncherTransitionStart(toView, animated, false);
3622 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003623 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003624 }
3625
3626 /**
3627 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003628 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003629 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003630 */
Adam Cohened307df2013-10-02 09:37:31 -07003631 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003632 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003633
Michael Jurkab3e22d92011-10-31 15:58:33 -07003634 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003635 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003636 mStateAnimation.cancel();
3637 mStateAnimation = null;
3638 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003639
Kenny Guyd794a3f2014-09-16 15:17:58 +01003640 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003641 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003642
Winson Chungf0ea4d32011-06-06 14:27:16 -07003643 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003644 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003645 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003646 final int itemsAlphaStagger =
3647 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003648
Winson Chungf0ea4d32011-06-06 14:27:16 -07003649 final float scaleFactor = (float)
3650 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3651 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003652 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003653 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003654 final ArrayList<View> layerViews = new ArrayList<View>();
3655
Adam Cohened307df2013-10-02 09:37:31 -07003656 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003657 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003658 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003659 } else if (toState == Workspace.State.SPRING_LOADED ||
3660 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003661 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003662 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003663 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003664
Adam Cohena38dc902014-09-07 17:48:55 +02003665 // If for some reason our views aren't initialized, don't animate
3666 boolean initialized = getAllAppsButton() != null;
3667
3668 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003669 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003670 if (workspaceAnim != null) {
3671 mStateAnimation.play(workspaceAnim);
3672 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003673
Adam Cohen9bfdb762014-07-21 17:44:06 -07003674 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3675 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003676
Adam Cohen9bfdb762014-07-21 17:44:06 -07003677 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003678
3679 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3680 int count = content.getChildCount();
3681 for (int i = 0; i < count; i++) {
3682 View child = content.getChildAt(i);
3683 if (child != page) {
3684 child.setVisibility(View.INVISIBLE);
3685 }
3686 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003687 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003688
Adam Cohen2854d252014-08-27 16:04:07 -07003689 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3690 // don't perform all these no-op animations. In particularly, this was causing
3691 // the all-apps button to pop in and out.
3692 if (fromView.getVisibility() == View.VISIBLE) {
3693 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3694 final boolean isWidgetTray =
3695 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003696
Adam Cohen2854d252014-08-27 16:04:07 -07003697 if (isWidgetTray) {
3698 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3699 } else {
3700 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003701 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003702
Adam Cohen2854d252014-08-27 16:04:07 -07003703 int width = revealView.getMeasuredWidth();
3704 int height = revealView.getMeasuredHeight();
3705 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3706
3707 // Hide the real page background, and swap in the fake one
3708 revealView.setVisibility(View.VISIBLE);
3709 content.setPageBackgroundsVisible(false);
3710
3711 final View allAppsButton = getAllAppsButton();
3712 revealView.setTranslationY(0);
3713 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3714 allAppsButton, null);
3715
3716 float xDrift = 0;
3717 float yDrift = 0;
3718 if (material) {
3719 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3720 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3721 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003722 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003723 xDrift = 0;
3724 }
3725
3726 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3727 TimeInterpolator decelerateInterpolator = material ?
3728 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003729 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003730
3731 // The vertical motion of the apps panel should be delayed by one frame
3732 // from the conceal animation in order to give the right feel. We correpsondingly
3733 // shorten the duration so that the slide and conceal end at the same time.
3734 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3735 0, yDrift);
3736 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3737 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3738 panelDriftY.setInterpolator(decelerateInterpolator);
3739 mStateAnimation.play(panelDriftY);
3740
3741 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3742 0, xDrift);
3743 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3744 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3745 panelDriftX.setInterpolator(decelerateInterpolator);
3746 mStateAnimation.play(panelDriftX);
3747
3748 if (isWidgetTray || !material) {
3749 float finalAlpha = material ? 0.4f : 0f;
3750 revealView.setAlpha(1f);
3751 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3752 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003753 panelAlpha.setDuration(material ? revealDuration : 150);
3754 panelAlpha.setInterpolator(decelerateInterpolator);
3755 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003756 mStateAnimation.play(panelAlpha);
3757 }
3758
3759 if (page != null) {
3760 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3761
3762 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3763 0, yDrift);
3764 page.setTranslationY(0);
3765 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3766 pageDrift.setInterpolator(decelerateInterpolator);
3767 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3768 mStateAnimation.play(pageDrift);
3769
3770 page.setAlpha(1f);
3771 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3772 itemsAlpha.setDuration(100);
3773 itemsAlpha.setInterpolator(decelerateInterpolator);
3774 mStateAnimation.play(itemsAlpha);
3775 }
3776
3777 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3778 pageIndicators.setAlpha(1f);
3779 ObjectAnimator indicatorsAlpha =
3780 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3781 indicatorsAlpha.setDuration(revealDuration);
3782 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3783 mStateAnimation.play(indicatorsAlpha);
3784
3785 width = revealView.getMeasuredWidth();
3786
3787 if (material) {
3788 if (!isWidgetTray) {
3789 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003790 }
Adam Cohen2854d252014-08-27 16:04:07 -07003791 int allAppsButtonSize = LauncherAppState.getInstance().
3792 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3793 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3794 Animator reveal =
3795 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3796 height / 2, revealRadius, finalRadius);
3797 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3798 reveal.setDuration(revealDuration);
3799 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003800
Adam Cohen2854d252014-08-27 16:04:07 -07003801 reveal.addListener(new AnimatorListenerAdapter() {
3802 public void onAnimationEnd(Animator animation) {
3803 revealView.setVisibility(View.INVISIBLE);
3804 if (!isWidgetTray) {
3805 allAppsButton.setVisibility(View.VISIBLE);
3806 }
3807 }
3808 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003809
Adam Cohen2854d252014-08-27 16:04:07 -07003810 mStateAnimation.play(reveal);
3811 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003812
Adam Cohen2854d252014-08-27 16:04:07 -07003813 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3814 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3815 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003816 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003817
3818 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003819 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003820 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003821 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003822 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3823 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003824 if (onCompleteRunnable != null) {
3825 onCompleteRunnable.run();
3826 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003827
3828 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003829 if (page != null) {
3830 page.setLayerType(View.LAYER_TYPE_NONE, null);
3831 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003832 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003833 // Unhide side pages
3834 int count = content.getChildCount();
3835 for (int i = 0; i < count; i++) {
3836 View child = content.getChildAt(i);
3837 child.setVisibility(View.VISIBLE);
3838 }
3839
3840 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003841 if (page != null) {
3842 page.setTranslationX(0);
3843 page.setTranslationY(0);
3844 page.setAlpha(1);
3845 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003846 content.setCurrentPage(content.getNextPage());
3847
Chet Haasebc2f0822012-10-26 17:59:53 -07003848 mAppsCustomizeContent.updateCurrentPageScroll();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003849
3850 // This can hold unnecessary references to views.
3851 mStateAnimation = null;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003852 }
3853 });
3854
Adam Cohen2854d252014-08-27 16:04:07 -07003855 final AnimatorSet stateAnimation = mStateAnimation;
3856 final Runnable startAnimRunnable = new Runnable() {
3857 public void run() {
3858 // Check that mStateAnimation hasn't changed while
3859 // we waited for a layout/draw pass
3860 if (mStateAnimation != stateAnimation)
3861 return;
3862 dispatchOnLauncherTransitionStart(fromView, animated, false);
3863 dispatchOnLauncherTransitionStart(toView, animated, false);
3864
Kenny Guyd794a3f2014-09-16 15:17:58 +01003865 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003866 for (int i = 0; i < layerViews.size(); i++) {
3867 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003868 if (v != null) {
Sunny Goyalfafca522014-11-03 11:30:01 -08003869 if (Utilities.isViewAttachedToWindow(v)) v.buildLayer();
Adam Cohen0f668f32014-09-08 19:54:17 +02003870 }
Adam Cohen2854d252014-08-27 16:04:07 -07003871 }
3872 }
3873 mStateAnimation.start();
3874 }
3875 };
3876 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003877 } else {
3878 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003879 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003880 dispatchOnLauncherTransitionStart(fromView, animated, true);
3881 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003882 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003883 dispatchOnLauncherTransitionStart(toView, animated, true);
3884 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003885 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003886 }
3887
Michael Jurkae326f182011-11-21 14:05:46 -08003888 @Override
3889 public void onTrimMemory(int level) {
3890 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003891 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3892 // The widget preview db can result in holding onto over
3893 // 3MB of memory for caching which isn't necessary.
3894 SQLiteDatabase.releaseMemory();
3895
3896 // We reset the apps customize tray in order to
3897 // to free all the memory associated with widget previews
3898 if (mAppsCustomizeTabHost != null) {
3899 mAppsCustomizeTabHost.reset();
3900 }
Michael Jurkae326f182011-11-21 14:05:46 -08003901 }
3902 }
3903
Adam Cohened307df2013-10-02 09:37:31 -07003904 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003905 showWorkspace(animated, null);
3906 }
3907
Adam Cohened307df2013-10-02 09:37:31 -07003908 protected void showWorkspace() {
3909 showWorkspace(true);
3910 }
3911
Adam Cohened66b2b2012-01-23 17:28:51 -08003912 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003913 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003914 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003915 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003916 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003917
Winson Chungc7d2b602012-05-16 17:02:20 -07003918 // Show the search bar (only animate if we were showing the drop target bar in spring
3919 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003920 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003921 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003922 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003923
Michael Jurkab3e22d92011-10-31 15:58:33 -07003924 // Set focus to the AppsCustomize button
3925 if (mAllAppsButton != null) {
3926 mAllAppsButton.requestFocus();
3927 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003928 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003929
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003930 // Change the state *after* we've called all the transition code
3931 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003932
Winson Chung5fb63472011-02-02 17:03:37 -08003933 // Resume the auto-advance of widgets
3934 mUserPresent = true;
3935 updateRunning();
3936
alanv1d4fde62012-10-17 13:15:47 -07003937 // Send an accessibility event to announce the context change
3938 getWindow().getDecorView()
3939 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003940
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003941 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003942 }
3943
Adam Cohened307df2013-10-02 09:37:31 -07003944 void showOverviewMode(boolean animated) {
3945 mWorkspace.setVisibility(View.VISIBLE);
3946 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3947 mState = State.WORKSPACE;
3948 onWorkspaceShown(animated);
3949 }
3950
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003951 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003952 }
3953
Winson Chung82963d52013-10-09 11:20:57 -07003954 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3955 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003956 if (mState != State.WORKSPACE) return;
3957
Winson Chung82963d52013-10-09 11:20:57 -07003958 if (resetPageToZero) {
3959 mAppsCustomizeTabHost.reset();
3960 }
Winson Chungc58497e2013-09-03 17:48:37 -07003961 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003962 mAppsCustomizeTabHost.post(new Runnable() {
3963 @Override
3964 public void run() {
3965 // We post this in-case the all apps view isn't yet constructed.
3966 mAppsCustomizeTabHost.requestFocus();
3967 }
3968 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003969
Michael Jurkab3e22d92011-10-31 15:58:33 -07003970 // Change the state *after* we've called all the transition code
3971 mState = State.APPS_CUSTOMIZE;
3972
3973 // Pause the auto-advance of widgets until we are out of AllApps
3974 mUserPresent = false;
3975 updateRunning();
3976 closeFolder();
3977
3978 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003979 getWindow().getDecorView()
3980 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003981 }
3982
Adam Cohen7777d962011-08-18 18:58:38 -07003983 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003984 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07003985 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07003986 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07003987 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08003988 }
Adam Cohen7777d962011-08-18 18:58:38 -07003989
Adam Cohenad4e15c2013-10-17 16:21:35 -07003990 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08003991 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07003992 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
3993
Winson Chunge7a03942011-08-05 15:05:12 -07003994 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07003995 @Override
3996 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07003997 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07003998 // Before we show workspace, hide all apps again because
3999 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
4000 // clean up our state transition functions
4001 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08004002 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07004003 } else {
4004 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07004005 }
Winson Chung557d6ed2011-07-08 15:34:52 -07004006 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07004007 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07004008 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07004009
Michael Jurkad3ef3062010-11-23 16:23:58 -08004010 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07004011 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07004012 final boolean animated = true;
4013 final boolean springLoaded = true;
4014 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07004015 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07004016 }
4017 // Otherwise, we are not in spring loaded mode, so don't do anything.
4018 }
4019
Michael Jurkab3e22d92011-10-31 15:58:33 -07004020 void lockAllApps() {
4021 // TODO
4022 }
4023
4024 void unlockAllApps() {
4025 // TODO
4026 }
4027
Winson Chungf0ea4d32011-06-06 14:27:16 -07004028 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07004029 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07004030 */
Winson Chung3d503fb2011-07-13 17:25:49 -07004031 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04004032 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07004033 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08004034 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07004035 int duration = 0;
4036 if (mSearchDropTargetBar != null) {
4037 duration = mSearchDropTargetBar.getTransitionOutDuration();
4038 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08004039 mHotseat.animate().alpha(0f).setDuration(duration);
4040 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004041 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07004042 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07004043 }
Winson Chungb26f3d62011-06-02 10:49:29 -07004044 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08004045 }
4046
Patrick Dubroy5f445422011-02-18 14:35:21 -08004047 /**
4048 * Add an item from all apps or customize onto the given workspace screen.
4049 * If layout is null, add to the current screen.
4050 */
4051 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07004052 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07004053 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07004054 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07004055 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07004056
Winson Chungdff8ebb2011-09-08 17:25:31 -07004057 /** Maps the current orientation to an index for referencing orientation correct global icons */
4058 private int getCurrentOrientationIndexForGlobalIcons() {
4059 // default - 0, landscape - 1
4060 switch (getResources().getConfiguration().orientation) {
4061 case Configuration.ORIENTATION_LANDSCAPE:
4062 return 1;
4063 default:
4064 return 0;
4065 }
4066 }
4067
Michael Jurkaae65ee32012-04-30 11:45:54 -07004068 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07004069 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004070 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07004071 // Look for the toolbar icon specified in the activity meta-data
4072 Bundle metaData = packageManager.getActivityInfo(
4073 activityName, PackageManager.GET_META_DATA).metaData;
4074 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07004075 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004076 if (iconResId != 0) {
4077 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004078 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004079 }
4080 }
4081 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07004082 // This can happen if the activity defines an invalid drawable
4083 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
4084 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01004085 } catch (Resources.NotFoundException nfe) {
4086 // This can happen if the activity defines an invalid drawable
4087 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
4088 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004089 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004090 return null;
4091 }
4092
4093 // if successful in getting icon, return it; otherwise, set button to use default drawable
4094 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004095 int buttonId, ComponentName activityName, int fallbackDrawableId,
4096 String toolbarResourceName) {
4097 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07004098 Resources r = getResources();
4099 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
4100 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004101
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004102 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004103 // If we were unable to find the icon via the meta-data, use a generic one
4104 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07004105 toolbarIcon = r.getDrawable(fallbackDrawableId);
4106 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004107 if (button != null) {
4108 button.setCompoundDrawables(toolbarIcon, null, null, null);
4109 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004110 return null;
4111 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07004112 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004113 if (button != null) {
4114 button.setCompoundDrawables(toolbarIcon, null, null, null);
4115 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004116 return toolbarIcon.getConstantState();
4117 }
4118 }
4119
4120 // if successful in getting icon, return it; otherwise, set button to use default drawable
4121 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004122 int buttonId, ComponentName activityName, int fallbackDrawableId,
4123 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004124 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07004125 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004126
Michael Jurka19e0fc52011-07-22 18:00:21 -07004127 if (button != null) {
4128 // If we were unable to find the icon via the meta-data, use a
4129 // generic one
4130 if (toolbarIcon == null) {
4131 button.setImageResource(fallbackDrawableId);
4132 } else {
4133 button.setImageDrawable(toolbarIcon);
4134 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004135 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07004136
4137 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
4138
Michael Jurka0423dcf2010-10-05 14:56:18 -07004139 }
4140
Winson Chung1b884092012-06-08 17:13:02 -07004141 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004142 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07004143 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004144 }
4145
Michael Jurkae7bf83b2010-12-14 18:02:21 -08004146 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08004147 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08004148 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08004149 }
4150
Winson Chungbb185bd2011-11-21 12:31:42 -08004151 private void invalidatePressedFocusedStates(View container, View button) {
4152 if (container instanceof HolographicLinearLayout) {
4153 HolographicLinearLayout layout = (HolographicLinearLayout) container;
4154 layout.invalidatePressedFocusedStates();
4155 } else if (button instanceof HolographicImageView) {
4156 HolographicImageView view = (HolographicImageView) button;
4157 view.invalidatePressedFocusedStates();
4158 }
4159 }
4160
Cristina Stancu476493b2013-08-07 17:20:14 +01004161 public View getQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07004162 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4163 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07004164 }
4165
Adam Cohen24ce0b32014-01-14 16:18:14 -08004166 if (mQsb == null) {
4167 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
4168 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01004169 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08004170 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01004171 }
4172
4173 protected boolean updateGlobalSearchIcon() {
Adam Cohen9211d422014-10-07 18:14:53 -07004174 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4175 return true;
4176 }
4177
Winson Chungc51db6a2011-10-05 11:44:49 -07004178 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004179 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07004180 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004181 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004182
Winson Chung1cad91e2011-05-25 17:41:01 -07004183 final SearchManager searchManager =
4184 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4185 ComponentName activityName = searchManager.getGlobalSearchActivity();
4186 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004187 int coi = getCurrentOrientationIndexForGlobalIcons();
4188 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004189 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
4190 TOOLBAR_SEARCH_ICON_METADATA_NAME);
4191 if (sGlobalSearchIcon[coi] == null) {
4192 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4193 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
4194 TOOLBAR_ICON_METADATA_NAME);
4195 }
4196
Winson Chungc51db6a2011-10-05 11:44:49 -07004197 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
4198 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08004199 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004200 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004201 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004202 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07004203 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
4204 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004205 if (searchButton != null) searchButton.setVisibility(View.GONE);
4206 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004207 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004208 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004209 }
4210 }
4211
Cristina Stancu476493b2013-08-07 17:20:14 +01004212 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Adam Cohen9211d422014-10-07 18:14:53 -07004213 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) return;
Winson Chungbb185bd2011-11-21 12:31:42 -08004214 final View searchButtonContainer = findViewById(R.id.search_button_container);
4215 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004216 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004217 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004218 }
4219
Cristina Stancu476493b2013-08-07 17:20:14 +01004220 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Adam Cohen9211d422014-10-07 18:14:53 -07004221 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4222 return true;
4223 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004224 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004225 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004226
Winson Chungc51db6a2011-10-05 11:44:49 -07004227 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07004228 final SearchManager searchManager =
4229 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4230 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
4231
4232 ComponentName activityName = null;
4233 if (globalSearchActivity != null) {
4234 // Check if the global search activity handles voice search
4235 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4236 intent.setPackage(globalSearchActivity.getPackageName());
4237 activityName = intent.resolveActivity(getPackageManager());
4238 }
4239
4240 if (activityName == null) {
4241 // Fallback: check if an activity other than the global search activity
4242 // resolves this
4243 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4244 activityName = intent.resolveActivity(getPackageManager());
4245 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004246 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004247 int coi = getCurrentOrientationIndexForGlobalIcons();
4248 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004249 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4250 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
4251 if (sVoiceSearchIcon[coi] == null) {
4252 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4253 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4254 TOOLBAR_ICON_METADATA_NAME);
4255 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004256 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07004257 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07004258 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08004259 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004260 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004261 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07004262 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004263 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004264 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004265 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004266 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07004267 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004268
Cristina Stancu476493b2013-08-07 17:20:14 +01004269 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Adam Cohen9211d422014-10-07 18:14:53 -07004270 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4271 return;
4272 }
4273
Winson Chungbb185bd2011-11-21 12:31:42 -08004274 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
4275 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004276 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004277 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004278 }
4279
Winson Chung5841efa2013-09-30 18:06:44 -07004280 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Adam Cohen9211d422014-10-07 18:14:53 -07004281 if (mLauncherCallbacks != null) {
4282 forceDisableVoiceButtonProxy |= mLauncherCallbacks.forceDisableVoiceButtonProxy();
4283 }
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004284 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
4285 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07004286 boolean visible = !forceDisableVoiceButtonProxy &&
4287 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004288 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004289 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004290 }
4291 }
Winson Chung5841efa2013-09-30 18:06:44 -07004292
4293 /**
4294 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
4295 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
4296 */
4297 public void disableVoiceButtonProxy(boolean disabled) {
4298 updateVoiceButtonProxyVisible(disabled);
4299 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08004300
Michael Jurkad7c28052012-04-27 15:43:36 -07004301 @Override
4302 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004303 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004304 final List<CharSequence> text = event.getText();
4305 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004306 // Populate event with a fake title based on the current state.
4307 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004308 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004309 } else {
4310 text.add(getString(R.string.all_apps_home_button_label));
4311 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004312 return result;
4313 }
4314
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004315 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004316 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004317 */
4318 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4319 @Override
4320 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004321 closeSystemDialogs();
4322 }
4323 }
4324
4325 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004326 * Receives notifications whenever the appwidgets are reset.
4327 */
4328 private class AppWidgetResetObserver extends ContentObserver {
4329 public AppWidgetResetObserver() {
4330 super(new Handler());
4331 }
4332
4333 @Override
4334 public void onChange(boolean selfChange) {
4335 onAppWidgetReset();
4336 }
4337 }
4338
4339 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004340 * If the activity is currently paused, signal that we need to run the passed Runnable
4341 * in onResume.
4342 *
4343 * This needs to be called from incoming places where resources might have been loaded
4344 * while we are paused. That is becaues the Configuration might be wrong
4345 * when we're not running, and if it comes back to what it was when we
4346 * were paused, we are not restarted.
4347 *
4348 * Implementation of the method from LauncherModel.Callbacks.
4349 *
4350 * @return true if we are currently paused. The caller might be able to
4351 * skip some work in that case since we will come back again.
4352 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004353 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004354 if (mPaused) {
4355 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004356 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004357 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004358 }
4359 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004360 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004361 return true;
4362 } else {
4363 return false;
4364 }
4365 }
4366
Michael Jurkac402cd92013-05-20 15:49:32 +02004367 private boolean waitUntilResume(Runnable run) {
4368 return waitUntilResume(run, false);
4369 }
4370
Michael Jurka1e2f4652013-07-08 18:03:46 -07004371 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004372 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004373 }
4374
Michael Jurka7607c2f2013-04-03 14:33:19 -07004375 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004376 * If the activity is currently paused, signal that we need to re-run the loader
4377 * in onResume.
4378 *
4379 * This needs to be called from incoming places where resources might have been loaded
4380 * while we are paused. That is becaues the Configuration might be wrong
4381 * when we're not running, and if it comes back to what it was when we
4382 * were paused, we are not restarted.
4383 *
4384 * Implementation of the method from LauncherModel.Callbacks.
4385 *
4386 * @return true if we are currently paused. The caller might be able to
4387 * skip some work in that case since we will come back again.
4388 */
4389 public boolean setLoadOnResume() {
4390 if (mPaused) {
4391 Log.i(TAG, "setLoadOnResume");
4392 mOnResumeNeedsLoad = true;
4393 return true;
4394 } else {
4395 return false;
4396 }
4397 }
4398
4399 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004400 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004401 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004402 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004403 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004404 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004405 } else {
4406 return SCREEN_COUNT / 2;
4407 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004408 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004409
Joe Onorato9c1289c2009-08-17 11:03:03 -04004410 /**
4411 * Refreshes the shortcuts shown on the workspace.
4412 *
4413 * Implementation of the method from LauncherModel.Callbacks.
4414 */
4415 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004416 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004417
Michael Jurka7607c2f2013-04-03 14:33:19 -07004418 // If we're starting binding all over again, clear any bind calls we'd postponed in
4419 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4420 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004421 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004422
Winson Chungd64d1762013-08-20 14:37:16 -07004423 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004424 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004425 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004426
Michael Jurka05bf644e2011-11-30 20:28:53 -08004427 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004428 if (mHotseat != null) {
4429 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004430 }
4431 }
4432
Adam Cohendcd297f2013-06-18 13:13:40 -07004433 @Override
4434 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004435 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004436
Adam Cohen5084cba2013-09-03 12:01:16 -07004437 // If there are no screens, we need to have an empty screen
4438 if (orderedScreenIds.size() == 0) {
4439 mWorkspace.addExtraEmptyScreen();
4440 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004441
4442 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004443 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004444 if (hasCustomContentToLeft()) {
4445 mWorkspace.createCustomContentContainer();
4446 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004447 }
Winson Chung64359a52013-07-08 17:17:08 -07004448 }
4449
4450 @Override
4451 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004452 // Log to disk
4453 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4454 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4455 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004456 int count = orderedScreenIds.size();
4457 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004458 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004459 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004460 }
4461
Adam Cohen39a06042013-07-19 14:30:12 -07004462 private boolean shouldShowWeightWatcher() {
4463 String spKey = LauncherAppState.getSharedPreferencesKey();
4464 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b8002013-08-15 15:44:26 -07004465 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004466
4467 return show;
4468 }
4469
4470 private void toggleShowWeightWatcher() {
4471 String spKey = LauncherAppState.getSharedPreferencesKey();
4472 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4473 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4474
4475 show = !show;
4476
4477 SharedPreferences.Editor editor = sp.edit();
4478 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4479 editor.commit();
4480
4481 if (mWeightWatcher != null) {
4482 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4483 }
4484 }
4485
Winson Chungd64d1762013-08-20 14:37:16 -07004486 public void bindAppsAdded(final ArrayList<Long> newScreens,
4487 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004488 final ArrayList<ItemInfo> addAnimated,
4489 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004490 Runnable r = new Runnable() {
4491 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004492 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004493 }
4494 };
4495 if (waitUntilResume(r)) {
4496 return;
4497 }
4498
Winson Chungd64d1762013-08-20 14:37:16 -07004499 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004500 if (newScreens != null) {
4501 bindAddScreens(newScreens);
4502 }
Winson Chungd64d1762013-08-20 14:37:16 -07004503
4504 // We add the items without animation on non-visible pages, and with
4505 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004506 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004507 bindItems(addNotAnimated, 0,
4508 addNotAnimated.size(), false);
4509 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004510 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004511 bindItems(addAnimated, 0,
4512 addAnimated.size(), true);
4513 }
Winson Chungc58497e2013-09-03 17:48:37 -07004514
Winson Chung87412982013-10-03 18:34:14 -07004515 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004516 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004517
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004518 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004519 addedApps != null && mAppsCustomizeContent != null) {
4520 mAppsCustomizeContent.addApps(addedApps);
4521 }
Winson Chungd64d1762013-08-20 14:37:16 -07004522 }
4523
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004524 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004525 * Bind the items start-end from the list.
4526 *
4527 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004528 */
Winson Chung64359a52013-07-08 17:17:08 -07004529 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4530 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004531 Runnable r = new Runnable() {
4532 public void run() {
4533 bindItems(shortcuts, start, end, forceAnimateIcons);
4534 }
4535 };
4536 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004537 return;
4538 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004539
Winson Chungf0c6ae02012-03-21 16:10:31 -07004540 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004541 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4542 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004543 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004544 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004545 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004546 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004547 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004548
4549 // Short circuit if we are loading dock items for a configuration which has no dock
4550 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4551 mHotseat == null) {
4552 continue;
4553 }
4554
Joe Onorato9c1289c2009-08-17 11:03:03 -04004555 switch (item.itemType) {
4556 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4557 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004558 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004559 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004560
Winson Chung64359a52013-07-08 17:17:08 -07004561 /*
4562 * TODO: FIX collision case
4563 */
Winson Chungce376632013-07-11 16:12:41 -07004564 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4565 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4566 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004567 View v = cl.getChildAt(item.cellX, item.cellY);
4568 Object tag = v.getTag();
4569 String desc = "Collision while binding workspace item: " + item
4570 + ". Collides with " + tag;
4571 if (LauncherAppState.isDogfoodBuild()) {
4572 throw (new RuntimeException(desc));
4573 } else {
4574 Log.d(TAG, desc);
4575 }
Winson Chungce376632013-07-11 16:12:41 -07004576 }
Winson Chung64359a52013-07-08 17:17:08 -07004577 }
4578
Adam Cohendcd297f2013-06-18 13:13:40 -07004579 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4580 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004581 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004582 // Animate all the applications up now
4583 shortcut.setAlpha(0f);
4584 shortcut.setScaleX(0f);
4585 shortcut.setScaleY(0f);
4586 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004587 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004588 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004589 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004590 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004591 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004592 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004593 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004594 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4595 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004596 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004597 default:
4598 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004599 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004600 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004601
Winson Chung997a9232013-07-24 15:33:46 -07004602 if (animateIcons) {
4603 // Animate to the correct page
4604 if (newShortcutsScreenId > -1) {
4605 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004606 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004607 final Runnable startBounceAnimRunnable = new Runnable() {
4608 public void run() {
4609 anim.playTogether(bounceAnims);
4610 anim.start();
4611 }
4612 };
Winson Chung997a9232013-07-24 15:33:46 -07004613 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004614 // We post the animation slightly delayed to prevent slowdowns
4615 // when we are loading right after we return to launcher.
4616 mWorkspace.postDelayed(new Runnable() {
4617 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004618 if (mWorkspace != null) {
4619 mWorkspace.snapToPage(newScreenIndex);
4620 mWorkspace.postDelayed(startBounceAnimRunnable,
4621 NEW_APPS_ANIMATION_DELAY);
4622 }
Winson Chung94d67682013-09-25 16:29:40 -07004623 }
4624 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004625 } else {
4626 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004627 }
4628 }
Winson Chung64359a52013-07-08 17:17:08 -07004629 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004630 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004631 }
4632
Joe Onorato9c1289c2009-08-17 11:03:03 -04004633 /**
4634 * Implementation of the method from LauncherModel.Callbacks.
4635 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004636 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004637 Runnable r = new Runnable() {
4638 public void run() {
4639 bindFolders(folders);
4640 }
4641 };
4642 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004643 return;
4644 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004645 sFolders.clear();
4646 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004647 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004648
4649 /**
4650 * Add the views for a widget to the workspace.
4651 *
4652 * Implementation of the method from LauncherModel.Callbacks.
4653 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004654 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004655 Runnable r = new Runnable() {
4656 public void run() {
4657 bindAppWidget(item);
4658 }
4659 };
4660 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004661 return;
4662 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004663
Daniel Sandler843e8602010-06-07 14:59:01 -04004664 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4665 if (DEBUG_WIDGETS) {
4666 Log.d(TAG, "bindAppWidget: " + item);
4667 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004668 final Workspace workspace = mWorkspace;
4669
Sunny Goyalff572272014-07-23 13:58:07 -07004670 AppWidgetProviderInfo appWidgetInfo;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004671 if (!mIsSafeModeEnabled
4672 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4673 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004674
4675 appWidgetInfo = mModel.findAppWidgetProviderInfoWithComponent(this, item.providerName);
4676 if (appWidgetInfo == null) {
4677 if (DEBUG_WIDGETS) {
4678 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4679 + " belongs to component " + item.providerName
4680 + ", as the povider is null");
4681 }
4682 LauncherModel.deleteItemFromDatabase(this, item);
4683 return;
4684 }
4685 // Note: This assumes that the id remap broadcast is received before this step.
4686 // If that is not the case, the id remap will be ignored and user may see the
4687 // click to setup view.
4688 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null, null);
4689 pendingInfo.spanX = item.spanX;
4690 pendingInfo.spanY = item.spanY;
4691 pendingInfo.minSpanX = item.minSpanX;
4692 pendingInfo.minSpanY = item.minSpanY;
4693 Bundle options =
4694 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4695
Sunny Goyalff572272014-07-23 13:58:07 -07004696 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004697 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4698 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004699
4700 // TODO consider showing a permission dialog when the widget is clicked.
4701 if (!success) {
4702 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4703 if (DEBUG_WIDGETS) {
4704 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4705 + " belongs to component " + item.providerName
4706 + ", as the launcher is unable to bing a new widget id");
4707 }
4708 LauncherModel.deleteItemFromDatabase(this, item);
4709 return;
4710 }
4711
4712 item.appWidgetId = newWidgetId;
4713
4714 // If the widget has a configure activity, it is still needs to set it up, otherwise
4715 // the widget is ready to go.
4716 item.restoreStatus = (appWidgetInfo.configure == null)
4717 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4718 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4719
4720 LauncherModel.updateItemInDatabase(this, item);
4721 }
4722
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004723 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004724 final int appWidgetId = item.appWidgetId;
4725 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4726 if (DEBUG_WIDGETS) {
4727 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4728 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004729
Sunny Goyal651077b2014-06-30 14:15:31 -07004730 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4731 } else {
4732 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004733 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4734 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004735 view.updateIcon(mIconCache);
4736 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004737 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004738 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004739 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004740
Joe Onorato9c1289c2009-08-17 11:03:03 -04004741 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004742 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004743
Adam Cohendcd297f2013-06-18 13:13:40 -07004744 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004745 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004746 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4747
Joe Onorato9c1289c2009-08-17 11:03:03 -04004748 workspace.requestLayout();
4749
Daniel Sandler843e8602010-06-07 14:59:01 -04004750 if (DEBUG_WIDGETS) {
4751 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4752 + (SystemClock.uptimeMillis()-start) + "ms");
4753 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004754 }
4755
Sunny Goyalff572272014-07-23 13:58:07 -07004756 /**
4757 * Restores a pending widget.
4758 *
4759 * @param appWidgetId The app widget id
4760 * @param cellInfo The position on screen where to create the widget.
4761 */
4762 private void completeRestoreAppWidget(final int appWidgetId) {
4763 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4764 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4765 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4766 return;
4767 }
4768
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004769 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004770 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4771
4772 mWorkspace.reinflateWidgetsIfNecessary();
4773 LauncherModel.updateItemInDatabase(this, info);
4774 }
4775
Adam Cohen1462de32012-07-24 22:34:36 -07004776 public void onPageBoundSynchronously(int page) {
4777 mSynchronouslyBoundPages.add(page);
4778 }
4779
Joe Onorato9c1289c2009-08-17 11:03:03 -04004780 /**
4781 * Callback saying that there aren't any more items to bind.
4782 *
4783 * Implementation of the method from LauncherModel.Callbacks.
4784 */
Adam Cohene25af792013-06-06 23:08:25 -07004785 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004786 Runnable r = new Runnable() {
4787 public void run() {
4788 finishBindingItems(upgradePath);
4789 }
4790 };
4791 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004792 return;
4793 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004794 if (mSavedState != null) {
4795 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004796 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004797 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004798 mSavedState = null;
4799 }
4800
Adam Cohen1462de32012-07-24 22:34:36 -07004801 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004802
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004803 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004804 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004805
4806 // If we received the result of any pending adds while the loader was running (e.g. the
4807 // widget configuration forced an orientation change), process them now.
4808 if (sPendingAddItem != null) {
4809 final long screenId = completeAdd(sPendingAddItem);
4810
4811 // TODO: this moves the user to the page where the pending item was added. Ideally,
4812 // the screen would be guaranteed to exist after bind, and the page would be set through
4813 // the workspace restore process.
4814 mWorkspace.post(new Runnable() {
4815 @Override
4816 public void run() {
4817 mWorkspace.snapToScreenId(screenId);
4818 }
4819 });
4820 sPendingAddItem = null;
4821 }
4822
Adam Cohene25af792013-06-06 23:08:25 -07004823 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004824 mWorkspace.getUniqueComponents(true, null);
4825 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004826 }
Sunny Goyale755d462014-07-22 13:48:29 -07004827 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004828
4829 if (mLauncherCallbacks != null) {
4830 mLauncherCallbacks.finishBindingItems(upgradePath);
4831 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004832 }
4833
Adam Cohen3ed316a2014-07-23 18:21:20 -07004834 private void sendLoadingCompleteBroadcastIfNecessary() {
4835 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4836 String permission =
4837 getResources().getString(R.string.receive_first_load_broadcast_permission);
4838 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4839 sendBroadcast(intent, permission);
4840 SharedPreferences.Editor editor = mSharedPrefs.edit();
4841 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4842 editor.apply();
4843 }
4844 }
4845
Winson Chunga0b7e862013-09-05 16:03:15 -07004846 public boolean isAllAppsButtonRank(int rank) {
4847 if (mHotseat != null) {
4848 return mHotseat.isAllAppsButtonRank(rank);
4849 }
4850 return false;
4851 }
4852
Winson Chunga2413752012-04-03 14:22:34 -07004853 private boolean canRunNewAppsAnimation() {
4854 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4855 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4856 }
4857
Winson Chungc9168342013-06-26 14:54:55 -07004858 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4859 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4860 PropertyValuesHolder.ofFloat("alpha", 1f),
4861 PropertyValuesHolder.ofFloat("scaleX", 1f),
4862 PropertyValuesHolder.ofFloat("scaleY", 1f));
4863 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4864 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4865 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4866 return bounceAnim;
4867 }
4868
Adam Cohen27772732013-11-11 14:00:56 +00004869 public boolean useVerticalBarLayout() {
4870 return LauncherAppState.getInstance().getDynamicGrid().
4871 getDeviceProfile().isVerticalBarLayout();
4872 }
4873
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004874 protected Rect getSearchBarBounds() {
4875 return LauncherAppState.getInstance().getDynamicGrid().
4876 getDeviceProfile().getSearchBarBounds();
4877 }
4878
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004879 @Override
4880 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004881 boolean searchVisible = updateGlobalSearchIcon();
4882 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004883 if (mSearchDropTargetBar != null) {
4884 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4885 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004886 }
4887
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004888 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004889 * Add the icons for all apps.
4890 *
4891 * Implementation of the method from LauncherModel.Callbacks.
4892 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004893 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004894 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004895 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4896 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4897 getHotseat().addAllAppsFolder(mIconCache, apps,
4898 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4899 }
4900 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004901 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004902 if (mAppsCustomizeContent != null) {
4903 mAppsCustomizeContent.onPackagesUpdated(
4904 LauncherModel.getSortedWidgetsAndShortcuts(this));
4905 }
Winson Chungc58497e2013-09-03 17:48:37 -07004906 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004907 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004908 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004909 mAppsCustomizeContent.onPackagesUpdated(
4910 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004911 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004912 }
Adam Cohen9211d422014-10-07 18:14:53 -07004913 if (mLauncherCallbacks != null) {
4914 mLauncherCallbacks.bindAllApplications(apps);
4915 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004916 }
4917
4918 /**
4919 * A package was updated.
4920 *
4921 * Implementation of the method from LauncherModel.Callbacks.
4922 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004923 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004924 Runnable r = new Runnable() {
4925 public void run() {
4926 bindAppsUpdated(apps);
4927 }
4928 };
4929 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004930 return;
4931 }
4932
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004933 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004934 mAppsCustomizeContent != null) {
4935 mAppsCustomizeContent.updateApps(apps);
4936 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004937 }
4938
Sunny Goyal4390ace2014-10-13 11:33:11 -07004939 @Override
4940 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4941 Runnable r = new Runnable() {
4942 public void run() {
4943 bindWidgetsRestored(widgets);
4944 }
4945 };
4946 if (waitUntilResume(r)) {
4947 return;
4948 }
4949 mWorkspace.widgetsRestored(widgets);
4950 }
4951
Joe Onorato9c1289c2009-08-17 11:03:03 -04004952 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004953 * Some shortcuts were updated in the background.
4954 *
4955 * Implementation of the method from LauncherModel.Callbacks.
4956 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004957 @Override
4958 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4959 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004960 Runnable r = new Runnable() {
4961 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004962 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004963 }
4964 };
4965 if (waitUntilResume(r)) {
4966 return;
4967 }
4968
Sunny Goyal4390ace2014-10-13 11:33:11 -07004969 if (!updated.isEmpty()) {
4970 mWorkspace.updateShortcuts(updated);
4971 }
4972
4973 if (!removed.isEmpty()) {
4974 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4975 for (ShortcutInfo si : removed) {
4976 removedComponents.add(si.getTargetComponent());
4977 }
4978 mWorkspace.removeItemsByComponentName(removedComponents, user);
4979 // Notify the drag controller
4980 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004981 }
4982 }
4983
4984 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004985 * Update the state of a package, typically related to install state.
4986 *
4987 * Implementation of the method from LauncherModel.Callbacks.
4988 */
Sunny Goyale755d462014-07-22 13:48:29 -07004989 @Override
4990 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004991 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07004992 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05004993 }
4994 }
4995
4996 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07004997 * Update the label and icon of all the icons in a package
4998 *
4999 * Implementation of the method from LauncherModel.Callbacks.
5000 */
5001 @Override
5002 public void updatePackageBadge(String packageName) {
5003 if (mWorkspace != null) {
5004 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
5005 }
5006 }
5007
5008 /**
Winson Chung83892cc2013-05-01 16:53:33 -07005009 * A package was uninstalled. We take both the super set of packageNames
5010 * in addition to specific applications to remove, the reason being that
5011 * this can be called when a package is updated as well. In that scenario,
5012 * we only remove specific components from the workspace, where as
5013 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04005014 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07005015 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04005016 * Implementation of the method from LauncherModel.Callbacks.
5017 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07005018 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07005019 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07005020 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07005021 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07005022 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07005023 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07005024 }
Winson Chungd64d1762013-08-20 14:37:16 -07005025 };
5026 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07005027 return;
5028 }
5029
Sunny Goyal1a745e82014-10-02 15:58:31 -07005030 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07005031 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
5032 for (AppInfo info : appInfos) {
5033 removedComponents.add(info.componentName);
5034 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07005035 if (!packageNames.isEmpty()) {
5036 mWorkspace.removeItemsByPackageName(packageNames, user);
5037 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07005038 if (!removedComponents.isEmpty()) {
5039 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07005040 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07005041 // Notify the drag controller
5042 mDragController.onAppsRemoved(packageNames, removedComponents);
5043
Sunny Goyal1a745e82014-10-02 15:58:31 -07005044 } else {
5045 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04005046 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07005047
Winson Chungdf95eb12013-10-16 14:57:07 -07005048 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08005049 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07005050 mAppsCustomizeContent != null) {
5051 mAppsCustomizeContent.removeApps(appInfos);
5052 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04005053 }
Joe Onoratobe386092009-11-17 17:32:16 -08005054
5055 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07005056 * A number of packages were updated.
5057 */
Michael Jurkac402cd92013-05-20 15:49:32 +02005058 private ArrayList<Object> mWidgetsAndShortcuts;
5059 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07005060 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02005061 bindPackagesUpdated(mWidgetsAndShortcuts);
5062 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07005063 }
Michael Jurkac402cd92013-05-20 15:49:32 +02005064 };
Michael Jurkac402cd92013-05-20 15:49:32 +02005065 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
5066 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
5067 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07005068 return;
5069 }
5070
Winson Chung64359a52013-07-08 17:17:08 -07005071 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08005072 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02005073 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07005074 }
Winson Chung80baf5a2010-08-09 16:03:15 -07005075 }
5076
Winson Chung400438b2011-01-16 17:53:48 -08005077 private int mapConfigurationOriActivityInfoOri(int configOri) {
5078 final Display d = getWindowManager().getDefaultDisplay();
5079 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
5080 switch (d.getRotation()) {
5081 case Surface.ROTATION_0:
5082 case Surface.ROTATION_180:
5083 // We are currently in the same basic orientation as the natural orientation
5084 naturalOri = configOri;
5085 break;
5086 case Surface.ROTATION_90:
5087 case Surface.ROTATION_270:
5088 // We are currently in the other basic orientation to the natural orientation
5089 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
5090 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
5091 break;
5092 }
5093
5094 int[] oriMap = {
5095 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
5096 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
5097 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
5098 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
5099 };
5100 // Since the map starts at portrait, we need to offset if this device's natural orientation
5101 // is landscape.
5102 int indexOffset = 0;
5103 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
5104 indexOffset = 1;
5105 }
5106 return oriMap[(d.getRotation() + indexOffset) % 4];
5107 }
Adam Cohen446e9402011-09-15 18:21:21 -07005108
Winson Chung4b919f82012-05-01 10:44:08 -07005109 public void lockScreenOrientation() {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08005110 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07005111 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
5112 .getConfiguration().orientation));
5113 }
5114 }
5115 public void unlockScreenOrientation(boolean immediate) {
Sunny Goyal4bbf4192014-11-11 12:23:59 -08005116 if (Utilities.isRotationEnabled(this)) {
Winson Chung4b919f82012-05-01 10:44:08 -07005117 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07005118 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07005119 } else {
5120 mHandler.postDelayed(new Runnable() {
5121 public void run() {
5122 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
5123 }
5124 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07005125 }
Winson Chung4b919f82012-05-01 10:44:08 -07005126 }
Winson Chung400438b2011-01-16 17:53:48 -08005127 }
5128
Winson Chunge43a1e72014-01-15 10:33:02 -08005129 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07005130 if (mLauncherCallbacks != null) {
5131 return mLauncherCallbacks.isLauncherPreinstalled();
5132 }
Winson Chunge43a1e72014-01-15 10:33:02 -08005133 PackageManager pm = getPackageManager();
5134 try {
5135 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
5136 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
5137 return true;
5138 } else {
5139 return false;
5140 }
5141 } catch (NameNotFoundException e) {
5142 e.printStackTrace();
5143 return false;
5144 }
5145 }
5146
Adam Cohenea90f832014-05-21 15:03:34 -07005147 /**
5148 * This method indicates whether or not we should suggest default wallpaper dimensions
5149 * when our wallpaper cropper was not yet used to set a wallpaper.
5150 */
5151 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07005152 if (mLauncherCallbacks != null) {
5153 return mLauncherCallbacks.overrideWallpaperDimensions();
5154 }
Adam Cohenea90f832014-05-21 15:03:34 -07005155 return true;
5156 }
5157
Adam Cohen432609a2014-03-13 17:03:22 -07005158 /**
5159 * To be overridden by subclasses to indicate that there is an activity to launch
5160 * before showing the standard launcher experience.
5161 */
5162 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07005163 if (mLauncherCallbacks != null) {
5164 return mLauncherCallbacks.hasFirstRunActivity();
5165 }
Adam Cohen432609a2014-03-13 17:03:22 -07005166 return false;
5167 }
5168
5169 /**
5170 * To be overridden by subclasses to launch any first run activity
5171 */
5172 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07005173 if (mLauncherCallbacks != null) {
5174 return mLauncherCallbacks.getFirstRunActivity();
5175 }
Adam Cohen432609a2014-03-13 17:03:22 -07005176 return null;
5177 }
5178
Winson Chunga6945242014-01-08 14:04:34 -08005179 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08005180 return !ActivityManager.isRunningInTestHarness() &&
5181 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08005182 }
5183
Adam Cohen4a9423d2014-03-28 14:22:31 -07005184 protected boolean hasRunFirstRunActivity() {
5185 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
5186 }
5187
Adam Cohen432609a2014-03-13 17:03:22 -07005188 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08005189 if (shouldRunFirstRunActivity() &&
5190 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08005191 Intent firstRunIntent = getFirstRunActivity();
5192 if (firstRunIntent != null) {
5193 startActivity(firstRunIntent);
5194 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07005195 return true;
Winson Chunga6945242014-01-08 14:04:34 -08005196 }
5197 }
Adam Cohen432609a2014-03-13 17:03:22 -07005198 return false;
Winson Chunga6945242014-01-08 14:04:34 -08005199 }
5200
5201 private void markFirstRunActivityShown() {
5202 SharedPreferences.Editor editor = mSharedPrefs.edit();
5203 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
5204 editor.apply();
5205 }
5206
Adam Cohen432609a2014-03-13 17:03:22 -07005207 /**
5208 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
5209 * screen that must be displayed and dismissed.
5210 */
5211 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07005212 if (mLauncherCallbacks != null) {
5213 return mLauncherCallbacks.hasDismissableIntroScreen();
5214 }
Adam Cohen432609a2014-03-13 17:03:22 -07005215 return false;
5216 }
5217
5218 /**
5219 * Full screen intro screen to be shown and dismissed before the launcher can be used.
5220 */
5221 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07005222 if (mLauncherCallbacks != null) {
5223 return mLauncherCallbacks.getIntroScreen();
5224 }
Adam Cohen432609a2014-03-13 17:03:22 -07005225 return null;
5226 }
5227
5228 /**
5229 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
5230 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
5231 */
5232 private boolean shouldShowIntroScreen() {
5233 return hasDismissableIntroScreen() &&
5234 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
5235 }
5236
5237 protected void showIntroScreen() {
5238 View introScreen = getIntroScreen();
5239 changeWallpaperVisiblity(false);
5240 if (introScreen != null) {
5241 mDragLayer.showOverlayView(introScreen);
5242 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07005243 if (mLauncherOverlayContainer != null) {
5244 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
5245 }
Adam Cohen432609a2014-03-13 17:03:22 -07005246 }
5247
5248 public void dismissIntroScreen() {
5249 markIntroScreenDismissed();
5250 if (showFirstRunActivity()) {
5251 // We delay hiding the intro view until the first run activity is showing. This
5252 // avoids a blip.
5253 mWorkspace.postDelayed(new Runnable() {
5254 @Override
5255 public void run() {
5256 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07005257 if (mLauncherOverlayContainer != null) {
5258 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
5259 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07005260 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005261 }
5262 }, ACTIVITY_START_DELAY);
5263 } else {
5264 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07005265 if (mLauncherOverlayContainer != null) {
5266 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
5267 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07005268 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005269 }
5270 changeWallpaperVisiblity(true);
5271 }
5272
5273 private void markIntroScreenDismissed() {
5274 SharedPreferences.Editor editor = mSharedPrefs.edit();
5275 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
5276 editor.apply();
5277 }
5278
Sunny Goyal88d60f12014-09-08 03:47:29 -07005279 private void showFirstRunClings() {
5280 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
5281 // on the device, then we always show the first run cling experience (or if there is no
5282 // launcher2). Otherwise, we prompt the user upon started for migration
5283 LauncherClings launcherClings = new LauncherClings(this);
5284 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
5285 if (mModel.canMigrateFromOldLauncherDb(this)) {
5286 launcherClings.showMigrationCling();
5287 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005288 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005289 }
5290 }
5291 }
5292
Winson Chunga6945242014-01-08 14:04:34 -08005293 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005294 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5295 if (mHotseat != null) mHotseat.setAlpha(1f);
5296 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5297 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005298 }
5299
5300 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005301 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5302 if (mHotseat != null) mHotseat.setAlpha(0f);
5303 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5304 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005305 }
5306
Mathew Inwood72fbec12013-11-19 15:45:07 +00005307 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005308 // Called from search suggestion, not supported in other profiles.
5309 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5310 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5311 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5312 myUser);
5313 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005314 return null;
5315 }
Kenny Guyed131872014-04-30 03:02:21 +01005316 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005317 }
5318
5319 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5320 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005321 // Called from search suggestion, not supported in other profiles.
5322 return createShortcutDragInfo(shortcutIntent, caption, icon,
5323 UserHandleCompat.myUserHandle());
5324 }
5325
5326 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5327 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005328 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005329 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005330 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005331 }
5332
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005333 protected void moveWorkspaceToDefaultScreen() {
5334 mWorkspace.moveToDefaultScreen(false);
5335 }
5336
Mathew Inwood72fbec12013-11-19 15:45:07 +00005337 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5338 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005339 mWorkspace.onExternalDragStartedWithItem(dragView);
5340 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005341 }
5342
Anjali Koppalf5d29132014-02-28 13:33:27 -08005343 @Override
5344 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005345 if (mLauncherCallbacks != null) {
5346 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5347 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005348 }
5349
Winson Chung80baf5a2010-08-09 16:03:15 -07005350 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005351 * Prints out out state for debugging.
5352 */
5353 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005354 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005355 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005356 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5357 Log.d(TAG, "mRestoring=" + mRestoring);
5358 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5359 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005360 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005361 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005362
Winson Chung785d2eb2011-04-14 16:08:02 -07005363 if (mAppsCustomizeContent != null) {
5364 mAppsCustomizeContent.dumpState();
5365 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005366 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005367 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005368
5369 @Override
5370 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5371 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005372 synchronized (sDumpLogs) {
5373 writer.println(" ");
5374 writer.println("Debug logs: ");
5375 for (int i = 0; i < sDumpLogs.size(); i++) {
5376 writer.println(" " + sDumpLogs.get(i));
5377 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005378 }
Adam Cohen9211d422014-10-07 18:14:53 -07005379 if (mLauncherCallbacks != null) {
5380 mLauncherCallbacks.dump(prefix, fd, writer, args);
5381 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005382 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005383
5384 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005385 if (DEBUG_DUMP_LOG) {
5386 synchronized (sDumpLogs) {
5387 Log.d(TAG, "");
5388 Log.d(TAG, "*********************");
5389 Log.d(TAG, "Launcher debug logs: ");
5390 for (int i = 0; i < sDumpLogs.size(); i++) {
5391 Log.d(TAG, " " + sDumpLogs.get(i));
5392 }
5393 Log.d(TAG, "*********************");
5394 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005395 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005396 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005397 }
5398
5399 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005400 addDumpLog(tag, log, null, debugLog);
5401 }
5402
5403 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005404 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005405 if (e != null) {
5406 Log.d(tag, log, e);
5407 } else {
5408 Log.d(tag, log);
5409 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005410 }
Winson Chungede41292013-09-19 16:27:36 -07005411 if (DEBUG_DUMP_LOG) {
5412 sDateStamp.setTime(System.currentTimeMillis());
5413 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005414 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5415 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005416 }
Winson Chungede41292013-09-19 16:27:36 -07005417 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005418 }
5419
Winson Chungede41292013-09-19 16:27:36 -07005420 public void dumpLogsToLocalData() {
5421 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005422 new AsyncTask<Void, Void, Void>() {
5423 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005424 boolean success = false;
5425 sDateStamp.setTime(sRunStart);
5426 String FILENAME = sDateStamp.getMonth() + "-"
5427 + sDateStamp.getDay() + "_"
5428 + sDateStamp.getHours() + "-"
5429 + sDateStamp.getMinutes() + "_"
5430 + sDateStamp.getSeconds() + ".txt";
5431
5432 FileOutputStream fos = null;
5433 File outFile = null;
5434 try {
5435 outFile = new File(getFilesDir(), FILENAME);
5436 outFile.createNewFile();
5437 fos = new FileOutputStream(outFile);
5438 } catch (Exception e) {
5439 e.printStackTrace();
5440 }
5441 if (fos != null) {
5442 PrintWriter writer = new PrintWriter(fos);
5443
5444 writer.println(" ");
5445 writer.println("Debug logs: ");
5446 synchronized (sDumpLogs) {
5447 for (int i = 0; i < sDumpLogs.size(); i++) {
5448 writer.println(" " + sDumpLogs.get(i));
5449 }
5450 }
5451 writer.close();
5452 }
5453 try {
5454 if (fos != null) {
5455 fos.close();
5456 success = true;
5457 }
5458 } catch (IOException e) {
5459 e.printStackTrace();
5460 }
Michael Jurka43467462013-11-14 12:03:58 +01005461 return null;
Winson Chungede41292013-09-19 16:27:36 -07005462 }
Michael Jurka43467462013-11-14 12:03:58 +01005463 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005464 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005465 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005466}
Michael Jurka2763be32011-02-24 11:19:57 -08005467
Michael Jurkaabded662011-03-04 12:06:57 -08005468interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005469 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005470 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005471 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005472 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005473 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005474}
Dan Sandlerd5024042014-01-09 15:01:33 -05005475
5476interface DebugIntents {
5477 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5478 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005479}