blob: a2de314d3ba8b15154819eed7212ae97922295b6 [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]
171 static final String FORCE_ENABLE_ROTATION_PROPERTY = "launcher_force_rotate";
172 static final String DUMP_STATE_PROPERTY = "launcher_dump_state";
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800173 static final String DISABLE_ALL_APPS_PROPERTY = "launcher_noallapps";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800174
Winson Chung2672ff92012-05-04 16:22:30 -0700175 // The Intent extra that defines whether to ignore the launch animation
176 static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
Daniel Sandler325dc232013-06-05 22:57:57 -0400177 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
Winson Chung2672ff92012-05-04 16:22:30 -0700178
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800179 // Type: int
180 private static final String RUNTIME_STATE_CURRENT_SCREEN = "launcher.current_screen";
Michael Jurka883f55b2010-10-21 15:47:14 -0700181 // Type: int
182 private static final String RUNTIME_STATE = "launcher.state";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800183 // Type: int
Winson Chung3d503fb2011-07-13 17:25:49 -0700184 private static final String RUNTIME_STATE_PENDING_ADD_CONTAINER = "launcher.add_container";
185 // Type: int
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800186 private static final String RUNTIME_STATE_PENDING_ADD_SCREEN = "launcher.add_screen";
187 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700188 private static final String RUNTIME_STATE_PENDING_ADD_CELL_X = "launcher.add_cell_x";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800189 // Type: int
Adam Cohenfbba09b2011-07-18 21:43:05 -0700190 private static final String RUNTIME_STATE_PENDING_ADD_CELL_Y = "launcher.add_cell_y";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800191 // Type: boolean
192 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME = "launcher.rename_folder";
193 // Type: long
194 private static final String RUNTIME_STATE_PENDING_FOLDER_RENAME_ID = "launcher.rename_folder_id";
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700195 // Type: int
196 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_X = "launcher.add_span_x";
197 // Type: int
198 private static final String RUNTIME_STATE_PENDING_ADD_SPAN_Y = "launcher.add_span_y";
199 // Type: parcelable
200 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_INFO = "launcher.add_widget_info";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000201 // Type: parcelable
Adam Cohen4637b5a2013-11-04 18:21:24 -0800202 private static final String RUNTIME_STATE_PENDING_ADD_WIDGET_ID = "launcher.add_widget_id";
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000203 // Type: int[]
204 private static final String RUNTIME_STATE_VIEW_IDS = "launcher.view_ids";
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800205
Adam Cohen432609a2014-03-13 17:03:22 -0700206 static final String INTRO_SCREEN_DISMISSED = "launcher.intro_screen_dismissed";
Adam Cohenb54a5982014-01-08 15:21:04 -0800207 static final String FIRST_RUN_ACTIVITY_DISPLAYED = "launcher.first_run_activity_displayed";
208
Adam Cohen3ed316a2014-07-23 18:21:20 -0700209 static final String FIRST_LOAD_COMPLETE = "launcher.first_load_complete";
210 static final String ACTION_FIRST_LOAD_COMPLETE =
211 "com.android.launcher3.action.FIRST_LOAD_COMPLETE";
212
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100213 private static final String TOOLBAR_ICON_METADATA_NAME = "com.android.launcher.toolbar_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700214 private static final String TOOLBAR_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100215 "com.android.launcher.toolbar_search_icon";
Michael Jurkaae65ee32012-04-30 11:45:54 -0700216 private static final String TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME =
Bjorn Bringerte441bbc2013-06-06 21:54:20 +0100217 "com.android.launcher.toolbar_voice_search_icon";
Patrick Dubroyceae05d2010-08-30 10:40:53 -0700218
Adam Cohen39a06042013-07-19 14:30:12 -0700219 public static final String SHOW_WEIGHT_WATCHER = "debug.show_mem";
Daniel Sandler6053b802013-08-15 15:44:26 -0700220 public static final boolean SHOW_WEIGHT_WATCHER_DEFAULT = false;
Adam Cohen39a06042013-07-19 14:30:12 -0700221
Winson Chunga6945242014-01-08 14:04:34 -0800222 public static final String USER_HAS_MIGRATED = "launcher.user_migrated_from_old_data";
223
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700224 /** The different states that Launcher can be in. */
Winson Chung4a2afa32012-07-19 14:53:05 -0700225 private enum State { NONE, WORKSPACE, APPS_CUSTOMIZE, APPS_CUSTOMIZE_SPRING_LOADED };
Michael Jurkac0e8fca2010-10-06 16:41:29 -0700226 private State mState = State.WORKSPACE;
227 private AnimatorSet mStateAnimation;
Patrick Dubroy6b509c12010-08-23 15:08:16 -0700228
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700229 private boolean mIsSafeModeEnabled;
230
Adam Cohenc2d6e892014-10-16 09:49:24 -0700231 LauncherOverlayCallbacks mLauncherOverlayCallbacks = new LauncherOverlayCallbacksImpl();
232 LauncherOverlay mLauncherOverlay;
Adam Cohena6d04922014-10-23 17:28:30 -0700233 InsettableFrameLayout mLauncherOverlayContainer;
Adam Cohenc2d6e892014-10-16 09:49:24 -0700234
Joe Onorato9c1289c2009-08-17 11:03:03 -0400235 static final int APPWIDGET_HOST_ID = 1024;
Adam Cohenad4e15c2013-10-17 16:21:35 -0700236 public static final int EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT = 300;
237 private static final int ON_ACTIVITY_RESULT_ANIMATION_DELAY = 500;
Adam Cohen646fdf72014-04-08 16:59:36 -0700238 private static final int ACTIVITY_START_DELAY = 1000;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800239
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800240 private static final Object sLock = new Object();
Mike Cleron3a2b3f22009-11-05 17:17:42 -0800241 private static int sScreen = DEFAULT_SCREEN;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800242
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000243 private HashMap<Integer, Integer> mItemIdToViewId = new HashMap<Integer, Integer>();
244 private static final AtomicInteger sNextGeneratedId = new AtomicInteger(1);
245
Winson Chunga2413752012-04-03 14:22:34 -0700246 // How long to wait before the new-shortcut animation automatically pans the workspace
Winson Chung94d67682013-09-25 16:29:40 -0700247 private static int NEW_APPS_PAGE_MOVE_DELAY = 500;
248 private static int NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS = 5;
Winson Chung64359a52013-07-08 17:17:08 -0700249 private static int NEW_APPS_ANIMATION_DELAY = 500;
Adam Cohen63f1ec02014-08-12 09:23:13 -0700250 private static final int SINGLE_FRAME_DELAY = 16;
Winson Chunga2413752012-04-03 14:22:34 -0700251
Joe Onorato2ca0ae72009-11-10 13:14:13 -0800252 private final BroadcastReceiver mCloseSystemDialogsReceiver
253 = new CloseSystemDialogsIntentReceiver();
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800254 private final ContentObserver mWidgetObserver = new AppWidgetResetObserver();
255
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800256 private LayoutInflater mInflater;
257
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800258 private Workspace mWorkspace;
Craig Mautner360310b2012-10-26 15:13:08 -0700259 private View mLauncherView;
Winson Chunga6945242014-01-08 14:04:34 -0800260 private View mPageIndicators;
Michael Jurkab737ee62011-11-15 15:57:22 -0800261 private DragLayer mDragLayer;
262 private DragController mDragController;
Adam Cohen39a06042013-07-19 14:30:12 -0700263 private View mWeightWatcher;
Romain Guycbb89e42009-06-08 15:52:54 -0700264
Sunny Goyalffe83f12014-08-14 17:39:34 -0700265 private AppWidgetManagerCompat mAppWidgetManager;
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700266 private LauncherAppWidgetHost mAppWidgetHost;
Romain Guycbb89e42009-06-08 15:52:54 -0700267
Michael Jurkac9d95c52011-08-29 14:03:34 -0700268 private ItemInfo mPendingAddInfo = new ItemInfo();
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700269 private AppWidgetProviderInfo mPendingAddWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800270 private int mPendingAddWidgetId = -1;
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700271
Michael Jurka0280c3b2010-09-17 15:00:07 -0700272 private int[] mTmpAddItemCellCoordinates = new int[2];
273
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800274 private FolderInfo mFolderInfo;
275
Winson Chung3d503fb2011-07-13 17:25:49 -0700276 private Hotseat mHotseat;
Jorim Jaggid017f882014-01-14 17:08:48 -0800277 private ViewGroup mOverviewPanel;
Adam Cohenf358a4b2013-07-23 16:47:31 -0700278
Michael Jurka838a4ca2011-02-07 13:33:06 -0800279 private View mAllAppsButton;
Winson Chung3d503fb2011-07-13 17:25:49 -0700280
Winson Chungc51db6a2011-10-05 11:44:49 -0700281 private SearchDropTargetBar mSearchDropTargetBar;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700282 private AppsCustomizeTabHost mAppsCustomizeTabHost;
283 private AppsCustomizePagedView mAppsCustomizeContent;
284 private boolean mAutoAdvanceRunning = false;
Adam Cohen24ce0b32014-01-14 16:18:14 -0800285 private View mQsb;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800286
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800287 private Bundle mSavedState;
Winson Chung4a2afa32012-07-19 14:53:05 -0700288 // We set the state in both onCreate and then onNewIntent in some cases, which causes both
289 // scroll issues (because the workspace may not have been measured yet) and extra work.
290 // Instead, just save the state that we need to restore Launcher to, and commit it in onResume.
291 private State mOnResumeState = State.NONE;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800292
293 private SpannableStringBuilder mDefaultKeySsb = null;
294
Joe Onorato9c1289c2009-08-17 11:03:03 -0400295 private boolean mWorkspaceLoading = true;
296
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800297 private boolean mPaused = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800298 private boolean mRestoring;
299 private boolean mWaitingForResult;
Joe Onoratoef2efcf2010-10-27 13:21:00 -0700300 private boolean mOnResumeNeedsLoad;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800301
Michael Jurka1e2f4652013-07-08 18:03:46 -0700302 private ArrayList<Runnable> mBindOnResumeCallbacks = new ArrayList<Runnable>();
Michael Jurka7607c2f2013-04-03 14:33:19 -0700303 private ArrayList<Runnable> mOnResumeCallbacks = new ArrayList<Runnable>();
304
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800305 private Bundle mSavedInstanceState;
306
Joe Onorato9c1289c2009-08-17 11:03:03 -0400307 private LauncherModel mModel;
Joe Onorato0589f0f2010-02-08 13:44:00 -0800308 private IconCache mIconCache;
Adam Cohend113e0c2010-11-11 10:48:05 -0800309 private boolean mUserPresent = true;
310 private boolean mVisible = false;
Adam Cohened307df2013-10-02 09:37:31 -0700311 private boolean mHasFocus = false;
Adam Cohend113e0c2010-11-11 10:48:05 -0800312 private boolean mAttached = false;
Joe Onorato9c1289c2009-08-17 11:03:03 -0400313
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700314 private static LocaleConfiguration sLocaleConfiguration = null;
315
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700316 private static HashMap<Long, FolderInfo> sFolders = new HashMap<Long, FolderInfo>();
Romain Guycbb89e42009-06-08 15:52:54 -0700317
Adam Cohen61f560d2013-09-30 15:58:20 -0700318 private View.OnTouchListener mHapticFeedbackTouchListener;
319
Adam Cohended9f8d2010-11-03 13:25:16 -0700320 // Related to the auto-advancing of widgets
321 private final int ADVANCE_MSG = 1;
322 private final int mAdvanceInterval = 20000;
323 private final int mAdvanceStagger = 250;
324 private long mAutoAdvanceSentTime;
325 private long mAutoAdvanceTimeLeft = -1;
326 private HashMap<View, AppWidgetProviderInfo> mWidgetsToAdvance =
327 new HashMap<View, AppWidgetProviderInfo>();
328
Winson Chung400438b2011-01-16 17:53:48 -0800329 // Determines how long to wait after a rotation before restoring the screen orientation to
330 // match the sensor state.
331 private final int mRestoreScreenOrientationDelay = 500;
332
Michael Jurka4ef207b2010-11-29 17:05:45 -0800333 // External icons saved in case of resource changes, orientation, etc.
Winson Chungdff8ebb2011-09-08 17:25:31 -0700334 private static Drawable.ConstantState[] sGlobalSearchIcon = new Drawable.ConstantState[2];
335 private static Drawable.ConstantState[] sVoiceSearchIcon = new Drawable.ConstantState[2];
Winson Chungd64a6662013-09-30 11:06:59 -0700336
Craig Mautner360310b2012-10-26 15:13:08 -0700337 private Drawable mWorkspaceBackgroundDrawable;
Craig Mautner360310b2012-10-26 15:13:08 -0700338
Adam Cohen1462de32012-07-24 22:34:36 -0700339 private final ArrayList<Integer> mSynchronouslyBoundPages = new ArrayList<Integer>();
Winson Chung6e1c0d32013-10-25 15:24:24 -0700340 private static final boolean DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE = false;
Adam Cohen1462de32012-07-24 22:34:36 -0700341
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700342 static final ArrayList<String> sDumpLogs = new ArrayList<String>();
Adam Cohen4caf2982013-08-20 18:54:31 -0700343 static Date sDateStamp = new Date();
344 static DateFormat sDateFormat =
345 DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT);
346 static long sRunStart = System.currentTimeMillis();
347 static final String CORRUPTION_EMAIL_SENT_KEY = "corruptionEmailSent";
Adam Cohen16d7ffc2011-10-05 17:49:14 -0700348
Winson Chung46353de2012-02-16 14:05:10 -0800349 // We only want to get the SharedPreferences once since it does an FS stat each time we get
350 // it from the context.
351 private SharedPreferences mSharedPrefs;
352
Adam Cohene25af792013-06-06 23:08:25 -0700353 private static ArrayList<ComponentName> mIntentsOnWorkspaceFromUpgradePath = null;
354
Winson Chungf0c6ae02012-03-21 16:10:31 -0700355 // Holds the page that we need to animate to, and the icon views that we need to animate up
356 // when we scroll to that page on resume.
Adam Cohen268c4752012-06-06 17:47:33 -0700357 private ImageView mFolderIconImageView;
358 private Bitmap mFolderIconBitmap;
359 private Canvas mFolderIconCanvas;
360 private Rect mRectForFolderAnimation = new Rect();
Adam Cohen2801caf2011-05-13 20:57:39 -0700361
Michael Jurkaddd62e92011-02-16 17:49:14 -0800362 private BubbleTextView mWaitingForResume;
363
Michael Jurkac1f5d262011-09-30 19:32:27 -0700364 private Runnable mBuildLayersRunnable = new Runnable() {
365 public void run() {
Michael Jurka9d906c72011-10-14 06:25:36 -0700366 if (mWorkspace != null) {
367 mWorkspace.buildPageHardwareLayers();
368 }
Michael Jurkac1f5d262011-09-30 19:32:27 -0700369 }
370 };
371
Adam Cohendb364c32014-05-20 14:23:40 -0700372 private static PendingAddArguments sPendingAddItem;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800373
Michael Jurka7267fa52013-09-26 15:29:57 -0700374 public static boolean sForceEnableRotation = isPropertyEnabled(FORCE_ENABLE_ROTATION_PROPERTY);
Michael Jurka0a457bf2012-11-19 14:05:05 -0800375
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800376 private static class PendingAddArguments {
377 int requestCode;
378 Intent intent;
Winson Chung3d503fb2011-07-13 17:25:49 -0700379 long container;
Adam Cohendcd297f2013-06-18 13:13:40 -0700380 long screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800381 int cellX;
382 int cellY;
Adam Cohendb364c32014-05-20 14:23:40 -0700383 int appWidgetId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800384 }
385
Daniel Sandlerff02d492013-08-05 02:12:05 -0400386 private Stats mStats;
387
Sunny Goyaldcbcc862014-08-12 15:58:36 -0700388 FocusIndicatorView mFocusHandler;
389
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -0800390 static boolean isPropertyEnabled(String propertyName) {
Michael Jurka0a457bf2012-11-19 14:05:05 -0800391 return Log.isLoggable(propertyName, Log.VERBOSE);
Michael Jurka9bc8eba2012-05-21 20:36:44 -0700392 }
393
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800394 @Override
395 protected void onCreate(Bundle savedInstanceState) {
Winson Chunga2413752012-04-03 14:22:34 -0700396 if (DEBUG_STRICT_MODE) {
397 StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
398 .detectDiskReads()
399 .detectDiskWrites()
400 .detectNetwork() // or .detectAll() for all detectable problems
401 .penaltyLog()
402 .build());
403 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
404 .detectLeakedSqlLiteObjects()
405 .detectLeakedClosableObjects()
406 .penaltyLog()
407 .penaltyDeath()
408 .build());
409 }
410
Adam Cohen9211d422014-10-07 18:14:53 -0700411 if (mLauncherCallbacks != null) {
412 mLauncherCallbacks.preOnCreate();
413 }
414
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800415 super.onCreate(savedInstanceState);
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400416
Daniel Sandlere060b0b2013-06-27 21:47:55 -0400417 LauncherAppState.setApplicationContext(getApplicationContext());
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400418 LauncherAppState app = LauncherAppState.getInstance();
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700419 LauncherAppState.getLauncherProvider().setLauncherProviderChangeListener(this);
Winson Chung6e1c0d32013-10-25 15:24:24 -0700420
Winson Chung5f8afe62013-08-12 16:19:28 -0700421 // Lazy-initialize the dynamic grid
Sunny Goyal33d44382014-10-16 09:24:19 -0700422 DeviceProfile grid = app.initDynamicGrid(this);
Winson Chung5f8afe62013-08-12 16:19:28 -0700423
424 // the LauncherApplication should call this, but in case of Instrumentation it might not be present yet
Daniel Sandlercc8befa2013-06-11 14:45:48 -0400425 mSharedPrefs = getSharedPreferences(LauncherAppState.getSharedPreferencesKey(),
Winson Chungf0c6ae02012-03-21 16:10:31 -0700426 Context.MODE_PRIVATE);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -0700427 mIsSafeModeEnabled = getPackageManager().isSafeMode();
Joe Onorato0589f0f2010-02-08 13:44:00 -0800428 mModel = app.setLauncher(this);
429 mIconCache = app.getIconCache();
Winson Chunge5467dc2013-10-14 17:03:04 -0700430 mIconCache.flushInvalidIcons(grid);
Joe Onorato41a12d22009-10-31 18:30:00 -0400431 mDragController = new DragController(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800432 mInflater = getLayoutInflater();
Romain Guycbb89e42009-06-08 15:52:54 -0700433
Daniel Sandlerff02d492013-08-05 02:12:05 -0400434 mStats = new Stats(this);
435
Sunny Goyalffe83f12014-08-14 17:39:34 -0700436 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this);
Adam Cohen53805212013-10-01 10:39:23 -0700437
The Android Open Source Project7376fae2009-03-11 12:11:58 -0700438 mAppWidgetHost = new LauncherAppWidgetHost(this, APPWIDGET_HOST_ID);
439 mAppWidgetHost.startListening();
Romain Guycbb89e42009-06-08 15:52:54 -0700440
Winson Chungb8b2a5a2012-07-12 17:55:31 -0700441 // If we are getting an onCreate, we can actually preempt onResume and unset mPaused here,
442 // this also ensures that any synchronous binding below doesn't re-trigger another
443 // LauncherModel load.
444 mPaused = false;
445
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800446 if (PROFILE_STARTUP) {
Christian Mehlmauer0fbe7bc2010-08-02 20:27:46 +0200447 android.os.Debug.startMethodTracing(
448 Environment.getExternalStorageDirectory() + "/launcher");
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800449 }
450
451 checkForLocaleChange();
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800452 setContentView(R.layout.launcher);
Michael Jurka7267fa52013-09-26 15:29:57 -0700453
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800454 setupViews();
Cristina Stancu476493b2013-08-07 17:20:14 +0100455 grid.layout(this);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800456
Jeff Sharkey1e2efc82009-11-06 15:17:59 -0800457 registerContentObservers();
458
Joe Onorato7c312c12009-08-13 21:36:53 -0700459 lockAllApps();
Joe Onorato7404ee42009-07-31 11:54:44 -0700460
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800461 mSavedState = savedInstanceState;
462 restoreState(mSavedState);
463
464 if (PROFILE_STARTUP) {
465 android.os.Debug.stopMethodTracing();
466 }
467
468 if (!mRestoring) {
Adam Cohendb78dc82014-06-19 14:50:51 -0700469 if (DISABLE_SYNCHRONOUS_BINDING_CURRENT_PAGE) {
Winson Chung4a2afa32012-07-19 14:53:05 -0700470 // If the user leaves launcher, then we should just load items asynchronously when
471 // they return.
Derek Prothro7aff3992013-12-10 14:00:37 -0500472 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Winson Chung4a2afa32012-07-19 14:53:05 -0700473 } else {
474 // We only load the page synchronously if the user rotates (or triggers a
475 // configuration change) while launcher is in the foreground
Winson Chung9b9fb962013-11-15 15:39:34 -0800476 mModel.startLoader(true, mWorkspace.getRestorePage());
Winson Chung4a2afa32012-07-19 14:53:05 -0700477 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800478 }
479
480 // For handling default keys
481 mDefaultKeySsb = new SpannableStringBuilder();
482 Selection.setSelection(mDefaultKeySsb, 0);
Joe Onorato34a0e1b2009-12-14 17:44:51 -0800483
484 IntentFilter filter = new IntentFilter(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
485 registerReceiver(mCloseSystemDialogsReceiver, filter);
Michael Jurka4ef207b2010-11-29 17:05:45 -0800486
Adam Cohenf9426d52012-06-04 17:26:21 -0700487 updateGlobalIcons();
488
489 // On large interfaces, we want the screen to auto-rotate based on the current orientation
490 unlockScreenOrientation(true);
Winson Chungaf40f202013-09-18 18:26:31 -0700491
Adam Cohen9211d422014-10-07 18:14:53 -0700492 if (mLauncherCallbacks != null) {
493 mLauncherCallbacks.onCreate(savedInstanceState);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700494 if (mLauncherCallbacks.hasLauncherOverlay()) {
495 ViewStub stub = (ViewStub) findViewById(R.id.launcher_overlay_stub);
Adam Cohena6d04922014-10-23 17:28:30 -0700496 mLauncherOverlayContainer = (InsettableFrameLayout) stub.inflate();
497 mLauncherOverlay = mLauncherCallbacks.setLauncherOverlayView(
498 mLauncherOverlayContainer, mLauncherOverlayCallbacks);
Adam Cohenc2d6e892014-10-16 09:49:24 -0700499 mWorkspace.setLauncherOverlay(mLauncherOverlay);
500 }
Adam Cohen9211d422014-10-07 18:14:53 -0700501 }
Adam Cohenc3e12c72014-10-31 16:15:36 -0700502
503 if (shouldShowIntroScreen()) {
504 showIntroScreen();
505 } else {
506 showFirstRunActivity();
507 showFirstRunClings();
508 }
Adam Cohen9211d422014-10-07 18:14:53 -0700509 }
510
511 private LauncherCallbacks mLauncherCallbacks;
512
513 public void onPostCreate(Bundle savedInstanceState) {
514 super.onPostCreate(savedInstanceState);
515 if (mLauncherCallbacks != null) {
516 mLauncherCallbacks.onPostCreate(savedInstanceState);
517 }
518 }
519
520 public boolean setLauncherCallbacks(LauncherCallbacks callbacks) {
521 mLauncherCallbacks = callbacks;
522 return true;
Adam Cohenf9426d52012-06-04 17:26:21 -0700523 }
524
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700525 @Override
Adam Cohen9211d422014-10-07 18:14:53 -0700526 public void onLauncherProviderChange() {
527 if (mLauncherCallbacks != null) {
528 mLauncherCallbacks.onLauncherProviderChange();
529 }
530 }
Anjali Koppal67e7cae2014-03-13 12:14:12 -0700531
Adam Cohen9211d422014-10-07 18:14:53 -0700532 /** To be overridden by subclasses to hint to Launcher that we have custom content */
Winson Chung98ca0f72013-07-29 12:58:51 -0700533 protected boolean hasCustomContentToLeft() {
Adam Cohen9211d422014-10-07 18:14:53 -0700534 if (mLauncherCallbacks != null) {
535 return mLauncherCallbacks.hasCustomContentToLeft();
536 }
Winson Chung98ca0f72013-07-29 12:58:51 -0700537 return false;
538 }
539
Dave Hawkeya8881582013-09-17 15:55:33 -0600540 /**
Derek Prothrodadd9842014-01-17 13:43:50 -0500541 * To be overridden by subclasses to populate the custom content container and call
Dave Hawkeya8881582013-09-17 15:55:33 -0600542 * {@link #addToCustomContentPage}. This will only be invoked if
543 * {@link #hasCustomContentToLeft()} is {@code true}.
544 */
Derek Prothrodadd9842014-01-17 13:43:50 -0500545 protected void populateCustomContentContainer() {
Adam Cohen9211d422014-10-07 18:14:53 -0700546 if (mLauncherCallbacks != null) {
547 mLauncherCallbacks.populateCustomContentContainer();
548 }
Dave Hawkeya8881582013-09-17 15:55:33 -0600549 }
550
551 /**
552 * Invoked by subclasses to signal a change to the {@link #addCustomContentToLeft} value to
553 * ensure the custom content page is added or removed if necessary.
554 */
555 protected void invalidateHasCustomContentToLeft() {
Dave Hawkey3a733a72013-09-30 11:52:21 -0600556 if (mWorkspace == null || mWorkspace.getScreenOrder().isEmpty()) {
Dave Hawkeya8881582013-09-17 15:55:33 -0600557 // Not bound yet, wait for bindScreens to be called.
558 return;
559 }
560
561 if (!mWorkspace.hasCustomContent() && hasCustomContentToLeft()) {
562 // Create the custom content page and call the subclass to populate it.
Derek Prothrodadd9842014-01-17 13:43:50 -0500563 mWorkspace.createCustomContentContainer();
564 populateCustomContentContainer();
Dave Hawkeya8881582013-09-17 15:55:33 -0600565 } else if (mWorkspace.hasCustomContent() && !hasCustomContentToLeft()) {
566 mWorkspace.removeCustomContentPage();
567 }
568 }
569
Adam Cohenf9426d52012-06-04 17:26:21 -0700570 private void updateGlobalIcons() {
Winson Chungc51db6a2011-10-05 11:44:49 -0700571 boolean searchVisible = false;
572 boolean voiceVisible = false;
Michael Jurka4ef207b2010-11-29 17:05:45 -0800573 // If we have a saved version of these external icons, we load them up immediately
Winson Chungdff8ebb2011-09-08 17:25:31 -0700574 int coi = getCurrentOrientationIndexForGlobalIcons();
Adam Cohena00673c2014-08-14 12:57:28 -0700575 if (sGlobalSearchIcon[coi] == null || sVoiceSearchIcon[coi] == null) {
Winson Chungc51db6a2011-10-05 11:44:49 -0700576 searchVisible = updateGlobalSearchIcon();
577 voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungf0ea4d32011-06-06 14:27:16 -0700578 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700579 if (sGlobalSearchIcon[coi] != null) {
580 updateGlobalSearchIcon(sGlobalSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700581 searchVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700582 }
Winson Chungdff8ebb2011-09-08 17:25:31 -0700583 if (sVoiceSearchIcon[coi] != null) {
584 updateVoiceSearchIcon(sVoiceSearchIcon[coi]);
Winson Chungc51db6a2011-10-05 11:44:49 -0700585 voiceVisible = true;
Winson Chungf0ea4d32011-06-06 14:27:16 -0700586 }
Winson Chungadf0c182012-08-23 14:59:07 -0700587 if (mSearchDropTargetBar != null) {
588 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
589 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800590 }
Romain Guycbb89e42009-06-08 15:52:54 -0700591
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800592 private void checkForLocaleChange() {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700593 if (sLocaleConfiguration == null) {
594 new AsyncTask<Void, Void, LocaleConfiguration>() {
595 @Override
596 protected LocaleConfiguration doInBackground(Void... unused) {
597 LocaleConfiguration localeConfiguration = new LocaleConfiguration();
598 readConfiguration(Launcher.this, localeConfiguration);
599 return localeConfiguration;
600 }
601
602 @Override
603 protected void onPostExecute(LocaleConfiguration result) {
604 sLocaleConfiguration = result;
605 checkForLocaleChange(); // recursive, but now with a locale configuration
606 }
607 }.execute();
608 return;
609 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700610
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800611 final Configuration configuration = getResources().getConfiguration();
612
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700613 final String previousLocale = sLocaleConfiguration.locale;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800614 final String locale = configuration.locale.toString();
615
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700616 final int previousMcc = sLocaleConfiguration.mcc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800617 final int mcc = configuration.mcc;
618
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700619 final int previousMnc = sLocaleConfiguration.mnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800620 final int mnc = configuration.mnc;
621
Romain Guy84f296c2009-11-04 15:00:44 -0800622 boolean localeChanged = !locale.equals(previousLocale) || mcc != previousMcc || mnc != previousMnc;
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800623
Romain Guy84f296c2009-11-04 15:00:44 -0800624 if (localeChanged) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700625 sLocaleConfiguration.locale = locale;
626 sLocaleConfiguration.mcc = mcc;
627 sLocaleConfiguration.mnc = mnc;
Romain Guy98d01652009-06-30 16:21:04 -0700628
Joe Onorato0589f0f2010-02-08 13:44:00 -0800629 mIconCache.flush();
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700630
631 final LocaleConfiguration localeConfiguration = sLocaleConfiguration;
Michael Jurka43467462013-11-14 12:03:58 +0100632 new AsyncTask<Void, Void, Void>() {
633 public Void doInBackground(Void ... args) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700634 writeConfiguration(Launcher.this, localeConfiguration);
Michael Jurka43467462013-11-14 12:03:58 +0100635 return null;
Brad Fitzpatrick319226a2010-09-01 13:45:16 -0700636 }
Michael Jurka43467462013-11-14 12:03:58 +0100637 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Romain Guy98d01652009-06-30 16:21:04 -0700638 }
639 }
640
641 private static class LocaleConfiguration {
642 public String locale;
643 public int mcc = -1;
644 public int mnc = -1;
645 }
Jason Samsfd22dac2009-09-20 17:24:16 -0700646
Romain Guy98d01652009-06-30 16:21:04 -0700647 private static void readConfiguration(Context context, LocaleConfiguration configuration) {
648 DataInputStream in = null;
649 try {
Helena Josol28db2802014-10-09 17:04:09 +0100650 in = new DataInputStream(context.openFileInput(LauncherFiles.LAUNCHER_PREFERENCES));
Romain Guy98d01652009-06-30 16:21:04 -0700651 configuration.locale = in.readUTF();
652 configuration.mcc = in.readInt();
653 configuration.mnc = in.readInt();
654 } catch (FileNotFoundException e) {
655 // Ignore
656 } catch (IOException e) {
657 // Ignore
658 } finally {
659 if (in != null) {
660 try {
661 in.close();
662 } catch (IOException e) {
663 // Ignore
664 }
665 }
666 }
667 }
668
669 private static void writeConfiguration(Context context, LocaleConfiguration configuration) {
670 DataOutputStream out = null;
671 try {
Helena Josol4fbbb3e2014-10-06 16:06:46 +0100672 out = new DataOutputStream(context.openFileOutput(
Helena Josol28db2802014-10-09 17:04:09 +0100673 LauncherFiles.LAUNCHER_PREFERENCES, MODE_PRIVATE));
Romain Guy98d01652009-06-30 16:21:04 -0700674 out.writeUTF(configuration.locale);
675 out.writeInt(configuration.mcc);
676 out.writeInt(configuration.mnc);
677 out.flush();
678 } catch (FileNotFoundException e) {
679 // Ignore
680 } catch (IOException e) {
681 //noinspection ResultOfMethodCallIgnored
Helena Josol28db2802014-10-09 17:04:09 +0100682 context.getFileStreamPath(LauncherFiles.LAUNCHER_PREFERENCES).delete();
Romain Guy98d01652009-06-30 16:21:04 -0700683 } finally {
684 if (out != null) {
685 try {
686 out.close();
687 } catch (IOException e) {
688 // Ignore
689 }
690 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800691 }
692 }
693
Anton Hanssona2f665f2013-09-26 12:18:32 +0100694 public Stats getStats() {
695 return mStats;
696 }
697
Bjorn Bringertc459e522013-06-07 19:36:01 +0100698 public LayoutInflater getInflater() {
699 return mInflater;
700 }
701
Winson Chung36a62fe2012-05-06 18:04:42 -0700702 boolean isDraggingEnabled() {
703 // We prevent dragging when we are loading the workspace as it is possible to pick up a view
704 // that is subsequently removed from the workspace in startBinding().
705 return !mModel.isLoadingWorkspace();
706 }
707
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800708 static int getScreen() {
709 synchronized (sLock) {
710 return sScreen;
711 }
712 }
713
714 static void setScreen(int screen) {
715 synchronized (sLock) {
716 sScreen = screen;
717 }
718 }
719
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000720 public static int generateViewId() {
Ian Parkinson0c2918f2014-10-06 14:24:31 +0100721 if (Build.VERSION.SDK_INT >= 17) {
722 return View.generateViewId();
723 } else {
724 // View.generateViewId() is not available. The following fallback logic is a copy
725 // of its implementation.
726 for (;;) {
727 final int result = sNextGeneratedId.get();
728 // aapt-generated IDs have the high byte nonzero; clamp to the range under that.
729 int newValue = result + 1;
730 if (newValue > 0x00FFFFFF) newValue = 1; // Roll over to 1, not 0.
731 if (sNextGeneratedId.compareAndSet(result, newValue)) {
732 return result;
733 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +0000734 }
735 }
736 }
737
738 public int getViewIdForItem(ItemInfo info) {
739 // This cast is safe given the > 2B range for int.
740 int itemId = (int) info.id;
741 if (mItemIdToViewId.containsKey(itemId)) {
742 return mItemIdToViewId.get(itemId);
743 }
744 int viewId = generateViewId();
745 mItemIdToViewId.put(itemId, viewId);
746 return viewId;
747 }
748
749 /**
Winson Chung557d6ed2011-07-08 15:34:52 -0700750 * Returns whether we should delay spring loaded mode -- for shortcuts and widgets that have
751 * a configuration step, this allows the proper animations to run after other transitions.
752 */
Adam Cohendb364c32014-05-20 14:23:40 -0700753 private long completeAdd(PendingAddArguments args) {
Adam Cohen83079e42014-09-19 17:43:08 -0700754 long screenId = args.screenId;
755 if (args.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
756 // When the screen id represents an actual screen (as opposed to a rank) we make sure
757 // that the drop page actually exists.
758 screenId = ensurePendingDropLayoutExists(args.screenId);
759 }
Adam Cohendb364c32014-05-20 14:23:40 -0700760
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800761 switch (args.requestCode) {
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800762 case REQUEST_CREATE_SHORTCUT:
Adam Cohendb364c32014-05-20 14:23:40 -0700763 completeAddShortcut(args.intent, args.container, screenId, args.cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -0700764 args.cellY);
Winson Chungb8472bb2011-08-05 13:49:21 -0700765 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800766 case REQUEST_CREATE_APPWIDGET:
Adam Cohendb364c32014-05-20 14:23:40 -0700767 completeAddAppWidget(args.appWidgetId, args.container, screenId, null, null);
Winson Chungb8472bb2011-08-05 13:49:21 -0700768 break;
Sunny Goyalff572272014-07-23 13:58:07 -0700769 case REQUEST_RECONFIGURE_APPWIDGET:
770 completeRestoreAppWidget(args.appWidgetId);
771 break;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800772 }
Michael Jurka27614d22012-04-02 06:40:22 -0700773 // Before adding this resetAddInfo(), after a shortcut was added to a workspace screen,
774 // if you turned the screen off and then back while in All Apps, Launcher would not
775 // return to the workspace. Clearing mAddInfo.container here fixes this issue
776 resetAddInfo();
Adam Cohendb364c32014-05-20 14:23:40 -0700777 return screenId;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800778 }
779
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800780 @Override
Michael Jurka8b805b12012-04-18 14:23:14 -0700781 protected void onActivityResult(
782 final int requestCode, final int resultCode, final Intent data) {
Winson Chunge341d302013-08-16 14:31:00 -0700783 // Reset the startActivity waiting flag
Anjali Koppalff7ceff2014-05-01 18:26:37 -0700784 setWaitingForResult(false);
Adam Cohendb364c32014-05-20 14:23:40 -0700785 final int pendingAddWidgetId = mPendingAddWidgetId;
Adam Cohen4637b5a2013-11-04 18:21:24 -0800786 mPendingAddWidgetId = -1;
Winson Chunge341d302013-08-16 14:31:00 -0700787
Adam Cohenad4e15c2013-10-17 16:21:35 -0700788 Runnable exitSpringLoaded = new Runnable() {
789 @Override
790 public void run() {
791 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
792 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
793 }
794 };
795
Michael Jurka8b805b12012-04-18 14:23:14 -0700796 if (requestCode == REQUEST_BIND_APPWIDGET) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700797 final int appWidgetId = data != null ?
Michael Jurka8b805b12012-04-18 14:23:14 -0700798 data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1) : -1;
799 if (resultCode == RESULT_CANCELED) {
800 completeTwoStageWidgetDrop(RESULT_CANCELED, appWidgetId);
Adam Cohen689ff162014-05-08 17:27:56 -0700801 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700802 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
Michael Jurka8b805b12012-04-18 14:23:14 -0700803 } else if (resultCode == RESULT_OK) {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700804 addAppWidgetImpl(appWidgetId, mPendingAddInfo, null,
805 mPendingAddWidgetInfo, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
Michael Jurka8b805b12012-04-18 14:23:14 -0700806 }
807 return;
Michael Jurka336fd4f2013-09-12 00:05:02 +0200808 } else if (requestCode == REQUEST_PICK_WALLPAPER) {
809 if (resultCode == RESULT_OK && mWorkspace.isInOverviewMode()) {
810 mWorkspace.exitOverviewMode(false);
811 }
812 return;
Michael Jurka8b805b12012-04-18 14:23:14 -0700813 }
Michael Jurka336fd4f2013-09-12 00:05:02 +0200814
Adam Cohened66b2b2012-01-23 17:28:51 -0800815 boolean isWidgetDrop = (requestCode == REQUEST_PICK_APPWIDGET ||
816 requestCode == REQUEST_CREATE_APPWIDGET);
Romain Guyaad5ef42009-06-10 02:48:37 -0700817
Adam Cohendb364c32014-05-20 14:23:40 -0700818 final boolean workspaceLocked = isWorkspaceLocked();
Adam Cohened66b2b2012-01-23 17:28:51 -0800819 // We have special handling for widgets
820 if (isWidgetDrop) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800821 final int appWidgetId;
822 int widgetId = data != null ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, -1)
823 : -1;
824 if (widgetId < 0) {
825 appWidgetId = pendingAddWidgetId;
826 } else {
827 appWidgetId = widgetId;
828 }
829
Adam Cohenad4e15c2013-10-17 16:21:35 -0700830 final int result;
Adam Cohenf0129b12013-11-04 17:57:36 -0800831 if (appWidgetId < 0 || resultCode == RESULT_CANCELED) {
Adam Cohendb364c32014-05-20 14:23:40 -0700832 Log.e(TAG, "Error: appWidgetId (EXTRA_APPWIDGET_ID) was not " +
833 "returned from the widget configuration activity.");
Adam Cohenad4e15c2013-10-17 16:21:35 -0700834 result = RESULT_CANCELED;
835 completeTwoStageWidgetDrop(result, appWidgetId);
Adam Cohendb364c32014-05-20 14:23:40 -0700836 final Runnable onComplete = new Runnable() {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700837 @Override
838 public void run() {
839 exitSpringLoadedDragModeDelayed(false, 0, null);
840 }
841 };
Adam Cohendb364c32014-05-20 14:23:40 -0700842 if (workspaceLocked) {
843 // No need to remove the empty screen if we're mid-binding, as the
844 // the bind will not add the empty screen.
845 mWorkspace.postDelayed(onComplete, ON_ACTIVITY_RESULT_ANIMATION_DELAY);
846 } else {
847 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
848 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
849 }
Winson Chung5aaab772012-04-27 15:24:02 -0700850 } else {
Adam Cohendb364c32014-05-20 14:23:40 -0700851 if (!workspaceLocked) {
Adam Cohen83079e42014-09-19 17:43:08 -0700852 if (mPendingAddInfo.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
853 // When the screen id represents an actual screen (as opposed to a rank)
854 // we make sure that the drop page actually exists.
855 mPendingAddInfo.screenId =
856 ensurePendingDropLayoutExists(mPendingAddInfo.screenId);
857 }
Adam Cohendb364c32014-05-20 14:23:40 -0700858 final CellLayout dropLayout = mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
859
860 dropLayout.setDropPending(true);
861 final Runnable onComplete = new Runnable() {
862 @Override
863 public void run() {
864 completeTwoStageWidgetDrop(resultCode, appWidgetId);
865 dropLayout.setDropPending(false);
866 }
867 };
868 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete,
869 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
870 } else {
871 PendingAddArguments args = preparePendingAddArgs(requestCode, data, appWidgetId,
872 mPendingAddInfo);
873 sPendingAddItem = args;
874 }
Winson Chung5aaab772012-04-27 15:24:02 -0700875 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800876 return;
877 }
878
Sunny Goyalff572272014-07-23 13:58:07 -0700879 if (requestCode == REQUEST_RECONFIGURE_APPWIDGET) {
880 if (resultCode == RESULT_OK) {
881 // Update the widget view.
882 PendingAddArguments args = preparePendingAddArgs(requestCode, data,
883 pendingAddWidgetId, mPendingAddInfo);
884 if (workspaceLocked) {
885 sPendingAddItem = args;
886 } else {
887 completeAdd(args);
888 }
889 }
890 // Leave the widget in the pending state if the user canceled the configure.
891 return;
892 }
893
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800894 // The pattern used here is that a user PICKs a specific application,
895 // which, depending on the target, might need to CREATE the actual target.
Romain Guycbb89e42009-06-08 15:52:54 -0700896
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800897 // For example, the user would PICK_SHORTCUT for "Music playlist", and we
898 // launch over to the Music app to actually CREATE_SHORTCUT.
Winson Chungc7da5552011-08-10 15:28:45 -0700899 if (resultCode == RESULT_OK && mPendingAddInfo.container != ItemInfo.NO_ID) {
Adam Cohendb364c32014-05-20 14:23:40 -0700900 final PendingAddArguments args = preparePendingAddArgs(requestCode, data, -1,
901 mPendingAddInfo);
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800902 if (isWorkspaceLocked()) {
Adam Cohendb364c32014-05-20 14:23:40 -0700903 sPendingAddItem = args;
Patrick Dubroy002cbf42011-03-03 16:36:21 -0800904 } else {
Adam Cohenad4e15c2013-10-17 16:21:35 -0700905 completeAdd(args);
Adam Cohendb364c32014-05-20 14:23:40 -0700906 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
907 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800908 }
Adam Cohen00074722013-10-11 17:46:09 -0700909 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen689ff162014-05-08 17:27:56 -0700910 mWorkspace.removeExtraEmptyScreenDelayed(true, exitSpringLoaded,
Adam Cohenad4e15c2013-10-17 16:21:35 -0700911 ON_ACTIVITY_RESULT_ANIMATION_DELAY, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800912 }
Adam Cohened66b2b2012-01-23 17:28:51 -0800913 mDragLayer.clearAnimatedView();
Adam Cohened66b2b2012-01-23 17:28:51 -0800914 }
915
Adam Cohendb364c32014-05-20 14:23:40 -0700916 private PendingAddArguments preparePendingAddArgs(int requestCode, Intent data, int
917 appWidgetId, ItemInfo info) {
918 PendingAddArguments args = new PendingAddArguments();
919 args.requestCode = requestCode;
920 args.intent = data;
921 args.container = info.container;
922 args.screenId = info.screenId;
923 args.cellX = info.cellX;
924 args.cellY = info.cellY;
925 args.appWidgetId = appWidgetId;
926 return args;
927 }
928
929 /**
930 * Check to see if a given screen id exists. If not, create it at the end, return the new id.
931 *
932 * @param screenId the screen id to check
933 * @return the new screen, or screenId if it exists
934 */
935 private long ensurePendingDropLayoutExists(long screenId) {
936 CellLayout dropLayout =
937 (CellLayout) mWorkspace.getScreenWithId(screenId);
938 if (dropLayout == null) {
939 // it's possible that the add screen was removed because it was
940 // empty and a re-bind occurred
941 mWorkspace.addExtraEmptyScreen();
942 return mWorkspace.commitExtraEmptyScreen();
943 } else {
944 return screenId;
945 }
946 }
947
Adam Cohened66b2b2012-01-23 17:28:51 -0800948 private void completeTwoStageWidgetDrop(final int resultCode, final int appWidgetId) {
Michael Jurka8b805b12012-04-18 14:23:14 -0700949 CellLayout cellLayout =
Adam Cohendcd297f2013-06-18 13:13:40 -0700950 (CellLayout) mWorkspace.getScreenWithId(mPendingAddInfo.screenId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800951 Runnable onCompleteRunnable = null;
952 int animationType = 0;
953
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700954 AppWidgetHostView boundWidget = null;
Adam Cohened66b2b2012-01-23 17:28:51 -0800955 if (resultCode == RESULT_OK) {
956 animationType = Workspace.COMPLETE_TWO_STAGE_WIDGET_DROP_ANIMATION;
957 final AppWidgetHostView layout = mAppWidgetHost.createView(this, appWidgetId,
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700958 mPendingAddWidgetInfo);
959 boundWidget = layout;
Adam Cohened66b2b2012-01-23 17:28:51 -0800960 onCompleteRunnable = new Runnable() {
961 @Override
962 public void run() {
963 completeAddAppWidget(appWidgetId, mPendingAddInfo.container,
Adam Cohendcd297f2013-06-18 13:13:40 -0700964 mPendingAddInfo.screenId, layout, null);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700965 exitSpringLoadedDragModeDelayed((resultCode != RESULT_CANCELED),
966 EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT, null);
Adam Cohened66b2b2012-01-23 17:28:51 -0800967 }
968 };
969 } else if (resultCode == RESULT_CANCELED) {
Adam Cohen4637b5a2013-11-04 18:21:24 -0800970 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Adam Cohened66b2b2012-01-23 17:28:51 -0800971 animationType = Workspace.CANCEL_TWO_STAGE_WIDGET_DROP_ANIMATION;
Adam Cohened66b2b2012-01-23 17:28:51 -0800972 }
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700973 if (mDragLayer.getAnimatedView() != null) {
974 mWorkspace.animateWidgetDrop(mPendingAddInfo, cellLayout,
975 (DragView) mDragLayer.getAnimatedView(), onCompleteRunnable,
976 animationType, boundWidget, true);
Adam Cohenad4e15c2013-10-17 16:21:35 -0700977 } else if (onCompleteRunnable != null) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -0700978 // The animated view may be null in the case of a rotation during widget configuration
979 onCompleteRunnable.run();
980 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -0800981 }
982
983 @Override
Michael Jurkacd496d72013-04-11 11:32:45 -0700984 protected void onStop() {
985 super.onStop();
986 FirstFrameAnimatorHelper.setIsVisible(false);
Adam Cohen9211d422014-10-07 18:14:53 -0700987
988 if (mLauncherCallbacks != null) {
989 mLauncherCallbacks.onStop();
990 }
Michael Jurkacd496d72013-04-11 11:32:45 -0700991 }
992
993 @Override
994 protected void onStart() {
995 super.onStart();
996 FirstFrameAnimatorHelper.setIsVisible(true);
Adam Cohen9211d422014-10-07 18:14:53 -0700997
998 if (mLauncherCallbacks != null) {
999 mLauncherCallbacks.onStart();
1000 }
Michael Jurkacd496d72013-04-11 11:32:45 -07001001 }
1002
1003 @Override
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001004 protected void onResume() {
Michael Jurka447bf842013-05-15 14:52:15 +02001005 long startTime = 0;
1006 if (DEBUG_RESUME_TIME) {
1007 startTime = System.currentTimeMillis();
Daniel Sandler924b9932013-06-12 00:38:25 -04001008 Log.v(TAG, "Launcher.onResume()");
Michael Jurka447bf842013-05-15 14:52:15 +02001009 }
Adam Cohen9211d422014-10-07 18:14:53 -07001010
1011 if (mLauncherCallbacks != null) {
1012 mLauncherCallbacks.preOnResume();
1013 }
1014
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001015 super.onResume();
Winson Chungf0c6ae02012-03-21 16:10:31 -07001016
Winson Chung4a2afa32012-07-19 14:53:05 -07001017 // Restore the previous launcher state
1018 if (mOnResumeState == State.WORKSPACE) {
1019 showWorkspace(false);
1020 } else if (mOnResumeState == State.APPS_CUSTOMIZE) {
Winson Chung07e045c2013-11-06 15:49:51 -08001021 showAllApps(false, mAppsCustomizeContent.getContentType(), false);
Winson Chung4a2afa32012-07-19 14:53:05 -07001022 }
1023 mOnResumeState = State.NONE;
1024
Craig Mautner360310b2012-10-26 15:13:08 -07001025 // Background was set to gradient in onPause(), restore to black if in all apps.
1026 setWorkspaceBackground(mState == State.WORKSPACE);
1027
Joe Onorato34a0e1b2009-12-14 17:44:51 -08001028 mPaused = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001029 if (mRestoring || mOnResumeNeedsLoad) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001030 setWorkspaceLoading(true);
Derek Prothro7aff3992013-12-10 14:00:37 -05001031 mModel.startLoader(true, PagedView.INVALID_RESTORE_PAGE);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001032 mRestoring = false;
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001033 mOnResumeNeedsLoad = false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001034 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001035 if (mBindOnResumeCallbacks.size() > 0) {
Michael Jurkac402cd92013-05-20 15:49:32 +02001036 // We might have postponed some bind calls until onResume (see waitUntilResume) --
1037 // execute them here
1038 long startTimeCallbacks = 0;
1039 if (DEBUG_RESUME_TIME) {
1040 startTimeCallbacks = System.currentTimeMillis();
1041 }
1042
1043 if (mAppsCustomizeContent != null) {
1044 mAppsCustomizeContent.setBulkBind(true);
1045 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001046 for (int i = 0; i < mBindOnResumeCallbacks.size(); i++) {
1047 mBindOnResumeCallbacks.get(i).run();
Michael Jurkac402cd92013-05-20 15:49:32 +02001048 }
1049 if (mAppsCustomizeContent != null) {
1050 mAppsCustomizeContent.setBulkBind(false);
1051 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07001052 mBindOnResumeCallbacks.clear();
Michael Jurkac402cd92013-05-20 15:49:32 +02001053 if (DEBUG_RESUME_TIME) {
1054 Log.d(TAG, "Time spent processing callbacks in onResume: " +
1055 (System.currentTimeMillis() - startTimeCallbacks));
1056 }
Michael Jurka447bf842013-05-15 14:52:15 +02001057 }
Michael Jurka54554252013-08-01 12:52:23 +02001058 if (mOnResumeCallbacks.size() > 0) {
1059 for (int i = 0; i < mOnResumeCallbacks.size(); i++) {
1060 mOnResumeCallbacks.get(i).run();
1061 }
1062 mOnResumeCallbacks.clear();
1063 }
Winson Chunge4e50662012-01-23 14:45:13 -08001064
1065 // Reset the pressed state of icons that were locked in the press state while activities
1066 // were launching
Michael Jurkaddd62e92011-02-16 17:49:14 -08001067 if (mWaitingForResume != null) {
Winson Chunge4e50662012-01-23 14:45:13 -08001068 // Resets the previous workspace icon press state
Michael Jurkaddd62e92011-02-16 17:49:14 -08001069 mWaitingForResume.setStayPressed(false);
1070 }
Winson Chung82963d52013-10-09 11:20:57 -07001071
Adam Cohen06dff352012-06-01 17:17:08 -07001072 // It is possible that widgets can receive updates while launcher is not in the foreground.
1073 // Consequently, the widgets will be inflated in the orientation of the foreground activity
1074 // (framework issue). On resuming, we ensure that any widgets are inflated for the current
1075 // orientation.
Adam Cohen3d509322012-06-06 14:10:04 -07001076 getWorkspace().reinflateWidgetsIfNecessary();
Adam Cohenf9426d52012-06-04 17:26:21 -07001077
Winson Chung780fe592013-09-26 14:48:44 -07001078 // Process any items that were added while Launcher was away.
1079 InstallShortcutReceiver.disableAndFlushInstallQueue(this);
1080
Winson Chung5841efa2013-09-30 18:06:44 -07001081 // Update the voice search button proxy
1082 updateVoiceButtonProxyVisible(false);
1083
Adam Cohenf9426d52012-06-04 17:26:21 -07001084 // Again, as with the above scenario, it's possible that one or more of the global icons
1085 // were updated in the wrong orientation.
1086 updateGlobalIcons();
Michael Jurka447bf842013-05-15 14:52:15 +02001087 if (DEBUG_RESUME_TIME) {
1088 Log.d(TAG, "Time spent in onResume: " + (System.currentTimeMillis() - startTime));
1089 }
Adam Cohen6fecd412013-10-02 17:41:50 -07001090
1091 if (mWorkspace.getCustomContentCallbacks() != null) {
1092 // If we are resuming and the custom content is the current page, we call onShow().
1093 // It is also poassible that onShow will instead be called slightly after first layout
1094 // if PagedView#setRestorePage was set to the custom content page in onCreate().
1095 if (mWorkspace.isOnOrMovingToCustomContent()) {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001096 mWorkspace.getCustomContentCallbacks().onShow(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07001097 }
1098 }
Adam Cohenedaaa302013-10-01 17:33:27 -07001099 mWorkspace.updateInteractionForState();
Adam Cohen53805212013-10-01 10:39:23 -07001100 mWorkspace.onResume();
Sunny Goyale755d462014-07-22 13:48:29 -07001101
1102 PackageInstallerCompat.getInstance(this).onResume();
Adam Cohen9211d422014-10-07 18:14:53 -07001103
1104 if (mLauncherCallbacks != null) {
1105 mLauncherCallbacks.onResume();
1106 }
Adam Cohen06dff352012-06-01 17:17:08 -07001107 }
1108
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001109 @Override
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001110 protected void onPause() {
Winson Chung997a9232013-07-24 15:33:46 -07001111 // Ensure that items added to Launcher are queued until Launcher returns
1112 InstallShortcutReceiver.enableInstallQueue();
Sunny Goyale755d462014-07-22 13:48:29 -07001113 PackageInstallerCompat.getInstance(this).onPause();
Winson Chung997a9232013-07-24 15:33:46 -07001114
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001115 super.onPause();
Joe Onoratoef2efcf2010-10-27 13:21:00 -07001116 mPaused = true;
Joe Onorato24b6fd82009-11-12 13:47:09 -08001117 mDragController.cancelDrag();
Winson Chunga2413752012-04-03 14:22:34 -07001118 mDragController.resetLastGestureUpTime();
Adam Cohen6fecd412013-10-02 17:41:50 -07001119
1120 // We call onHide() aggressively. The custom content callbacks should be able to
1121 // debounce excess onHide calls.
1122 if (mWorkspace.getCustomContentCallbacks() != null) {
1123 mWorkspace.getCustomContentCallbacks().onHide();
1124 }
Romain Guycbb89e42009-06-08 15:52:54 -07001125
Adam Cohen9211d422014-10-07 18:14:53 -07001126 if (mLauncherCallbacks != null) {
1127 mLauncherCallbacks.onPause();
Adam Cohenbffe7452013-07-22 18:21:45 -07001128 }
Adam Cohenbffe7452013-07-22 18:21:45 -07001129 }
1130
1131 public interface CustomContentCallbacks {
Selim Cinek3a8a8f72014-01-16 10:38:38 -08001132 // Custom content is completely shown. {@code fromResume} indicates whether this was caused
1133 // by a onResume or by scrolling otherwise.
1134 public void onShow(boolean fromResume);
Adam Cohenbffe7452013-07-22 18:21:45 -07001135
1136 // Custom content is completely hidden
1137 public void onHide();
Adam Cohenc36fa5c2013-08-29 11:54:42 -07001138
1139 // Custom content scroll progress changed. From 0 (not showing) to 1 (fully showing).
1140 public void onScrollProgressChanged(float progress);
Jan-Willem Maarse2ff91c42014-07-10 15:58:12 -07001141
1142 // Indicates whether the user is allowed to scroll away from the custom content.
1143 boolean isScrollingAllowed();
Adam Cohenbffe7452013-07-22 18:21:45 -07001144 }
1145
Adam Cohenc2d6e892014-10-16 09:49:24 -07001146 public interface LauncherOverlay {
1147
1148 /**
1149 * Touch interaction leading to overscroll has begun
1150 */
1151 public void onScrollInteractionBegin();
1152
1153 /**
1154 * Touch interaction related to overscroll has ended
1155 */
1156 public void onScrollInteractionEnd();
1157
1158 /**
1159 * Scroll progress, between 0 and 100, when the user scrolls beyond the leftmost
1160 * screen (or in the case of RTL, the rightmost screen).
1161 */
1162 public void onScrollChange(int progress, boolean rtl);
1163
1164 /**
1165 * Screen has stopped scrolling
1166 */
1167 public void onScrollSettled();
1168
1169 /**
1170 * This method can be called by the Launcher in order to force the LauncherOverlay
1171 * to exit fully immersive mode.
1172 */
1173 public void forceExitFullImmersion();
1174 }
1175
1176 public interface LauncherOverlayCallbacks {
1177 /**
1178 * This method indicates whether a call to {@link #enterFullImmersion()} will succeed,
1179 * however it doesn't modify any state within the launcher.
1180 */
1181 public boolean canEnterFullImmersion();
1182
1183 /**
1184 * Should be called to tell Launcher that the LauncherOverlay will take over interaction,
1185 * eg. by occupying the full screen and handling all touch events.
1186 *
1187 * @return true if Launcher allows the LauncherOverlay to become fully immersive. In this
1188 * case, Launcher will modify any necessary state and assumes the overlay is
1189 * handling all interaction. If false, the LauncherOverlay should cancel any
1190 *
1191 */
1192 public boolean enterFullImmersion();
1193
1194 /**
1195 * Must be called when exiting fully immersive mode. Indicates to Launcher that it has
1196 * full control over UI and state.
1197 */
1198 public void exitFullImmersion();
1199 }
1200
1201 class LauncherOverlayCallbacksImpl implements LauncherOverlayCallbacks {
1202
1203 @Override
1204 public boolean canEnterFullImmersion() {
1205 return mState == State.WORKSPACE;
1206 }
1207
1208 @Override
1209 public boolean enterFullImmersion() {
1210 if (mState == State.WORKSPACE) {
1211 // When fully immersed, disregard any touches which fall through.
1212 mDragLayer.setBlockTouch(true);
1213 return true;
1214 }
1215 return false;
1216 }
1217
1218 @Override
1219 public void exitFullImmersion() {
1220 mDragLayer.setBlockTouch(false);
1221 }
1222 }
1223
Jorim Jaggid017f882014-01-14 17:08:48 -08001224 protected boolean hasSettings() {
Adam Cohen9211d422014-10-07 18:14:53 -07001225 if (mLauncherCallbacks != null) {
1226 return mLauncherCallbacks.hasSettings();
1227 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001228 return false;
1229 }
1230
Adam Cohenbffe7452013-07-22 18:21:45 -07001231
Adam Cohen9211d422014-10-07 18:14:53 -07001232 public void addToCustomContentPage(View customContent,
Adam Cohen53805212013-10-01 10:39:23 -07001233 CustomContentCallbacks callbacks, String description) {
1234 mWorkspace.addToCustomContentPage(customContent, callbacks, description);
Adam Cohen66a01fd2013-06-11 12:48:00 -07001235 }
1236
Adam Cohenedb40762013-07-18 16:45:45 -07001237 // The custom content needs to offset its content to account for the QSB
1238 public int getTopOffsetForCustomContent() {
1239 return mWorkspace.getPaddingTop();
1240 }
1241
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001242 @Override
1243 public Object onRetainNonConfigurationInstance() {
Joe Onorato9c1289c2009-08-17 11:03:03 -04001244 // Flag the loader to stop early before switching
Adam Cohen1a85c582014-09-30 09:48:49 -07001245 if (mModel.isCurrentCallbacks(this)) {
1246 mModel.stopLoader();
1247 }
Winson Chung785d2eb2011-04-14 16:08:02 -07001248 if (mAppsCustomizeContent != null) {
1249 mAppsCustomizeContent.surrender();
1250 }
Romain Guy13c2e7b2010-03-10 19:45:00 -08001251 return Boolean.TRUE;
Jeffrey Sharkey99c87582009-03-24 17:59:43 -07001252 }
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001253
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001254 // We can't hide the IME if it was forced open. So don't bother
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001255 @Override
1256 public void onWindowFocusChanged(boolean hasFocus) {
1257 super.onWindowFocusChanged(hasFocus);
Adam Cohened307df2013-10-02 09:37:31 -07001258 mHasFocus = hasFocus;
Adam Cohen9211d422014-10-07 18:14:53 -07001259
1260 if (mLauncherCallbacks != null) {
1261 mLauncherCallbacks.onWindowFocusChanged(hasFocus);
1262 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001263 }
Joe Onorato2d7e7d02010-01-29 15:57:19 -08001264
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001265 private boolean acceptFilter() {
1266 final InputMethodManager inputManager = (InputMethodManager)
1267 getSystemService(Context.INPUT_METHOD_SERVICE);
1268 return !inputManager.isFullscreenMode();
1269 }
1270
1271 @Override
1272 public boolean onKeyDown(int keyCode, KeyEvent event) {
Winson Chung97d85d22011-04-13 11:27:36 -07001273 final int uniChar = event.getUnicodeChar();
1274 final boolean handled = super.onKeyDown(keyCode, event);
1275 final boolean isKeyNotWhitespace = uniChar > 0 && !Character.isWhitespace(uniChar);
1276 if (!handled && acceptFilter() && isKeyNotWhitespace) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001277 boolean gotKey = TextKeyListener.getInstance().onKeyDown(mWorkspace, mDefaultKeySsb,
1278 keyCode, event);
1279 if (gotKey && mDefaultKeySsb != null && mDefaultKeySsb.length() > 0) {
Karl Rosaen138a0412009-04-23 19:00:21 -07001280 // something usable has been typed - start a search
Romain Guycbb89e42009-06-08 15:52:54 -07001281 // the typed text will be retrieved and cleared by
Karl Rosaen138a0412009-04-23 19:00:21 -07001282 // showSearchDialog()
1283 // If there are multiple keystrokes before the search dialog takes focus,
1284 // onSearchRequested() will be called for every keystroke,
1285 // but it is idempotent, so it's fine.
1286 return onSearchRequested();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001287 }
1288 }
1289
Joe Onorato8a9625e2010-01-28 15:55:35 -08001290 // Eat the long press event so the keyboard doesn't come up.
1291 if (keyCode == KeyEvent.KEYCODE_MENU && event.isLongPress()) {
1292 return true;
1293 }
1294
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001295 return handled;
1296 }
1297
Karl Rosaen138a0412009-04-23 19:00:21 -07001298 private String getTypedText() {
1299 return mDefaultKeySsb.toString();
1300 }
1301
1302 private void clearTypedText() {
1303 mDefaultKeySsb.clear();
1304 mDefaultKeySsb.clearSpans();
1305 Selection.setSelection(mDefaultKeySsb, 0);
1306 }
1307
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001308 /**
Michael Jurka883f55b2010-10-21 15:47:14 -07001309 * Given the integer (ordinal) value of a State enum instance, convert it to a variable of type
1310 * State
1311 */
1312 private static State intToState(int stateOrdinal) {
1313 State state = State.WORKSPACE;
1314 final State[] stateValues = State.values();
1315 for (int i = 0; i < stateValues.length; i++) {
1316 if (stateValues[i].ordinal() == stateOrdinal) {
1317 state = stateValues[i];
1318 break;
1319 }
1320 }
1321 return state;
1322 }
1323
1324 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001325 * Restores the previous state, if it exists.
1326 *
1327 * @param savedState The previous state.
1328 */
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001329 @SuppressWarnings("unchecked")
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001330 private void restoreState(Bundle savedState) {
1331 if (savedState == null) {
1332 return;
1333 }
1334
Michael Jurka883f55b2010-10-21 15:47:14 -07001335 State state = intToState(savedState.getInt(RUNTIME_STATE, State.WORKSPACE.ordinal()));
Winson Chungf0ea4d32011-06-06 14:27:16 -07001336 if (state == State.APPS_CUSTOMIZE) {
Winson Chung4a2afa32012-07-19 14:53:05 -07001337 mOnResumeState = State.APPS_CUSTOMIZE;
Joe Onorato3a8820b2009-11-10 15:06:42 -08001338 }
1339
Adam Cohen21cd0022013-10-09 18:57:02 -07001340 int currentScreen = savedState.getInt(RUNTIME_STATE_CURRENT_SCREEN,
1341 PagedView.INVALID_RESTORE_PAGE);
1342 if (currentScreen != PagedView.INVALID_RESTORE_PAGE) {
Winson Chung8c87cd82013-07-23 16:20:10 -07001343 mWorkspace.setRestorePage(currentScreen);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001344 }
1345
Winson Chung3d503fb2011-07-13 17:25:49 -07001346 final long pendingAddContainer = savedState.getLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, -1);
Adam Cohendcd297f2013-06-18 13:13:40 -07001347 final long pendingAddScreen = savedState.getLong(RUNTIME_STATE_PENDING_ADD_SCREEN, -1);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001348
Winson Chung3d503fb2011-07-13 17:25:49 -07001349 if (pendingAddContainer != ItemInfo.NO_ID && pendingAddScreen > -1) {
1350 mPendingAddInfo.container = pendingAddContainer;
Adam Cohendcd297f2013-06-18 13:13:40 -07001351 mPendingAddInfo.screenId = pendingAddScreen;
Winson Chung3d503fb2011-07-13 17:25:49 -07001352 mPendingAddInfo.cellX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_X);
1353 mPendingAddInfo.cellY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_CELL_Y);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07001354 mPendingAddInfo.spanX = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_X);
1355 mPendingAddInfo.spanY = savedState.getInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y);
1356 mPendingAddWidgetInfo = savedState.getParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO);
Adam Cohen4637b5a2013-11-04 18:21:24 -08001357 mPendingAddWidgetId = savedState.getInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID);
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001358 setWaitingForResult(true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001359 mRestoring = true;
1360 }
1361
1362 boolean renameFolder = savedState.getBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, false);
1363 if (renameFolder) {
1364 long id = savedState.getLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07001365 mFolderInfo = mModel.getFolderById(this, sFolders, id);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001366 mRestoring = true;
1367 }
Winson Chunga12a2502010-12-20 14:41:35 -08001368
Winson Chung785d2eb2011-04-14 16:08:02 -07001369 // Restore the AppsCustomize tab
1370 if (mAppsCustomizeTabHost != null) {
1371 String curTab = savedState.getString("apps_customize_currentTab");
1372 if (curTab != null) {
Winson Chungc93e5ae2012-07-23 20:48:26 -07001373 mAppsCustomizeTabHost.setContentTypeImmediate(
Winson Chung785d2eb2011-04-14 16:08:02 -07001374 mAppsCustomizeTabHost.getContentTypeForTabTag(curTab));
Winson Chungf314b0e2011-08-16 11:54:27 -07001375 mAppsCustomizeContent.loadAssociatedPages(
1376 mAppsCustomizeContent.getCurrentPage());
Winson Chung785d2eb2011-04-14 16:08:02 -07001377 }
1378
Winson Chung5afbf7b2011-07-25 11:53:08 -07001379 int currentIndex = savedState.getInt("apps_customize_currentIndex");
1380 mAppsCustomizeContent.restorePageForIndex(currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07001381 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00001382 mItemIdToViewId = (HashMap<Integer, Integer>)
1383 savedState.getSerializable(RUNTIME_STATE_VIEW_IDS);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001384 }
1385
1386 /**
1387 * Finds all the views we need and configure them properly.
1388 */
1389 private void setupViews() {
Michael Jurkaa63c4522010-08-19 13:52:27 -07001390 final DragController dragController = mDragController;
Joe Onorato00acb122009-08-04 16:04:30 -04001391
Craig Mautner360310b2012-10-26 15:13:08 -07001392 mLauncherView = findViewById(R.id.launcher);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001393 mFocusHandler = (FocusIndicatorView) findViewById(R.id.focus_indicator);
Winson Chung4c98d922011-05-31 16:50:48 -07001394 mDragLayer = (DragLayer) findViewById(R.id.drag_layer);
1395 mWorkspace = (Workspace) mDragLayer.findViewById(R.id.workspace);
Anjali Koppalf5d29132014-02-28 13:33:27 -08001396 mWorkspace.setPageSwitchListener(this);
Winson Chunga6945242014-01-08 14:04:34 -08001397 mPageIndicators = mDragLayer.findViewById(R.id.page_indicator);
Craig Mautner360310b2012-10-26 15:13:08 -07001398
John Spurlock77e1f472013-09-11 10:09:51 -04001399 mLauncherView.setSystemUiVisibility(
1400 View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
Craig Mautner360310b2012-10-26 15:13:08 -07001401 mWorkspaceBackgroundDrawable = getResources().getDrawable(R.drawable.workspace_bg);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001402
Winson Chung4c98d922011-05-31 16:50:48 -07001403 // Setup the drag layer
1404 mDragLayer.setup(this, dragController);
1405
Winson Chung3d503fb2011-07-13 17:25:49 -07001406 // Setup the hotseat
1407 mHotseat = (Hotseat) findViewById(R.id.hotseat);
1408 if (mHotseat != null) {
1409 mHotseat.setup(this);
Winson Chung11a1a532013-09-13 11:14:45 -07001410 mHotseat.setOnLongClickListener(this);
Winson Chung3d503fb2011-07-13 17:25:49 -07001411 }
1412
Jorim Jaggid017f882014-01-14 17:08:48 -08001413 mOverviewPanel = (ViewGroup) findViewById(R.id.overview_panel);
Adam Cohen61f560d2013-09-30 15:58:20 -07001414 View widgetButton = findViewById(R.id.widget_button);
1415 widgetButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001416 @Override
1417 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001418 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001419 onClickAddWidgetButton(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001420 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001421 }
1422 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001423 widgetButton.setOnTouchListener(getHapticFeedbackTouchListener());
1424
1425 View wallpaperButton = findViewById(R.id.wallpaper_button);
1426 wallpaperButton.setOnClickListener(new OnClickListener() {
Adam Cohenf358a4b2013-07-23 16:47:31 -07001427 @Override
1428 public void onClick(View arg0) {
Winson Chung8e15fdf2013-11-11 15:47:45 -08001429 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001430 onClickWallpaperPicker(arg0);
Winson Chung8e15fdf2013-11-11 15:47:45 -08001431 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07001432 }
1433 });
Adam Cohen61f560d2013-09-30 15:58:20 -07001434 wallpaperButton.setOnTouchListener(getHapticFeedbackTouchListener());
1435
1436 View settingsButton = findViewById(R.id.settings_button);
Jorim Jaggid017f882014-01-14 17:08:48 -08001437 if (hasSettings()) {
1438 settingsButton.setOnClickListener(new OnClickListener() {
1439 @Override
1440 public void onClick(View arg0) {
1441 if (!mWorkspace.isSwitchingState()) {
Anjali Koppal7b168a12014-03-04 17:16:11 -08001442 onClickSettingsButton(arg0);
Jorim Jaggid017f882014-01-14 17:08:48 -08001443 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08001444 }
Jorim Jaggid017f882014-01-14 17:08:48 -08001445 });
1446 settingsButton.setOnTouchListener(getHapticFeedbackTouchListener());
1447 } else {
1448 settingsButton.setVisibility(View.GONE);
Jorim Jaggid017f882014-01-14 17:08:48 -08001449 }
1450
Adam Cohendbdff6b2013-09-18 19:09:15 -07001451 mOverviewPanel.setAlpha(0f);
Adam Cohenf358a4b2013-07-23 16:47:31 -07001452
Winson Chung4c98d922011-05-31 16:50:48 -07001453 // Setup the workspace
1454 mWorkspace.setHapticFeedbackEnabled(false);
1455 mWorkspace.setOnLongClickListener(this);
Adam Cohencff6af82011-09-13 14:51:53 -07001456 mWorkspace.setup(dragController);
Michael Jurkad74c9842011-07-10 12:44:21 -07001457 dragController.addDragListener(mWorkspace);
Winson Chung4c98d922011-05-31 16:50:48 -07001458
Winson Chungf0ea4d32011-06-06 14:27:16 -07001459 // Get the search/delete bar
Adam Cohen24ce0b32014-01-14 16:18:14 -08001460 mSearchDropTargetBar = (SearchDropTargetBar)
1461 mDragLayer.findViewById(R.id.search_drop_target_bar);
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07001462
Winson Chungf0ea4d32011-06-06 14:27:16 -07001463 // Setup AppsCustomize
Craig Mautner360310b2012-10-26 15:13:08 -07001464 mAppsCustomizeTabHost = (AppsCustomizeTabHost) findViewById(R.id.apps_customize_pane);
Winson Chungf0ea4d32011-06-06 14:27:16 -07001465 mAppsCustomizeContent = (AppsCustomizePagedView)
1466 mAppsCustomizeTabHost.findViewById(R.id.apps_customize_pane_content);
1467 mAppsCustomizeContent.setup(this, dragController);
Craig Mautner360310b2012-10-26 15:13:08 -07001468
Winson Chung3d503fb2011-07-13 17:25:49 -07001469 // Setup the drag controller (drop targets have to be added in reverse order in priority)
Winson Chung4c98d922011-05-31 16:50:48 -07001470 dragController.setDragScoller(mWorkspace);
1471 dragController.setScrollView(mDragLayer);
1472 dragController.setMoveTarget(mWorkspace);
1473 dragController.addDropTarget(mWorkspace);
Winson Chungc51db6a2011-10-05 11:44:49 -07001474 if (mSearchDropTargetBar != null) {
1475 mSearchDropTargetBar.setup(this, dragController);
Michael Jurkae0f5a612011-02-07 16:45:41 -08001476 }
Daniel Sandler924b9932013-06-12 00:38:25 -04001477
Daniel Sandlera127b7a2013-06-17 14:25:46 -04001478 if (getResources().getBoolean(R.bool.debug_memory_enabled)) {
Daniel Sandler924b9932013-06-12 00:38:25 -04001479 Log.v(TAG, "adding WeightWatcher");
Adam Cohen39a06042013-07-19 14:30:12 -07001480 mWeightWatcher = new WeightWatcher(this);
1481 mWeightWatcher.setAlpha(0.5f);
1482 ((FrameLayout) mLauncherView).addView(mWeightWatcher,
Daniel Sandler924b9932013-06-12 00:38:25 -04001483 new FrameLayout.LayoutParams(
1484 FrameLayout.LayoutParams.MATCH_PARENT,
Daniel Sandlerb9eb2862013-06-14 20:17:30 -04001485 FrameLayout.LayoutParams.WRAP_CONTENT,
Daniel Sandler924b9932013-06-12 00:38:25 -04001486 Gravity.BOTTOM)
1487 );
Adam Cohen39a06042013-07-19 14:30:12 -07001488
1489 boolean show = shouldShowWeightWatcher();
1490 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
Daniel Sandler924b9932013-06-12 00:38:25 -04001491 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001492 }
1493
1494 /**
Anjali Koppal5ad44842014-03-10 20:34:39 -07001495 * Sets the all apps button. This method is called from {@link Hotseat}.
1496 */
1497 public void setAllAppsButton(View allAppsButton) {
1498 mAllAppsButton = allAppsButton;
1499 }
1500
1501 public View getAllAppsButton() {
1502 return mAllAppsButton;
1503 }
1504
1505 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001506 * Creates a view representing a shortcut.
1507 *
1508 * @param info The data structure describing the shortcut.
1509 *
1510 * @return A View inflated from R.layout.application.
1511 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001512 View createShortcut(ShortcutInfo info) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001513 return createShortcut(R.layout.application,
Michael Jurka0142d492010-08-25 17:46:15 -07001514 (ViewGroup) mWorkspace.getChildAt(mWorkspace.getCurrentPage()), info);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001515 }
1516
1517 /**
1518 * Creates a view representing a shortcut inflated from the specified resource.
1519 *
1520 * @param layoutResId The id of the XML layout used to create the shortcut.
1521 * @param parent The group the shortcut belongs to.
1522 * @param info The data structure describing the shortcut.
1523 *
1524 * @return A View inflated from layoutResId.
1525 */
Joe Onorato0589f0f2010-02-08 13:44:00 -08001526 View createShortcut(int layoutResId, ViewGroup parent, ShortcutInfo info) {
Michael Jurka67b2f6c2010-11-17 12:33:46 -08001527 BubbleTextView favorite = (BubbleTextView) mInflater.inflate(layoutResId, parent, false);
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07001528 favorite.applyFromShortcutInfo(info, mIconCache, true);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001529 favorite.setOnClickListener(this);
Sunny Goyaldcbcc862014-08-12 15:58:36 -07001530 favorite.setOnFocusChangeListener(mFocusHandler);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001531 return favorite;
1532 }
1533
1534 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001535 * Add a shortcut to the workspace.
1536 *
1537 * @param data The intent describing the shortcut.
1538 * @param cellInfo The position on screen where to create the shortcut.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001539 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001540 private void completeAddShortcut(Intent data, long container, long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07001541 int cellY) {
1542 int[] cellXY = mTmpAddItemCellCoordinates;
1543 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohendcd297f2013-06-18 13:13:40 -07001544 CellLayout layout = getCellLayout(container, screenId);
Romain Guycbb89e42009-06-08 15:52:54 -07001545
Michael Jurkad3ef3062010-11-23 16:23:58 -08001546 boolean foundCellSpan = false;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001547
Sunny Goyal2350bc92014-10-14 16:42:54 -07001548 ShortcutInfo info = mModel.infoFromShortcutIntent(this, data);
Adam Cohend9198822011-11-22 16:42:47 -08001549 if (info == null) {
1550 return;
1551 }
Adam Cohen558baaf2011-08-15 15:22:57 -07001552 final View view = createShortcut(info);
1553
Adam Cohenfbba09b2011-07-18 21:43:05 -07001554 // First we check if we already know the exact location where we want to add this item.
1555 if (cellX >= 0 && cellY >= 0) {
1556 cellXY[0] = cellX;
1557 cellXY[1] = cellY;
1558 foundCellSpan = true;
Adam Cohen558baaf2011-08-15 15:22:57 -07001559
1560 // If appropriate, either create a folder or add to an existing folder
Adam Cohen482ed822012-03-02 14:15:13 -08001561 if (mWorkspace.createUserFolderIfNecessary(view, container, layout, cellXY, 0,
Adam Cohen558baaf2011-08-15 15:22:57 -07001562 true, null,null)) {
1563 return;
1564 }
1565 DragObject dragObject = new DragObject();
1566 dragObject.dragInfo = info;
Adam Cohen482ed822012-03-02 14:15:13 -08001567 if (mWorkspace.addToExistingFolderIfNecessary(view, layout, cellXY, 0, dragObject,
1568 true)) {
Adam Cohen558baaf2011-08-15 15:22:57 -07001569 return;
1570 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07001571 } else if (touchXY != null) {
Michael Jurkad3ef3062010-11-23 16:23:58 -08001572 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001573 int[] result = layout.findNearestVacantArea(touchXY[0], touchXY[1], 1, 1, cellXY);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001574 foundCellSpan = (result != null);
1575 } else {
Adam Cohenfbba09b2011-07-18 21:43:05 -07001576 foundCellSpan = layout.findCellForSpan(cellXY, 1, 1);
Michael Jurkad3ef3062010-11-23 16:23:58 -08001577 }
1578
1579 if (!foundCellSpan) {
Winson Chung93eef082012-03-23 15:59:27 -07001580 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka0280c3b2010-09-17 15:00:07 -07001581 return;
1582 }
1583
Adam Cohendcd297f2013-06-18 13:13:40 -07001584 LauncherModel.addItemToDatabase(this, info, container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001585
1586 if (!mRestoring) {
Adam Cohendcd297f2013-06-18 13:13:40 -07001587 mWorkspace.addInScreen(view, container, screenId, cellXY[0], cellXY[1], 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07001588 isWorkspaceLocked());
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001589 }
1590 }
1591
Adam Cohen2f093b62012-04-30 18:59:53 -07001592 static int[] getSpanForWidget(Context context, ComponentName component, int minWidth,
1593 int minHeight) {
1594 Rect padding = AppWidgetHostView.getDefaultPaddingForWidget(context, component, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001595 // We want to account for the extra amount of padding that we are adding to the widget
1596 // to ensure that it gets the full amount of space that it has requested
1597 int requiredWidth = minWidth + padding.left + padding.right;
1598 int requiredHeight = minHeight + padding.top + padding.bottom;
Winson Chung66700732013-08-20 16:56:15 -07001599 return CellLayout.rectToCell(requiredWidth, requiredHeight, null);
Adam Cohenf814aa02011-09-01 13:48:05 -07001600 }
1601
Adam Cohen2f093b62012-04-30 18:59:53 -07001602 static int[] getSpanForWidget(Context context, AppWidgetProviderInfo info) {
1603 return getSpanForWidget(context, info.provider, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001604 }
1605
Adam Cohen2f093b62012-04-30 18:59:53 -07001606 static int[] getMinSpanForWidget(Context context, AppWidgetProviderInfo info) {
1607 return getSpanForWidget(context, info.provider, info.minResizeWidth, info.minResizeHeight);
Adam Cohencbf47e32011-09-16 17:32:37 -07001608 }
1609
Adam Cohen2f093b62012-04-30 18:59:53 -07001610 static int[] getSpanForWidget(Context context, PendingAddWidgetInfo info) {
1611 return getSpanForWidget(context, info.componentName, info.minWidth, info.minHeight);
Adam Cohenf814aa02011-09-01 13:48:05 -07001612 }
1613
Adam Cohen2f093b62012-04-30 18:59:53 -07001614 static int[] getMinSpanForWidget(Context context, PendingAddWidgetInfo info) {
1615 return getSpanForWidget(context, info.componentName, info.minResizeWidth,
Winson Chunga5c96362012-04-12 14:04:41 -07001616 info.minResizeHeight);
Adam Cohend41fbf52012-02-16 23:53:59 -08001617 }
1618
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001619 /**
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001620 * Add a widget to the workspace.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001621 *
Romain Guy5bbc91b2010-08-18 11:38:46 -07001622 * @param appWidgetId The app widget id
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001623 * @param cellInfo The position on screen where to create the widget.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001624 */
Adam Cohendcd297f2013-06-18 13:13:40 -07001625 private void completeAddAppWidget(final int appWidgetId, long container, long screenId,
Adam Cohened66b2b2012-01-23 17:28:51 -08001626 AppWidgetHostView hostView, AppWidgetProviderInfo appWidgetInfo) {
1627 if (appWidgetInfo == null) {
1628 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
1629 }
Romain Guycbb89e42009-06-08 15:52:54 -07001630
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001631 // Calculate the grid spans needed to fit this widget
Adam Cohendcd297f2013-06-18 13:13:40 -07001632 CellLayout layout = getCellLayout(container, screenId);
Adam Cohen09353862011-07-14 16:10:03 -07001633
Adam Cohen2f093b62012-04-30 18:59:53 -07001634 int[] minSpanXY = getMinSpanForWidget(this, appWidgetInfo);
1635 int[] spanXY = getSpanForWidget(this, appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001636
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001637 // Try finding open space on Launcher screen
Michael Jurkaa63c4522010-08-19 13:52:27 -07001638 // We have saved the position to which the widget was dragged-- this really only matters
1639 // if we are placing widgets on a "spring-loaded" screen
Winson Chung3d503fb2011-07-13 17:25:49 -07001640 int[] cellXY = mTmpAddItemCellCoordinates;
1641 int[] touchXY = mPendingAddInfo.dropPos;
Adam Cohend41fbf52012-02-16 23:53:59 -08001642 int[] finalSpan = new int[2];
Michael Jurka0280c3b2010-09-17 15:00:07 -07001643 boolean foundCellSpan = false;
Winson Chung3d503fb2011-07-13 17:25:49 -07001644 if (mPendingAddInfo.cellX >= 0 && mPendingAddInfo.cellY >= 0) {
1645 cellXY[0] = mPendingAddInfo.cellX;
1646 cellXY[1] = mPendingAddInfo.cellY;
Adam Cohend41fbf52012-02-16 23:53:59 -08001647 spanXY[0] = mPendingAddInfo.spanX;
1648 spanXY[1] = mPendingAddInfo.spanY;
Adam Cohenfbba09b2011-07-18 21:43:05 -07001649 foundCellSpan = true;
1650 } else if (touchXY != null) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07001651 // when dragging and dropping, just find the closest free spot
Winson Chung3d503fb2011-07-13 17:25:49 -07001652 int[] result = layout.findNearestVacantArea(
Adam Cohend41fbf52012-02-16 23:53:59 -08001653 touchXY[0], touchXY[1], minSpanXY[0], minSpanXY[1], spanXY[0],
1654 spanXY[1], cellXY, finalSpan);
1655 spanXY[0] = finalSpan[0];
1656 spanXY[1] = finalSpan[1];
Michael Jurkad3ef3062010-11-23 16:23:58 -08001657 foundCellSpan = (result != null);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001658 } else {
Adam Cohend41fbf52012-02-16 23:53:59 -08001659 foundCellSpan = layout.findCellForSpan(cellXY, minSpanXY[0], minSpanXY[1]);
Michael Jurkaa63c4522010-08-19 13:52:27 -07001660 }
1661
Michael Jurka0280c3b2010-09-17 15:00:07 -07001662 if (!foundCellSpan) {
Winson Chungf7640c82011-02-28 13:47:29 -08001663 if (appWidgetId != -1) {
1664 // Deleting an app widget ID is a void call but writes to disk before returning
1665 // to the caller...
Michael Jurka43467462013-11-14 12:03:58 +01001666 new AsyncTask<Void, Void, Void>() {
1667 public Void doInBackground(Void ... args) {
Winson Chungf7640c82011-02-28 13:47:29 -08001668 mAppWidgetHost.deleteAppWidgetId(appWidgetId);
Michael Jurka43467462013-11-14 12:03:58 +01001669 return null;
Winson Chungf7640c82011-02-28 13:47:29 -08001670 }
Michael Jurka43467462013-11-14 12:03:58 +01001671 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Winson Chungf7640c82011-02-28 13:47:29 -08001672 }
Winson Chung93eef082012-03-23 15:59:27 -07001673 showOutOfSpaceMessage(isHotseatLayout(layout));
Romain Guy18042c82009-11-06 11:44:55 -08001674 return;
1675 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001676
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001677 // Build Launcher-specific widget info and save to database
Winson Chung11a49372012-04-27 15:12:38 -07001678 LauncherAppWidgetInfo launcherInfo = new LauncherAppWidgetInfo(appWidgetId,
1679 appWidgetInfo.provider);
Michael Jurka0280c3b2010-09-17 15:00:07 -07001680 launcherInfo.spanX = spanXY[0];
1681 launcherInfo.spanY = spanXY[1];
Adam Cohend41fbf52012-02-16 23:53:59 -08001682 launcherInfo.minSpanX = mPendingAddInfo.minSpanX;
1683 launcherInfo.minSpanY = mPendingAddInfo.minSpanY;
Sunny Goyalffe83f12014-08-14 17:39:34 -07001684 launcherInfo.user = mAppWidgetManager.getUser(appWidgetInfo);
Romain Guycbb89e42009-06-08 15:52:54 -07001685
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001686 LauncherModel.addItemToDatabase(this, launcherInfo,
Adam Cohendcd297f2013-06-18 13:13:40 -07001687 container, screenId, cellXY[0], cellXY[1], false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001688
1689 if (!mRestoring) {
Adam Cohened66b2b2012-01-23 17:28:51 -08001690 if (hostView == null) {
1691 // Perform actual inflation because we're live
1692 launcherInfo.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
1693 launcherInfo.hostView.setAppWidget(appWidgetId, appWidgetInfo);
1694 } else {
1695 // The AppWidgetHostView has already been inflated and instantiated
1696 launcherInfo.hostView = hostView;
1697 }
Romain Guycbb89e42009-06-08 15:52:54 -07001698
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001699 launcherInfo.hostView.setTag(launcherInfo);
Adam Cohend41fbf52012-02-16 23:53:59 -08001700 launcherInfo.hostView.setVisibility(View.VISIBLE);
Adam Cohenaaa5c212012-10-05 18:14:31 -07001701 launcherInfo.notifyWidgetSizeChanged(this);
1702
Adam Cohendcd297f2013-06-18 13:13:40 -07001703 mWorkspace.addInScreen(launcherInfo.hostView, container, screenId, cellXY[0], cellXY[1],
Joe Onorato9c1289c2009-08-17 11:03:03 -04001704 launcherInfo.spanX, launcherInfo.spanY, isWorkspaceLocked());
Adam Cohended9f8d2010-11-03 13:25:16 -07001705
1706 addWidgetToAutoAdvanceIfNeeded(launcherInfo.hostView, appWidgetInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001707 }
Adam Cohen6af9af02012-02-02 15:41:45 -08001708 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001709 }
Romain Guycbb89e42009-06-08 15:52:54 -07001710
Adam Cohended9f8d2010-11-03 13:25:16 -07001711 private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
1712 @Override
1713 public void onReceive(Context context, Intent intent) {
1714 final String action = intent.getAction();
1715 if (Intent.ACTION_SCREEN_OFF.equals(action)) {
1716 mUserPresent = false;
Adam Cohenbec6ac52011-07-19 20:50:27 -07001717 mDragLayer.clearAllResizeFrames();
Adam Cohended9f8d2010-11-03 13:25:16 -07001718 updateRunning();
Winson Chung337cd9d2011-03-30 10:39:30 -07001719
Winson Chungc100e8e2011-08-09 16:02:43 -07001720 // Reset AllApps to its initial state only if we are not in the middle of
Winson Chungb8472bb2011-08-05 13:49:21 -07001721 // processing a multi-step drop
Winson Chungc100e8e2011-08-09 16:02:43 -07001722 if (mAppsCustomizeTabHost != null && mPendingAddInfo.container == ItemInfo.NO_ID) {
Adam Cohened307df2013-10-02 09:37:31 -07001723 showWorkspace(false);
Winson Chung785d2eb2011-04-14 16:08:02 -07001724 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001725 } else if (Intent.ACTION_USER_PRESENT.equals(action)) {
1726 mUserPresent = true;
1727 updateRunning();
Dan Sandlerd5024042014-01-09 15:01:33 -05001728 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.DELETE_DATABASE.equals(action)) {
1729 mModel.resetLoadedState(false, true);
1730 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1731 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE);
1732 } else if (ENABLE_DEBUG_INTENTS && DebugIntents.MIGRATE_DATABASE.equals(action)) {
1733 mModel.resetLoadedState(false, true);
1734 mModel.startLoader(false, PagedView.INVALID_RESTORE_PAGE,
1735 LauncherModel.LOADER_FLAG_CLEAR_WORKSPACE
1736 | LauncherModel.LOADER_FLAG_MIGRATE_SHORTCUTS);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001737 } else if (LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED.equals(action)
1738 || LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED.equals(action)) {
1739 getModel().forceReload();
Adam Cohended9f8d2010-11-03 13:25:16 -07001740 }
1741 }
1742 };
1743
1744 @Override
1745 public void onAttachedToWindow() {
1746 super.onAttachedToWindow();
1747
1748 // Listen for broadcasts related to user-presence
1749 final IntentFilter filter = new IntentFilter();
1750 filter.addAction(Intent.ACTION_SCREEN_OFF);
1751 filter.addAction(Intent.ACTION_USER_PRESENT);
Amith Yamasani6cc806d2014-05-02 13:47:11 -07001752 // For handling managed profiles
1753 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_ADDED);
1754 filter.addAction(LauncherAppsCompat.ACTION_MANAGED_PROFILE_REMOVED);
Dan Sandlerd5024042014-01-09 15:01:33 -05001755 if (ENABLE_DEBUG_INTENTS) {
1756 filter.addAction(DebugIntents.DELETE_DATABASE);
1757 filter.addAction(DebugIntents.MIGRATE_DATABASE);
1758 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001759 registerReceiver(mReceiver, filter);
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001760 FirstFrameAnimatorHelper.initializeDrawListener(getWindow().getDecorView());
Adam Cohen0b395352014-06-09 22:54:36 +00001761 setupTransparentSystemBarsForLmp();
Adam Cohend113e0c2010-11-11 10:48:05 -08001762 mAttached = true;
Adam Cohended9f8d2010-11-03 13:25:16 -07001763 mVisible = true;
1764 }
1765
Adam Cohen0b395352014-06-09 22:54:36 +00001766 /**
1767 * Sets up transparent navigation and status bars in LMP.
1768 * This method is a no-op for other platform versions.
1769 */
1770 @TargetApi(19)
1771 private void setupTransparentSystemBarsForLmp() {
1772 // TODO(sansid): use the APIs directly when compiling against L sdk.
1773 // Currently we use reflection to access the flags and the API to set the transparency
1774 // on the System bars.
Kenny Guyd794a3f2014-09-16 15:17:58 +01001775 if (Utilities.isLmpOrAbove()) {
Adam Cohen0b395352014-06-09 22:54:36 +00001776 try {
1777 getWindow().getAttributes().systemUiVisibility |=
1778 (View.SYSTEM_UI_FLAG_LAYOUT_STABLE
1779 | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
1780 | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);
1781 getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS
1782 | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
1783 Field drawsSysBackgroundsField = WindowManager.LayoutParams.class.getField(
1784 "FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS");
1785 getWindow().addFlags(drawsSysBackgroundsField.getInt(null));
1786
1787 Method setStatusBarColorMethod =
1788 Window.class.getDeclaredMethod("setStatusBarColor", int.class);
1789 Method setNavigationBarColorMethod =
1790 Window.class.getDeclaredMethod("setNavigationBarColor", int.class);
1791 setStatusBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1792 setNavigationBarColorMethod.invoke(getWindow(), Color.TRANSPARENT);
1793 } catch (NoSuchFieldException e) {
1794 Log.w(TAG, "NoSuchFieldException while setting up transparent bars");
1795 } catch (NoSuchMethodException ex) {
1796 Log.w(TAG, "NoSuchMethodException while setting up transparent bars");
1797 } catch (IllegalAccessException e) {
1798 Log.w(TAG, "IllegalAccessException while setting up transparent bars");
1799 } catch (IllegalArgumentException e) {
1800 Log.w(TAG, "IllegalArgumentException while setting up transparent bars");
1801 } catch (InvocationTargetException e) {
1802 Log.w(TAG, "InvocationTargetException while setting up transparent bars");
1803 } finally {}
1804 }
1805 }
1806
Adam Cohended9f8d2010-11-03 13:25:16 -07001807 @Override
1808 public void onDetachedFromWindow() {
1809 super.onDetachedFromWindow();
1810 mVisible = false;
1811
Adam Cohend113e0c2010-11-11 10:48:05 -08001812 if (mAttached) {
1813 unregisterReceiver(mReceiver);
1814 mAttached = false;
1815 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001816 updateRunning();
1817 }
1818
1819 public void onWindowVisibilityChanged(int visibility) {
1820 mVisible = visibility == View.VISIBLE;
1821 updateRunning();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001822 // The following code used to be in onResume, but it turns out onResume is called when
1823 // you're in All Apps and click home to go to the workspace. onWindowVisibilityChanged
1824 // is a more appropriate event to handle
1825 if (mVisible) {
1826 mAppsCustomizeTabHost.onWindowVisible();
1827 if (!mWorkspaceLoading) {
1828 final ViewTreeObserver observer = mWorkspace.getViewTreeObserver();
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001829 // We want to let Launcher draw itself at least once before we force it to build
1830 // layers on all the workspace pages, so that transitioning to Launcher from other
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001831 // apps is nice and speedy.
1832 observer.addOnDrawListener(new ViewTreeObserver.OnDrawListener() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001833 private boolean mStarted = false;
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001834 public void onDraw() {
Michael Jurkadf96add2013-04-03 16:25:02 -07001835 if (mStarted) return;
1836 mStarted = true;
Michael Jurka6ee21d22012-02-21 18:20:27 -08001837 // We delay the layer building a bit in order to give
1838 // other message processing a time to run. In particular
1839 // this avoids a delay in hiding the IME if it was
1840 // currently shown, because doing that may involve
1841 // some communication back with the app.
Winson Chungaeae56b2012-02-24 15:47:27 -08001842 mWorkspace.postDelayed(mBuildLayersRunnable, 500);
Michael Jurkadf96add2013-04-03 16:25:02 -07001843 final ViewTreeObserver.OnDrawListener listener = this;
1844 mWorkspace.post(new Runnable() {
1845 public void run() {
Michael Jurkab68e03a2013-04-11 11:36:41 -07001846 if (mWorkspace != null &&
1847 mWorkspace.getViewTreeObserver() != null) {
1848 mWorkspace.getViewTreeObserver().
1849 removeOnDrawListener(listener);
1850 }
Michael Jurkadf96add2013-04-03 16:25:02 -07001851 }
1852 });
Michael Jurkaf1ad6082013-03-13 12:55:46 +01001853 return;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08001854 }
1855 });
1856 }
1857 clearTypedText();
1858 }
Adam Cohended9f8d2010-11-03 13:25:16 -07001859 }
1860
1861 private void sendAdvanceMessage(long delay) {
1862 mHandler.removeMessages(ADVANCE_MSG);
1863 Message msg = mHandler.obtainMessage(ADVANCE_MSG);
1864 mHandler.sendMessageDelayed(msg, delay);
1865 mAutoAdvanceSentTime = System.currentTimeMillis();
1866 }
1867
1868 private void updateRunning() {
1869 boolean autoAdvanceRunning = mVisible && mUserPresent && !mWidgetsToAdvance.isEmpty();
1870 if (autoAdvanceRunning != mAutoAdvanceRunning) {
1871 mAutoAdvanceRunning = autoAdvanceRunning;
1872 if (autoAdvanceRunning) {
1873 long delay = mAutoAdvanceTimeLeft == -1 ? mAdvanceInterval : mAutoAdvanceTimeLeft;
1874 sendAdvanceMessage(delay);
1875 } else {
1876 if (!mWidgetsToAdvance.isEmpty()) {
1877 mAutoAdvanceTimeLeft = Math.max(0, mAdvanceInterval -
1878 (System.currentTimeMillis() - mAutoAdvanceSentTime));
1879 }
1880 mHandler.removeMessages(ADVANCE_MSG);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08001881 mHandler.removeMessages(0); // Remove messages sent using postDelayed()
Adam Cohended9f8d2010-11-03 13:25:16 -07001882 }
1883 }
1884 }
1885
1886 private final Handler mHandler = new Handler() {
1887 @Override
1888 public void handleMessage(Message msg) {
1889 if (msg.what == ADVANCE_MSG) {
1890 int i = 0;
1891 for (View key: mWidgetsToAdvance.keySet()) {
1892 final View v = key.findViewById(mWidgetsToAdvance.get(key).autoAdvanceViewId);
1893 final int delay = mAdvanceStagger * i;
1894 if (v instanceof Advanceable) {
1895 postDelayed(new Runnable() {
1896 public void run() {
1897 ((Advanceable) v).advance();
1898 }
1899 }, delay);
1900 }
1901 i++;
1902 }
1903 sendAdvanceMessage(mAdvanceInterval);
1904 }
1905 }
1906 };
1907
1908 void addWidgetToAutoAdvanceIfNeeded(View hostView, AppWidgetProviderInfo appWidgetInfo) {
Adam Cohen292c0252011-07-18 13:55:17 -07001909 if (appWidgetInfo == null || appWidgetInfo.autoAdvanceViewId == -1) return;
Adam Cohended9f8d2010-11-03 13:25:16 -07001910 View v = hostView.findViewById(appWidgetInfo.autoAdvanceViewId);
1911 if (v instanceof Advanceable) {
1912 mWidgetsToAdvance.put(hostView, appWidgetInfo);
Adam Cohen2ddf13e2011-01-19 21:26:33 -08001913 ((Advanceable) v).fyiWillBeAdvancedByHostKThx();
Adam Cohended9f8d2010-11-03 13:25:16 -07001914 updateRunning();
1915 }
1916 }
1917
1918 void removeWidgetToAutoAdvance(View hostView) {
1919 if (mWidgetsToAdvance.containsKey(hostView)) {
1920 mWidgetsToAdvance.remove(hostView);
1921 updateRunning();
1922 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07001923 }
1924
Joe Onorato9c1289c2009-08-17 11:03:03 -04001925 public void removeAppWidget(LauncherAppWidgetInfo launcherInfo) {
Adam Cohended9f8d2010-11-03 13:25:16 -07001926 removeWidgetToAutoAdvance(launcherInfo.hostView);
Joe Onorato9c1289c2009-08-17 11:03:03 -04001927 launcherInfo.hostView = null;
1928 }
1929
Winson Chung93eef082012-03-23 15:59:27 -07001930 void showOutOfSpaceMessage(boolean isHotseatLayout) {
1931 int strId = (isHotseatLayout ? R.string.hotseat_out_of_space : R.string.out_of_space);
1932 Toast.makeText(this, getString(strId), Toast.LENGTH_SHORT).show();
Adam Cohended9f8d2010-11-03 13:25:16 -07001933 }
1934
Winson Chunga6945242014-01-08 14:04:34 -08001935 public DragLayer getDragLayer() {
1936 return mDragLayer;
1937 }
1938
1939 public Workspace getWorkspace() {
1940 return mWorkspace;
1941 }
1942
1943 public Hotseat getHotseat() {
1944 return mHotseat;
1945 }
1946
Jorim Jaggid017f882014-01-14 17:08:48 -08001947 public ViewGroup getOverviewPanel() {
Winson Chunga6945242014-01-08 14:04:34 -08001948 return mOverviewPanel;
1949 }
1950
1951 public SearchDropTargetBar getSearchBar() {
1952 return mSearchDropTargetBar;
1953 }
1954
The Android Open Source Project7376fae2009-03-11 12:11:58 -07001955 public LauncherAppWidgetHost getAppWidgetHost() {
1956 return mAppWidgetHost;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001957 }
Romain Guycbb89e42009-06-08 15:52:54 -07001958
Winson Chunga9abd0e2010-10-27 17:18:37 -07001959 public LauncherModel getModel() {
1960 return mModel;
1961 }
1962
Winson Chunga6945242014-01-08 14:04:34 -08001963 protected SharedPreferences getSharedPrefs() {
1964 return mSharedPrefs;
1965 }
1966
Bjorn Bringertc459e522013-06-07 19:36:01 +01001967 public void closeSystemDialogs() {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001968 getWindow().closeAllPanels();
1969
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001970 // Whatever we were doing is hereby canceled.
Anjali Koppalff7ceff2014-05-01 18:26:37 -07001971 setWaitingForResult(false);
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001972 }
1973
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001974 @Override
1975 protected void onNewIntent(Intent intent) {
Michael Jurka447bf842013-05-15 14:52:15 +02001976 long startTime = 0;
1977 if (DEBUG_RESUME_TIME) {
1978 startTime = System.currentTimeMillis();
1979 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08001980 super.onNewIntent(intent);
1981
1982 // Close the menu
1983 if (Intent.ACTION_MAIN.equals(intent.getAction())) {
Joe Onoratoa5c32d62009-11-19 10:28:49 -08001984 // also will cancel mWaitingForResult.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08001985 closeSystemDialogs();
Jason Samsfd22dac2009-09-20 17:24:16 -07001986
Adam Cohened307df2013-10-02 09:37:31 -07001987 final boolean alreadyOnHome = mHasFocus && ((intent.getFlags() &
1988 Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT)
1989 != Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
Michael Jurka01f0ed42010-08-20 00:41:17 -07001990
Adam Cohen6fecd412013-10-02 17:41:50 -07001991 if (mWorkspace == null) {
1992 // Can be cases where mWorkspace is null, this prevents a NPE
1993 return;
1994 }
1995 Folder openFolder = mWorkspace.getOpenFolder();
1996 // In all these cases, only animate if we're already on home
1997 mWorkspace.exitWidgetResizeMode();
Adam Cohen9211d422014-10-07 18:14:53 -07001998
1999 boolean moveToDefaultScreen = mLauncherCallbacks != null ?
2000 mLauncherCallbacks.shouldMoveToDefaultScreenOnHomeIntent() : true;
Adam Cohen6fecd412013-10-02 17:41:50 -07002001 if (alreadyOnHome && mState == State.WORKSPACE && !mWorkspace.isTouchActive() &&
Adam Cohen9211d422014-10-07 18:14:53 -07002002 openFolder == null && moveToDefaultScreen) {
Adam Cohen6fecd412013-10-02 17:41:50 -07002003 mWorkspace.moveToDefaultScreen(true);
Winson Chung4a2afa32012-07-19 14:53:05 -07002004 }
Romain Guy1dd3a072009-07-16 13:21:01 -07002005
Adam Cohen6fecd412013-10-02 17:41:50 -07002006 closeFolder();
2007 exitSpringLoadedDragMode();
2008
2009 // If we are already on home, then just animate back to the workspace,
2010 // otherwise, just wait until onResume to set the state back to Workspace
2011 if (alreadyOnHome) {
Adam Cohened307df2013-10-02 09:37:31 -07002012 showWorkspace(true);
Adam Cohen6fecd412013-10-02 17:41:50 -07002013 } else {
2014 mOnResumeState = State.WORKSPACE;
2015 }
2016
2017 final View v = getWindow().peekDecorView();
2018 if (v != null && v.getWindowToken() != null) {
2019 InputMethodManager imm = (InputMethodManager)getSystemService(
2020 INPUT_METHOD_SERVICE);
2021 imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
2022 }
2023
2024 // Reset the apps customize page
Winson Chunge7e97e62013-12-02 17:02:50 -08002025 if (!alreadyOnHome && mAppsCustomizeTabHost != null) {
Adam Cohen6fecd412013-10-02 17:41:50 -07002026 mAppsCustomizeTabHost.reset();
2027 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002028
Adam Cohen9211d422014-10-07 18:14:53 -07002029 if (mLauncherCallbacks != null) {
2030 mLauncherCallbacks.onHomeIntent();
2031 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002032 }
Adam Cohened307df2013-10-02 09:37:31 -07002033
Michael Jurka447bf842013-05-15 14:52:15 +02002034 if (DEBUG_RESUME_TIME) {
2035 Log.d(TAG, "Time spent in onNewIntent: " + (System.currentTimeMillis() - startTime));
2036 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002037
Adam Cohen9211d422014-10-07 18:14:53 -07002038 if (mLauncherCallbacks != null) {
2039 mLauncherCallbacks.onNewIntent(intent);
2040 }
Adam Coppa120b8e2013-11-12 16:26:04 +00002041 }
2042
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002043 @Override
Adam Cohen1462de32012-07-24 22:34:36 -07002044 public void onRestoreInstanceState(Bundle state) {
2045 super.onRestoreInstanceState(state);
2046 for (int page: mSynchronouslyBoundPages) {
2047 mWorkspace.restoreInstanceStateForChild(page);
2048 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002049 }
2050
2051 @Override
2052 protected void onSaveInstanceState(Bundle outState) {
Adam Cohen21cd0022013-10-09 18:57:02 -07002053 if (mWorkspace.getChildCount() > 0) {
Winson Chung9b9fb962013-11-15 15:39:34 -08002054 outState.putInt(RUNTIME_STATE_CURRENT_SCREEN,
2055 mWorkspace.getCurrentPageOffsetFromCustomContent());
Adam Cohen21cd0022013-10-09 18:57:02 -07002056 }
Adam Cohen95bb8002011-07-03 23:40:28 -07002057 super.onSaveInstanceState(outState);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002058
Michael Jurka883f55b2010-10-21 15:47:14 -07002059 outState.putInt(RUNTIME_STATE, mState.ordinal());
Adam Cohen51e95032011-07-11 14:44:19 -07002060 // We close any open folder since it will not be re-opened, and we need to make sure
2061 // this state is reflected.
2062 closeFolder();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002063
Adam Cohendcd297f2013-06-18 13:13:40 -07002064 if (mPendingAddInfo.container != ItemInfo.NO_ID && mPendingAddInfo.screenId > -1 &&
Winson Chung3d503fb2011-07-13 17:25:49 -07002065 mWaitingForResult) {
2066 outState.putLong(RUNTIME_STATE_PENDING_ADD_CONTAINER, mPendingAddInfo.container);
Adam Cohendcd297f2013-06-18 13:13:40 -07002067 outState.putLong(RUNTIME_STATE_PENDING_ADD_SCREEN, mPendingAddInfo.screenId);
Winson Chung3d503fb2011-07-13 17:25:49 -07002068 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_X, mPendingAddInfo.cellX);
2069 outState.putInt(RUNTIME_STATE_PENDING_ADD_CELL_Y, mPendingAddInfo.cellY);
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002070 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_X, mPendingAddInfo.spanX);
2071 outState.putInt(RUNTIME_STATE_PENDING_ADD_SPAN_Y, mPendingAddInfo.spanY);
2072 outState.putParcelable(RUNTIME_STATE_PENDING_ADD_WIDGET_INFO, mPendingAddWidgetInfo);
Adam Cohen4637b5a2013-11-04 18:21:24 -08002073 outState.putInt(RUNTIME_STATE_PENDING_ADD_WIDGET_ID, mPendingAddWidgetId);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002074 }
2075
2076 if (mFolderInfo != null && mWaitingForResult) {
2077 outState.putBoolean(RUNTIME_STATE_PENDING_FOLDER_RENAME, true);
2078 outState.putLong(RUNTIME_STATE_PENDING_FOLDER_RENAME_ID, mFolderInfo.id);
2079 }
Michael Jurka946ad472010-07-09 18:05:18 -07002080
Winson Chung785d2eb2011-04-14 16:08:02 -07002081 // Save the current AppsCustomize tab
2082 if (mAppsCustomizeTabHost != null) {
Winson Chung07e045c2013-11-06 15:49:51 -08002083 AppsCustomizePagedView.ContentType type = mAppsCustomizeContent.getContentType();
2084 String currentTabTag = mAppsCustomizeTabHost.getTabTagForContentType(type);
Winson Chung785d2eb2011-04-14 16:08:02 -07002085 if (currentTabTag != null) {
2086 outState.putString("apps_customize_currentTab", currentTabTag);
2087 }
Winson Chung5afbf7b2011-07-25 11:53:08 -07002088 int currentIndex = mAppsCustomizeContent.getSaveInstanceStateIndex();
2089 outState.putInt("apps_customize_currentIndex", currentIndex);
Winson Chung785d2eb2011-04-14 16:08:02 -07002090 }
Adam Cohenc76e1dd2013-11-14 11:09:14 +00002091 outState.putSerializable(RUNTIME_STATE_VIEW_IDS, mItemIdToViewId);
Adam Cohen9211d422014-10-07 18:14:53 -07002092
2093 if (mLauncherCallbacks != null) {
2094 mLauncherCallbacks.onSaveInstanceState(outState);
2095 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002096 }
2097
2098 @Override
2099 public void onDestroy() {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002100 super.onDestroy();
Romain Guycbb89e42009-06-08 15:52:54 -07002101
Winson Chunge7a03942011-08-05 15:05:12 -07002102 // Remove all pending runnables
2103 mHandler.removeMessages(ADVANCE_MSG);
2104 mHandler.removeMessages(0);
Michael Jurka9d906c72011-10-14 06:25:36 -07002105 mWorkspace.removeCallbacks(mBuildLayersRunnable);
Winson Chunge7a03942011-08-05 15:05:12 -07002106
Winson Chungcd2b0142011-06-08 16:02:26 -07002107 // Stop callbacks from LauncherModel
Daniel Sandlercc8befa2013-06-11 14:45:48 -04002108 LauncherAppState app = (LauncherAppState.getInstance());
Adam Cohen1a85c582014-09-30 09:48:49 -07002109
2110 // It's possible to receive onDestroy after a new Launcher activity has
2111 // been created. In this case, don't interfere with the new Launcher.
2112 if (mModel.isCurrentCallbacks(this)) {
2113 mModel.stopLoader();
2114 app.setLauncher(null);
2115 }
Winson Chungcd2b0142011-06-08 16:02:26 -07002116
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002117 try {
The Android Open Source Project7376fae2009-03-11 12:11:58 -07002118 mAppWidgetHost.stopListening();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002119 } catch (NullPointerException ex) {
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08002120 Log.w(TAG, "problem while stopping AppWidgetHost during Launcher destruction", ex);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002121 }
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002122 mAppWidgetHost = null;
2123
2124 mWidgetsToAdvance.clear();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002125
2126 TextKeyListener.getInstance().release();
2127
Winson Chung81b52252012-08-27 15:34:29 -07002128 // Disconnect any of the callbacks and drawables associated with ItemInfos on the workspace
2129 // to prevent leaking Launcher activities on orientation change.
2130 if (mModel != null) {
2131 mModel.unbindItemInfosAndClearQueuedBindRunnables();
2132 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002133
2134 getContentResolver().unregisterContentObserver(mWidgetObserver);
Joe Onorato34a0e1b2009-12-14 17:44:51 -08002135 unregisterReceiver(mCloseSystemDialogsReceiver);
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002136
Adam Cohenaccf3bf2012-04-30 16:07:43 -07002137 mDragLayer.clearAllResizeFrames();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002138 ((ViewGroup) mWorkspace.getParent()).removeAllViews();
Adam Cohend552dd92013-11-26 12:13:11 -08002139 mWorkspace.removeAllWorkspaceScreens();
Patrick Dubroy2313eff2011-01-11 20:01:31 -08002140 mWorkspace = null;
2141 mDragController = null;
Patrick Dubroy60b7c532011-01-16 17:19:32 -08002142
Michael Jurka2ecf9952012-06-18 12:52:28 -07002143 LauncherAnimUtils.onDestroyActivity();
Adam Cohen9211d422014-10-07 18:14:53 -07002144
2145 if (mLauncherCallbacks != null) {
2146 mLauncherCallbacks.onDestroy();
2147 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002148 }
2149
Adam Cohena9cf38f2011-05-02 15:36:58 -07002150 public DragController getDragController() {
2151 return mDragController;
2152 }
2153
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002154 @Override
2155 public void startActivityForResult(Intent intent, int requestCode) {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002156 if (requestCode >= 0) {
2157 setWaitingForResult(true);
2158 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002159 super.startActivityForResult(intent, requestCode);
2160 }
2161
Winson Chung70d72102011-08-12 11:24:35 -07002162 /**
2163 * Indicates that we want global search for this activity by setting the globalSearch
2164 * argument for {@link #startSearch} to true.
2165 */
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002166 @Override
Romain Guycbb89e42009-06-08 15:52:54 -07002167 public void startSearch(String initialQuery, boolean selectInitialQuery,
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002168 Bundle appSearchData, boolean globalSearch) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002169
Michael Jurkac0e8fca2010-10-06 16:41:29 -07002170 showWorkspace(true);
Romain Guycbb89e42009-06-08 15:52:54 -07002171
Karl Rosaen138a0412009-04-23 19:00:21 -07002172 if (initialQuery == null) {
2173 // Use any text typed in the launcher as the initial query
2174 initialQuery = getTypedText();
Karl Rosaen138a0412009-04-23 19:00:21 -07002175 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002176 if (appSearchData == null) {
2177 appSearchData = new Bundle();
Bjorn Bringert32b12d22013-06-06 13:00:41 +01002178 appSearchData.putString("source", "launcher-search");
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002179 }
Winson Chungadf0c182012-08-23 14:59:07 -07002180 Rect sourceBounds = new Rect();
2181 if (mSearchDropTargetBar != null) {
2182 sourceBounds = mSearchDropTargetBar.getSearchBarBounds();
2183 }
Romain Guycbb89e42009-06-08 15:52:54 -07002184
Ian Parkinson047036e2014-09-01 15:40:53 +01002185 boolean clearTextImmediately = startSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002186 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002187 if (clearTextImmediately) {
2188 clearTypedText();
2189 }
Bjorn Bringertc459e522013-06-07 19:36:01 +01002190 }
2191
Ian Parkinson047036e2014-09-01 15:40:53 +01002192 /**
2193 * Start a text search.
2194 *
2195 * @return {@code true} if the search will start immediately, so any further keypresses
2196 * will be handled directly by the search UI. {@code false} if {@link Launcher} should continue
2197 * to buffer keypresses.
2198 */
2199 public boolean startSearch(String initialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002200 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Adam Cohen9211d422014-10-07 18:14:53 -07002201 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2202 return mLauncherCallbacks.startSearch(initialQuery, selectInitialQuery, appSearchData,
2203 sourceBounds);
2204 }
2205
Michael Jurkaa33411c2012-06-14 16:18:21 -07002206 startGlobalSearch(initialQuery, selectInitialQuery,
Bjorn Bringertc459e522013-06-07 19:36:01 +01002207 appSearchData, sourceBounds);
Ian Parkinson047036e2014-09-01 15:40:53 +01002208 return false;
Michael Jurkaa33411c2012-06-14 16:18:21 -07002209 }
2210
2211 /**
2212 * Starts the global search activity. This code is a copied from SearchManager
2213 */
Bjorn Bringertc459e522013-06-07 19:36:01 +01002214 private void startGlobalSearch(String initialQuery,
Michael Jurkaa33411c2012-06-14 16:18:21 -07002215 boolean selectInitialQuery, Bundle appSearchData, Rect sourceBounds) {
Karl Rosaen138a0412009-04-23 19:00:21 -07002216 final SearchManager searchManager =
Michael Jurkaa33411c2012-06-14 16:18:21 -07002217 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2218 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
2219 if (globalSearchActivity == null) {
2220 Log.w(TAG, "No global search activity found.");
2221 return;
2222 }
2223 Intent intent = new Intent(SearchManager.INTENT_ACTION_GLOBAL_SEARCH);
2224 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
2225 intent.setComponent(globalSearchActivity);
2226 // Make sure that we have a Bundle to put source in
2227 if (appSearchData == null) {
2228 appSearchData = new Bundle();
2229 } else {
2230 appSearchData = new Bundle(appSearchData);
2231 }
Adam Cohen9211d422014-10-07 18:14:53 -07002232 // Set source to package name of app that starts global search if not set already.
Michael Jurkaa33411c2012-06-14 16:18:21 -07002233 if (!appSearchData.containsKey("source")) {
2234 appSearchData.putString("source", getPackageName());
2235 }
2236 intent.putExtra(SearchManager.APP_DATA, appSearchData);
2237 if (!TextUtils.isEmpty(initialQuery)) {
2238 intent.putExtra(SearchManager.QUERY, initialQuery);
2239 }
2240 if (selectInitialQuery) {
2241 intent.putExtra(SearchManager.EXTRA_SELECT_QUERY, selectInitialQuery);
2242 }
2243 intent.setSourceBounds(sourceBounds);
2244 try {
2245 startActivity(intent);
2246 } catch (ActivityNotFoundException ex) {
2247 Log.e(TAG, "Global search activity not found: " + globalSearchActivity);
2248 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002249 }
2250
Yura4f93ec62014-02-04 14:15:21 +00002251 public boolean isOnCustomContent() {
2252 return mWorkspace.isOnOrMovingToCustomContent();
2253 }
2254
Winson Chung70d72102011-08-12 11:24:35 -07002255 @Override
Winson Chung70d72102011-08-12 11:24:35 -07002256 public boolean onPrepareOptionsMenu(Menu menu) {
2257 super.onPrepareOptionsMenu(menu);
Yura4f93ec62014-02-04 14:15:21 +00002258 if (!isOnCustomContent()) {
2259 // Close any open folders
2260 closeFolder();
2261 // Stop resizing any widgets
2262 mWorkspace.exitWidgetResizeMode();
2263 if (!mWorkspace.isInOverviewMode()) {
2264 // Show the overview mode
2265 showOverviewMode(true);
2266 } else {
2267 showWorkspace(true);
2268 }
Winson Chunge0298742014-01-17 12:03:00 -08002269 }
Adam Cohen9211d422014-10-07 18:14:53 -07002270 if (mLauncherCallbacks != null) {
2271 return mLauncherCallbacks.onPrepareOptionsMenu(menu);
2272 }
2273
Michael Jurkab94f3f82013-09-11 18:08:54 +02002274 return false;
Winson Chung70d72102011-08-12 11:24:35 -07002275 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002276
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002277 @Override
2278 public boolean onSearchRequested() {
Romain Guycbb89e42009-06-08 15:52:54 -07002279 startSearch(null, false, null, true);
Amith Yamasania135ba82011-08-09 17:42:01 -07002280 // Use a custom animation for launching search
Karl Rosaen138a0412009-04-23 19:00:21 -07002281 return true;
The Android Open Source Projectca9475f2009-03-13 13:04:24 -07002282 }
2283
Joe Onorato9c1289c2009-08-17 11:03:03 -04002284 public boolean isWorkspaceLocked() {
2285 return mWorkspaceLoading || mWaitingForResult;
2286 }
2287
Adam Cohen517a7f52014-03-01 12:12:59 -08002288 public boolean isWorkspaceLoading() {
2289 return mWorkspaceLoading;
2290 }
2291
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002292 private void setWorkspaceLoading(boolean value) {
2293 boolean isLocked = isWorkspaceLocked();
2294 mWorkspaceLoading = value;
2295 if (isLocked != isWorkspaceLocked()) {
2296 onWorkspaceLockedChanged();
2297 }
2298 }
2299
2300 private void setWaitingForResult(boolean value) {
2301 boolean isLocked = isWorkspaceLocked();
2302 mWaitingForResult = value;
2303 if (isLocked != isWorkspaceLocked()) {
2304 onWorkspaceLockedChanged();
2305 }
2306 }
2307
Adam Cohen9211d422014-10-07 18:14:53 -07002308 protected void onWorkspaceLockedChanged() {
2309 if (mLauncherCallbacks != null) {
2310 mLauncherCallbacks.onWorkspaceLockedChanged();
2311 }
2312 }
Anjali Koppalff7ceff2014-05-01 18:26:37 -07002313
Michael Jurka0280c3b2010-09-17 15:00:07 -07002314 private void resetAddInfo() {
Winson Chung3d503fb2011-07-13 17:25:49 -07002315 mPendingAddInfo.container = ItemInfo.NO_ID;
Adam Cohendcd297f2013-06-18 13:13:40 -07002316 mPendingAddInfo.screenId = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002317 mPendingAddInfo.cellX = mPendingAddInfo.cellY = -1;
2318 mPendingAddInfo.spanX = mPendingAddInfo.spanY = -1;
Adam Cohend41fbf52012-02-16 23:53:59 -08002319 mPendingAddInfo.minSpanX = mPendingAddInfo.minSpanY = -1;
Winson Chung3d503fb2011-07-13 17:25:49 -07002320 mPendingAddInfo.dropPos = null;
Michael Jurka0280c3b2010-09-17 15:00:07 -07002321 }
Michael Jurkaa63c4522010-08-19 13:52:27 -07002322
Adam Cohenad4e15c2013-10-17 16:21:35 -07002323 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2324 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo) {
2325 addAppWidgetImpl(appWidgetId, info, boundWidget, appWidgetInfo, 0);
2326 }
2327
2328 void addAppWidgetImpl(final int appWidgetId, final ItemInfo info,
2329 final AppWidgetHostView boundWidget, final AppWidgetProviderInfo appWidgetInfo, int
2330 delay) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002331 if (appWidgetInfo.configure != null) {
2332 mPendingAddWidgetInfo = appWidgetInfo;
Adam Cohen4637b5a2013-11-04 18:21:24 -08002333 mPendingAddWidgetId = appWidgetId;
Michael Jurkaaf442092010-06-10 17:01:57 -07002334
Bjorn Bringert7984c942009-12-09 15:38:25 +00002335 // Launch over to configure widget, if needed
Sunny Goyalffe83f12014-08-14 17:39:34 -07002336 mAppWidgetManager.startConfigActivity(appWidgetInfo, appWidgetId, this,
2337 mAppWidgetHost, REQUEST_CREATE_APPWIDGET);
2338
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002339 } else {
Bjorn Bringert7984c942009-12-09 15:38:25 +00002340 // Otherwise just add it
Adam Cohenad4e15c2013-10-17 16:21:35 -07002341 Runnable onComplete = new Runnable() {
2342 @Override
2343 public void run() {
2344 // Exit spring loaded mode if necessary after adding the widget
2345 exitSpringLoadedDragModeDelayed(true, EXIT_SPRINGLOADED_MODE_SHORT_TIMEOUT,
2346 null);
2347 }
2348 };
Adam Cohendcd297f2013-06-18 13:13:40 -07002349 completeAddAppWidget(appWidgetId, info.container, info.screenId, boundWidget,
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002350 appWidgetInfo);
Adam Cohen689ff162014-05-08 17:27:56 -07002351 mWorkspace.removeExtraEmptyScreenDelayed(true, onComplete, delay, false);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002352 }
2353 }
Romain Guycbb89e42009-06-08 15:52:54 -07002354
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002355 protected void moveToCustomContentScreen(boolean animate) {
Sandeep Siddharthaf2b47f12013-09-26 19:49:27 -07002356 // Close any folders that may be open.
2357 closeFolder();
Allan Wojciechowskiaf110e82013-09-12 10:48:23 +01002358 mWorkspace.moveToCustomContentScreen(animate);
2359 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002360 /**
2361 * Process a shortcut drop.
2362 *
2363 * @param componentName The name of the component
Adam Cohendcd297f2013-06-18 13:13:40 -07002364 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002365 * @param cell The cell it should be added to, optional
2366 * @param position The location on the screen where it was dropped, optional
2367 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002368 void processShortcutFromDrop(ComponentName componentName, long container, long screenId,
Winson Chung3d503fb2011-07-13 17:25:49 -07002369 int[] cell, int[] loc) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07002370 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002371 mPendingAddInfo.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002372 mPendingAddInfo.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002373 mPendingAddInfo.dropPos = loc;
Adam Cohenfbba09b2011-07-18 21:43:05 -07002374
2375 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002376 mPendingAddInfo.cellX = cell[0];
2377 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002378 }
Michael Jurka0280c3b2010-09-17 15:00:07 -07002379
2380 Intent createShortcutIntent = new Intent(Intent.ACTION_CREATE_SHORTCUT);
2381 createShortcutIntent.setComponent(componentName);
2382 processShortcut(createShortcutIntent);
2383 }
2384
Adam Cohenfbba09b2011-07-18 21:43:05 -07002385 /**
2386 * Process a widget drop.
2387 *
2388 * @param info The PendingAppWidgetInfo of the widget being added.
Adam Cohendcd297f2013-06-18 13:13:40 -07002389 * @param screenId The ID of the screen where it should be added
Adam Cohenfbba09b2011-07-18 21:43:05 -07002390 * @param cell The cell it should be added to, optional
2391 * @param position The location on the screen where it was dropped, optional
2392 */
Adam Cohendcd297f2013-06-18 13:13:40 -07002393 void addAppWidgetFromDrop(PendingAddWidgetInfo info, long container, long screenId,
Adam Cohend41fbf52012-02-16 23:53:59 -08002394 int[] cell, int[] span, int[] loc) {
Adam Cohenfbba09b2011-07-18 21:43:05 -07002395 resetAddInfo();
Winson Chung3d503fb2011-07-13 17:25:49 -07002396 mPendingAddInfo.container = info.container = container;
Adam Cohendcd297f2013-06-18 13:13:40 -07002397 mPendingAddInfo.screenId = info.screenId = screenId;
Winson Chung3d503fb2011-07-13 17:25:49 -07002398 mPendingAddInfo.dropPos = loc;
Adam Cohend41fbf52012-02-16 23:53:59 -08002399 mPendingAddInfo.minSpanX = info.minSpanX;
2400 mPendingAddInfo.minSpanY = info.minSpanY;
2401
Adam Cohenfbba09b2011-07-18 21:43:05 -07002402 if (cell != null) {
Winson Chung3d503fb2011-07-13 17:25:49 -07002403 mPendingAddInfo.cellX = cell[0];
2404 mPendingAddInfo.cellY = cell[1];
Adam Cohenfbba09b2011-07-18 21:43:05 -07002405 }
Adam Cohend41fbf52012-02-16 23:53:59 -08002406 if (span != null) {
2407 mPendingAddInfo.spanX = span[0];
2408 mPendingAddInfo.spanY = span[1];
2409 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002410
Adam Cohened66b2b2012-01-23 17:28:51 -08002411 AppWidgetHostView hostView = info.boundWidget;
2412 int appWidgetId;
2413 if (hostView != null) {
2414 appWidgetId = hostView.getAppWidgetId();
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002415 addAppWidgetImpl(appWidgetId, info, hostView, info.info);
Adam Cohened66b2b2012-01-23 17:28:51 -08002416 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002417 // In this case, we either need to start an activity to get permission to bind
2418 // the widget, or we need to start an activity to configure the widget, or both.
Adam Cohened66b2b2012-01-23 17:28:51 -08002419 appWidgetId = getAppWidgetHost().allocateAppWidgetId();
Adam Cohendd70d662012-10-04 16:53:44 -07002420 Bundle options = info.bindOptions;
2421
Sunny Goyalffe83f12014-08-14 17:39:34 -07002422 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
2423 appWidgetId, info.info, options);
Adam Cohendd70d662012-10-04 16:53:44 -07002424 if (success) {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002425 addAppWidgetImpl(appWidgetId, info, null, info.info);
Michael Jurka8b805b12012-04-18 14:23:14 -07002426 } else {
Adam Cohen9d5b7d82012-05-09 18:00:44 -07002427 mPendingAddWidgetInfo = info.info;
Michael Jurka8b805b12012-04-18 14:23:14 -07002428 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND);
2429 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId);
2430 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, info.componentName);
Sunny Goyalffe83f12014-08-14 17:39:34 -07002431 mAppWidgetManager.getUser(mPendingAddWidgetInfo)
2432 .addToIntent(intent, AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE);
Adam Cohendd70d662012-10-04 16:53:44 -07002433 // TODO: we need to make sure that this accounts for the options bundle.
2434 // intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_OPTIONS, options);
Michael Jurka8b805b12012-04-18 14:23:14 -07002435 startActivityForResult(intent, REQUEST_BIND_APPWIDGET);
2436 }
Adam Cohened66b2b2012-01-23 17:28:51 -08002437 }
Adam Cohenfbba09b2011-07-18 21:43:05 -07002438 }
2439
Joe Onoratodeb98af2010-02-19 14:59:39 -08002440 void processShortcut(Intent intent) {
Kenny Guyed131872014-04-30 03:02:21 +01002441 Utilities.startActivityForResultSafely(this, intent, REQUEST_CREATE_SHORTCUT);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002442 }
2443
Winson Chung24ab2f12010-09-16 14:10:47 -07002444 void processWallpaper(Intent intent) {
2445 startActivityForResult(intent, REQUEST_PICK_WALLPAPER);
2446 }
2447
Adam Cohendcd297f2013-06-18 13:13:40 -07002448 FolderIcon addFolder(CellLayout layout, long container, final long screenId, int cellX,
Winson Chung3d503fb2011-07-13 17:25:49 -07002449 int cellY) {
Michael Jurkac9d95c52011-08-29 14:03:34 -07002450 final FolderInfo folderInfo = new FolderInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002451 folderInfo.title = getText(R.string.folder_name);
2452
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002453 // Update the model
Adam Cohendcd297f2013-06-18 13:13:40 -07002454 LauncherModel.addItemToDatabase(Launcher.this, folderInfo, container, screenId, cellX, cellY,
Winson Chung3d503fb2011-07-13 17:25:49 -07002455 false);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002456 sFolders.put(folderInfo.id, folderInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002457
2458 // Create the view
Winson Chung3d503fb2011-07-13 17:25:49 -07002459 FolderIcon newFolder =
2460 FolderIcon.fromXml(R.layout.folder_icon, this, layout, folderInfo, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07002461 mWorkspace.addInScreen(newFolder, container, screenId, cellX, cellY, 1, 1,
Winson Chung3d503fb2011-07-13 17:25:49 -07002462 isWorkspaceLocked());
Winson Chung5f8afe62013-08-12 16:19:28 -07002463 // Force measure the new folder icon
2464 CellLayout parent = mWorkspace.getParentCellLayoutForView(newFolder);
2465 parent.getShortcutsAndWidgets().measureChild(newFolder);
Adam Cohendf035382011-04-11 17:22:04 -07002466 return newFolder;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002467 }
Romain Guycbb89e42009-06-08 15:52:54 -07002468
Joe Onorato9c1289c2009-08-17 11:03:03 -04002469 void removeFolder(FolderInfo folder) {
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07002470 sFolders.remove(folder.id);
Joe Onorato9c1289c2009-08-17 11:03:03 -04002471 }
2472
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002473 protected ComponentName getWallpaperPickerComponent() {
Adam Cohen9211d422014-10-07 18:14:53 -07002474 if (mLauncherCallbacks != null) {
2475 return mLauncherCallbacks.getWallpaperPickerComponent();
2476 }
Michael Jurka7ad868b2013-12-12 15:04:25 +01002477 return new ComponentName(getPackageName(), LauncherWallpaperPickerActivity.class.getName());
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002478 }
2479
Joe Onorato2ca0ae72009-11-10 13:14:13 -08002480 /**
2481 * Registers various content observers. The current implementation registers
2482 * only a favorites observer to keep track of the favorites applications.
2483 */
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002484 private void registerContentObservers() {
2485 ContentResolver resolver = getContentResolver();
2486 resolver.registerContentObserver(LauncherProvider.CONTENT_APPWIDGET_RESET_URI,
2487 true, mWidgetObserver);
2488 }
2489
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002490 @Override
2491 public boolean dispatchKeyEvent(KeyEvent event) {
2492 if (event.getAction() == KeyEvent.ACTION_DOWN) {
2493 switch (event.getKeyCode()) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002494 case KeyEvent.KEYCODE_HOME:
Dianne Hackborn67800862009-07-24 17:15:20 -07002495 return true;
Joe Onoratobe386092009-11-17 17:32:16 -08002496 case KeyEvent.KEYCODE_VOLUME_DOWN:
Michael Jurka0a457bf2012-11-19 14:05:05 -08002497 if (isPropertyEnabled(DUMP_STATE_PROPERTY)) {
Joe Onoratobe386092009-11-17 17:32:16 -08002498 dumpState();
2499 return true;
2500 }
2501 break;
Dianne Hackborn67800862009-07-24 17:15:20 -07002502 }
2503 } else if (event.getAction() == KeyEvent.ACTION_UP) {
2504 switch (event.getKeyCode()) {
Dianne Hackborn67800862009-07-24 17:15:20 -07002505 case KeyEvent.KEYCODE_HOME:
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002506 return true;
2507 }
2508 }
2509
2510 return super.dispatchKeyEvent(event);
2511 }
2512
Joe Onorato88ec0992009-11-19 13:16:06 -08002513 @Override
2514 public void onBackPressed() {
Adam Cohen9211d422014-10-07 18:14:53 -07002515 if (mLauncherCallbacks != null && mLauncherCallbacks.handleBackPressed()) {
2516 return;
2517 }
2518
Winson Chungc93e5ae2012-07-23 20:48:26 -07002519 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07002520 if (mAppsCustomizeContent.getContentType() ==
2521 AppsCustomizePagedView.ContentType.Applications) {
2522 showWorkspace(true);
2523 } else {
2524 showOverviewMode(true);
2525 }
Adam Cohenf358a4b2013-07-23 16:47:31 -07002526 } else if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002527 mWorkspace.exitOverviewMode(true);
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002528 } else if (mWorkspace.getOpenFolder() != null) {
Adam Cohen76fc0852011-06-17 13:26:23 -07002529 Folder openFolder = mWorkspace.getOpenFolder();
2530 if (openFolder.isEditingName()) {
2531 openFolder.dismissEditingName();
2532 } else {
2533 closeFolder();
2534 }
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002535 } else {
Patrick Dubroy758a9232011-03-03 19:54:56 -08002536 mWorkspace.exitWidgetResizeMode();
2537
Patrick Dubroy94f78a52011-02-28 17:39:16 -08002538 // Back button is a no-op here, but give at least some feedback for the button press
2539 mWorkspace.showOutlinesTemporarily();
Michael Jurkaaf442092010-06-10 17:01:57 -07002540 }
Joe Onorato88ec0992009-11-19 13:16:06 -08002541 }
2542
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002543 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002544 * Re-listen when widgets are reset.
2545 */
2546 private void onAppWidgetReset() {
Michael Jurkabbbad6b2011-02-07 13:04:09 -08002547 if (mAppWidgetHost != null) {
2548 mAppWidgetHost.startListening();
2549 }
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08002550 }
2551
2552 /**
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002553 * Launches the intent referred by the clicked shortcut.
2554 *
2555 * @param v The view representing the clicked shortcut.
2556 */
2557 public void onClick(View v) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002558 // Make sure that rogue clicks don't get through while allapps is launching, or after the
2559 // view has detached (it's possible for this to happen if the view is removed mid touch).
2560 if (v.getWindowToken() == null) {
2561 return;
2562 }
2563
Winson Chung9b0b2fe2012-02-24 13:03:34 -08002564 if (!mWorkspace.isFinishedSwitchingState()) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07002565 return;
2566 }
Adam Cohen2f84ef22011-07-26 17:16:44 -07002567
Adam Cohen1697b792013-09-17 19:08:21 -07002568 if (v instanceof Workspace) {
2569 if (mWorkspace.isInOverviewMode()) {
2570 mWorkspace.exitOverviewMode(true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002571 }
2572 return;
2573 }
2574
2575 if (v instanceof CellLayout) {
2576 if (mWorkspace.isInOverviewMode()) {
Michael Jurka336fd4f2013-09-12 00:05:02 +02002577 mWorkspace.exitOverviewMode(mWorkspace.indexOfChild(v), true);
Adam Cohenf358a4b2013-07-23 16:47:31 -07002578 }
2579 }
2580
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002581 Object tag = v.getTag();
Joe Onorato0589f0f2010-02-08 13:44:00 -08002582 if (tag instanceof ShortcutInfo) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002583 onClickAppShortcut(v);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002584 } else if (tag instanceof FolderInfo) {
Adam Cohena9cf38f2011-05-02 15:36:58 -07002585 if (v instanceof FolderIcon) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002586 onClickFolderIcon(v);
Adam Cohena9cf38f2011-05-02 15:36:58 -07002587 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07002588 } else if (v == mAllAppsButton) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002589 onClickAllAppsButton(v);
Sunny Goyal508da152014-08-14 10:53:27 -07002590 } else if (tag instanceof AppInfo) {
2591 startAppShortcutOrInfoActivity(v);
Sunny Goyalff572272014-07-23 13:58:07 -07002592 } else if (tag instanceof LauncherAppWidgetInfo) {
2593 if (v instanceof PendingAppWidgetHostView) {
2594 onClickPendingWidget((PendingAppWidgetHostView) v);
2595 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08002596 }
2597 }
2598
Sunny Goyal508da152014-08-14 10:53:27 -07002599 public void onClickPagedViewIcon(View v) {
2600 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002601 if (mLauncherCallbacks != null) {
2602 mLauncherCallbacks.onClickPagedViewIcon(v);
2603 }
Sunny Goyal508da152014-08-14 10:53:27 -07002604 }
2605
Michael Jurka0e260592010-06-30 17:07:39 -07002606 public boolean onTouch(View v, MotionEvent event) {
Michael Jurka0e260592010-06-30 17:07:39 -07002607 return false;
2608 }
2609
Michael Jurkaaf442092010-06-10 17:01:57 -07002610 /**
Sunny Goyalff572272014-07-23 13:58:07 -07002611 * Event handler for the app widget view which has not fully restored.
2612 */
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002613 public void onClickPendingWidget(final PendingAppWidgetHostView v) {
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002614 if (mIsSafeModeEnabled) {
2615 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2616 return;
2617 }
2618
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002619 final LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) v.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07002620 if (v.isReadyForClickSetup()) {
Sunny Goyalff572272014-07-23 13:58:07 -07002621 int widgetId = info.appWidgetId;
2622 AppWidgetProviderInfo appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(widgetId);
2623 if (appWidgetInfo != null) {
2624 mPendingAddWidgetInfo = appWidgetInfo;
2625 mPendingAddInfo.copyFrom(info);
2626 mPendingAddWidgetId = widgetId;
2627
Sunny Goyalffe83f12014-08-14 17:39:34 -07002628 AppWidgetManagerCompat.getInstance(this).startConfigActivity(appWidgetInfo,
2629 info.appWidgetId, this, mAppWidgetHost, REQUEST_RECONFIGURE_APPWIDGET);
Sunny Goyalff572272014-07-23 13:58:07 -07002630 }
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002631 } else if (info.installProgress < 0) {
2632 // The install has not been queued
2633 final String packageName = info.providerName.getPackageName();
2634 showBrokenAppInstallDialog(packageName,
2635 new DialogInterface.OnClickListener() {
2636 public void onClick(DialogInterface dialog, int id) {
2637 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
2638 }
2639 });
2640 } else {
2641 // Download has started.
2642 final String packageName = info.providerName.getPackageName();
2643 startActivitySafely(v, LauncherModel.getMarketIntent(packageName), info);
Sunny Goyalff572272014-07-23 13:58:07 -07002644 }
2645 }
2646
2647 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002648 * Event handler for the search button
2649 *
2650 * @param v The view that was clicked.
2651 */
2652 public void onClickSearchButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002653 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2654
Amith Yamasania135ba82011-08-09 17:42:01 -07002655 onSearchRequested();
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002656 }
2657
2658 /**
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002659 * Event handler for the voice button
2660 *
2661 * @param v The view that was clicked.
2662 */
2663 public void onClickVoiceButton(View v) {
Winson Chungbb185bd2011-11-21 12:31:42 -08002664 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002665
Bjorn Bringertc459e522013-06-07 19:36:01 +01002666 startVoice();
2667 }
2668
2669 public void startVoice() {
Adam Cohen9211d422014-10-07 18:14:53 -07002670 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
2671 mLauncherCallbacks.startVoice();
2672 return;
2673 }
2674
Michael Jurkaae65ee32012-04-30 11:45:54 -07002675 try {
2676 final SearchManager searchManager =
2677 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
2678 ComponentName activityName = searchManager.getGlobalSearchActivity();
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 if (activityName != null) {
2682 intent.setPackage(activityName.getPackageName());
2683 }
Michael Jurka86a720e2012-05-09 11:23:23 -07002684 startActivity(null, intent, "onClickVoiceButton");
Michael Jurkaae65ee32012-04-30 11:45:54 -07002685 } catch (ActivityNotFoundException e) {
2686 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
Andy Huangf615f712012-06-07 13:38:04 -07002687 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Michael Jurkaae65ee32012-04-30 11:45:54 -07002688 startActivitySafely(null, intent, "onClickVoiceButton");
2689 }
Amith Yamasani6d7fe502010-11-16 09:05:07 -08002690 }
2691
2692 /**
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002693 * Event handler for the "grid" button that appears on the home screen, which
2694 * enters all apps mode.
2695 *
2696 * @param v The view that was clicked.
2697 */
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002698 protected void onClickAllAppsButton(View v) {
2699 if (LOGD) Log.d(TAG, "onClickAllAppsButton");
2700 if (isAllAppsVisible()) {
2701 showWorkspace(true);
2702 } else {
2703 showAllApps(true, AppsCustomizePagedView.ContentType.Applications, false);
2704 }
Adam Cohen9211d422014-10-07 18:14:53 -07002705 if (mLauncherCallbacks != null) {
2706 mLauncherCallbacks.onClickAllAppsButton(v);
2707 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002708 }
2709
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002710 private void showBrokenAppInstallDialog(final String packageName,
2711 DialogInterface.OnClickListener onSearchClickListener) {
Sunny Goyale03b8122014-10-08 09:55:24 -07002712 new AlertDialog.Builder(this)
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002713 .setTitle(R.string.abandoned_promises_title)
2714 .setMessage(R.string.abandoned_promise_explanation)
2715 .setPositiveButton(R.string.abandoned_search, onSearchClickListener)
2716 .setNeutralButton(R.string.abandoned_clean_this,
2717 new DialogInterface.OnClickListener() {
2718 public void onClick(DialogInterface dialog, int id) {
2719 final UserHandleCompat user = UserHandleCompat.myUserHandle();
2720 mWorkspace.removeAbandonedPromise(packageName, user);
2721 }
2722 })
2723 .create().show();
2724 return;
2725 }
2726
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002727 /**
2728 * Event handler for an app shortcut click.
2729 *
2730 * @param v The view that was clicked. Must be a tagged with a {@link ShortcutInfo}.
2731 */
Chris Wren40c5ed32014-06-24 18:24:23 -04002732 protected void onClickAppShortcut(final View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002733 if (LOGD) Log.d(TAG, "onClickAppShortcut");
2734 Object tag = v.getTag();
2735 if (!(tag instanceof ShortcutInfo)) {
2736 throw new IllegalArgumentException("Input must be a Shortcut");
2737 }
2738
2739 // Open shortcut
2740 final ShortcutInfo shortcut = (ShortcutInfo) tag;
Sunny Goyal1a745e82014-10-02 15:58:31 -07002741
2742 if (shortcut.isDisabled != 0) {
2743 int error = R.string.activity_not_available;
2744 if ((shortcut.isDisabled & ShortcutInfo.FLAG_DISABLED_SAFEMODE) != 0) {
2745 error = R.string.safemode_shortcut_error;
2746 }
2747 Toast.makeText(this, error, Toast.LENGTH_SHORT).show();
2748 return;
2749 }
2750
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002751 final Intent intent = shortcut.intent;
2752
2753 // Check for special shortcuts
2754 if (intent.getComponent() != null) {
2755 final String shortcutClass = intent.getComponent().getClassName();
2756
2757 if (shortcutClass.equals(MemoryDumpActivity.class.getName())) {
2758 MemoryDumpActivity.startDump(this);
2759 return;
2760 } else if (shortcutClass.equals(ToggleWeightWatcher.class.getName())) {
2761 toggleShowWeightWatcher();
2762 return;
2763 }
2764 }
2765
Chris Wren40c5ed32014-06-24 18:24:23 -04002766 // Check for abandoned promise
Sunny Goyal34942622014-08-29 17:20:55 -07002767 if ((v instanceof BubbleTextView)
2768 && shortcut.isPromise()
2769 && !shortcut.hasStatusFlag(ShortcutInfo.FLAG_INSTALL_SESSION_ACTIVE)) {
Sunny Goyale7b8cd92014-08-27 14:04:33 -07002770 showBrokenAppInstallDialog(
Sunny Goyal34942622014-08-29 17:20:55 -07002771 shortcut.getTargetComponent().getPackageName(),
Chris Wren40c5ed32014-06-24 18:24:23 -04002772 new DialogInterface.OnClickListener() {
2773 public void onClick(DialogInterface dialog, int id) {
Sunny Goyal508da152014-08-14 10:53:27 -07002774 startAppShortcutOrInfoActivity(v);
Chris Wren40c5ed32014-06-24 18:24:23 -04002775 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002776 });
Chris Wren40c5ed32014-06-24 18:24:23 -04002777 return;
2778 }
2779
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002780 // Start activities
Sunny Goyal508da152014-08-14 10:53:27 -07002781 startAppShortcutOrInfoActivity(v);
Adam Cohen9211d422014-10-07 18:14:53 -07002782
2783 if (mLauncherCallbacks != null) {
2784 mLauncherCallbacks.onClickAppShortcut(v);
2785 }
Chris Wren40c5ed32014-06-24 18:24:23 -04002786 }
2787
Sunny Goyal508da152014-08-14 10:53:27 -07002788 private void startAppShortcutOrInfoActivity(View v) {
Chris Wren40c5ed32014-06-24 18:24:23 -04002789 Object tag = v.getTag();
Sunny Goyal508da152014-08-14 10:53:27 -07002790 final ShortcutInfo shortcut;
2791 final Intent intent;
2792 if (tag instanceof ShortcutInfo) {
2793 shortcut = (ShortcutInfo) tag;
2794 intent = shortcut.intent;
2795 int[] pos = new int[2];
2796 v.getLocationOnScreen(pos);
2797 intent.setSourceBounds(new Rect(pos[0], pos[1],
2798 pos[0] + v.getWidth(), pos[1] + v.getHeight()));
Chris Wren40c5ed32014-06-24 18:24:23 -04002799
Sunny Goyal508da152014-08-14 10:53:27 -07002800 } else if (tag instanceof AppInfo) {
2801 shortcut = null;
2802 intent = ((AppInfo) tag).intent;
2803 } else {
2804 throw new IllegalArgumentException("Input must be a Shortcut or AppInfo");
2805 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002806
2807 boolean success = startActivitySafely(v, intent, tag);
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002808 mStats.recordLaunch(intent, shortcut);
2809
2810 if (success && v instanceof BubbleTextView) {
2811 mWaitingForResume = (BubbleTextView) v;
2812 mWaitingForResume.setStayPressed(true);
2813 }
2814 }
2815
2816 /**
2817 * Event handler for a folder icon click.
2818 *
2819 * @param v The view that was clicked. Must be an instance of {@link FolderIcon}.
2820 */
2821 protected void onClickFolderIcon(View v) {
2822 if (LOGD) Log.d(TAG, "onClickFolder");
2823 if (!(v instanceof FolderIcon)){
2824 throw new IllegalArgumentException("Input must be a FolderIcon");
2825 }
2826
2827 FolderIcon folderIcon = (FolderIcon) v;
2828 final FolderInfo info = folderIcon.getFolderInfo();
2829 Folder openFolder = mWorkspace.getFolderForTag(info);
2830
2831 // If the folder info reports that the associated folder is open, then verify that
2832 // it is actually opened. There have been a few instances where this gets out of sync.
2833 if (info.opened && openFolder == null) {
2834 Log.d(TAG, "Folder info marked as open, but associated folder is not open. Screen: "
2835 + info.screenId + " (" + info.cellX + ", " + info.cellY + ")");
2836 info.opened = false;
2837 }
2838
2839 if (!info.opened && !folderIcon.getFolder().isDestroyed()) {
2840 // Close any open folder
2841 closeFolder();
2842 // Open the requested folder
2843 openFolder(folderIcon);
2844 } else {
2845 // Find the open folder...
2846 int folderScreen;
2847 if (openFolder != null) {
2848 folderScreen = mWorkspace.getPageForView(openFolder);
2849 // .. and close it
2850 closeFolder(openFolder);
2851 if (folderScreen != mWorkspace.getCurrentPage()) {
2852 // Close any folder open on the current screen
2853 closeFolder();
2854 // Pull the folder onto this screen
2855 openFolder(folderIcon);
2856 }
2857 }
2858 }
Adam Cohen9211d422014-10-07 18:14:53 -07002859
2860 if (mLauncherCallbacks != null) {
2861 mLauncherCallbacks.onClickFolderIcon(v);
2862 }
Michael Jurka5130e402011-10-13 04:55:35 -07002863 }
2864
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002865 /**
2866 * Event handler for the (Add) Widgets button that appears after a long press
2867 * on the home screen.
2868 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002869 protected void onClickAddWidgetButton(View view) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002870 if (LOGD) Log.d(TAG, "onClickAddWidgetButton");
Sunny Goyal9b4b0812014-10-08 10:47:28 -07002871 if (mIsSafeModeEnabled) {
2872 Toast.makeText(this, R.string.safemode_widget_error, Toast.LENGTH_SHORT).show();
2873 } else {
2874 showAllApps(true, AppsCustomizePagedView.ContentType.Widgets, true);
2875 if (mLauncherCallbacks != null) {
2876 mLauncherCallbacks.onClickAddWidgetButton(view);
2877 }
Adam Cohen9211d422014-10-07 18:14:53 -07002878 }
Sandeep Siddharthad8058372014-01-28 10:41:15 -08002879 }
2880
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002881 /**
2882 * Event handler for the wallpaper picker button that appears after a long press
2883 * on the home screen.
2884 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002885 protected void onClickWallpaperPicker(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002886 if (LOGD) Log.d(TAG, "onClickWallpaperPicker");
2887 final Intent pickWallpaper = new Intent(Intent.ACTION_SET_WALLPAPER);
2888 pickWallpaper.setComponent(getWallpaperPickerComponent());
2889 startActivityForResult(pickWallpaper, REQUEST_PICK_WALLPAPER);
Adam Cohen9211d422014-10-07 18:14:53 -07002890
2891 if (mLauncherCallbacks != null) {
2892 mLauncherCallbacks.onClickWallpaperPicker(v);
2893 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002894 }
2895
2896 /**
2897 * Event handler for a click on the settings button that appears after a long press
2898 * on the home screen.
2899 */
Anjali Koppal7b168a12014-03-04 17:16:11 -08002900 protected void onClickSettingsButton(View v) {
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002901 if (LOGD) Log.d(TAG, "onClickSettingsButton");
Adam Cohen9211d422014-10-07 18:14:53 -07002902 if (mLauncherCallbacks != null) {
2903 mLauncherCallbacks.onClickSettingsButton(v);
2904 }
Sandeep Siddharthaa9420302014-02-12 18:24:31 -08002905 }
2906
Michael Jurka5130e402011-10-13 04:55:35 -07002907 public void onTouchDownAllAppsButton(View v) {
Michael Jurka2a552322011-10-11 15:22:05 -07002908 // Provide the same haptic feedback that the system offers for virtual keys.
2909 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
Michael Jurka2c3af5f2010-08-03 13:53:20 -07002910 }
2911
Adam Cohen61f560d2013-09-30 15:58:20 -07002912 public void performHapticFeedbackOnTouchDown(View v) {
2913 // Provide the same haptic feedback that the system offers for virtual keys.
2914 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2915 }
2916
2917 public View.OnTouchListener getHapticFeedbackTouchListener() {
2918 if (mHapticFeedbackTouchListener == null) {
2919 mHapticFeedbackTouchListener = new View.OnTouchListener() {
2920 @Override
2921 public boolean onTouch(View v, MotionEvent event) {
2922 if ((event.getAction() & MotionEvent.ACTION_MASK) == MotionEvent.ACTION_DOWN) {
2923 v.performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY);
2924 }
2925 return false;
2926 }
2927 };
2928 }
2929 return mHapticFeedbackTouchListener;
2930 }
2931
Adam Cohen9211d422014-10-07 18:14:53 -07002932 public void onDragStarted(View view) {
2933 if (isOnCustomContent()) {
2934 // Custom content screen doesn't participate in drag and drop. If on custom
2935 // content screen, move to default.
2936 moveWorkspaceToDefaultScreen();
2937 }
2938
2939 if (mLauncherCallbacks != null) {
2940 mLauncherCallbacks.onDragStarted(view);
2941 }
2942 }
Anjali Koppal62d18ed2014-03-10 17:04:03 -07002943
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002944 /**
2945 * Called when the user stops interacting with the launcher.
2946 * This implies that the user is now on the homescreen and is not doing housekeeping.
2947 */
Adam Cohen9211d422014-10-07 18:14:53 -07002948 protected void onInteractionEnd() {
2949 if (mLauncherCallbacks != null) {
2950 mLauncherCallbacks.onInteractionEnd();
2951 }
2952 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002953
2954 /**
2955 * Called when the user starts interacting with the launcher.
2956 * The possible interactions are:
2957 * - open all apps
2958 * - reorder an app shortcut, or a widget
2959 * - open the overview mode.
2960 * This is a good time to stop doing things that only make sense
2961 * when the user is on the homescreen and not doing housekeeping.
2962 */
Adam Cohen9211d422014-10-07 18:14:53 -07002963 protected void onInteractionBegin() {
2964 if (mLauncherCallbacks != null) {
2965 mLauncherCallbacks.onInteractionBegin();
2966 }
2967 }
Sandeep Siddharthaab2d9d72013-09-17 13:18:24 -07002968
Kenny Guyf07af7b2014-07-31 11:39:16 +01002969 void startApplicationDetailsActivity(ComponentName componentName, UserHandleCompat user) {
Patrick Dubroybc6840b2010-09-01 11:54:27 -07002970 String packageName = componentName.getPackageName();
Kenny Guyf07af7b2014-07-31 11:39:16 +01002971 try {
2972 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
2973 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
2974 launcherApps.showAppDetailsForProfile(componentName, user);
2975 } catch (SecurityException e) {
2976 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2977 Log.e(TAG, "Launcher does not have permission to launch settings");
2978 } catch (ActivityNotFoundException e) {
2979 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
2980 Log.e(TAG, "Unable to launch settings");
2981 }
Patrick Dubroy4ed62782010-08-17 15:11:18 -07002982 }
2983
Michael Jurka1e2f4652013-07-08 18:03:46 -07002984 // returns true if the activity was started
Kenny Guyd31df542014-06-30 15:12:11 +01002985 boolean startApplicationUninstallActivity(ComponentName componentName, int flags,
2986 UserHandleCompat user) {
Michael Jurkaeadbfc52013-09-04 00:45:37 +02002987 if ((flags & AppInfo.DOWNLOADED_FLAG) == 0) {
Patrick Dubroy5539af72010-09-07 15:22:01 -07002988 // System applications cannot be installed. For now, show a toast explaining that.
2989 // We may give them the option of disabling apps this way.
2990 int messageId = R.string.uninstall_system_app_text;
2991 Toast.makeText(this, messageId, Toast.LENGTH_SHORT).show();
Michael Jurka1e2f4652013-07-08 18:03:46 -07002992 return false;
Patrick Dubroy5539af72010-09-07 15:22:01 -07002993 } else {
Michael Jurka1e2f4652013-07-08 18:03:46 -07002994 String packageName = componentName.getPackageName();
2995 String className = componentName.getClassName();
Patrick Dubroy5539af72010-09-07 15:22:01 -07002996 Intent intent = new Intent(
2997 Intent.ACTION_DELETE, Uri.fromParts("package", packageName, className));
Winson Chungdff8ebb2011-09-08 17:25:31 -07002998 intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
2999 Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
Kenny Guyd31df542014-06-30 15:12:11 +01003000 if (user != null) {
3001 user.addToIntent(intent, Intent.EXTRA_USER);
3002 }
Patrick Dubroy5539af72010-09-07 15:22:01 -07003003 startActivity(intent);
Michael Jurka1e2f4652013-07-08 18:03:46 -07003004 return true;
Patrick Dubroy5539af72010-09-07 15:22:01 -07003005 }
Patrick Dubroybc6840b2010-09-01 11:54:27 -07003006 }
3007
Michael Jurka86a720e2012-05-09 11:23:23 -07003008 boolean startActivity(View v, Intent intent, Object tag) {
Kenny Guyb6cc40b2014-05-13 15:58:58 +01003009 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003010 try {
Winson Chung2672ff92012-05-04 16:22:30 -07003011 // Only launch using the new animation if the shortcut has not opted out (this is a
3012 // private contract between launcher and may be ignored in the future).
3013 boolean useLaunchAnimation = (v != null) &&
3014 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
Kenny Guyed131872014-04-30 03:02:21 +01003015 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
3016 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyed131872014-04-30 03:02:21 +01003017
Kenny Guy1317e2d2014-05-08 18:52:50 +01003018 UserHandleCompat user = null;
3019 if (intent.hasExtra(AppInfo.EXTRA_PROFILE)) {
3020 long serialNumber = intent.getLongExtra(AppInfo.EXTRA_PROFILE, -1);
3021 user = userManager.getUserForSerialNumber(serialNumber);
3022 }
3023
3024 Bundle optsBundle = null;
Winson Chung2672ff92012-05-04 16:22:30 -07003025 if (useLaunchAnimation) {
Kenny Guyd794a3f2014-09-16 15:17:58 +01003026 ActivityOptions opts = Utilities.isLmpOrAbove() ?
Adam Cohen4da294d2014-07-28 10:56:19 -07003027 ActivityOptions.makeCustomAnimation(this, R.anim.task_open_enter, R.anim.no_anim) :
3028 ActivityOptions.makeScaleUpAnimation(v, 0, 0, v.getMeasuredWidth(), v.getMeasuredHeight());
Adam Cohen6ea3b112014-06-11 11:38:49 -07003029 optsBundle = opts.toBundle();
3030 }
Kenny Guy1317e2d2014-05-08 18:52:50 +01003031
3032 if (user == null || user.equals(UserHandleCompat.myUserHandle())) {
3033 // Could be launching some bookkeeping activity
3034 startActivity(intent, optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003035 } else {
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003036 // TODO Component can be null when shortcuts are supported for secondary user
Kenny Guyc2bd8102014-06-30 12:30:31 +01003037 launcherApps.startActivityForProfile(intent.getComponent(), user,
3038 intent.getSourceBounds(), optsBundle);
Winson Chungc7450e32012-04-17 17:34:08 -07003039 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003040 return true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003041 } catch (SecurityException e) {
3042 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Joe Onoratoa30ce8e2009-11-11 08:16:49 -08003043 Log.e(TAG, "Launcher does not have the permission to launch " + intent +
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003044 ". Make sure to create a MAIN intent-filter for the corresponding activity " +
Joe Onoratof984e852010-03-25 09:47:45 -07003045 "or use the exported attribute for this activity. "
3046 + "tag="+ tag + " intent=" + intent, e);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003047 }
Michael Jurkaddd62e92011-02-16 17:49:14 -08003048 return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003049 }
Winson Chungaafa03c2010-06-11 17:34:16 -07003050
Michael Jurka86a720e2012-05-09 11:23:23 -07003051 boolean startActivitySafely(View v, Intent intent, Object tag) {
3052 boolean success = false;
Sunny Goyalc5c60ad2014-07-14 12:02:01 -07003053 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
3054 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
3055 return false;
3056 }
Michael Jurka86a720e2012-05-09 11:23:23 -07003057 try {
3058 success = startActivity(v, intent, tag);
3059 } catch (ActivityNotFoundException e) {
3060 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
3061 Log.e(TAG, "Unable to launch. tag=" + tag + " intent=" + intent, e);
3062 }
3063 return success;
3064 }
3065
Adam Cohen268c4752012-06-06 17:47:33 -07003066 /**
3067 * This method draws the FolderIcon to an ImageView and then adds and positions that ImageView
3068 * in the DragLayer in the exact absolute location of the original FolderIcon.
3069 */
3070 private void copyFolderIconToImage(FolderIcon fi) {
3071 final int width = fi.getMeasuredWidth();
3072 final int height = fi.getMeasuredHeight();
3073
3074 // Lazy load ImageView, Bitmap and Canvas
3075 if (mFolderIconImageView == null) {
3076 mFolderIconImageView = new ImageView(this);
3077 }
3078 if (mFolderIconBitmap == null || mFolderIconBitmap.getWidth() != width ||
3079 mFolderIconBitmap.getHeight() != height) {
3080 mFolderIconBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
3081 mFolderIconCanvas = new Canvas(mFolderIconBitmap);
3082 }
3083
3084 DragLayer.LayoutParams lp;
3085 if (mFolderIconImageView.getLayoutParams() instanceof DragLayer.LayoutParams) {
3086 lp = (DragLayer.LayoutParams) mFolderIconImageView.getLayoutParams();
3087 } else {
3088 lp = new DragLayer.LayoutParams(width, height);
3089 }
3090
Adam Cohen307fe232012-08-16 17:55:58 -07003091 // The layout from which the folder is being opened may be scaled, adjust the starting
3092 // view size by this scale factor.
3093 float scale = mDragLayer.getDescendantRectRelativeToSelf(fi, mRectForFolderAnimation);
Adam Cohen268c4752012-06-06 17:47:33 -07003094 lp.customPosition = true;
3095 lp.x = mRectForFolderAnimation.left;
3096 lp.y = mRectForFolderAnimation.top;
Adam Cohen307fe232012-08-16 17:55:58 -07003097 lp.width = (int) (scale * width);
3098 lp.height = (int) (scale * height);
Adam Cohen268c4752012-06-06 17:47:33 -07003099
3100 mFolderIconCanvas.drawColor(0, PorterDuff.Mode.CLEAR);
3101 fi.draw(mFolderIconCanvas);
3102 mFolderIconImageView.setImageBitmap(mFolderIconBitmap);
Adam Cohenfb91f302012-06-11 15:45:18 -07003103 if (fi.getFolder() != null) {
3104 mFolderIconImageView.setPivotX(fi.getFolder().getPivotXForIconAnimation());
3105 mFolderIconImageView.setPivotY(fi.getFolder().getPivotYForIconAnimation());
Adam Cohen8ec23032012-06-08 14:46:22 -07003106 }
Adam Cohen268c4752012-06-06 17:47:33 -07003107 // Just in case this image view is still in the drag layer from a previous animation,
3108 // we remove it and re-add it.
3109 if (mDragLayer.indexOfChild(mFolderIconImageView) != -1) {
3110 mDragLayer.removeView(mFolderIconImageView);
3111 }
3112 mDragLayer.addView(mFolderIconImageView, lp);
Adam Cohenfb91f302012-06-11 15:45:18 -07003113 if (fi.getFolder() != null) {
3114 fi.getFolder().bringToFront();
Adam Cohen8ec23032012-06-08 14:46:22 -07003115 }
Adam Cohen268c4752012-06-06 17:47:33 -07003116 }
3117
Adam Cohen2801caf2011-05-13 20:57:39 -07003118 private void growAndFadeOutFolderIcon(FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003119 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003120 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 0);
3121 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.5f);
3122 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.5f);
3123
Adam Cohenc51934b2011-07-26 21:07:43 -07003124 FolderInfo info = (FolderInfo) fi.getTag();
3125 if (info.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3126 CellLayout cl = (CellLayout) fi.getParent().getParent();
Winson Chunge50adee2011-08-11 16:12:00 -07003127 CellLayout.LayoutParams lp = (CellLayout.LayoutParams) fi.getLayoutParams();
3128 cl.setFolderLeaveBehindCell(lp.cellX, lp.cellY);
Adam Cohenc51934b2011-07-26 21:07:43 -07003129 }
3130
Adam Cohen268c4752012-06-06 17:47:33 -07003131 // Push an ImageView copy of the FolderIcon into the DragLayer and hide the original
3132 copyFolderIconToImage(fi);
3133 fi.setVisibility(View.INVISIBLE);
3134
Michael Jurka2ecf9952012-06-18 12:52:28 -07003135 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003136 scaleX, scaleY);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003137 if (Utilities.isLmpOrAbove()) {
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003138 oa.setInterpolator(new LogDecelerateInterpolator(100, 0));
3139 }
3140 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohen2801caf2011-05-13 20:57:39 -07003141 oa.start();
3142 }
3143
Adam Cohen268c4752012-06-06 17:47:33 -07003144 private void shrinkAndFadeInFolderIcon(final FolderIcon fi) {
Adam Cohen9932a9b2011-08-02 22:14:07 -07003145 if (fi == null) return;
Adam Cohen2801caf2011-05-13 20:57:39 -07003146 PropertyValuesHolder alpha = PropertyValuesHolder.ofFloat("alpha", 1.0f);
3147 PropertyValuesHolder scaleX = PropertyValuesHolder.ofFloat("scaleX", 1.0f);
3148 PropertyValuesHolder scaleY = PropertyValuesHolder.ofFloat("scaleY", 1.0f);
3149
Adam Cohen268c4752012-06-06 17:47:33 -07003150 final CellLayout cl = (CellLayout) fi.getParent().getParent();
Adam Cohenc51934b2011-07-26 21:07:43 -07003151
Adam Cohen268c4752012-06-06 17:47:33 -07003152 // We remove and re-draw the FolderIcon in-case it has changed
3153 mDragLayer.removeView(mFolderIconImageView);
3154 copyFolderIconToImage(fi);
Michael Jurka2ecf9952012-06-18 12:52:28 -07003155 ObjectAnimator oa = LauncherAnimUtils.ofPropertyValuesHolder(mFolderIconImageView, alpha,
Adam Cohen268c4752012-06-06 17:47:33 -07003156 scaleX, scaleY);
Adam Cohenc4fe9ea2014-08-18 18:54:10 -07003157 oa.setDuration(getResources().getInteger(R.integer.config_folderExpandDuration));
Adam Cohenc51934b2011-07-26 21:07:43 -07003158 oa.addListener(new AnimatorListenerAdapter() {
3159 @Override
3160 public void onAnimationEnd(Animator animation) {
Adam Cohen268c4752012-06-06 17:47:33 -07003161 if (cl != null) {
3162 cl.clearFolderLeaveBehind();
3163 // Remove the ImageView copy of the FolderIcon and make the original visible.
3164 mDragLayer.removeView(mFolderIconImageView);
3165 fi.setVisibility(View.VISIBLE);
Adam Cohenc51934b2011-07-26 21:07:43 -07003166 }
3167 }
3168 });
Adam Cohen2801caf2011-05-13 20:57:39 -07003169 oa.start();
3170 }
3171
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003172 /**
Michael Jurka774bd372010-10-22 13:40:50 -07003173 * Opens the user folder described by the specified tag. The opening of the folder
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003174 * is animated relative to the specified View. If the View is null, no animation
3175 * is played.
3176 *
3177 * @param folderInfo The FolderInfo describing the folder to open.
3178 */
Adam Cohena9cf38f2011-05-02 15:36:58 -07003179 public void openFolder(FolderIcon folderIcon) {
Adam Cohenfb91f302012-06-11 15:45:18 -07003180 Folder folder = folderIcon.getFolder();
Adam Cohena9cf38f2011-05-02 15:36:58 -07003181 FolderInfo info = folder.mInfo;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003182
Adam Cohena9cf38f2011-05-02 15:36:58 -07003183 info.opened = true;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003184
Adam Cohen4554ee12011-08-03 16:13:21 -07003185 // Just verify that the folder hasn't already been added to the DragLayer.
3186 // There was a one-off crash where the folder had a parent already.
3187 if (folder.getParent() == null) {
3188 mDragLayer.addView(folder);
3189 mDragController.addDropTarget((DropTarget) folder);
3190 } else {
3191 Log.w(TAG, "Opening folder (" + folder + ") which already has a parent (" +
3192 folder.getParent() + ").");
3193 }
Adam Cohena9cf38f2011-05-02 15:36:58 -07003194 folder.animateOpen();
Adam Cohen268c4752012-06-06 17:47:33 -07003195 growAndFadeOutFolderIcon(folderIcon);
Winson Chung83ca4802013-04-12 15:10:52 -07003196
3197 // Notify the accessibility manager that this folder "window" has appeared and occluded
3198 // the workspace items
3199 folder.sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
3200 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_CONTENT_CHANGED);
Adam Cohen4554ee12011-08-03 16:13:21 -07003201 }
3202
3203 public void closeFolder() {
Adam Cohen37c717f2013-11-26 11:50:15 -08003204 Folder folder = mWorkspace != null ? mWorkspace.getOpenFolder() : null;
Adam Cohen4554ee12011-08-03 16:13:21 -07003205 if (folder != null) {
Adam Cohenac56cff2011-09-28 20:45:37 -07003206 if (folder.isEditingName()) {
3207 folder.dismissEditingName();
3208 }
Adam Cohen4554ee12011-08-03 16:13:21 -07003209 closeFolder(folder);
3210 }
3211 }
3212
3213 void closeFolder(Folder folder) {
3214 folder.getInfo().opened = false;
3215
3216 ViewGroup parent = (ViewGroup) folder.getParent().getParent();
3217 if (parent != null) {
3218 FolderIcon fi = (FolderIcon) mWorkspace.getViewForTag(folder.mInfo);
3219 shrinkAndFadeInFolderIcon(fi);
3220 }
3221 folder.animateClosed();
Winson Chung83ca4802013-04-12 15:10:52 -07003222
3223 // Notify the accessibility manager that this folder "window" has disappeard and no
3224 // longer occludeds the workspace items
3225 getDragLayer().sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003226 }
3227
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003228 public boolean onLongClick(View v) {
Winson Chung36a62fe2012-05-06 18:04:42 -07003229 if (!isDraggingEnabled()) return false;
3230 if (isWorkspaceLocked()) return false;
3231 if (mState != State.WORKSPACE) return false;
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003232
Adam Cohen1697b792013-09-17 19:08:21 -07003233 if (v instanceof Workspace) {
3234 if (!mWorkspace.isInOverviewMode()) {
Adam Cohen93c97562013-09-26 13:48:01 -07003235 if (mWorkspace.enterOverviewMode()) {
3236 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3237 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
3238 return true;
3239 } else {
3240 return false;
3241 }
Adam Cohenaa4c8c12014-06-06 11:52:52 -07003242 } else {
3243 return false;
Adam Cohen1697b792013-09-17 19:08:21 -07003244 }
Adam Cohen1697b792013-09-17 19:08:21 -07003245 }
3246
Adam Cohene0aaa0d2014-05-12 12:44:22 -07003247 CellLayout.CellInfo longClickCellInfo = null;
3248 View itemUnderLongClick = null;
3249 if (v.getTag() instanceof ItemInfo) {
3250 ItemInfo info = (ItemInfo) v.getTag();
3251 longClickCellInfo = new CellLayout.CellInfo(v, info);;
3252 itemUnderLongClick = longClickCellInfo.cell;
3253 resetAddInfo();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003254 }
3255
Winson Chung3d503fb2011-07-13 17:25:49 -07003256 // The hotseat touch handling does not go through Workspace, and we always allow long press
3257 // on hotseat items.
Dan Sandlere26d0942014-01-13 14:30:14 -05003258 final boolean inHotseat = isHotseatLayout(v);
3259 boolean allowLongPress = inHotseat || mWorkspace.allowLongPress();
Winson Chung3d503fb2011-07-13 17:25:49 -07003260 if (allowLongPress && !mDragController.isDragging()) {
Michael Jurka0280c3b2010-09-17 15:00:07 -07003261 if (itemUnderLongClick == null) {
Adam Cohend3ecce92013-09-16 14:58:00 -07003262 // User long pressed on empty space
3263 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
3264 HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
Adam Cohend3ecce92013-09-16 14:58:00 -07003265 if (mWorkspace.isInOverviewMode()) {
3266 mWorkspace.startReordering(v);
3267 } else {
3268 mWorkspace.enterOverviewMode();
Adam Cohendedbd962013-07-11 14:21:49 -07003269 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003270 } else {
Dan Sandlere26d0942014-01-13 14:30:14 -05003271 final boolean isAllAppsButton = inHotseat && isAllAppsButtonRank(
3272 mHotseat.getOrderInHotseat(
3273 longClickCellInfo.cellX,
3274 longClickCellInfo.cellY));
3275 if (!(itemUnderLongClick instanceof Folder || isAllAppsButton)) {
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003276 // User long pressed on an item
Michael Jurka0280c3b2010-09-17 15:00:07 -07003277 mWorkspace.startDrag(longClickCellInfo);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08003278 }
3279 }
3280 }
3281 return true;
3282 }
3283
Winson Chung3d503fb2011-07-13 17:25:49 -07003284 boolean isHotseatLayout(View layout) {
3285 return mHotseat != null && layout != null &&
3286 (layout instanceof CellLayout) && (layout == mHotseat.getLayout());
3287 }
Romain Guy1fbc1c82009-11-09 20:43:08 -08003288
Winson Chung3d503fb2011-07-13 17:25:49 -07003289 /**
3290 * Returns the CellLayout of the specified container at the specified screen.
3291 */
Adam Cohendcd297f2013-06-18 13:13:40 -07003292 CellLayout getCellLayout(long container, long screenId) {
Winson Chung3d503fb2011-07-13 17:25:49 -07003293 if (container == LauncherSettings.Favorites.CONTAINER_HOTSEAT) {
3294 if (mHotseat != null) {
3295 return mHotseat.getLayout();
3296 } else {
3297 return null;
Romain Guye6b8e2f2009-11-10 11:56:55 -08003298 }
Winson Chung3d503fb2011-07-13 17:25:49 -07003299 } else {
Adam Cohendcd297f2013-06-18 13:13:40 -07003300 return (CellLayout) mWorkspace.getScreenWithId(screenId);
Romain Guya6abce82009-11-10 02:54:41 -08003301 }
3302 }
3303
Daniel Sandler843e8602010-06-07 14:59:01 -04003304 public boolean isAllAppsVisible() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003305 return (mState == State.APPS_CUSTOMIZE) || (mOnResumeState == State.APPS_CUSTOMIZE);
Joe Onoratofb0ca672009-09-14 17:55:46 -04003306 }
3307
Craig Mautner360310b2012-10-26 15:13:08 -07003308 private void setWorkspaceBackground(boolean workspace) {
3309 mLauncherView.setBackground(workspace ?
Michael Jurka496fefb2013-05-06 15:39:11 +02003310 mWorkspaceBackgroundDrawable : null);
Craig Mautner360310b2012-10-26 15:13:08 -07003311 }
3312
Selim Cinek3a8a8f72014-01-16 10:38:38 -08003313 protected void changeWallpaperVisiblity(boolean visible) {
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003314 int wpflags = visible ? WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER : 0;
3315 int curflags = getWindow().getAttributes().flags
3316 & WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER;
3317 if (wpflags != curflags) {
3318 getWindow().setFlags(wpflags, WindowManager.LayoutParams.FLAG_SHOW_WALLPAPER);
3319 }
Craig Mautner360310b2012-10-26 15:13:08 -07003320 setWorkspaceBackground(visible);
Dianne Hackbornbb003a52011-08-30 14:40:07 -07003321 }
3322
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003323 private void dispatchOnLauncherTransitionPrepare(View v, boolean animated, boolean toWorkspace) {
3324 if (v instanceof LauncherTransitionable) {
3325 ((LauncherTransitionable) v).onLauncherTransitionPrepare(this, animated, toWorkspace);
3326 }
3327 }
3328
Michael Jurkabed61d22012-02-14 22:51:29 -08003329 private void dispatchOnLauncherTransitionStart(View v, boolean animated, boolean toWorkspace) {
3330 if (v instanceof LauncherTransitionable) {
3331 ((LauncherTransitionable) v).onLauncherTransitionStart(this, animated, toWorkspace);
3332 }
Winson Chung70442722012-02-10 15:43:22 -08003333
3334 // Update the workspace transition step as well
3335 dispatchOnLauncherTransitionStep(v, 0f);
3336 }
3337
3338 private void dispatchOnLauncherTransitionStep(View v, float t) {
3339 if (v instanceof LauncherTransitionable) {
3340 ((LauncherTransitionable) v).onLauncherTransitionStep(this, t);
3341 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003342 }
3343
3344 private void dispatchOnLauncherTransitionEnd(View v, boolean animated, boolean toWorkspace) {
3345 if (v instanceof LauncherTransitionable) {
3346 ((LauncherTransitionable) v).onLauncherTransitionEnd(this, animated, toWorkspace);
3347 }
Winson Chung70442722012-02-10 15:43:22 -08003348
3349 // Update the workspace transition step as well
3350 dispatchOnLauncherTransitionStep(v, 1f);
Michael Jurkabed61d22012-02-14 22:51:29 -08003351 }
3352
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003353 /**
Michael Jurkab3e22d92011-10-31 15:58:33 -07003354 * Things to test when changing the following seven functions.
3355 * - Home from workspace
3356 * - from center screen
3357 * - from other screens
3358 * - Home from all apps
3359 * - from center screen
3360 * - from other screens
3361 * - Back from all apps
3362 * - from center screen
3363 * - from other screens
3364 * - Launch app from workspace and quit
3365 * - with back
3366 * - with home
3367 * - Launch app from all apps and quit
3368 * - with back
3369 * - with home
3370 * - Go to a screen that's not the default, then all
3371 * apps, and launch and app, and go back
3372 * - with back
3373 * -with home
3374 * - On workspace, long press power and go back
3375 * - with back
3376 * - with home
3377 * - On all apps, long press power and go back
3378 * - with back
3379 * - with home
3380 * - On workspace, power off
3381 * - On all apps, power off
3382 * - Launch an app and turn off the screen while in that app
3383 * - Go back with home key
3384 * - Go back with back key TODO: make this not go to workspace
3385 * - From all apps
3386 * - From workspace
3387 * - Enter and exit car mode (becuase it causes an extra configuration changed)
3388 * - From all apps
3389 * - From the center workspace
3390 * - From another workspace
3391 */
3392
3393 /**
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003394 * Zoom the camera out from the workspace to reveal 'toView'.
3395 * Assumes that the view to show is anchored at either the very top or very bottom
3396 * of the screen.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003397 */
Michael Jurkabed61d22012-02-14 22:51:29 -08003398 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded) {
Winson Chungc58497e2013-09-03 17:48:37 -07003399 AppsCustomizePagedView.ContentType contentType = mAppsCustomizeContent.getContentType();
3400 showAppsCustomizeHelper(animated, springLoaded, contentType);
3401 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003402
Winson Chungc58497e2013-09-03 17:48:37 -07003403 private void showAppsCustomizeHelper(final boolean animated, final boolean springLoaded,
3404 final AppsCustomizePagedView.ContentType contentType) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003405 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003406 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003407 mStateAnimation.cancel();
3408 mStateAnimation = null;
3409 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003410
Kenny Guyd794a3f2014-09-16 15:17:58 +01003411 boolean material = Utilities.isLmpOrAbove();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003412
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003413 final Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003414
Winson Chungf0ea4d32011-06-06 14:27:16 -07003415 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomInTime);
3416 final int fadeDuration = res.getInteger(R.integer.config_appsCustomizeFadeInTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003417 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeRevealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003418 final int itemsAlphaStagger =
3419 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003420
Winson Chungf0ea4d32011-06-06 14:27:16 -07003421 final float scale = (float) res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
Michael Jurkabed61d22012-02-14 22:51:29 -08003422 final View fromView = mWorkspace;
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003423 final AppsCustomizeTabHost toView = mAppsCustomizeTabHost;
Patrick Dubroy558654c2010-07-23 16:48:11 -07003424
Adam Cohen2854d252014-08-27 16:04:07 -07003425 final ArrayList<View> layerViews = new ArrayList<View>();
3426
Adam Cohen6c5891a2014-07-09 23:53:15 -07003427 Workspace.State workspaceState = contentType == AppsCustomizePagedView.ContentType.Widgets ?
3428 Workspace.State.OVERVIEW_HIDDEN : Workspace.State.NORMAL_HIDDEN;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003429 Animator workspaceAnim =
Adam Cohen2854d252014-08-27 16:04:07 -07003430 mWorkspace.getChangeStateAnimation(workspaceState, animated, layerViews);
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08003431 if (!LauncherAppState.isDisableAllApps()
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08003432 || contentType == AppsCustomizePagedView.ContentType.Widgets) {
3433 // Set the content type for the all apps/widgets space
Winson Chungc58497e2013-09-03 17:48:37 -07003434 mAppsCustomizeTabHost.setContentTypeImmediate(contentType);
3435 }
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003436
Adam Cohena38dc902014-09-07 17:48:55 +02003437 // If for some reason our views aren't initialized, don't animate
3438 boolean initialized = getAllAppsButton() != null;
3439
3440 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003441 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003442 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3443 toView.findViewById(R.id.apps_customize_pane_content);
Adam Cohenf16e5712011-01-13 13:31:45 -08003444
Adam Cohen9bfdb762014-07-21 17:44:06 -07003445 final View page = content.getPageAt(content.getCurrentPage());
3446 final View revealView = toView.findViewById(R.id.fake_page);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003447
Adam Cohen63f1ec02014-08-12 09:23:13 -07003448 final float initialPanelAlpha = 1f;
3449
3450 final boolean isWidgetTray = contentType == AppsCustomizePagedView.ContentType.Widgets;
3451 if (isWidgetTray) {
Adam Cohen4e243a22014-08-10 18:30:55 -07003452 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3453 } else {
3454 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
3455 }
3456
Adam Cohen9bfdb762014-07-21 17:44:06 -07003457 // Hide the real page background, and swap in the fake one
Adam Cohen9bfdb762014-07-21 17:44:06 -07003458 content.setPageBackgroundsVisible(false);
Adam Cohen2854d252014-08-27 16:04:07 -07003459 revealView.setVisibility(View.VISIBLE);
3460 // We need to hide this view as the animation start will be posted.
3461 revealView.setAlpha(0);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003462
Adam Cohen9bfdb762014-07-21 17:44:06 -07003463 int width = revealView.getMeasuredWidth();
3464 int height = revealView.getMeasuredHeight();
Adam Cohen9bfdb762014-07-21 17:44:06 -07003465 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
Adam Cohen2854d252014-08-27 16:04:07 -07003466
Adam Cohen63f1ec02014-08-12 09:23:13 -07003467 revealView.setTranslationY(0);
Adam Cohen94afab42014-08-24 16:10:54 -07003468 revealView.setTranslationX(0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003469
Adam Cohen63f1ec02014-08-12 09:23:13 -07003470 // Get the y delta between the center of the page and the center of the all apps button
3471 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3472 getAllAppsButton(), null);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003473
Adam Cohen2854d252014-08-27 16:04:07 -07003474 float alpha = 0;
3475 float xDrift = 0;
3476 float yDrift = 0;
3477 if (material) {
3478 alpha = isWidgetTray ? 0.3f : 1f;
3479 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3480 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3481 } else {
3482 yDrift = 2 * height / 3;
3483 xDrift = 0;
3484 }
3485 final float initAlpha = alpha;
3486
Adam Cohen9bfdb762014-07-21 17:44:06 -07003487 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003488 layerViews.add(revealView);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003489 PropertyValuesHolder panelAlpha = PropertyValuesHolder.ofFloat("alpha", initAlpha, 1f);
Adam Cohen94afab42014-08-24 16:10:54 -07003490 PropertyValuesHolder panelDriftY =
Adam Cohen63f1ec02014-08-12 09:23:13 -07003491 PropertyValuesHolder.ofFloat("translationY", yDrift, 0);
Adam Cohen94afab42014-08-24 16:10:54 -07003492 PropertyValuesHolder panelDriftX =
3493 PropertyValuesHolder.ofFloat("translationX", xDrift, 0);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003494
Adam Cohen2854d252014-08-27 16:04:07 -07003495 ObjectAnimator panelAlphaAndDrift = ObjectAnimator.ofPropertyValuesHolder(revealView,
3496 panelAlpha, panelDriftY, panelDriftX);
3497
Adam Cohen9bfdb762014-07-21 17:44:06 -07003498 panelAlphaAndDrift.setDuration(revealDuration);
3499 panelAlphaAndDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen6c5891a2014-07-09 23:53:15 -07003500
Adam Cohen9bfdb762014-07-21 17:44:06 -07003501 mStateAnimation.play(panelAlphaAndDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003502
Adam Cohen4e243a22014-08-10 18:30:55 -07003503 if (page != null) {
3504 page.setVisibility(View.VISIBLE);
3505 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
Adam Cohen2854d252014-08-27 16:04:07 -07003506 layerViews.add(page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003507
Adam Cohen2854d252014-08-27 16:04:07 -07003508 ObjectAnimator pageDrift = ObjectAnimator.ofFloat(page, "translationY", yDrift, 0);
3509 page.setTranslationY(yDrift);
Adam Cohen4e243a22014-08-10 18:30:55 -07003510 pageDrift.setDuration(revealDuration);
3511 pageDrift.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003512 pageDrift.setStartDelay(itemsAlphaStagger);
Adam Cohen4e243a22014-08-10 18:30:55 -07003513 mStateAnimation.play(pageDrift);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003514
Adam Cohen63f1ec02014-08-12 09:23:13 -07003515 page.setAlpha(0f);
Adam Cohen2854d252014-08-27 16:04:07 -07003516 ObjectAnimator itemsAlpha = ObjectAnimator.ofFloat(page, "alpha", 0f, 1f);
Adam Cohen4e243a22014-08-10 18:30:55 -07003517 itemsAlpha.setDuration(revealDuration);
3518 itemsAlpha.setInterpolator(new AccelerateInterpolator(1.5f));
3519 itemsAlpha.setStartDelay(itemsAlphaStagger);
3520 mStateAnimation.play(itemsAlpha);
3521 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003522
Adam Cohen4e243a22014-08-10 18:30:55 -07003523 View pageIndicators = toView.findViewById(R.id.apps_customize_page_indicator);
3524 pageIndicators.setAlpha(0.01f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003525 ObjectAnimator indicatorsAlpha =
Adam Cohen2854d252014-08-27 16:04:07 -07003526 ObjectAnimator.ofFloat(pageIndicators, "alpha", 1f);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003527 indicatorsAlpha.setDuration(revealDuration);
3528 mStateAnimation.play(indicatorsAlpha);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003529
Adam Cohen9bfdb762014-07-21 17:44:06 -07003530 if (material) {
Adam Cohen63f1ec02014-08-12 09:23:13 -07003531 final View allApps = getAllAppsButton();
3532 int allAppsButtonSize = LauncherAppState.getInstance().
3533 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3534 float startRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
Adam Cohen2854d252014-08-27 16:04:07 -07003535 Animator reveal = ViewAnimationUtils.createCircularReveal(revealView, width / 2,
Adam Cohen63f1ec02014-08-12 09:23:13 -07003536 height / 2, startRadius, revealRadius);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003537 reveal.setDuration(revealDuration);
3538 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003539
3540 reveal.addListener(new AnimatorListenerAdapter() {
3541 public void onAnimationStart(Animator animation) {
3542 if (!isWidgetTray) {
3543 allApps.setVisibility(View.INVISIBLE);
3544 }
3545 }
3546 public void onAnimationEnd(Animator animation) {
3547 if (!isWidgetTray) {
3548 allApps.setVisibility(View.VISIBLE);
3549 }
3550 }
3551 });
Adam Cohen6c5891a2014-07-09 23:53:15 -07003552 mStateAnimation.play(reveal);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003553 }
Michael Jurka1899a362011-11-03 13:50:45 -07003554
Adam Cohen9bfdb762014-07-21 17:44:06 -07003555 mStateAnimation.addListener(new AnimatorListenerAdapter() {
3556 @Override
3557 public void onAnimationEnd(Animator animation) {
3558 dispatchOnLauncherTransitionEnd(fromView, animated, false);
3559 dispatchOnLauncherTransitionEnd(toView, animated, false);
3560
3561 revealView.setVisibility(View.INVISIBLE);
3562 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003563 if (page != null) {
3564 page.setLayerType(View.LAYER_TYPE_NONE, null);
3565 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003566 content.setPageBackgroundsVisible(true);
3567
3568 // Hide the search bar
3569 if (mSearchDropTargetBar != null) {
3570 mSearchDropTargetBar.hideSearchBar(false);
3571 }
Adam Cohen3f9c9712014-10-31 11:48:25 -07003572
3573 // This can hold unnecessary references to views.
3574 mStateAnimation = null;
Adam Cohen9bfdb762014-07-21 17:44:06 -07003575 }
3576
Adam Cohen9bfdb762014-07-21 17:44:06 -07003577 });
3578
Adam Cohen6c5891a2014-07-09 23:53:15 -07003579 if (workspaceAnim != null) {
3580 mStateAnimation.play(workspaceAnim);
3581 }
Adam Cohen2854d252014-08-27 16:04:07 -07003582
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003583 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
3584 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003585 final AnimatorSet stateAnimation = mStateAnimation;
3586 final Runnable startAnimRunnable = new Runnable() {
3587 public void run() {
3588 // Check that mStateAnimation hasn't changed while
3589 // we waited for a layout/draw pass
3590 if (mStateAnimation != stateAnimation)
3591 return;
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003592 dispatchOnLauncherTransitionStart(fromView, animated, false);
3593 dispatchOnLauncherTransitionStart(toView, animated, false);
Adam Cohen2854d252014-08-27 16:04:07 -07003594
3595 revealView.setAlpha(initAlpha);
Kenny Guyd794a3f2014-09-16 15:17:58 +01003596 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003597 for (int i = 0; i < layerViews.size(); i++) {
3598 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003599 if (v != null) {
3600 boolean attached = true;
3601 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3602 attached = v.isAttachedToWindow();
3603 }
3604 if (attached) v.buildLayer();
3605 }
Adam Cohen2854d252014-08-27 16:04:07 -07003606 }
3607 }
3608 mStateAnimation.start();
Michael Jurka3dcd79e2012-05-31 07:50:33 -07003609 }
3610 };
Adam Cohen2854d252014-08-27 16:04:07 -07003611 toView.bringToFront();
3612 toView.setVisibility(View.VISIBLE);
3613 toView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003614 } else {
3615 toView.setTranslationX(0.0f);
3616 toView.setTranslationY(0.0f);
3617 toView.setScaleX(1.0f);
3618 toView.setScaleY(1.0f);
3619 toView.setVisibility(View.VISIBLE);
Winson Chung785d2eb2011-04-14 16:08:02 -07003620 toView.bringToFront();
Winson Chung3ac74c52011-06-30 17:39:37 -07003621
Daniel Sandlere4f98912013-06-25 15:13:26 -04003622 if (!springLoaded && !LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungc7d2b602012-05-16 17:02:20 -07003623 // Hide the search bar
Winson Chungadf0c182012-08-23 14:59:07 -07003624 if (mSearchDropTargetBar != null) {
3625 mSearchDropTargetBar.hideSearchBar(false);
3626 }
Michael Jurkaabded662011-03-04 12:06:57 -08003627 }
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003628 dispatchOnLauncherTransitionPrepare(fromView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003629 dispatchOnLauncherTransitionStart(fromView, animated, false);
3630 dispatchOnLauncherTransitionEnd(fromView, animated, false);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003631 dispatchOnLauncherTransitionPrepare(toView, animated, false);
Michael Jurkabed61d22012-02-14 22:51:29 -08003632 dispatchOnLauncherTransitionStart(toView, animated, false);
3633 dispatchOnLauncherTransitionEnd(toView, animated, false);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003634 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003635 }
3636
3637 /**
3638 * Zoom the camera back into the workspace, hiding 'fromView'.
Michael Jurkab3e22d92011-10-31 15:58:33 -07003639 * This is the opposite of showAppsCustomizeHelper.
Patrick Dubroy6b509c12010-08-23 15:08:16 -07003640 * @param animated If true, the transition will be animated.
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003641 */
Adam Cohened307df2013-10-02 09:37:31 -07003642 private void hideAppsCustomizeHelper(Workspace.State toState, final boolean animated,
Adam Cohened66b2b2012-01-23 17:28:51 -08003643 final boolean springLoaded, final Runnable onCompleteRunnable) {
Michael Jurkabed61d22012-02-14 22:51:29 -08003644
Michael Jurkab3e22d92011-10-31 15:58:33 -07003645 if (mStateAnimation != null) {
Michael Jurkaf1ad6082013-03-13 12:55:46 +01003646 mStateAnimation.setDuration(0);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003647 mStateAnimation.cancel();
3648 mStateAnimation = null;
3649 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003650
Kenny Guyd794a3f2014-09-16 15:17:58 +01003651 boolean material = Utilities.isLmpOrAbove();
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003652 Resources res = getResources();
Adam Cohenf16e5712011-01-13 13:31:45 -08003653
Winson Chungf0ea4d32011-06-06 14:27:16 -07003654 final int duration = res.getInteger(R.integer.config_appsCustomizeZoomOutTime);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003655 final int fadeOutDuration = res.getInteger(R.integer.config_appsCustomizeFadeOutTime);
Adam Cohen63f1ec02014-08-12 09:23:13 -07003656 final int revealDuration = res.getInteger(R.integer.config_appsCustomizeConcealTime);
Adam Cohen9bfdb762014-07-21 17:44:06 -07003657 final int itemsAlphaStagger =
3658 res.getInteger(R.integer.config_appsCustomizeItemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003659
Winson Chungf0ea4d32011-06-06 14:27:16 -07003660 final float scaleFactor = (float)
3661 res.getInteger(R.integer.config_appsCustomizeZoomScaleFactor);
3662 final View fromView = mAppsCustomizeTabHost;
Michael Jurkabed61d22012-02-14 22:51:29 -08003663 final View toView = mWorkspace;
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003664 Animator workspaceAnim = null;
Adam Cohen2854d252014-08-27 16:04:07 -07003665 final ArrayList<View> layerViews = new ArrayList<View>();
3666
Adam Cohened307df2013-10-02 09:37:31 -07003667 if (toState == Workspace.State.NORMAL) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003668 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003669 toState, animated, layerViews);
Adam Cohened307df2013-10-02 09:37:31 -07003670 } else if (toState == Workspace.State.SPRING_LOADED ||
3671 toState == Workspace.State.OVERVIEW) {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003672 workspaceAnim = mWorkspace.getChangeStateAnimation(
Adam Cohen2854d252014-08-27 16:04:07 -07003673 toState, animated, layerViews);
Michael Jurka2a4b1a82011-12-07 14:00:02 -08003674 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07003675
Adam Cohena38dc902014-09-07 17:48:55 +02003676 // If for some reason our views aren't initialized, don't animate
3677 boolean initialized = getAllAppsButton() != null;
3678
3679 if (animated && initialized) {
Adam Cohen9bfdb762014-07-21 17:44:06 -07003680 mStateAnimation = LauncherAnimUtils.createAnimatorSet();
Adam Cohen2854d252014-08-27 16:04:07 -07003681 if (workspaceAnim != null) {
3682 mStateAnimation.play(workspaceAnim);
3683 }
Michael Jurka159b4cc2012-01-17 03:00:35 -08003684
Adam Cohen9bfdb762014-07-21 17:44:06 -07003685 final AppsCustomizePagedView content = (AppsCustomizePagedView)
3686 fromView.findViewById(R.id.apps_customize_pane_content);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003687
Adam Cohen9bfdb762014-07-21 17:44:06 -07003688 final View page = content.getPageAt(content.getNextPage());
Adam Cohen8e894fa2014-09-08 19:45:43 +02003689
3690 // We need to hide side pages of the Apps / Widget tray to avoid some ugly edge cases
3691 int count = content.getChildCount();
3692 for (int i = 0; i < count; i++) {
3693 View child = content.getChildAt(i);
3694 if (child != page) {
3695 child.setVisibility(View.INVISIBLE);
3696 }
3697 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003698 final View revealView = fromView.findViewById(R.id.fake_page);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003699
Adam Cohen2854d252014-08-27 16:04:07 -07003700 // hideAppsCustomizeHelper is called in some cases when it is already hidden
3701 // don't perform all these no-op animations. In particularly, this was causing
3702 // the all-apps button to pop in and out.
3703 if (fromView.getVisibility() == View.VISIBLE) {
3704 AppsCustomizePagedView.ContentType contentType = content.getContentType();
3705 final boolean isWidgetTray =
3706 contentType == AppsCustomizePagedView.ContentType.Widgets;
Adam Cohen63f1ec02014-08-12 09:23:13 -07003707
Adam Cohen2854d252014-08-27 16:04:07 -07003708 if (isWidgetTray) {
3709 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel_dark));
3710 } else {
3711 revealView.setBackground(res.getDrawable(R.drawable.quantum_panel));
Adam Cohen63f1ec02014-08-12 09:23:13 -07003712 }
Adam Cohen6c5891a2014-07-09 23:53:15 -07003713
Adam Cohen2854d252014-08-27 16:04:07 -07003714 int width = revealView.getMeasuredWidth();
3715 int height = revealView.getMeasuredHeight();
3716 float revealRadius = (float) Math.sqrt((width * width) / 4 + (height * height) / 4);
3717
3718 // Hide the real page background, and swap in the fake one
3719 revealView.setVisibility(View.VISIBLE);
3720 content.setPageBackgroundsVisible(false);
3721
3722 final View allAppsButton = getAllAppsButton();
3723 revealView.setTranslationY(0);
3724 int[] allAppsToPanelDelta = Utilities.getCenterDeltaInScreenSpace(revealView,
3725 allAppsButton, null);
3726
3727 float xDrift = 0;
3728 float yDrift = 0;
3729 if (material) {
3730 yDrift = isWidgetTray ? height / 2 : allAppsToPanelDelta[1];
3731 xDrift = isWidgetTray ? 0 : allAppsToPanelDelta[0];
3732 } else {
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003733 yDrift = 2 * height / 3;
Adam Cohen2854d252014-08-27 16:04:07 -07003734 xDrift = 0;
3735 }
3736
3737 revealView.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3738 TimeInterpolator decelerateInterpolator = material ?
3739 new LogDecelerateInterpolator(100, 0) :
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003740 new DecelerateInterpolator(1f);
Adam Cohen2854d252014-08-27 16:04:07 -07003741
3742 // The vertical motion of the apps panel should be delayed by one frame
3743 // from the conceal animation in order to give the right feel. We correpsondingly
3744 // shorten the duration so that the slide and conceal end at the same time.
3745 ObjectAnimator panelDriftY = LauncherAnimUtils.ofFloat(revealView, "translationY",
3746 0, yDrift);
3747 panelDriftY.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3748 panelDriftY.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3749 panelDriftY.setInterpolator(decelerateInterpolator);
3750 mStateAnimation.play(panelDriftY);
3751
3752 ObjectAnimator panelDriftX = LauncherAnimUtils.ofFloat(revealView, "translationX",
3753 0, xDrift);
3754 panelDriftX.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3755 panelDriftX.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3756 panelDriftX.setInterpolator(decelerateInterpolator);
3757 mStateAnimation.play(panelDriftX);
3758
3759 if (isWidgetTray || !material) {
3760 float finalAlpha = material ? 0.4f : 0f;
3761 revealView.setAlpha(1f);
3762 ObjectAnimator panelAlpha = LauncherAnimUtils.ofFloat(revealView, "alpha",
3763 1f, finalAlpha);
Adam Cohen0fb5ce22014-10-24 13:50:20 -07003764 panelAlpha.setDuration(material ? revealDuration : 150);
3765 panelAlpha.setInterpolator(decelerateInterpolator);
3766 panelAlpha.setStartDelay(material ? 0 : itemsAlphaStagger + SINGLE_FRAME_DELAY);
Adam Cohen2854d252014-08-27 16:04:07 -07003767 mStateAnimation.play(panelAlpha);
3768 }
3769
3770 if (page != null) {
3771 page.setLayerType(View.LAYER_TYPE_HARDWARE, null);
3772
3773 ObjectAnimator pageDrift = LauncherAnimUtils.ofFloat(page, "translationY",
3774 0, yDrift);
3775 page.setTranslationY(0);
3776 pageDrift.setDuration(revealDuration - SINGLE_FRAME_DELAY);
3777 pageDrift.setInterpolator(decelerateInterpolator);
3778 pageDrift.setStartDelay(itemsAlphaStagger + SINGLE_FRAME_DELAY);
3779 mStateAnimation.play(pageDrift);
3780
3781 page.setAlpha(1f);
3782 ObjectAnimator itemsAlpha = LauncherAnimUtils.ofFloat(page, "alpha", 1f, 0f);
3783 itemsAlpha.setDuration(100);
3784 itemsAlpha.setInterpolator(decelerateInterpolator);
3785 mStateAnimation.play(itemsAlpha);
3786 }
3787
3788 View pageIndicators = fromView.findViewById(R.id.apps_customize_page_indicator);
3789 pageIndicators.setAlpha(1f);
3790 ObjectAnimator indicatorsAlpha =
3791 LauncherAnimUtils.ofFloat(pageIndicators, "alpha", 0f);
3792 indicatorsAlpha.setDuration(revealDuration);
3793 indicatorsAlpha.setInterpolator(new DecelerateInterpolator(1.5f));
3794 mStateAnimation.play(indicatorsAlpha);
3795
3796 width = revealView.getMeasuredWidth();
3797
3798 if (material) {
3799 if (!isWidgetTray) {
3800 allAppsButton.setVisibility(View.INVISIBLE);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003801 }
Adam Cohen2854d252014-08-27 16:04:07 -07003802 int allAppsButtonSize = LauncherAppState.getInstance().
3803 getDynamicGrid().getDeviceProfile().allAppsButtonVisualSize;
3804 float finalRadius = isWidgetTray ? 0 : allAppsButtonSize / 2;
3805 Animator reveal =
3806 LauncherAnimUtils.createCircularReveal(revealView, width / 2,
3807 height / 2, revealRadius, finalRadius);
3808 reveal.setInterpolator(new LogDecelerateInterpolator(100, 0));
3809 reveal.setDuration(revealDuration);
3810 reveal.setStartDelay(itemsAlphaStagger);
Adam Cohen6c5891a2014-07-09 23:53:15 -07003811
Adam Cohen2854d252014-08-27 16:04:07 -07003812 reveal.addListener(new AnimatorListenerAdapter() {
3813 public void onAnimationEnd(Animator animation) {
3814 revealView.setVisibility(View.INVISIBLE);
3815 if (!isWidgetTray) {
3816 allAppsButton.setVisibility(View.VISIBLE);
3817 }
3818 }
3819 });
Adam Cohen9bfdb762014-07-21 17:44:06 -07003820
Adam Cohen2854d252014-08-27 16:04:07 -07003821 mStateAnimation.play(reveal);
3822 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003823
Adam Cohen2854d252014-08-27 16:04:07 -07003824 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
3825 dispatchOnLauncherTransitionPrepare(toView, animated, true);
3826 mAppsCustomizeContent.stopScrolling();
Adam Cohen6c5891a2014-07-09 23:53:15 -07003827 }
Michael Jurkabed61d22012-02-14 22:51:29 -08003828
3829 mStateAnimation.addListener(new AnimatorListenerAdapter() {
Gilles Debunnedd6c9922010-10-25 11:23:41 -07003830 @Override
Michael Jurka8edd75c2010-12-17 20:15:06 -08003831 public void onAnimationEnd(Animator animation) {
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003832 fromView.setVisibility(View.GONE);
Michael Jurkabed61d22012-02-14 22:51:29 -08003833 dispatchOnLauncherTransitionEnd(fromView, animated, true);
3834 dispatchOnLauncherTransitionEnd(toView, animated, true);
Adam Cohened66b2b2012-01-23 17:28:51 -08003835 if (onCompleteRunnable != null) {
3836 onCompleteRunnable.run();
3837 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003838
3839 revealView.setLayerType(View.LAYER_TYPE_NONE, null);
Adam Cohen4e243a22014-08-10 18:30:55 -07003840 if (page != null) {
3841 page.setLayerType(View.LAYER_TYPE_NONE, null);
3842 }
Adam Cohen9bfdb762014-07-21 17:44:06 -07003843 content.setPageBackgroundsVisible(true);
Adam Cohen8e894fa2014-09-08 19:45:43 +02003844 // Unhide side pages
3845 int count = content.getChildCount();
3846 for (int i = 0; i < count; i++) {
3847 View child = content.getChildAt(i);
3848 child.setVisibility(View.VISIBLE);
3849 }
3850
3851 // Reset page transforms
Adam Cohen493f6bc2014-09-18 08:11:05 -07003852 if (page != null) {
3853 page.setTranslationX(0);
3854 page.setTranslationY(0);
3855 page.setAlpha(1);
3856 }
Adam Cohen8e894fa2014-09-08 19:45:43 +02003857 content.setCurrentPage(content.getNextPage());
3858
Chet Haasebc2f0822012-10-26 17:59:53 -07003859 mAppsCustomizeContent.updateCurrentPageScroll();
Adam Cohen3f9c9712014-10-31 11:48:25 -07003860
3861 // This can hold unnecessary references to views.
3862 mStateAnimation = null;
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003863 }
3864 });
3865
Adam Cohen2854d252014-08-27 16:04:07 -07003866 final AnimatorSet stateAnimation = mStateAnimation;
3867 final Runnable startAnimRunnable = new Runnable() {
3868 public void run() {
3869 // Check that mStateAnimation hasn't changed while
3870 // we waited for a layout/draw pass
3871 if (mStateAnimation != stateAnimation)
3872 return;
3873 dispatchOnLauncherTransitionStart(fromView, animated, false);
3874 dispatchOnLauncherTransitionStart(toView, animated, false);
3875
Kenny Guyd794a3f2014-09-16 15:17:58 +01003876 if (Utilities.isLmpOrAbove()) {
Adam Cohen2854d252014-08-27 16:04:07 -07003877 for (int i = 0; i < layerViews.size(); i++) {
3878 View v = layerViews.get(i);
Adam Cohen0f668f32014-09-08 19:54:17 +02003879 if (v != null) {
3880 boolean attached = true;
3881 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
3882 attached = v.isAttachedToWindow();
3883 }
3884 if (attached) v.buildLayer();
3885 }
Adam Cohen2854d252014-08-27 16:04:07 -07003886 }
3887 }
3888 mStateAnimation.start();
3889 }
3890 };
3891 fromView.post(startAnimRunnable);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003892 } else {
3893 fromView.setVisibility(View.GONE);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003894 dispatchOnLauncherTransitionPrepare(fromView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003895 dispatchOnLauncherTransitionStart(fromView, animated, true);
3896 dispatchOnLauncherTransitionEnd(fromView, animated, true);
Michael Jurkaa35e35a2012-04-26 15:04:28 -07003897 dispatchOnLauncherTransitionPrepare(toView, animated, true);
Michael Jurkabed61d22012-02-14 22:51:29 -08003898 dispatchOnLauncherTransitionStart(toView, animated, true);
3899 dispatchOnLauncherTransitionEnd(toView, animated, true);
Patrick Dubroydf7c3a72010-08-26 17:52:56 -07003900 }
Patrick Dubroy3ec8bdd2010-08-06 16:01:33 -07003901 }
3902
Michael Jurkae326f182011-11-21 14:05:46 -08003903 @Override
3904 public void onTrimMemory(int level) {
3905 super.onTrimMemory(level);
Adam Cohen3f9c9712014-10-31 11:48:25 -07003906 if (level >= ComponentCallbacks2.TRIM_MEMORY_UI_HIDDEN) {
3907 // The widget preview db can result in holding onto over
3908 // 3MB of memory for caching which isn't necessary.
3909 SQLiteDatabase.releaseMemory();
3910
3911 // We reset the apps customize tray in order to
3912 // to free all the memory associated with widget previews
3913 if (mAppsCustomizeTabHost != null) {
3914 mAppsCustomizeTabHost.reset();
3915 }
Michael Jurkae326f182011-11-21 14:05:46 -08003916 }
3917 }
3918
Adam Cohened307df2013-10-02 09:37:31 -07003919 protected void showWorkspace(boolean animated) {
Adam Cohened66b2b2012-01-23 17:28:51 -08003920 showWorkspace(animated, null);
3921 }
3922
Adam Cohened307df2013-10-02 09:37:31 -07003923 protected void showWorkspace() {
3924 showWorkspace(true);
3925 }
3926
Adam Cohened66b2b2012-01-23 17:28:51 -08003927 void showWorkspace(boolean animated, Runnable onCompleteRunnable) {
Adam Cohene25c5d12014-06-18 10:34:58 -07003928 if (mState != State.WORKSPACE || mWorkspace.getState() != Workspace.State.NORMAL) {
Winson Chung2d75f122013-09-23 16:53:31 -07003929 boolean wasInSpringLoadedMode = (mState != State.WORKSPACE);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003930 mWorkspace.setVisibility(View.VISIBLE);
Adam Cohened307df2013-10-02 09:37:31 -07003931 hideAppsCustomizeHelper(Workspace.State.NORMAL, animated, false, onCompleteRunnable);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003932
Winson Chungc7d2b602012-05-16 17:02:20 -07003933 // Show the search bar (only animate if we were showing the drop target bar in spring
3934 // loaded mode)
Winson Chungadf0c182012-08-23 14:59:07 -07003935 if (mSearchDropTargetBar != null) {
Winson Chung04c0a5c2013-10-08 17:13:12 -07003936 mSearchDropTargetBar.showSearchBar(animated && wasInSpringLoadedMode);
Winson Chungadf0c182012-08-23 14:59:07 -07003937 }
Winson Chungc7d2b602012-05-16 17:02:20 -07003938
Michael Jurkab3e22d92011-10-31 15:58:33 -07003939 // Set focus to the AppsCustomize button
3940 if (mAllAppsButton != null) {
3941 mAllAppsButton.requestFocus();
3942 }
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003943 }
Adam Lesinski6b879f02010-11-04 16:15:23 -07003944
Michael Jurkac0e8fca2010-10-06 16:41:29 -07003945 // Change the state *after* we've called all the transition code
3946 mState = State.WORKSPACE;
Svetoslav Ganov815ba2d2011-01-07 14:55:17 -08003947
Winson Chung5fb63472011-02-02 17:03:37 -08003948 // Resume the auto-advance of widgets
3949 mUserPresent = true;
3950 updateRunning();
3951
alanv1d4fde62012-10-17 13:15:47 -07003952 // Send an accessibility event to announce the context change
3953 getWindow().getDecorView()
3954 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003955
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003956 onWorkspaceShown(animated);
Bjorn Bringertc459e522013-06-07 19:36:01 +01003957 }
3958
Adam Cohened307df2013-10-02 09:37:31 -07003959 void showOverviewMode(boolean animated) {
3960 mWorkspace.setVisibility(View.VISIBLE);
3961 hideAppsCustomizeHelper(Workspace.State.OVERVIEW, animated, false, null);
3962 mState = State.WORKSPACE;
3963 onWorkspaceShown(animated);
3964 }
3965
Bjorn Bringert69688aa2013-06-11 20:26:38 +01003966 public void onWorkspaceShown(boolean animated) {
Joe Onorato00acb122009-08-04 16:04:30 -04003967 }
3968
Winson Chung82963d52013-10-09 11:20:57 -07003969 void showAllApps(boolean animated, AppsCustomizePagedView.ContentType contentType,
3970 boolean resetPageToZero) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07003971 if (mState != State.WORKSPACE) return;
3972
Winson Chung82963d52013-10-09 11:20:57 -07003973 if (resetPageToZero) {
3974 mAppsCustomizeTabHost.reset();
3975 }
Winson Chungc58497e2013-09-03 17:48:37 -07003976 showAppsCustomizeHelper(animated, false, contentType);
Adam Cohendcc5e712014-06-23 15:38:55 -04003977 mAppsCustomizeTabHost.post(new Runnable() {
3978 @Override
3979 public void run() {
3980 // We post this in-case the all apps view isn't yet constructed.
3981 mAppsCustomizeTabHost.requestFocus();
3982 }
3983 });
Michael Jurkab3e22d92011-10-31 15:58:33 -07003984
Michael Jurkab3e22d92011-10-31 15:58:33 -07003985 // Change the state *after* we've called all the transition code
3986 mState = State.APPS_CUSTOMIZE;
3987
3988 // Pause the auto-advance of widgets until we are out of AllApps
3989 mUserPresent = false;
3990 updateRunning();
3991 closeFolder();
3992
3993 // Send an accessibility event to announce the context change
alanv1d4fde62012-10-17 13:15:47 -07003994 getWindow().getDecorView()
3995 .sendAccessibilityEvent(AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED);
Michael Jurkab3e22d92011-10-31 15:58:33 -07003996 }
3997
Adam Cohen7777d962011-08-18 18:58:38 -07003998 void enterSpringLoadedDragMode() {
Winson Chungc93e5ae2012-07-23 20:48:26 -07003999 if (isAllAppsVisible()) {
Adam Cohened307df2013-10-02 09:37:31 -07004000 hideAppsCustomizeHelper(Workspace.State.SPRING_LOADED, true, true, null);
Winson Chungc07918d2011-07-01 15:35:26 -07004001 mState = State.APPS_CUSTOMIZE_SPRING_LOADED;
Winson Chungb26f3d62011-06-02 10:49:29 -07004002 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08004003 }
Adam Cohen7777d962011-08-18 18:58:38 -07004004
Adam Cohenad4e15c2013-10-17 16:21:35 -07004005 void exitSpringLoadedDragModeDelayed(final boolean successfulDrop, int delay,
Adam Cohened66b2b2012-01-23 17:28:51 -08004006 final Runnable onCompleteRunnable) {
Winson Chung09bfc452011-09-09 11:30:20 -07004007 if (mState != State.APPS_CUSTOMIZE_SPRING_LOADED) return;
4008
Winson Chunge7a03942011-08-05 15:05:12 -07004009 mHandler.postDelayed(new Runnable() {
Winson Chung557d6ed2011-07-08 15:34:52 -07004010 @Override
4011 public void run() {
Winson Chung6a3fd3f2011-08-02 14:03:26 -07004012 if (successfulDrop) {
Michael Jurka7bdb25a2011-08-03 15:16:44 -07004013 // Before we show workspace, hide all apps again because
4014 // exitSpringLoadedDragMode made it visible. This is a bit hacky; we should
4015 // clean up our state transition functions
4016 mAppsCustomizeTabHost.setVisibility(View.GONE);
Adam Cohened66b2b2012-01-23 17:28:51 -08004017 showWorkspace(true, onCompleteRunnable);
Adam Cohen7777d962011-08-18 18:58:38 -07004018 } else {
4019 exitSpringLoadedDragMode();
Winson Chung6a3fd3f2011-08-02 14:03:26 -07004020 }
Winson Chung557d6ed2011-07-08 15:34:52 -07004021 }
Adam Cohenad4e15c2013-10-17 16:21:35 -07004022 }, delay);
Winson Chung557d6ed2011-07-08 15:34:52 -07004023 }
Michael Jurkab3e22d92011-10-31 15:58:33 -07004024
Michael Jurkad3ef3062010-11-23 16:23:58 -08004025 void exitSpringLoadedDragMode() {
Winson Chungb26f3d62011-06-02 10:49:29 -07004026 if (mState == State.APPS_CUSTOMIZE_SPRING_LOADED) {
Michael Jurkab3e22d92011-10-31 15:58:33 -07004027 final boolean animated = true;
4028 final boolean springLoaded = true;
4029 showAppsCustomizeHelper(animated, springLoaded);
Winson Chungb26f3d62011-06-02 10:49:29 -07004030 mState = State.APPS_CUSTOMIZE;
Winson Chungf0ea4d32011-06-06 14:27:16 -07004031 }
4032 // Otherwise, we are not in spring loaded mode, so don't do anything.
4033 }
4034
Michael Jurkab3e22d92011-10-31 15:58:33 -07004035 void lockAllApps() {
4036 // TODO
4037 }
4038
4039 void unlockAllApps() {
4040 // TODO
4041 }
4042
Winson Chungf0ea4d32011-06-06 14:27:16 -07004043 /**
Winson Chung3d503fb2011-07-13 17:25:49 -07004044 * Hides the hotseat area.
Winson Chungf0ea4d32011-06-06 14:27:16 -07004045 */
Winson Chung3d503fb2011-07-13 17:25:49 -07004046 void hideHotseat(boolean animated) {
Daniel Sandlere4f98912013-06-25 15:13:26 -04004047 if (!LauncherAppState.getInstance().isScreenLarge()) {
Winson Chungf0ea4d32011-06-06 14:27:16 -07004048 if (animated) {
Michael Jurka7407d2a2011-12-12 21:48:38 -08004049 if (mHotseat.getAlpha() != 0f) {
Winson Chungadf0c182012-08-23 14:59:07 -07004050 int duration = 0;
4051 if (mSearchDropTargetBar != null) {
4052 duration = mSearchDropTargetBar.getTransitionOutDuration();
4053 }
Michael Jurka7407d2a2011-12-12 21:48:38 -08004054 mHotseat.animate().alpha(0f).setDuration(duration);
4055 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07004056 } else {
Winson Chung3d503fb2011-07-13 17:25:49 -07004057 mHotseat.setAlpha(0f);
Winson Chungf0ea4d32011-06-06 14:27:16 -07004058 }
Winson Chungb26f3d62011-06-02 10:49:29 -07004059 }
Michael Jurkad3ef3062010-11-23 16:23:58 -08004060 }
4061
Patrick Dubroy5f445422011-02-18 14:35:21 -08004062 /**
4063 * Add an item from all apps or customize onto the given workspace screen.
4064 * If layout is null, add to the current screen.
4065 */
4066 void addExternalItemToScreen(ItemInfo itemInfo, final CellLayout layout) {
Michael Jurka6b4b25d2010-10-20 18:19:45 -07004067 if (!mWorkspace.addExternalItemToScreen(itemInfo, layout)) {
Winson Chung93eef082012-03-23 15:59:27 -07004068 showOutOfSpaceMessage(isHotseatLayout(layout));
Michael Jurka213d9632010-07-28 11:29:25 -07004069 }
Michael Jurka6b4b25d2010-10-20 18:19:45 -07004070 }
Patrick Dubroy2b9ff372010-09-07 17:49:27 -07004071
Winson Chungdff8ebb2011-09-08 17:25:31 -07004072 /** Maps the current orientation to an index for referencing orientation correct global icons */
4073 private int getCurrentOrientationIndexForGlobalIcons() {
4074 // default - 0, landscape - 1
4075 switch (getResources().getConfiguration().orientation) {
4076 case Configuration.ORIENTATION_LANDSCAPE:
4077 return 1;
4078 default:
4079 return 0;
4080 }
4081 }
4082
Michael Jurkaae65ee32012-04-30 11:45:54 -07004083 private Drawable getExternalPackageToolbarIcon(ComponentName activityName, String resourceName) {
Michael Jurka0423dcf2010-10-05 14:56:18 -07004084 try {
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004085 PackageManager packageManager = getPackageManager();
Michael Jurka0423dcf2010-10-05 14:56:18 -07004086 // Look for the toolbar icon specified in the activity meta-data
4087 Bundle metaData = packageManager.getActivityInfo(
4088 activityName, PackageManager.GET_META_DATA).metaData;
4089 if (metaData != null) {
Michael Jurkaae65ee32012-04-30 11:45:54 -07004090 int iconResId = metaData.getInt(resourceName);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004091 if (iconResId != 0) {
4092 Resources res = packageManager.getResourcesForActivity(activityName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004093 return res.getDrawable(iconResId);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004094 }
4095 }
4096 } catch (NameNotFoundException e) {
Michael Jurkab6052a92011-07-12 17:02:45 -07004097 // This can happen if the activity defines an invalid drawable
4098 Log.w(TAG, "Failed to load toolbar icon; " + activityName.flattenToShortString() +
4099 " not found", e);
Mathew Inwood70d51022011-07-12 13:41:41 +01004100 } catch (Resources.NotFoundException nfe) {
4101 // This can happen if the activity defines an invalid drawable
4102 Log.w(TAG, "Failed to load toolbar icon from " + activityName.flattenToShortString(),
4103 nfe);
Michael Jurka0423dcf2010-10-05 14:56:18 -07004104 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004105 return null;
4106 }
4107
4108 // if successful in getting icon, return it; otherwise, set button to use default drawable
4109 private Drawable.ConstantState updateTextButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004110 int buttonId, ComponentName activityName, int fallbackDrawableId,
4111 String toolbarResourceName) {
4112 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chung128bbcd2011-08-31 16:58:52 -07004113 Resources r = getResources();
4114 int w = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_width);
4115 int h = r.getDimensionPixelSize(R.dimen.toolbar_external_icon_height);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004116
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004117 TextView button = (TextView) findViewById(buttonId);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004118 // If we were unable to find the icon via the meta-data, use a generic one
4119 if (toolbarIcon == null) {
Winson Chung128bbcd2011-08-31 16:58:52 -07004120 toolbarIcon = r.getDrawable(fallbackDrawableId);
4121 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004122 if (button != null) {
4123 button.setCompoundDrawables(toolbarIcon, null, null, null);
4124 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004125 return null;
4126 } else {
Winson Chung128bbcd2011-08-31 16:58:52 -07004127 toolbarIcon.setBounds(0, 0, w, h);
Michael Jurka4da7a3e2011-10-28 15:04:35 -07004128 if (button != null) {
4129 button.setCompoundDrawables(toolbarIcon, null, null, null);
4130 }
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004131 return toolbarIcon.getConstantState();
4132 }
4133 }
4134
4135 // if successful in getting icon, return it; otherwise, set button to use default drawable
4136 private Drawable.ConstantState updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004137 int buttonId, ComponentName activityName, int fallbackDrawableId,
4138 String toolbarResourceName) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004139 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkaae65ee32012-04-30 11:45:54 -07004140 Drawable toolbarIcon = getExternalPackageToolbarIcon(activityName, toolbarResourceName);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004141
Michael Jurka19e0fc52011-07-22 18:00:21 -07004142 if (button != null) {
4143 // If we were unable to find the icon via the meta-data, use a
4144 // generic one
4145 if (toolbarIcon == null) {
4146 button.setImageResource(fallbackDrawableId);
4147 } else {
4148 button.setImageDrawable(toolbarIcon);
4149 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004150 }
Michael Jurka19e0fc52011-07-22 18:00:21 -07004151
4152 return toolbarIcon != null ? toolbarIcon.getConstantState() : null;
4153
Michael Jurka0423dcf2010-10-05 14:56:18 -07004154 }
4155
Winson Chung1b884092012-06-08 17:13:02 -07004156 private void updateTextButtonWithDrawable(int buttonId, Drawable d) {
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004157 TextView button = (TextView) findViewById(buttonId);
Winson Chung1b884092012-06-08 17:13:02 -07004158 button.setCompoundDrawables(d, null, null, null);
Winson Chungfbb3d9b2011-03-01 12:43:02 -08004159 }
4160
Michael Jurkae7bf83b2010-12-14 18:02:21 -08004161 private void updateButtonWithDrawable(int buttonId, Drawable.ConstantState d) {
Michael Jurka4ef207b2010-11-29 17:05:45 -08004162 ImageView button = (ImageView) findViewById(buttonId);
Michael Jurkae7bf83b2010-12-14 18:02:21 -08004163 button.setImageDrawable(d.newDrawable(getResources()));
Michael Jurka4ef207b2010-11-29 17:05:45 -08004164 }
4165
Winson Chungbb185bd2011-11-21 12:31:42 -08004166 private void invalidatePressedFocusedStates(View container, View button) {
4167 if (container instanceof HolographicLinearLayout) {
4168 HolographicLinearLayout layout = (HolographicLinearLayout) container;
4169 layout.invalidatePressedFocusedStates();
4170 } else if (button instanceof HolographicImageView) {
4171 HolographicImageView view = (HolographicImageView) button;
4172 view.invalidatePressedFocusedStates();
4173 }
4174 }
4175
Cristina Stancu476493b2013-08-07 17:20:14 +01004176 public View getQsbBar() {
Adam Cohenc2d6e892014-10-16 09:49:24 -07004177 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4178 return mLauncherCallbacks.getQsbBar();
Adam Cohen9211d422014-10-07 18:14:53 -07004179 }
4180
Adam Cohen24ce0b32014-01-14 16:18:14 -08004181 if (mQsb == null) {
4182 mQsb = mInflater.inflate(R.layout.qsb, mSearchDropTargetBar, false);
4183 mSearchDropTargetBar.addView(mQsb);
Cristina Stancu476493b2013-08-07 17:20:14 +01004184 }
Adam Cohen24ce0b32014-01-14 16:18:14 -08004185 return mQsb;
Cristina Stancu476493b2013-08-07 17:20:14 +01004186 }
4187
4188 protected boolean updateGlobalSearchIcon() {
Adam Cohen9211d422014-10-07 18:14:53 -07004189 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4190 return true;
4191 }
4192
Winson Chungc51db6a2011-10-05 11:44:49 -07004193 final View searchButtonContainer = findViewById(R.id.search_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004194 final ImageView searchButton = (ImageView) findViewById(R.id.search_button);
Winson Chungc51db6a2011-10-05 11:44:49 -07004195 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004196 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004197
Winson Chung1cad91e2011-05-25 17:41:01 -07004198 final SearchManager searchManager =
4199 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4200 ComponentName activityName = searchManager.getGlobalSearchActivity();
4201 if (activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004202 int coi = getCurrentOrientationIndexForGlobalIcons();
4203 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004204 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
4205 TOOLBAR_SEARCH_ICON_METADATA_NAME);
4206 if (sGlobalSearchIcon[coi] == null) {
4207 sGlobalSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4208 R.id.search_button, activityName, R.drawable.ic_home_search_normal_holo,
4209 TOOLBAR_ICON_METADATA_NAME);
4210 }
4211
Winson Chungc51db6a2011-10-05 11:44:49 -07004212 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.VISIBLE);
4213 searchButton.setVisibility(View.VISIBLE);
Winson Chungbb185bd2011-11-21 12:31:42 -08004214 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004215 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004216 } else {
Winson Chungcbf7c4d2011-08-23 11:58:54 -07004217 // We disable both search and voice search when there is no global search provider
Winson Chungc51db6a2011-10-05 11:44:49 -07004218 if (searchButtonContainer != null) searchButtonContainer.setVisibility(View.GONE);
4219 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004220 if (searchButton != null) searchButton.setVisibility(View.GONE);
4221 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004222 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004223 return false;
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004224 }
4225 }
4226
Cristina Stancu476493b2013-08-07 17:20:14 +01004227 protected void updateGlobalSearchIcon(Drawable.ConstantState d) {
Adam Cohen9211d422014-10-07 18:14:53 -07004228 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) return;
Winson Chungbb185bd2011-11-21 12:31:42 -08004229 final View searchButtonContainer = findViewById(R.id.search_button_container);
4230 final View searchButton = (ImageView) findViewById(R.id.search_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004231 updateButtonWithDrawable(R.id.search_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004232 invalidatePressedFocusedStates(searchButtonContainer, searchButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004233 }
4234
Cristina Stancu476493b2013-08-07 17:20:14 +01004235 protected boolean updateVoiceSearchIcon(boolean searchVisible) {
Adam Cohen9211d422014-10-07 18:14:53 -07004236 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4237 return true;
4238 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004239 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
Winson Chung1cad91e2011-05-25 17:41:01 -07004240 final View voiceButton = findViewById(R.id.voice_button);
Winson Chung97d85d22011-04-13 11:27:36 -07004241
Winson Chungc51db6a2011-10-05 11:44:49 -07004242 // We only show/update the voice search icon if the search icon is enabled as well
Michael Jurkaae65ee32012-04-30 11:45:54 -07004243 final SearchManager searchManager =
4244 (SearchManager) getSystemService(Context.SEARCH_SERVICE);
4245 ComponentName globalSearchActivity = searchManager.getGlobalSearchActivity();
4246
4247 ComponentName activityName = null;
4248 if (globalSearchActivity != null) {
4249 // Check if the global search activity handles voice search
4250 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4251 intent.setPackage(globalSearchActivity.getPackageName());
4252 activityName = intent.resolveActivity(getPackageManager());
4253 }
4254
4255 if (activityName == null) {
4256 // Fallback: check if an activity other than the global search activity
4257 // resolves this
4258 Intent intent = new Intent(RecognizerIntent.ACTION_WEB_SEARCH);
4259 activityName = intent.resolveActivity(getPackageManager());
4260 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004261 if (searchVisible && activityName != null) {
Winson Chungdff8ebb2011-09-08 17:25:31 -07004262 int coi = getCurrentOrientationIndexForGlobalIcons();
4263 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
Michael Jurkaae65ee32012-04-30 11:45:54 -07004264 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4265 TOOLBAR_VOICE_SEARCH_ICON_METADATA_NAME);
4266 if (sVoiceSearchIcon[coi] == null) {
4267 sVoiceSearchIcon[coi] = updateButtonWithIconFromExternalActivity(
4268 R.id.voice_button, activityName, R.drawable.ic_home_voice_search_holo,
4269 TOOLBAR_ICON_METADATA_NAME);
4270 }
Winson Chungc51db6a2011-10-05 11:44:49 -07004271 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.VISIBLE);
Winson Chung1cad91e2011-05-25 17:41:01 -07004272 voiceButton.setVisibility(View.VISIBLE);
Winson Chung5841efa2013-09-30 18:06:44 -07004273 updateVoiceButtonProxyVisible(false);
Winson Chungbb185bd2011-11-21 12:31:42 -08004274 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Winson Chungc51db6a2011-10-05 11:44:49 -07004275 return true;
Winson Chung1cad91e2011-05-25 17:41:01 -07004276 } else {
Winson Chungc51db6a2011-10-05 11:44:49 -07004277 if (voiceButtonContainer != null) voiceButtonContainer.setVisibility(View.GONE);
John Spurlock2c38e702013-09-18 10:48:25 -04004278 if (voiceButton != null) voiceButton.setVisibility(View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004279 updateVoiceButtonProxyVisible(false);
Winson Chungc51db6a2011-10-05 11:44:49 -07004280 return false;
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004281 }
Michael Jurka6ae0fcf2010-10-01 12:23:12 -07004282 }
Michael Jurka0423dcf2010-10-05 14:56:18 -07004283
Cristina Stancu476493b2013-08-07 17:20:14 +01004284 protected void updateVoiceSearchIcon(Drawable.ConstantState d) {
Adam Cohen9211d422014-10-07 18:14:53 -07004285 if (mLauncherCallbacks != null && mLauncherCallbacks.providesSearch()) {
4286 return;
4287 }
4288
Winson Chungbb185bd2011-11-21 12:31:42 -08004289 final View voiceButtonContainer = findViewById(R.id.voice_button_container);
4290 final View voiceButton = findViewById(R.id.voice_button);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004291 updateButtonWithDrawable(R.id.voice_button, d);
Winson Chungbb185bd2011-11-21 12:31:42 -08004292 invalidatePressedFocusedStates(voiceButtonContainer, voiceButton);
Michael Jurka4ef207b2010-11-29 17:05:45 -08004293 }
4294
Winson Chung5841efa2013-09-30 18:06:44 -07004295 public void updateVoiceButtonProxyVisible(boolean forceDisableVoiceButtonProxy) {
Adam Cohen9211d422014-10-07 18:14:53 -07004296 if (mLauncherCallbacks != null) {
4297 forceDisableVoiceButtonProxy |= mLauncherCallbacks.forceDisableVoiceButtonProxy();
4298 }
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004299 final View voiceButtonProxy = findViewById(R.id.voice_button_proxy);
4300 if (voiceButtonProxy != null) {
Winson Chung5841efa2013-09-30 18:06:44 -07004301 boolean visible = !forceDisableVoiceButtonProxy &&
4302 mWorkspace.shouldVoiceButtonProxyBeVisible();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004303 voiceButtonProxy.setVisibility(visible ? View.VISIBLE : View.GONE);
Winson Chung5841efa2013-09-30 18:06:44 -07004304 voiceButtonProxy.bringToFront();
Bjorn Bringertecf33e42013-06-11 15:46:07 +01004305 }
4306 }
Winson Chung5841efa2013-09-30 18:06:44 -07004307
4308 /**
4309 * This is an overrid eot disable the voice button proxy. If disabled is true, then the voice button proxy
4310 * will be hidden regardless of what shouldVoiceButtonProxyBeVisible() returns.
4311 */
4312 public void disableVoiceButtonProxy(boolean disabled) {
4313 updateVoiceButtonProxyVisible(disabled);
4314 }
Michael Jurka4ef207b2010-11-29 17:05:45 -08004315
Michael Jurkad7c28052012-04-27 15:43:36 -07004316 @Override
4317 public boolean dispatchPopulateAccessibilityEvent(AccessibilityEvent event) {
alanv1d4fde62012-10-17 13:15:47 -07004318 final boolean result = super.dispatchPopulateAccessibilityEvent(event);
Michael Jurkad7c28052012-04-27 15:43:36 -07004319 final List<CharSequence> text = event.getText();
4320 text.clear();
alanv1d4fde62012-10-17 13:15:47 -07004321 // Populate event with a fake title based on the current state.
4322 if (mState == State.APPS_CUSTOMIZE) {
Adam Cohen6c5891a2014-07-09 23:53:15 -07004323 text.add(mAppsCustomizeTabHost.getContentTag());
alanv1d4fde62012-10-17 13:15:47 -07004324 } else {
4325 text.add(getString(R.string.all_apps_home_button_label));
4326 }
Michael Jurkad7c28052012-04-27 15:43:36 -07004327 return result;
4328 }
4329
Patrick Dubroyceae05d2010-08-30 10:40:53 -07004330 /**
Winson Chungb8472bb2011-08-05 13:49:21 -07004331 * Receives notifications when system dialogs are to be closed.
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004332 */
4333 private class CloseSystemDialogsIntentReceiver extends BroadcastReceiver {
4334 @Override
4335 public void onReceive(Context context, Intent intent) {
Joe Onorato2ca0ae72009-11-10 13:14:13 -08004336 closeSystemDialogs();
4337 }
4338 }
4339
4340 /**
Jeff Sharkey1e2efc82009-11-06 15:17:59 -08004341 * Receives notifications whenever the appwidgets are reset.
4342 */
4343 private class AppWidgetResetObserver extends ContentObserver {
4344 public AppWidgetResetObserver() {
4345 super(new Handler());
4346 }
4347
4348 @Override
4349 public void onChange(boolean selfChange) {
4350 onAppWidgetReset();
4351 }
4352 }
4353
4354 /**
Michael Jurka7607c2f2013-04-03 14:33:19 -07004355 * If the activity is currently paused, signal that we need to run the passed Runnable
4356 * in onResume.
4357 *
4358 * This needs to be called from incoming places where resources might have been loaded
4359 * while we are paused. That is becaues the Configuration might be wrong
4360 * when we're not running, and if it comes back to what it was when we
4361 * were paused, we are not restarted.
4362 *
4363 * Implementation of the method from LauncherModel.Callbacks.
4364 *
4365 * @return true if we are currently paused. The caller might be able to
4366 * skip some work in that case since we will come back again.
4367 */
Michael Jurkac402cd92013-05-20 15:49:32 +02004368 private boolean waitUntilResume(Runnable run, boolean deletePreviousRunnables) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004369 if (mPaused) {
4370 Log.i(TAG, "Deferring update until onResume");
Michael Jurkac402cd92013-05-20 15:49:32 +02004371 if (deletePreviousRunnables) {
Michael Jurka1e2f4652013-07-08 18:03:46 -07004372 while (mBindOnResumeCallbacks.remove(run)) {
Michael Jurkac402cd92013-05-20 15:49:32 +02004373 }
4374 }
Michael Jurka1e2f4652013-07-08 18:03:46 -07004375 mBindOnResumeCallbacks.add(run);
Michael Jurka7607c2f2013-04-03 14:33:19 -07004376 return true;
4377 } else {
4378 return false;
4379 }
4380 }
4381
Michael Jurkac402cd92013-05-20 15:49:32 +02004382 private boolean waitUntilResume(Runnable run) {
4383 return waitUntilResume(run, false);
4384 }
4385
Michael Jurka1e2f4652013-07-08 18:03:46 -07004386 public void addOnResumeCallback(Runnable run) {
Michael Jurka54554252013-08-01 12:52:23 +02004387 mOnResumeCallbacks.add(run);
Michael Jurka1e2f4652013-07-08 18:03:46 -07004388 }
4389
Michael Jurka7607c2f2013-04-03 14:33:19 -07004390 /**
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004391 * If the activity is currently paused, signal that we need to re-run the loader
4392 * in onResume.
4393 *
4394 * This needs to be called from incoming places where resources might have been loaded
4395 * while we are paused. That is becaues the Configuration might be wrong
4396 * when we're not running, and if it comes back to what it was when we
4397 * were paused, we are not restarted.
4398 *
4399 * Implementation of the method from LauncherModel.Callbacks.
4400 *
4401 * @return true if we are currently paused. The caller might be able to
4402 * skip some work in that case since we will come back again.
4403 */
4404 public boolean setLoadOnResume() {
4405 if (mPaused) {
4406 Log.i(TAG, "setLoadOnResume");
4407 mOnResumeNeedsLoad = true;
4408 return true;
4409 } else {
4410 return false;
4411 }
4412 }
4413
4414 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004415 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004416 */
Joe Onorato9c1289c2009-08-17 11:03:03 -04004417 public int getCurrentWorkspaceScreen() {
Joe Onoratod0afc872010-06-11 00:03:15 -07004418 if (mWorkspace != null) {
Michael Jurka0142d492010-08-25 17:46:15 -07004419 return mWorkspace.getCurrentPage();
Joe Onoratod0afc872010-06-11 00:03:15 -07004420 } else {
4421 return SCREEN_COUNT / 2;
4422 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004423 }
The Android Open Source Projectf96811c2009-03-18 17:39:48 -07004424
Joe Onorato9c1289c2009-08-17 11:03:03 -04004425 /**
4426 * Refreshes the shortcuts shown on the workspace.
4427 *
4428 * Implementation of the method from LauncherModel.Callbacks.
4429 */
4430 public void startBinding() {
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004431 setWorkspaceLoading(true);
Adam Cohen517a7f52014-03-01 12:12:59 -08004432
Michael Jurka7607c2f2013-04-03 14:33:19 -07004433 // If we're starting binding all over again, clear any bind calls we'd postponed in
4434 // the past (see waitUntilResume) -- we don't need them since we're starting binding
4435 // from scratch again
Michael Jurka1e2f4652013-07-08 18:03:46 -07004436 mBindOnResumeCallbacks.clear();
Adam Cohendf035382011-04-11 17:22:04 -07004437
Winson Chungd64d1762013-08-20 14:37:16 -07004438 // Clear the workspace because it's going to be rebound
Adam Cohendf035382011-04-11 17:22:04 -07004439 mWorkspace.clearDropTargets();
Winson Chung9e6a0a22013-08-27 11:58:12 -07004440 mWorkspace.removeAllWorkspaceScreens();
Winson Chungd64d1762013-08-20 14:37:16 -07004441
Michael Jurka05bf6442011-11-30 20:28:53 -08004442 mWidgetsToAdvance.clear();
Winson Chung3d503fb2011-07-13 17:25:49 -07004443 if (mHotseat != null) {
4444 mHotseat.resetLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004445 }
4446 }
4447
Adam Cohendcd297f2013-06-18 13:13:40 -07004448 @Override
4449 public void bindScreens(ArrayList<Long> orderedScreenIds) {
Winson Chung64359a52013-07-08 17:17:08 -07004450 bindAddScreens(orderedScreenIds);
Winson Chung0e6a7132013-08-23 12:55:10 -07004451
Adam Cohen5084cba2013-09-03 12:01:16 -07004452 // If there are no screens, we need to have an empty screen
4453 if (orderedScreenIds.size() == 0) {
4454 mWorkspace.addExtraEmptyScreen();
4455 }
Winson Chung0e6a7132013-08-23 12:55:10 -07004456
4457 // Create the custom content page (this call updates mDefaultScreen which calls
Dave Hawkeya8881582013-09-17 15:55:33 -06004458 // setCurrentPage() so ensure that all pages are added before calling this).
Derek Prothrodadd9842014-01-17 13:43:50 -05004459 if (hasCustomContentToLeft()) {
4460 mWorkspace.createCustomContentContainer();
4461 populateCustomContentContainer();
Winson Chung0e6a7132013-08-23 12:55:10 -07004462 }
Winson Chung64359a52013-07-08 17:17:08 -07004463 }
4464
4465 @Override
4466 public void bindAddScreens(ArrayList<Long> orderedScreenIds) {
Winson Chunga90303b2013-11-15 13:05:06 -08004467 // Log to disk
4468 Launcher.addDumpLog(TAG, "11683562 - bindAddScreens()", true);
4469 Launcher.addDumpLog(TAG, "11683562 - orderedScreenIds: " +
4470 TextUtils.join(", ", orderedScreenIds), true);
Adam Cohendcd297f2013-06-18 13:13:40 -07004471 int count = orderedScreenIds.size();
4472 for (int i = 0; i < count; i++) {
Adam Cohen89bddfa2013-08-20 11:57:13 -07004473 mWorkspace.insertNewWorkspaceScreenBeforeEmptyScreen(orderedScreenIds.get(i));
Adam Cohendcd297f2013-06-18 13:13:40 -07004474 }
Adam Cohendcd297f2013-06-18 13:13:40 -07004475 }
4476
Adam Cohen39a06042013-07-19 14:30:12 -07004477 private boolean shouldShowWeightWatcher() {
4478 String spKey = LauncherAppState.getSharedPreferencesKey();
4479 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
Daniel Sandler6053b802013-08-15 15:44:26 -07004480 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, SHOW_WEIGHT_WATCHER_DEFAULT);
Adam Cohen39a06042013-07-19 14:30:12 -07004481
4482 return show;
4483 }
4484
4485 private void toggleShowWeightWatcher() {
4486 String spKey = LauncherAppState.getSharedPreferencesKey();
4487 SharedPreferences sp = getSharedPreferences(spKey, Context.MODE_PRIVATE);
4488 boolean show = sp.getBoolean(SHOW_WEIGHT_WATCHER, true);
4489
4490 show = !show;
4491
4492 SharedPreferences.Editor editor = sp.edit();
4493 editor.putBoolean(SHOW_WEIGHT_WATCHER, show);
4494 editor.commit();
4495
4496 if (mWeightWatcher != null) {
4497 mWeightWatcher.setVisibility(show ? View.VISIBLE : View.GONE);
4498 }
4499 }
4500
Winson Chungd64d1762013-08-20 14:37:16 -07004501 public void bindAppsAdded(final ArrayList<Long> newScreens,
4502 final ArrayList<ItemInfo> addNotAnimated,
Winson Chungc58497e2013-09-03 17:48:37 -07004503 final ArrayList<ItemInfo> addAnimated,
4504 final ArrayList<AppInfo> addedApps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004505 Runnable r = new Runnable() {
4506 public void run() {
Winson Chungc58497e2013-09-03 17:48:37 -07004507 bindAppsAdded(newScreens, addNotAnimated, addAnimated, addedApps);
Winson Chungd64d1762013-08-20 14:37:16 -07004508 }
4509 };
4510 if (waitUntilResume(r)) {
4511 return;
4512 }
4513
Winson Chungd64d1762013-08-20 14:37:16 -07004514 // Add the new screens
Adam Cohen76a47a12014-02-05 11:47:43 -08004515 if (newScreens != null) {
4516 bindAddScreens(newScreens);
4517 }
Winson Chungd64d1762013-08-20 14:37:16 -07004518
4519 // We add the items without animation on non-visible pages, and with
4520 // animations on the new page (which we will try and snap to).
Adam Cohen76a47a12014-02-05 11:47:43 -08004521 if (addNotAnimated != null && !addNotAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004522 bindItems(addNotAnimated, 0,
4523 addNotAnimated.size(), false);
4524 }
Adam Cohen76a47a12014-02-05 11:47:43 -08004525 if (addAnimated != null && !addAnimated.isEmpty()) {
Winson Chungd64d1762013-08-20 14:37:16 -07004526 bindItems(addAnimated, 0,
4527 addAnimated.size(), true);
4528 }
Winson Chungc58497e2013-09-03 17:48:37 -07004529
Winson Chung87412982013-10-03 18:34:14 -07004530 // Remove the extra empty screen
Adam Cohen689ff162014-05-08 17:27:56 -07004531 mWorkspace.removeExtraEmptyScreen(false, false);
Winson Chung87412982013-10-03 18:34:14 -07004532
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004533 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004534 addedApps != null && mAppsCustomizeContent != null) {
4535 mAppsCustomizeContent.addApps(addedApps);
4536 }
Winson Chungd64d1762013-08-20 14:37:16 -07004537 }
4538
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004539 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004540 * Bind the items start-end from the list.
4541 *
4542 * Implementation of the method from LauncherModel.Callbacks.
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004543 */
Winson Chung64359a52013-07-08 17:17:08 -07004544 public void bindItems(final ArrayList<ItemInfo> shortcuts, final int start, final int end,
4545 final boolean forceAnimateIcons) {
Winson Chungd64d1762013-08-20 14:37:16 -07004546 Runnable r = new Runnable() {
4547 public void run() {
4548 bindItems(shortcuts, start, end, forceAnimateIcons);
4549 }
4550 };
4551 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004552 return;
4553 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004554
Winson Chungf0c6ae02012-03-21 16:10:31 -07004555 // Get the list of added shortcuts and intersect them with the set of shortcuts here
Winson Chung64359a52013-07-08 17:17:08 -07004556 final AnimatorSet anim = LauncherAnimUtils.createAnimatorSet();
4557 final Collection<Animator> bounceAnims = new ArrayList<Animator>();
Winson Chung997a9232013-07-24 15:33:46 -07004558 final boolean animateIcons = forceAnimateIcons && canRunNewAppsAnimation();
Winson Chungf0c6ae02012-03-21 16:10:31 -07004559 Workspace workspace = mWorkspace;
Winson Chung997a9232013-07-24 15:33:46 -07004560 long newShortcutsScreenId = -1;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004561 for (int i = start; i < end; i++) {
Joe Onorato9c1289c2009-08-17 11:03:03 -04004562 final ItemInfo item = shortcuts.get(i);
Winson Chung4d279d92011-07-21 11:46:32 -07004563
4564 // Short circuit if we are loading dock items for a configuration which has no dock
4565 if (item.container == LauncherSettings.Favorites.CONTAINER_HOTSEAT &&
4566 mHotseat == null) {
4567 continue;
4568 }
4569
Joe Onorato9c1289c2009-08-17 11:03:03 -04004570 switch (item.itemType) {
4571 case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
4572 case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
Winson Chungf0c6ae02012-03-21 16:10:31 -07004573 ShortcutInfo info = (ShortcutInfo) item;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004574 View shortcut = createShortcut(info);
Adam Cohendcd297f2013-06-18 13:13:40 -07004575
Winson Chung64359a52013-07-08 17:17:08 -07004576 /*
4577 * TODO: FIX collision case
4578 */
Winson Chungce376632013-07-11 16:12:41 -07004579 if (item.container == LauncherSettings.Favorites.CONTAINER_DESKTOP) {
4580 CellLayout cl = mWorkspace.getScreenWithId(item.screenId);
4581 if (cl != null && cl.isOccupied(item.cellX, item.cellY)) {
Adam Cohen8f006ed2014-04-03 18:12:34 -07004582 View v = cl.getChildAt(item.cellX, item.cellY);
4583 Object tag = v.getTag();
4584 String desc = "Collision while binding workspace item: " + item
4585 + ". Collides with " + tag;
4586 if (LauncherAppState.isDogfoodBuild()) {
4587 throw (new RuntimeException(desc));
4588 } else {
4589 Log.d(TAG, desc);
4590 }
Winson Chungce376632013-07-11 16:12:41 -07004591 }
Winson Chung64359a52013-07-08 17:17:08 -07004592 }
4593
Adam Cohendcd297f2013-06-18 13:13:40 -07004594 workspace.addInScreenFromBind(shortcut, item.container, item.screenId, item.cellX,
4595 item.cellY, 1, 1);
Winson Chung997a9232013-07-24 15:33:46 -07004596 if (animateIcons) {
Winson Chung64359a52013-07-08 17:17:08 -07004597 // Animate all the applications up now
4598 shortcut.setAlpha(0f);
4599 shortcut.setScaleX(0f);
4600 shortcut.setScaleY(0f);
4601 bounceAnims.add(createNewAppBounceAnimation(shortcut, i));
Winson Chung997a9232013-07-24 15:33:46 -07004602 newShortcutsScreenId = item.screenId;
Winson Chungf0c6ae02012-03-21 16:10:31 -07004603 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004604 break;
Adam Cohendf2cc412011-04-27 16:56:57 -07004605 case LauncherSettings.Favorites.ITEM_TYPE_FOLDER:
Winson Chung3d503fb2011-07-13 17:25:49 -07004606 FolderIcon newFolder = FolderIcon.fromXml(R.layout.folder_icon, this,
Michael Jurka0142d492010-08-25 17:46:15 -07004607 (ViewGroup) workspace.getChildAt(workspace.getCurrentPage()),
Adam Cohendf2cc412011-04-27 16:56:57 -07004608 (FolderInfo) item, mIconCache);
Adam Cohendcd297f2013-06-18 13:13:40 -07004609 workspace.addInScreenFromBind(newFolder, item.container, item.screenId, item.cellX,
4610 item.cellY, 1, 1);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004611 break;
Winson Chungc763c4e2013-07-19 13:49:06 -07004612 default:
4613 throw new RuntimeException("Invalid Item Type");
Joe Onorato9c1289c2009-08-17 11:03:03 -04004614 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004615 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004616
Winson Chung997a9232013-07-24 15:33:46 -07004617 if (animateIcons) {
4618 // Animate to the correct page
4619 if (newShortcutsScreenId > -1) {
4620 long currentScreenId = mWorkspace.getScreenIdForPageIndex(mWorkspace.getNextPage());
Winson Chung94d67682013-09-25 16:29:40 -07004621 final int newScreenIndex = mWorkspace.getPageIndexForScreenId(newShortcutsScreenId);
Winson Chungb2323832013-10-03 15:22:09 -07004622 final Runnable startBounceAnimRunnable = new Runnable() {
4623 public void run() {
4624 anim.playTogether(bounceAnims);
4625 anim.start();
4626 }
4627 };
Winson Chung997a9232013-07-24 15:33:46 -07004628 if (newShortcutsScreenId != currentScreenId) {
Winson Chung94d67682013-09-25 16:29:40 -07004629 // We post the animation slightly delayed to prevent slowdowns
4630 // when we are loading right after we return to launcher.
4631 mWorkspace.postDelayed(new Runnable() {
4632 public void run() {
Ian Parkinson94449152014-01-21 13:09:59 +00004633 if (mWorkspace != null) {
4634 mWorkspace.snapToPage(newScreenIndex);
4635 mWorkspace.postDelayed(startBounceAnimRunnable,
4636 NEW_APPS_ANIMATION_DELAY);
4637 }
Winson Chung94d67682013-09-25 16:29:40 -07004638 }
4639 }, NEW_APPS_PAGE_MOVE_DELAY);
Winson Chungb2323832013-10-03 15:22:09 -07004640 } else {
4641 mWorkspace.postDelayed(startBounceAnimRunnable, NEW_APPS_ANIMATION_DELAY);
Winson Chung997a9232013-07-24 15:33:46 -07004642 }
4643 }
Winson Chung64359a52013-07-08 17:17:08 -07004644 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004645 workspace.requestLayout();
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004646 }
4647
Joe Onorato9c1289c2009-08-17 11:03:03 -04004648 /**
4649 * Implementation of the method from LauncherModel.Callbacks.
4650 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004651 public void bindFolders(final HashMap<Long, FolderInfo> folders) {
Winson Chungd64d1762013-08-20 14:37:16 -07004652 Runnable r = new Runnable() {
4653 public void run() {
4654 bindFolders(folders);
4655 }
4656 };
4657 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004658 return;
4659 }
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07004660 sFolders.clear();
4661 sFolders.putAll(folders);
The Android Open Source Project31dd5032009-03-03 19:32:27 -08004662 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004663
4664 /**
4665 * Add the views for a widget to the workspace.
4666 *
4667 * Implementation of the method from LauncherModel.Callbacks.
4668 */
Michael Jurka7607c2f2013-04-03 14:33:19 -07004669 public void bindAppWidget(final LauncherAppWidgetInfo item) {
Winson Chungd64d1762013-08-20 14:37:16 -07004670 Runnable r = new Runnable() {
4671 public void run() {
4672 bindAppWidget(item);
4673 }
4674 };
4675 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004676 return;
4677 }
Joe Onoratoef2efcf2010-10-27 13:21:00 -07004678
Daniel Sandler843e8602010-06-07 14:59:01 -04004679 final long start = DEBUG_WIDGETS ? SystemClock.uptimeMillis() : 0;
4680 if (DEBUG_WIDGETS) {
4681 Log.d(TAG, "bindAppWidget: " + item);
4682 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004683 final Workspace workspace = mWorkspace;
4684
Sunny Goyalff572272014-07-23 13:58:07 -07004685 AppWidgetProviderInfo appWidgetInfo;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004686 if (!mIsSafeModeEnabled
4687 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_PROVIDER_NOT_READY) == 0)
4688 && ((item.restoreStatus & LauncherAppWidgetInfo.FLAG_ID_NOT_VALID) != 0)) {
Sunny Goyalff572272014-07-23 13:58:07 -07004689
4690 appWidgetInfo = mModel.findAppWidgetProviderInfoWithComponent(this, item.providerName);
4691 if (appWidgetInfo == null) {
4692 if (DEBUG_WIDGETS) {
4693 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4694 + " belongs to component " + item.providerName
4695 + ", as the povider is null");
4696 }
4697 LauncherModel.deleteItemFromDatabase(this, item);
4698 return;
4699 }
4700 // Note: This assumes that the id remap broadcast is received before this step.
4701 // If that is not the case, the id remap will be ignored and user may see the
4702 // click to setup view.
4703 PendingAddWidgetInfo pendingInfo = new PendingAddWidgetInfo(appWidgetInfo, null, null);
4704 pendingInfo.spanX = item.spanX;
4705 pendingInfo.spanY = item.spanY;
4706 pendingInfo.minSpanX = item.minSpanX;
4707 pendingInfo.minSpanY = item.minSpanY;
4708 Bundle options =
4709 AppsCustomizePagedView.getDefaultOptionsForWidget(this, pendingInfo);
4710
Sunny Goyalff572272014-07-23 13:58:07 -07004711 int newWidgetId = mAppWidgetHost.allocateAppWidgetId();
Sunny Goyalffe83f12014-08-14 17:39:34 -07004712 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed(
4713 newWidgetId, appWidgetInfo, options);
Sunny Goyalff572272014-07-23 13:58:07 -07004714
4715 // TODO consider showing a permission dialog when the widget is clicked.
4716 if (!success) {
4717 mAppWidgetHost.deleteAppWidgetId(newWidgetId);
4718 if (DEBUG_WIDGETS) {
4719 Log.d(TAG, "Removing restored widget: id=" + item.appWidgetId
4720 + " belongs to component " + item.providerName
4721 + ", as the launcher is unable to bing a new widget id");
4722 }
4723 LauncherModel.deleteItemFromDatabase(this, item);
4724 return;
4725 }
4726
4727 item.appWidgetId = newWidgetId;
4728
4729 // If the widget has a configure activity, it is still needs to set it up, otherwise
4730 // the widget is ready to go.
4731 item.restoreStatus = (appWidgetInfo.configure == null)
4732 ? LauncherAppWidgetInfo.RESTORE_COMPLETED
4733 : LauncherAppWidgetInfo.FLAG_UI_NOT_READY;
4734
4735 LauncherModel.updateItemInDatabase(this, item);
4736 }
4737
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004738 if (!mIsSafeModeEnabled && item.restoreStatus == LauncherAppWidgetInfo.RESTORE_COMPLETED) {
Sunny Goyal651077b2014-06-30 14:15:31 -07004739 final int appWidgetId = item.appWidgetId;
4740 appWidgetInfo = mAppWidgetManager.getAppWidgetInfo(appWidgetId);
4741 if (DEBUG_WIDGETS) {
4742 Log.d(TAG, "bindAppWidget: id=" + item.appWidgetId + " belongs to component " + appWidgetInfo.provider);
4743 }
Daniel Sandler843e8602010-06-07 14:59:01 -04004744
Sunny Goyal651077b2014-06-30 14:15:31 -07004745 item.hostView = mAppWidgetHost.createView(this, appWidgetId, appWidgetInfo);
4746 } else {
4747 appWidgetInfo = null;
Sunny Goyal9b4b0812014-10-08 10:47:28 -07004748 PendingAppWidgetHostView view = new PendingAppWidgetHostView(this, item,
4749 mIsSafeModeEnabled);
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004750 view.updateIcon(mIconCache);
4751 item.hostView = view;
Sunny Goyal651077b2014-06-30 14:15:31 -07004752 item.hostView.updateAppWidget(null);
Sunny Goyalff572272014-07-23 13:58:07 -07004753 item.hostView.setOnClickListener(this);
Sunny Goyal651077b2014-06-30 14:15:31 -07004754 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004755
Joe Onorato9c1289c2009-08-17 11:03:03 -04004756 item.hostView.setTag(item);
Winson Chung211bac32012-05-15 13:43:57 -07004757 item.onBindAppWidget(this);
Joe Onorato9c1289c2009-08-17 11:03:03 -04004758
Adam Cohendcd297f2013-06-18 13:13:40 -07004759 workspace.addInScreen(item.hostView, item.container, item.screenId, item.cellX,
Joe Onorato9c1289c2009-08-17 11:03:03 -04004760 item.cellY, item.spanX, item.spanY, false);
Adam Cohended9f8d2010-11-03 13:25:16 -07004761 addWidgetToAutoAdvanceIfNeeded(item.hostView, appWidgetInfo);
4762
Joe Onorato9c1289c2009-08-17 11:03:03 -04004763 workspace.requestLayout();
4764
Daniel Sandler843e8602010-06-07 14:59:01 -04004765 if (DEBUG_WIDGETS) {
4766 Log.d(TAG, "bound widget id="+item.appWidgetId+" in "
4767 + (SystemClock.uptimeMillis()-start) + "ms");
4768 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004769 }
4770
Sunny Goyalff572272014-07-23 13:58:07 -07004771 /**
4772 * Restores a pending widget.
4773 *
4774 * @param appWidgetId The app widget id
4775 * @param cellInfo The position on screen where to create the widget.
4776 */
4777 private void completeRestoreAppWidget(final int appWidgetId) {
4778 LauncherAppWidgetHostView view = mWorkspace.getWidgetForAppWidgetId(appWidgetId);
4779 if ((view == null) || !(view instanceof PendingAppWidgetHostView)) {
4780 Log.e(TAG, "Widget update called, when the widget no longer exists.");
4781 return;
4782 }
4783
Sunny Goyal0fc1be12014-08-11 17:05:23 -07004784 LauncherAppWidgetInfo info = (LauncherAppWidgetInfo) view.getTag();
Sunny Goyalff572272014-07-23 13:58:07 -07004785 info.restoreStatus = LauncherAppWidgetInfo.RESTORE_COMPLETED;
4786
4787 mWorkspace.reinflateWidgetsIfNecessary();
4788 LauncherModel.updateItemInDatabase(this, info);
4789 }
4790
Adam Cohen1462de32012-07-24 22:34:36 -07004791 public void onPageBoundSynchronously(int page) {
4792 mSynchronouslyBoundPages.add(page);
4793 }
4794
Joe Onorato9c1289c2009-08-17 11:03:03 -04004795 /**
4796 * Callback saying that there aren't any more items to bind.
4797 *
4798 * Implementation of the method from LauncherModel.Callbacks.
4799 */
Adam Cohene25af792013-06-06 23:08:25 -07004800 public void finishBindingItems(final boolean upgradePath) {
Winson Chungd64d1762013-08-20 14:37:16 -07004801 Runnable r = new Runnable() {
4802 public void run() {
4803 finishBindingItems(upgradePath);
4804 }
4805 };
4806 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004807 return;
4808 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004809 if (mSavedState != null) {
4810 if (!mWorkspace.hasFocus()) {
Michael Jurka0142d492010-08-25 17:46:15 -07004811 mWorkspace.getChildAt(mWorkspace.getCurrentPage()).requestFocus();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004812 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004813 mSavedState = null;
4814 }
4815
Adam Cohen1462de32012-07-24 22:34:36 -07004816 mWorkspace.restoreInstanceStateForRemainingPages();
Joe Onorato9c1289c2009-08-17 11:03:03 -04004817
Anjali Koppalff7ceff2014-05-01 18:26:37 -07004818 setWorkspaceLoading(false);
Adam Cohen3ed316a2014-07-23 18:21:20 -07004819 sendLoadingCompleteBroadcastIfNecessary();
Adam Cohendb364c32014-05-20 14:23:40 -07004820
4821 // If we received the result of any pending adds while the loader was running (e.g. the
4822 // widget configuration forced an orientation change), process them now.
4823 if (sPendingAddItem != null) {
4824 final long screenId = completeAdd(sPendingAddItem);
4825
4826 // TODO: this moves the user to the page where the pending item was added. Ideally,
4827 // the screen would be guaranteed to exist after bind, and the page would be set through
4828 // the workspace restore process.
4829 mWorkspace.post(new Runnable() {
4830 @Override
4831 public void run() {
4832 mWorkspace.snapToScreenId(screenId);
4833 }
4834 });
4835 sPendingAddItem = null;
4836 }
4837
Adam Cohene25af792013-06-06 23:08:25 -07004838 if (upgradePath) {
Adam Cohen4caf2982013-08-20 18:54:31 -07004839 mWorkspace.getUniqueComponents(true, null);
4840 mIntentsOnWorkspaceFromUpgradePath = mWorkspace.getUniqueComponents(true, null);
Adam Cohene25af792013-06-06 23:08:25 -07004841 }
Sunny Goyale755d462014-07-22 13:48:29 -07004842 PackageInstallerCompat.getInstance(this).onFinishBind();
Adam Cohen9211d422014-10-07 18:14:53 -07004843
4844 if (mLauncherCallbacks != null) {
4845 mLauncherCallbacks.finishBindingItems(upgradePath);
4846 }
Winson Chungf0c6ae02012-03-21 16:10:31 -07004847 }
4848
Adam Cohen3ed316a2014-07-23 18:21:20 -07004849 private void sendLoadingCompleteBroadcastIfNecessary() {
4850 if (!mSharedPrefs.getBoolean(FIRST_LOAD_COMPLETE, false)) {
4851 String permission =
4852 getResources().getString(R.string.receive_first_load_broadcast_permission);
4853 Intent intent = new Intent(ACTION_FIRST_LOAD_COMPLETE);
4854 sendBroadcast(intent, permission);
4855 SharedPreferences.Editor editor = mSharedPrefs.edit();
4856 editor.putBoolean(FIRST_LOAD_COMPLETE, true);
4857 editor.apply();
4858 }
4859 }
4860
Winson Chunga0b7e862013-09-05 16:03:15 -07004861 public boolean isAllAppsButtonRank(int rank) {
4862 if (mHotseat != null) {
4863 return mHotseat.isAllAppsButtonRank(rank);
4864 }
4865 return false;
4866 }
4867
Winson Chunga2413752012-04-03 14:22:34 -07004868 private boolean canRunNewAppsAnimation() {
4869 long diff = System.currentTimeMillis() - mDragController.getLastGestureUpTime();
4870 return diff > (NEW_APPS_ANIMATION_INACTIVE_TIMEOUT_SECONDS * 1000);
4871 }
4872
Winson Chungc9168342013-06-26 14:54:55 -07004873 private ValueAnimator createNewAppBounceAnimation(View v, int i) {
4874 ValueAnimator bounceAnim = LauncherAnimUtils.ofPropertyValuesHolder(v,
4875 PropertyValuesHolder.ofFloat("alpha", 1f),
4876 PropertyValuesHolder.ofFloat("scaleX", 1f),
4877 PropertyValuesHolder.ofFloat("scaleY", 1f));
4878 bounceAnim.setDuration(InstallShortcutReceiver.NEW_SHORTCUT_BOUNCE_DURATION);
4879 bounceAnim.setStartDelay(i * InstallShortcutReceiver.NEW_SHORTCUT_STAGGER_DELAY);
4880 bounceAnim.setInterpolator(new SmoothPagedView.OvershootInterpolator());
4881 return bounceAnim;
4882 }
4883
Adam Cohen27772732013-11-11 14:00:56 +00004884 public boolean useVerticalBarLayout() {
4885 return LauncherAppState.getInstance().getDynamicGrid().
4886 getDeviceProfile().isVerticalBarLayout();
4887 }
4888
Mac Duy Hai6def4f92013-11-28 16:17:33 +00004889 protected Rect getSearchBarBounds() {
4890 return LauncherAppState.getInstance().getDynamicGrid().
4891 getDeviceProfile().getSearchBarBounds();
4892 }
4893
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004894 @Override
4895 public void bindSearchablesChanged() {
Winson Chungc51db6a2011-10-05 11:44:49 -07004896 boolean searchVisible = updateGlobalSearchIcon();
4897 boolean voiceVisible = updateVoiceSearchIcon(searchVisible);
Winson Chungadf0c182012-08-23 14:59:07 -07004898 if (mSearchDropTargetBar != null) {
4899 mSearchDropTargetBar.onSearchPackagesChanged(searchVisible, voiceVisible);
4900 }
Narayan Kamathcb1a4772011-06-28 13:46:59 +01004901 }
4902
Amith Yamasani6d7fe502010-11-16 09:05:07 -08004903 /**
Joe Onorato9c1289c2009-08-17 11:03:03 -04004904 * Add the icons for all apps.
4905 *
4906 * Implementation of the method from LauncherModel.Callbacks.
4907 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004908 public void bindAllApplications(final ArrayList<AppInfo> apps) {
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004909 if (LauncherAppState.isDisableAllApps()) {
Winson Chungc58497e2013-09-03 17:48:37 -07004910 if (mIntentsOnWorkspaceFromUpgradePath != null) {
4911 if (LauncherModel.UPGRADE_USE_MORE_APPS_FOLDER) {
4912 getHotseat().addAllAppsFolder(mIconCache, apps,
4913 mIntentsOnWorkspaceFromUpgradePath, Launcher.this, mWorkspace);
4914 }
4915 mIntentsOnWorkspaceFromUpgradePath = null;
Daniel Sandler8707e0f2013-08-15 15:54:18 -07004916 }
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08004917 if (mAppsCustomizeContent != null) {
4918 mAppsCustomizeContent.onPackagesUpdated(
4919 LauncherModel.getSortedWidgetsAndShortcuts(this));
4920 }
Winson Chungc58497e2013-09-03 17:48:37 -07004921 } else {
Winson Chungad7db6e2013-10-08 14:01:06 -07004922 if (mAppsCustomizeContent != null) {
Winson Chungc58497e2013-09-03 17:48:37 -07004923 mAppsCustomizeContent.setApps(apps);
Winson Chung6e1c0d32013-10-25 15:24:24 -07004924 mAppsCustomizeContent.onPackagesUpdated(
4925 LauncherModel.getSortedWidgetsAndShortcuts(this));
Winson Chungc58497e2013-09-03 17:48:37 -07004926 }
Winson Chung785d2eb2011-04-14 16:08:02 -07004927 }
Adam Cohen9211d422014-10-07 18:14:53 -07004928 if (mLauncherCallbacks != null) {
4929 mLauncherCallbacks.bindAllApplications(apps);
4930 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004931 }
4932
4933 /**
4934 * A package was updated.
4935 *
4936 * Implementation of the method from LauncherModel.Callbacks.
4937 */
Michael Jurkaeadbfc52013-09-04 00:45:37 +02004938 public void bindAppsUpdated(final ArrayList<AppInfo> apps) {
Winson Chungd64d1762013-08-20 14:37:16 -07004939 Runnable r = new Runnable() {
4940 public void run() {
4941 bindAppsUpdated(apps);
4942 }
4943 };
4944 if (waitUntilResume(r)) {
Michael Jurka7607c2f2013-04-03 14:33:19 -07004945 return;
4946 }
4947
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08004948 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07004949 mAppsCustomizeContent != null) {
4950 mAppsCustomizeContent.updateApps(apps);
4951 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04004952 }
4953
Sunny Goyal4390ace2014-10-13 11:33:11 -07004954 @Override
4955 public void bindWidgetsRestored(final ArrayList<LauncherAppWidgetInfo> widgets) {
4956 Runnable r = new Runnable() {
4957 public void run() {
4958 bindWidgetsRestored(widgets);
4959 }
4960 };
4961 if (waitUntilResume(r)) {
4962 return;
4963 }
4964 mWorkspace.widgetsRestored(widgets);
4965 }
4966
Joe Onorato9c1289c2009-08-17 11:03:03 -04004967 /**
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004968 * Some shortcuts were updated in the background.
4969 *
4970 * Implementation of the method from LauncherModel.Callbacks.
4971 */
Sunny Goyal4390ace2014-10-13 11:33:11 -07004972 @Override
4973 public void bindShortcutsChanged(final ArrayList<ShortcutInfo> updated,
4974 final ArrayList<ShortcutInfo> removed, final UserHandleCompat user) {
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004975 Runnable r = new Runnable() {
4976 public void run() {
Sunny Goyal4390ace2014-10-13 11:33:11 -07004977 bindShortcutsChanged(updated, removed, user);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004978 }
4979 };
4980 if (waitUntilResume(r)) {
4981 return;
4982 }
4983
Sunny Goyal4390ace2014-10-13 11:33:11 -07004984 if (!updated.isEmpty()) {
4985 mWorkspace.updateShortcuts(updated);
4986 }
4987
4988 if (!removed.isEmpty()) {
4989 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
4990 for (ShortcutInfo si : removed) {
4991 removedComponents.add(si.getTargetComponent());
4992 }
4993 mWorkspace.removeItemsByComponentName(removedComponents, user);
4994 // Notify the drag controller
4995 mDragController.onAppsRemoved(new ArrayList<String>(), removedComponents);
Sunny Goyalb50cc8c2014-10-06 16:23:56 -07004996 }
4997 }
4998
4999 /**
Chris Wrenaeff7ea2014-02-14 16:59:24 -05005000 * Update the state of a package, typically related to install state.
5001 *
5002 * Implementation of the method from LauncherModel.Callbacks.
5003 */
Sunny Goyale755d462014-07-22 13:48:29 -07005004 @Override
5005 public void updatePackageState(ArrayList<PackageInstallInfo> installInfo) {
Chris Wrenaeff7ea2014-02-14 16:59:24 -05005006 if (mWorkspace != null) {
Sunny Goyale755d462014-07-22 13:48:29 -07005007 mWorkspace.updatePackageState(installInfo);
Chris Wrenaeff7ea2014-02-14 16:59:24 -05005008 }
5009 }
5010
5011 /**
Sunny Goyala22666f2014-09-18 13:25:15 -07005012 * Update the label and icon of all the icons in a package
5013 *
5014 * Implementation of the method from LauncherModel.Callbacks.
5015 */
5016 @Override
5017 public void updatePackageBadge(String packageName) {
5018 if (mWorkspace != null) {
5019 mWorkspace.updatePackageBadge(packageName, UserHandleCompat.myUserHandle());
5020 }
5021 }
5022
5023 /**
Winson Chung83892cc2013-05-01 16:53:33 -07005024 * A package was uninstalled. We take both the super set of packageNames
5025 * in addition to specific applications to remove, the reason being that
5026 * this can be called when a package is updated as well. In that scenario,
5027 * we only remove specific components from the workspace, where as
5028 * package-removal should clear all items by package name.
Joe Onorato9c1289c2009-08-17 11:03:03 -04005029 *
Sunny Goyal1a745e82014-10-02 15:58:31 -07005030 * @param reason if non-zero, the icons are not permanently removed, rather marked as disabled.
Joe Onorato9c1289c2009-08-17 11:03:03 -04005031 * Implementation of the method from LauncherModel.Callbacks.
5032 */
Sunny Goyal1a745e82014-10-02 15:58:31 -07005033 @Override
Winson Chung83892cc2013-05-01 16:53:33 -07005034 public void bindComponentsRemoved(final ArrayList<String> packageNames,
Sunny Goyal1a745e82014-10-02 15:58:31 -07005035 final ArrayList<AppInfo> appInfos, final UserHandleCompat user, final int reason) {
Winson Chungd64d1762013-08-20 14:37:16 -07005036 Runnable r = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07005037 public void run() {
Sunny Goyal1a745e82014-10-02 15:58:31 -07005038 bindComponentsRemoved(packageNames, appInfos, user, reason);
Winson Chung83892cc2013-05-01 16:53:33 -07005039 }
Winson Chungd64d1762013-08-20 14:37:16 -07005040 };
5041 if (waitUntilResume(r)) {
Winson Chung83892cc2013-05-01 16:53:33 -07005042 return;
5043 }
5044
Sunny Goyal1a745e82014-10-02 15:58:31 -07005045 if (reason == 0) {
Sunny Goyal4390ace2014-10-13 11:33:11 -07005046 HashSet<ComponentName> removedComponents = new HashSet<ComponentName>();
5047 for (AppInfo info : appInfos) {
5048 removedComponents.add(info.componentName);
5049 }
Sunny Goyal1a745e82014-10-02 15:58:31 -07005050 if (!packageNames.isEmpty()) {
5051 mWorkspace.removeItemsByPackageName(packageNames, user);
5052 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07005053 if (!removedComponents.isEmpty()) {
5054 mWorkspace.removeItemsByComponentName(removedComponents, user);
Sunny Goyal1a745e82014-10-02 15:58:31 -07005055 }
Sunny Goyal4390ace2014-10-13 11:33:11 -07005056 // Notify the drag controller
5057 mDragController.onAppsRemoved(packageNames, removedComponents);
5058
Sunny Goyal1a745e82014-10-02 15:58:31 -07005059 } else {
5060 mWorkspace.disableShortcutsByPackageName(packageNames, user, reason);
Joe Onorato36115782010-06-17 13:28:48 -04005061 }
Winson Chungf0ea4d32011-06-06 14:27:16 -07005062
Winson Chungdf95eb12013-10-16 14:57:07 -07005063 // Update AllApps
Nilesh Agrawal16f3ea82014-01-09 17:14:01 -08005064 if (!LauncherAppState.isDisableAllApps() &&
Winson Chungc58497e2013-09-03 17:48:37 -07005065 mAppsCustomizeContent != null) {
5066 mAppsCustomizeContent.removeApps(appInfos);
5067 }
Joe Onorato9c1289c2009-08-17 11:03:03 -04005068 }
Joe Onoratobe386092009-11-17 17:32:16 -08005069
5070 /**
Winson Chung80baf5a2010-08-09 16:03:15 -07005071 * A number of packages were updated.
5072 */
Michael Jurkac402cd92013-05-20 15:49:32 +02005073 private ArrayList<Object> mWidgetsAndShortcuts;
5074 private Runnable mBindPackagesUpdatedRunnable = new Runnable() {
Winson Chung83892cc2013-05-01 16:53:33 -07005075 public void run() {
Michael Jurkac402cd92013-05-20 15:49:32 +02005076 bindPackagesUpdated(mWidgetsAndShortcuts);
5077 mWidgetsAndShortcuts = null;
Winson Chung83892cc2013-05-01 16:53:33 -07005078 }
Michael Jurkac402cd92013-05-20 15:49:32 +02005079 };
Michael Jurkac402cd92013-05-20 15:49:32 +02005080 public void bindPackagesUpdated(final ArrayList<Object> widgetsAndShortcuts) {
5081 if (waitUntilResume(mBindPackagesUpdatedRunnable, true)) {
5082 mWidgetsAndShortcuts = widgetsAndShortcuts;
Winson Chung83892cc2013-05-01 16:53:33 -07005083 return;
5084 }
5085
Winson Chung64359a52013-07-08 17:17:08 -07005086 // Update the widgets pane
Nilesh Agrawal306c1cf2013-12-12 14:56:00 -08005087 if (mAppsCustomizeContent != null) {
Michael Jurkac402cd92013-05-20 15:49:32 +02005088 mAppsCustomizeContent.onPackagesUpdated(widgetsAndShortcuts);
Winson Chung785d2eb2011-04-14 16:08:02 -07005089 }
Winson Chung80baf5a2010-08-09 16:03:15 -07005090 }
5091
Winson Chung400438b2011-01-16 17:53:48 -08005092 private int mapConfigurationOriActivityInfoOri(int configOri) {
5093 final Display d = getWindowManager().getDefaultDisplay();
5094 int naturalOri = Configuration.ORIENTATION_LANDSCAPE;
5095 switch (d.getRotation()) {
5096 case Surface.ROTATION_0:
5097 case Surface.ROTATION_180:
5098 // We are currently in the same basic orientation as the natural orientation
5099 naturalOri = configOri;
5100 break;
5101 case Surface.ROTATION_90:
5102 case Surface.ROTATION_270:
5103 // We are currently in the other basic orientation to the natural orientation
5104 naturalOri = (configOri == Configuration.ORIENTATION_LANDSCAPE) ?
5105 Configuration.ORIENTATION_PORTRAIT : Configuration.ORIENTATION_LANDSCAPE;
5106 break;
5107 }
5108
5109 int[] oriMap = {
5110 ActivityInfo.SCREEN_ORIENTATION_PORTRAIT,
5111 ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE,
5112 ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT,
5113 ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE
5114 };
5115 // Since the map starts at portrait, we need to offset if this device's natural orientation
5116 // is landscape.
5117 int indexOffset = 0;
5118 if (naturalOri == Configuration.ORIENTATION_LANDSCAPE) {
5119 indexOffset = 1;
5120 }
5121 return oriMap[(d.getRotation() + indexOffset) % 4];
5122 }
Adam Cohen446e9402011-09-15 18:21:21 -07005123
Winson Chung4b919f82012-05-01 10:44:08 -07005124 public boolean isRotationEnabled() {
Michael Jurka0a457bf2012-11-19 14:05:05 -08005125 boolean enableRotation = sForceEnableRotation ||
Winson Chung4b919f82012-05-01 10:44:08 -07005126 getResources().getBoolean(R.bool.allow_rotation);
5127 return enableRotation;
Winson Chung400438b2011-01-16 17:53:48 -08005128 }
Winson Chung4b919f82012-05-01 10:44:08 -07005129 public void lockScreenOrientation() {
5130 if (isRotationEnabled()) {
5131 setRequestedOrientation(mapConfigurationOriActivityInfoOri(getResources()
5132 .getConfiguration().orientation));
5133 }
5134 }
5135 public void unlockScreenOrientation(boolean immediate) {
5136 if (isRotationEnabled()) {
5137 if (immediate) {
Winson Chung641d71d2012-04-26 15:58:01 -07005138 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
Winson Chung4b919f82012-05-01 10:44:08 -07005139 } else {
5140 mHandler.postDelayed(new Runnable() {
5141 public void run() {
5142 setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
5143 }
5144 }, mRestoreScreenOrientationDelay);
Winson Chung641d71d2012-04-26 15:58:01 -07005145 }
Winson Chung4b919f82012-05-01 10:44:08 -07005146 }
Winson Chung400438b2011-01-16 17:53:48 -08005147 }
5148
Winson Chunge43a1e72014-01-15 10:33:02 -08005149 protected boolean isLauncherPreinstalled() {
Adam Cohen9211d422014-10-07 18:14:53 -07005150 if (mLauncherCallbacks != null) {
5151 return mLauncherCallbacks.isLauncherPreinstalled();
5152 }
Winson Chunge43a1e72014-01-15 10:33:02 -08005153 PackageManager pm = getPackageManager();
5154 try {
5155 ApplicationInfo ai = pm.getApplicationInfo(getComponentName().getPackageName(), 0);
5156 if ((ai.flags & ApplicationInfo.FLAG_SYSTEM) != 0) {
5157 return true;
5158 } else {
5159 return false;
5160 }
5161 } catch (NameNotFoundException e) {
5162 e.printStackTrace();
5163 return false;
5164 }
5165 }
5166
Adam Cohenea90f832014-05-21 15:03:34 -07005167 /**
5168 * This method indicates whether or not we should suggest default wallpaper dimensions
5169 * when our wallpaper cropper was not yet used to set a wallpaper.
5170 */
5171 protected boolean overrideWallpaperDimensions() {
Adam Cohen9211d422014-10-07 18:14:53 -07005172 if (mLauncherCallbacks != null) {
5173 return mLauncherCallbacks.overrideWallpaperDimensions();
5174 }
Adam Cohenea90f832014-05-21 15:03:34 -07005175 return true;
5176 }
5177
Adam Cohen432609a2014-03-13 17:03:22 -07005178 /**
5179 * To be overridden by subclasses to indicate that there is an activity to launch
5180 * before showing the standard launcher experience.
5181 */
5182 protected boolean hasFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07005183 if (mLauncherCallbacks != null) {
5184 return mLauncherCallbacks.hasFirstRunActivity();
5185 }
Adam Cohen432609a2014-03-13 17:03:22 -07005186 return false;
5187 }
5188
5189 /**
5190 * To be overridden by subclasses to launch any first run activity
5191 */
5192 protected Intent getFirstRunActivity() {
Adam Cohen9211d422014-10-07 18:14:53 -07005193 if (mLauncherCallbacks != null) {
5194 return mLauncherCallbacks.getFirstRunActivity();
5195 }
Adam Cohen432609a2014-03-13 17:03:22 -07005196 return null;
5197 }
5198
Winson Chunga6945242014-01-08 14:04:34 -08005199 private boolean shouldRunFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08005200 return !ActivityManager.isRunningInTestHarness() &&
5201 !mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
Winson Chunga6945242014-01-08 14:04:34 -08005202 }
5203
Adam Cohen4a9423d2014-03-28 14:22:31 -07005204 protected boolean hasRunFirstRunActivity() {
5205 return mSharedPrefs.getBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, false);
5206 }
5207
Adam Cohen432609a2014-03-13 17:03:22 -07005208 public boolean showFirstRunActivity() {
Adam Cohenbc4539d2014-01-14 14:05:32 -08005209 if (shouldRunFirstRunActivity() &&
5210 hasFirstRunActivity()) {
Winson Chunga6945242014-01-08 14:04:34 -08005211 Intent firstRunIntent = getFirstRunActivity();
5212 if (firstRunIntent != null) {
5213 startActivity(firstRunIntent);
5214 markFirstRunActivityShown();
Adam Cohen432609a2014-03-13 17:03:22 -07005215 return true;
Winson Chunga6945242014-01-08 14:04:34 -08005216 }
5217 }
Adam Cohen432609a2014-03-13 17:03:22 -07005218 return false;
Winson Chunga6945242014-01-08 14:04:34 -08005219 }
5220
5221 private void markFirstRunActivityShown() {
5222 SharedPreferences.Editor editor = mSharedPrefs.edit();
5223 editor.putBoolean(FIRST_RUN_ACTIVITY_DISPLAYED, true);
5224 editor.apply();
5225 }
5226
Adam Cohen432609a2014-03-13 17:03:22 -07005227 /**
5228 * To be overridden by subclasses to indicate that there is an in-activity full-screen intro
5229 * screen that must be displayed and dismissed.
5230 */
5231 protected boolean hasDismissableIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07005232 if (mLauncherCallbacks != null) {
5233 return mLauncherCallbacks.hasDismissableIntroScreen();
5234 }
Adam Cohen432609a2014-03-13 17:03:22 -07005235 return false;
5236 }
5237
5238 /**
5239 * Full screen intro screen to be shown and dismissed before the launcher can be used.
5240 */
5241 protected View getIntroScreen() {
Adam Cohen9211d422014-10-07 18:14:53 -07005242 if (mLauncherCallbacks != null) {
5243 return mLauncherCallbacks.getIntroScreen();
5244 }
Adam Cohen432609a2014-03-13 17:03:22 -07005245 return null;
5246 }
5247
5248 /**
5249 * To be overriden by subclasses to indicate whether the in-activity intro screen has been
5250 * dismissed. This method is ignored if #hasDismissableIntroScreen returns false.
5251 */
5252 private boolean shouldShowIntroScreen() {
5253 return hasDismissableIntroScreen() &&
5254 !mSharedPrefs.getBoolean(INTRO_SCREEN_DISMISSED, false);
5255 }
5256
5257 protected void showIntroScreen() {
5258 View introScreen = getIntroScreen();
5259 changeWallpaperVisiblity(false);
5260 if (introScreen != null) {
5261 mDragLayer.showOverlayView(introScreen);
5262 }
Adam Cohenc3e12c72014-10-31 16:15:36 -07005263 if (mLauncherOverlayContainer != null) {
5264 mLauncherOverlayContainer.setVisibility(View.INVISIBLE);
5265 }
Adam Cohen432609a2014-03-13 17:03:22 -07005266 }
5267
5268 public void dismissIntroScreen() {
5269 markIntroScreenDismissed();
5270 if (showFirstRunActivity()) {
5271 // We delay hiding the intro view until the first run activity is showing. This
5272 // avoids a blip.
5273 mWorkspace.postDelayed(new Runnable() {
5274 @Override
5275 public void run() {
5276 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07005277 if (mLauncherOverlayContainer != null) {
5278 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
5279 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07005280 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005281 }
5282 }, ACTIVITY_START_DELAY);
5283 } else {
5284 mDragLayer.dismissOverlayView();
Adam Cohenc3e12c72014-10-31 16:15:36 -07005285 if (mLauncherOverlayContainer != null) {
5286 mLauncherOverlayContainer.setVisibility(View.VISIBLE);
5287 }
Sunny Goyal88d60f12014-09-08 03:47:29 -07005288 showFirstRunClings();
Adam Cohen432609a2014-03-13 17:03:22 -07005289 }
5290 changeWallpaperVisiblity(true);
5291 }
5292
5293 private void markIntroScreenDismissed() {
5294 SharedPreferences.Editor editor = mSharedPrefs.edit();
5295 editor.putBoolean(INTRO_SCREEN_DISMISSED, true);
5296 editor.apply();
5297 }
5298
Sunny Goyal88d60f12014-09-08 03:47:29 -07005299 private void showFirstRunClings() {
5300 // The two first run cling paths are mutually exclusive, if the launcher is preinstalled
5301 // on the device, then we always show the first run cling experience (or if there is no
5302 // launcher2). Otherwise, we prompt the user upon started for migration
5303 LauncherClings launcherClings = new LauncherClings(this);
5304 if (launcherClings.shouldShowFirstRunOrMigrationClings()) {
5305 if (mModel.canMigrateFromOldLauncherDb(this)) {
5306 launcherClings.showMigrationCling();
5307 } else {
Sunny Goyalaf0628f2014-09-19 10:14:48 -07005308 launcherClings.showLongPressCling(true);
Sunny Goyal88d60f12014-09-08 03:47:29 -07005309 }
5310 }
5311 }
5312
Winson Chunga6945242014-01-08 14:04:34 -08005313 void showWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005314 if (mWorkspace != null) mWorkspace.setAlpha(1f);
5315 if (mHotseat != null) mHotseat.setAlpha(1f);
5316 if (mPageIndicators != null) mPageIndicators.setAlpha(1f);
5317 if (mSearchDropTargetBar != null) mSearchDropTargetBar.showSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005318 }
5319
5320 void hideWorkspaceSearchAndHotseat() {
Winson Chung205cd772014-01-15 14:31:59 -08005321 if (mWorkspace != null) mWorkspace.setAlpha(0f);
5322 if (mHotseat != null) mHotseat.setAlpha(0f);
5323 if (mPageIndicators != null) mPageIndicators.setAlpha(0f);
5324 if (mSearchDropTargetBar != null) mSearchDropTargetBar.hideSearchBar(false);
Winson Chunga6945242014-01-08 14:04:34 -08005325 }
5326
Mathew Inwood72fbec12013-11-19 15:45:07 +00005327 public ItemInfo createAppDragInfo(Intent appLaunchIntent) {
Kenny Guyed131872014-04-30 03:02:21 +01005328 // Called from search suggestion, not supported in other profiles.
5329 final UserHandleCompat myUser = UserHandleCompat.myUserHandle();
5330 LauncherAppsCompat launcherApps = LauncherAppsCompat.getInstance(this);
5331 LauncherActivityInfoCompat activityInfo = launcherApps.resolveActivity(appLaunchIntent,
5332 myUser);
5333 if (activityInfo == null) {
Mathew Inwood72fbec12013-11-19 15:45:07 +00005334 return null;
5335 }
Kenny Guyed131872014-04-30 03:02:21 +01005336 return new AppInfo(this, activityInfo, myUser, mIconCache, null);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005337 }
5338
5339 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5340 Bitmap icon) {
Kenny Guyed131872014-04-30 03:02:21 +01005341 // Called from search suggestion, not supported in other profiles.
5342 return createShortcutDragInfo(shortcutIntent, caption, icon,
5343 UserHandleCompat.myUserHandle());
5344 }
5345
5346 public ItemInfo createShortcutDragInfo(Intent shortcutIntent, CharSequence caption,
5347 Bitmap icon, UserHandleCompat user) {
Kenny Guyc2bd8102014-06-30 12:30:31 +01005348 UserManagerCompat userManager = UserManagerCompat.getInstance(this);
Kenny Guyd6fe5262014-07-21 17:11:41 +01005349 CharSequence contentDescription = userManager.getBadgedLabelForUser(caption, user);
Kenny Guyc2bd8102014-06-30 12:30:31 +01005350 return new ShortcutInfo(shortcutIntent, caption, contentDescription, icon, user);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005351 }
5352
Mathew Inwoodb90860a2014-04-11 17:17:39 +01005353 protected void moveWorkspaceToDefaultScreen() {
5354 mWorkspace.moveToDefaultScreen(false);
5355 }
5356
Mathew Inwood72fbec12013-11-19 15:45:07 +00005357 public void startDrag(View dragView, ItemInfo dragInfo, DragSource source) {
5358 dragView.setTag(dragInfo);
Winson Chungbabb53e2014-04-14 17:12:49 -07005359 mWorkspace.onExternalDragStartedWithItem(dragView);
5360 mWorkspace.beginExternalDragShared(dragView, source);
Mathew Inwood72fbec12013-11-19 15:45:07 +00005361 }
5362
Anjali Koppalf5d29132014-02-28 13:33:27 -08005363 @Override
5364 public void onPageSwitch(View newPage, int newPageIndex) {
Adam Cohen9211d422014-10-07 18:14:53 -07005365 if (mLauncherCallbacks != null) {
5366 mLauncherCallbacks.onPageSwitch(newPage, newPageIndex);
5367 }
Anjali Koppalf5d29132014-02-28 13:33:27 -08005368 }
5369
Winson Chung80baf5a2010-08-09 16:03:15 -07005370 /**
Joe Onoratobe386092009-11-17 17:32:16 -08005371 * Prints out out state for debugging.
5372 */
5373 public void dumpState() {
Daniel Sandler325dc232013-06-05 22:57:57 -04005374 Log.d(TAG, "BEGIN launcher3 dump state for launcher " + this);
Joe Onorato39bfc132009-11-18 17:22:01 -08005375 Log.d(TAG, "mSavedState=" + mSavedState);
Joe Onorato39bfc132009-11-18 17:22:01 -08005376 Log.d(TAG, "mWorkspaceLoading=" + mWorkspaceLoading);
5377 Log.d(TAG, "mRestoring=" + mRestoring);
5378 Log.d(TAG, "mWaitingForResult=" + mWaitingForResult);
5379 Log.d(TAG, "mSavedInstanceState=" + mSavedInstanceState);
Brad Fitzpatrick319226a2010-09-01 13:45:16 -07005380 Log.d(TAG, "sFolders.size=" + sFolders.size());
Joe Onoratobe386092009-11-17 17:32:16 -08005381 mModel.dumpState();
Winson Chungf0ea4d32011-06-06 14:27:16 -07005382
Winson Chung785d2eb2011-04-14 16:08:02 -07005383 if (mAppsCustomizeContent != null) {
5384 mAppsCustomizeContent.dumpState();
5385 }
Daniel Sandler325dc232013-06-05 22:57:57 -04005386 Log.d(TAG, "END launcher3 dump state");
Joe Onoratobe386092009-11-17 17:32:16 -08005387 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005388
5389 @Override
5390 public void dump(String prefix, FileDescriptor fd, PrintWriter writer, String[] args) {
5391 super.dump(prefix, fd, writer, args);
Adam Cohen4caf2982013-08-20 18:54:31 -07005392 synchronized (sDumpLogs) {
5393 writer.println(" ");
5394 writer.println("Debug logs: ");
5395 for (int i = 0; i < sDumpLogs.size(); i++) {
5396 writer.println(" " + sDumpLogs.get(i));
5397 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005398 }
Adam Cohen9211d422014-10-07 18:14:53 -07005399 if (mLauncherCallbacks != null) {
5400 mLauncherCallbacks.dump(prefix, fd, writer, args);
5401 }
Adam Cohen16d7ffc2011-10-05 17:49:14 -07005402 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005403
5404 public static void dumpDebugLogsToConsole() {
Winson Chungede41292013-09-19 16:27:36 -07005405 if (DEBUG_DUMP_LOG) {
5406 synchronized (sDumpLogs) {
5407 Log.d(TAG, "");
5408 Log.d(TAG, "*********************");
5409 Log.d(TAG, "Launcher debug logs: ");
5410 for (int i = 0; i < sDumpLogs.size(); i++) {
5411 Log.d(TAG, " " + sDumpLogs.get(i));
5412 }
5413 Log.d(TAG, "*********************");
5414 Log.d(TAG, "");
Adam Cohen4caf2982013-08-20 18:54:31 -07005415 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005416 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005417 }
5418
5419 public static void addDumpLog(String tag, String log, boolean debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005420 addDumpLog(tag, log, null, debugLog);
5421 }
5422
5423 public static void addDumpLog(String tag, String log, Exception e, boolean debugLog) {
Adam Cohen4caf2982013-08-20 18:54:31 -07005424 if (debugLog) {
Dan Sandler295ae182013-12-10 16:05:47 -05005425 if (e != null) {
5426 Log.d(tag, log, e);
5427 } else {
5428 Log.d(tag, log);
5429 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005430 }
Winson Chungede41292013-09-19 16:27:36 -07005431 if (DEBUG_DUMP_LOG) {
5432 sDateStamp.setTime(System.currentTimeMillis());
5433 synchronized (sDumpLogs) {
Dan Sandler295ae182013-12-10 16:05:47 -05005434 sDumpLogs.add(sDateFormat.format(sDateStamp) + ": " + tag + ", " + log
5435 + (e == null ? "" : (", Exception: " + e)));
Adam Cohen4caf2982013-08-20 18:54:31 -07005436 }
Winson Chungede41292013-09-19 16:27:36 -07005437 }
Adam Cohen4caf2982013-08-20 18:54:31 -07005438 }
5439
Winson Chungede41292013-09-19 16:27:36 -07005440 public void dumpLogsToLocalData() {
5441 if (DEBUG_DUMP_LOG) {
Michael Jurka43467462013-11-14 12:03:58 +01005442 new AsyncTask<Void, Void, Void>() {
5443 public Void doInBackground(Void ... args) {
Winson Chungede41292013-09-19 16:27:36 -07005444 boolean success = false;
5445 sDateStamp.setTime(sRunStart);
5446 String FILENAME = sDateStamp.getMonth() + "-"
5447 + sDateStamp.getDay() + "_"
5448 + sDateStamp.getHours() + "-"
5449 + sDateStamp.getMinutes() + "_"
5450 + sDateStamp.getSeconds() + ".txt";
5451
5452 FileOutputStream fos = null;
5453 File outFile = null;
5454 try {
5455 outFile = new File(getFilesDir(), FILENAME);
5456 outFile.createNewFile();
5457 fos = new FileOutputStream(outFile);
5458 } catch (Exception e) {
5459 e.printStackTrace();
5460 }
5461 if (fos != null) {
5462 PrintWriter writer = new PrintWriter(fos);
5463
5464 writer.println(" ");
5465 writer.println("Debug logs: ");
5466 synchronized (sDumpLogs) {
5467 for (int i = 0; i < sDumpLogs.size(); i++) {
5468 writer.println(" " + sDumpLogs.get(i));
5469 }
5470 }
5471 writer.close();
5472 }
5473 try {
5474 if (fos != null) {
5475 fos.close();
5476 success = true;
5477 }
5478 } catch (IOException e) {
5479 e.printStackTrace();
5480 }
Michael Jurka43467462013-11-14 12:03:58 +01005481 return null;
Winson Chungede41292013-09-19 16:27:36 -07005482 }
Michael Jurka43467462013-11-14 12:03:58 +01005483 }.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, (Void) null);
Adam Cohen4caf2982013-08-20 18:54:31 -07005484 }
Adam Cohen487f7dd2012-06-28 18:12:10 -07005485 }
The Android Open Source Project31dd5032009-03-03 19:32:27 -08005486}
Michael Jurka2763be32011-02-24 11:19:57 -08005487
Michael Jurkaabded662011-03-04 12:06:57 -08005488interface LauncherTransitionable {
Michael Jurka2a4b1a82011-12-07 14:00:02 -08005489 View getContent();
Michael Jurkaa35e35a2012-04-26 15:04:28 -07005490 void onLauncherTransitionPrepare(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurkabed61d22012-02-14 22:51:29 -08005491 void onLauncherTransitionStart(Launcher l, boolean animated, boolean toWorkspace);
Winson Chung70442722012-02-10 15:43:22 -08005492 void onLauncherTransitionStep(Launcher l, float t);
Michael Jurkabed61d22012-02-14 22:51:29 -08005493 void onLauncherTransitionEnd(Launcher l, boolean animated, boolean toWorkspace);
Michael Jurka2763be32011-02-24 11:19:57 -08005494}
Dan Sandlerd5024042014-01-09 15:01:33 -05005495
5496interface DebugIntents {
5497 static final String DELETE_DATABASE = "com.android.launcher3.action.DELETE_DATABASE";
5498 static final String MIGRATE_DATABASE = "com.android.launcher3.action.MIGRATE_DATABASE";
Adam Cohen8b5b05b2014-01-10 21:42:39 +00005499}